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