comparison gtk/plugins/win32/winprefs/winprefs.c @ 14224:ab8a105eff62

[gaim-migrate @ 16905] First step of getting wingaim working again. libgaim and gtk are compiling. The protocols aren't compiling yet. There are a number of things that are compiling, but should be cleaned up. committer: Tailor Script <tailor@pidgin.im>
author Daniel Atallah <daniel.atallah@gmail.com>
date Sun, 20 Aug 2006 16:49:37 +0000
parents
children 7635195195c0
comparison
equal deleted inserted replaced
14223:7c560c01b8f9 14224:ab8a105eff62
1 /*
2 * gaim - WinGaim Options Plugin
3 *
4 * Gaim 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 /*
49 * LOCALS
50 */
51 static const char *PREF_DBLIST_DOCKABLE = "/plugins/gtk/win32/winprefs/dblist_dockable";
52 static const char *PREF_DBLIST_DOCKED = "/plugins/gtk/win32/winprefs/dblist_docked";
53 static const char *PREF_DBLIST_HEIGHT = "/plugins/gtk/win32/winprefs/dblist_height";
54 static const char *PREF_DBLIST_SIDE = "/plugins/gtk/win32/winprefs/dblist_side";
55 static const char *PREF_BLIST_ON_TOP = "/plugins/gtk/win32/winprefs/blist_on_top";
56 static const char *PREF_IM_BLINK = "/plugins/gtk/win32/winprefs/im_blink";
57 static const char *PREF_CHAT_BLINK = "/plugins/gtk/win32/winprefs/chat_blink";
58
59 /* Deprecated */
60 static const char *PREF_DBLIST_ON_TOP = "/plugins/gtk/win32/winprefs/dblist_on_top";
61
62 static GaimPlugin *handle = NULL;
63 static GtkAppBar *blist_ab = NULL;
64 static GtkWidget *blist = NULL;
65 static guint blist_visible_cb_id = 0;
66
67 /* flash info */
68
69 struct _WGAIM_FLASH_INFO {
70 guint t_handle;
71 guint sig_handler;
72 };
73
74 enum {
75 BLIST_TOP_NEVER = 0,
76 BLIST_TOP_ALWAYS,
77 BLIST_TOP_DOCKED,
78 };
79
80 typedef struct _WGAIM_FLASH_INFO WGAIM_FLASH_INFO;
81
82 typedef BOOL (CALLBACK* LPFNFLASHWINDOWEX)(PFLASHWINFO);
83
84 static LPFNFLASHWINDOWEX MyFlashWindowEx = NULL;
85
86 /*
87 * CODE
88 */
89
90 /* BLIST DOCKING */
91
92 static void blist_save_state() {
93 if(blist_ab) {
94 if(gaim_prefs_get_bool(PREF_DBLIST_DOCKABLE) && blist_ab->docked) {
95 gaim_prefs_set_int(PREF_DBLIST_HEIGHT, blist_ab->undocked_height);
96 gaim_prefs_set_int(PREF_DBLIST_SIDE, blist_ab->side);
97 gaim_prefs_set_bool(PREF_DBLIST_DOCKED, blist_ab->docked);
98 } else
99 gaim_prefs_set_bool(PREF_DBLIST_DOCKED, FALSE);
100 }
101 }
102
103 static void blist_set_ontop(gboolean val) {
104 if(!blist)
105 return;
106
107 if(val)
108 SetWindowPos(GDK_WINDOW_HWND(blist->window), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
109 else
110 SetWindowPos(GDK_WINDOW_HWND(blist->window), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
111
112 }
113
114 static void blist_dock_cb(gboolean val) {
115 if(val) {
116 gaim_debug_info(WINPREFS_PLUGIN_ID, "Blist Docking...\n");
117 if(gaim_prefs_get_int(PREF_BLIST_ON_TOP) != BLIST_TOP_NEVER)
118 blist_set_ontop(TRUE);
119 } else {
120 gaim_debug_info(WINPREFS_PLUGIN_ID, "Blist Undocking...\n");
121 if(gaim_prefs_get_int(PREF_BLIST_ON_TOP) == BLIST_TOP_ALWAYS)
122 blist_set_ontop(TRUE);
123 else
124 blist_set_ontop(FALSE);
125 }
126 }
127
128 static void blist_set_dockable(gboolean val) {
129 if(val) {
130 if(blist_ab == NULL && blist != NULL) {
131 blist_ab = gtk_appbar_add(blist);
132 gtk_appbar_add_dock_cb(blist_ab, blist_dock_cb);
133 }
134 } else {
135 if(blist_ab != NULL) {
136 gtk_appbar_remove(blist_ab);
137 blist_ab = NULL;
138 }
139
140 if(gaim_prefs_get_int(PREF_BLIST_ON_TOP) == BLIST_TOP_ALWAYS)
141 blist_set_ontop(TRUE);
142 else
143 blist_set_ontop(FALSE);
144 }
145 }
146
147 /* PLUGIN CALLBACKS */
148
149 /* We need this because the blist destroy cb won't be called before the
150 plugin is unloaded, when quitting */
151 static void gaim_quit_cb() {
152 gaim_debug_info(WINPREFS_PLUGIN_ID, "gaim_quit_cb: removing appbar\n");
153 blist_save_state();
154 blist_set_dockable(FALSE);
155 }
156
157 /* Listen for the first time the window stops being withdrawn */
158 static void blist_visible_cb(const char *pref, GaimPrefType type,
159 gconstpointer value, gpointer user_data) {
160 if(gaim_prefs_get_bool(pref)) {
161 gtk_appbar_dock(blist_ab,
162 gaim_prefs_get_int(PREF_DBLIST_SIDE));
163
164 if(gaim_prefs_get_int(PREF_BLIST_ON_TOP)
165 == BLIST_TOP_DOCKED)
166 blist_set_ontop(TRUE);
167
168 /* We only need to be notified about this once */
169 gaim_prefs_disconnect_callback(blist_visible_cb_id);
170 }
171 }
172
173 /* This needs to be delayed otherwise, when the blist is originally created and
174 * hidden, it'll trigger the blist_visible_cb */
175 static gboolean listen_for_blist_visible_cb(gpointer data) {
176 if (handle != NULL)
177 blist_visible_cb_id =
178 gaim_prefs_connect_callback(handle,
179 "/gaim/gtk/blist/list_visible",
180 blist_visible_cb, NULL);
181
182 return FALSE;
183 }
184
185 static void blist_create_cb(GaimBuddyList *gaim_blist, void *data) {
186 gaim_debug_info(WINPREFS_PLUGIN_ID, "buddy list created\n");
187
188 blist = GAIM_GTK_BLIST(gaim_blist)->window;
189
190 if(gaim_prefs_get_bool(PREF_DBLIST_DOCKABLE)) {
191 blist_set_dockable(TRUE);
192 if(gaim_prefs_get_bool(PREF_DBLIST_DOCKED)) {
193 blist_ab->undocked_height = gaim_prefs_get_int(PREF_DBLIST_HEIGHT);
194 if(!(gdk_window_get_state(blist->window)
195 & GDK_WINDOW_STATE_WITHDRAWN)) {
196 gtk_appbar_dock(blist_ab,
197 gaim_prefs_get_int(PREF_DBLIST_SIDE));
198 if(gaim_prefs_get_int(PREF_BLIST_ON_TOP)
199 == BLIST_TOP_DOCKED)
200 blist_set_ontop(TRUE);
201 } else {
202 g_idle_add(listen_for_blist_visible_cb, NULL);
203 }
204 }
205 }
206
207 if(gaim_prefs_get_int(PREF_BLIST_ON_TOP) == BLIST_TOP_ALWAYS)
208 blist_set_ontop(TRUE);
209
210 }
211
212 /* AUTOSTART */
213
214 static gboolean open_run_key(PHKEY phKey, REGSAM samDesired) {
215 /* First try current user key (for WinNT & Win2k +), fall back to local machine */
216 if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER,
217 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
218 0, samDesired, phKey));
219 else if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,
220 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
221 0, samDesired, phKey));
222 else {
223 gaim_debug_error(WINPREFS_PLUGIN_ID, "open_run_key: Could not open key for writing value\n");
224 return FALSE;
225 }
226 return TRUE;
227 }
228
229 /* WIN PREFS GENERAL */
230
231 static void
232 winprefs_set_autostart(GtkWidget *w)
233 {
234 HKEY hKey;
235
236 if(!open_run_key(&hKey, KEY_SET_VALUE))
237 return;
238 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) {
239 char buffer[1024];
240 DWORD size;
241
242 if((size = GetModuleFileName(gtkwgaim_hinstance(),
243 (LPBYTE) buffer , sizeof(buffer))) == 0) {
244 gaim_debug_error(WINPREFS_PLUGIN_ID, "GetModuleFileName Error. Could not set Gaim autostart.\n");
245 RegCloseKey(hKey);
246 return;
247 }
248
249 /* Now set value of new key */
250 if(ERROR_SUCCESS != RegSetValueEx(hKey, "Gaim", 0, REG_SZ, buffer, size))
251 gaim_debug_error(WINPREFS_PLUGIN_ID, "Could not set registry key value\n");
252 } else {
253 if(ERROR_SUCCESS != RegDeleteValue(hKey, "Gaim"))
254 gaim_debug_error(WINPREFS_PLUGIN_ID, "Could not delete registry key value\n");
255 }
256 RegCloseKey(hKey);
257 }
258
259 static void
260 winprefs_set_blist_dockable(const char *pref, GaimPrefType type,
261 gconstpointer value, gpointer user_data)
262 {
263 blist_set_dockable(GPOINTER_TO_INT(value));
264 }
265
266 static void
267 winprefs_set_blist_ontop(const char *pref, GaimPrefType type,
268 gconstpointer value, gpointer user_data)
269 {
270 gint setting = gaim_prefs_get_int(PREF_BLIST_ON_TOP);
271 if((setting == BLIST_TOP_DOCKED && blist_ab && blist_ab->docked)
272 || setting == BLIST_TOP_ALWAYS)
273 blist_set_ontop(TRUE);
274 else
275 blist_set_ontop(FALSE);
276 }
277
278 static void load_winver_specific_procs(void) {
279 /* Used for Win98+ and WinNT5+ */
280 MyFlashWindowEx = (LPFNFLASHWINDOWEX) wgaim_find_and_loadproc("user32.dll", "FlashWindowEx");
281 }
282
283 /* Window flasher */
284 static gboolean flash_window_cb(gpointer data) {
285 FlashWindow((HWND) data, TRUE);
286 return TRUE;
287 }
288
289 static int
290 halt_flash_filter(GtkWidget *widget, GdkEventFocus *event, gpointer data)
291 {
292 if(MyFlashWindowEx) {
293 HWND hWnd = data;
294 FLASHWINFO info;
295
296 if(!IsWindow(hWnd))
297 return 0;
298
299 memset(&info, 0, sizeof(FLASHWINFO));
300 info.cbSize = sizeof(FLASHWINFO);
301 info.hwnd = hWnd;
302 info.dwFlags = FLASHW_STOP;
303 info.dwTimeout = 0;
304 MyFlashWindowEx(&info);
305
306 } else {
307 WGAIM_FLASH_INFO *finfo = data;
308 /* Stop flashing and remove filter */
309 gaim_debug(GAIM_DEBUG_INFO, "wgaim", "Removing timeout\n");
310 gaim_timeout_remove(finfo->t_handle);
311 gaim_debug(GAIM_DEBUG_INFO, "wgaim", "Disconnecting signal handler\n");
312 g_signal_handler_disconnect(G_OBJECT(widget),
313 finfo->sig_handler);
314 gaim_debug(GAIM_DEBUG_INFO, "wgaim", "done\n");
315 g_free(finfo);
316 }
317 return 0;
318 }
319
320 /* FlashWindowEx is only supported by Win98+ and WinNT5+. If it's
321 not supported we do it our own way */
322 static gboolean
323 wgaim_conv_blink(GaimConversation *conv, int flags)
324 {
325 GaimGtkWindow *win;
326 GtkWidget *window;
327
328 /* Don't flash for our own messages or system messages */
329 if(flags & GAIM_MESSAGE_SEND || flags & GAIM_MESSAGE_SYSTEM)
330 return FALSE;
331
332 if(conv == NULL) {
333 gaim_debug_info("winprefs", "gar!\n");
334 return FALSE;
335 }
336 win = gaim_gtkconv_get_window(GAIM_GTK_CONVERSATION(conv));
337 if(win == NULL) {
338 gaim_debug_info("winprefs", "gar2!\n");
339 return FALSE;
340 }
341 window = win->window;
342
343 if(MyFlashWindowEx) {
344 FLASHWINFO info;
345 if(GetForegroundWindow() == GDK_WINDOW_HWND(window->window))
346 return FALSE;
347
348 memset(&info, 0, sizeof(FLASHWINFO));
349 info.cbSize = sizeof(FLASHWINFO);
350 info.hwnd = GDK_WINDOW_HWND(window->window);
351 info.dwFlags = FLASHW_ALL | FLASHW_TIMER;
352 info.dwTimeout = 0;
353 MyFlashWindowEx(&info);
354 /* Stop flashing when window receives focus */
355 g_signal_connect(G_OBJECT(window), "focus-in-event",
356 G_CALLBACK(halt_flash_filter), info.hwnd);
357 } else {
358 WGAIM_FLASH_INFO *finfo = g_new0(WGAIM_FLASH_INFO, 1);
359
360 /* Start Flashing window */
361 finfo->t_handle = gaim_timeout_add(1000, flash_window_cb,
362 GDK_WINDOW_HWND(window->window));
363 finfo->sig_handler = g_signal_connect(G_OBJECT(window),
364 "focus-in-event", G_CALLBACK(halt_flash_filter), finfo);
365 }
366
367 return FALSE;
368 }
369
370 static gboolean
371 wgaim_conv_im_blink(GaimAccount *account, const char *who, char **message,
372 GaimConversation *conv, int flags, void *data)
373 {
374 if(!gaim_prefs_get_bool(PREF_IM_BLINK))
375 return FALSE;
376
377 return wgaim_conv_blink(conv, flags);
378
379 }
380
381 static gboolean
382 wgaim_conv_chat_blink(GaimAccount *account, const char *who, char **message,
383 GaimConversation *conv, int flags, void *data)
384 {
385 if(!gaim_prefs_get_bool(PREF_CHAT_BLINK))
386 return FALSE;
387
388 return wgaim_conv_blink(conv, flags);
389 }
390
391
392 /*
393 * EXPORTED FUNCTIONS
394 */
395
396 static gboolean plugin_load(GaimPlugin *plugin) {
397 /* Find out how to go blinky */
398 load_winver_specific_procs();
399
400 handle = plugin;
401
402 /* blist docking init */
403 if(gaim_get_blist() && GAIM_GTK_BLIST(gaim_get_blist())
404 && GAIM_GTK_BLIST(gaim_get_blist())->window) {
405 blist_create_cb(gaim_get_blist(), NULL);
406 }
407
408 /* This really shouldn't happen anymore generally, but if for some strange
409 reason, the blist is recreated, we need to set it up again. */
410 gaim_signal_connect(gaim_gtk_blist_get_handle(), "gtkblist-created",
411 plugin, GAIM_CALLBACK(blist_create_cb), NULL);
412
413 gaim_signal_connect(gaim_gtk_conversations_get_handle(),
414 "displaying-im-msg", plugin, GAIM_CALLBACK(wgaim_conv_im_blink),
415 NULL);
416
417 gaim_signal_connect(gaim_gtk_conversations_get_handle(),
418 "displaying-chat-msg", plugin, GAIM_CALLBACK(wgaim_conv_chat_blink),
419 NULL);
420
421 gaim_signal_connect((void*)gaim_get_core(), "quitting", plugin,
422 GAIM_CALLBACK(gaim_quit_cb), NULL);
423
424 gaim_prefs_connect_callback(handle, PREF_BLIST_ON_TOP,
425 winprefs_set_blist_ontop, NULL);
426 gaim_prefs_connect_callback(handle, PREF_DBLIST_DOCKABLE,
427 winprefs_set_blist_dockable, NULL);
428
429 return TRUE;
430 }
431
432 static gboolean plugin_unload(GaimPlugin *plugin) {
433 blist_set_dockable(FALSE);
434 blist_set_ontop(FALSE);
435
436 handle = NULL;
437
438 return TRUE;
439 }
440
441 static GtkWidget* get_config_frame(GaimPlugin *plugin) {
442 GtkWidget *ret;
443 GtkWidget *vbox;
444 GtkWidget *button;
445 char* gtk_version = NULL;
446 HKEY hKey;
447
448 ret = gtk_vbox_new(FALSE, 18);
449 gtk_container_set_border_width(GTK_CONTAINER(ret), 12);
450
451 gtk_version = g_strdup_printf("GTK+\t%u.%u.%u\nGlib\t%u.%u.%u",
452 gtk_major_version, gtk_minor_version, gtk_micro_version,
453 glib_major_version, glib_minor_version, glib_micro_version);
454
455 /* Display Installed GTK+ Runtime Version */
456 if(gtk_version) {
457 GtkWidget *label;
458 vbox = gaim_gtk_make_frame(ret, _("GTK+ Runtime Version"));
459 label = gtk_label_new(gtk_version);
460 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
461 gtk_widget_show(label);
462 g_free(gtk_version);
463 }
464
465 /* Autostart */
466 vbox = gaim_gtk_make_frame(ret, _("Startup"));
467 button = gtk_check_button_new_with_mnemonic(_("_Start Gaim on Windows startup"));
468 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
469 if(open_run_key(&hKey, KEY_QUERY_VALUE)) {
470 if(ERROR_SUCCESS == RegQueryValueEx(hKey, "Gaim", 0, NULL, NULL, NULL)) {
471 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE);
472 }
473 RegCloseKey(hKey);
474 }
475 g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(winprefs_set_autostart), NULL);
476 gtk_widget_show(button);
477
478 /* Buddy List */
479 vbox = gaim_gtk_make_frame(ret, _("Buddy List"));
480 gaim_gtk_prefs_checkbox(_("_Dockable Buddy List"),
481 PREF_DBLIST_DOCKABLE, vbox);
482
483 /* Blist On Top */
484 gaim_gtk_prefs_dropdown(vbox, _("_Keep Buddy List window on top:"),
485 GAIM_PREF_INT, PREF_BLIST_ON_TOP,
486 _("Never"), BLIST_TOP_NEVER,
487 _("Always"), BLIST_TOP_ALWAYS,
488 /* XXX: Did this ever work? */
489 _("Only when docked"), BLIST_TOP_DOCKED,
490 NULL);
491
492 /* Conversations */
493 vbox = gaim_gtk_make_frame(ret, _("Conversations"));
494 gaim_gtk_prefs_checkbox(_("_Flash window when IMs are received"),
495 PREF_IM_BLINK, vbox);
496 gaim_gtk_prefs_checkbox(_("_Flash window when chat messages are received"),
497 PREF_CHAT_BLINK, vbox);
498
499 gtk_widget_show_all(ret);
500 return ret;
501 }
502
503 static GaimGtkPluginUiInfo ui_info =
504 {
505 get_config_frame,
506 0
507 };
508
509 static GaimPluginInfo info =
510 {
511 GAIM_PLUGIN_MAGIC,
512 GAIM_MAJOR_VERSION,
513 GAIM_MINOR_VERSION,
514 GAIM_PLUGIN_STANDARD,
515 GAIM_GTK_PLUGIN_TYPE,
516 0,
517 NULL,
518 GAIM_PRIORITY_DEFAULT,
519 WINPREFS_PLUGIN_ID,
520 N_("WinGaim Options"),
521 VERSION,
522 N_("Options specific to Windows Gaim."),
523 N_("Provides options specific to Windows Gaim, such as buddy list docking and conversation flashing."),
524 "Herman Bloggs <hermanator12002@yahoo.com>",
525 GAIM_WEBSITE,
526 plugin_load,
527 plugin_unload,
528 NULL,
529 &ui_info,
530 NULL,
531 NULL,
532 NULL
533 };
534
535 static void
536 init_plugin(GaimPlugin *plugin)
537 {
538 gaim_prefs_add_none("/plugins/gtk");
539 gaim_prefs_add_none("/plugins/gtk/win32");
540 gaim_prefs_add_none("/plugins/gtk/win32/winprefs");
541 gaim_prefs_add_bool(PREF_DBLIST_DOCKABLE, FALSE);
542 gaim_prefs_add_bool(PREF_DBLIST_DOCKED, FALSE);
543 gaim_prefs_add_int(PREF_DBLIST_HEIGHT, 0);
544 gaim_prefs_add_int(PREF_DBLIST_SIDE, 0);
545 gaim_prefs_add_bool(PREF_IM_BLINK, TRUE);
546 gaim_prefs_add_bool(PREF_CHAT_BLINK, FALSE);
547
548 /* Convert old preferences */
549 if(gaim_prefs_exists(PREF_DBLIST_ON_TOP)) {
550 gint blist_top = BLIST_TOP_NEVER;
551 if(gaim_prefs_get_bool(PREF_BLIST_ON_TOP))
552 blist_top = BLIST_TOP_ALWAYS;
553 else if(gaim_prefs_get_bool(PREF_DBLIST_ON_TOP))
554 blist_top = BLIST_TOP_DOCKED;
555 gaim_prefs_remove(PREF_BLIST_ON_TOP);
556 gaim_prefs_remove(PREF_DBLIST_ON_TOP);
557 gaim_prefs_add_int(PREF_BLIST_ON_TOP, blist_top);
558 } else
559 gaim_prefs_add_int(PREF_BLIST_ON_TOP, BLIST_TOP_NEVER);
560 }
561
562 GAIM_INIT_PLUGIN(winprefs, init_plugin, info)
563