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