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