3a2214734fe6eced70184b11b547703c8e479032
[claws.git] / src / plugins / trayicon / trayicon.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2003-2008 the Claws Mail Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <stdio.h>
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29
30 #include "common/claws.h"
31 #include "common/version.h"
32 #include "plugin.h"
33 #include "utils.h"
34 #include "hooks.h"
35 #include "folder.h"
36 #include "mainwindow.h"
37 #include "gtkutils.h"
38 #include "menu.h"
39 #include "toolbar.h"
40 #include "prefs_common.h"
41 #include "main.h"
42 #include "alertpanel.h"
43 #include "account.h"
44 #include "gtk/manage_window.h"
45
46 #include "eggtrayicon.h"
47
48 #include "trayicon_prefs.h"
49
50 #include "stock_pixmap.h"
51
52 #define PLUGIN_NAME (_("Trayicon"))
53
54 static guint item_hook_id;
55 static guint folder_hook_id;
56 static guint offline_hook_id;
57 static guint account_hook_id;
58 static guint close_hook_id;
59 static guint iconified_hook_id;
60 static guint theme_hook_id;
61
62 static GdkPixmap *newmail_pixmap[2] = {NULL, NULL};
63 static GdkPixmap *newmail_bitmap[2] = {NULL, NULL};
64 static GdkPixmap *unreadmail_pixmap[2] = {NULL, NULL};
65 static GdkPixmap *unreadmail_bitmap[2] = {NULL, NULL};
66 static GdkPixmap *newmarkedmail_pixmap[2] = {NULL, NULL};
67 static GdkPixmap *newmarkedmail_bitmap[2] = {NULL, NULL};
68 static GdkPixmap *unreadmarkedmail_pixmap[2] = {NULL, NULL};
69 static GdkPixmap *unreadmarkedmail_bitmap[2] = {NULL, NULL};
70 static GdkPixmap *nomail_pixmap[2] = {NULL, NULL};
71 static GdkPixmap *nomail_bitmap[2] = {NULL, NULL};
72
73 static EggTrayIcon *trayicon;
74 static GtkWidget *eventbox;
75 static GtkWidget *image = NULL;
76
77 #if !(GTK_CHECK_VERSION(2,12,0))
78 static GtkTooltips *tooltips;
79 #endif
80 static GtkWidget *traymenu_popup;
81 static gboolean updating_menu = FALSE;
82
83 guint destroy_signal_id;
84
85 typedef enum
86 {
87         TRAYICON_NEW,
88         TRAYICON_NEWMARKED,
89         TRAYICON_UNREAD,
90         TRAYICON_UNREADMARKED,
91         TRAYICON_NOTHING
92 } TrayIconType;
93
94 static void trayicon_get_all_cb     (GtkAction *action, gpointer data);
95 static void trayicon_compose_cb     (GtkAction *action, gpointer data);
96 static void trayicon_compose_acc_cb (GtkMenuItem *menuitem, gpointer data );
97 static void trayicon_addressbook_cb (GtkAction *action, gpointer data);
98 static void trayicon_exit_cb        (GtkAction *action, gpointer data);
99 static void trayicon_toggle_offline_cb  (GtkAction *action, gpointer data);
100 static void resize_cb               (GtkWidget *widget, GtkRequisition *req, gpointer user_data);
101
102 static GtkActionEntry trayicon_popup_menu_entries[] =
103 {
104         {"TrayiconPopup",                       NULL, "TrayiconPopup" },
105         {"TrayiconPopup/GetMail",               NULL, N_("_Get Mail"), NULL, NULL, G_CALLBACK(trayicon_get_all_cb) },
106         {"TrayiconPopup/---",                   NULL, "---", NULL, NULL, G_CALLBACK(trayicon_compose_cb) },
107         {"TrayiconPopup/Email",                 NULL, N_("_Email"), NULL, NULL, G_CALLBACK(trayicon_compose_cb) },
108         {"TrayiconPopup/EmailAcc",              NULL, N_("E_mail from account"), NULL, NULL, NULL },
109         {"TrayiconPopup/OpenAB",                NULL, N_("Open A_ddressbook"), NULL, NULL, G_CALLBACK(trayicon_addressbook_cb) },
110         {"TrayiconPopup/Exit",                  NULL, N_("E_xit Claws Mail"), NULL, NULL, G_CALLBACK(trayicon_exit_cb) },
111 };
112
113 static GtkToggleActionEntry trayicon_popup_toggle_menu_entries[] =
114 {
115         {"TrayiconPopup/ToggleOffline",         NULL, N_("_Work Offline"), NULL, NULL, G_CALLBACK(trayicon_toggle_offline_cb) },
116 };
117
118 static gboolean trayicon_set_accounts_hook(gpointer source, gpointer data)
119 {
120         GList *cur_ac;
121         GtkWidget *menu, *submenu;
122         GtkWidget *menuitem;
123         PrefsAccount *ac_prefs;
124
125         GList *account_list = account_get_list();
126
127         menu = gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/TrayiconPopup/EmailAcc");
128         gtk_widget_show(menu);
129
130         gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu), NULL);
131         submenu = gtk_menu_new();
132         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
133                 ac_prefs = (PrefsAccount *)cur_ac->data;
134
135                 menuitem = gtk_menu_item_new_with_label
136                         (ac_prefs->account_name ? ac_prefs->account_name
137                          : _("Untitled"));
138                 gtk_widget_show(menuitem);
139                 gtk_menu_shell_append(GTK_MENU_SHELL(submenu), menuitem);
140                 g_signal_connect(G_OBJECT(menuitem), "activate",
141                                  G_CALLBACK(trayicon_compose_acc_cb),
142                                  ac_prefs);
143         }
144         gtk_widget_show(submenu);
145         gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu), submenu);
146         return FALSE;
147 }
148
149 static void set_trayicon_pixmap(TrayIconType icontype)
150 {
151         GdkPixmap *pixmap = NULL;
152         GdkBitmap *bitmap = NULL;
153         static GdkPixmap *last_pixmap = NULL;
154
155         switch(icontype) {
156         case TRAYICON_NEW:
157                 pixmap = newmail_pixmap[prefs_common.work_offline];
158                 bitmap = newmail_bitmap[prefs_common.work_offline];
159                 break;
160         case TRAYICON_NEWMARKED:
161                 pixmap = newmarkedmail_pixmap[prefs_common.work_offline];
162                 bitmap = newmarkedmail_bitmap[prefs_common.work_offline];
163                 break;
164         case TRAYICON_UNREAD:
165                 pixmap = unreadmail_pixmap[prefs_common.work_offline];
166                 bitmap = unreadmail_bitmap[prefs_common.work_offline];
167                 break;
168         case TRAYICON_UNREADMARKED:
169                 pixmap = unreadmarkedmail_pixmap[prefs_common.work_offline];
170                 bitmap = unreadmarkedmail_bitmap[prefs_common.work_offline];
171                 break;
172         default:
173                 pixmap = nomail_pixmap[prefs_common.work_offline];
174                 bitmap = nomail_bitmap[prefs_common.work_offline];
175                 break;
176         }
177
178         if (pixmap == last_pixmap) {
179                 return;
180         }
181
182         gtk_image_set_from_pixmap(GTK_IMAGE(image), pixmap, bitmap);
183
184         last_pixmap = pixmap;
185 }
186
187 static void update(FolderItem *removed_item)
188 {
189         guint new, unread, unreadmarked, marked, total;
190         guint replied, forwarded, locked, ignored, watched;
191         gchar *buf;
192         TrayIconType icontype = TRAYICON_NOTHING;
193
194         folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total,
195                                 &replied, &forwarded, &locked, &ignored,
196                                 &watched);
197         if (removed_item) {
198                 total -= removed_item->total_msgs;
199                 new -= removed_item->new_msgs;
200                 unread -= removed_item->unread_msgs;
201         }
202
203         buf = g_strdup_printf(_("New %d, Unread: %d, Total: %d"), new, unread, total);
204
205 #if !(GTK_CHECK_VERSION(2,12,0))
206         gtk_tooltips_set_tip(tooltips, eventbox, buf, "");
207 #else
208         gtk_widget_set_tooltip_text(eventbox, buf);
209 #endif
210         g_free(buf);
211
212         if (new > 0 && unreadmarked > 0) {
213                 icontype = TRAYICON_NEWMARKED;
214         } else if (new > 0) {
215                 icontype = TRAYICON_NEW;
216         } else if (unreadmarked > 0) {
217                 icontype = TRAYICON_UNREADMARKED;
218         } else if (unread > 0) {
219                 icontype = TRAYICON_UNREAD;
220         }
221
222         set_trayicon_pixmap(icontype);
223 }
224
225 static gboolean folder_item_update_hook(gpointer source, gpointer data)
226 {
227         update(NULL);
228
229         return FALSE;
230 }
231
232 static gboolean folder_update_hook(gpointer source, gpointer data)
233 {
234         FolderUpdateData *hookdata;
235         hookdata = source;
236         if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
237                 update(hookdata->item);
238         else
239                 update(NULL);
240
241         return FALSE;
242 }
243
244 static gboolean offline_update_hook(gpointer source, gpointer data)
245 {
246         update(NULL);
247         return FALSE;
248 }
249
250 static gboolean trayicon_close_hook(gpointer source, gpointer data)
251 {
252         if (source) {
253                 gboolean *close_allowed = (gboolean*)source;
254
255                 if (trayicon_prefs.close_to_tray) {
256                 MainWindow *mainwin = mainwindow_get_mainwindow();
257
258                         *close_allowed = FALSE;
259                 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
260                         main_window_hide(mainwin);
261         }
262         }
263         return FALSE;
264 }
265
266 static gboolean trayicon_got_iconified_hook(gpointer source, gpointer data)
267 {
268         MainWindow *mainwin = mainwindow_get_mainwindow();
269
270         if (trayicon_prefs.hide_when_iconified
271                         && GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))
272                         && !gtk_window_get_skip_taskbar_hint(GTK_WINDOW(mainwin->window))) {
273                 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), TRUE);
274         }
275         return FALSE;
276 }
277
278 static void resize_cb(GtkWidget *widget, GtkRequisition *req,
279                       gpointer user_data)
280 {
281         update(NULL);
282 }
283
284 static void fix_folderview_scroll(MainWindow *mainwin)
285 {
286         static gboolean fix_done = FALSE;
287
288         if (fix_done)
289                 return;
290
291         gtk_widget_queue_resize(mainwin->folderview->ctree);
292
293         fix_done = TRUE;
294 }
295
296 static gboolean click_cb(GtkWidget * widget,
297                          GdkEventButton * event, gpointer user_data)
298 {
299         MainWindow *mainwin;
300
301         if (event == NULL) {
302                 return TRUE;
303         }
304
305         mainwin = mainwindow_get_mainwindow();
306
307         switch (event->button) {
308         case 1:
309                 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))) {
310                         if ((gdk_window_get_state(GTK_WIDGET(mainwin->window)->window)&GDK_WINDOW_STATE_ICONIFIED)
311                                         || mainwindow_is_obscured()) {
312                                 gtk_window_deiconify(GTK_WINDOW(mainwin->window));
313                                 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), FALSE);
314                                 main_window_show(mainwin);
315                                 gtk_window_present(GTK_WINDOW(mainwin->window));
316                         } else {
317                                 main_window_hide(mainwin);
318                         }
319                 } else {
320                         gtk_window_deiconify(GTK_WINDOW(mainwin->window));
321                         gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), FALSE);
322                         main_window_show(mainwin);
323                         gtk_window_present(GTK_WINDOW(mainwin->window));
324                         fix_folderview_scroll(mainwin);
325         }
326                 break;
327         case 3:
328                 /* tell callbacks to skip any event */
329                 updating_menu = TRUE;
330                 /* initialize checkitem according to current offline state */
331                 cm_toggle_menu_set_active("TrayiconPopup/ToggleOffline", prefs_common.work_offline);
332                 cm_menu_set_sensitive("TrayiconPopup/GetMail", mainwin->lock_count == 0);
333                 updating_menu = FALSE;
334
335                 gtk_menu_popup( GTK_MENU(traymenu_popup), NULL, NULL, NULL, NULL,
336                        event->button, event->time );
337                 break;
338         default:
339                 return TRUE;
340         }
341         return TRUE;
342 }
343
344 static void create_trayicon(void);
345
346 static void destroy_cb(GtkWidget *widget, gpointer *data)
347 {
348         debug_print("Widget destroyed\n");
349
350         create_trayicon();
351 }
352
353 static gboolean trayicon_update_theme(gpointer source, gpointer data)
354 {
355         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NOMAIL, &nomail_pixmap[0], &nomail_bitmap[0]);
356         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMAIL, &unreadmail_pixmap[0], &unreadmail_bitmap[0]);
357         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMAIL, &newmail_pixmap[0], &newmail_bitmap[0]);
358         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMARKEDMAIL, &unreadmarkedmail_pixmap[0], &unreadmarkedmail_bitmap[0]);
359         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMARKEDMAIL, &newmarkedmail_pixmap[0], &newmarkedmail_bitmap[0]);
360
361         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NOMAIL_OFFLINE, &nomail_pixmap[1], &nomail_bitmap[1]);
362         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMAIL_OFFLINE, &unreadmail_pixmap[1], &unreadmail_bitmap[1]);
363         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMAIL_OFFLINE, &newmail_pixmap[1], &newmail_bitmap[1]);
364         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMARKEDMAIL_OFFLINE, &unreadmarkedmail_pixmap[1], &unreadmarkedmail_bitmap[1]);
365         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMARKEDMAIL_OFFLINE, &newmarkedmail_pixmap[1], &newmarkedmail_bitmap[1]);
366
367         if (image != NULL)
368                 update(NULL);
369
370         return FALSE;
371 }
372
373 static void create_trayicon()
374 {
375         GtkActionGroup *action_group;
376         trayicon = egg_tray_icon_new("Claws Mail");
377         gtk_widget_realize(GTK_WIDGET(trayicon));
378         gtk_window_set_default_size(GTK_WINDOW(trayicon), 16, 16);
379         gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
380
381         trayicon_update_theme(NULL, NULL);
382
383         eventbox = gtk_event_box_new();
384         gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
385         gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
386
387         image = gtk_image_new_from_pixmap(nomail_pixmap[0], nomail_bitmap[0]);
388         gtk_container_add(GTK_CONTAINER(eventbox), image);
389
390         destroy_signal_id =
391         g_signal_connect(G_OBJECT(trayicon), "destroy",
392                 G_CALLBACK(destroy_cb), NULL);
393         g_signal_connect(G_OBJECT(trayicon), "size-request",
394                 G_CALLBACK(resize_cb), NULL);
395         g_signal_connect(G_OBJECT(eventbox), "button-press-event",
396                 G_CALLBACK(click_cb), NULL);
397
398 #if !(GTK_CHECK_VERSION(2,12,0))
399         tooltips = gtk_tooltips_new();
400         gtk_tooltips_enable(tooltips);
401 #endif
402
403         action_group = cm_menu_create_action_group("TrayiconPopup", trayicon_popup_menu_entries,
404                         G_N_ELEMENTS(trayicon_popup_menu_entries), (gpointer)NULL);
405         gtk_action_group_add_toggle_actions(action_group, trayicon_popup_toggle_menu_entries,
406                         G_N_ELEMENTS(trayicon_popup_toggle_menu_entries), (gpointer)NULL);
407
408         MENUITEM_ADDUI("/Menus", "TrayiconPopup", "TrayiconPopup", GTK_UI_MANAGER_MENU)
409         MENUITEM_ADDUI("/Menus/TrayiconPopup", "GetMail", "TrayiconPopup/GetMail", GTK_UI_MANAGER_MENUITEM)
410         MENUITEM_ADDUI("/Menus/TrayiconPopup", "Separator1", "TrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
411         MENUITEM_ADDUI("/Menus/TrayiconPopup", "Email", "TrayiconPopup/Email", GTK_UI_MANAGER_MENUITEM)
412         MENUITEM_ADDUI("/Menus/TrayiconPopup", "EmailAcc", "TrayiconPopup/EmailAcc", GTK_UI_MANAGER_MENU)
413         MENUITEM_ADDUI("/Menus/TrayiconPopup", "Separator2", "TrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
414         MENUITEM_ADDUI("/Menus/TrayiconPopup", "OpenAB", "TrayiconPopup/OpenAB", GTK_UI_MANAGER_MENUITEM)
415         MENUITEM_ADDUI("/Menus/TrayiconPopup", "Separator3", "TrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
416         MENUITEM_ADDUI("/Menus/TrayiconPopup", "ToggleOffline", "TrayiconPopup/ToggleOffline", GTK_UI_MANAGER_MENUITEM)
417         MENUITEM_ADDUI("/Menus/TrayiconPopup", "Separator4", "TrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
418         MENUITEM_ADDUI("/Menus/TrayiconPopup", "Exit", "TrayiconPopup/Exit", GTK_UI_MANAGER_MENUITEM)
419
420         traymenu_popup = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
421                                 gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/TrayiconPopup")) );
422
423         gtk_widget_show_all(GTK_WIDGET(trayicon));
424
425         update(NULL);
426 }
427
428 int plugin_init(gchar **error)
429 {
430         if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
431                                 VERSION_NUMERIC, PLUGIN_NAME, error))
432                 return -1;
433
434         item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
435         if (item_hook_id == -1) {
436                 *error = g_strdup(_("Failed to register folder item update hook"));
437                 goto err_out_item;
438         }
439
440         folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, folder_update_hook, NULL);
441         if (folder_hook_id == -1) {
442                 *error = g_strdup(_("Failed to register folder update hook"));
443                 goto err_out_folder;
444         }
445
446         offline_hook_id = hooks_register_hook (OFFLINE_SWITCH_HOOKLIST, offline_update_hook, NULL);
447         if (offline_hook_id == -1) {
448                 *error = g_strdup(_("Failed to register offline switch hook"));
449                 goto err_out_offline;
450         }
451
452         account_hook_id = hooks_register_hook (ACCOUNT_LIST_CHANGED_HOOKLIST, trayicon_set_accounts_hook, NULL);
453         if (account_hook_id == -1) {
454                 *error = g_strdup(_("Failed to register account list changed hook"));
455                 goto err_out_account;
456         }
457
458         close_hook_id = hooks_register_hook (MAIN_WINDOW_CLOSE, trayicon_close_hook, NULL);
459         if (close_hook_id == -1) {
460                 *error = g_strdup(_("Failed to register close hook"));
461                 goto err_out_close;
462         }
463
464         iconified_hook_id = hooks_register_hook (MAIN_WINDOW_GOT_ICONIFIED, trayicon_got_iconified_hook, NULL);
465         if (iconified_hook_id == -1) {
466                 *error = g_strdup(_("Failed to register got iconified hook"));
467                 goto err_out_iconified;
468         }
469
470         theme_hook_id = hooks_register_hook(THEME_CHANGED_HOOKLIST, trayicon_update_theme, NULL);
471         if (theme_hook_id == -1) {
472                 *error = g_strdup(_("Failed to register theme change hook"));
473                 goto err_out_theme;
474         }
475
476         create_trayicon();
477         trayicon_set_accounts_hook(NULL, NULL);
478
479         trayicon_prefs_init();
480
481         if (trayicon_prefs.hide_at_startup && claws_is_starting()) {
482                 MainWindow *mainwin = mainwindow_get_mainwindow();
483
484                 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
485                         main_window_hide(mainwin);
486                 main_set_show_at_startup(FALSE);
487         }
488
489         return 0;
490
491 err_out_theme:
492         hooks_unregister_hook(MAIN_WINDOW_GOT_ICONIFIED, iconified_hook_id);
493 err_out_iconified:
494         hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
495 err_out_close:
496         hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
497 err_out_account:
498         hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
499 err_out_offline:
500         hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
501 err_out_folder:
502         hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
503 err_out_item:
504         return -1;
505 }
506
507 gboolean plugin_done(void)
508 {
509         trayicon_prefs_done();
510
511         hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
512         hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
513         hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
514         hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
515         hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
516         hooks_unregister_hook(MAIN_WINDOW_GOT_ICONIFIED, iconified_hook_id);
517         hooks_unregister_hook(THEME_CHANGED_HOOKLIST, theme_hook_id);
518
519         if (claws_is_exiting())
520                 return TRUE;
521
522         g_signal_handler_disconnect(G_OBJECT(trayicon), destroy_signal_id);
523         
524         gtk_widget_destroy(GTK_WIDGET(trayicon));
525
526         while (gtk_events_pending()) {
527                 gtk_main_iteration();
528         }
529         return TRUE;
530 }
531
532 const gchar *plugin_name(void)
533 {
534         return PLUGIN_NAME;
535 }
536
537 const gchar *plugin_desc(void)
538 {
539         return _("This plugin places a mailbox icon in the system tray that "
540                  "indicates if you have new or unread mail.\n"
541                  "\n"
542                  "The mailbox is empty if you have no unread mail, otherwise "
543                  "it contains a letter. A tooltip shows new, unread and total "
544                  "number of messages.");
545 }
546
547 const gchar *plugin_type(void)
548 {
549         return "GTK2";
550 }
551
552 const gchar *plugin_licence(void)
553 {
554         return "GPL3+";
555 }
556
557 const gchar *plugin_version(void)
558 {
559         return VERSION;
560 }
561
562
563 /* popup menu callbacks */
564 static void trayicon_get_all_cb( GtkAction *action, gpointer data )
565 {
566         MainWindow *mainwin = mainwindow_get_mainwindow();
567         inc_all_account_mail_cb(mainwin, 0, NULL);
568 }
569
570 static void trayicon_compose_cb( GtkAction *action, gpointer data )
571 {
572         MainWindow *mainwin = mainwindow_get_mainwindow();
573         compose_mail_cb(mainwin, 0, NULL);
574 }
575
576 static void trayicon_compose_acc_cb( GtkMenuItem *menuitem, gpointer data )
577 {
578         compose_new((PrefsAccount *)data, NULL, NULL);
579 }
580
581 static void trayicon_addressbook_cb( GtkAction *action, gpointer data )
582 {
583         addressbook_open(NULL);
584 }
585
586 static void trayicon_toggle_offline_cb( GtkAction *action, gpointer data )
587 {
588         /* toggle offline mode if menu checkitem has been clicked */
589         if (!updating_menu) {
590                 MainWindow *mainwin = mainwindow_get_mainwindow();
591                 main_window_toggle_work_offline(mainwin, !prefs_common.work_offline, TRUE);
592         }
593 }
594
595 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
596 {
597         if (prefs_common.confirm_on_exit) {
598                 if (alertpanel(_("Exit"), _("Exit Claws Mail?"),
599                                GTK_STOCK_CANCEL, GTK_STOCK_OK,
600                                NULL) != G_ALERTALTERNATE) {
601                         return;
602                 }
603                 manage_window_focus_in(mainwin->window, NULL, NULL);
604         }
605
606         app_will_exit(NULL, mainwin);
607 }
608
609 static void trayicon_exit_cb( GtkAction *action, gpointer data )
610 {
611         MainWindow *mainwin = mainwindow_get_mainwindow();
612
613         if (mainwin->lock_count == 0) {
614                 app_exit_cb(mainwin, 0, NULL);
615         }
616 }
617
618 struct PluginFeature *plugin_provides(void)
619 {
620         static struct PluginFeature features[] = 
621                 { {PLUGIN_NOTIFIER, N_("Trayicon")},
622                   {PLUGIN_NOTHING, NULL}};
623         return features;
624 }