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