2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2009 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 GdkPixbuf *newmail_pixmap[2] = {NULL, NULL};
63 static GdkPixbuf *unreadmail_pixmap[2] = {NULL, NULL};
64 static GdkPixbuf *newmarkedmail_pixmap[2] = {NULL, NULL};
65 static GdkPixbuf *unreadmarkedmail_pixmap[2] = {NULL, NULL};
66 static GdkPixbuf *nomail_pixmap[2] = {NULL, NULL};
68 static EggTrayIcon *trayicon;
69 static GtkWidget *eventbox;
70 static GtkWidget *image = NULL;
72 #if !(GTK_CHECK_VERSION(2,12,0))
73 static GtkTooltips *tooltips;
75 static GtkWidget *traymenu_popup;
76 static gboolean updating_menu = FALSE;
78 guint destroy_signal_id;
85 TRAYICON_UNREADMARKED,
89 static void trayicon_get_all_cb (GtkAction *action, gpointer data);
90 static void trayicon_compose_cb (GtkAction *action, gpointer data);
91 static void trayicon_compose_acc_cb (GtkMenuItem *menuitem, gpointer data );
92 static void trayicon_addressbook_cb (GtkAction *action, gpointer data);
93 static void trayicon_exit_cb (GtkAction *action, gpointer data);
94 static void trayicon_toggle_offline_cb (GtkAction *action, gpointer data);
95 static void resize_cb (GtkWidget *widget, GtkRequisition *req, gpointer user_data);
97 static GtkActionEntry trayicon_popup_menu_entries[] =
99 {"TrayiconPopup", NULL, "TrayiconPopup" },
100 {"TrayiconPopup/GetMail", NULL, N_("_Get Mail"), NULL, NULL, G_CALLBACK(trayicon_get_all_cb) },
101 {"TrayiconPopup/---", NULL, "---", NULL, NULL, G_CALLBACK(trayicon_compose_cb) },
102 {"TrayiconPopup/Email", NULL, N_("_Email"), NULL, NULL, G_CALLBACK(trayicon_compose_cb) },
103 {"TrayiconPopup/EmailAcc", NULL, N_("E_mail from account"), NULL, NULL, NULL },
104 {"TrayiconPopup/OpenAB", NULL, N_("Open A_ddressbook"), NULL, NULL, G_CALLBACK(trayicon_addressbook_cb) },
105 {"TrayiconPopup/Exit", NULL, N_("E_xit Claws Mail"), NULL, NULL, G_CALLBACK(trayicon_exit_cb) },
108 static GtkToggleActionEntry trayicon_popup_toggle_menu_entries[] =
110 {"TrayiconPopup/ToggleOffline", NULL, N_("_Work Offline"), NULL, NULL, G_CALLBACK(trayicon_toggle_offline_cb) },
113 static gboolean trayicon_set_accounts_hook(gpointer source, gpointer data)
116 GtkWidget *menu, *submenu;
118 PrefsAccount *ac_prefs;
120 GList *account_list = account_get_list();
122 menu = gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/TrayiconPopup/EmailAcc");
123 gtk_widget_show(menu);
125 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu), NULL);
126 submenu = gtk_menu_new();
127 for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
128 ac_prefs = (PrefsAccount *)cur_ac->data;
130 menuitem = gtk_menu_item_new_with_label
131 (ac_prefs->account_name ? ac_prefs->account_name
133 gtk_widget_show(menuitem);
134 gtk_menu_shell_append(GTK_MENU_SHELL(submenu), menuitem);
135 g_signal_connect(G_OBJECT(menuitem), "activate",
136 G_CALLBACK(trayicon_compose_acc_cb),
139 gtk_widget_show(submenu);
140 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu), submenu);
144 static void set_trayicon_pixmap(TrayIconType icontype)
146 GdkPixbuf *pixmap = NULL;
147 static GdkPixbuf *last_pixmap = NULL;
151 pixmap = newmail_pixmap[prefs_common.work_offline];
153 case TRAYICON_NEWMARKED:
154 pixmap = newmarkedmail_pixmap[prefs_common.work_offline];
156 case TRAYICON_UNREAD:
157 pixmap = unreadmail_pixmap[prefs_common.work_offline];
159 case TRAYICON_UNREADMARKED:
160 pixmap = unreadmarkedmail_pixmap[prefs_common.work_offline];
163 pixmap = nomail_pixmap[prefs_common.work_offline];
167 if (pixmap == last_pixmap) {
171 gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixmap);
173 last_pixmap = pixmap;
176 static void update(FolderItem *removed_item)
178 guint new, unread, unreadmarked, marked, total;
179 guint replied, forwarded, locked, ignored, watched;
181 TrayIconType icontype = TRAYICON_NOTHING;
183 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total,
184 &replied, &forwarded, &locked, &ignored,
187 total -= removed_item->total_msgs;
188 new -= removed_item->new_msgs;
189 unread -= removed_item->unread_msgs;
192 buf = g_strdup_printf(_("New %d, Unread: %d, Total: %d"), new, unread, total);
194 #if !(GTK_CHECK_VERSION(2,12,0))
195 gtk_tooltips_set_tip(tooltips, eventbox, buf, "");
197 gtk_widget_set_tooltip_text(eventbox, buf);
201 if (new > 0 && unreadmarked > 0) {
202 icontype = TRAYICON_NEWMARKED;
203 } else if (new > 0) {
204 icontype = TRAYICON_NEW;
205 } else if (unreadmarked > 0) {
206 icontype = TRAYICON_UNREADMARKED;
207 } else if (unread > 0) {
208 icontype = TRAYICON_UNREAD;
211 set_trayicon_pixmap(icontype);
214 static gboolean folder_item_update_hook(gpointer source, gpointer data)
221 static gboolean folder_update_hook(gpointer source, gpointer data)
223 FolderUpdateData *hookdata;
225 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
226 update(hookdata->item);
233 static gboolean offline_update_hook(gpointer source, gpointer data)
239 static gboolean trayicon_close_hook(gpointer source, gpointer data)
242 gboolean *close_allowed = (gboolean*)source;
244 if (trayicon_prefs.close_to_tray) {
245 MainWindow *mainwin = mainwindow_get_mainwindow();
247 *close_allowed = FALSE;
248 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
249 main_window_hide(mainwin);
255 static gboolean trayicon_got_iconified_hook(gpointer source, gpointer data)
257 MainWindow *mainwin = mainwindow_get_mainwindow();
259 if (trayicon_prefs.hide_when_iconified
260 && GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))
261 && !gtk_window_get_skip_taskbar_hint(GTK_WINDOW(mainwin->window))) {
262 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), TRUE);
267 static void resize_cb(GtkWidget *widget, GtkRequisition *req,
273 static void fix_folderview_scroll(MainWindow *mainwin)
275 static gboolean fix_done = FALSE;
280 gtk_widget_queue_resize(mainwin->folderview->ctree);
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));
313 fix_folderview_scroll(mainwin);
317 /* tell callbacks to skip any event */
318 updating_menu = TRUE;
319 /* initialize checkitem according to current offline state */
320 cm_toggle_menu_set_active("TrayiconPopup/ToggleOffline", prefs_common.work_offline);
321 cm_menu_set_sensitive("TrayiconPopup/GetMail", mainwin->lock_count == 0);
322 updating_menu = FALSE;
324 gtk_menu_popup( GTK_MENU(traymenu_popup), NULL, NULL, NULL, NULL,
325 event->button, event->time );
333 static void create_trayicon(void);
335 static void destroy_cb(GtkWidget *widget, gpointer *data)
337 debug_print("Widget destroyed\n");
342 static gboolean trayicon_update_theme(gpointer source, gpointer data)
344 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NOMAIL, &nomail_pixmap[0]);
345 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMAIL, &unreadmail_pixmap[0]);
346 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMAIL, &newmail_pixmap[0]);
347 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMARKEDMAIL, &unreadmarkedmail_pixmap[0]);
348 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMARKEDMAIL, &newmarkedmail_pixmap[0]);
350 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NOMAIL_OFFLINE, &nomail_pixmap[1]);
351 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMAIL_OFFLINE, &unreadmail_pixmap[1]);
352 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMAIL_OFFLINE, &newmail_pixmap[1]);
353 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMARKEDMAIL_OFFLINE, &unreadmarkedmail_pixmap[1]);
354 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMARKEDMAIL_OFFLINE, &newmarkedmail_pixmap[1]);
362 static void create_trayicon()
364 GtkActionGroup *action_group;
365 trayicon = egg_tray_icon_new("Claws Mail");
366 gtk_widget_realize(GTK_WIDGET(trayicon));
367 gtk_window_set_default_size(GTK_WINDOW(trayicon), 16, 16);
368 gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
370 trayicon_update_theme(NULL, NULL);
372 eventbox = gtk_event_box_new();
373 gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
374 gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
376 image = gtk_image_new_from_pixbuf(nomail_pixmap[0]);
377 gtk_container_add(GTK_CONTAINER(eventbox), image);
380 g_signal_connect(G_OBJECT(trayicon), "destroy",
381 G_CALLBACK(destroy_cb), NULL);
382 g_signal_connect(G_OBJECT(trayicon), "size-request",
383 G_CALLBACK(resize_cb), NULL);
384 g_signal_connect(G_OBJECT(eventbox), "button-press-event",
385 G_CALLBACK(click_cb), NULL);
387 #if !(GTK_CHECK_VERSION(2,12,0))
388 tooltips = gtk_tooltips_new();
389 gtk_tooltips_enable(tooltips);
392 action_group = cm_menu_create_action_group("TrayiconPopup", trayicon_popup_menu_entries,
393 G_N_ELEMENTS(trayicon_popup_menu_entries), (gpointer)NULL);
394 gtk_action_group_add_toggle_actions(action_group, trayicon_popup_toggle_menu_entries,
395 G_N_ELEMENTS(trayicon_popup_toggle_menu_entries), (gpointer)NULL);
397 MENUITEM_ADDUI("/Menus", "TrayiconPopup", "TrayiconPopup", GTK_UI_MANAGER_MENU)
398 MENUITEM_ADDUI("/Menus/TrayiconPopup", "GetMail", "TrayiconPopup/GetMail", GTK_UI_MANAGER_MENUITEM)
399 MENUITEM_ADDUI("/Menus/TrayiconPopup", "Separator1", "TrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
400 MENUITEM_ADDUI("/Menus/TrayiconPopup", "Email", "TrayiconPopup/Email", GTK_UI_MANAGER_MENUITEM)
401 MENUITEM_ADDUI("/Menus/TrayiconPopup", "EmailAcc", "TrayiconPopup/EmailAcc", GTK_UI_MANAGER_MENU)
402 MENUITEM_ADDUI("/Menus/TrayiconPopup", "Separator2", "TrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
403 MENUITEM_ADDUI("/Menus/TrayiconPopup", "OpenAB", "TrayiconPopup/OpenAB", GTK_UI_MANAGER_MENUITEM)
404 MENUITEM_ADDUI("/Menus/TrayiconPopup", "Separator3", "TrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
405 MENUITEM_ADDUI("/Menus/TrayiconPopup", "ToggleOffline", "TrayiconPopup/ToggleOffline", GTK_UI_MANAGER_MENUITEM)
406 MENUITEM_ADDUI("/Menus/TrayiconPopup", "Separator4", "TrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
407 MENUITEM_ADDUI("/Menus/TrayiconPopup", "Exit", "TrayiconPopup/Exit", GTK_UI_MANAGER_MENUITEM)
409 traymenu_popup = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
410 gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/TrayiconPopup")) );
412 gtk_widget_show_all(GTK_WIDGET(trayicon));
417 int plugin_init(gchar **error)
419 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
420 VERSION_NUMERIC, PLUGIN_NAME, error))
423 item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
424 if (item_hook_id == -1) {
425 *error = g_strdup(_("Failed to register folder item update hook"));
429 folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, folder_update_hook, NULL);
430 if (folder_hook_id == -1) {
431 *error = g_strdup(_("Failed to register folder update hook"));
435 offline_hook_id = hooks_register_hook (OFFLINE_SWITCH_HOOKLIST, offline_update_hook, NULL);
436 if (offline_hook_id == -1) {
437 *error = g_strdup(_("Failed to register offline switch hook"));
438 goto err_out_offline;
441 account_hook_id = hooks_register_hook (ACCOUNT_LIST_CHANGED_HOOKLIST, trayicon_set_accounts_hook, NULL);
442 if (account_hook_id == -1) {
443 *error = g_strdup(_("Failed to register account list changed hook"));
444 goto err_out_account;
447 close_hook_id = hooks_register_hook (MAIN_WINDOW_CLOSE, trayicon_close_hook, NULL);
448 if (close_hook_id == -1) {
449 *error = g_strdup(_("Failed to register close hook"));
453 iconified_hook_id = hooks_register_hook (MAIN_WINDOW_GOT_ICONIFIED, trayicon_got_iconified_hook, NULL);
454 if (iconified_hook_id == -1) {
455 *error = g_strdup(_("Failed to register got iconified hook"));
456 goto err_out_iconified;
459 theme_hook_id = hooks_register_hook(THEME_CHANGED_HOOKLIST, trayicon_update_theme, NULL);
460 if (theme_hook_id == -1) {
461 *error = g_strdup(_("Failed to register theme change hook"));
466 trayicon_set_accounts_hook(NULL, NULL);
468 trayicon_prefs_init();
470 if (trayicon_prefs.hide_at_startup && claws_is_starting()) {
471 MainWindow *mainwin = mainwindow_get_mainwindow();
473 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
474 main_window_hide(mainwin);
475 main_set_show_at_startup(FALSE);
481 hooks_unregister_hook(MAIN_WINDOW_GOT_ICONIFIED, iconified_hook_id);
483 hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
485 hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
487 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
489 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
491 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
496 gboolean plugin_done(void)
498 trayicon_prefs_done();
500 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
501 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
502 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
503 hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
504 hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
505 hooks_unregister_hook(MAIN_WINDOW_GOT_ICONIFIED, iconified_hook_id);
506 hooks_unregister_hook(THEME_CHANGED_HOOKLIST, theme_hook_id);
508 if (claws_is_exiting())
511 g_signal_handler_disconnect(G_OBJECT(trayicon), destroy_signal_id);
513 gtk_widget_destroy(GTK_WIDGET(trayicon));
515 while (gtk_events_pending()) {
516 gtk_main_iteration();
521 const gchar *plugin_name(void)
526 const gchar *plugin_desc(void)
528 return _("This plugin places a mailbox icon in the system tray that "
529 "indicates if you have new or unread mail.\n"
531 "The mailbox is empty if you have no unread mail, otherwise "
532 "it contains a letter. A tooltip shows new, unread and total "
533 "number of messages.");
536 const gchar *plugin_type(void)
541 const gchar *plugin_licence(void)
546 const gchar *plugin_version(void)
552 /* popup menu callbacks */
553 static void trayicon_get_all_cb( GtkAction *action, gpointer data )
555 MainWindow *mainwin = mainwindow_get_mainwindow();
556 inc_all_account_mail_cb(mainwin, 0, NULL);
559 static void trayicon_compose_cb( GtkAction *action, gpointer data )
561 MainWindow *mainwin = mainwindow_get_mainwindow();
562 compose_mail_cb(mainwin, 0, NULL);
565 static void trayicon_compose_acc_cb( GtkMenuItem *menuitem, gpointer data )
567 compose_new((PrefsAccount *)data, NULL, NULL);
570 static void trayicon_addressbook_cb( GtkAction *action, gpointer data )
572 addressbook_open(NULL);
575 static void trayicon_toggle_offline_cb( GtkAction *action, gpointer data )
577 /* toggle offline mode if menu checkitem has been clicked */
578 if (!updating_menu) {
579 MainWindow *mainwin = mainwindow_get_mainwindow();
580 main_window_toggle_work_offline(mainwin, !prefs_common.work_offline, TRUE);
584 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
586 if (prefs_common.clean_on_exit) {
587 if (!main_window_empty_trash(mainwin, prefs_common.ask_on_clean, TRUE))
591 if (prefs_common.confirm_on_exit) {
592 if (alertpanel(_("Exit"), _("Exit Claws Mail?"),
593 GTK_STOCK_CANCEL, GTK_STOCK_OK,
594 NULL) != G_ALERTALTERNATE) {
597 manage_window_focus_in(mainwin->window, NULL, NULL);
600 app_will_exit(NULL, mainwin);
603 static void trayicon_exit_cb( GtkAction *action, gpointer data )
605 MainWindow *mainwin = mainwindow_get_mainwindow();
607 if (mainwin->lock_count == 0) {
608 app_exit_cb(mainwin, 0, NULL);
612 struct PluginFeature *plugin_provides(void)
614 static struct PluginFeature features[] =
615 { {PLUGIN_NOTIFIER, N_("Trayicon")},
616 {PLUGIN_NOTHING, NULL}};