bda0cd5e22b74d986d35387112130e521126ef32
[claws.git] / src / plugins / trayicon / trayicon.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2003-2007 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
61 static GdkPixmap *newmail_pixmap[2];
62 static GdkPixmap *newmail_bitmap[2];
63 static GdkPixmap *unreadmail_pixmap[2];
64 static GdkPixmap *unreadmail_bitmap[2];
65 static GdkPixmap *newmarkedmail_pixmap[2];
66 static GdkPixmap *newmarkedmail_bitmap[2];
67 static GdkPixmap *unreadmarkedmail_pixmap[2];
68 static GdkPixmap *unreadmarkedmail_bitmap[2];
69 static GdkPixmap *nomail_pixmap[2];
70 static GdkPixmap *nomail_bitmap[2];
71
72 static EggTrayIcon *trayicon;
73 static GtkWidget *eventbox;
74 static GtkWidget *image;
75 static GtkTooltips *tooltips;
76 static GtkWidget *traymenu_popup;
77 static GtkItemFactory *traymenu_factory;
78 static gboolean updating_menu = FALSE;
79
80 guint destroy_signal_id;
81
82 typedef enum
83 {
84         TRAYICON_NEW,
85         TRAYICON_NEWMARKED,
86         TRAYICON_UNREAD,
87         TRAYICON_UNREADMARKED,
88         TRAYICON_NOTHING
89 } TrayIconType;
90
91 static void trayicon_get_all_cb     (gpointer data, guint action, GtkWidget *widget);
92 static void trayicon_compose_cb     (gpointer data, guint action, GtkWidget *widget);
93 static void trayicon_compose_acc_cb (GtkMenuItem *menuitem, gpointer data );
94 static void trayicon_addressbook_cb (gpointer data, guint action, GtkWidget *widget);
95 static void trayicon_exit_cb        (gpointer data, guint action, GtkWidget *widget);
96 static void trayicon_toggle_offline_cb  (gpointer data, guint action, GtkWidget *widget);
97 static void resize_cb               (GtkWidget *widget, GtkRequisition *req, gpointer user_data);
98
99 static GtkItemFactoryEntry trayicon_popup_menu_entries[] =
100 {
101         {N_("/_Get Mail"),              NULL, trayicon_get_all_cb,              0, NULL},
102         {"/---",                        NULL, NULL,                             0, "<Separator>"},
103         {N_("/_Email"),                 NULL, trayicon_compose_cb,              0, NULL},
104         {N_("/_Email from account"),    NULL, NULL,                             0, "<Branch>"},
105         {"/---",                        NULL, NULL,                             0, "<Separator>"},
106         {N_("/Open A_ddressbook"),      NULL, trayicon_addressbook_cb,          0, NULL},
107         {"/---",                        NULL, NULL,                             0, "<Separator>"},
108         {N_("/_Work Offline"),          NULL, trayicon_toggle_offline_cb,       0, "<CheckItem>"},
109         {"/---",                        NULL, NULL,                             0, "<Separator>"},
110         {N_("/E_xit Claws Mail"),       NULL, trayicon_exit_cb,                 0, NULL}
111 };
112
113 static gboolean trayicon_set_accounts_hook(gpointer source, gpointer data)
114 {
115         GList *cur_ac, *cur_item = NULL;
116         GtkWidget *menu;
117         GtkWidget *menuitem;
118         PrefsAccount *ac_prefs;
119
120         GList *account_list = account_get_list();
121
122         menu = gtk_item_factory_get_widget(traymenu_factory,
123                                            "/Email from account");
124
125         /* destroy all previous menu item */
126         cur_item = GTK_MENU_SHELL(menu)->children;
127         while (cur_item != NULL) {
128                 GList *next = cur_item->next;
129                 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
130                 cur_item = next;
131         }
132
133         for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
134                 ac_prefs = (PrefsAccount *)cur_ac->data;
135
136                 menuitem = gtk_menu_item_new_with_label
137                         (ac_prefs->account_name ? ac_prefs->account_name
138                          : _("Untitled"));
139                 gtk_widget_show(menuitem);
140                 gtk_menu_append(GTK_MENU(menu), menuitem);
141                 g_signal_connect(G_OBJECT(menuitem), "activate",
142                                  G_CALLBACK(trayicon_compose_acc_cb),
143                                  ac_prefs);
144         }
145         return FALSE;
146 }
147
148 static void set_trayicon_pixmap(TrayIconType icontype)
149 {
150         GdkPixmap *pixmap = NULL;
151         GdkBitmap *bitmap = NULL;
152         static GdkPixmap *last_pixmap = NULL;
153
154         switch(icontype) {
155         case TRAYICON_NEW:
156                 pixmap = newmail_pixmap[prefs_common.work_offline];
157                 bitmap = newmail_bitmap[prefs_common.work_offline];
158                 break;
159         case TRAYICON_NEWMARKED:
160                 pixmap = newmarkedmail_pixmap[prefs_common.work_offline];
161                 bitmap = newmarkedmail_bitmap[prefs_common.work_offline];
162                 break;
163         case TRAYICON_UNREAD:
164                 pixmap = unreadmail_pixmap[prefs_common.work_offline];
165                 bitmap = unreadmail_bitmap[prefs_common.work_offline];
166                 break;
167         case TRAYICON_UNREADMARKED:
168                 pixmap = unreadmarkedmail_pixmap[prefs_common.work_offline];
169                 bitmap = unreadmarkedmail_bitmap[prefs_common.work_offline];
170                 break;
171         default:
172                 pixmap = nomail_pixmap[prefs_common.work_offline];
173                 bitmap = nomail_bitmap[prefs_common.work_offline];
174                 break;
175         }
176
177         if (pixmap == last_pixmap) {
178                 return;
179         }
180
181         gtk_image_set_from_pixmap(GTK_IMAGE(image), pixmap, bitmap);
182
183         last_pixmap = pixmap;
184 }
185
186 static void update(FolderItem *removed_item)
187 {
188         guint new, unread, unreadmarked, marked, total;
189         gchar *buf;
190         TrayIconType icontype = TRAYICON_NOTHING;
191
192         folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total);
193         if (removed_item) {
194                 total -= removed_item->total_msgs;
195                 new -= removed_item->new_msgs;
196                 unread -= removed_item->unread_msgs;
197         }
198
199         buf = g_strdup_printf(_("New %d, Unread: %d, Total: %d"), new, unread, total);
200
201         gtk_tooltips_set_tip(tooltips, eventbox, buf, "");
202         g_free(buf);
203
204         if (new > 0 && unreadmarked > 0) {
205                 icontype = TRAYICON_NEWMARKED;
206         } else if (new > 0) {
207                 icontype = TRAYICON_NEW;
208         } else if (unreadmarked > 0) {
209                 icontype = TRAYICON_UNREADMARKED;
210         } else if (unread > 0) {
211                 icontype = TRAYICON_UNREAD;
212         }
213
214         set_trayicon_pixmap(icontype);
215 }
216
217 static gboolean folder_item_update_hook(gpointer source, gpointer data)
218 {
219         update(NULL);
220
221         return FALSE;
222 }
223
224 static gboolean folder_update_hook(gpointer source, gpointer data)
225 {
226         FolderUpdateData *hookdata;
227         hookdata = source;
228         if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
229                 update(hookdata->item);
230         else
231                 update(NULL);
232
233         return FALSE;
234 }
235
236 static gboolean offline_update_hook(gpointer source, gpointer data)
237 {
238         update(NULL);
239         return FALSE;
240 }
241
242 static gboolean trayicon_close_hook(gpointer source, gpointer data)
243 {
244         if (source) {
245                 gboolean *close_allowed = (gboolean*)source;
246
247                 if (trayicon_prefs.close_to_tray) {
248                 MainWindow *mainwin = mainwindow_get_mainwindow();
249
250                         *close_allowed = FALSE;
251                 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
252                         main_window_hide(mainwin);
253         }
254         }
255         return FALSE;
256 }
257
258 static gboolean trayicon_got_iconified_hook(gpointer source, gpointer data)
259 {
260         MainWindow *mainwin = mainwindow_get_mainwindow();
261
262         if (trayicon_prefs.hide_when_iconified
263                         && GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))
264                         && !gtk_window_get_skip_taskbar_hint(GTK_WINDOW(mainwin->window))) {
265                 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), TRUE);
266         }
267         return FALSE;
268 }
269
270 static void resize_cb(GtkWidget *widget, GtkRequisition *req,
271                       gpointer user_data)
272 {
273         update(NULL);
274 }
275
276 static gboolean click_cb(GtkWidget * widget,
277                          GdkEventButton * event, gpointer user_data)
278 {
279         MainWindow *mainwin;
280
281         if (event == NULL) {
282                 return TRUE;
283         }
284
285         mainwin = mainwindow_get_mainwindow();
286
287         switch (event->button) {
288         case 1:
289                 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))) {
290                         if ((gdk_window_get_state(GTK_WIDGET(mainwin->window)->window)&GDK_WINDOW_STATE_ICONIFIED)
291                                         || mainwindow_is_obscured()) {
292                                 gtk_window_deiconify(GTK_WINDOW(mainwin->window));
293                                 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), FALSE);
294                                 main_window_show(mainwin);
295                                 gtk_window_present(GTK_WINDOW(mainwin->window));
296                         } else {
297                                 main_window_hide(mainwin);
298                         }
299                 } else {
300                         gtk_window_deiconify(GTK_WINDOW(mainwin->window));
301                         gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), FALSE);
302                         main_window_show(mainwin);
303                         gtk_window_present(GTK_WINDOW(mainwin->window));
304         }
305                 break;
306         case 3:
307                 /* tell callbacks to skip any event */
308                 updating_menu = TRUE;
309                 /* initialize checkitem according to current offline state */
310                 gtk_check_menu_item_set_active(
311                         GTK_CHECK_MENU_ITEM(gtk_item_factory_get_item(traymenu_factory,
312                         "/Work Offline")), prefs_common.work_offline);
313                 gtk_widget_set_sensitive(
314                         GTK_WIDGET(gtk_item_factory_get_item(traymenu_factory,
315                         "/Get Mail")), mainwin->lock_count == 0);
316                 updating_menu = FALSE;
317
318                 gtk_menu_popup( GTK_MENU(traymenu_popup), NULL, NULL, NULL, NULL,
319                        event->button, event->time );
320                 break;
321         default:
322                 return TRUE;
323         }
324         return TRUE;
325 }
326
327 static void create_trayicon(void);
328
329 static void destroy_cb(GtkWidget *widget, gpointer *data)
330 {
331         debug_print("Widget destroyed\n");
332
333         create_trayicon();
334 }
335
336 static void create_trayicon()
337 {
338         gint n_entries = 0;
339
340         trayicon = egg_tray_icon_new("Claws Mail");
341         gtk_widget_realize(GTK_WIDGET(trayicon));
342         gtk_window_set_default_size(GTK_WINDOW(trayicon), 16, 16);
343         gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
344
345         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NOMAIL, &nomail_pixmap[0], &nomail_bitmap[0]);
346         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMAIL, &unreadmail_pixmap[0], &unreadmail_bitmap[0]);
347         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMAIL, &newmail_pixmap[0], &newmail_bitmap[0]);
348         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMARKEDMAIL, &unreadmarkedmail_pixmap[0], &unreadmarkedmail_bitmap[0]);
349         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMARKEDMAIL, &newmarkedmail_pixmap[0], &newmarkedmail_bitmap[0]);
350
351         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NOMAIL_OFFLINE, &nomail_pixmap[1], &nomail_bitmap[1]);
352         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMAIL_OFFLINE, &unreadmail_pixmap[1], &unreadmail_bitmap[1]);
353         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMAIL_OFFLINE, &newmail_pixmap[1], &newmail_bitmap[1]);
354         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMARKEDMAIL_OFFLINE, &unreadmarkedmail_pixmap[1], &unreadmarkedmail_bitmap[1]);
355         stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMARKEDMAIL_OFFLINE, &newmarkedmail_pixmap[1], &newmarkedmail_bitmap[1]);
356
357
358         eventbox = gtk_event_box_new();
359         gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
360         gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
361
362         image = gtk_image_new_from_pixmap(nomail_pixmap[0], nomail_bitmap[0]);
363         gtk_container_add(GTK_CONTAINER(eventbox), image);
364
365         destroy_signal_id =
366         g_signal_connect(G_OBJECT(trayicon), "destroy",
367                 G_CALLBACK(destroy_cb), NULL);
368         g_signal_connect(GTK_OBJECT(trayicon), "size-request",
369                 G_CALLBACK(resize_cb), NULL);
370         g_signal_connect(G_OBJECT(eventbox), "button-press-event",
371                 G_CALLBACK(click_cb), NULL);
372
373         tooltips = gtk_tooltips_new();
374         gtk_tooltips_enable(tooltips);
375
376         n_entries = sizeof(trayicon_popup_menu_entries) /
377         sizeof(trayicon_popup_menu_entries[0]);
378         traymenu_popup = menu_create_items(trayicon_popup_menu_entries,
379                                                 n_entries, "<TrayiconMenu>", &traymenu_factory,
380                                                 NULL);
381
382         gtk_widget_show_all(GTK_WIDGET(trayicon));
383
384         update(NULL);
385 }
386
387 int plugin_init(gchar **error)
388 {
389         if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
390                                 VERSION_NUMERIC, PLUGIN_NAME, error))
391                 return -1;
392
393         item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
394         if (item_hook_id == -1) {
395                 *error = g_strdup(_("Failed to register folder item update hook"));
396                 goto err_out_item;
397         }
398
399         folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, folder_update_hook, NULL);
400         if (folder_hook_id == -1) {
401                 *error = g_strdup(_("Failed to register folder update hook"));
402                 goto err_out_folder;
403         }
404
405         offline_hook_id = hooks_register_hook (OFFLINE_SWITCH_HOOKLIST, offline_update_hook, NULL);
406         if (offline_hook_id == -1) {
407                 *error = g_strdup(_("Failed to register offline switch hook"));
408                 goto err_out_offline;
409         }
410
411         account_hook_id = hooks_register_hook (ACCOUNT_LIST_CHANGED_HOOKLIST, trayicon_set_accounts_hook, NULL);
412         if (account_hook_id == -1) {
413                 *error = g_strdup(_("Failed to register account list changed hook"));
414                 goto err_out_account;
415         }
416
417         close_hook_id = hooks_register_hook (MAIN_WINDOW_CLOSE, trayicon_close_hook, NULL);
418         if (close_hook_id == -1) {
419                 *error = g_strdup(_("Failed to register close hook"));
420                 goto err_out_close;
421         }
422
423         iconified_hook_id = hooks_register_hook (MAIN_WINDOW_GOT_ICONIFIED, trayicon_got_iconified_hook, NULL);
424         if (iconified_hook_id == -1) {
425                 *error = g_strdup(_("Failed to register got iconified hook"));
426                 goto err_out_iconified;
427         }
428
429         create_trayicon();
430         trayicon_set_accounts_hook(NULL, NULL);
431
432         trayicon_prefs_init();
433
434         if (trayicon_prefs.hide_at_startup && claws_is_starting()) {
435                 MainWindow *mainwin = mainwindow_get_mainwindow();
436
437                 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
438                         main_window_hide(mainwin);
439                 main_set_show_at_startup(FALSE);
440         }
441
442         return 0;
443
444 err_out_iconified:
445         hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
446 err_out_close:
447         hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
448 err_out_account:
449         hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
450 err_out_offline:
451         hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
452 err_out_folder:
453         hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
454 err_out_item:
455         return -1;
456 }
457
458 gboolean plugin_done(void)
459 {
460         trayicon_prefs_done();
461
462         hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
463         hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
464         hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
465         hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
466         hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
467         hooks_unregister_hook(MAIN_WINDOW_GOT_ICONIFIED, iconified_hook_id);
468
469         if (claws_is_exiting())
470                 return TRUE;
471
472         g_signal_handler_disconnect(G_OBJECT(trayicon), destroy_signal_id);
473         
474         gtk_widget_destroy(GTK_WIDGET(trayicon));
475
476         while (gtk_events_pending()) {
477                 gtk_main_iteration();
478         }
479         return TRUE;
480 }
481
482 const gchar *plugin_name(void)
483 {
484         return PLUGIN_NAME;
485 }
486
487 const gchar *plugin_desc(void)
488 {
489         return _("This plugin places a mailbox icon in the system tray that "
490                  "indicates if you have new or unread mail.\n"
491                  "\n"
492                  "The mailbox is empty if you have no unread mail, otherwise "
493                  "it contains a letter. A tooltip shows new, unread and total "
494                  "number of messages.");
495 }
496
497 const gchar *plugin_type(void)
498 {
499         return "GTK2";
500 }
501
502 const gchar *plugin_licence(void)
503 {
504         return "GPL3+";
505 }
506
507 const gchar *plugin_version(void)
508 {
509         return VERSION;
510 }
511
512
513 /* popup menu callbacks */
514 static void trayicon_get_all_cb( gpointer data, guint action, GtkWidget *widget )
515 {
516         MainWindow *mainwin = mainwindow_get_mainwindow();
517         inc_all_account_mail_cb(mainwin, 0, NULL);
518 }
519
520 static void trayicon_compose_cb( gpointer data, guint action, GtkWidget *widget )
521 {
522         MainWindow *mainwin = mainwindow_get_mainwindow();
523         compose_mail_cb(mainwin, 0, NULL);
524 }
525
526 static void trayicon_compose_acc_cb( GtkMenuItem *menuitem, gpointer data )
527 {
528         compose_new((PrefsAccount *)data, NULL, NULL);
529 }
530
531 static void trayicon_addressbook_cb( gpointer data, guint action, GtkWidget *widget )
532 {
533         addressbook_open(NULL);
534 }
535
536 static void trayicon_toggle_offline_cb( gpointer data, guint action, GtkWidget *widget )
537 {
538         /* toggle offline mode if menu checkitem has been clicked */
539         if (!updating_menu) {
540                 MainWindow *mainwin = mainwindow_get_mainwindow();
541                 main_window_toggle_work_offline(mainwin, !prefs_common.work_offline, TRUE);
542         }
543 }
544
545 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
546 {
547         if (prefs_common.confirm_on_exit) {
548                 if (alertpanel(_("Exit"), _("Exit Claws Mail?"),
549                                GTK_STOCK_CANCEL, GTK_STOCK_OK,
550                                NULL) != G_ALERTALTERNATE) {
551                         return;
552                 }
553                 manage_window_focus_in(mainwin->window, NULL, NULL);
554         }
555
556         app_will_exit(NULL, mainwin);
557 }
558
559 static void trayicon_exit_cb( gpointer data, guint action, GtkWidget *widget )
560 {
561         MainWindow *mainwin = mainwindow_get_mainwindow();
562
563         if (mainwin->lock_count == 0) {
564                 app_exit_cb(mainwin, 0, NULL);
565         }
566 }
567
568 struct PluginFeature *plugin_provides(void)
569 {
570         static struct PluginFeature features[] = 
571                 { {PLUGIN_NOTIFIER, N_("Trayicon")},
572                   {PLUGIN_NOTHING, NULL}};
573         return features;
574 }