2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2011 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 "trayicon_prefs.h"
48 #include "stock_pixmap.h"
50 #define PLUGIN_NAME (_("Trayicon"))
52 static guint item_hook_id;
53 static guint folder_hook_id;
54 static guint offline_hook_id;
55 static guint account_hook_id;
56 static guint close_hook_id;
57 static guint iconified_hook_id;
58 static guint theme_hook_id;
60 static GdkPixbuf *newmail_pixbuf[2] = {NULL, NULL};
61 static GdkPixbuf *unreadmail_pixbuf[2] = {NULL, NULL};
62 static GdkPixbuf *newmarkedmail_pixbuf[2] = {NULL, NULL};
63 static GdkPixbuf *unreadmarkedmail_pixbuf[2] = {NULL, NULL};
64 static GdkPixbuf *nomail_pixbuf[2] = {NULL, NULL};
66 static GtkStatusIcon *trayicon;
67 static GtkWidget *focused_widget = NULL;
69 static GtkWidget *traymenu_popup;
70 static gboolean updating_menu = FALSE;
77 TRAYICON_UNREADMARKED,
81 static void trayicon_get_all_cb (GtkAction *action, gpointer data);
82 static void trayicon_compose_cb (GtkAction *action, gpointer data);
83 static void trayicon_compose_acc_cb (GtkMenuItem *menuitem, gpointer data );
84 static void trayicon_addressbook_cb (GtkAction *action, gpointer data);
85 static void trayicon_exit_cb (GtkAction *action, gpointer data);
86 static void trayicon_toggle_offline_cb (GtkAction *action, gpointer data);
88 static GtkActionEntry trayicon_popup_menu_entries[] =
90 {"TrayiconPopup", NULL, "TrayiconPopup" },
91 {"TrayiconPopup/GetMail", NULL, N_("_Get Mail"), NULL, NULL, G_CALLBACK(trayicon_get_all_cb) },
92 {"TrayiconPopup/---", NULL, "---", NULL, NULL, G_CALLBACK(trayicon_compose_cb) },
93 {"TrayiconPopup/Email", NULL, N_("_Email"), NULL, NULL, G_CALLBACK(trayicon_compose_cb) },
94 {"TrayiconPopup/EmailAcc", NULL, N_("E_mail from account"), NULL, NULL, NULL },
95 {"TrayiconPopup/OpenAB", NULL, N_("Open A_ddressbook"), NULL, NULL, G_CALLBACK(trayicon_addressbook_cb) },
96 {"TrayiconPopup/Exit", NULL, N_("E_xit Claws Mail"), NULL, NULL, G_CALLBACK(trayicon_exit_cb) },
99 static GtkToggleActionEntry trayicon_popup_toggle_menu_entries[] =
101 {"TrayiconPopup/ToggleOffline", NULL, N_("_Work Offline"), NULL, NULL, G_CALLBACK(trayicon_toggle_offline_cb) },
104 static gboolean trayicon_set_accounts_hook(gpointer source, gpointer data)
107 GtkWidget *menu, *submenu;
109 PrefsAccount *ac_prefs;
111 GList *account_list = account_get_list();
113 menu = gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/TrayiconPopup/EmailAcc");
114 gtk_widget_show(menu);
116 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu), NULL);
117 submenu = gtk_menu_new();
118 for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
119 ac_prefs = (PrefsAccount *)cur_ac->data;
121 menuitem = gtk_menu_item_new_with_label
122 (ac_prefs->account_name ? ac_prefs->account_name
124 gtk_widget_show(menuitem);
125 gtk_menu_shell_append(GTK_MENU_SHELL(submenu), menuitem);
126 g_signal_connect(G_OBJECT(menuitem), "activate",
127 G_CALLBACK(trayicon_compose_acc_cb),
130 gtk_widget_show(submenu);
131 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu), submenu);
135 static void set_trayicon_pixbuf(TrayIconType icontype)
137 GdkPixbuf *pixbuf = NULL;
138 static GdkPixbuf *last_pixbuf = NULL;
142 pixbuf = newmail_pixbuf[prefs_common.work_offline];
144 case TRAYICON_NEWMARKED:
145 pixbuf = newmarkedmail_pixbuf[prefs_common.work_offline];
147 case TRAYICON_UNREAD:
148 pixbuf = unreadmail_pixbuf[prefs_common.work_offline];
150 case TRAYICON_UNREADMARKED:
151 pixbuf = unreadmarkedmail_pixbuf[prefs_common.work_offline];
154 pixbuf = nomail_pixbuf[prefs_common.work_offline];
158 if (pixbuf == last_pixbuf) {
162 gtk_status_icon_set_from_pixbuf(GTK_STATUS_ICON(trayicon), pixbuf);
164 last_pixbuf = pixbuf;
167 static void update(FolderItem *removed_item)
169 guint new, unread, unreadmarked, marked, total;
170 guint replied, forwarded, locked, ignored, watched;
172 TrayIconType icontype = TRAYICON_NOTHING;
174 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total,
175 &replied, &forwarded, &locked, &ignored,
178 total -= removed_item->total_msgs;
179 new -= removed_item->new_msgs;
180 unread -= removed_item->unread_msgs;
183 buf = g_strdup_printf(_("New %d, Unread: %d, Total: %d"), new, unread, total);
185 #if !(GTK_CHECK_VERSION(2,16,0))
186 gtk_status_icon_set_tooltip(trayicon, buf);
188 gtk_status_icon_set_tooltip_text(trayicon, buf);
192 if (new > 0 && unreadmarked > 0) {
193 icontype = TRAYICON_NEWMARKED;
194 } else if (new > 0) {
195 icontype = TRAYICON_NEW;
196 } else if (unreadmarked > 0) {
197 icontype = TRAYICON_UNREADMARKED;
198 } else if (unread > 0) {
199 icontype = TRAYICON_UNREAD;
202 set_trayicon_pixbuf(icontype);
205 static gboolean folder_item_update_hook(gpointer source, gpointer data)
212 static gboolean folder_update_hook(gpointer source, gpointer data)
214 FolderUpdateData *hookdata;
216 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
217 update(hookdata->item);
224 static gboolean offline_update_hook(gpointer source, gpointer data)
230 static gboolean trayicon_close_hook(gpointer source, gpointer data)
233 gboolean *close_allowed = (gboolean*)source;
235 if (trayicon_prefs.close_to_tray) {
236 MainWindow *mainwin = mainwindow_get_mainwindow();
238 *close_allowed = FALSE;
239 focused_widget = gtk_window_get_focus(GTK_WINDOW(mainwin->window));
241 if (gtkut_widget_get_visible(GTK_WIDGET(mainwin->window)))
242 main_window_hide(mainwin);
248 static gboolean trayicon_got_iconified_hook(gpointer source, gpointer data)
250 MainWindow *mainwin = mainwindow_get_mainwindow();
252 if (trayicon_prefs.hide_when_iconified
253 && gtkut_widget_get_visible(GTK_WIDGET(mainwin->window))
254 && !gtk_window_get_skip_taskbar_hint(GTK_WINDOW(mainwin->window))) {
255 focused_widget = gtk_window_get_focus(GTK_WINDOW(mainwin->window));
256 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), TRUE);
261 static void fix_folderview_scroll(MainWindow *mainwin)
263 static gboolean fix_done = FALSE;
268 gtk_widget_queue_resize(mainwin->folderview->ctree);
273 static gboolean click_cb(GtkWidget * widget,
274 GdkEventButton * event, gpointer user_data)
282 mainwin = mainwindow_get_mainwindow();
284 switch (event->button) {
286 if (gtkut_widget_get_visible(GTK_WIDGET(mainwin->window))) {
287 if ((gdk_window_get_state(gtk_widget_get_window(
288 GTK_WIDGET(mainwin->window)))&GDK_WINDOW_STATE_ICONIFIED)
289 || mainwindow_is_obscured()) {
290 gtk_window_deiconify(GTK_WINDOW(mainwin->window));
291 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), FALSE);
292 main_window_show(mainwin);
293 gtk_window_present(GTK_WINDOW(mainwin->window));
294 gtk_window_set_focus(GTK_WINDOW(mainwin->window), focused_widget);
296 focused_widget = gtk_window_get_focus(GTK_WINDOW(mainwin->window));
297 main_window_hide(mainwin);
300 gtk_window_deiconify(GTK_WINDOW(mainwin->window));
301 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), FALSE);
302 main_window_show(mainwin);
303 gtk_window_present(GTK_WINDOW(mainwin->window));
304 fix_folderview_scroll(mainwin);
305 gtk_window_set_focus(GTK_WINDOW(mainwin->window), focused_widget);
309 /* tell callbacks to skip any event */
310 updating_menu = TRUE;
311 /* initialize checkitem according to current offline state */
312 cm_toggle_menu_set_active("TrayiconPopup/ToggleOffline", prefs_common.work_offline);
313 cm_menu_set_sensitive("TrayiconPopup/GetMail", mainwin->lock_count == 0);
314 updating_menu = FALSE;
316 gtk_menu_popup( GTK_MENU(traymenu_popup), NULL, NULL, NULL, NULL,
317 event->button, event->time );
325 static void create_trayicon(void);
327 static gboolean trayicon_update_theme(gpointer source, gpointer data)
329 MainWindow *mainwin = mainwindow_get_mainwindow();
330 stock_pixbuf_gdk(GTK_WIDGET(mainwin->window), STOCK_PIXMAP_TRAY_NOMAIL, &nomail_pixbuf[0]);
331 stock_pixbuf_gdk(GTK_WIDGET(mainwin->window), STOCK_PIXMAP_TRAY_UNREADMAIL, &unreadmail_pixbuf[0]);
332 stock_pixbuf_gdk(GTK_WIDGET(mainwin->window), STOCK_PIXMAP_TRAY_NEWMAIL, &newmail_pixbuf[0]);
333 stock_pixbuf_gdk(GTK_WIDGET(mainwin->window), STOCK_PIXMAP_TRAY_UNREADMARKEDMAIL, &unreadmarkedmail_pixbuf[0]);
334 stock_pixbuf_gdk(GTK_WIDGET(mainwin->window), STOCK_PIXMAP_TRAY_NEWMARKEDMAIL, &newmarkedmail_pixbuf[0]);
336 stock_pixbuf_gdk(GTK_WIDGET(mainwin->window), STOCK_PIXMAP_TRAY_NOMAIL_OFFLINE, &nomail_pixbuf[1]);
337 stock_pixbuf_gdk(GTK_WIDGET(mainwin->window), STOCK_PIXMAP_TRAY_UNREADMAIL_OFFLINE, &unreadmail_pixbuf[1]);
338 stock_pixbuf_gdk(GTK_WIDGET(mainwin->window), STOCK_PIXMAP_TRAY_NEWMAIL_OFFLINE, &newmail_pixbuf[1]);
339 stock_pixbuf_gdk(GTK_WIDGET(mainwin->window), STOCK_PIXMAP_TRAY_UNREADMARKEDMAIL_OFFLINE, &unreadmarkedmail_pixbuf[1]);
340 stock_pixbuf_gdk(GTK_WIDGET(mainwin->window), STOCK_PIXMAP_TRAY_NEWMARKEDMAIL_OFFLINE, &newmarkedmail_pixbuf[1]);
347 static void create_trayicon()
349 GtkActionGroup *action_group;
350 trayicon = gtk_status_icon_new();
351 #if GTK_CHECK_VERSION(2,18,0)
352 gtk_status_icon_set_title(GTK_STATUS_ICON(trayicon), _("Claws Mail"));
354 g_signal_connect(G_OBJECT(trayicon), "button-press-event",
355 G_CALLBACK(click_cb), NULL);
357 action_group = cm_menu_create_action_group("TrayiconPopup", trayicon_popup_menu_entries,
358 G_N_ELEMENTS(trayicon_popup_menu_entries), (gpointer)NULL);
359 gtk_action_group_add_toggle_actions(action_group, trayicon_popup_toggle_menu_entries,
360 G_N_ELEMENTS(trayicon_popup_toggle_menu_entries), (gpointer)NULL);
362 MENUITEM_ADDUI("/Menus", "TrayiconPopup", "TrayiconPopup", GTK_UI_MANAGER_MENU)
363 MENUITEM_ADDUI("/Menus/TrayiconPopup", "GetMail", "TrayiconPopup/GetMail", GTK_UI_MANAGER_MENUITEM)
364 MENUITEM_ADDUI("/Menus/TrayiconPopup", "Separator1", "TrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
365 MENUITEM_ADDUI("/Menus/TrayiconPopup", "Email", "TrayiconPopup/Email", GTK_UI_MANAGER_MENUITEM)
366 MENUITEM_ADDUI("/Menus/TrayiconPopup", "EmailAcc", "TrayiconPopup/EmailAcc", GTK_UI_MANAGER_MENU)
367 MENUITEM_ADDUI("/Menus/TrayiconPopup", "Separator2", "TrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
368 MENUITEM_ADDUI("/Menus/TrayiconPopup", "OpenAB", "TrayiconPopup/OpenAB", GTK_UI_MANAGER_MENUITEM)
369 MENUITEM_ADDUI("/Menus/TrayiconPopup", "Separator3", "TrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
370 MENUITEM_ADDUI("/Menus/TrayiconPopup", "ToggleOffline", "TrayiconPopup/ToggleOffline", GTK_UI_MANAGER_MENUITEM)
371 MENUITEM_ADDUI("/Menus/TrayiconPopup", "Separator4", "TrayiconPopup/---", GTK_UI_MANAGER_SEPARATOR)
372 MENUITEM_ADDUI("/Menus/TrayiconPopup", "Exit", "TrayiconPopup/Exit", GTK_UI_MANAGER_MENUITEM)
374 traymenu_popup = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
375 gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/TrayiconPopup")) );
377 trayicon_update_theme(NULL, NULL);
380 int plugin_init(gchar **error)
382 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
383 VERSION_NUMERIC, PLUGIN_NAME, error))
386 item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
387 if (item_hook_id == -1) {
388 *error = g_strdup(_("Failed to register folder item update hook"));
392 folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, folder_update_hook, NULL);
393 if (folder_hook_id == -1) {
394 *error = g_strdup(_("Failed to register folder update hook"));
398 offline_hook_id = hooks_register_hook (OFFLINE_SWITCH_HOOKLIST, offline_update_hook, NULL);
399 if (offline_hook_id == -1) {
400 *error = g_strdup(_("Failed to register offline switch hook"));
401 goto err_out_offline;
404 account_hook_id = hooks_register_hook (ACCOUNT_LIST_CHANGED_HOOKLIST, trayicon_set_accounts_hook, NULL);
405 if (account_hook_id == -1) {
406 *error = g_strdup(_("Failed to register account list changed hook"));
407 goto err_out_account;
410 close_hook_id = hooks_register_hook (MAIN_WINDOW_CLOSE, trayicon_close_hook, NULL);
411 if (close_hook_id == -1) {
412 *error = g_strdup(_("Failed to register close hook"));
416 iconified_hook_id = hooks_register_hook (MAIN_WINDOW_GOT_ICONIFIED, trayicon_got_iconified_hook, NULL);
417 if (iconified_hook_id == -1) {
418 *error = g_strdup(_("Failed to register got iconified hook"));
419 goto err_out_iconified;
422 theme_hook_id = hooks_register_hook(THEME_CHANGED_HOOKLIST, trayicon_update_theme, NULL);
423 if (theme_hook_id == -1) {
424 *error = g_strdup(_("Failed to register theme change hook"));
429 trayicon_set_accounts_hook(NULL, NULL);
431 trayicon_prefs_init();
433 if (trayicon_prefs.hide_at_startup && claws_is_starting()) {
434 MainWindow *mainwin = mainwindow_get_mainwindow();
436 if (gtkut_widget_get_visible(GTK_WIDGET(mainwin->window)))
437 main_window_hide(mainwin);
438 main_set_show_at_startup(FALSE);
444 hooks_unregister_hook(MAIN_WINDOW_GOT_ICONIFIED, iconified_hook_id);
446 hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
448 hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
450 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
452 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
454 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
459 gboolean plugin_done(void)
461 trayicon_prefs_done();
463 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
464 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
465 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
466 hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
467 hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
468 hooks_unregister_hook(MAIN_WINDOW_GOT_ICONIFIED, iconified_hook_id);
469 hooks_unregister_hook(THEME_CHANGED_HOOKLIST, theme_hook_id);
471 if (claws_is_exiting())
474 gtk_status_icon_set_visible(trayicon, FALSE);
475 g_object_unref(G_OBJECT(trayicon));
478 while (gtk_events_pending()) {
479 gtk_main_iteration();
484 const gchar *plugin_name(void)
489 const gchar *plugin_desc(void)
491 return _("This plugin places a mailbox icon in the system tray that "
492 "indicates if you have new or unread mail.\n"
494 "The mailbox is empty if you have no unread mail, otherwise "
495 "it contains a letter. A tooltip shows new, unread and total "
496 "number of messages.");
499 const gchar *plugin_type(void)
504 const gchar *plugin_licence(void)
509 const gchar *plugin_version(void)
515 /* popup menu callbacks */
516 static void trayicon_get_all_cb( GtkAction *action, gpointer data )
518 MainWindow *mainwin = mainwindow_get_mainwindow();
519 inc_all_account_mail_cb(mainwin, 0, NULL);
522 static void trayicon_compose_cb( GtkAction *action, gpointer data )
524 MainWindow *mainwin = mainwindow_get_mainwindow();
525 compose_mail_cb(mainwin, 0, NULL);
528 static void trayicon_compose_acc_cb( GtkMenuItem *menuitem, gpointer data )
530 compose_new((PrefsAccount *)data, NULL, NULL);
533 static void trayicon_addressbook_cb( GtkAction *action, gpointer data )
535 addressbook_open(NULL);
538 static void trayicon_toggle_offline_cb( GtkAction *action, gpointer data )
540 /* toggle offline mode if menu checkitem has been clicked */
541 if (!updating_menu) {
542 MainWindow *mainwin = mainwindow_get_mainwindow();
543 main_window_toggle_work_offline(mainwin, !prefs_common.work_offline, TRUE);
547 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
549 if (prefs_common.clean_on_exit) {
550 if (!main_window_empty_trash(mainwin, prefs_common.ask_on_clean, TRUE))
554 if (prefs_common.confirm_on_exit) {
555 if (alertpanel(_("Exit"), _("Exit Claws Mail?"),
556 GTK_STOCK_CANCEL, GTK_STOCK_OK,
557 NULL) != G_ALERTALTERNATE) {
560 manage_window_focus_in(mainwin->window, NULL, NULL);
563 app_will_exit(NULL, mainwin);
566 static void trayicon_exit_cb( GtkAction *action, gpointer data )
568 MainWindow *mainwin = mainwindow_get_mainwindow();
570 if (mainwin->lock_count == 0) {
571 app_exit_cb(mainwin, 0, NULL);
575 struct PluginFeature *plugin_provides(void)
577 static struct PluginFeature features[] =
578 { {PLUGIN_NOTIFIER, N_("Trayicon")},
579 {PLUGIN_NOTHING, NULL}};