2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and 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/sylpheed.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 "newmarkedmail.xpm"
49 #include "unreadmarkedmail.xpm"
50 #include "newmail.xpm"
51 #include "unreadmail.xpm"
53 #include "newmarkedmail.offline.xpm"
54 #include "unreadmarkedmail.offline.xpm"
55 #include "newmail.offline.xpm"
56 #include "unreadmail.offline.xpm"
57 #include "nomail.offline.xpm"
59 static guint item_hook_id;
60 static guint folder_hook_id;
61 static guint offline_hook_id;
62 static guint account_hook_id;
64 static GdkPixmap *newmail_pixmap[2];
65 static GdkPixmap *newmail_bitmap[2];
66 static GdkPixmap *unreadmail_pixmap[2];
67 static GdkPixmap *unreadmail_bitmap[2];
68 static GdkPixmap *newmarkedmail_pixmap[2];
69 static GdkPixmap *newmarkedmail_bitmap[2];
70 static GdkPixmap *unreadmarkedmail_pixmap[2];
71 static GdkPixmap *unreadmarkedmail_bitmap[2];
72 static GdkPixmap *nomail_pixmap[2];
73 static GdkPixmap *nomail_bitmap[2];
75 static EggTrayIcon *trayicon;
76 static GtkWidget *eventbox;
77 static GtkWidget *image;
78 static GtkTooltips *tooltips;
79 static GtkWidget *traymenu_popup;
80 static GtkItemFactory *traymenu_factory;
81 static gboolean updating_menu = FALSE;
83 guint destroy_signal_id;
90 TRAYICON_UNREADMARKED,
94 static void trayicon_get_all_cb (gpointer data, guint action, GtkWidget *widget);
95 static void trayicon_compose_cb (gpointer data, guint action, GtkWidget *widget);
96 static void trayicon_compose_acc_cb (GtkMenuItem *menuitem, gpointer data );
97 static void trayicon_addressbook_cb (gpointer data, guint action, GtkWidget *widget);
98 static void trayicon_exit_cb (gpointer data, guint action, GtkWidget *widget);
99 static void trayicon_toggle_offline_cb (gpointer data, guint action, GtkWidget *widget);
100 static void resize_cb (GtkWidget *widget, GtkRequisition *req, gpointer user_data);
102 static GtkItemFactoryEntry trayicon_popup_menu_entries[] =
104 {N_("/_Get Mail"), NULL, trayicon_get_all_cb, 0, NULL},
105 {N_("/---"), NULL, NULL, 0, "<Separator>"},
106 {N_("/_Email"), NULL, trayicon_compose_cb, 0, NULL},
107 {N_("/_Email from account"), NULL, NULL, 0, "<Branch>"},
108 {N_("/---"), NULL, NULL, 0, "<Separator>"},
109 {N_("/Open A_ddressbook"), NULL, trayicon_addressbook_cb, 0, NULL},
110 {N_("/---"), NULL, NULL, 0, "<Separator>"},
111 {N_("/_Work Offline"), NULL, trayicon_toggle_offline_cb, 0, "<CheckItem>"},
112 {N_("/---"), NULL, NULL, 0, "<Separator>"},
113 {N_("/E_xit Claws Mail"), NULL, trayicon_exit_cb, 0, NULL}
116 static gboolean trayicon_set_accounts_hook(gpointer source, gpointer data)
118 GList *cur_ac, *cur_item = NULL;
121 PrefsAccount *ac_prefs;
123 GList *account_list = account_get_list();
125 menu = gtk_item_factory_get_widget(traymenu_factory,
126 "/Email from account");
128 /* destroy all previous menu item */
129 cur_item = GTK_MENU_SHELL(menu)->children;
130 while (cur_item != NULL) {
131 GList *next = cur_item->next;
132 gtk_widget_destroy(GTK_WIDGET(cur_item->data));
136 for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
137 ac_prefs = (PrefsAccount *)cur_ac->data;
139 menuitem = gtk_menu_item_new_with_label
140 (ac_prefs->account_name ? ac_prefs->account_name
142 gtk_widget_show(menuitem);
143 gtk_menu_append(GTK_MENU(menu), menuitem);
144 g_signal_connect(G_OBJECT(menuitem), "activate",
145 G_CALLBACK(trayicon_compose_acc_cb),
151 static void set_trayicon_pixmap(TrayIconType icontype)
153 GdkPixmap *pixmap = NULL;
154 GdkBitmap *bitmap = NULL;
155 static GdkPixmap *last_pixmap = NULL;
159 pixmap = newmail_pixmap[prefs_common.work_offline];
160 bitmap = newmail_bitmap[prefs_common.work_offline];
162 case TRAYICON_NEWMARKED:
163 pixmap = newmarkedmail_pixmap[prefs_common.work_offline];
164 bitmap = newmarkedmail_bitmap[prefs_common.work_offline];
166 case TRAYICON_UNREAD:
167 pixmap = unreadmail_pixmap[prefs_common.work_offline];
168 bitmap = unreadmail_bitmap[prefs_common.work_offline];
170 case TRAYICON_UNREADMARKED:
171 pixmap = unreadmarkedmail_pixmap[prefs_common.work_offline];
172 bitmap = unreadmarkedmail_bitmap[prefs_common.work_offline];
175 pixmap = nomail_pixmap[prefs_common.work_offline];
176 bitmap = nomail_bitmap[prefs_common.work_offline];
180 if (pixmap == last_pixmap) {
184 gtk_image_set_from_pixmap(GTK_IMAGE(image), pixmap, bitmap);
186 last_pixmap = pixmap;
189 static void update(FolderItem *removed_item)
191 guint new, unread, unreadmarked, marked, total;
193 TrayIconType icontype = TRAYICON_NOTHING;
195 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total);
197 total -= removed_item->total_msgs;
198 new -= removed_item->new_msgs;
199 unread -= removed_item->unread_msgs;
202 buf = g_strdup_printf(_("New %d, Unread: %d, Total: %d"), new, unread, total);
204 gtk_tooltips_set_tip(tooltips, eventbox, buf, "");
207 if (new > 0 && unreadmarked > 0) {
208 icontype = TRAYICON_NEWMARKED;
209 } else if (new > 0) {
210 icontype = TRAYICON_NEW;
211 } else if (unreadmarked > 0) {
212 icontype = TRAYICON_UNREADMARKED;
213 } else if (unread > 0) {
214 icontype = TRAYICON_UNREAD;
217 set_trayicon_pixmap(icontype);
220 static gboolean folder_item_update_hook(gpointer source, gpointer data)
227 static gboolean folder_update_hook(gpointer source, gpointer data)
229 FolderUpdateData *hookdata;
231 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
232 update(hookdata->item);
239 static gboolean offline_update_hook(gpointer source, gpointer data)
245 static void resize_cb(GtkWidget *widget, GtkRequisition *req,
251 static gboolean click_cb(GtkWidget * widget,
252 GdkEventButton * event, gpointer user_data)
260 mainwin = mainwindow_get_mainwindow();
262 switch (event->button) {
264 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))) {
265 main_window_hide(mainwin);
267 main_window_show(mainwin);
271 /* tell callbacks to skip any event */
272 updating_menu = TRUE;
273 /* initialize checkitem according to current offline state */
274 gtk_check_menu_item_set_active(
275 GTK_CHECK_MENU_ITEM(gtk_item_factory_get_item(traymenu_factory,
276 _("/Work Offline"))), prefs_common.work_offline);
277 gtk_widget_set_sensitive(
278 GTK_WIDGET(gtk_item_factory_get_item(traymenu_factory,
279 _("/Get Mail"))), mainwin->lock_count == 0);
280 updating_menu = FALSE;
282 gtk_menu_popup( GTK_MENU(traymenu_popup), NULL, NULL, NULL, NULL,
283 event->button, event->time );
291 static void create_trayicon(void);
293 static void destroy_cb(GtkWidget *widget, gpointer *data)
295 debug_print("Widget destroyed\n");
300 static void create_trayicon()
304 trayicon = egg_tray_icon_new("Claws Mail");
305 gtk_widget_realize(GTK_WIDGET(trayicon));
306 gtk_window_set_default_size(GTK_WINDOW(trayicon), 16, 16);
307 gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
309 PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap[0], nomail_bitmap[0], nomail_xpm);
310 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap[0], unreadmail_bitmap[0], unreadmail_xpm);
311 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap[0], newmail_bitmap[0], newmail_xpm);
312 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmarkedmail_pixmap[0], unreadmarkedmail_bitmap[0], unreadmarkedmail_xpm);
313 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmarkedmail_pixmap[0], newmarkedmail_bitmap[0], newmarkedmail_xpm);
315 PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap[1], nomail_bitmap[1], nomail_offline_xpm);
316 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap[1], unreadmail_bitmap[1], unreadmail_offline_xpm);
317 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap[1], newmail_bitmap[1], newmail_offline_xpm);
318 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmarkedmail_pixmap[1], unreadmarkedmail_bitmap[1], unreadmarkedmail_offline_xpm);
319 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmarkedmail_pixmap[1], newmarkedmail_bitmap[1], newmarkedmail_offline_xpm);
322 eventbox = gtk_event_box_new();
323 gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
324 gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
326 image = gtk_image_new_from_pixmap(nomail_pixmap[0], nomail_bitmap[0]);
327 gtk_container_add(GTK_CONTAINER(eventbox), image);
330 g_signal_connect(G_OBJECT(trayicon), "destroy",
331 G_CALLBACK(destroy_cb), NULL);
332 g_signal_connect(GTK_OBJECT(trayicon), "size-request",
333 G_CALLBACK(resize_cb), NULL);
334 g_signal_connect(G_OBJECT(eventbox), "button-press-event",
335 G_CALLBACK(click_cb), NULL);
337 tooltips = gtk_tooltips_new();
338 gtk_tooltips_set_delay(tooltips, 1000);
339 gtk_tooltips_enable(tooltips);
341 n_entries = sizeof(trayicon_popup_menu_entries) /
342 sizeof(trayicon_popup_menu_entries[0]);
343 traymenu_popup = menu_create_items(trayicon_popup_menu_entries,
344 n_entries, "<TrayiconMenu>", &traymenu_factory,
347 gtk_widget_show_all(GTK_WIDGET(trayicon));
352 int plugin_init(gchar **error)
354 if ((sylpheed_get_version() > VERSION_NUMERIC)) {
355 *error = g_strdup(_("Your version of Claws Mail is newer than the version the Trayicon plugin was built with"));
359 if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
360 *error = g_strdup(_("Your version of Claws Mail is too old for the Trayicon plugin"));
364 item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
365 if (item_hook_id == -1) {
366 *error = g_strdup(_("Failed to register folder item update hook"));
370 folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, folder_update_hook, NULL);
371 if (folder_hook_id == -1) {
372 *error = g_strdup(_("Failed to register folder update hook"));
376 offline_hook_id = hooks_register_hook (OFFLINE_SWITCH_HOOKLIST, offline_update_hook, NULL);
377 if (offline_hook_id == -1) {
378 *error = g_strdup(_("Failed to register offline switch hook"));
382 account_hook_id = hooks_register_hook (ACCOUNT_LIST_CHANGED_HOOKLIST, trayicon_set_accounts_hook, NULL);
383 if (offline_hook_id == -1) {
384 *error = g_strdup(_("Failed to register offline switch hook"));
389 trayicon_set_accounts_hook(NULL, NULL);
394 void plugin_done(void)
396 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
397 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
398 hooks_unregister_hook(OFFLINE_SWITCH_HOOKLIST, offline_hook_id);
399 hooks_unregister_hook(ACCOUNT_LIST_CHANGED_HOOKLIST, account_hook_id);
401 if (sylpheed_is_exiting())
404 g_signal_handler_disconnect(G_OBJECT(trayicon), destroy_signal_id);
406 gtk_widget_destroy(GTK_WIDGET(trayicon));
408 while (gtk_events_pending()) {
409 gtk_main_iteration();
413 const gchar *plugin_name(void)
415 return _("Trayicon");
418 const gchar *plugin_desc(void)
420 return _("This plugin places a mailbox icon in the system tray that "
421 "indicates if you have new or unread mail.\n"
423 "The mailbox is empty if you have no unread mail, otherwise "
424 "it contains a letter. A tooltip shows new, unread and total "
425 "number of messages.");
428 const gchar *plugin_type(void)
433 const gchar *plugin_licence(void)
438 const gchar *plugin_version(void)
444 /* popup menu callbacks */
445 static void trayicon_get_all_cb( gpointer data, guint action, GtkWidget *widget )
447 MainWindow *mainwin = mainwindow_get_mainwindow();
448 inc_all_account_mail_cb(mainwin, 0, NULL);
451 static void trayicon_compose_cb( gpointer data, guint action, GtkWidget *widget )
453 MainWindow *mainwin = mainwindow_get_mainwindow();
454 compose_mail_cb(mainwin, 0, NULL);
457 static void trayicon_compose_acc_cb( GtkMenuItem *menuitem, gpointer data )
459 compose_new((PrefsAccount *)data, NULL, NULL);
462 static void trayicon_addressbook_cb( gpointer data, guint action, GtkWidget *widget )
464 addressbook_open(NULL);
467 static void trayicon_toggle_offline_cb( gpointer data, guint action, GtkWidget *widget )
469 /* toggle offline mode if menu checkitem has been clicked */
470 if (!updating_menu) {
471 MainWindow *mainwin = mainwindow_get_mainwindow();
472 main_window_toggle_work_offline(mainwin, !prefs_common.work_offline, TRUE);
476 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
478 if (prefs_common.confirm_on_exit) {
479 if (alertpanel(_("Exit"), _("Exit this program?"),
480 GTK_STOCK_CANCEL, GTK_STOCK_OK,
481 NULL) != G_ALERTALTERNATE) {
484 manage_window_focus_in(mainwin->window, NULL, NULL);
487 app_will_exit(NULL, mainwin);
490 static void trayicon_exit_cb( gpointer data, guint action, GtkWidget *widget )
492 MainWindow *mainwin = mainwindow_get_mainwindow();
494 if (mainwin->lock_count == 0) {
495 app_exit_cb(mainwin, 0, NULL);
499 struct PluginFeature *plugin_provides(void)
501 static struct PluginFeature features[] =
502 { {PLUGIN_NOTIFIER, N_("Trayicon")},
503 {PLUGIN_NOTHING, NULL}};