a64669d5db8a8543e519528be46fcfdb7a206691
[claws.git] / src / plugins / trayicon / trayicon.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto and the Sylpheed-Claws Team
4  *
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.
9  *
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.
14  *
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <stdio.h>
25
26 #include <glib.h>
27 #include <gtk/gtk.h>
28
29 #include "common/sylpheed.h"
30 #include "common/version.h"
31 #include "plugin.h"
32 #include "utils.h"
33 #include "hooks.h"
34 #include "folder.h"
35 #include "mainwindow.h"
36 #include "gtkutils.h"
37 #include "intl.h"
38
39 #include "eggtrayicon.h"
40 #include "newmail.xpm"
41 #include "unreadmail.xpm"
42 #include "nomail.xpm"
43
44 static guint hook_id;
45
46 static GdkPixmap *newmail_pixmap;
47 static GdkPixmap *newmail_bitmap;
48 static GdkPixmap *unreadmail_pixmap;
49 static GdkPixmap *unreadmail_bitmap;
50 static GdkPixmap *nomail_pixmap;
51 static GdkPixmap *nomail_bitmap;
52
53 static EggTrayIcon *trayicon;
54 static GtkWidget *eventbox;
55 static GtkWidget *image;
56 static GtkTooltips *tooltips;
57 guint destroy_signal_id;
58
59 typedef enum
60 {
61         TRAYICON_NEW,
62         TRAYICON_UNREAD,
63         TRAYICON_UNREADMARKED,
64         TRAYICON_NOTHING,
65 } TrayIconType;
66
67 /* static gboolean mainwin_hidden = FALSE; */
68
69 static void set_trayicon_pixmap(TrayIconType icontype)
70 {
71         GdkPixmap *pixmap = NULL;
72         GdkBitmap *bitmap = NULL;
73
74         switch(icontype) {
75         case TRAYICON_NEW:
76                 pixmap = newmail_pixmap;
77                 bitmap = newmail_bitmap;
78                 break;
79         case TRAYICON_UNREAD:
80         case TRAYICON_UNREADMARKED:
81                 pixmap = unreadmail_pixmap;
82                 bitmap = unreadmail_bitmap;
83                 break;
84         default:
85                 pixmap = nomail_pixmap;
86                 bitmap = nomail_bitmap;
87                 break;
88         }
89
90         gtk_pixmap_set(GTK_PIXMAP(image), pixmap, bitmap);
91         gtk_widget_shape_combine_mask(GTK_WIDGET(trayicon), bitmap, GTK_WIDGET(image)->allocation.x, GTK_WIDGET(image)->allocation.y);
92 }
93
94 static void update(void)
95 {
96         gint new, unread, unreadmarked, total;
97         gchar *buf;
98
99         folder_count_total_msgs(&new, &unread, &unreadmarked, &total);
100         buf = g_strdup_printf("New %d, Unread: %d, Total: %d", new, unread, total);
101
102         gtk_tooltips_set_tip(tooltips, eventbox, buf, "");
103         g_free(buf);
104
105         set_trayicon_pixmap(new > 0 ? TRAYICON_NEW : (unread > 0 ? TRAYICON_UNREAD : TRAYICON_NOTHING));
106 }
107
108 static gboolean folder_item_update_hook(gpointer source, gpointer data)
109 {
110         update();
111
112         return FALSE;
113 }
114
115 static gboolean click_cb(GtkWidget * widget,
116                          GdkEventButton * event, gpointer user_data)
117 {
118         MainWindow *mainwin;
119
120         mainwin = mainwindow_get_mainwindow();
121         if (GTK_WIDGET_VISIBLE(GTK_WIDGET(mainwin->window))) {
122                 main_window_hide(mainwin);
123         } else {
124                 main_window_show(mainwin);
125         }
126         return TRUE;
127 }
128
129 static void resize_cb(GtkWidget *widget, GtkAllocation *allocation)
130 {
131         update();
132 }
133
134 static void create_trayicon(void);
135
136 static void destroy_cb(GtkWidget *widget, gpointer *data)
137 {
138         debug_print("Widget destroyed\n");
139
140         create_trayicon();
141 }
142
143 static void create_trayicon()
144 {
145 #if 0
146         GtkPacker *packer;
147 #endif
148
149         trayicon = egg_tray_icon_new("Sylpheed-Claws");
150 //        trayicon = gtk_window_new(GTK_WINDOW_TOPLEVEL);
151         gtk_widget_realize(GTK_WIDGET(trayicon));
152         gtk_window_set_default_size(GTK_WINDOW(trayicon), 16, 16);
153         gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
154
155         PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap, nomail_bitmap, nomail_xpm);
156         PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap, unreadmail_bitmap, unreadmail_xpm);
157         PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap, newmail_bitmap, newmail_xpm);
158
159         eventbox = gtk_event_box_new();
160         gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
161         gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
162
163 #warning FIXME_GTK2
164 #if 0
165         packer = GTK_PACKER(gtk_packer_new());
166         gtk_container_add(GTK_CONTAINER(eventbox), GTK_WIDGET(packer));
167         gtk_container_set_border_width(GTK_CONTAINER(packer), 0);
168
169         image = gtk_pixmap_new(nomail_pixmap, nomail_bitmap);
170         gtk_packer_add_defaults(GTK_PACKER(packer), GTK_WIDGET(image), GTK_SIDE_TOP, GTK_ANCHOR_CENTER, GTK_PACK_EXPAND);
171 #else
172         image = gtk_pixmap_new(nomail_pixmap, nomail_bitmap);
173         gtk_container_add(GTK_CONTAINER(eventbox), image);
174 #endif
175
176         destroy_signal_id =
177         g_signal_connect(G_OBJECT(trayicon), "destroy",
178                          G_CALLBACK(destroy_cb), NULL);
179         g_signal_connect(G_OBJECT(trayicon), "size_allocate",
180                          G_CALLBACK(resize_cb), NULL);
181         g_signal_connect(G_OBJECT(eventbox), "button-press-event",
182                          G_CALLBACK(click_cb), NULL);
183
184         tooltips = gtk_tooltips_new();
185         gtk_tooltips_set_delay(tooltips, 1000);
186         gtk_tooltips_enable(tooltips);
187
188         gtk_widget_show_all(GTK_WIDGET(trayicon));
189
190         update();
191 }
192
193 int plugin_init(gchar **error)
194 {
195         if ((sylpheed_get_version() > VERSION_NUMERIC)) {
196                 *error = g_strdup("Your sylpheed version is newer than the version the plugin was built with");
197                 return -1;
198         }
199
200         if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
201                 *error = g_strdup("Your sylpheed version is too old");
202                 return -1;
203         }
204
205         hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
206         if (hook_id == -1) {
207                 *error = g_strdup("Failed to register folder item update hook");
208                 return -1;
209         }
210
211         create_trayicon();
212
213         return 0;
214 }
215
216 void plugin_done(void)
217 {
218         g_signal_handler_disconnect(G_OBJECT(trayicon), destroy_signal_id);
219
220         gtk_widget_destroy(GTK_WIDGET(trayicon));
221         hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, hook_id);
222
223         while (gtk_events_pending())
224                 gtk_main_iteration();           
225 }
226
227 const gchar *plugin_name(void)
228 {
229         return _("Trayicon");
230 }
231
232 const gchar *plugin_desc(void)
233 {
234         return _("This plugin places a mailbox icon in the system tray that "
235                  "indicates if you have new or unread mail.\n"
236                  "\n"
237                  "The mailbox is empty if you have no unread mail, otherwise "
238                  "it contains a letter. A tooltip shows new, unread and total "
239                  "number of messages.");
240 }
241
242 const gchar *plugin_type(void)
243 {
244         return "GTK";
245 }