2007-02-01 [wwp] 2.7.2cvs11
[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                                 gtk_window_present(GTK_WINDOW(mainwin->window));
291                         } else {
292                                 main_window_hide(mainwin);
293                         }
294                 } else {
295                         gtk_window_deiconify(GTK_WINDOW(mainwin->window));
296                         main_window_show(mainwin);
297                         gtk_window_present(GTK_WINDOW(mainwin->window));
298         }
299                 break;
300         case 3:
301                 /* tell callbacks to skip any event */
302                 updating_menu = TRUE;
303                 /* initialize checkitem according to current offline state */
304                 gtk_check_menu_item_set_active(
305                         GTK_CHECK_MENU_ITEM(gtk_item_factory_get_item(traymenu_factory,
306                         _("/Work Offline"))), prefs_common.work_offline);
307                 gtk_widget_set_sensitive(
308                         GTK_WIDGET(gtk_item_factory_get_item(traymenu_factory,
309                         _("/Get Mail"))), mainwin->lock_count == 0);
310                 updating_menu = FALSE;
311
312                 gtk_menu_popup( GTK_MENU(traymenu_popup), NULL, NULL, NULL, NULL,
313                        event->button, event->time );
314                 break;
315         default:
316                 return TRUE;
317         }
318         return TRUE;
319 }
320
321 static void create_trayicon(void);
322
323 static void destroy_cb(GtkWidget *widget, gpointer *data)
324 {
325         debug_print("Widget destroyed\n");
326
327         create_trayicon();
328 }
329
330 static void create_trayicon()
331 {
332         gint n_entries = 0;
333
334         trayicon = egg_tray_icon_new("Claws Mail");
335         gtk_widget_realize(GTK_WIDGET(trayicon));
336         gtk_window_set_default_size(GTK_WINDOW(trayicon), 16, 16);
337         gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
338
339         PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap[0], nomail_bitmap[0], nomail_xpm);
340         PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap[0], unreadmail_bitmap[0], unreadmail_xpm);
341         PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap[0], newmail_bitmap[0], newmail_xpm);
342         PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmarkedmail_pixmap[0], unreadmarkedmail_bitmap[0], unreadmarkedmail_xpm);
343         PIXMAP_CREATE(GTK_WIDGET(trayicon), newmarkedmail_pixmap[0], newmarkedmail_bitmap[0], newmarkedmail_xpm);
344
345         PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap[1], nomail_bitmap[1], nomail_offline_xpm);
346         PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap[1], unreadmail_bitmap[1], unreadmail_offline_xpm);
347         PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap[1], newmail_bitmap[1], newmail_offline_xpm);
348         PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmarkedmail_pixmap[1], unreadmarkedmail_bitmap[1], unreadmarkedmail_offline_xpm);
349         PIXMAP_CREATE(GTK_WIDGET(trayicon), newmarkedmail_pixmap[1], newmarkedmail_bitmap[1], newmarkedmail_offline_xpm);
350
351
352         eventbox = gtk_event_box_new();
353         gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
354         gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
355
356         image = gtk_image_new_from_pixmap(nomail_pixmap[0], nomail_bitmap[0]);
357         gtk_container_add(GTK_CONTAINER(eventbox), image);
358
359         destroy_signal_id =
360         g_signal_connect(G_OBJECT(trayicon), "destroy",
361                 G_CALLBACK(destroy_cb), NULL);
362         g_signal_connect(GTK_OBJECT(trayicon), "size-request",
363                 G_CALLBACK(resize_cb), NULL);
364         g_signal_connect(G_OBJECT(eventbox), "button-press-event",
365                 G_CALLBACK(click_cb), NULL);
366
367         tooltips = gtk_tooltips_new();
368         gtk_tooltips_enable(tooltips);
369
370         n_entries = sizeof(trayicon_popup_menu_entries) /
371         sizeof(trayicon_popup_menu_entries[0]);
372         traymenu_popup = menu_create_items(trayicon_popup_menu_entries,
373                                                 n_entries, "<TrayiconMenu>", &traymenu_factory,
374                                                 NULL);
375
376         gtk_widget_show_all(GTK_WIDGET(trayicon));
377
378         update(NULL);
379 }
380
381 int plugin_init(gchar **error)
382 {
383         if (!check_plugin_version(MAKE_NUMERIC_VERSION(0, 9, 3, 86),
384                                 VERSION_NUMERIC, PLUGIN_NAME, error))
385                 return -1;
386
387         item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
388         if (item_hook_id == -1) {
389                 *error = g_strdup(_("Failed to register folder item update hook"));
390                 return -1;
391         }
392
393         folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, folder_update_hook, NULL);
394         if (folder_hook_id == -1) {
395                 *error = g_strdup(_("Failed to register folder update hook"));
396                 return -1;
397         }
398
399         offline_hook_id = hooks_register_hook (OFFLINE_SWITCH_HOOKLIST, offline_update_hook, NULL);
400         if (offline_hook_id == -1) {
401                 *error = g_strdup(_("Failed to register offline switch hook"));
402                 return -1;
403         }
404
405         account_hook_id = hooks_register_hook (ACCOUNT_LIST_CHANGED_HOOKLIST, trayicon_set_accounts_hook, NULL);
406         if (offline_hook_id == -1) {
407                 *error = g_strdup(_("Failed to register account list changed hook"));
408                 return -1;
409         }
410
411         close_hook_id = hooks_register_hook (MAIN_WINDOW_CLOSE, trayicon_close_hook, NULL);
412         if (close_hook_id == -1) {
413                 *error = g_strdup(_("Failed to register close hook"));
414                 return -1;
415         }
416
417         create_trayicon();
418         trayicon_set_accounts_hook(NULL, NULL);
419
420         trayicon_prefs_init();
421
422         if (trayicon_prefs.hide_at_startup && claws_is_starting()) {
423                 MainWindow *mainwin = mainwindow_get_mainwindow();
424
425                 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
426                         main_window_hide(mainwin);
427         }
428
429         return 0;
430 }
431
432 void plugin_done(void)
433 {
434         trayicon_prefs_done();
435
436         hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
437         hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
438         hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
439         hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
440         hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
441
442         if (claws_is_exiting())
443                 return;
444
445         g_signal_handler_disconnect(G_OBJECT(trayicon), destroy_signal_id);
446         
447         gtk_widget_destroy(GTK_WIDGET(trayicon));
448
449         while (gtk_events_pending()) {
450                 gtk_main_iteration();
451         }
452 }
453
454 const gchar *plugin_name(void)
455 {
456         return PLUGIN_NAME;
457 }
458
459 const gchar *plugin_desc(void)
460 {
461         return _("This plugin places a mailbox icon in the system tray that "
462                  "indicates if you have new or unread mail.\n"
463                  "\n"
464                  "The mailbox is empty if you have no unread mail, otherwise "
465                  "it contains a letter. A tooltip shows new, unread and total "
466                  "number of messages.");
467 }
468
469 const gchar *plugin_type(void)
470 {
471         return "GTK2";
472 }
473
474 const gchar *plugin_licence(void)
475 {
476         return "GPL";
477 }
478
479 const gchar *plugin_version(void)
480 {
481         return VERSION;
482 }
483
484
485 /* popup menu callbacks */
486 static void trayicon_get_all_cb( gpointer data, guint action, GtkWidget *widget )
487 {
488         MainWindow *mainwin = mainwindow_get_mainwindow();
489         inc_all_account_mail_cb(mainwin, 0, NULL);
490 }
491
492 static void trayicon_compose_cb( gpointer data, guint action, GtkWidget *widget )
493 {
494         MainWindow *mainwin = mainwindow_get_mainwindow();
495         compose_mail_cb(mainwin, 0, NULL);
496 }
497
498 static void trayicon_compose_acc_cb( GtkMenuItem *menuitem, gpointer data )
499 {
500         compose_new((PrefsAccount *)data, NULL, NULL);
501 }
502
503 static void trayicon_addressbook_cb( gpointer data, guint action, GtkWidget *widget )
504 {
505         addressbook_open(NULL);
506 }
507
508 static void trayicon_toggle_offline_cb( gpointer data, guint action, GtkWidget *widget )
509 {
510         /* toggle offline mode if menu checkitem has been clicked */
511         if (!updating_menu) {
512                 MainWindow *mainwin = mainwindow_get_mainwindow();
513                 main_window_toggle_work_offline(mainwin, !prefs_common.work_offline, TRUE);
514         }
515 }
516
517 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
518 {
519         if (prefs_common.confirm_on_exit) {
520                 if (alertpanel(_("Exit"), _("Exit Claws Mail?"),
521                                GTK_STOCK_CANCEL, GTK_STOCK_OK,
522                                NULL) != G_ALERTALTERNATE) {
523                         return;
524                 }
525                 manage_window_focus_in(mainwin->window, NULL, NULL);
526         }
527
528         app_will_exit(NULL, mainwin);
529 }
530
531 static void trayicon_exit_cb( gpointer data, guint action, GtkWidget *widget )
532 {
533         MainWindow *mainwin = mainwindow_get_mainwindow();
534
535         if (mainwin->lock_count == 0) {
536                 app_exit_cb(mainwin, 0, NULL);
537         }
538 }
539
540 struct PluginFeature *plugin_provides(void)
541 {
542         static struct PluginFeature features[] = 
543                 { {PLUGIN_NOTIFIER, N_("Trayicon")},
544                   {PLUGIN_NOTHING, NULL}};
545         return features;
546 }