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