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