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;
71 static GtkWidget *focused_widget = NULL;
73 #if !(GTK_CHECK_VERSION(2,12,0))
74 static GtkTooltips *tooltips;
76 static GtkWidget *traymenu_popup;
77 static gboolean updating_menu = FALSE;
79 guint destroy_signal_id;
86 TRAYICON_UNREADMARKED,
90 static void trayicon_get_all_cb (GtkAction *action, gpointer data);
91 static void trayicon_compose_cb (GtkAction *action, gpointer data);
92 static void trayicon_compose_acc_cb (GtkMenuItem *menuitem, gpointer data );
93 static void trayicon_addressbook_cb (GtkAction *action, gpointer data);
94 static void trayicon_exit_cb (GtkAction *action, gpointer data);
95 static void trayicon_toggle_offline_cb (GtkAction *action, gpointer data);
96 static void resize_cb (GtkWidget *widget, GtkRequisition *req, gpointer user_data);
98 static GtkActionEntry trayicon_popup_menu_entries[] =
100 {"TrayiconPopup", NULL, "TrayiconPopup" },
101 {"TrayiconPopup/GetMail", NULL, N_("_Get Mail"), NULL, NULL, G_CALLBACK(trayicon_get_all_cb) },
102 {"TrayiconPopup/---", NULL, "---", NULL, NULL, G_CALLBACK(trayicon_compose_cb) },
103 {"TrayiconPopup/Email", NULL, N_("_Email"), NULL, NULL, G_CALLBACK(trayicon_compose_cb) },
104 {"TrayiconPopup/EmailAcc", NULL, N_("E_mail from account"), NULL, NULL, NULL },
105 {"TrayiconPopup/OpenAB", NULL, N_("Open A_ddressbook"), NULL, NULL, G_CALLBACK(trayicon_addressbook_cb) },
106 {"TrayiconPopup/Exit", NULL, N_("E_xit Claws Mail"), NULL, NULL, G_CALLBACK(trayicon_exit_cb) },
109 static GtkToggleActionEntry trayicon_popup_toggle_menu_entries[] =
111 {"TrayiconPopup/ToggleOffline", NULL, N_("_Work Offline"), NULL, NULL, G_CALLBACK(trayicon_toggle_offline_cb) },
114 static gboolean trayicon_set_accounts_hook(gpointer source, gpointer data)
117 GtkWidget *menu, *submenu;
119 PrefsAccount *ac_prefs;
121 GList *account_list = account_get_list();
123 menu = gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/TrayiconPopup/EmailAcc");
124 gtk_widget_show(menu);
126 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu), NULL);
127 submenu = gtk_menu_new();
128 for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
129 ac_prefs = (PrefsAccount *)cur_ac->data;
131 menuitem = gtk_menu_item_new_with_label
132 (ac_prefs->account_name ? ac_prefs->account_name
134 gtk_widget_show(menuitem);
135 gtk_menu_shell_append(GTK_MENU_SHELL(submenu), menuitem);
136 g_signal_connect(G_OBJECT(menuitem), "activate",
137 G_CALLBACK(trayicon_compose_acc_cb),
140 gtk_widget_show(submenu);
141 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu), submenu);
145 static void set_trayicon_pixmap(TrayIconType icontype)
147 GdkPixbuf *pixmap = NULL;
148 static GdkPixbuf *last_pixmap = NULL;
152 pixmap = newmail_pixmap[prefs_common.work_offline];
154 case TRAYICON_NEWMARKED:
155 pixmap = newmarkedmail_pixmap[prefs_common.work_offline];
157 case TRAYICON_UNREAD:
158 pixmap = unreadmail_pixmap[prefs_common.work_offline];
160 case TRAYICON_UNREADMARKED:
161 pixmap = unreadmarkedmail_pixmap[prefs_common.work_offline];
164 pixmap = nomail_pixmap[prefs_common.work_offline];
168 if (pixmap == last_pixmap) {
172 gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixmap);
174 last_pixmap = pixmap;
177 static void update(FolderItem *removed_item)
179 guint new, unread, unreadmarked, marked, total;
180 guint replied, forwarded, locked, ignored, watched;
182 TrayIconType icontype = TRAYICON_NOTHING;
184 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total,
185 &replied, &forwarded, &locked, &ignored,
188 total -= removed_item->total_msgs;
189 new -= removed_item->new_msgs;
190 unread -= removed_item->unread_msgs;
193 buf = g_strdup_printf(_("New %d, Unread: %d, Total: %d"), new, unread, total);
195 #if !(GTK_CHECK_VERSION(2,12,0))
196 gtk_tooltips_set_tip(tooltips, eventbox, buf, "");
198 gtk_widget_set_tooltip_text(eventbox, buf);
202 if (new > 0 && unreadmarked > 0) {
203 icontype = TRAYICON_NEWMARKED;
204 } else if (new > 0) {
205 icontype = TRAYICON_NEW;
206 } else if (unreadmarked > 0) {
207 icontype = TRAYICON_UNREADMARKED;
208 } else if (unread > 0) {
209 icontype = TRAYICON_UNREAD;
212 set_trayicon_pixmap(icontype);
215 static gboolean folder_item_update_hook(gpointer source, gpointer data)
222 static gboolean folder_update_hook(gpointer source, gpointer data)
224 FolderUpdateData *hookdata;
226 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
227 update(hookdata->item);
234 static gboolean offline_update_hook(gpointer source, gpointer data)
240 static gboolean trayicon_close_hook(gpointer source, gpointer data)
243 gboolean *close_allowed = (gboolean*)source;
245 if (trayicon_prefs.close_to_tray) {
246 MainWindow *mainwin = mainwindow_get_mainwindow();
248 *close_allowed = FALSE;
249 focused_widget = gtk_window_get_focus(GTK_WINDOW(mainwin->window));
251 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
252 main_window_hide(mainwin);
258 static gboolean trayicon_got_iconified_hook(gpointer source, gpointer data)
260 MainWindow *mainwin = mainwindow_get_mainwindow();
262 if (trayicon_prefs.hide_when_iconified
263 && GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))
264 && !gtk_window_get_skip_taskbar_hint(GTK_WINDOW(mainwin->window))) {
265 focused_widget = gtk_window_get_focus(GTK_WINDOW(mainwin->window));
266 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), TRUE);
271 static void resize_cb(GtkWidget *widget, GtkRequisition *req,
277 static void fix_folderview_scroll(MainWindow *mainwin)
279 static gboolean fix_done = FALSE;
284 gtk_widget_queue_resize(mainwin->folderview->ctree);
289 static gboolean click_cb(GtkWidget * widget,
290 GdkEventButton * event, gpointer user_data)
298 mainwin = mainwindow_get_mainwindow();
300 switch (event->button) {
302 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))) {
303 if ((gdk_window_get_state(GTK_WIDGET(mainwin->window)->window)&GDK_WINDOW_STATE_ICONIFIED)
304 || mainwindow_is_obscured()) {
305 gtk_window_deiconify(GTK_WINDOW(mainwin->window));
306 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), FALSE);
307 main_window_show(mainwin);
308 gtk_window_present(GTK_WINDOW(mainwin->window));
309 gtk_window_set_focus(GTK_WINDOW(mainwin->window), focused_widget);
311 focused_widget = gtk_window_get_focus(GTK_WINDOW(mainwin->window));
312 main_window_hide(mainwin);
315 gtk_window_deiconify(GTK_WINDOW(mainwin->window));
316 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), FALSE);
317 main_window_show(mainwin);
318 gtk_window_present(GTK_WINDOW(mainwin->window));
319 fix_folderview_scroll(mainwin);
320 gtk_window_set_focus(GTK_WINDOW(mainwin->window), focused_widget);
324 /* tell callbacks to skip any event */
325 updating_menu = TRUE;
326 /* initialize checkitem according to current offline state */
327 cm_toggle_menu_set_active("TrayiconPopup/ToggleOffline", prefs_common.work_offline);
328 cm_menu_set_sensitive("TrayiconPopup/GetMail", mainwin->lock_count == 0);
329 updating_menu = FALSE;
331 gtk_menu_popup( GTK_MENU(traymenu_popup), NULL, NULL, NULL, NULL,
332 event->button, event->time );
340 static void create_trayicon(void);
342 static void destroy_cb(GtkWidget *widget, gpointer *data)
344 debug_print("Widget destroyed\n");
349 static gboolean trayicon_update_theme(gpointer source, gpointer data)
351 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NOMAIL, &nomail_pixmap[0]);
352 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMAIL, &unreadmail_pixmap[0]);
353 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMAIL, &newmail_pixmap[0]);
354 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMARKEDMAIL, &unreadmarkedmail_pixmap[0]);
355 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMARKEDMAIL, &newmarkedmail_pixmap[0]);
357 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NOMAIL_OFFLINE, &nomail_pixmap[1]);
358 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMAIL_OFFLINE, &unreadmail_pixmap[1]);
359 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMAIL_OFFLINE, &newmail_pixmap[1]);
360 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMARKEDMAIL_OFFLINE, &unreadmarkedmail_pixmap[1]);
361 stock_pixbuf_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMARKEDMAIL_OFFLINE, &newmarkedmail_pixmap[1]);
369 static void create_trayicon()
371 GtkActionGroup *action_group;
372 trayicon = egg_tray_icon_new("Claws Mail");
373 gtk_widget_realize(GTK_WIDGET(trayicon));
374 gtk_window_set_default_size(GTK_WINDOW(trayicon), 16, 16);
375 gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
377 trayicon_update_theme(NULL, NULL);
379 eventbox = gtk_event_box_new();
380 gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
381 gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
383 image = gtk_image_new_from_pixbuf(nomail_pixmap[0]);
384 gtk_container_add(GTK_CONTAINER(eventbox), image);
387 g_signal_connect(G_OBJECT(trayicon), "destroy",
388 G_CALLBACK(destroy_cb), NULL);
389 g_signal_connect(G_OBJECT(trayicon), "size-request",
390 G_CALLBACK(resize_cb), NULL);
391 g_signal_connect(G_OBJECT(eventbox), "button-press-event",
392 G_CALLBACK(click_cb), NULL);
394 #if !(GTK_CHECK_VERSION(2,12,0))
395 tooltips = gtk_tooltips_new();
396 gtk_tooltips_enable(tooltips);
399 action_group = cm_menu_create_action_group("TrayiconPopup", trayicon_popup_menu_entries,
400 G_N_ELEMENTS(trayicon_popup_menu_entries), (gpointer)NULL);
401 gtk_action_group_add_toggle_actions(action_group, trayicon_popup_toggle_menu_entries,
402 G_N_ELEMENTS(trayicon_popup_toggle_menu_entries), (gpointer)NULL);
404 MENUITEM_ADDUI("/Menus", "TrayiconPopup", "TrayiconPopup", GTK_UI_MANAGER_MENU)
405 MENUITEM_ADDUI("/Menus/TrayiconPopup", "GetMail", "TrayiconPopup/GetMail", GTK_UI_MANAGER_MENUITEM)
406 MENUITEM_ADDUI("/Menus/TrayiconPopup", "Separator1", "TrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
407 MENUITEM_ADDUI("/Menus/TrayiconPopup", "Email", "TrayiconPopup/Email", GTK_UI_MANAGER_MENUITEM)
408 MENUITEM_ADDUI("/Menus/TrayiconPopup", "EmailAcc", "TrayiconPopup/EmailAcc", GTK_UI_MANAGER_MENU)
409 MENUITEM_ADDUI("/Menus/TrayiconPopup", "Separator2", "TrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
410 MENUITEM_ADDUI("/Menus/TrayiconPopup", "OpenAB", "TrayiconPopup/OpenAB", GTK_UI_MANAGER_MENUITEM)
411 MENUITEM_ADDUI("/Menus/TrayiconPopup", "Separator3", "TrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
412 MENUITEM_ADDUI("/Menus/TrayiconPopup", "ToggleOffline", "TrayiconPopup/ToggleOffline", GTK_UI_MANAGER_MENUITEM)
413 MENUITEM_ADDUI("/Menus/TrayiconPopup", "Separator4", "TrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
414 MENUITEM_ADDUI("/Menus/TrayiconPopup", "Exit", "TrayiconPopup/Exit", GTK_UI_MANAGER_MENUITEM)
416 traymenu_popup = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
417 gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/TrayiconPopup")) );
419 gtk_widget_show_all(GTK_WIDGET(trayicon));
424 int plugin_init(gchar **error)
426 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
427 VERSION_NUMERIC, PLUGIN_NAME, error))
430 item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
431 if (item_hook_id == -1) {
432 *error = g_strdup(_("Failed to register folder item update hook"));
436 folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, folder_update_hook, NULL);
437 if (folder_hook_id == -1) {
438 *error = g_strdup(_("Failed to register folder update hook"));
442 offline_hook_id = hooks_register_hook (OFFLINE_SWITCH_HOOKLIST, offline_update_hook, NULL);
443 if (offline_hook_id == -1) {
444 *error = g_strdup(_("Failed to register offline switch hook"));
445 goto err_out_offline;
448 account_hook_id = hooks_register_hook (ACCOUNT_LIST_CHANGED_HOOKLIST, trayicon_set_accounts_hook, NULL);
449 if (account_hook_id == -1) {
450 *error = g_strdup(_("Failed to register account list changed hook"));
451 goto err_out_account;
454 close_hook_id = hooks_register_hook (MAIN_WINDOW_CLOSE, trayicon_close_hook, NULL);
455 if (close_hook_id == -1) {
456 *error = g_strdup(_("Failed to register close hook"));
460 iconified_hook_id = hooks_register_hook (MAIN_WINDOW_GOT_ICONIFIED, trayicon_got_iconified_hook, NULL);
461 if (iconified_hook_id == -1) {
462 *error = g_strdup(_("Failed to register got iconified hook"));
463 goto err_out_iconified;
466 theme_hook_id = hooks_register_hook(THEME_CHANGED_HOOKLIST, trayicon_update_theme, NULL);
467 if (theme_hook_id == -1) {
468 *error = g_strdup(_("Failed to register theme change hook"));
473 trayicon_set_accounts_hook(NULL, NULL);
475 trayicon_prefs_init();
477 if (trayicon_prefs.hide_at_startup && claws_is_starting()) {
478 MainWindow *mainwin = mainwindow_get_mainwindow();
480 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
481 main_window_hide(mainwin);
482 main_set_show_at_startup(FALSE);
488 hooks_unregister_hook(MAIN_WINDOW_GOT_ICONIFIED, iconified_hook_id);
490 hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
492 hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
494 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
496 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
498 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
503 gboolean plugin_done(void)
505 trayicon_prefs_done();
507 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
508 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
509 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
510 hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
511 hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
512 hooks_unregister_hook(MAIN_WINDOW_GOT_ICONIFIED, iconified_hook_id);
513 hooks_unregister_hook(THEME_CHANGED_HOOKLIST, theme_hook_id);
515 if (claws_is_exiting())
518 g_signal_handler_disconnect(G_OBJECT(trayicon), destroy_signal_id);
520 gtk_widget_destroy(GTK_WIDGET(trayicon));
522 while (gtk_events_pending()) {
523 gtk_main_iteration();
528 const gchar *plugin_name(void)
533 const gchar *plugin_desc(void)
535 return _("This plugin places a mailbox icon in the system tray that "
536 "indicates if you have new or unread mail.\n"
538 "The mailbox is empty if you have no unread mail, otherwise "
539 "it contains a letter. A tooltip shows new, unread and total "
540 "number of messages.");
543 const gchar *plugin_type(void)
548 const gchar *plugin_licence(void)
553 const gchar *plugin_version(void)
559 /* popup menu callbacks */
560 static void trayicon_get_all_cb( GtkAction *action, gpointer data )
562 MainWindow *mainwin = mainwindow_get_mainwindow();
563 inc_all_account_mail_cb(mainwin, 0, NULL);
566 static void trayicon_compose_cb( GtkAction *action, gpointer data )
568 MainWindow *mainwin = mainwindow_get_mainwindow();
569 compose_mail_cb(mainwin, 0, NULL);
572 static void trayicon_compose_acc_cb( GtkMenuItem *menuitem, gpointer data )
574 compose_new((PrefsAccount *)data, NULL, NULL);
577 static void trayicon_addressbook_cb( GtkAction *action, gpointer data )
579 addressbook_open(NULL);
582 static void trayicon_toggle_offline_cb( GtkAction *action, gpointer data )
584 /* toggle offline mode if menu checkitem has been clicked */
585 if (!updating_menu) {
586 MainWindow *mainwin = mainwindow_get_mainwindow();
587 main_window_toggle_work_offline(mainwin, !prefs_common.work_offline, TRUE);
591 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
593 if (prefs_common.clean_on_exit) {
594 if (!main_window_empty_trash(mainwin, prefs_common.ask_on_clean, TRUE))
598 if (prefs_common.confirm_on_exit) {
599 if (alertpanel(_("Exit"), _("Exit Claws Mail?"),
600 GTK_STOCK_CANCEL, GTK_STOCK_OK,
601 NULL) != G_ALERTALTERNATE) {
604 manage_window_focus_in(mainwin->window, NULL, NULL);
607 app_will_exit(NULL, mainwin);
610 static void trayicon_exit_cb( GtkAction *action, gpointer data )
612 MainWindow *mainwin = mainwindow_get_mainwindow();
614 if (mainwin->lock_count == 0) {
615 app_exit_cb(mainwin, 0, NULL);
619 struct PluginFeature *plugin_provides(void)
621 static struct PluginFeature features[] =
622 { {PLUGIN_NOTIFIER, N_("Trayicon")},
623 {PLUGIN_NOTHING, NULL}};