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);
291 main_window_hide(mainwin);
294 gtk_window_deiconify(GTK_WINDOW(mainwin->window));
295 main_window_show(mainwin);
299 /* tell callbacks to skip any event */
300 updating_menu = TRUE;
301 /* initialize checkitem according to current offline state */
302 gtk_check_menu_item_set_active(
303 GTK_CHECK_MENU_ITEM(gtk_item_factory_get_item(traymenu_factory,
304 _("/Work Offline"))), prefs_common.work_offline);
305 gtk_widget_set_sensitive(
306 GTK_WIDGET(gtk_item_factory_get_item(traymenu_factory,
307 _("/Get Mail"))), mainwin->lock_count == 0);
308 updating_menu = FALSE;
310 gtk_menu_popup( GTK_MENU(traymenu_popup), NULL, NULL, NULL, NULL,
311 event->button, event->time );
319 static void create_trayicon(void);
321 static void destroy_cb(GtkWidget *widget, gpointer *data)
323 debug_print("Widget destroyed\n");
328 static void create_trayicon()
332 trayicon = egg_tray_icon_new("Claws Mail");
333 gtk_widget_realize(GTK_WIDGET(trayicon));
334 gtk_window_set_default_size(GTK_WINDOW(trayicon), 16, 16);
335 gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
337 PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap[0], nomail_bitmap[0], nomail_xpm);
338 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap[0], unreadmail_bitmap[0], unreadmail_xpm);
339 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap[0], newmail_bitmap[0], newmail_xpm);
340 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmarkedmail_pixmap[0], unreadmarkedmail_bitmap[0], unreadmarkedmail_xpm);
341 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmarkedmail_pixmap[0], newmarkedmail_bitmap[0], newmarkedmail_xpm);
343 PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap[1], nomail_bitmap[1], nomail_offline_xpm);
344 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap[1], unreadmail_bitmap[1], unreadmail_offline_xpm);
345 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap[1], newmail_bitmap[1], newmail_offline_xpm);
346 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmarkedmail_pixmap[1], unreadmarkedmail_bitmap[1], unreadmarkedmail_offline_xpm);
347 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmarkedmail_pixmap[1], newmarkedmail_bitmap[1], newmarkedmail_offline_xpm);
350 eventbox = gtk_event_box_new();
351 gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
352 gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
354 image = gtk_image_new_from_pixmap(nomail_pixmap[0], nomail_bitmap[0]);
355 gtk_container_add(GTK_CONTAINER(eventbox), image);
358 g_signal_connect(G_OBJECT(trayicon), "destroy",
359 G_CALLBACK(destroy_cb), NULL);
360 g_signal_connect(GTK_OBJECT(trayicon), "size-request",
361 G_CALLBACK(resize_cb), NULL);
362 g_signal_connect(G_OBJECT(eventbox), "button-press-event",
363 G_CALLBACK(click_cb), NULL);
365 tooltips = gtk_tooltips_new();
366 gtk_tooltips_enable(tooltips);
368 n_entries = sizeof(trayicon_popup_menu_entries) /
369 sizeof(trayicon_popup_menu_entries[0]);
370 traymenu_popup = menu_create_items(trayicon_popup_menu_entries,
371 n_entries, "<TrayiconMenu>", &traymenu_factory,
374 gtk_widget_show_all(GTK_WIDGET(trayicon));
379 int plugin_init(gchar **error)
381 if (!check_plugin_version(MAKE_NUMERIC_VERSION(0, 9, 3, 86),
382 VERSION_NUMERIC, PLUGIN_NAME, error))
385 item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
386 if (item_hook_id == -1) {
387 *error = g_strdup(_("Failed to register folder item update hook"));
391 folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, folder_update_hook, NULL);
392 if (folder_hook_id == -1) {
393 *error = g_strdup(_("Failed to register folder update hook"));
397 offline_hook_id = hooks_register_hook (OFFLINE_SWITCH_HOOKLIST, offline_update_hook, NULL);
398 if (offline_hook_id == -1) {
399 *error = g_strdup(_("Failed to register offline switch hook"));
403 account_hook_id = hooks_register_hook (ACCOUNT_LIST_CHANGED_HOOKLIST, trayicon_set_accounts_hook, NULL);
404 if (offline_hook_id == -1) {
405 *error = g_strdup(_("Failed to register account list changed hook"));
409 close_hook_id = hooks_register_hook (MAIN_WINDOW_CLOSE, trayicon_close_hook, NULL);
410 if (close_hook_id == -1) {
411 *error = g_strdup(_("Failed to register close hook"));
416 trayicon_set_accounts_hook(NULL, NULL);
418 trayicon_prefs_init();
420 if (trayicon_prefs.hide_at_startup && claws_is_starting()) {
421 MainWindow *mainwin = mainwindow_get_mainwindow();
423 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window)))
424 main_window_hide(mainwin);
430 void plugin_done(void)
432 trayicon_prefs_done();
434 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
435 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
436 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
437 hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
438 hooks_unregister_hook(MAIN_WINDOW_CLOSE, close_hook_id);
440 if (claws_is_exiting())
443 g_signal_handler_disconnect(G_OBJECT(trayicon), destroy_signal_id);
445 gtk_widget_destroy(GTK_WIDGET(trayicon));
447 while (gtk_events_pending()) {
448 gtk_main_iteration();
452 const gchar *plugin_name(void)
457 const gchar *plugin_desc(void)
459 return _("This plugin places a mailbox icon in the system tray that "
460 "indicates if you have new or unread mail.\n"
462 "The mailbox is empty if you have no unread mail, otherwise "
463 "it contains a letter. A tooltip shows new, unread and total "
464 "number of messages.");
467 const gchar *plugin_type(void)
472 const gchar *plugin_licence(void)
477 const gchar *plugin_version(void)
483 /* popup menu callbacks */
484 static void trayicon_get_all_cb( gpointer data, guint action, GtkWidget *widget )
486 MainWindow *mainwin = mainwindow_get_mainwindow();
487 inc_all_account_mail_cb(mainwin, 0, NULL);
490 static void trayicon_compose_cb( gpointer data, guint action, GtkWidget *widget )
492 MainWindow *mainwin = mainwindow_get_mainwindow();
493 compose_mail_cb(mainwin, 0, NULL);
496 static void trayicon_compose_acc_cb( GtkMenuItem *menuitem, gpointer data )
498 compose_new((PrefsAccount *)data, NULL, NULL);
501 static void trayicon_addressbook_cb( gpointer data, guint action, GtkWidget *widget )
503 addressbook_open(NULL);
506 static void trayicon_toggle_offline_cb( gpointer data, guint action, GtkWidget *widget )
508 /* toggle offline mode if menu checkitem has been clicked */
509 if (!updating_menu) {
510 MainWindow *mainwin = mainwindow_get_mainwindow();
511 main_window_toggle_work_offline(mainwin, !prefs_common.work_offline, TRUE);
515 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
517 if (prefs_common.confirm_on_exit) {
518 if (alertpanel(_("Exit"), _("Exit Claws Mail?"),
519 GTK_STOCK_CANCEL, GTK_STOCK_OK,
520 NULL) != G_ALERTALTERNATE) {
523 manage_window_focus_in(mainwin->window, NULL, NULL);
526 app_will_exit(NULL, mainwin);
529 static void trayicon_exit_cb( gpointer data, guint action, GtkWidget *widget )
531 MainWindow *mainwin = mainwindow_get_mainwindow();
533 if (mainwin->lock_count == 0) {
534 app_exit_cb(mainwin, 0, NULL);
538 struct PluginFeature *plugin_provides(void)
540 static struct PluginFeature features[] =
541 { {PLUGIN_NOTIFIER, N_("Trayicon")},
542 {PLUGIN_NOTHING, NULL}};