0.8.11claws41
[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 "hooks.h"
27 #include "folder.h"
28 #include "mainwindow.h"
29 #include "gtkutils.h"
30
31 #include "eggtrayicon.h"
32 #include "newmail.xpm"
33 #include "unreadmail.xpm"
34 #include "nomail.xpm"
35
36 static gint hook_id;
37
38 static GdkPixmap *newmail_pixmap;
39 static GdkPixmap *newmail_bitmap;
40 static GdkPixmap *unreadmail_pixmap;
41 static GdkPixmap *unreadmail_bitmap;
42 static GdkPixmap *nomail_pixmap;
43 static GdkPixmap *nomail_bitmap;
44
45 static EggTrayIcon *trayicon;
46 static GtkWidget *eventbox;
47 static GtkWidget *image;
48 static GtkTooltips *tooltips;
49
50 typedef enum
51 {
52         TRAYICON_NEW,
53         TRAYICON_UNREAD,
54         TRAYICON_UNREADMARKED,
55         TRAYICON_NOTHING,
56 } TrayIconType;
57
58 static gboolean mainwin_hidden = FALSE;
59
60 static gboolean click_cb(GtkWidget * widget,
61                          GdkEventButton * event, gpointer user_data)
62 {
63 /*
64         MainWindow *mainwin;
65
66         mainwin = mainwindow_get_mainwindow();
67         if (mainwin_hidden) {
68                 gtk_widget_show(mainwin->window);
69                 mainwin_hidden = FALSE;
70         } else {
71                 gtk_widget_hide(mainwin->window);
72                 mainwin_hidden = TRUE;
73         }
74 */
75         return TRUE;
76 }
77
78 static void set_trayicon_pixmap(TrayIconType icontype)
79 {
80         GdkPixmap *pixmap = NULL;
81         GdkBitmap *bitmap = NULL;
82
83         switch(icontype) {
84         case TRAYICON_NEW:
85                 pixmap = newmail_pixmap;
86                 bitmap = newmail_bitmap;
87                 break;
88         case TRAYICON_UNREAD:
89         case TRAYICON_UNREADMARKED:
90                 pixmap = unreadmail_pixmap;
91                 bitmap = unreadmail_bitmap;
92                 break;
93         default:
94                 pixmap = nomail_pixmap;
95                 bitmap = nomail_bitmap;
96                 break;
97         }
98
99         gtk_pixmap_set(GTK_PIXMAP(image), pixmap, bitmap);
100         gtk_widget_shape_combine_mask(trayicon, bitmap, 0, 3);
101 }
102
103 static void update()
104 {
105         gint new, unread, unreadmarked, total;
106         gchar *buf;
107
108         folder_count_total_msgs(&new, &unread, &unreadmarked, &total);
109         buf = g_strdup_printf("New %d, Unread: %d, Total: %d", new, unread, total);
110
111         gtk_tooltips_set_tip(tooltips, eventbox, buf, "");
112         g_free(buf);
113
114         set_trayicon_pixmap(new > 0 ? TRAYICON_NEW : (unread > 0 ? TRAYICON_UNREAD : TRAYICON_NOTHING));
115
116         return FALSE;
117 }
118
119 static gboolean folder_item_update_hook(gpointer source, gpointer data)
120 {
121         update();
122 }
123
124 int plugin_init(gchar **error)
125 {
126         hook_id = hooks_register_hook (FOLDER_ITEM_UPDATE_HOOKLIST, folder_item_update_hook, NULL);
127         if (hook_id == -1) {
128                 *error = g_strdup("Failed to register folder item update hook");
129                 return -1;
130         }
131
132         trayicon = egg_tray_icon_new("Sylpheed-Claws");
133         gtk_widget_set_usize(GTK_WIDGET(trayicon), 16, 16);
134         gtk_container_set_border_width(GTK_CONTAINER(trayicon), 0);
135
136         PIXMAP_CREATE(GTK_WIDGET(trayicon), nomail_pixmap, nomail_bitmap, nomail_xpm);
137         PIXMAP_CREATE(GTK_WIDGET(trayicon), unreadmail_pixmap, unreadmail_bitmap, unreadmail_xpm);
138         PIXMAP_CREATE(GTK_WIDGET(trayicon), newmail_pixmap, newmail_bitmap, newmail_xpm);
139
140         printf("%p\n", nomail_bitmap->user_data);
141     
142         eventbox = gtk_event_box_new();
143         gtk_container_set_border_width(eventbox, 0);
144         gtk_container_add(GTK_CONTAINER(trayicon), GTK_WIDGET(eventbox));
145
146         image = gtk_pixmap_new (nomail_pixmap, nomail_bitmap);
147         gtk_container_add(GTK_CONTAINER(eventbox), GTK_WIDGET(image));
148
149         gtk_signal_connect(GTK_OBJECT(eventbox), "button-press-event", GTK_SIGNAL_FUNC(click_cb), NULL);
150
151         tooltips = gtk_tooltips_new();
152         gtk_tooltips_set_delay(tooltips, 1000);
153         gtk_tooltips_enable(tooltips);
154
155         gtk_widget_show_all(GTK_WIDGET(trayicon));
156
157         update();
158
159         return 0;
160 }
161
162 void plugin_done()
163 {
164         gtk_widget_destroy(GTK_WIDGET(trayicon));
165         hooks_unregister_hook(FOLDER_ITEM_UPDATE_HOOKLIST, hook_id);
166 }
167
168 const gchar *plugin_name()
169 {
170         return "Trayicon";
171 }
172
173 const gchar *plugin_desc()
174 {
175         return "";
176 }
177
178 const gchar *plugin_type()
179 {
180         return "GTK";
181 }