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