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