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