1 /*
2  * pidgin - Windows Pidgin Options Plugin
3  *
4  * Pidgin is the legal property of its developers, whose names are too numerous
5  * to list here.  Please refer to the COPYRIGHT file distributed with this
6  * source distribution.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
21  *
22  */
23 #include <gtk/gtk.h>
24 #include <gdk/gdkwin32.h>
25 
26 #include "internal.h"
27 
28 #include "gtkwin32dep.h"
29 
30 #include "core.h"
31 #include "debug.h"
32 #include "prefs.h"
33 #include "signals.h"
34 #include "version.h"
35 
36 #include "gtkappbar.h"
37 #include "gtkblist.h"
38 #include "gtkconv.h"
39 #include "gtkplugin.h"
40 #include "gtkprefs.h"
41 #include "gtkutils.h"
42 
43 /*
44  *  MACROS & DEFINES
45  */
46 #define WINPREFS_PLUGIN_ID "gtk-win-prefs"
47 
48 #define RUNKEY "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"
49 
50 /*
51  *  LOCALS
52  */
53 static const char *PREF_DBLIST_DOCKABLE = "/plugins/gtk/win32/winprefs/dblist_dockable";
54 static const char *PREF_DBLIST_DOCKED = "/plugins/gtk/win32/winprefs/dblist_docked";
55 static const char *PREF_DBLIST_HEIGHT = "/plugins/gtk/win32/winprefs/dblist_height";
56 static const char *PREF_DBLIST_SIDE = "/plugins/gtk/win32/winprefs/dblist_side";
57 static const char *PREF_BLIST_ON_TOP = "/plugins/gtk/win32/winprefs/blist_on_top";
58 /* Deprecated */
59 static const char *PREF_CHAT_BLINK = "/plugins/gtk/win32/winprefs/chat_blink";
60 static const char *PREF_DBLIST_ON_TOP = "/plugins/gtk/win32/winprefs/dblist_on_top";
61 
62 static PurplePlugin *handle = NULL;
63 static GtkAppBar *blist_ab = NULL;
64 static GtkWidget *blist = NULL;
65 static guint blist_visible_cb_id = 0;
66 
67 enum {
68 	BLIST_TOP_NEVER = 0,
69 	BLIST_TOP_ALWAYS,
70 	BLIST_TOP_DOCKED,
71 };
72 
73 /*
74  *  CODE
75  */
76 
77 /* BLIST DOCKING */
78 
blist_save_state()79 static void blist_save_state() {
80 	if(blist_ab) {
81 		if(purple_prefs_get_bool(PREF_DBLIST_DOCKABLE) && blist_ab->docked) {
82 			purple_prefs_set_int(PREF_DBLIST_HEIGHT, blist_ab->undocked_height);
83 			purple_prefs_set_int(PREF_DBLIST_SIDE, blist_ab->side);
84 			purple_prefs_set_bool(PREF_DBLIST_DOCKED, blist_ab->docked);
85 		} else
86 			purple_prefs_set_bool(PREF_DBLIST_DOCKED, FALSE);
87 	}
88 }
89 
blist_set_ontop(gboolean val)90 static void blist_set_ontop(gboolean val) {
91 	if(!blist)
92 		return;
93 
94 	gtk_window_set_keep_above(GTK_WINDOW(PIDGIN_BLIST(purple_get_blist())->window), val);
95 }
96 
blist_dock_cb(gboolean val)97 static void blist_dock_cb(gboolean val) {
98 	if(val) {
99 		purple_debug_info(WINPREFS_PLUGIN_ID, "Blist Docking...\n");
100 		if(purple_prefs_get_int(PREF_BLIST_ON_TOP) != BLIST_TOP_NEVER)
101 			blist_set_ontop(TRUE);
102 	} else {
103 		purple_debug_info(WINPREFS_PLUGIN_ID, "Blist Undocking...\n");
104 		blist_set_ontop(purple_prefs_get_int(PREF_BLIST_ON_TOP) == BLIST_TOP_ALWAYS);
105 	}
106 }
107 
blist_set_dockable(gboolean val)108 static void blist_set_dockable(gboolean val) {
109 	if(val) {
110 		if(blist_ab == NULL && blist != NULL) {
111 			blist_ab = gtk_appbar_add(blist);
112 			gtk_appbar_add_dock_cb(blist_ab, blist_dock_cb);
113 		}
114 	} else {
115 		if(blist_ab != NULL) {
116 			gtk_appbar_remove(blist_ab);
117 			blist_ab = NULL;
118 		}
119 
120 		blist_set_ontop(purple_prefs_get_int(PREF_BLIST_ON_TOP) == BLIST_TOP_ALWAYS);
121 	}
122 }
123 
124 /* PLUGIN CALLBACKS */
125 
126 /* We need this because the blist destroy cb won't be called before the
127    plugin is unloaded, when quitting */
purple_quit_cb()128 static void purple_quit_cb() {
129 	purple_debug_info(WINPREFS_PLUGIN_ID, "purple_quit_cb: removing appbar\n");
130 	blist_save_state();
131 	blist_set_dockable(FALSE);
132 }
133 
134 /* Listen for the first time the window stops being withdrawn */
blist_visible_cb(const char * pref,PurplePrefType type,gconstpointer value,gpointer user_data)135 static void blist_visible_cb(const char *pref, PurplePrefType type,
136 		gconstpointer value, gpointer user_data) {
137 	if(purple_prefs_get_bool(pref)) {
138 		gtk_appbar_dock(blist_ab,
139 			purple_prefs_get_int(PREF_DBLIST_SIDE));
140 
141 		if(purple_prefs_get_int(PREF_BLIST_ON_TOP)
142 				== BLIST_TOP_DOCKED)
143 			blist_set_ontop(TRUE);
144 
145 		/* We only need to be notified about this once */
146 		purple_prefs_disconnect_callback(blist_visible_cb_id);
147 	}
148 }
149 
150 /* This needs to be delayed otherwise, when the blist is originally created and
151  * hidden, it'll trigger the blist_visible_cb */
listen_for_blist_visible_cb(gpointer data)152 static gboolean listen_for_blist_visible_cb(gpointer data) {
153 	if (handle != NULL)
154 		blist_visible_cb_id =
155 			purple_prefs_connect_callback(handle,
156 				PIDGIN_PREFS_ROOT "/blist/list_visible",
157 				blist_visible_cb, NULL);
158 
159 	return FALSE;
160 }
161 
blist_create_cb(PurpleBuddyList * purple_blist,void * data)162 static void blist_create_cb(PurpleBuddyList *purple_blist, void *data) {
163 	purple_debug_info(WINPREFS_PLUGIN_ID, "buddy list created\n");
164 
165 	blist = PIDGIN_BLIST(purple_blist)->window;
166 
167 	if(purple_prefs_get_bool(PREF_DBLIST_DOCKABLE)) {
168 		blist_set_dockable(TRUE);
169 		if(purple_prefs_get_bool(PREF_DBLIST_DOCKED)) {
170 			blist_ab->undocked_height = purple_prefs_get_int(PREF_DBLIST_HEIGHT);
171 			if(!(gdk_window_get_state(blist->window)
172 					& GDK_WINDOW_STATE_WITHDRAWN)) {
173 				gtk_appbar_dock(blist_ab,
174 					purple_prefs_get_int(PREF_DBLIST_SIDE));
175 				if(purple_prefs_get_int(PREF_BLIST_ON_TOP)
176 						== BLIST_TOP_DOCKED)
177 					blist_set_ontop(TRUE);
178 			} else {
179 				g_idle_add(listen_for_blist_visible_cb, NULL);
180 			}
181 		}
182 	}
183 
184 	if(purple_prefs_get_int(PREF_BLIST_ON_TOP) == BLIST_TOP_ALWAYS)
185 		blist_set_ontop(TRUE);
186 
187 }
188 
189 /* WIN PREFS GENERAL */
190 
191 static void
winprefs_set_autostart(GtkWidget * w)192 winprefs_set_autostart(GtkWidget *w) {
193 	char *runval = NULL;
194 
195 	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)))
196 		runval = g_strdup_printf("\"%s" G_DIR_SEPARATOR_S "pidgin.exe\"", wpurple_install_dir());
197 
198 	if(!wpurple_write_reg_string(HKEY_CURRENT_USER, RUNKEY, "Pidgin", runval)
199 		/* For Win98 */
200 		&& !wpurple_write_reg_string(HKEY_LOCAL_MACHINE, RUNKEY, "Pidgin", runval))
201 			purple_debug_error(WINPREFS_PLUGIN_ID, "Could not set registry key value\n");
202 
203 	g_free(runval);
204 }
205 
206 static void
winprefs_set_multiple_instances(GtkWidget * w)207 winprefs_set_multiple_instances(GtkWidget *w) {
208 	wpurple_write_reg_string(HKEY_CURRENT_USER, "Environment", "PIDGIN_MULTI_INST",
209 			gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)) ? "1" : NULL);
210 }
211 
212 static void
winprefs_set_blist_dockable(const char * pref,PurplePrefType type,gconstpointer value,gpointer user_data)213 winprefs_set_blist_dockable(const char *pref, PurplePrefType type,
214 		gconstpointer value, gpointer user_data)
215 {
216 	blist_set_dockable(GPOINTER_TO_INT(value));
217 }
218 
219 static void
winprefs_set_blist_ontop(const char * pref,PurplePrefType type,gconstpointer value,gpointer user_data)220 winprefs_set_blist_ontop(const char *pref, PurplePrefType type,
221 		gconstpointer value, gpointer user_data)
222 {
223 	gint setting = purple_prefs_get_int(PREF_BLIST_ON_TOP);
224 	if((setting == BLIST_TOP_DOCKED && blist_ab && blist_ab->docked)
225 		|| setting == BLIST_TOP_ALWAYS)
226 		blist_set_ontop(TRUE);
227 	else
228 		blist_set_ontop(FALSE);
229 }
230 
231 /*
232  *  EXPORTED FUNCTIONS
233  */
234 
plugin_load(PurplePlugin * plugin)235 static gboolean plugin_load(PurplePlugin *plugin) {
236 	handle = plugin;
237 
238 	/* blist docking init */
239 	if(purple_get_blist() && PIDGIN_BLIST(purple_get_blist())
240 			&& PIDGIN_BLIST(purple_get_blist())->window) {
241 		blist_create_cb(purple_get_blist(), NULL);
242 	}
243 
244 	/* This really shouldn't happen anymore generally, but if for some strange
245 	   reason, the blist is recreated, we need to set it up again. */
246 	purple_signal_connect(pidgin_blist_get_handle(), "gtkblist-created",
247 		plugin, PURPLE_CALLBACK(blist_create_cb), NULL);
248 
249 	purple_signal_connect((void*)purple_get_core(), "quitting", plugin,
250 		PURPLE_CALLBACK(purple_quit_cb), NULL);
251 
252 	purple_prefs_connect_callback(handle, PREF_BLIST_ON_TOP,
253 		winprefs_set_blist_ontop, NULL);
254 	purple_prefs_connect_callback(handle, PREF_DBLIST_DOCKABLE,
255 		winprefs_set_blist_dockable, NULL);
256 
257 	return TRUE;
258 }
259 
plugin_unload(PurplePlugin * plugin)260 static gboolean plugin_unload(PurplePlugin *plugin) {
261 	blist_set_dockable(FALSE);
262 	blist_set_ontop(FALSE);
263 
264 	handle = NULL;
265 
266 	return TRUE;
267 }
268 
get_config_frame(PurplePlugin * plugin)269 static GtkWidget* get_config_frame(PurplePlugin *plugin) {
270 	GtkWidget *ret;
271 	GtkWidget *vbox;
272 	GtkWidget *button;
273 	char *run_key_val;
274 	char *tmp;
275 
276 	ret = gtk_vbox_new(FALSE, 18);
277 	gtk_container_set_border_width(GTK_CONTAINER(ret), 12);
278 
279 	/* Autostart */
280 	vbox = pidgin_make_frame(ret, _("Startup"));
281 	tmp = g_strdup_printf(_("_Start %s on Windows startup"), PIDGIN_NAME);
282 	button = gtk_check_button_new_with_mnemonic(tmp);
283 	g_free(tmp);
284 	gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
285 	if ((run_key_val = wpurple_read_reg_string(HKEY_CURRENT_USER, RUNKEY, "Pidgin"))
286 			|| (run_key_val = wpurple_read_reg_string(HKEY_LOCAL_MACHINE, RUNKEY, "Pidgin"))) {
287 		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE);
288 		g_free(run_key_val);
289 	}
290 	g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(winprefs_set_autostart), NULL);
291 	gtk_widget_show(button);
292 
293 	button = gtk_check_button_new_with_mnemonic(_("Allow multiple instances"));
294 	gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
295 	if ((run_key_val = wpurple_read_reg_string(HKEY_CURRENT_USER, "Environment", "PIDGIN_MULTI_INST"))) {
296 		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE);
297 		g_free(run_key_val);
298 	}
299 	g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(winprefs_set_multiple_instances), NULL);
300 	gtk_widget_show(button);
301 
302 	/* Buddy List */
303 	vbox = pidgin_make_frame(ret, _("Buddy List"));
304 	pidgin_prefs_checkbox(_("_Dockable Buddy List"),
305 							PREF_DBLIST_DOCKABLE, vbox);
306 
307 	/* Blist On Top */
308 	pidgin_prefs_dropdown(vbox, _("_Keep Buddy List window on top:"),
309 		PURPLE_PREF_INT, PREF_BLIST_ON_TOP,
310 		_("Never"), BLIST_TOP_NEVER,
311 		_("Always"), BLIST_TOP_ALWAYS,
312 		/* XXX: Did this ever work? */
313 		_("Only when docked"), BLIST_TOP_DOCKED,
314 		NULL);
315 
316 	gtk_widget_show_all(ret);
317 	return ret;
318 }
319 
320 static PidginPluginUiInfo ui_info =
321 {
322 	get_config_frame,
323 	0,
324 
325 	/* padding */
326 	NULL,
327 	NULL,
328 	NULL,
329 	NULL
330 };
331 
332 static PurplePluginInfo info =
333 {
334 	PURPLE_PLUGIN_MAGIC,
335 	PURPLE_MAJOR_VERSION,
336 	PURPLE_MINOR_VERSION,
337 	PURPLE_PLUGIN_STANDARD,
338 	PIDGIN_PLUGIN_TYPE,
339 	0,
340 	NULL,
341 	PURPLE_PRIORITY_DEFAULT,
342 	WINPREFS_PLUGIN_ID,
343 	N_("Windows Pidgin Options"),
344 	DISPLAY_VERSION,
345 	N_("Options specific to Pidgin for Windows."),
346 	N_("Provides options specific to Pidgin for Windows, such as buddy list docking."),
347 	"Herman Bloggs <hermanator12002@yahoo.com>",
348 	PURPLE_WEBSITE,
349 	plugin_load,
350 	plugin_unload,
351 	NULL,
352 	&ui_info,
353 	NULL,
354 	NULL,
355 	NULL,
356 
357 	/* padding */
358 	NULL,
359 	NULL,
360 	NULL,
361 	NULL
362 };
363 
364 static void
init_plugin(PurplePlugin * plugin)365 init_plugin(PurplePlugin *plugin)
366 {
367 	purple_prefs_add_none("/plugins/gtk");
368 	purple_prefs_add_none("/plugins/gtk/win32");
369 	purple_prefs_add_none("/plugins/gtk/win32/winprefs");
370 	purple_prefs_add_bool(PREF_DBLIST_DOCKABLE, FALSE);
371 	purple_prefs_add_bool(PREF_DBLIST_DOCKED, FALSE);
372 	purple_prefs_add_int(PREF_DBLIST_HEIGHT, 0);
373 	purple_prefs_add_int(PREF_DBLIST_SIDE, 0);
374 
375 	/* Convert old preferences */
376 	if(purple_prefs_exists(PREF_DBLIST_ON_TOP)) {
377 		gint blist_top = BLIST_TOP_NEVER;
378 		if(purple_prefs_get_bool(PREF_BLIST_ON_TOP))
379 			blist_top = BLIST_TOP_ALWAYS;
380 		else if(purple_prefs_get_bool(PREF_DBLIST_ON_TOP))
381 			blist_top = BLIST_TOP_DOCKED;
382 		purple_prefs_remove(PREF_BLIST_ON_TOP);
383 		purple_prefs_remove(PREF_DBLIST_ON_TOP);
384 		purple_prefs_add_int(PREF_BLIST_ON_TOP, blist_top);
385 	} else
386 		purple_prefs_add_int(PREF_BLIST_ON_TOP, BLIST_TOP_NEVER);
387 	purple_prefs_remove(PREF_CHAT_BLINK);
388 }
389 
390 PURPLE_INIT_PLUGIN(winprefs, init_plugin, info)
391 
392