1 /*
2  * SendButton - Add a Send button to the conversation window entry area.
3  * Copyright (C) 2008 Etan Reisner <deryni@pidgin.im>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301, USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #include "internal.h"
25 
26 #include "version.h"
27 
28 #include "pidgin.h"
29 
30 #include "gtkconv.h"
31 #include "gtkplugin.h"
32 
33 static void
send_button_cb(GtkButton * button,PidginConversation * gtkconv)34 send_button_cb(GtkButton *button, PidginConversation *gtkconv)
35 {
36 	g_signal_emit_by_name(gtkconv->entry, "message_send");
37 }
38 
39 static void
input_buffer_changed(GtkTextBuffer * text_buffer,GtkWidget * send_button)40 input_buffer_changed(GtkTextBuffer *text_buffer, GtkWidget *send_button)
41 {
42 	if (gtk_text_buffer_get_char_count(text_buffer) != 0)
43 		gtk_widget_set_sensitive(send_button, TRUE);
44 	else
45 		gtk_widget_set_sensitive(send_button, FALSE);
46 }
47 
48 static void
create_send_button_pidgin(PidginConversation * gtkconv)49 create_send_button_pidgin(PidginConversation *gtkconv)
50 {
51 	GtkWidget *send_button;
52 	GtkTextBuffer *buf;
53 	guint signal_id;
54 
55 	send_button = g_object_get_data(G_OBJECT(gtkconv->lower_hbox),
56 	                                "send_button");
57 
58 	if (send_button != NULL)
59 		return;
60 
61 	send_button = gtk_button_new_with_mnemonic(_("_Send"));
62 	g_signal_connect(G_OBJECT(send_button), "clicked",
63 	                 G_CALLBACK(send_button_cb), gtkconv);
64 	gtk_box_pack_end(GTK_BOX(gtkconv->lower_hbox), send_button, FALSE,
65 	                 FALSE, 0);
66 	gtk_widget_show(send_button);
67 
68 	buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(gtkconv->entry));
69 	if (buf) {
70 		signal_id = g_signal_connect(G_OBJECT(buf), "changed",
71 		                             G_CALLBACK(input_buffer_changed),
72 		                             send_button);
73 		g_object_set_data(G_OBJECT(send_button), "buffer-signal",
74 		                  GINT_TO_POINTER(signal_id));
75 		input_buffer_changed(buf, send_button);
76 	}
77 
78 	g_object_set_data(G_OBJECT(gtkconv->lower_hbox), "send_button",
79 	                  send_button);
80 }
81 
82 static void
remove_send_button_pidgin(PidginConversation * gtkconv)83 remove_send_button_pidgin(PidginConversation *gtkconv)
84 {
85 	GtkWidget *send_button = NULL;
86 
87 	send_button = g_object_get_data(G_OBJECT(gtkconv->lower_hbox),
88 	                                "send_button");
89 	if (send_button != NULL) {
90 		GtkTextBuffer *buf;
91 		guint signal_id;
92 
93 		buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(gtkconv->entry));
94 		signal_id = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(send_button),
95 		                                              "buffer-signal"));
96 		if (buf && signal_id)
97 			g_signal_handler_disconnect(G_OBJECT(buf), signal_id);
98 
99 		gtk_widget_destroy(send_button);
100 		g_object_set_data(G_OBJECT(gtkconv->lower_hbox),
101 		                  "send_button", NULL);
102 	}
103 }
104 
105 static void
conversation_displayed_cb(PidginConversation * gtkconv)106 conversation_displayed_cb(PidginConversation *gtkconv)
107 {
108 	GtkWidget *send_button = NULL;
109 
110 	send_button = g_object_get_data(G_OBJECT(gtkconv->lower_hbox),
111 	                                "send_button");
112 	if (send_button == NULL) {
113 		create_send_button_pidgin(gtkconv);
114 	}
115 }
116 
117 static gboolean
plugin_load(PurplePlugin * plugin)118 plugin_load(PurplePlugin *plugin)
119 {
120 	GList *convs = purple_get_conversations();
121 	void *gtk_conv_handle = pidgin_conversations_get_handle();
122 
123 	purple_signal_connect(gtk_conv_handle, "conversation-displayed", plugin,
124 	                      PURPLE_CALLBACK(conversation_displayed_cb), NULL);
125 	/*
126 	purple_signal_connect(gtk_conv_handle, "conversation-hiding", plugin,
127 	                      PURPLE_CALLBACK(conversation_hiding_cb), NULL);
128 	 */
129 
130 	while (convs) {
131 
132 		PurpleConversation *conv = (PurpleConversation *)convs->data;
133 
134 		/* Setup Send button */
135 		if (PIDGIN_IS_PIDGIN_CONVERSATION(conv)) {
136 			create_send_button_pidgin(PIDGIN_CONVERSATION(conv));
137 		}
138 
139 		convs = convs->next;
140 	}
141 
142 	return TRUE;
143 }
144 
145 static gboolean
plugin_unload(PurplePlugin * plugin)146 plugin_unload(PurplePlugin *plugin)
147 {
148 	GList *convs = purple_get_conversations();
149 
150 	while (convs) {
151 		PurpleConversation *conv = (PurpleConversation *)convs->data;
152 
153 		/* Remove Send button */
154 		if (PIDGIN_IS_PIDGIN_CONVERSATION(conv)) {
155 			remove_send_button_pidgin(PIDGIN_CONVERSATION(conv));
156 		}
157 
158 		convs = convs->next;
159 	}
160 
161 	return TRUE;
162 }
163 
164 static PurplePluginInfo info =
165 {
166 	PURPLE_PLUGIN_MAGIC,
167 	PURPLE_MAJOR_VERSION,                           /**< major version */
168 	PURPLE_MINOR_VERSION,                           /**< minor version */
169 	PURPLE_PLUGIN_STANDARD,                         /**< type */
170 	PIDGIN_PLUGIN_TYPE,                             /**< ui_requirement */
171 	0,                                              /**< flags */
172 	NULL,                                           /**< dependencies */
173 	PURPLE_PRIORITY_DEFAULT,                        /**< priority */
174 
175 	"gtksendbutton",                                /**< id */
176 	N_("Send Button"),                              /**< name */
177 	DISPLAY_VERSION,                                /**< version */
178 	N_("Conversation Window Send Button."),         /**< summary */
179 	N_("Adds a Send button to the entry area of "
180 	   "the conversation window. Intended for use "
181 	   "when no physical keyboard is present."),    /**< description */
182 	"Etan Reisner <deryni@pidgin.im>",              /**< author */
183 	PURPLE_WEBSITE,                                 /**< homepage */
184 	plugin_load,                                    /**< load */
185 	plugin_unload,                                  /**< unload */
186 	NULL,                                           /**< destroy */
187 	NULL,                                           /**< ui_info */
188 	NULL,                                           /**< extra_info */
189 	NULL,                                           /**< prefs_info */
190 	NULL,                                           /**< actions */
191 
192 	/* padding */
193 	NULL,
194 	NULL,
195 	NULL,
196 	NULL
197 };
198 
199 static void
init_plugin(PurplePlugin * plugin)200 init_plugin(PurplePlugin *plugin)
201 {
202 }
203 
204 PURPLE_INIT_PLUGIN(sendbutton, init_plugin, info)
205