2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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"
43 #include "gtk/manage_window.h"
45 #include "eggtrayicon.h"
46 #include "newmarkedmail.xpm"
47 #include "unreadmarkedmail.xpm"
48 #include "newmail.xpm"
49 #include "unreadmail.xpm"
52 static guint item_hook_id;
53 static guint folder_hook_id;
55 static GdkPixmap *newmail_pixmap;
56 static GdkPixmap *newmail_bitmap;
57 static GdkPixmap *unreadmail_pixmap;
58 static GdkPixmap *unreadmail_bitmap;
59 static GdkPixmap *newmarkedmail_pixmap;
60 static GdkPixmap *newmarkedmail_bitmap;
61 static GdkPixmap *unreadmarkedmail_pixmap;
62 static GdkPixmap *unreadmarkedmail_bitmap;
63 static GdkPixmap *nomail_pixmap;
64 static GdkPixmap *nomail_bitmap;
66 static EggTrayIcon *trayicon;
67 static GtkWidget *eventbox;
68 static GtkWidget *image;
69 static GtkTooltips *tooltips;
70 static GtkWidget *traymenu_popup;
71 static GtkItemFactory *traymenu_factory;
72 static gboolean updating_menu = FALSE;
74 guint destroy_signal_id;
81 TRAYICON_UNREADMARKED,
85 static void trayicon_get_all_cb (gpointer data, guint action, GtkWidget *widget);
86 static void trayicon_compose_cb (gpointer data, guint action, GtkWidget *widget);
87 static void trayicon_addressbook_cb (gpointer data, guint action, GtkWidget *widget);
88 static void trayicon_exit_cb (gpointer data, guint action, GtkWidget *widget);
89 static void trayicon_toggle_offline_cb (gpointer data, guint action, GtkWidget *widget);
90 static void resize_cb (GtkWidget *widget, GtkRequisition *req, gpointer user_data);
92 static GtkItemFactoryEntry trayicon_popup_menu_entries[] =
94 {N_("/_Get Mail"), NULL, trayicon_get_all_cb, 0, NULL},
95 {N_("/---"), NULL, NULL, 0, "<Separator>"},
96 {N_("/_Email"), NULL, trayicon_compose_cb, 0, NULL},
97 {N_("/Open A_ddressbook"), NULL, trayicon_addressbook_cb, 0, NULL},
98 {N_("/---"), NULL, NULL, 0, "<Separator>"},
99 {N_("/_Work Offline"), NULL, trayicon_toggle_offline_cb, 0, "<CheckItem>"},
100 {N_("/---"), NULL, NULL, 0, "<Separator>"},
101 {N_("/E_xit Sylpheed-Claws"), NULL, trayicon_exit_cb, 0, NULL}
104 static void set_trayicon_pixmap(TrayIconType icontype)
106 GdkPixmap *pixmap = NULL;
107 GdkBitmap *bitmap = NULL;
108 static GdkPixmap *last_pixmap = NULL;
112 pixmap = newmail_pixmap;
113 bitmap = newmail_bitmap;
115 case TRAYICON_NEWMARKED:
116 pixmap = newmarkedmail_pixmap;
117 bitmap = newmarkedmail_bitmap;
119 case TRAYICON_UNREAD:
120 pixmap = unreadmail_pixmap;
121 bitmap = unreadmail_bitmap;
123 case TRAYICON_UNREADMARKED:
124 pixmap = unreadmarkedmail_pixmap;
125 bitmap = unreadmarkedmail_bitmap;
128 pixmap = nomail_pixmap;
129 bitmap = nomail_bitmap;
133 if (pixmap == last_pixmap) {
137 gtk_image_set_from_pixmap(GTK_IMAGE(image), pixmap, bitmap);
139 last_pixmap = pixmap;
142 static void update(FolderItem *removed_item)
144 guint new, unread, unreadmarked, marked, total;
146 TrayIconType icontype = TRAYICON_NOTHING;
148 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total);
150 total -= removed_item->total_msgs;
151 new -= removed_item->new_msgs;
152 unread -= removed_item->unread_msgs;
155 buf = g_strdup_printf(_("New %d, Unread: %d, Total: %d"), new, unread, total);
157 gtk_tooltips_set_tip(tooltips, eventbox, buf, "");
160 if (new > 0 && unreadmarked > 0) {
161 icontype = TRAYICON_NEWMARKED;
162 } else if (new > 0) {
163 icontype = TRAYICON_NEW;
164 } else if (unreadmarked > 0) {
165 icontype = TRAYICON_UNREADMARKED;
166 } else if (unread > 0) {
167 icontype = TRAYICON_UNREAD;
170 set_trayicon_pixmap(icontype);
173 static gboolean folder_item_update_hook(gpointer source, gpointer data)
180 static gboolean folder_update_hook(gpointer source, gpointer data)
182 FolderUpdateData *hookdata;
184 if (hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
185 update(hookdata->item);
192 static void resize_cb(GtkWidget *widget, GtkRequisition *req,
198 static gboolean click_cb(GtkWidget * widget,
199 GdkEventButton * event, gpointer user_data)
207 mainwin = mainwindow_get_mainwindow();
209 switch (event->button) {
211 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))) {
212 main_window_hide(mainwin);
214 main_window_show(mainwin);
218 /* tell callbacks to skip any event */
219 updating_menu = TRUE;
220 /* initialize checkitem according to current offline state */
221 gtk_check_menu_item_set_active(
222 GTK_CHECK_MENU_ITEM(gtk_item_factory_get_item(traymenu_factory,
223 _("/Work Offline"))), prefs_common.work_offline);
224 gtk_widget_set_sensitive(
225 GTK_WIDGET(gtk_item_factory_get_item(traymenu_factory,
226 _("/Get Mail"))), mainwin->lock_count == 0);
227 updating_menu = FALSE;
229 gtk_menu_popup( GTK_MENU(traymenu_popup), NULL, NULL, NULL, NULL,
230 event->button, event->time );
238 static void create_trayicon(void);
240 static void destroy_cb(GtkWidget *widget, gpointer *data)
242 debug_print("Widget destroyed\n");
247 static void create_trayicon()
251 trayicon = egg_tray_icon_new("Sylpheed-Claws");
252 gtk_widget_realize(GTK_WIDGET(trayicon));
253 gtk_window_set_default_size(GTK_WINDOW(trayicon), 16, 16);
254 gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
256 PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap, nomail_bitmap, nomail_xpm);
257 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap, unreadmail_bitmap, unreadmail_xpm);
258 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap, newmail_bitmap, newmail_xpm);
259 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmarkedmail_pixmap, unreadmarkedmail_bitmap, unreadmarkedmail_xpm);
260 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmarkedmail_pixmap, newmarkedmail_bitmap, newmarkedmail_xpm);
262 eventbox = gtk_event_box_new();
263 gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
264 gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
266 image = gtk_image_new_from_pixmap(nomail_pixmap, nomail_bitmap);
267 gtk_container_add(GTK_CONTAINER(eventbox), image);
270 g_signal_connect(G_OBJECT(trayicon), "destroy",
271 G_CALLBACK(destroy_cb), NULL);
272 g_signal_connect(GTK_OBJECT(trayicon), "size-request",
273 G_CALLBACK(resize_cb), NULL);
274 g_signal_connect(G_OBJECT(eventbox), "button-press-event",
275 G_CALLBACK(click_cb), NULL);
277 tooltips = gtk_tooltips_new();
278 gtk_tooltips_set_delay(tooltips, 1000);
279 gtk_tooltips_enable(tooltips);
281 n_entries = sizeof(trayicon_popup_menu_entries) /
282 sizeof(trayicon_popup_menu_entries[0]);
283 traymenu_popup = menu_create_items(trayicon_popup_menu_entries,
284 n_entries, "<TrayiconMenu>", &traymenu_factory,
287 gtk_widget_show_all(GTK_WIDGET(trayicon));
292 int plugin_init(gchar **error)
294 if ((sylpheed_get_version() > VERSION_NUMERIC)) {
295 *error = g_strdup(_("Your version of Sylpheed-Claws is newer than the version the Trayicon plugin was built with"));
299 if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
300 *error = g_strdup(_("Your version of Sylpheed-Claws is too old for the Trayicon plugin"));
304 item_hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
305 if (item_hook_id == -1) {
306 *error = g_strdup(_("Failed to register folder item update hook"));
310 folder_hook_id = hooks_register_hook (FOLDER_UPDATE_HOOKLIST, folder_update_hook, NULL);
311 if (folder_hook_id == -1) {
312 *error = g_strdup(_("Failed to register folder update hook"));
321 void plugin_done(void)
323 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, item_hook_id);
324 hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST, folder_hook_id);
326 if (sylpheed_is_exiting())
329 g_signal_handler_disconnect(G_OBJECT(trayicon), destroy_signal_id);
331 gtk_widget_destroy(GTK_WIDGET(trayicon));
333 while (gtk_events_pending()) {
334 gtk_main_iteration();
338 const gchar *plugin_name(void)
340 return _("Trayicon");
343 const gchar *plugin_desc(void)
345 return _("This plugin places a mailbox icon in the system tray that "
346 "indicates if you have new or unread mail.\n"
348 "The mailbox is empty if you have no unread mail, otherwise "
349 "it contains a letter. A tooltip shows new, unread and total "
350 "number of messages.");
353 const gchar *plugin_type(void)
358 const gchar *plugin_licence(void)
363 const gchar *plugin_version(void)
369 /* popup menu callbacks */
370 static void trayicon_get_all_cb( gpointer data, guint action, GtkWidget *widget )
372 MainWindow *mainwin = mainwindow_get_mainwindow();
373 inc_all_account_mail_cb(mainwin, 0, NULL);
376 static void trayicon_compose_cb( gpointer data, guint action, GtkWidget *widget )
378 MainWindow *mainwin = mainwindow_get_mainwindow();
379 compose_mail_cb(mainwin, 0, NULL);
382 static void trayicon_addressbook_cb( gpointer data, guint action, GtkWidget *widget )
384 addressbook_open(NULL);
387 static void trayicon_toggle_offline_cb( gpointer data, guint action, GtkWidget *widget )
389 /* toggle offline mode if menu checkitem has been clicked */
390 if (!updating_menu) {
391 MainWindow *mainwin = mainwindow_get_mainwindow();
392 main_window_toggle_work_offline(mainwin, !prefs_common.work_offline, FALSE);
396 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
398 if (prefs_common.confirm_on_exit) {
399 if (alertpanel(_("Exit"), _("Exit this program?"),
400 GTK_STOCK_CANCEL, GTK_STOCK_OK,
401 NULL) != G_ALERTALTERNATE) {
404 manage_window_focus_in(mainwin->window, NULL, NULL);
407 app_will_exit(NULL, mainwin);
410 static void trayicon_exit_cb( gpointer data, guint action, GtkWidget *widget )
412 MainWindow *mainwin = mainwindow_get_mainwindow();
414 if (mainwin->lock_count == 0) {
415 app_exit_cb(mainwin, 0, NULL);
419 struct PluginFeature *plugin_provides(void)
421 static struct PluginFeature features[] =
422 { {PLUGIN_NOTIFIER, N_("Trayicon")},
423 {PLUGIN_NOTHING, NULL}};