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