2d5b22e93af57487e665786dab71685db92dd029
[claws.git] / src / stock_pixmap.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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 <glib.h>
21 #include <gtk/gtkwidget.h>
22 #include <gtk/gtkpixmap.h>
23 #include <string.h>
24 #include <dirent.h>
25
26 #include "stock_pixmap.h"
27 #include "gtkutils.h"
28 #include "prefs_common.h"
29 #include "defs.h"
30
31 #include "pixmaps/address.xpm"
32 #include "pixmaps/book.xpm"
33 #include "pixmaps/category.xpm"
34 #include "pixmaps/checkbox_off.xpm"
35 #include "pixmaps/checkbox_on.xpm"
36 #include "pixmaps/clip.xpm"
37 #include "pixmaps/clipkey.xpm"
38 #include "pixmaps/complete.xpm"
39 #include "pixmaps/continue.xpm"
40 #include "pixmaps/deleted.xpm"
41 #include "pixmaps/dir_close.xpm"
42 #include "pixmaps/dir_open.xpm"
43 #include "pixmaps/dir_open_hrm.xpm"
44 #include "pixmaps/error.xpm"
45 #include "pixmaps/forwarded.xpm"
46 #include "pixmaps/group.xpm"
47 #include "pixmaps/inbox.xpm"
48 #include "pixmaps/inbox_hrm.xpm"
49 #include "pixmaps/interface.xpm"
50 #include "pixmaps/jpilot.xpm"
51 #include "pixmaps/key.xpm"
52 #include "pixmaps/ldap.xpm"
53 #include "pixmaps/linewrap.xpm"
54 #include "pixmaps/mark.xpm"
55 #include "pixmaps/locked.xpm"
56 #include "pixmaps/new.xpm"
57 #include "pixmaps/outbox.xpm"
58 #include "pixmaps/outbox_hrm.xpm"
59 #include "pixmaps/replied.xpm"
60 #include "pixmaps/close.xpm"
61 #include "pixmaps/down_arrow.xpm"
62 #include "pixmaps/exec.xpm"
63 #include "pixmaps/mail.xpm"
64 #include "pixmaps/mail_attach.xpm"
65 #include "pixmaps/mail_compose.xpm"
66 #include "pixmaps/mail_forward.xpm"
67 #include "pixmaps/mail_receive.xpm"
68 #include "pixmaps/mail_receive_all.xpm"
69 #include "pixmaps/mail_reply.xpm"
70 #include "pixmaps/mail_reply_to_all.xpm"
71 #include "pixmaps/mail_reply_to_author.xpm"
72 #include "pixmaps/mail_send.xpm"
73 #include "pixmaps/mail_send_queue.xpm"
74 #include "pixmaps/news_compose.xpm"
75 #include "pixmaps/paste.xpm"
76 #include "pixmaps/preferences.xpm"
77 #include "pixmaps/properties.xpm"
78 #include "pixmaps/sylpheed_logo.xpm"
79 #include "pixmaps/address_book.xpm"
80 #include "pixmaps/trash.xpm"
81 #include "pixmaps/trash_hrm.xpm"
82 #include "pixmaps/unread.xpm"
83 #include "pixmaps/vcard.xpm"
84 #include "pixmaps/ignorethread.xpm"
85
86 typedef struct _StockPixmapData StockPixmapData;
87
88 struct _StockPixmapData
89 {
90         gchar **data;
91         GdkPixmap *pixmap;
92         GdkBitmap *mask;
93         gchar *file;
94         gchar *icon_path;
95 };
96
97 static void stock_pixmap_find_themes_in_dir(GList **list, const gchar *dirname);
98
99 static StockPixmapData pixmaps[] =
100 {
101         {address_xpm                    , NULL, NULL, "address", "  "},
102         {address_book_xpm               , NULL, NULL, "address_book", "  "},
103         {book_xpm                               , NULL, NULL, "book", "  "},
104         {category_xpm                   , NULL, NULL, "category", "  "},
105         {checkbox_off_xpm               , NULL, NULL, "checkbox_off", "  "},
106         {checkbox_on_xpm                , NULL, NULL, "checkbox_on", "  "},
107         {clip_xpm                               , NULL, NULL, "clip", "  "},
108         {clipkey_xpm                    , NULL, NULL, "clipkey", "  "},
109         {close_xpm                              , NULL, NULL, "close", "  "},
110         {complete_xpm                   , NULL, NULL, "complete", "  "},
111         {continue_xpm                   , NULL, NULL, "continue", "  "},
112         {deleted_xpm                    , NULL, NULL, "deleted", "  "},
113         {dir_close_xpm                  , NULL, NULL, "dir_close", "  "},
114         {dir_close_xpm                  , NULL, NULL, "dir_close_hrm", " "},
115         {dir_open_xpm                   , NULL, NULL, "dir_open", "  "},
116         {dir_open_hrm_xpm               , NULL, NULL, "dir_open_hrm", "  "},
117         {down_arrow_xpm                 , NULL, NULL, "down_arrow", "  "},
118         {mail_compose_xpm               , NULL, NULL, "edit_extern", "  "},
119         {error_xpm                              , NULL, NULL, "error", "  "},
120         {exec_xpm                               , NULL, NULL, "exec", "  "},
121         {forwarded_xpm                  , NULL, NULL, "forwarded", "  "},
122         {group_xpm                              , NULL, NULL, "group", "  "},
123         {ignorethread_xpm               , NULL, NULL, "ignorethread", "  "},
124         {inbox_xpm                              , NULL, NULL, "inbox_close", "  "},
125         {inbox_hrm_xpm                  , NULL, NULL, "inbox_close_hrm", "  "},
126         {inbox_xpm                              , NULL, NULL, "inbox_open", "  "},
127         {inbox_hrm_xpm                  , NULL, NULL, "inbox_open_hrm", "  "},
128         {paste_xpm                              , NULL, NULL, "insert_file", "  "},
129         {interface_xpm                  , NULL, NULL, "interface", "  "},
130         {jpilot_xpm                             , NULL, NULL, "jpilot", "  "},
131         {key_xpm                                , NULL, NULL, "key", "  "},
132         {ldap_xpm                               , NULL, NULL, "ldap", "  "},
133         {linewrap_xpm                   , NULL, NULL, "linewrap", "  "},
134         {locked_xpm                             , NULL, NULL, "locked", "  "},
135         {mail_xpm                               , NULL, NULL, "mail", "  "},
136         {mail_attach_xpm                , NULL, NULL, "mail_attach", "  "},
137         {mail_compose_xpm               , NULL, NULL, "mail_compose", "  "},
138         {mail_forward_xpm               , NULL, NULL, "mail_forward", "  "},
139         {mail_receive_xpm               , NULL, NULL, "mail_receive", "  "},
140         {mail_receive_all_xpm   , NULL, NULL, "mail_receive_all", "  "},
141         {mail_reply_xpm                 , NULL, NULL, "mail_reply", "  "},
142         {mail_reply_to_all_xpm  , NULL, NULL, "mail_reply_to_all", "  "},
143         {mail_reply_to_author_xpm
144                                                         , NULL, NULL, "mail_reply_to_author", "  "},
145         {mail_send_xpm                  , NULL, NULL, "mail_send", "  "},
146         {mail_send_queue_xpm    , NULL, NULL, "mail_send_queue", "  "},
147         {mail_xpm                               , NULL, NULL, "mail_sign", "  "},
148         {mark_xpm                               , NULL, NULL, "mark", "  "},
149         {new_xpm                                , NULL, NULL, "new", "  "},
150         {news_compose_xpm               , NULL, NULL, "news_compose", "  "},
151         {outbox_xpm                             , NULL, NULL, "outbox_close", "  "},
152         {outbox_hrm_xpm                 , NULL, NULL, "outbox_close_hrm", "  "},
153         {outbox_xpm                             , NULL, NULL, "outbox_open", "  "},
154         {outbox_hrm_xpm                 , NULL, NULL, "outbox_open_hrm", "  "},
155         {replied_xpm                    , NULL, NULL, "replied", "  "},
156         {paste_xpm                              , NULL, NULL, "paste", "  "},
157         {preferences_xpm                , NULL, NULL, "preferences", "  "},
158         {properties_xpm                 , NULL, NULL, "properties", "  "},
159         {outbox_xpm                             , NULL, NULL, "queue_close", "  "},
160         {outbox_hrm_xpm                 , NULL, NULL, "queue_close_hrm", "  "},
161         {outbox_xpm                             , NULL, NULL, "queue_open", "  "},
162         {outbox_hrm_xpm                 , NULL, NULL, "queue_open_hrm", "  "},
163         {sylpheed_logo_xpm              , NULL, NULL, "sylpheed_logo", "  "},
164         {trash_xpm                              , NULL, NULL, "trash_open", "  "},
165         {trash_hrm_xpm                  , NULL, NULL, "trash_open_hrm", "  "},
166         {trash_xpm                              , NULL, NULL, "trash_close", "  "},
167         {trash_hrm_xpm                  , NULL, NULL, "trash_close_hrm", "  "},
168         {unread_xpm                             , NULL, NULL, "unread", "  "},
169         {vcard_xpm                              , NULL, NULL, "vcard", "  "},
170 };
171
172 /* return newly constructed GtkPixmap from GdkPixmap */
173 GtkWidget *stock_pixmap_widget(GtkWidget *window, StockPixmap icon)
174 {
175         GdkPixmap *pixmap;
176         GdkBitmap *mask;
177
178         g_return_val_if_fail(window != NULL, NULL);
179         g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, NULL);
180
181         stock_pixmap_gdk(window, icon, &pixmap, &mask);
182         return gtk_pixmap_new(pixmap, mask);
183 }
184
185 /* create GdkPixmap if it has not created yet */
186 gint stock_pixmap_gdk(GtkWidget *window, StockPixmap icon,
187                       GdkPixmap **pixmap, GdkBitmap **mask)
188 {
189         StockPixmapData *pix_d;
190
191         if (pixmap) *pixmap = NULL;
192         if (mask)   *mask   = NULL;
193
194         g_return_val_if_fail(window != NULL, -1);
195         g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, -1);
196
197         pix_d = &pixmaps[icon];
198
199         if (!pix_d->pixmap || (strcmp(pix_d->icon_path, prefs_common.pixmap_theme_path) != 0)) {
200                 GdkPixmap *pix = NULL;
201         
202                 if (strcmp(prefs_common.pixmap_theme_path, DEFAULT_PIXMAP_THEME) != 0) {
203                         if ( is_dir_exist(prefs_common.pixmap_theme_path) ) {
204                                 char *icon_file_name = g_strconcat(prefs_common.pixmap_theme_path,
205                                                                                          G_DIR_SEPARATOR_S,
206                                                                                          pix_d->file,
207                                                                                          ".xpm",
208                                                                                          NULL);
209                                 
210                                 if (file_exist(icon_file_name, FALSE))
211                                         PIXMAP_CREATE_FROM_FILE(window, pix, pix_d->mask, icon_file_name);
212
213                                 if (pix) pix_d->icon_path = prefs_common.pixmap_theme_path;
214                                 g_free(icon_file_name);
215                         } else {
216                                 /* even the path does not exist (deleted between two sessions), so
217                                 set the preferences to the internal theme */
218                                 prefs_common.pixmap_theme_path = DEFAULT_PIXMAP_THEME;
219                         }
220                 }
221                 pix_d->pixmap = pix;
222         }
223
224         if (!pix_d->pixmap) {
225                 PIXMAP_CREATE(window, pix_d->pixmap, pix_d->mask, pix_d->data);
226                 if (pix_d->pixmap) pix_d->icon_path = DEFAULT_PIXMAP_THEME;     
227         }
228
229         g_return_val_if_fail(pix_d->pixmap != NULL, -1);
230         
231         if (pixmap) *pixmap = pix_d->pixmap;
232         if (mask)   *mask   = pix_d->mask;
233
234         return 0;
235 }
236
237 static void stock_pixmap_find_themes_in_dir(GList **list, const gchar *dirname)
238 {
239         struct dirent **namelist;
240         int n;
241
242         n = scandir(dirname, &namelist, 0, alphasort);
243         if (n > 0) {
244                 while(n--) {
245                         gchar *entry = namelist[n]->d_name;
246                         gchar *fullentry = g_strconcat(dirname, G_DIR_SEPARATOR_S, entry, NULL);
247                         if ((strcmp(entry, ".") != 0) && (strcmp(entry, "..") != 0) && (is_dir_exist(fullentry))) {
248                                 gchar *filetoexist;
249                                 int i;
250                                 for ( i = 0; i < N_STOCK_PIXMAPS; i++) {
251                                         filetoexist = g_strconcat(fullentry, G_DIR_SEPARATOR_S, pixmaps[i].file, ".xpm", NULL);
252                                         if (file_exist(filetoexist, FALSE)) {
253                                                 *list = g_list_append(*list, fullentry);
254                                                 break;
255                                         }
256                                         g_free(filetoexist);
257                                 }
258                         }
259                         g_free(namelist[n]);
260                 }
261                 g_free(namelist);
262         }
263 }
264
265 void stock_pixmap_get_themes(GList **list)
266 {
267         gchar *defaulttheme = DEFAULT_PIXMAP_THEME;
268         gchar *userthemes = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
269                                      RC_DIR, G_DIR_SEPARATOR_S, PIXMAP_THEME_DIR, NULL);
270         gchar *systemthemes = g_strconcat(PACKAGE_DATA_DIR, G_DIR_SEPARATOR_S,
271                                          PIXMAP_THEME_DIR, NULL);
272         
273         *list = g_list_append(*list, defaulttheme);
274         
275         stock_pixmap_find_themes_in_dir(list, userthemes);
276         stock_pixmap_find_themes_in_dir(list, systemthemes);
277         
278         g_free(userthemes);
279 }