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"
54 static GdkPixmap *newmail_pixmap;
55 static GdkPixmap *newmail_bitmap;
56 static GdkPixmap *unreadmail_pixmap;
57 static GdkPixmap *unreadmail_bitmap;
58 static GdkPixmap *newmarkedmail_pixmap;
59 static GdkPixmap *newmarkedmail_bitmap;
60 static GdkPixmap *unreadmarkedmail_pixmap;
61 static GdkPixmap *unreadmarkedmail_bitmap;
62 static GdkPixmap *nomail_pixmap;
63 static GdkPixmap *nomail_bitmap;
65 static EggTrayIcon *trayicon;
66 static GtkWidget *eventbox;
67 static GtkWidget *image;
68 static GtkTooltips *tooltips;
69 static GtkWidget *traymenu_popup;
70 static GtkItemFactory *traymenu_factory;
71 static gboolean updating_menu = FALSE;
73 guint destroy_signal_id;
80 TRAYICON_UNREADMARKED,
84 static void trayicon_get_all_cb (gpointer data, guint action, GtkWidget *widget);
85 static void trayicon_compose_cb (gpointer data, guint action, GtkWidget *widget);
86 static void trayicon_addressbook_cb (gpointer data, guint action, GtkWidget *widget);
87 static void trayicon_exit_cb (gpointer data, guint action, GtkWidget *widget);
88 static void trayicon_toggle_offline_cb (gpointer data, guint action, GtkWidget *widget);
89 static void resize_cb (GtkWidget *widget, GtkRequisition *req, gpointer user_data);
91 static GtkItemFactoryEntry trayicon_popup_menu_entries[] =
93 {N_("/_Get Mail"), NULL, trayicon_get_all_cb, 0, NULL},
94 {N_("/---"), NULL, NULL, 0, "<Separator>"},
95 {N_("/_Email"), NULL, trayicon_compose_cb, 0, NULL},
96 {N_("/Open A_ddressbook"), NULL, trayicon_addressbook_cb, 0, NULL},
97 {N_("/---"), NULL, NULL, 0, "<Separator>"},
98 {N_("/_Work Offline"), NULL, trayicon_toggle_offline_cb, 0, "<CheckItem>"},
99 {N_("/---"), NULL, NULL, 0, "<Separator>"},
100 {N_("/E_xit Sylpheed-Claws"), NULL, trayicon_exit_cb, 0, NULL}
103 static void set_trayicon_pixmap(TrayIconType icontype)
105 GdkPixmap *pixmap = NULL;
106 GdkBitmap *bitmap = NULL;
107 static GdkPixmap *last_pixmap = NULL;
111 pixmap = newmail_pixmap;
112 bitmap = newmail_bitmap;
114 case TRAYICON_NEWMARKED:
115 pixmap = newmarkedmail_pixmap;
116 bitmap = newmarkedmail_bitmap;
118 case TRAYICON_UNREAD:
119 pixmap = unreadmail_pixmap;
120 bitmap = unreadmail_bitmap;
122 case TRAYICON_UNREADMARKED:
123 pixmap = unreadmarkedmail_pixmap;
124 bitmap = unreadmarkedmail_bitmap;
127 pixmap = nomail_pixmap;
128 bitmap = nomail_bitmap;
132 if (pixmap == last_pixmap) {
136 gtk_image_set_from_pixmap(GTK_IMAGE(image), pixmap, bitmap);
138 last_pixmap = pixmap;
141 static void update(void)
143 guint new, unread, unreadmarked, marked, total;
145 TrayIconType icontype = TRAYICON_NOTHING;
147 folder_count_total_msgs(&new, &unread, &unreadmarked, &marked, &total);
148 buf = g_strdup_printf(_("New %d, Unread: %d, Total: %d"), new, unread, total);
150 gtk_tooltips_set_tip(tooltips, eventbox, buf, "");
153 if (new > 0 && unreadmarked > 0) {
154 icontype = TRAYICON_NEWMARKED;
155 } else if (new > 0) {
156 icontype = TRAYICON_NEW;
157 } else if (unreadmarked > 0) {
158 icontype = TRAYICON_UNREADMARKED;
159 } else if (unread > 0) {
160 icontype = TRAYICON_UNREAD;
163 set_trayicon_pixmap(icontype);
166 static gboolean folder_item_update_hook(gpointer source, gpointer data)
173 static void resize_cb(GtkWidget *widget, GtkRequisition *req,
179 static gboolean click_cb(GtkWidget * widget,
180 GdkEventButton * event, gpointer user_data)
188 mainwin = mainwindow_get_mainwindow();
190 switch (event->button) {
192 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))) {
193 main_window_hide(mainwin);
195 main_window_show(mainwin);
199 /* tell callbacks to skip any event */
200 updating_menu = TRUE;
201 /* initialize checkitem according to current offline state */
202 gtk_check_menu_item_set_active(
203 GTK_CHECK_MENU_ITEM(gtk_item_factory_get_item(traymenu_factory,
204 _("/Work Offline"))), prefs_common.work_offline);
205 gtk_widget_set_sensitive(
206 GTK_WIDGET(gtk_item_factory_get_item(traymenu_factory,
207 _("/Get Mail"))), mainwin->lock_count == 0);
208 updating_menu = FALSE;
210 gtk_menu_popup( GTK_MENU(traymenu_popup), NULL, NULL, NULL, NULL,
211 event->button, event->time );
219 static void create_trayicon(void);
221 static void destroy_cb(GtkWidget *widget, gpointer *data)
223 debug_print("Widget destroyed\n");
228 static void create_trayicon()
235 trayicon = egg_tray_icon_new("Sylpheed-Claws");
236 gtk_widget_realize(GTK_WIDGET(trayicon));
237 gtk_window_set_default_size(GTK_WINDOW(trayicon), 16, 16);
238 gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
240 PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap, nomail_bitmap, nomail_xpm);
241 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap, unreadmail_bitmap, unreadmail_xpm);
242 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap, newmail_bitmap, newmail_xpm);
243 PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmarkedmail_pixmap, unreadmarkedmail_bitmap, unreadmarkedmail_xpm);
244 PIXMAP_CREATE(GTK_WIDGET(trayicon), newmarkedmail_pixmap, newmarkedmail_bitmap, newmarkedmail_xpm);
246 eventbox = gtk_event_box_new();
247 gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
248 gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
250 image = gtk_image_new_from_pixmap(nomail_pixmap, nomail_bitmap);
251 gtk_container_add(GTK_CONTAINER(eventbox), image);
254 g_signal_connect(G_OBJECT(trayicon), "destroy",
255 G_CALLBACK(destroy_cb), NULL);
256 g_signal_connect(GTK_OBJECT(trayicon), "size-request",
257 G_CALLBACK(resize_cb), NULL);
258 g_signal_connect(G_OBJECT(eventbox), "button-press-event",
259 G_CALLBACK(click_cb), NULL);
261 tooltips = gtk_tooltips_new();
262 gtk_tooltips_set_delay(tooltips, 1000);
263 gtk_tooltips_enable(tooltips);
265 n_entries = sizeof(trayicon_popup_menu_entries) /
266 sizeof(trayicon_popup_menu_entries[0]);
267 traymenu_popup = menu_create_items(trayicon_popup_menu_entries,
268 n_entries, "<TrayiconMenu>", &traymenu_factory,
271 gtk_widget_show_all(GTK_WIDGET(trayicon));
276 int plugin_init(gchar **error)
278 if ((sylpheed_get_version() > VERSION_NUMERIC)) {
279 *error = g_strdup("Your version of Sylpheed-Claws is newer than the version the Trayicon plugin was built with");
283 if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
284 *error = g_strdup("Your version of Sylpheed-Claws is too old for the Trayicon plugin");
288 hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
290 *error = g_strdup("Failed to register folder item update hook");
299 void plugin_done(void)
301 g_signal_handler_disconnect(G_OBJECT(trayicon), destroy_signal_id);
303 gtk_widget_destroy(GTK_WIDGET(trayicon));
304 hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, hook_id);
306 while (gtk_events_pending()) {
307 gtk_main_iteration();
311 const gchar *plugin_name(void)
313 return _("Trayicon");
316 const gchar *plugin_desc(void)
318 return _("This plugin places a mailbox icon in the system tray that "
319 "indicates if you have new or unread mail.\n"
321 "The mailbox is empty if you have no unread mail, otherwise "
322 "it contains a letter. A tooltip shows new, unread and total "
323 "number of messages.");
326 const gchar *plugin_type(void)
331 const gchar *plugin_licence(void)
336 const gchar *plugin_version(void)
342 /* popup menu callbacks */
343 static void trayicon_get_all_cb( gpointer data, guint action, GtkWidget *widget )
345 MainWindow *mainwin = mainwindow_get_mainwindow();
346 inc_all_account_mail_cb(mainwin, 0, NULL);
349 static void trayicon_compose_cb( gpointer data, guint action, GtkWidget *widget )
351 MainWindow *mainwin = mainwindow_get_mainwindow();
352 compose_mail_cb(mainwin, 0, NULL);
355 static void trayicon_addressbook_cb( gpointer data, guint action, GtkWidget *widget )
357 addressbook_open(NULL);
360 static void trayicon_toggle_offline_cb( gpointer data, guint action, GtkWidget *widget )
362 /* toggle offline mode if menu checkitem has been clicked */
363 if (!updating_menu) {
364 MainWindow *mainwin = mainwindow_get_mainwindow();
365 main_window_toggle_work_offline(mainwin, !prefs_common.work_offline, FALSE);
369 static void app_exit_cb(MainWindow *mainwin, guint action, GtkWidget *widget)
371 if (prefs_common.confirm_on_exit) {
372 if (alertpanel(_("Exit"), _("Exit this program?"),
373 GTK_STOCK_CANCEL, GTK_STOCK_OK,
374 NULL) != G_ALERTALTERNATE) {
377 manage_window_focus_in(mainwin->window, NULL, NULL);
380 app_will_exit(NULL, mainwin);
383 static void trayicon_exit_cb( gpointer data, guint action, GtkWidget *widget )
385 MainWindow *mainwin = mainwindow_get_mainwindow();
387 if (mainwin->lock_count == 0) {
388 app_exit_cb(mainwin, 0, NULL);