2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2008 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 "stock_pixmap.h"
52 #define PLUGIN_NAME (_("Trayicon"))
54 static guint item_hook_id;
55 static guint folder_hook_id;
56 static guint offline_hook_id;
57 static guint account_hook_id;
58 static guint close_hook_id;
59 static guint iconified_hook_id;
60 static guint theme_hook_id;
62 static GdkPixmap *newmail_pixmap[2] = {NULL, NULL};
63 static GdkPixmap *newmail_bitmap[2] = {NULL, NULL};
64 static GdkPixmap *unreadmail_pixmap[2] = {NULL, NULL};
65 static GdkPixmap *unreadmail_bitmap[2] = {NULL, NULL};
66 static GdkPixmap *newmarkedmail_pixmap[2] = {NULL, NULL};
67 static GdkPixmap *newmarkedmail_bitmap[2] = {NULL, NULL};
68 static GdkPixmap *unreadmarkedmail_pixmap[2] = {NULL, NULL};
69 static GdkPixmap *unreadmarkedmail_bitmap[2] = {NULL, NULL};
70 static GdkPixmap *nomail_pixmap[2] = {NULL, NULL};
71 static GdkPixmap *nomail_bitmap[2] = {NULL, NULL};
73 static EggTrayIcon *trayicon;
74 static GtkWidget *eventbox;
75 static GtkWidget *image = NULL;
76 static GtkTooltips *tooltips;
77 static GtkWidget *traymenu_popup;
78 static GtkItemFactory *traymenu_factory;
79 static gboolean updating_menu = FALSE;
81 guint destroy_signal_id;
88 TRAYICON_UNREADMARKED,
92 static void trayicon_get_all_cb (gpointer data, guint action, GtkWidget *widget);
93 static void trayicon_compose_cb (gpointer data, guint action, GtkWidget *widget);
94 static void trayicon_compose_acc_cb (GtkMenuItem *menuitem, gpointer data );
95 static void trayicon_addressbook_cb (gpointer data, guint action, GtkWidget *widget);
96 static void trayicon_exit_cb (gpointer data, guint action, GtkWidget *widget);
97 static void trayicon_toggle_offline_cb (gpointer data, guint action, GtkWidget *widget);
98 static void resize_cb (GtkWidget *widget, GtkRequisition *req, gpointer user_data);
100 static GtkItemFactoryEntry trayicon_popup_menu_entries[] =
102 {N_("/_Get Mail"), NULL, trayicon_get_all_cb, 0, NULL},
103 {"/---", NULL, NULL, 0, "<Separator>"},
104 {N_("/_Email"), NULL, trayicon_compose_cb, 0, NULL},
105 {N_("/_Email from account"), NULL, NULL, 0, "<Branch>"},
106 {"/---", NULL, NULL, 0, "<Separator>"},
107 {N_("/Open A_ddressbook"), NULL, trayicon_addressbook_cb, 0, NULL},
108 {"/---", NULL, NULL, 0, "<Separator>"},
109 {N_("/_Work Offline"), NULL, trayicon_toggle_offline_cb, 0, "<CheckItem>"},
110 {"/---", NULL, NULL, 0, "<Separator>"},
111 {N_("/E_xit Claws Mail"), NULL, trayicon_exit_cb, 0, NULL}
114 static gboolean trayicon_set_accounts_hook(gpointer source, gpointer data)
116 GList *cur_ac, *cur_item = NULL;
119 PrefsAccount *ac_prefs;
121 GList *account_list = account_get_list();
123 menu = gtk_item_factory_get_widget(traymenu_factory,
124 "/Email from account");
126 /* destroy all previous menu item */
127 cur_item = GTK_MENU_SHELL(menu)->children;
128 while (cur_item != NULL) {
129 GList *next = cur_item->next;
130 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
134 for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
135 ac_prefs = (PrefsAccount *)cur_ac->data;
137 menuitem = gtk_menu_item_new_with_label
138 (ac_prefs->account_name ? ac_prefs->account_name
140 gtk_widget_show(menuitem);
141 gtk_menu_append(GTK_MENU(menu), menuitem);
142 g_signal_connect(G_OBJECT(menuitem), "activate",
143 G_CALLBACK(trayicon_compose_acc_cb),
149 static void set_trayicon_pixmap(TrayIconType icontype)
151 GdkPixmap *pixmap = NULL;
152 GdkBitmap *bitmap = NULL;
153 static GdkPixmap *last_pixmap = NULL;
157 pixmap = newmail_pixmap[prefs_common.work_offline];
158 bitmap = newmail_bitmap[prefs_common.work_offline];
160 case TRAYICON_NEWMARKED:
161 pixmap = newmarkedmail_pixmap[prefs_common.work_offline];
162 bitmap = newmarkedmail_bitmap[prefs_common.work_offline];
164 case TRAYICON_UNREAD:
165 pixmap = unreadmail_pixmap[prefs_common.work_offline];
166 bitmap = unreadmail_bitmap[prefs_common.work_offline];
168 case TRAYICON_UNREADMARKED:
169 pixmap = unreadmarkedmail_pixmap[prefs_common.work_offline];
170 bitmap = unreadmarkedmail_bitmap[prefs_common.work_offline];
173 pixmap = nomail_pixmap[prefs_common.work_offline];
174 bitmap = nomail_bitmap[prefs_common.work_offline];
178 if (pixmap == last_pixmap) {
182 gtk_image_set_from_pixmap(GTK_IMAGE(image), pixmap, bitmap);
184 last_pixmap = pixmap;
187 static void update(FolderItem *removed_item)
189 guint new, unread, unreadmarked, marked, total;
190 guint replied, forwarded, locked, ignored, watched;
192 TrayIconType icontype = TRAYICON_NOTHING;
194 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total,
195 &replied, &forwarded, &locked, &ignored,
198 total -= removed_item->total_msgs;
199 new -= removed_item->new_msgs;
200 unread -= removed_item->unread_msgs;
203 buf = g_strdup_printf(_("New %d, Unread: %d, Total: %d"), new, unread, total);
205 gtk_tooltips_set_tip(tooltips, eventbox, buf, "");
208 if (new > 0 && unreadmarked > 0) {
209 icontype = TRAYICON_NEWMARKED;
210 } else if (new > 0) {
211 icontype = TRAYICON_NEW;
212 } else if (unreadmarked > 0) {
213 icontype = TRAYICON_UNREADMARKED;
214 } else if (unread > 0) {
215 icontype = TRAYICON_UNREAD;
218 set_trayicon_pixmap(icontype);
221 static gboolean folder_item_update_hook(gpointer source, gpointer data)
228 static gboolean folder_update_hook(gpointer source, gpointer data)
230 FolderUpdateData *hookdata;
232 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
233 update(hookdata->item);
240 static gboolean offline_update_hook(gpointer source, gpointer data)
246 static gboolean trayicon_close_hook(gpointer source, gpointer data)
249 gboolean *close_allowed = (gboolean*)source;
251 if (trayicon_prefs.close_to_tray) {
252 MainWindow *mainwin = mainwindow_get_mainwindow();
254 *close_allowed = FALSE;
255 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
256 main_window_hide(mainwin);
262 static gboolean trayicon_got_iconified_hook(gpointer source, gpointer data)
264 MainWindow *mainwin = mainwindow_get_mainwindow();
266 if (trayicon_prefs.hide_when_iconified
267 && GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))
268 && !gtk_window_get_skip_taskbar_hint(GTK_WINDOW(mainwin->window))) {
269 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), TRUE);
274 static void resize_cb(GtkWidget *widget, GtkRequisition *req,
280 static void fix_folderview_scroll(MainWindow *mainwin)
282 static gboolean fix_done = FALSE;
287 gtk_widget_queue_resize(mainwin->folderview->ctree);
292 static gboolean click_cb(GtkWidget * widget,
293 GdkEventButton * event, gpointer user_data)
301 mainwin = mainwindow_get_mainwindow();
303 switch (event->button) {
305 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))) {
306 if ((gdk_window_get_state(GTK_WIDGET(mainwin->window)->window)&GDK_WINDOW_STATE_ICONIFIED)
307 || mainwindow_is_obscured()) {
308 gtk_window_deiconify(GTK_WINDOW(mainwin->window));
309 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), FALSE);
310 main_window_show(mainwin);
311 gtk_window_present(GTK_WINDOW(mainwin->window));
313 main_window_hide(mainwin);
316 gtk_window_deiconify(GTK_WINDOW(mainwin->window));
317 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), FALSE);
318 main_window_show(mainwin);
319 gtk_window_present(GTK_WINDOW(mainwin->window));
320 fix_folderview_scroll(mainwin);
324 /* tell callbacks to skip any event */
325 updating_menu = TRUE;
326 /* initialize checkitem according to current offline state */
327 gtk_check_menu_item_set_active(
328 GTK_CHECK_MENU_ITEM(gtk_item_factory_get_item(traymenu_factory,
329 "/Work Offline")), prefs_common.work_offline);
330 gtk_widget_set_sensitive(
331 GTK_WIDGET(gtk_item_factory_get_item(traymenu_factory,
332 "/Get Mail")), mainwin->lock_count == 0);
333 updating_menu = FALSE;
335 gtk_menu_popup( GTK_MENU(traymenu_popup), NULL, NULL, NULL, NULL,
336 event->button, event->time );
344 static void create_trayicon(void);
346 static void destroy_cb(GtkWidget *widget, gpointer *data)
348 debug_print("Widget destroyed\n");
353 static gboolean trayicon_update_theme(gpointer source, gpointer data)
355 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NOMAIL, &nomail_pixmap[0], &nomail_bitmap[0]);
356 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMAIL, &unreadmail_pixmap[0], &unreadmail_bitmap[0]);
357 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMAIL, &newmail_pixmap[0], &newmail_bitmap[0]);
358 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMARKEDMAIL, &unreadmarkedmail_pixmap[0], &unreadmarkedmail_bitmap[0]);
359 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMARKEDMAIL, &newmarkedmail_pixmap[0], &newmarkedmail_bitmap[0]);
361 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NOMAIL_OFFLINE, &nomail_pixmap[1], &nomail_bitmap[1]);
362 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMAIL_OFFLINE, &unreadmail_pixmap[1], &unreadmail_bitmap[1]);
363 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMAIL_OFFLINE, &newmail_pixmap[1], &newmail_bitmap[1]);
364 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMARKEDMAIL_OFFLINE, &unreadmarkedmail_pixmap[1], &unreadmarkedmail_bitmap[1]);
365 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMARKEDMAIL_OFFLINE, &newmarkedmail_pixmap[1], &newmarkedmail_bitmap[1]);
373 static void create_trayicon()
377 trayicon = egg_tray_icon_new("Claws Mail");
378 gtk_widget_realize(GTK_WIDGET(trayicon));
379 gtk_window_set_default_size(GTK_WINDOW(trayicon), 16, 16);
380 gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
382 trayicon_update_theme(NULL, NULL);
384 eventbox = gtk_event_box_new();
385 gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
386 gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
388 image = gtk_image_new_from_pixmap(nomail_pixmap[0], nomail_bitmap[0]);
389 gtk_container_add(GTK_CONTAINER(eventbox), image);
392 g_signal_connect(G_OBJECT(trayicon), "destroy",
393 G_CALLBACK(destroy_cb), NULL);
394 g_signal_connect(GTK_OBJECT(trayicon), "size-request",
395 G_CALLBACK(resize_cb), NULL);
396 g_signal_connect(G_OBJECT(eventbox), "button-press-event",
397 G_CALLBACK(click_cb), NULL);
399 tooltips = gtk_tooltips_new();
400 gtk_tooltips_enable(tooltips);
402 n_entries = sizeof(trayicon_popup_menu_entries) /
403 sizeof(trayicon_popup_menu_entries[0]);
404 traymenu_popup = menu_create_items(trayicon_popup_menu_entries,
405 n_entries, "<TrayiconMenu>", &traymenu_factory,
408 gtk_widget_show_all(GTK_WIDGET(trayicon));
413 int plugin_init(gchar **error)
415 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
416 VERSION_NUMERIC, PLUGIN_NAME, error))
419 item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
420 if (item_hook_id == -1) {
421 *error = g_strdup(_("Failed to register folder item update hook"));
425 folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, folder_update_hook, NULL);
426 if (folder_hook_id == -1) {
427 *error = g_strdup(_("Failed to register folder update hook"));
431 offline_hook_id = hooks_register_hook (OFFLINE_SWITCH_HOOKLIST, offline_update_hook, NULL);
432 if (offline_hook_id == -1) {
433 *error = g_strdup(_("Failed to register offline switch hook"));
434 goto err_out_offline;
437 account_hook_id = hooks_register_hook (ACCOUNT_LIST_CHANGED_HOOKLIST, trayicon_set_accounts_hook, NULL);
438 if (account_hook_id == -1) {
439 *error = g_strdup(_("Failed to register account list changed hook"));
440 goto err_out_account;
443 close_hook_id = hooks_register_hook (MAIN_WINDOW_CLOSE, trayicon_close_hook, NULL);
444 if (close_hook_id == -1) {
445 *error = g_strdup(_("Failed to register close hook"));
449 iconified_hook_id = hooks_register_hook (MAIN_WINDOW_GOT_ICONIFIED, trayicon_got_iconified_hook, NULL);
450 if (iconified_hook_id == -1) {
451 *error = g_strdup(_("Failed to register got iconified hook"));
452 goto err_out_iconified;
455 theme_hook_id = hooks_register_hook(THEME_CHANGED_HOOKLIST, trayicon_update_theme, NULL);
456 if (theme_hook_id == -1) {
457 *error = g_strdup(_("Failed to register theme change hook"));
462 trayicon_set_accounts_hook(NULL, NULL);
464 trayicon_prefs_init();
466 if (trayicon_prefs.hide_at_startup && claws_is_starting()) {
467 MainWindow *mainwin = mainwindow_get_mainwindow();
469 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
470 main_window_hide(mainwin);
471 main_set_show_at_startup(FALSE);
477 hooks_unregister_hook(MAIN_WINDOW_GOT_ICONIFIED, iconified_hook_id);
479 hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
481 hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
483 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
485 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
487 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
492 gboolean plugin_done(void)
494 trayicon_prefs_done();
496 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
497 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
498 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
499 hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
500 hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
501 hooks_unregister_hook(MAIN_WINDOW_GOT_ICONIFIED, iconified_hook_id);
502 hooks_unregister_hook(THEME_CHANGED_HOOKLIST, theme_hook_id);
504 if (claws_is_exiting())
507 g_signal_handler_disconnect(G_OBJECT(trayicon), destroy_signal_id);
509 gtk_widget_destroy(GTK_WIDGET(trayicon));
511 while (gtk_events_pending()) {
512 gtk_main_iteration();
517 const gchar *plugin_name(void)
522 const gchar *plugin_desc(void)
524 return _("This plugin places a mailbox icon in the system tray that "
525 "indicates if you have new or unread mail.\n"
527 "The mailbox is empty if you have no unread mail, otherwise "
528 "it contains a letter. A tooltip shows new, unread and total "
529 "number of messages.");
532 const gchar *plugin_type(void)
537 const gchar *plugin_licence(void)
542 const gchar *plugin_version(void)
548 /* popup menu callbacks */
549 static void trayicon_get_all_cb( gpointer data, guint action, GtkWidget *widget )
551 MainWindow *mainwin = mainwindow_get_mainwindow();
552 inc_all_account_mail_cb(mainwin, 0, NULL);
555 static void trayicon_compose_cb( gpointer data, guint action, GtkWidget *widget )
557 MainWindow *mainwin = mainwindow_get_mainwindow();
558 compose_mail_cb(mainwin, 0, NULL);
561 static void trayicon_compose_acc_cb( GtkMenuItem *menuitem, gpointer data )
563 compose_new((PrefsAccount *)data, NULL, NULL);
566 static void trayicon_addressbook_cb( gpointer data, guint action, GtkWidget *widget )
568 addressbook_open(NULL);
571 static void trayicon_toggle_offline_cb( gpointer data, guint action, GtkWidget *widget )
573 /* toggle offline mode if menu checkitem has been clicked */
574 if (!updating_menu) {
575 MainWindow *mainwin = mainwindow_get_mainwindow();
576 main_window_toggle_work_offline(mainwin, !prefs_common.work_offline, TRUE);
580 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
582 if (prefs_common.confirm_on_exit) {
583 if (alertpanel(_("Exit"), _("Exit Claws Mail?"),
584 GTK_STOCK_CANCEL, GTK_STOCK_OK,
585 NULL) != G_ALERTALTERNATE) {
588 manage_window_focus_in(mainwin->window, NULL, NULL);
591 app_will_exit(NULL, mainwin);
594 static void trayicon_exit_cb( gpointer data, guint action, GtkWidget *widget )
596 MainWindow *mainwin = mainwindow_get_mainwindow();
598 if (mainwin->lock_count == 0) {
599 app_exit_cb(mainwin, 0, NULL);
603 struct PluginFeature *plugin_provides(void)
605 static struct PluginFeature features[] =
606 { {PLUGIN_NOTIFIER, N_("Trayicon")},
607 {PLUGIN_NOTHING, NULL}};