2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2007 the Claws Mail Team
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <glib/gi18n.h>
30 #include "common/claws.h"
31 #include "common/version.h"
36 #include "mainwindow.h"
40 #include "prefs_common.h"
42 #include "alertpanel.h"
44 #include "gtk/manage_window.h"
46 #include "eggtrayicon.h"
48 #include "trayicon_prefs.h"
50 #include "newmarkedmail.xpm"
51 #include "unreadmarkedmail.xpm"
52 #include "newmail.xpm"
53 #include "unreadmail.xpm"
55 #include "newmarkedmail.offline.xpm"
56 #include "unreadmarkedmail.offline.xpm"
57 #include "newmail.offline.xpm"
58 #include "unreadmail.offline.xpm"
59 #include "nomail.offline.xpm"
61 #define PLUGIN_NAME (_("Trayicon"))
63 static guint item_hook_id;
64 static guint folder_hook_id;
65 static guint offline_hook_id;
66 static guint account_hook_id;
67 static guint close_hook_id;
68 static guint iconified_hook_id;
70 static GdkPixmap *newmail_pixmap[2];
71 static GdkPixmap *newmail_bitmap[2];
72 static GdkPixmap *unreadmail_pixmap[2];
73 static GdkPixmap *unreadmail_bitmap[2];
74 static GdkPixmap *newmarkedmail_pixmap[2];
75 static GdkPixmap *newmarkedmail_bitmap[2];
76 static GdkPixmap *unreadmarkedmail_pixmap[2];
77 static GdkPixmap *unreadmarkedmail_bitmap[2];
78 static GdkPixmap *nomail_pixmap[2];
79 static GdkPixmap *nomail_bitmap[2];
81 static EggTrayIcon *trayicon;
82 static GtkWidget *eventbox;
83 static GtkWidget *image;
84 static GtkTooltips *tooltips;
85 static GtkWidget *traymenu_popup;
86 static GtkItemFactory *traymenu_factory;
87 static gboolean updating_menu = FALSE;
89 guint destroy_signal_id;
96 TRAYICON_UNREADMARKED,
100 static void trayicon_get_all_cb (gpointer data, guint action, GtkWidget *widget);
101 static void trayicon_compose_cb (gpointer data, guint action, GtkWidget *widget);
102 static void trayicon_compose_acc_cb (GtkMenuItem *menuitem, gpointer data );
103 static void trayicon_addressbook_cb (gpointer data, guint action, GtkWidget *widget);
104 static void trayicon_exit_cb (gpointer data, guint action, GtkWidget *widget);
105 static void trayicon_toggle_offline_cb (gpointer data, guint action, GtkWidget *widget);
106 static void resize_cb (GtkWidget *widget, GtkRequisition *req, gpointer user_data);
108 static GtkItemFactoryEntry trayicon_popup_menu_entries[] =
110 {N_("/_Get Mail"), NULL, trayicon_get_all_cb, 0, NULL},
111 {"/---", NULL, NULL, 0, "<Separator>"},
112 {N_("/_Email"), NULL, trayicon_compose_cb, 0, NULL},
113 {N_("/_Email from account"), NULL, NULL, 0, "<Branch>"},
114 {"/---", NULL, NULL, 0, "<Separator>"},
115 {N_("/Open A_ddressbook"), NULL, trayicon_addressbook_cb, 0, NULL},
116 {"/---", NULL, NULL, 0, "<Separator>"},
117 {N_("/_Work Offline"), NULL, trayicon_toggle_offline_cb, 0, "<CheckItem>"},
118 {"/---", NULL, NULL, 0, "<Separator>"},
119 {N_("/E_xit Claws Mail"), NULL, trayicon_exit_cb, 0, NULL}
122 static gboolean trayicon_set_accounts_hook(gpointer source, gpointer data)
124 GList *cur_ac, *cur_item = NULL;
127 PrefsAccount *ac_prefs;
129 GList *account_list = account_get_list();
131 menu = gtk_item_factory_get_widget(traymenu_factory,
132 "/Email from account");
134 /* destroy all previous menu item */
135 cur_item = GTK_MENU_SHELL(menu)->children;
136 while (cur_item != NULL) {
137 GList *next = cur_item->next;
138 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
142 for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
143 ac_prefs = (PrefsAccount *)cur_ac->data;
145 menuitem = gtk_menu_item_new_with_label
146 (ac_prefs->account_name ? ac_prefs->account_name
148 gtk_widget_show(menuitem);
149 gtk_menu_append(GTK_MENU(menu), menuitem);
150 g_signal_connect(G_OBJECT(menuitem), "activate",
151 G_CALLBACK(trayicon_compose_acc_cb),
157 static void set_trayicon_pixmap(TrayIconType icontype)
159 GdkPixmap *pixmap = NULL;
160 GdkBitmap *bitmap = NULL;
161 static GdkPixmap *last_pixmap = NULL;
165 pixmap = newmail_pixmap[prefs_common.work_offline];
166 bitmap = newmail_bitmap[prefs_common.work_offline];
168 case TRAYICON_NEWMARKED:
169 pixmap = newmarkedmail_pixmap[prefs_common.work_offline];
170 bitmap = newmarkedmail_bitmap[prefs_common.work_offline];
172 case TRAYICON_UNREAD:
173 pixmap = unreadmail_pixmap[prefs_common.work_offline];
174 bitmap = unreadmail_bitmap[prefs_common.work_offline];
176 case TRAYICON_UNREADMARKED:
177 pixmap = unreadmarkedmail_pixmap[prefs_common.work_offline];
178 bitmap = unreadmarkedmail_bitmap[prefs_common.work_offline];
181 pixmap = nomail_pixmap[prefs_common.work_offline];
182 bitmap = nomail_bitmap[prefs_common.work_offline];
186 if (pixmap == last_pixmap) {
190 gtk_image_set_from_pixmap(GTK_IMAGE(image), pixmap, bitmap);
192 last_pixmap = pixmap;
195 static void update(FolderItem *removed_item)
197 guint new, unread, unreadmarked, marked, total;
199 TrayIconType icontype = TRAYICON_NOTHING;
201 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total);
203 total -= removed_item->total_msgs;
204 new -= removed_item->new_msgs;
205 unread -= removed_item->unread_msgs;
208 buf = g_strdup_printf(_("New %d, Unread: %d, Total: %d"), new, unread, total);
210 gtk_tooltips_set_tip(tooltips, eventbox, buf, "");
213 if (new > 0 && unreadmarked > 0) {
214 icontype = TRAYICON_NEWMARKED;
215 } else if (new > 0) {
216 icontype = TRAYICON_NEW;
217 } else if (unreadmarked > 0) {
218 icontype = TRAYICON_UNREADMARKED;
219 } else if (unread > 0) {
220 icontype = TRAYICON_UNREAD;
223 set_trayicon_pixmap(icontype);
226 static gboolean folder_item_update_hook(gpointer source, gpointer data)
233 static gboolean folder_update_hook(gpointer source, gpointer data)
235 FolderUpdateData *hookdata;
237 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
238 update(hookdata->item);
245 static gboolean offline_update_hook(gpointer source, gpointer data)
251 static gboolean trayicon_close_hook(gpointer source, gpointer data)
254 gboolean *close_allowed = (gboolean*)source;
256 if (trayicon_prefs.close_to_tray) {
257 MainWindow *mainwin = mainwindow_get_mainwindow();
259 *close_allowed = FALSE;
260 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
261 main_window_hide(mainwin);
267 static gboolean trayicon_got_iconified_hook(gpointer source, gpointer data)
269 MainWindow *mainwin = mainwindow_get_mainwindow();
271 if (trayicon_prefs.hide_when_iconified
272 && GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))
273 && !gtk_window_get_skip_taskbar_hint(GTK_WINDOW(mainwin->window))) {
274 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), TRUE);
279 static void resize_cb(GtkWidget *widget, GtkRequisition *req,
285 static gboolean click_cb(GtkWidget * widget,
286 GdkEventButton * event, gpointer user_data)
294 mainwin = mainwindow_get_mainwindow();
296 switch (event->button) {
298 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))) {
299 if ((gdk_window_get_state(GTK_WIDGET(mainwin->window)->window)&GDK_WINDOW_STATE_ICONIFIED)
300 || mainwindow_is_obscured()) {
301 gtk_window_deiconify(GTK_WINDOW(mainwin->window));
302 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), FALSE);
303 main_window_show(mainwin);
304 gtk_window_present(GTK_WINDOW(mainwin->window));
306 main_window_hide(mainwin);
309 gtk_window_deiconify(GTK_WINDOW(mainwin->window));
310 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), FALSE);
311 main_window_show(mainwin);
312 gtk_window_present(GTK_WINDOW(mainwin->window));
316 /* tell callbacks to skip any event */
317 updating_menu = TRUE;
318 /* initialize checkitem according to current offline state */
319 gtk_check_menu_item_set_active(
320 GTK_CHECK_MENU_ITEM(gtk_item_factory_get_item(traymenu_factory,
321 "/Work Offline")), prefs_common.work_offline);
322 gtk_widget_set_sensitive(
323 GTK_WIDGET(gtk_item_factory_get_item(traymenu_factory,
324 "/Get Mail")), mainwin->lock_count == 0);
325 updating_menu = FALSE;
327 gtk_menu_popup( GTK_MENU(traymenu_popup), NULL, NULL, NULL, NULL,
328 event->button, event->time );
336 static void create_trayicon(void);
338 static void destroy_cb(GtkWidget *widget, gpointer *data)
340 debug_print("Widget destroyed\n");
345 static void create_trayicon()
349 trayicon = egg_tray_icon_new("Claws Mail");
350 gtk_widget_realize(GTK_WIDGET(trayicon));
351 gtk_window_set_default_size(GTK_WINDOW(trayicon), 16, 16);
352 gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
354 PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap[0], nomail_bitmap[0], nomail_xpm);
355 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap[0], unreadmail_bitmap[0], unreadmail_xpm);
356 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap[0], newmail_bitmap[0], newmail_xpm);
357 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmarkedmail_pixmap[0], unreadmarkedmail_bitmap[0], unreadmarkedmail_xpm);
358 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmarkedmail_pixmap[0], newmarkedmail_bitmap[0], newmarkedmail_xpm);
360 PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap[1], nomail_bitmap[1], nomail_offline_xpm);
361 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap[1], unreadmail_bitmap[1], unreadmail_offline_xpm);
362 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap[1], newmail_bitmap[1], newmail_offline_xpm);
363 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmarkedmail_pixmap[1], unreadmarkedmail_bitmap[1], unreadmarkedmail_offline_xpm);
364 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmarkedmail_pixmap[1], newmarkedmail_bitmap[1], newmarkedmail_offline_xpm);
367 eventbox = gtk_event_box_new();
368 gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
369 gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
371 image = gtk_image_new_from_pixmap(nomail_pixmap[0], nomail_bitmap[0]);
372 gtk_container_add(GTK_CONTAINER(eventbox), image);
375 g_signal_connect(G_OBJECT(trayicon), "destroy",
376 G_CALLBACK(destroy_cb), NULL);
377 g_signal_connect(GTK_OBJECT(trayicon), "size-request",
378 G_CALLBACK(resize_cb), NULL);
379 g_signal_connect(G_OBJECT(eventbox), "button-press-event",
380 G_CALLBACK(click_cb), NULL);
382 tooltips = gtk_tooltips_new();
383 gtk_tooltips_enable(tooltips);
385 n_entries = sizeof(trayicon_popup_menu_entries) /
386 sizeof(trayicon_popup_menu_entries[0]);
387 traymenu_popup = menu_create_items(trayicon_popup_menu_entries,
388 n_entries, "<TrayiconMenu>", &traymenu_factory,
391 gtk_widget_show_all(GTK_WIDGET(trayicon));
396 int plugin_init(gchar **error)
398 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
399 VERSION_NUMERIC, PLUGIN_NAME, error))
402 item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
403 if (item_hook_id == -1) {
404 *error = g_strdup(_("Failed to register folder item update hook"));
408 folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, folder_update_hook, NULL);
409 if (folder_hook_id == -1) {
410 *error = g_strdup(_("Failed to register folder update hook"));
414 offline_hook_id = hooks_register_hook (OFFLINE_SWITCH_HOOKLIST, offline_update_hook, NULL);
415 if (offline_hook_id == -1) {
416 *error = g_strdup(_("Failed to register offline switch hook"));
420 account_hook_id = hooks_register_hook (ACCOUNT_LIST_CHANGED_HOOKLIST, trayicon_set_accounts_hook, NULL);
421 if (account_hook_id == -1) {
422 *error = g_strdup(_("Failed to register account list changed hook"));
426 close_hook_id = hooks_register_hook (MAIN_WINDOW_CLOSE, trayicon_close_hook, NULL);
427 if (close_hook_id == -1) {
428 *error = g_strdup(_("Failed to register close hook"));
432 iconified_hook_id = hooks_register_hook (MAIN_WINDOW_GOT_ICONIFIED, trayicon_got_iconified_hook, NULL);
433 if (iconified_hook_id == -1) {
434 *error = g_strdup(_("Failed to register got iconified hook"));
439 trayicon_set_accounts_hook(NULL, NULL);
441 trayicon_prefs_init();
443 if (trayicon_prefs.hide_at_startup && claws_is_starting()) {
444 MainWindow *mainwin = mainwindow_get_mainwindow();
446 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
447 main_window_hide(mainwin);
448 main_set_show_at_startup(FALSE);
454 gboolean plugin_done(void)
456 trayicon_prefs_done();
458 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
459 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
460 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
461 hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
462 hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
463 hooks_unregister_hook(MAIN_WINDOW_GOT_ICONIFIED, iconified_hook_id);
465 if (claws_is_exiting())
468 g_signal_handler_disconnect(G_OBJECT(trayicon), destroy_signal_id);
470 gtk_widget_destroy(GTK_WIDGET(trayicon));
472 while (gtk_events_pending()) {
473 gtk_main_iteration();
478 const gchar *plugin_name(void)
483 const gchar *plugin_desc(void)
485 return _("This plugin places a mailbox icon in the system tray that "
486 "indicates if you have new or unread mail.\n"
488 "The mailbox is empty if you have no unread mail, otherwise "
489 "it contains a letter. A tooltip shows new, unread and total "
490 "number of messages.");
493 const gchar *plugin_type(void)
498 const gchar *plugin_licence(void)
503 const gchar *plugin_version(void)
509 /* popup menu callbacks */
510 static void trayicon_get_all_cb( gpointer data, guint action, GtkWidget *widget )
512 MainWindow *mainwin = mainwindow_get_mainwindow();
513 inc_all_account_mail_cb(mainwin, 0, NULL);
516 static void trayicon_compose_cb( gpointer data, guint action, GtkWidget *widget )
518 MainWindow *mainwin = mainwindow_get_mainwindow();
519 compose_mail_cb(mainwin, 0, NULL);
522 static void trayicon_compose_acc_cb( GtkMenuItem *menuitem, gpointer data )
524 compose_new((PrefsAccount *)data, NULL, NULL);
527 static void trayicon_addressbook_cb( gpointer data, guint action, GtkWidget *widget )
529 addressbook_open(NULL);
532 static void trayicon_toggle_offline_cb( gpointer data, guint action, GtkWidget *widget )
534 /* toggle offline mode if menu checkitem has been clicked */
535 if (!updating_menu) {
536 MainWindow *mainwin = mainwindow_get_mainwindow();
537 main_window_toggle_work_offline(mainwin, !prefs_common.work_offline, TRUE);
541 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
543 if (prefs_common.confirm_on_exit) {
544 if (alertpanel(_("Exit"), _("Exit Claws Mail?"),
545 GTK_STOCK_CANCEL, GTK_STOCK_OK,
546 NULL) != G_ALERTALTERNATE) {
549 manage_window_focus_in(mainwin->window, NULL, NULL);
552 app_will_exit(NULL, mainwin);
555 static void trayicon_exit_cb( gpointer data, guint action, GtkWidget *widget )
557 MainWindow *mainwin = mainwindow_get_mainwindow();
559 if (mainwin->lock_count == 0) {
560 app_exit_cb(mainwin, 0, NULL);
564 struct PluginFeature *plugin_provides(void)
566 static struct PluginFeature features[] =
567 { {PLUGIN_NOTIFIER, N_("Trayicon")},
568 {PLUGIN_NOTHING, NULL}};