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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 "newmarkedmail.xpm"
51 #include "unreadmarkedmail.xpm"
52 #include "newmail.xpm"
53 #include "unreadmail.xpm"
55 #include "newmarkedmail.offline.xpm"
56 #include "unreadmarkedmail.offline.xpm"
57 #include "newmail.offline.xpm"
58 #include "unreadmail.offline.xpm"
59 #include "nomail.offline.xpm"
61 #define PLUGIN_NAME (_("Trayicon"))
63 static guint item_hook_id;
64 static guint folder_hook_id;
65 static guint offline_hook_id;
66 static guint account_hook_id;
67 static guint close_hook_id;
69 static GdkPixmap *newmail_pixmap[2];
70 static GdkPixmap *newmail_bitmap[2];
71 static GdkPixmap *unreadmail_pixmap[2];
72 static GdkPixmap *unreadmail_bitmap[2];
73 static GdkPixmap *newmarkedmail_pixmap[2];
74 static GdkPixmap *newmarkedmail_bitmap[2];
75 static GdkPixmap *unreadmarkedmail_pixmap[2];
76 static GdkPixmap *unreadmarkedmail_bitmap[2];
77 static GdkPixmap *nomail_pixmap[2];
78 static GdkPixmap *nomail_bitmap[2];
80 static EggTrayIcon *trayicon;
81 static GtkWidget *eventbox;
82 static GtkWidget *image;
83 static GtkTooltips *tooltips;
84 static GtkWidget *traymenu_popup;
85 static GtkItemFactory *traymenu_factory;
86 static gboolean updating_menu = FALSE;
88 guint destroy_signal_id;
95 TRAYICON_UNREADMARKED,
99 static void trayicon_get_all_cb (gpointer data, guint action, GtkWidget *widget);
100 static void trayicon_compose_cb (gpointer data, guint action, GtkWidget *widget);
101 static void trayicon_compose_acc_cb (GtkMenuItem *menuitem, gpointer data );
102 static void trayicon_addressbook_cb (gpointer data, guint action, GtkWidget *widget);
103 static void trayicon_exit_cb (gpointer data, guint action, GtkWidget *widget);
104 static void trayicon_toggle_offline_cb (gpointer data, guint action, GtkWidget *widget);
105 static void resize_cb (GtkWidget *widget, GtkRequisition *req, gpointer user_data);
107 static GtkItemFactoryEntry trayicon_popup_menu_entries[] =
109 {N_("/_Get Mail"), NULL, trayicon_get_all_cb, 0, NULL},
110 {"/---", NULL, NULL, 0, "<Separator>"},
111 {N_("/_Email"), NULL, trayicon_compose_cb, 0, NULL},
112 {N_("/_Email from account"), NULL, NULL, 0, "<Branch>"},
113 {"/---", NULL, NULL, 0, "<Separator>"},
114 {N_("/Open A_ddressbook"), NULL, trayicon_addressbook_cb, 0, NULL},
115 {"/---", NULL, NULL, 0, "<Separator>"},
116 {N_("/_Work Offline"), NULL, trayicon_toggle_offline_cb, 0, "<CheckItem>"},
117 {"/---", NULL, NULL, 0, "<Separator>"},
118 {N_("/E_xit Claws Mail"), NULL, trayicon_exit_cb, 0, NULL}
121 static gboolean trayicon_set_accounts_hook(gpointer source, gpointer data)
123 GList *cur_ac, *cur_item = NULL;
126 PrefsAccount *ac_prefs;
128 GList *account_list = account_get_list();
130 menu = gtk_item_factory_get_widget(traymenu_factory,
131 "/Email from account");
133 /* destroy all previous menu item */
134 cur_item = GTK_MENU_SHELL(menu)->children;
135 while (cur_item != NULL) {
136 GList *next = cur_item->next;
137 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
141 for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
142 ac_prefs = (PrefsAccount *)cur_ac->data;
144 menuitem = gtk_menu_item_new_with_label
145 (ac_prefs->account_name ? ac_prefs->account_name
147 gtk_widget_show(menuitem);
148 gtk_menu_append(GTK_MENU(menu), menuitem);
149 g_signal_connect(G_OBJECT(menuitem), "activate",
150 G_CALLBACK(trayicon_compose_acc_cb),
156 static void set_trayicon_pixmap(TrayIconType icontype)
158 GdkPixmap *pixmap = NULL;
159 GdkBitmap *bitmap = NULL;
160 static GdkPixmap *last_pixmap = NULL;
164 pixmap = newmail_pixmap[prefs_common.work_offline];
165 bitmap = newmail_bitmap[prefs_common.work_offline];
167 case TRAYICON_NEWMARKED:
168 pixmap = newmarkedmail_pixmap[prefs_common.work_offline];
169 bitmap = newmarkedmail_bitmap[prefs_common.work_offline];
171 case TRAYICON_UNREAD:
172 pixmap = unreadmail_pixmap[prefs_common.work_offline];
173 bitmap = unreadmail_bitmap[prefs_common.work_offline];
175 case TRAYICON_UNREADMARKED:
176 pixmap = unreadmarkedmail_pixmap[prefs_common.work_offline];
177 bitmap = unreadmarkedmail_bitmap[prefs_common.work_offline];
180 pixmap = nomail_pixmap[prefs_common.work_offline];
181 bitmap = nomail_bitmap[prefs_common.work_offline];
185 if (pixmap == last_pixmap) {
189 gtk_image_set_from_pixmap(GTK_IMAGE(image), pixmap, bitmap);
191 last_pixmap = pixmap;
194 static void update(FolderItem *removed_item)
196 guint new, unread, unreadmarked, marked, total;
198 TrayIconType icontype = TRAYICON_NOTHING;
200 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total);
202 total -= removed_item->total_msgs;
203 new -= removed_item->new_msgs;
204 unread -= removed_item->unread_msgs;
207 buf = g_strdup_printf(_("New %d, Unread: %d, Total: %d"), new, unread, total);
209 gtk_tooltips_set_tip(tooltips, eventbox, buf, "");
212 if (new > 0 && unreadmarked > 0) {
213 icontype = TRAYICON_NEWMARKED;
214 } else if (new > 0) {
215 icontype = TRAYICON_NEW;
216 } else if (unreadmarked > 0) {
217 icontype = TRAYICON_UNREADMARKED;
218 } else if (unread > 0) {
219 icontype = TRAYICON_UNREAD;
222 set_trayicon_pixmap(icontype);
225 static gboolean folder_item_update_hook(gpointer source, gpointer data)
232 static gboolean folder_update_hook(gpointer source, gpointer data)
234 FolderUpdateData *hookdata;
236 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
237 update(hookdata->item);
244 static gboolean offline_update_hook(gpointer source, gpointer data)
250 static gboolean trayicon_close_hook(gpointer source, gpointer data)
253 gboolean *close_allowed = (gboolean*)source;
255 if (trayicon_prefs.close_to_tray) {
256 MainWindow *mainwin = mainwindow_get_mainwindow();
258 *close_allowed = FALSE;
259 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
260 main_window_hide(mainwin);
266 static void resize_cb(GtkWidget *widget, GtkRequisition *req,
272 static gboolean click_cb(GtkWidget * widget,
273 GdkEventButton * event, gpointer user_data)
281 mainwin = mainwindow_get_mainwindow();
283 switch (event->button) {
285 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))) {
286 if ((gdk_window_get_state(GTK_WIDGET(mainwin->window)->window)&GDK_WINDOW_STATE_ICONIFIED)
287 || mainwindow_is_obscured()) {
288 gtk_window_deiconify(GTK_WINDOW(mainwin->window));
289 main_window_show(mainwin);
290 gtk_window_present(GTK_WINDOW(mainwin->window));
292 main_window_hide(mainwin);
295 gtk_window_deiconify(GTK_WINDOW(mainwin->window));
296 main_window_show(mainwin);
297 gtk_window_present(GTK_WINDOW(mainwin->window));
301 /* tell callbacks to skip any event */
302 updating_menu = TRUE;
303 /* initialize checkitem according to current offline state */
304 gtk_check_menu_item_set_active(
305 GTK_CHECK_MENU_ITEM(gtk_item_factory_get_item(traymenu_factory,
306 _("/Work Offline"))), prefs_common.work_offline);
307 gtk_widget_set_sensitive(
308 GTK_WIDGET(gtk_item_factory_get_item(traymenu_factory,
309 _("/Get Mail"))), mainwin->lock_count == 0);
310 updating_menu = FALSE;
312 gtk_menu_popup( GTK_MENU(traymenu_popup), NULL, NULL, NULL, NULL,
313 event->button, event->time );
321 static void create_trayicon(void);
323 static void destroy_cb(GtkWidget *widget, gpointer *data)
325 debug_print("Widget destroyed\n");
330 static void create_trayicon()
334 trayicon = egg_tray_icon_new("Claws Mail");
335 gtk_widget_realize(GTK_WIDGET(trayicon));
336 gtk_window_set_default_size(GTK_WINDOW(trayicon), 16, 16);
337 gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
339 PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap[0], nomail_bitmap[0], nomail_xpm);
340 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap[0], unreadmail_bitmap[0], unreadmail_xpm);
341 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap[0], newmail_bitmap[0], newmail_xpm);
342 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmarkedmail_pixmap[0], unreadmarkedmail_bitmap[0], unreadmarkedmail_xpm);
343 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmarkedmail_pixmap[0], newmarkedmail_bitmap[0], newmarkedmail_xpm);
345 PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap[1], nomail_bitmap[1], nomail_offline_xpm);
346 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap[1], unreadmail_bitmap[1], unreadmail_offline_xpm);
347 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap[1], newmail_bitmap[1], newmail_offline_xpm);
348 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmarkedmail_pixmap[1], unreadmarkedmail_bitmap[1], unreadmarkedmail_offline_xpm);
349 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmarkedmail_pixmap[1], newmarkedmail_bitmap[1], newmarkedmail_offline_xpm);
352 eventbox = gtk_event_box_new();
353 gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
354 gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
356 image = gtk_image_new_from_pixmap(nomail_pixmap[0], nomail_bitmap[0]);
357 gtk_container_add(GTK_CONTAINER(eventbox), image);
360 g_signal_connect(G_OBJECT(trayicon), "destroy",
361 G_CALLBACK(destroy_cb), NULL);
362 g_signal_connect(GTK_OBJECT(trayicon), "size-request",
363 G_CALLBACK(resize_cb), NULL);
364 g_signal_connect(G_OBJECT(eventbox), "button-press-event",
365 G_CALLBACK(click_cb), NULL);
367 tooltips = gtk_tooltips_new();
368 gtk_tooltips_enable(tooltips);
370 n_entries = sizeof(trayicon_popup_menu_entries) /
371 sizeof(trayicon_popup_menu_entries[0]);
372 traymenu_popup = menu_create_items(trayicon_popup_menu_entries,
373 n_entries, "<TrayiconMenu>", &traymenu_factory,
376 gtk_widget_show_all(GTK_WIDGET(trayicon));
381 int plugin_init(gchar **error)
383 if (!check_plugin_version(MAKE_NUMERIC_VERSION(0, 9, 3, 86),
384 VERSION_NUMERIC, PLUGIN_NAME, error))
387 item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
388 if (item_hook_id == -1) {
389 *error = g_strdup(_("Failed to register folder item update hook"));
393 folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, folder_update_hook, NULL);
394 if (folder_hook_id == -1) {
395 *error = g_strdup(_("Failed to register folder update hook"));
399 offline_hook_id = hooks_register_hook (OFFLINE_SWITCH_HOOKLIST, offline_update_hook, NULL);
400 if (offline_hook_id == -1) {
401 *error = g_strdup(_("Failed to register offline switch hook"));
405 account_hook_id = hooks_register_hook (ACCOUNT_LIST_CHANGED_HOOKLIST, trayicon_set_accounts_hook, NULL);
406 if (offline_hook_id == -1) {
407 *error = g_strdup(_("Failed to register account list changed hook"));
411 close_hook_id = hooks_register_hook (MAIN_WINDOW_CLOSE, trayicon_close_hook, NULL);
412 if (close_hook_id == -1) {
413 *error = g_strdup(_("Failed to register close hook"));
418 trayicon_set_accounts_hook(NULL, NULL);
420 trayicon_prefs_init();
422 if (trayicon_prefs.hide_at_startup && claws_is_starting()) {
423 MainWindow *mainwin = mainwindow_get_mainwindow();
425 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
426 main_window_hide(mainwin);
432 void plugin_done(void)
434 trayicon_prefs_done();
436 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
437 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
438 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
439 hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
440 hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
442 if (claws_is_exiting())
445 g_signal_handler_disconnect(G_OBJECT(trayicon), destroy_signal_id);
447 gtk_widget_destroy(GTK_WIDGET(trayicon));
449 while (gtk_events_pending()) {
450 gtk_main_iteration();
454 const gchar *plugin_name(void)
459 const gchar *plugin_desc(void)
461 return _("This plugin places a mailbox icon in the system tray that "
462 "indicates if you have new or unread mail.\n"
464 "The mailbox is empty if you have no unread mail, otherwise "
465 "it contains a letter. A tooltip shows new, unread and total "
466 "number of messages.");
469 const gchar *plugin_type(void)
474 const gchar *plugin_licence(void)
479 const gchar *plugin_version(void)
485 /* popup menu callbacks */
486 static void trayicon_get_all_cb( gpointer data, guint action, GtkWidget *widget )
488 MainWindow *mainwin = mainwindow_get_mainwindow();
489 inc_all_account_mail_cb(mainwin, 0, NULL);
492 static void trayicon_compose_cb( gpointer data, guint action, GtkWidget *widget )
494 MainWindow *mainwin = mainwindow_get_mainwindow();
495 compose_mail_cb(mainwin, 0, NULL);
498 static void trayicon_compose_acc_cb( GtkMenuItem *menuitem, gpointer data )
500 compose_new((PrefsAccount *)data, NULL, NULL);
503 static void trayicon_addressbook_cb( gpointer data, guint action, GtkWidget *widget )
505 addressbook_open(NULL);
508 static void trayicon_toggle_offline_cb( gpointer data, guint action, GtkWidget *widget )
510 /* toggle offline mode if menu checkitem has been clicked */
511 if (!updating_menu) {
512 MainWindow *mainwin = mainwindow_get_mainwindow();
513 main_window_toggle_work_offline(mainwin, !prefs_common.work_offline, TRUE);
517 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
519 if (prefs_common.confirm_on_exit) {
520 if (alertpanel(_("Exit"), _("Exit Claws Mail?"),
521 GTK_STOCK_CANCEL, GTK_STOCK_OK,
522 NULL) != G_ALERTALTERNATE) {
525 manage_window_focus_in(mainwin->window, NULL, NULL);
528 app_will_exit(NULL, mainwin);
531 static void trayicon_exit_cb( gpointer data, guint action, GtkWidget *widget )
533 MainWindow *mainwin = mainwindow_get_mainwindow();
535 if (mainwin->lock_count == 0) {
536 app_exit_cb(mainwin, 0, NULL);
540 struct PluginFeature *plugin_provides(void)
542 static struct PluginFeature features[] =
543 { {PLUGIN_NOTIFIER, N_("Trayicon")},
544 {PLUGIN_NOTHING, NULL}};