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 "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;
61 static GdkPixmap *newmail_pixmap[2];
62 static GdkPixmap *newmail_bitmap[2];
63 static GdkPixmap *unreadmail_pixmap[2];
64 static GdkPixmap *unreadmail_bitmap[2];
65 static GdkPixmap *newmarkedmail_pixmap[2];
66 static GdkPixmap *newmarkedmail_bitmap[2];
67 static GdkPixmap *unreadmarkedmail_pixmap[2];
68 static GdkPixmap *unreadmarkedmail_bitmap[2];
69 static GdkPixmap *nomail_pixmap[2];
70 static GdkPixmap *nomail_bitmap[2];
72 static EggTrayIcon *trayicon;
73 static GtkWidget *eventbox;
74 static GtkWidget *image;
75 static GtkTooltips *tooltips;
76 static GtkWidget *traymenu_popup;
77 static GtkItemFactory *traymenu_factory;
78 static gboolean updating_menu = FALSE;
80 guint destroy_signal_id;
87 TRAYICON_UNREADMARKED,
91 static void trayicon_get_all_cb (gpointer data, guint action, GtkWidget *widget);
92 static void trayicon_compose_cb (gpointer data, guint action, GtkWidget *widget);
93 static void trayicon_compose_acc_cb (GtkMenuItem *menuitem, gpointer data );
94 static void trayicon_addressbook_cb (gpointer data, guint action, GtkWidget *widget);
95 static void trayicon_exit_cb (gpointer data, guint action, GtkWidget *widget);
96 static void trayicon_toggle_offline_cb (gpointer data, guint action, GtkWidget *widget);
97 static void resize_cb (GtkWidget *widget, GtkRequisition *req, gpointer user_data);
99 static GtkItemFactoryEntry trayicon_popup_menu_entries[] =
101 {N_("/_Get Mail"), NULL, trayicon_get_all_cb, 0, NULL},
102 {"/---", NULL, NULL, 0, "<Separator>"},
103 {N_("/_Email"), NULL, trayicon_compose_cb, 0, NULL},
104 {N_("/_Email from account"), NULL, NULL, 0, "<Branch>"},
105 {"/---", NULL, NULL, 0, "<Separator>"},
106 {N_("/Open A_ddressbook"), NULL, trayicon_addressbook_cb, 0, NULL},
107 {"/---", NULL, NULL, 0, "<Separator>"},
108 {N_("/_Work Offline"), NULL, trayicon_toggle_offline_cb, 0, "<CheckItem>"},
109 {"/---", NULL, NULL, 0, "<Separator>"},
110 {N_("/E_xit Claws Mail"), NULL, trayicon_exit_cb, 0, NULL}
113 static gboolean trayicon_set_accounts_hook(gpointer source, gpointer data)
115 GList *cur_ac, *cur_item = NULL;
118 PrefsAccount *ac_prefs;
120 GList *account_list = account_get_list();
122 menu = gtk_item_factory_get_widget(traymenu_factory,
123 "/Email from account");
125 /* destroy all previous menu item */
126 cur_item = GTK_MENU_SHELL(menu)->children;
127 while (cur_item != NULL) {
128 GList *next = cur_item->next;
129 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
133 for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
134 ac_prefs = (PrefsAccount *)cur_ac->data;
136 menuitem = gtk_menu_item_new_with_label
137 (ac_prefs->account_name ? ac_prefs->account_name
139 gtk_widget_show(menuitem);
140 gtk_menu_append(GTK_MENU(menu), menuitem);
141 g_signal_connect(G_OBJECT(menuitem), "activate",
142 G_CALLBACK(trayicon_compose_acc_cb),
148 static void set_trayicon_pixmap(TrayIconType icontype)
150 GdkPixmap *pixmap = NULL;
151 GdkBitmap *bitmap = NULL;
152 static GdkPixmap *last_pixmap = NULL;
156 pixmap = newmail_pixmap[prefs_common.work_offline];
157 bitmap = newmail_bitmap[prefs_common.work_offline];
159 case TRAYICON_NEWMARKED:
160 pixmap = newmarkedmail_pixmap[prefs_common.work_offline];
161 bitmap = newmarkedmail_bitmap[prefs_common.work_offline];
163 case TRAYICON_UNREAD:
164 pixmap = unreadmail_pixmap[prefs_common.work_offline];
165 bitmap = unreadmail_bitmap[prefs_common.work_offline];
167 case TRAYICON_UNREADMARKED:
168 pixmap = unreadmarkedmail_pixmap[prefs_common.work_offline];
169 bitmap = unreadmarkedmail_bitmap[prefs_common.work_offline];
172 pixmap = nomail_pixmap[prefs_common.work_offline];
173 bitmap = nomail_bitmap[prefs_common.work_offline];
177 if (pixmap == last_pixmap) {
181 gtk_image_set_from_pixmap(GTK_IMAGE(image), pixmap, bitmap);
183 last_pixmap = pixmap;
186 static void update(FolderItem *removed_item)
188 guint new, unread, unreadmarked, marked, total;
190 TrayIconType icontype = TRAYICON_NOTHING;
192 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total);
194 total -= removed_item->total_msgs;
195 new -= removed_item->new_msgs;
196 unread -= removed_item->unread_msgs;
199 buf = g_strdup_printf(_("New %d, Unread: %d, Total: %d"), new, unread, total);
201 gtk_tooltips_set_tip(tooltips, eventbox, buf, "");
204 if (new > 0 && unreadmarked > 0) {
205 icontype = TRAYICON_NEWMARKED;
206 } else if (new > 0) {
207 icontype = TRAYICON_NEW;
208 } else if (unreadmarked > 0) {
209 icontype = TRAYICON_UNREADMARKED;
210 } else if (unread > 0) {
211 icontype = TRAYICON_UNREAD;
214 set_trayicon_pixmap(icontype);
217 static gboolean folder_item_update_hook(gpointer source, gpointer data)
224 static gboolean folder_update_hook(gpointer source, gpointer data)
226 FolderUpdateData *hookdata;
228 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
229 update(hookdata->item);
236 static gboolean offline_update_hook(gpointer source, gpointer data)
242 static gboolean trayicon_close_hook(gpointer source, gpointer data)
245 gboolean *close_allowed = (gboolean*)source;
247 if (trayicon_prefs.close_to_tray) {
248 MainWindow *mainwin = mainwindow_get_mainwindow();
250 *close_allowed = FALSE;
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 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), TRUE);
270 static void resize_cb(GtkWidget *widget, GtkRequisition *req,
276 static gboolean click_cb(GtkWidget * widget,
277 GdkEventButton * event, gpointer user_data)
285 mainwin = mainwindow_get_mainwindow();
287 switch (event->button) {
289 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))) {
290 if ((gdk_window_get_state(GTK_WIDGET(mainwin->window)->window)&GDK_WINDOW_STATE_ICONIFIED)
291 || mainwindow_is_obscured()) {
292 gtk_window_deiconify(GTK_WINDOW(mainwin->window));
293 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(mainwin->window), FALSE);
294 main_window_show(mainwin);
295 gtk_window_present(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));
307 /* tell callbacks to skip any event */
308 updating_menu = TRUE;
309 /* initialize checkitem according to current offline state */
310 gtk_check_menu_item_set_active(
311 GTK_CHECK_MENU_ITEM(gtk_item_factory_get_item(traymenu_factory,
312 "/Work Offline")), prefs_common.work_offline);
313 gtk_widget_set_sensitive(
314 GTK_WIDGET(gtk_item_factory_get_item(traymenu_factory,
315 "/Get Mail")), mainwin->lock_count == 0);
316 updating_menu = FALSE;
318 gtk_menu_popup( GTK_MENU(traymenu_popup), NULL, NULL, NULL, NULL,
319 event->button, event->time );
327 static void create_trayicon(void);
329 static void destroy_cb(GtkWidget *widget, gpointer *data)
331 debug_print("Widget destroyed\n");
336 static void create_trayicon()
340 trayicon = egg_tray_icon_new("Claws Mail");
341 gtk_widget_realize(GTK_WIDGET(trayicon));
342 gtk_window_set_default_size(GTK_WINDOW(trayicon), 16, 16);
343 gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
345 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NOMAIL, &nomail_pixmap[0], &nomail_bitmap[0]);
346 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMAIL, &unreadmail_pixmap[0], &unreadmail_bitmap[0]);
347 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMAIL, &newmail_pixmap[0], &newmail_bitmap[0]);
348 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMARKEDMAIL, &unreadmarkedmail_pixmap[0], &unreadmarkedmail_bitmap[0]);
349 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMARKEDMAIL, &newmarkedmail_pixmap[0], &newmarkedmail_bitmap[0]);
351 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NOMAIL_OFFLINE, &nomail_pixmap[1], &nomail_bitmap[1]);
352 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMAIL_OFFLINE, &unreadmail_pixmap[1], &unreadmail_bitmap[1]);
353 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMAIL_OFFLINE, &newmail_pixmap[1], &newmail_bitmap[1]);
354 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_UNREADMARKEDMAIL_OFFLINE, &unreadmarkedmail_pixmap[1], &unreadmarkedmail_bitmap[1]);
355 stock_pixmap_gdk(GTK_WIDGET(trayicon), STOCK_PIXMAP_TRAY_NEWMARKEDMAIL_OFFLINE, &newmarkedmail_pixmap[1], &newmarkedmail_bitmap[1]);
358 eventbox = gtk_event_box_new();
359 gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
360 gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
362 image = gtk_image_new_from_pixmap(nomail_pixmap[0], nomail_bitmap[0]);
363 gtk_container_add(GTK_CONTAINER(eventbox), image);
366 g_signal_connect(G_OBJECT(trayicon), "destroy",
367 G_CALLBACK(destroy_cb), NULL);
368 g_signal_connect(GTK_OBJECT(trayicon), "size-request",
369 G_CALLBACK(resize_cb), NULL);
370 g_signal_connect(G_OBJECT(eventbox), "button-press-event",
371 G_CALLBACK(click_cb), NULL);
373 tooltips = gtk_tooltips_new();
374 gtk_tooltips_enable(tooltips);
376 n_entries = sizeof(trayicon_popup_menu_entries) /
377 sizeof(trayicon_popup_menu_entries[0]);
378 traymenu_popup = menu_create_items(trayicon_popup_menu_entries,
379 n_entries, "<TrayiconMenu>", &traymenu_factory,
382 gtk_widget_show_all(GTK_WIDGET(trayicon));
387 int plugin_init(gchar **error)
389 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
390 VERSION_NUMERIC, PLUGIN_NAME, error))
393 item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
394 if (item_hook_id == -1) {
395 *error = g_strdup(_("Failed to register folder item update hook"));
399 folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, folder_update_hook, NULL);
400 if (folder_hook_id == -1) {
401 *error = g_strdup(_("Failed to register folder update hook"));
405 offline_hook_id = hooks_register_hook (OFFLINE_SWITCH_HOOKLIST, offline_update_hook, NULL);
406 if (offline_hook_id == -1) {
407 *error = g_strdup(_("Failed to register offline switch hook"));
408 goto err_out_offline;
411 account_hook_id = hooks_register_hook (ACCOUNT_LIST_CHANGED_HOOKLIST, trayicon_set_accounts_hook, NULL);
412 if (account_hook_id == -1) {
413 *error = g_strdup(_("Failed to register account list changed hook"));
414 goto err_out_account;
417 close_hook_id = hooks_register_hook (MAIN_WINDOW_CLOSE, trayicon_close_hook, NULL);
418 if (close_hook_id == -1) {
419 *error = g_strdup(_("Failed to register close hook"));
423 iconified_hook_id = hooks_register_hook (MAIN_WINDOW_GOT_ICONIFIED, trayicon_got_iconified_hook, NULL);
424 if (iconified_hook_id == -1) {
425 *error = g_strdup(_("Failed to register got iconified hook"));
426 goto err_out_iconified;
430 trayicon_set_accounts_hook(NULL, NULL);
432 trayicon_prefs_init();
434 if (trayicon_prefs.hide_at_startup && claws_is_starting()) {
435 MainWindow *mainwin = mainwindow_get_mainwindow();
437 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
438 main_window_hide(mainwin);
439 main_set_show_at_startup(FALSE);
445 hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
447 hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
449 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
451 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
453 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
458 gboolean plugin_done(void)
460 trayicon_prefs_done();
462 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
463 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
464 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
465 hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
466 hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
467 hooks_unregister_hook(MAIN_WINDOW_GOT_ICONIFIED, iconified_hook_id);
469 if (claws_is_exiting())
472 g_signal_handler_disconnect(G_OBJECT(trayicon), destroy_signal_id);
474 gtk_widget_destroy(GTK_WIDGET(trayicon));
476 while (gtk_events_pending()) {
477 gtk_main_iteration();
482 const gchar *plugin_name(void)
487 const gchar *plugin_desc(void)
489 return _("This plugin places a mailbox icon in the system tray that "
490 "indicates if you have new or unread mail.\n"
492 "The mailbox is empty if you have no unread mail, otherwise "
493 "it contains a letter. A tooltip shows new, unread and total "
494 "number of messages.");
497 const gchar *plugin_type(void)
502 const gchar *plugin_licence(void)
507 const gchar *plugin_version(void)
513 /* popup menu callbacks */
514 static void trayicon_get_all_cb( gpointer data, guint action, GtkWidget *widget )
516 MainWindow *mainwin = mainwindow_get_mainwindow();
517 inc_all_account_mail_cb(mainwin, 0, NULL);
520 static void trayicon_compose_cb( gpointer data, guint action, GtkWidget *widget )
522 MainWindow *mainwin = mainwindow_get_mainwindow();
523 compose_mail_cb(mainwin, 0, NULL);
526 static void trayicon_compose_acc_cb( GtkMenuItem *menuitem, gpointer data )
528 compose_new((PrefsAccount *)data, NULL, NULL);
531 static void trayicon_addressbook_cb( gpointer data, guint action, GtkWidget *widget )
533 addressbook_open(NULL);
536 static void trayicon_toggle_offline_cb( gpointer data, guint action, GtkWidget *widget )
538 /* toggle offline mode if menu checkitem has been clicked */
539 if (!updating_menu) {
540 MainWindow *mainwin = mainwindow_get_mainwindow();
541 main_window_toggle_work_offline(mainwin, !prefs_common.work_offline, TRUE);
545 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
547 if (prefs_common.confirm_on_exit) {
548 if (alertpanel(_("Exit"), _("Exit Claws Mail?"),
549 GTK_STOCK_CANCEL, GTK_STOCK_OK,
550 NULL) != G_ALERTALTERNATE) {
553 manage_window_focus_in(mainwin->window, NULL, NULL);
556 app_will_exit(NULL, mainwin);
559 static void trayicon_exit_cb( gpointer data, guint action, GtkWidget *widget )
561 MainWindow *mainwin = mainwindow_get_mainwindow();
563 if (mainwin->lock_count == 0) {
564 app_exit_cb(mainwin, 0, NULL);
568 struct PluginFeature *plugin_provides(void)
570 static struct PluginFeature features[] =
571 { {PLUGIN_NOTIFIER, N_("Trayicon")},
572 {PLUGIN_NOTHING, NULL}};