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