0fb2559eb83d78ad43e7d6d2761d0652b679a760
[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/up_arrow.xpm"
64 #include "pixmaps/exec.xpm"
65 #include "pixmaps/mail.xpm"
66 #include "pixmaps/mail_attach.xpm"
67 #include "pixmaps/mail_compose.xpm"
68 #include "pixmaps/mail_forward.xpm"
69 #include "pixmaps/mail_receive.xpm"
70 #include "pixmaps/mail_receive_all.xpm"
71 #include "pixmaps/mail_reply.xpm"
72 #include "pixmaps/mail_reply_to_all.xpm"
73 #include "pixmaps/mail_reply_to_author.xpm"
74 #include "pixmaps/mail_send.xpm"
75 #include "pixmaps/mail_send_queue.xpm"
76 #include "pixmaps/news_compose.xpm"
77 #include "pixmaps/paste.xpm"
78 #include "pixmaps/preferences.xpm"
79 #include "pixmaps/properties.xpm"
80 #include "pixmaps/sylpheed_logo.xpm"
81 #include "pixmaps/address_book.xpm"
82 #include "pixmaps/trash.xpm"
83 #include "pixmaps/trash_hrm.xpm"
84 #include "pixmaps/unread.xpm"
85 #include "pixmaps/vcard.xpm"
86 #include "pixmaps/ignorethread.xpm"
87 #include "pixmaps/work_online.xpm"
88 #include "pixmaps/work_offline.xpm"
89 #include "pixmaps/notice_warn.xpm"
90 #include "pixmaps/notice_error.xpm"
91 #include "pixmaps/notice_note.xpm"
92 #include "pixmaps/quicksearch.xpm"
93 #include "pixmaps/gpg_signed.xpm"
94 #include "pixmaps/drafts_close.xpm"
95 #include "pixmaps/drafts_open.xpm"
96 #include "pixmaps/messageview_text.xpm"
97 #include "pixmaps/mime_text_plain.xpm"
98 #include "pixmaps/mime_text_html.xpm"
99 #include "pixmaps/mime_application.xpm"
100 #include "pixmaps/mime_application_octet_stream.xpm"
101 #include "pixmaps/mime_image.xpm"
102 #include "pixmaps/mime_audio.xpm"
103 #include "pixmaps/mime_text_enriched.xpm"
104 #include "pixmaps/mime_unknown.xpm"
105 #include "pixmaps/mime_gpg_signed.xpm"
106 #include "pixmaps/mime_gpg_passed.xpm"
107 #include "pixmaps/mime_gpg_failed.xpm"
108 #include "pixmaps/mime_gpg_unknown.xpm"
109 #include "pixmaps/mime_gpg_expired.xpm"                  
110 #include "pixmaps/mime_message.xpm"                  
111 #include "pixmaps/address_search.xpm"
112
113 typedef struct _StockPixmapData StockPixmapData;
114
115 struct _StockPixmapData
116 {
117         gchar **data;
118         GdkPixmap *pixmap;
119         GdkBitmap *mask;
120         gchar *file;
121         gchar *icon_path;
122 };
123
124 static void stock_pixmap_find_themes_in_dir(GList **list, const gchar *dirname);
125
126 static StockPixmapData pixmaps[] =
127 {
128         {address_xpm                            , NULL, NULL, "address", "  "},
129         {address_book_xpm                       , NULL, NULL, "address_book", "  "},
130         {address_search_xpm                     , NULL, NULL, "address_search", "  "},
131         {book_xpm                               , NULL, NULL, "book", "  "},
132         {category_xpm                           , NULL, NULL, "category", "  "},
133         {checkbox_off_xpm                       , NULL, NULL, "checkbox_off", "  "},
134         {checkbox_on_xpm                        , NULL, NULL, "checkbox_on", "  "},
135         {clip_xpm                               , NULL, NULL, "clip", "  "},
136         {clipkey_xpm                            , NULL, NULL, "clipkey", "  "},
137         {close_xpm                              , NULL, NULL, "close", "  "},
138         {complete_xpm                           , NULL, NULL, "complete", "  "},
139         {continue_xpm                           , NULL, NULL, "continue", "  "},
140         {deleted_xpm                            , NULL, NULL, "deleted", "  "},
141         {dir_close_xpm                          , NULL, NULL, "dir_close", "  "},
142         {dir_close_xpm                          , NULL, NULL, "dir_close_hrm", " "},
143         {dir_open_xpm                           , NULL, NULL, "dir_open", "  "},
144         {dir_open_hrm_xpm                       , NULL, NULL, "dir_open_hrm", "  "},
145         {down_arrow_xpm                         , NULL, NULL, "down_arrow", "  "},
146         {up_arrow_xpm                           , NULL, NULL, "up_arrow", "  "},
147         {mail_compose_xpm                       , NULL, NULL, "edit_extern", "  "},
148         {error_xpm                              , NULL, NULL, "error", "  "},
149         {exec_xpm                               , NULL, NULL, "exec", "  "},
150         {forwarded_xpm                          , NULL, NULL, "forwarded", "  "},
151         {group_xpm                              , NULL, NULL, "group", "  "},
152         {ignorethread_xpm                       , NULL, NULL, "ignorethread", "  "},
153         {inbox_xpm                              , NULL, NULL, "inbox_close", "  "},
154         {inbox_hrm_xpm                          , NULL, NULL, "inbox_close_hrm", "  "},
155         {inbox_xpm                              , NULL, NULL, "inbox_open", "  "},
156         {inbox_hrm_xpm                          , NULL, NULL, "inbox_open_hrm", "  "},
157         {paste_xpm                              , NULL, NULL, "insert_file", "  "},
158         {interface_xpm                          , NULL, NULL, "interface", "  "},
159         {jpilot_xpm                             , NULL, NULL, "jpilot", "  "},
160         {key_xpm                                , NULL, NULL, "key", "  "},
161         {ldap_xpm                               , NULL, NULL, "ldap", "  "},
162         {linewrap_xpm                           , NULL, NULL, "linewrap", "  "},
163         {locked_xpm                             , NULL, NULL, "locked", "  "},
164         {mail_xpm                               , NULL, NULL, "mail", "  "},
165         {mail_attach_xpm                        , NULL, NULL, "mail_attach", "  "},
166         {mail_compose_xpm                       , NULL, NULL, "mail_compose", "  "},
167         {mail_forward_xpm                       , NULL, NULL, "mail_forward", "  "},
168         {mail_receive_xpm                       , NULL, NULL, "mail_receive", "  "},
169         {mail_receive_all_xpm                   , NULL, NULL, "mail_receive_all", "  "},
170         {mail_reply_xpm                         , NULL, NULL, "mail_reply", "  "},
171         {mail_reply_to_all_xpm                  , NULL, NULL, "mail_reply_to_all", "  "},
172         {mail_reply_to_author_xpm
173                                                 , NULL, NULL, "mail_reply_to_author", "  "},
174         {mail_send_xpm                          , NULL, NULL, "mail_send", "  "},
175         {mail_send_queue_xpm                    , NULL, NULL, "mail_send_queue", "  "},
176         {mail_xpm                               , NULL, NULL, "mail_sign", "  "},
177         {mark_xpm                               , NULL, NULL, "mark", "  "},
178         {new_xpm                                , NULL, NULL, "new", "  "},
179         {news_compose_xpm                       , NULL, NULL, "news_compose", "  "},
180         {outbox_xpm                             , NULL, NULL, "outbox_close", "  "},
181         {outbox_hrm_xpm                         , NULL, NULL, "outbox_close_hrm", "  "},
182         {outbox_xpm                             , NULL, NULL, "outbox_open", "  "},
183         {outbox_hrm_xpm                         , NULL, NULL, "outbox_open_hrm", "  "},
184         {replied_xpm                            , NULL, NULL, "replied", "  "},
185         {paste_xpm                              , NULL, NULL, "paste", "  "},
186         {preferences_xpm                        , NULL, NULL, "preferences", "  "},
187         {properties_xpm                         , NULL, NULL, "properties", "  "},
188         {outbox_xpm                             , NULL, NULL, "queue_close", "  "},
189         {outbox_hrm_xpm                         , NULL, NULL, "queue_close_hrm", "  "},
190         {outbox_xpm                             , NULL, NULL, "queue_open", "  "},
191         {outbox_hrm_xpm                         , NULL, NULL, "queue_open_hrm", "  "},
192         {trash_xpm                              , NULL, NULL, "trash_open", "  "},
193         {trash_hrm_xpm                          , NULL, NULL, "trash_open_hrm", "  "},
194         {trash_xpm                              , NULL, NULL, "trash_close", "  "},
195         {trash_hrm_xpm                          , NULL, NULL, "trash_close_hrm", "  "},
196         {unread_xpm                             , NULL, NULL, "unread", "  "},
197         {vcard_xpm                              , NULL, NULL, "vcard", "  "},
198         {work_online_xpm                        , NULL, NULL, "work_online", "  "},
199         {work_offline_xpm                       , NULL, NULL, "work_offline", "  "},
200         {notice_warn_xpm                        , NULL, NULL, "notice_warn",  "  "},
201         {notice_error_xpm                       , NULL, NULL, "notice_error",  "  "},
202         {notice_note_xpm                        , NULL, NULL, "notice_note",  "  "},
203         {quicksearch_xpm                        , NULL, NULL, "quicksearch",  "  "},
204         {gpg_signed_xpm                         , NULL, NULL, "gpg_signed", "  "},
205         {drafts_close_xpm                       , NULL, NULL, "drafts_close", "  "},
206         {drafts_open_xpm                        , NULL, NULL, "drafts_open", "  "},
207         {messageview_text_xpm                   , NULL, NULL, "messageview_text", "  "},
208         {mime_text_plain_xpm                    , NULL, NULL, "mime_text_plain", "  "},
209         {mime_text_html_xpm                     , NULL, NULL, "mime_text_html", "  "},
210         {mime_application_xpm                   , NULL, NULL, "mime_application", "  "},
211         {mime_application_octet_stream_xpm      , NULL, NULL, "mime_application_octet_stream", "  "},
212         {mime_image_xpm                         , NULL, NULL, "mime_image", "  "},
213         {mime_audio_xpm                         , NULL, NULL, "mime_audio", "  "},
214         {mime_text_enriched_xpm                 , NULL, NULL, "mime_text_enriched", "  "},
215         {mime_unknown_xpm                       , NULL, NULL, "mime_unknown", "  "},    
216         {mime_gpg_signed_xpm                    , NULL, NULL, "mime_gpg_signed", "  "},
217         {mime_gpg_passed_xpm                    , NULL, NULL, "mime_gpg_passed", "  "},
218         {mime_gpg_failed_xpm                    , NULL, NULL, "mime_gpg_failed", "  "}, 
219         {mime_gpg_unknown_xpm                   , NULL, NULL, "mime_gpg_unknown", "  "},
220         {mime_gpg_expired_xpm                   , NULL, NULL, "mime_gpg_expired", "  "},
221         {mime_message_xpm                       , NULL, NULL, "mime_message", "  "},
222         {sylpheed_logo_xpm                      , NULL, NULL, "sylpheed_logo", "  "},
223 };
224
225 /* return newly constructed GtkPixmap from GdkPixmap */
226 GtkWidget *stock_pixmap_widget(GtkWidget *window, StockPixmap icon)
227 {
228         GdkPixmap *pixmap;
229         GdkBitmap *mask;
230
231         g_return_val_if_fail(window != NULL, NULL);
232         g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, NULL);
233
234         if (stock_pixmap_gdk(window, icon, &pixmap, &mask) != -1)
235                 return gtk_pixmap_new(pixmap, mask);
236         
237         return NULL;
238 }
239
240 /* create GdkPixmap if it has not created yet */
241 gint stock_pixmap_gdk(GtkWidget *window, StockPixmap icon,
242                       GdkPixmap **pixmap, GdkBitmap **mask)
243 {
244         StockPixmapData *pix_d;
245
246         if (pixmap) *pixmap = NULL;
247         if (mask)   *mask   = NULL;
248
249         g_return_val_if_fail(window != NULL, -1);
250         g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, -1);
251
252         pix_d = &pixmaps[icon];
253
254         if (!pix_d->pixmap || (strcmp(pix_d->icon_path, prefs_common.pixmap_theme_path) != 0)) {
255                 GdkPixmap *pix = NULL;
256         
257                 if (strcmp(prefs_common.pixmap_theme_path, DEFAULT_PIXMAP_THEME) != 0) {
258                         if ( is_dir_exist(prefs_common.pixmap_theme_path) ) {
259                                 char *icon_file_name; 
260                                 
261                                 icon_file_name = g_strconcat(prefs_common.pixmap_theme_path,
262                                                              G_DIR_SEPARATOR_S,
263                                                              pix_d->file,
264                                                              ".xpm",
265                                                              NULL);
266                                 if (is_file_exist(icon_file_name))
267                                         PIXMAP_CREATE_FROM_FILE(window, pix, pix_d->mask, icon_file_name);
268                                 if (pix) 
269                                         pix_d->icon_path = prefs_common.pixmap_theme_path;
270                                 g_free(icon_file_name);
271                         } else {
272                                 /* even the path does not exist (deleted between two sessions), so
273                                 set the preferences to the internal theme */
274                                 prefs_common.pixmap_theme_path = DEFAULT_PIXMAP_THEME;
275                         }
276                 }
277                 pix_d->pixmap = pix;
278         }
279
280         if (!pix_d->pixmap) {
281                 PIXMAP_CREATE(window, pix_d->pixmap, pix_d->mask, pix_d->data);
282                 if (pix_d->pixmap) 
283                         pix_d->icon_path = DEFAULT_PIXMAP_THEME;        
284         }
285
286         g_return_val_if_fail(pix_d->pixmap != NULL, -1);
287         
288         if (pixmap) 
289                 *pixmap = pix_d->pixmap;
290         if (mask)   
291                 *mask   = pix_d->mask;
292
293         return 0;
294 }
295
296 static void stock_pixmap_find_themes_in_dir(GList **list, const gchar *dirname)
297 {
298         struct dirent *d;
299         DIR *dp;
300         
301         if ((dp = opendir(dirname)) == NULL) {
302                 debug_print("dir %s not found, skipping theme scan", dirname);
303                 return;
304         }
305         
306         while ((d = readdir(dp)) != NULL) {
307                 gchar *entry;
308                 gchar *fullentry;
309
310                 entry     = d->d_name;
311                 fullentry = g_strconcat(dirname, G_DIR_SEPARATOR_S, entry, NULL);
312                 
313                 if (strcmp(entry, ".") != 0 && strcmp(entry, "..") != 0 && is_dir_exist(fullentry)) {
314                         gchar *filetoexist;
315                         int i;
316                         
317                         for (i = 0; i < N_STOCK_PIXMAPS; i++) {
318                                 filetoexist = g_strconcat(fullentry, G_DIR_SEPARATOR_S, pixmaps[i].file, ".xpm", NULL);
319                                 if (is_file_exist(filetoexist)) {
320                                         *list = g_list_append(*list, fullentry);
321                                         break;
322                                 }
323                         }
324                         g_free(filetoexist);
325                         if (i == N_STOCK_PIXMAPS) 
326                                 g_free(fullentry);
327                 } else 
328                         g_free(fullentry);
329         }
330         closedir(dp);
331 }
332
333 GList *stock_pixmap_themes_list_new(void)
334 {
335         gchar *defaulttheme;
336         gchar *userthemes;
337         gchar *systemthemes;
338         GList *list = NULL;
339         
340         defaulttheme = g_strdup(DEFAULT_PIXMAP_THEME);
341         userthemes   = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
342                                    RC_DIR, G_DIR_SEPARATOR_S, 
343                                    PIXMAP_THEME_DIR, NULL);
344         systemthemes = g_strconcat(PACKAGE_DATA_DIR, G_DIR_SEPARATOR_S,
345                                    PIXMAP_THEME_DIR, NULL);
346
347         list = g_list_append(list, defaulttheme);
348         
349         stock_pixmap_find_themes_in_dir(&list, userthemes);
350         stock_pixmap_find_themes_in_dir(&list, systemthemes);
351
352         g_free(userthemes);
353         g_free(systemthemes);
354         return list;
355 }
356
357 void stock_pixmap_themes_list_free(GList *list)
358 {
359         GList *ptr;
360
361         for (ptr = g_list_first(list); ptr != NULL; ptr = g_list_next(ptr)) 
362                 if (ptr->data)
363                         g_free(ptr->data);
364         g_list_free(list);              
365 }
366
367 gchar *stock_pixmap_get_name (StockPixmap icon)
368 {
369         g_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, NULL);
370         
371         return pixmaps[icon].file;
372
373 }
374
375 StockPixmap stock_pixmap_get_icon (gchar *file)
376 {
377         gint i;
378         
379         for (i = 0; i < N_STOCK_PIXMAPS; i++) {
380                 if (strcmp (pixmaps[i].file, file) == 0)
381                         return i;
382         }
383         return -1;
384 }