a8b67349a72590f163085b9d81685fcb5de3e2f5
[claws.git] / src / prefs_themes.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2003-2007 Hiroyuki Yamamoto & the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include "defs.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/stat.h>
30
31 #include <glib.h>
32 #include <glib/gi18n.h>
33 #include <gtk/gtk.h>
34
35 #include "utils.h"
36 #include "codeconv.h"
37 #include "prefs_common.h"
38 #include "prefs_gtk.h"
39
40 #include "gtk/gtkutils.h"
41 #include "gtk/prefswindow.h"
42 #include "gtk/filesel.h"
43
44 #include "stock_pixmap.h"
45 #include "mainwindow.h"
46 #include "compose.h"
47 #include "alertpanel.h"
48
49 #define IS_CURRENT_THEME(path)  (strcmp(prefs_common.pixmap_theme_path, path) == 0)
50 #define IS_INTERNAL_THEME(path) (strcmp(DEFAULT_PIXMAP_THEME, path) == 0)
51 #define IS_SYSTEM_THEME(path)   (prefs_themes_is_system_theme(path))
52
53 #define PREVIEW_ICONS 7
54
55 typedef struct _ThemesPage
56 {
57         PrefsPage page;
58
59         GtkWidget *window;              /* do not modify */
60
61         GtkWidget *op_menu;
62         GtkWidget *btn_install;
63         GtkWidget *btn_more;
64         GtkWidget *global;
65
66         GtkWidget *name;
67         GtkWidget *author;
68         GtkWidget *url;
69         GtkWidget *status;
70         
71         GtkWidget *icons[PREVIEW_ICONS];
72         
73         GtkWidget *btn_use;
74         GtkWidget *btn_remove;
75
76         GdkPixbuf *pixbufs[PREVIEW_ICONS];
77
78         /* gchar     *theme_path; */
79 } ThemesPage;
80
81 typedef struct _ThemeInfo
82 {
83         gchar *name;
84         gchar *author;
85         gchar *url;
86         gchar *status;
87 } ThemeInfo;
88
89 typedef struct _ThemeName
90 {
91         gchar *name;
92         GList *item;
93 } ThemeName;
94
95 typedef struct _ThemesData
96 {
97         GList      *themes;
98         GList      *names;
99         gchar      *displayed;
100         ThemesPage *page;
101 } ThemesData;
102
103 typedef void (*FileFunc) (const gchar *filename, gpointer data);
104
105 typedef struct _DirInfo {
106         gint bytes;
107         gint files;
108         gint pixms;
109 } DirInfo;
110
111 typedef struct _CopyInfo {
112         gchar *dest;
113         gchar *status;
114 } CopyInfo;
115
116 static ThemesData *prefs_themes_data;
117
118 StockPixmap prefs_themes_icons[PREVIEW_ICONS] = { 
119         STOCK_PIXMAP_DIR_CLOSE,
120         STOCK_PIXMAP_MAIL_SEND,
121         STOCK_PIXMAP_MAIL_RECEIVE, 
122         STOCK_PIXMAP_MAIL_ATTACH,
123         STOCK_PIXMAP_BOOK, 
124         STOCK_PIXMAP_MIME_TEXT_PLAIN, 
125         STOCK_PIXMAP_REPLIED
126 };
127
128
129
130 static void prefs_themes_btn_use_clicked_cb     (GtkWidget *widget, gpointer data);
131 static void prefs_themes_btn_remove_clicked_cb  (GtkWidget *widget, gpointer data);
132 static void prefs_themes_btn_install_clicked_cb (GtkWidget *widget, gpointer data);
133 static void prefs_themes_menu_item_activated_cb (GtkWidget *widget, gpointer data);
134
135 static void prefs_themes_update_buttons         (const ThemesData *tdata);
136 static void prefs_themes_display_global_stats   (const ThemesData *tdata);
137 static void prefs_themes_get_theme_info         (ThemesData *tdata);
138 static void prefs_themes_display_theme_info     (ThemesData *tdata, const ThemeInfo *info);
139 static void prefs_themes_get_themes_and_names   (ThemesData *tdata);
140 static int prefs_themes_cmp_name(gconstpointer a, gconstpointer b);
141 static void prefs_themes_free_names             (ThemesData *tdata);
142
143 static void prefs_themes_set_themes_menu        (GtkComboBox *combo, const ThemesData *tdata);
144
145 static gchar *prefs_themes_get_theme_stats      (const gchar *dirname);
146 static gboolean prefs_themes_is_system_theme    (const gchar *dirname);
147
148 static void prefs_themes_create_widget          (PrefsPage *page, GtkWindow *window, gpointer data);
149 static void prefs_themes_destroy_widget         (PrefsPage *page);
150 static void prefs_themes_save                   (PrefsPage *page);
151
152 static void prefs_themes_foreach_file           (const gchar *dirname, const FileFunc func, gpointer data);
153 static void prefs_themes_file_stats             (const gchar *filename, gpointer data);
154 static void prefs_themes_file_remove            (const gchar *filename, gpointer data);
155 static void prefs_themes_file_install           (const gchar *filename, gpointer data);
156
157
158
159 static void prefs_themes_file_stats(const gchar *filename, gpointer data)
160 {
161         struct stat s;
162         DirInfo    *di = (DirInfo *)data;
163         gint        len;
164         
165         if (0 == g_stat(filename, &s) && 0 != S_ISREG(s.st_mode)) {
166                 di->bytes += s.st_size;
167                 di->files++;
168                 len = strlen(filename);
169                 if (len > 4) {
170                         const gchar *extension = filename+(len-4);
171                         if (!strcmp(extension, ".xpm"))
172                                 di->pixms++;
173                         else if (!strcmp(extension, ".png"))
174                                 di->pixms++;
175                 }
176         }
177 }
178         
179 static void prefs_themes_file_remove(const gchar *filename, gpointer data)
180 {
181         gchar **status = (gchar **)data;
182         gchar *base;
183         
184         if ((*status) != NULL)
185                 return;
186         
187         base = g_path_get_basename(filename);
188         if (TRUE == is_dir_exist(filename)) {
189                 if (strcmp(base, ".") != 0 && strcmp(base, "..") != 0)
190                         g_warning("prefs_themes_file_remove(): subdir in theme dir skipped: '%s'.\n",
191                                                 base);
192         }
193         else if (0 != claws_unlink(filename)) {
194                 (*status) = g_strdup(filename);
195         }
196         g_free(base);
197 }
198
199 static void prefs_themes_file_install(const gchar *filename, gpointer data)
200 {
201         CopyInfo *ci = (CopyInfo *)data;
202         gchar *base;
203         
204         if (ci->status != NULL)
205                 return;
206         
207         base = g_path_get_basename(filename);
208         if (TRUE == is_dir_exist(filename)) {
209                 if (strcmp(base, ".") != 0 && strcmp(base, "..") !=0 )
210                         g_warning("prefs_themes_file_install(): subdir in theme dir skipped: '%s'.\n",
211                                                 base);
212         }
213         else {
214                 gchar *fulldest;
215                 
216                 fulldest = g_strconcat(ci->dest, G_DIR_SEPARATOR_S, base, NULL);
217                 
218                 if (0 != copy_file(filename, fulldest, FALSE)) {
219                         ci->status = g_strdup(filename);
220                 }
221                 g_free(fulldest);
222         }
223         g_free(base);
224 }
225
226 static void prefs_themes_foreach_file(const gchar *dirname, const FileFunc func, gpointer data)
227 {
228         struct dirent *d;
229         DIR           *dp;
230
231         g_return_if_fail(dirname != NULL);
232         g_return_if_fail(func != NULL);
233         
234         if ((dp = opendir(dirname)) == NULL) {
235                 debug_print("directory %s not found\n", dirname);
236                 return;
237         }
238
239         while ((d = readdir(dp)) != NULL) {
240                 gchar *entry;
241                 gchar *fullentry;
242
243                 entry     = d->d_name;
244                 fullentry = g_strconcat(dirname, G_DIR_SEPARATOR_S, entry, NULL);
245
246                 (*func)(fullentry, data);
247                 
248                 g_free(fullentry);
249         }
250         closedir(dp);
251 }
252
253 static gboolean prefs_themes_is_system_theme(const gchar *dirname)
254 {
255         gint len;
256         
257         g_return_val_if_fail(dirname != NULL, FALSE);
258
259         len = strlen(PACKAGE_DATA_DIR);
260         if (strlen(dirname) > len && 0 == strncmp(dirname, PACKAGE_DATA_DIR, len))
261                 return TRUE;
262         
263         return FALSE;
264 }
265
266 static void prefs_themes_set_themes_menu(GtkComboBox *combo, const ThemesData *tdata)
267 {
268         GtkListStore *store;
269         GtkTreeIter iter;
270         GList     *themes = tdata->names;
271         gint       i = 0, active = 0;
272         GList     *sorted_list = NULL;
273
274         g_return_if_fail(combo != NULL);
275
276         /* sort theme data list by data name */
277         while (themes != NULL) {
278                 ThemeName *tname = (ThemeName *)(themes->data);
279
280                 sorted_list = g_list_insert_sorted(sorted_list, (gpointer)(tname),
281                                                    (GCompareFunc)prefs_themes_cmp_name);
282
283                 themes = g_list_next(themes);
284         }
285
286         store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
287         
288         /* feed gtk_menu w/ sorted themes names */
289         themes = sorted_list;
290         while (themes != NULL) {
291                 ThemeName *tname = (ThemeName *)(themes->data);
292                 gchar     *tpath = (gchar *)(tname->item->data);
293
294                 gtk_list_store_append(store, &iter);
295                 gtk_list_store_set(store, &iter,
296                                    0, tname->name,
297                                    1, tname->item->data, -1);
298
299                 if (tdata->displayed != NULL && !strcmp2(tdata->displayed,tpath))
300                         active = i;
301                 ++i;
302
303                 themes = g_list_next(themes);
304         }
305
306         g_list_free(sorted_list);
307
308         g_signal_connect(G_OBJECT(combo), "changed",
309                          G_CALLBACK(prefs_themes_menu_item_activated_cb),
310                          NULL);
311
312         gtk_combo_box_set_model(combo, GTK_TREE_MODEL(store));
313         gtk_combo_box_set_active(combo, active);
314 }
315
316 static int prefs_themes_cmp_name(gconstpointer a_p, gconstpointer b_p)
317 {
318         /* compare two ThemeData structures by their name attribute */
319         return strcmp2((gchar *)(((ThemeName*)a_p)->name),
320                                         (gchar *)(((ThemeName*)b_p)->name));
321 }
322
323 static void prefs_themes_get_themes_and_names(ThemesData *tdata)
324 {
325         GList *tpaths;
326         
327         g_return_if_fail(tdata != NULL);
328         
329         if (tdata->themes != NULL)
330                 stock_pixmap_themes_list_free(tdata->themes);
331         if (tdata->names != NULL)
332                 prefs_themes_free_names(tdata);
333         
334         tdata->themes = stock_pixmap_themes_list_new();
335         
336         tpaths = tdata->themes;
337         while (tpaths != NULL) {
338                 ThemeName *name = g_new0(ThemeName, 1);
339                 gchar *sname = g_path_get_basename((const gchar *)(tpaths->data));
340                 
341                 if (IS_INTERNAL_THEME(sname))
342                         name->name = g_strdup(_("Default internal theme"));
343                 else
344                         name->name = g_strdup(sname);
345                 name->item = tpaths;
346                         
347                 tdata->names = g_list_append(tdata->names, name);
348                 if (!strcmp2(tpaths->data, prefs_common.pixmap_theme_path)) {
349                         tdata->displayed = (gchar *)tpaths->data;
350                 }
351                 tpaths = g_list_next(tpaths);
352                 g_free(sname);  
353         }
354 }
355
356 void prefs_themes_init(void)
357 {
358         ThemesData   *tdata;
359         ThemesPage   *page;
360         GList        *tpaths;
361         static gchar *path[3];
362
363         path[0] = _("Display");
364         path[1] = _("Themes");
365         path[2] = NULL;
366
367         debug_print("Creating preferences for themes...\n");
368         
369         tdata = g_new0(ThemesData, 1);
370         prefs_themes_data = tdata;
371
372         prefs_themes_get_themes_and_names(tdata);
373         
374         page = g_new0(ThemesPage, 1);
375         
376         page->page.path = path;
377         page->page.create_widget = prefs_themes_create_widget;
378         page->page.destroy_widget = prefs_themes_destroy_widget;
379         page->page.save_page = prefs_themes_save;
380         page->page.weight = 130.0;
381         prefs_gtk_register_page((PrefsPage *) page);
382
383         tdata->page = page;
384
385         tpaths = g_list_first(tdata->themes);
386         if (tdata->displayed == NULL)
387                 tdata->displayed = (gchar *)(tpaths->data);
388 }
389
390 static void prefs_themes_free_names(ThemesData *tdata)
391 {
392         GList *names;
393         
394         names = tdata->names;
395         while (names != NULL) {
396                 ThemeName *tn = (ThemeName *)(names->data);
397                 
398                 tn->item = NULL;
399                 g_free(tn->name);
400                 g_free(tn);
401                 
402                 names = g_list_next(names);
403         }
404         g_list_free(names);
405         tdata->names = NULL;
406 }
407
408 void prefs_themes_done(void)
409 {
410         ThemesData *tdata = prefs_themes_data;
411
412         debug_print("Finished preferences for themes.\n");
413         
414         stock_pixmap_themes_list_free(tdata->themes);
415         prefs_themes_free_names(tdata); 
416         g_free(tdata->page);
417         g_free(tdata);
418 }
419
420 static void prefs_themes_btn_use_clicked_cb(GtkWidget *widget, gpointer data)
421 {
422         ThemesData *tdata = prefs_themes_data;
423         gchar      *theme_str;
424
425         theme_str = tdata->displayed;
426         
427         g_free(prefs_common.pixmap_theme_path);
428         
429         prefs_common.pixmap_theme_path = g_strdup(theme_str);
430        
431         main_window_reflect_prefs_all_real(TRUE);
432         compose_reflect_prefs_pixmap_theme();
433        
434         prefs_themes_update_buttons(tdata);
435 }
436
437 static void prefs_themes_btn_remove_clicked_cb(GtkWidget *widget, gpointer data)
438 {
439         ThemesData *tdata = prefs_themes_data;
440         gchar      *theme_str;
441         gchar      *alert_title = NULL;
442         AlertValue  val = 0;
443         gchar      *tmp = NULL;
444
445         theme_str = tdata->displayed;
446         
447         tmp = g_path_get_basename(theme_str);
448
449         if (IS_SYSTEM_THEME(theme_str)) {
450                 if (!superuser_p()) {
451                         alertpanel_error(_("Only root can remove system themes"));
452                         return;
453                 }
454                 alert_title = g_strdup_printf(_("Remove system theme '%s'"), tmp);
455         }
456         if (NULL == alert_title) {
457                 alert_title = g_strdup_printf(_("Remove theme '%s'"), tmp);
458         }
459
460         g_free(tmp);
461
462         val = alertpanel(alert_title,
463                          _("Are you sure you want to remove this theme?"),
464                          GTK_STOCK_NO, GTK_STOCK_YES, NULL);
465         g_free(alert_title);
466
467         if (G_ALERTALTERNATE == val) {
468                 gchar *status = NULL;
469                 
470                 prefs_themes_foreach_file(theme_str, prefs_themes_file_remove, &status); 
471                 if (0 != rmdir(theme_str)) {
472                         if (status != NULL) {
473                                 alertpanel_error(_("File %s failed\nwhile removing theme."), status);
474                                 g_free(status);
475                         }
476                         else
477                                 alertpanel_error(_("Removing theme directory failed."));
478                 }
479                 else {  
480                         alertpanel_notice(_("Theme removed successfully"));
481                         /* update interface back to first theme */
482                         prefs_themes_get_themes_and_names(tdata);
483                         prefs_themes_set_themes_menu(GTK_COMBO_BOX(tdata->page->op_menu), tdata);
484                         prefs_themes_display_global_stats(tdata);
485                         tdata->displayed = (gchar *)((g_list_first(tdata->themes))->data);
486                         prefs_themes_get_theme_info(tdata);
487                 }
488         }
489 }
490
491 static void prefs_themes_btn_install_clicked_cb(GtkWidget *widget, gpointer data)
492 {
493         gchar      *filename, *source;
494         gchar      *themeinfo, *themename;
495         gchar      *alert_title = NULL;
496         CopyInfo   *cinfo;
497         AlertValue  val = 0;
498         ThemesData *tdata = prefs_themes_data;
499         
500         filename = filesel_select_file_open_folder(_("Select theme folder"), NULL);
501         if (filename == NULL) 
502                 return;
503         
504         if (filename[strlen(filename) - 1] != G_DIR_SEPARATOR)
505                 filename = g_strconcat(filename, G_DIR_SEPARATOR_S, NULL);
506         else
507                 filename = g_strdup(filename);
508
509         cinfo = g_new0(CopyInfo, 1);
510         source = g_path_get_dirname(filename);
511         themename = g_path_get_basename(source);
512         debug_print("Installing '%s' theme from %s\n", themename, filename);
513
514         themeinfo = g_strconcat(source, G_DIR_SEPARATOR_S, THEMEINFO_FILE, NULL);
515         alert_title = g_strdup_printf(_("Install theme '%s'"), themename);
516         if (file_exist(themeinfo, FALSE) == FALSE) {
517                 val = alertpanel(alert_title,
518                                  _("This folder doesn't seem to be a theme folder.\nInstall anyway?"),
519                                  GTK_STOCK_NO, GTK_STOCK_YES, NULL);
520                 if (G_ALERTALTERNATE != val)
521                         goto end_inst;
522         }
523         if (superuser_p ()) {
524                 val = alertpanel(alert_title,
525                                  _("Do you want to install theme for all users?"),
526                                  GTK_STOCK_NO, GTK_STOCK_YES, NULL);
527                 switch (val) {
528                 case G_ALERTALTERNATE:
529                         cinfo->dest = g_strconcat(PACKAGE_DATA_DIR, G_DIR_SEPARATOR_S,
530                                                   PIXMAP_THEME_DIR, G_DIR_SEPARATOR_S, 
531                                                   themename, NULL);
532                         break;
533                 case G_ALERTDEFAULT:
534                         break;
535                 default:
536                         goto end_inst;
537                 }
538         }
539         g_free(alert_title);
540         if (cinfo->dest == NULL) {
541                 cinfo->dest = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
542                                           PIXMAP_THEME_DIR, G_DIR_SEPARATOR_S,
543                                           themename, NULL);
544         }
545         if (TRUE == is_dir_exist(cinfo->dest)) {
546                 AlertValue val = alertpanel_full(_("Theme exists"),
547                                 _("A theme with the same name is\nalready installed in this location.\n\n"
548                                   "Do you want to replace it?"),
549                                 GTK_STOCK_CANCEL, _("Overwrite"), NULL, FALSE,
550                                 NULL, ALERT_WARNING, G_ALERTDEFAULT);
551                 if (val == G_ALERTALTERNATE) {
552                         if (remove_dir_recursive(cinfo->dest) < 0) {
553                                 alertpanel_error(_("Couldn't delete the old theme in %s."), cinfo->dest);
554                                 goto end_inst;
555                         }
556                 } else {
557                         goto end_inst;
558                 }
559         }
560         if (0 != make_dir_hier(cinfo->dest)) {
561                 alertpanel_error(_("Couldn't create destination directory %s."), cinfo->dest);
562                 goto end_inst;
563         }
564         prefs_themes_foreach_file(source, prefs_themes_file_install, cinfo);
565         if (cinfo->status == NULL) {
566                 GList *insted;
567
568                 /* update interface to show newly installed theme */
569                 prefs_themes_get_themes_and_names(tdata);
570                 insted = g_list_find_custom(tdata->themes, 
571                                             (gpointer)(cinfo->dest), 
572                                             (GCompareFunc)strcmp2);
573                 if (NULL != insted) {
574                         alertpanel_notice(_("Theme installed successfully."));
575                         tdata->displayed = (gchar *)(insted->data);
576                         prefs_themes_set_themes_menu(GTK_COMBO_BOX(tdata->page->op_menu), tdata);
577                         prefs_themes_display_global_stats(tdata);
578                         prefs_themes_get_theme_info(tdata);
579                 }
580                 else
581                         alertpanel_error(_("Failed installing theme"));
582         }
583         else
584                 alertpanel_error(_("File %s failed\nwhile installing theme."), cinfo->status);
585 end_inst:
586         g_free(cinfo->dest);
587         g_free(filename);
588         g_free(source);
589         g_free(themeinfo);
590         g_free(cinfo);
591         g_free(themename);
592 }
593
594 static void prefs_themes_menu_item_activated_cb(GtkWidget *widget, gpointer data)
595 {
596         ThemesData *tdata = prefs_themes_data;
597         gchar      *path;
598         GtkTreeModel *model;
599         GtkTreeIter iter;
600         
601         g_return_if_fail(gtk_combo_box_get_active_iter(GTK_COMBO_BOX(widget), &iter));
602         
603         model = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));                 
604         gtk_tree_model_get(model, &iter, 1, &path, -1); 
605
606         tdata->displayed = path;
607         prefs_themes_get_theme_info(tdata);
608 }
609
610 static void prefs_themes_update_buttons(const ThemesData *tdata)
611 {
612         ThemesPage *theme = tdata->page;
613         gboolean    can_rem, can_use;
614
615         can_use = !IS_CURRENT_THEME(tdata->displayed);
616         can_rem = can_use && !IS_INTERNAL_THEME(tdata->displayed);
617         
618         if (theme->btn_use != NULL)
619                 gtk_widget_set_sensitive(theme->btn_use, can_use);
620         if (theme->btn_remove != NULL)
621                 gtk_widget_set_sensitive(theme->btn_remove, can_rem);
622 }
623
624 /* placeholders may already be utf8 (i18n) */
625 #define SET_LABEL_TEXT_UTF8(label, text)                                \
626 {                                                                       \
627         gchar *tmpstr;                                                  \
628                                                                         \
629         if (!g_utf8_validate(text, -1, NULL))                           \
630                 tmpstr = conv_codeset_strdup(text,                      \
631                         conv_get_locale_charset_str(),  CS_UTF_8);      \
632         else                                                            \
633                 tmpstr = g_strdup(text);                                \
634                                                                         \
635         gtk_label_set_text(GTK_LABEL(label), tmpstr);                   \
636         gtk_label_set_selectable(GTK_LABEL(label), TRUE);               \
637         g_free(tmpstr);                                                 \
638 }
639 static void prefs_themes_display_theme_info(ThemesData *tdata, const ThemeInfo *info)
640 {
641         ThemesPage *theme = tdata->page;
642         gchar *save_prefs_path;
643         gint   i;
644
645         SET_LABEL_TEXT_UTF8(theme->name,        info->name);
646         SET_LABEL_TEXT_UTF8(theme->author,      info->author);
647         SET_LABEL_TEXT_UTF8(theme->url,         info->url);
648         SET_LABEL_TEXT_UTF8(theme->status,      info->status);
649
650         save_prefs_path = prefs_common.pixmap_theme_path;
651         prefs_common.pixmap_theme_path = tdata->displayed;
652         for (i = 0; i < PREVIEW_ICONS; ++i) {
653                 stock_pixbuf_gdk(theme->window, prefs_themes_icons[i], 
654                                 &(theme->pixbufs[i]));
655                 gtk_image_set_from_pixbuf(GTK_IMAGE(theme->icons[i]),
656                                 theme->pixbufs[i]);
657         }
658         prefs_common.pixmap_theme_path = save_prefs_path;
659
660         prefs_themes_update_buttons(tdata);
661 }
662 #undef SET_LABEL_TEXT_UTF8
663
664 static void prefs_themes_display_global_stats(const ThemesData *tdata)
665 {
666         ThemesPage *theme = tdata->page;
667         GList      *tnames = tdata->names;
668         gchar      *gstats;
669         gint        sys = 0;
670         gint        usr = 0;
671         gint        all = 0;
672
673         while (tnames != NULL) {
674                 ThemeName *tname = (ThemeName *)(tnames->data);
675                 gchar     *tpath = (gchar *)(tname->item->data);
676                 
677                 if (IS_SYSTEM_THEME(tpath)) 
678                         ++sys;
679                 else if (!IS_INTERNAL_THEME(tpath)) 
680                         ++usr;
681                 ++all;
682                 tnames = g_list_next(tnames);
683         }
684
685         gstats = g_strdup_printf(_("%d themes available (%d user, %d system, 1 internal)"),
686                                  all, usr, sys);
687         gtk_label_set_text(GTK_LABEL(theme->global), gstats);
688         gtk_label_set_justify (GTK_LABEL (theme->global), GTK_JUSTIFY_LEFT);
689         gtkut_widget_set_small_font_size (theme->global);
690         g_free(gstats);
691 }
692
693 #define INFOFILE_LINE_LEN 80
694
695 #define FGETS_INFOFILE_LINE() \
696         line[0] = '\0'; \
697         fgets(line, INFOFILE_LINE_LEN, finfo); \
698         if ((len = strlen(line)) > 0) { \
699                 if (line[len - 1] == '\n') line[len - 1] = '\0'; \
700         } \
701         else { \
702                 strcpy(line, _("Unknown")); \
703         }
704
705 static void prefs_themes_get_theme_info(ThemesData *tdata)
706 {
707         FILE  *finfo;
708         gchar *sinfo;
709         gchar *path;
710         gchar  line[INFOFILE_LINE_LEN];
711         gint   len;
712         ThemeInfo *info;
713         ThemesPage *theme = tdata->page;
714
715         g_return_if_fail(theme != NULL);
716         path = tdata->displayed;
717         g_return_if_fail(path != NULL);
718
719         debug_print("Getting theme info for %s\n", path);
720         
721         info = g_new0(ThemeInfo, 1);
722         
723         if (IS_INTERNAL_THEME(path)) {
724                 info->name = g_strdup(_("Default internal theme"));
725                 info->author = g_strdup(_("The Claws Mail Team"));
726                 info->url = g_strdup(HOMEPAGE_URI);
727                 info->status = g_strdup_printf(_("Internal theme has %d icons"), N_STOCK_PIXMAPS);
728         }
729         else {
730                 sinfo = g_strconcat(path, G_DIR_SEPARATOR_S, THEMEINFO_FILE, NULL);
731                 finfo = g_fopen(sinfo, "r");
732                 if (finfo == NULL) {
733                         info->name = g_strdup(_("No info file available for this theme"));
734                         info->author = g_strdup(_("Unknown"));
735                         info->url = g_strdup(_("Unknown"));
736                 }
737                 else {
738                         FGETS_INFOFILE_LINE()
739                         info->name = g_strdup(line);
740                         FGETS_INFOFILE_LINE()
741                         info->author = g_strdup(line);
742                         FGETS_INFOFILE_LINE()
743                         info->url = g_strdup(line);
744                 
745                         fclose(finfo);
746                 }
747                 g_free(sinfo);
748
749                 info->status = prefs_themes_get_theme_stats(path);
750                 if (info->status == NULL) {
751                         info->status = g_strdup(_("Error: couldn't get theme status"));
752                 }
753         }
754
755         prefs_themes_display_theme_info(tdata, info);
756
757         g_free(info->name);
758         g_free(info->author);
759         g_free(info->url);
760         g_free(info->status);
761         
762         g_free(info);
763 }
764
765 #undef FGETS_INFOFILE_LINE
766
767 static gchar *prefs_themes_get_theme_stats(const gchar *dirname)
768 {
769         gchar   *stats;
770         DirInfo *dinfo;
771
772         dinfo = g_new0(DirInfo, 1);
773         
774         prefs_themes_foreach_file(dirname, prefs_themes_file_stats, dinfo);
775         stats = g_strdup_printf(_("%d files (%d icons), size: %s"), 
776                                 dinfo->files, dinfo->pixms, to_human_readable((goffset)dinfo->bytes));
777         
778         g_free(dinfo);
779         return stats;
780 }
781
782 /* BEGIN GLADE CODE */
783 /* This is a dummy pixmap we use when a pixmap can't be found. */
784 static char *dummy_pixmap_xpm[] = {
785         /* columns rows colors chars-per-pixel */
786         "1 1 1 1",
787         "  c None",
788         /* pixels */
789         " "
790 };
791
792 /* This is an internally used function to create pixmaps. */
793 static GtkWidget* create_dummy_pixmap(GtkWidget *widget)
794 {
795         GdkColormap *colormap;
796         GdkPixmap *gdkpixmap;
797         GdkBitmap *mask;
798         GtkWidget *pixmap;
799
800         colormap = gtk_widget_get_colormap (widget);
801         gdkpixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL, colormap, &mask,
802                         NULL, dummy_pixmap_xpm);
803         if (gdkpixmap == NULL)
804                 g_error ("Couldn't create replacement pixmap.");
805         pixmap = gtk_image_new_from_pixmap(gdkpixmap, mask);
806         g_object_unref (gdkpixmap);
807         g_object_unref (mask);
808         return pixmap;
809 }
810 /* END GLADE CODE */
811
812 /* glade generates some calls to a create_pixmap support function 
813  * we don't really need. */
814 #define create_pixmap(widget,filename) create_dummy_pixmap(widget)
815
816 static void prefs_themes_create_widget(PrefsPage *page, GtkWindow *window, gpointer data)
817 {
818         ThemesPage *prefs_themes = (ThemesPage *)page;
819         ThemesData *tdata = prefs_themes_data;
820
821         GtkWidget *vbox1;
822         GtkWidget *frame1;
823         GtkWidget *vbox2;
824         GtkWidget *hbox3;
825         GtkWidget *menu_themes;
826         GtkWidget *btn_install;
827         GtkWidget *btn_more;
828         GtkWidget *label_global_status;
829         GtkWidget *frame_info;
830         GtkWidget *table1;
831         GtkWidget *label1;
832         GtkWidget *label2;
833         GtkWidget *label3;
834         GtkWidget *label_name;
835         GtkWidget *label_author;
836         GtkWidget *label_url;
837         GtkWidget *label4;
838         GtkWidget *label_status;
839         GtkWidget *frame_preview;
840         GtkWidget *hbox1;
841         GtkWidget *icon_1;
842         GtkWidget *icon_2;
843         GtkWidget *icon_3;
844         GtkWidget *icon_4;
845         GtkWidget *icon_5;
846         GtkWidget *icon_6;
847         GtkWidget *icon_7;
848         GtkWidget *frame_buttons;
849         GtkWidget *hbuttonbox1;
850         GtkWidget *btn_use;
851         GtkWidget *btn_remove;
852         GtkCellRenderer *renderer;
853
854         vbox1 = gtk_vbox_new (FALSE, VSPACING);
855         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
856         gtk_widget_show (vbox1);
857
858         vbox2 = gtkut_get_options_frame(vbox1, &frame1, _("Selector"));
859
860         hbox3 = gtk_hbox_new (FALSE, 5);
861         gtk_widget_show (hbox3);
862         gtk_box_pack_start (GTK_BOX (vbox2), hbox3, FALSE, FALSE, 0);
863         gtk_container_set_border_width (GTK_CONTAINER (hbox3), 5);
864
865         menu_themes = gtk_combo_box_new();
866         gtk_widget_show (menu_themes);
867         gtk_box_pack_start (GTK_BOX (hbox3), menu_themes, FALSE, FALSE, 0);
868
869         btn_install = gtk_button_new_with_label (_("Install new..."));
870         gtk_widget_show (btn_install);
871         gtk_box_pack_start (GTK_BOX (hbox3), btn_install, FALSE, FALSE, 0);
872         GTK_WIDGET_SET_FLAGS (btn_install, GTK_CAN_DEFAULT);
873
874         btn_more = gtkut_get_link_btn((GtkWidget *)window, THEMES_URI, _("Get more..."));
875         gtk_widget_show (btn_more);
876         gtk_box_pack_start (GTK_BOX (hbox3), btn_more, FALSE, FALSE, 0);
877
878         label_global_status = gtk_label_new ("");
879         gtk_widget_show (label_global_status);
880         gtk_box_pack_start (GTK_BOX (vbox2), label_global_status, FALSE, FALSE, 0);
881         gtk_label_set_justify (GTK_LABEL (label_global_status), GTK_JUSTIFY_LEFT);
882         gtk_misc_set_alignment (GTK_MISC (label_global_status), 0, 0.5);
883         gtk_misc_set_padding (GTK_MISC (label_global_status), 6, 0);
884
885         PACK_FRAME(vbox1, frame_info, _("Information"));
886
887         table1 = gtk_table_new (4, 2, FALSE);
888         gtk_widget_show (table1);
889         gtk_container_add (GTK_CONTAINER (frame_info), table1);
890
891         label1 = gtk_label_new (_("Name: "));
892         gtk_widget_show (label1);
893         gtk_table_attach (GTK_TABLE (table1), label1, 0, 1, 0, 1,
894                         (GtkAttachOptions) (GTK_FILL),
895                         (GtkAttachOptions) (0), 8, 2);
896         gtk_label_set_justify (GTK_LABEL (label1), GTK_JUSTIFY_LEFT);
897         gtk_misc_set_alignment (GTK_MISC (label1), 0, 0.5);
898
899         label2 = gtk_label_new (_("Author: "));
900         gtk_widget_show (label2);
901         gtk_table_attach (GTK_TABLE (table1), label2, 0, 1, 1, 2,
902                         (GtkAttachOptions) (GTK_FILL),
903                         (GtkAttachOptions) (0), 8, 2);
904         gtk_label_set_justify (GTK_LABEL (label2), GTK_JUSTIFY_LEFT);
905         gtk_misc_set_alignment (GTK_MISC (label2), 0, 0.5);
906
907         label3 = gtk_label_new (_("URL:"));
908         gtk_widget_show (label3);
909         gtk_table_attach (GTK_TABLE (table1), label3, 0, 1, 2, 3,
910                         (GtkAttachOptions) (GTK_FILL),
911                         (GtkAttachOptions) (0), 8, 2);
912         gtk_misc_set_alignment (GTK_MISC (label3), 0, 0.5);
913
914         label_name = gtk_label_new ("");
915         gtk_widget_show (label_name);
916         gtk_table_attach (GTK_TABLE (table1), label_name, 1, 2, 0, 1,
917                         (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
918                         (GtkAttachOptions) (0), 0, 0);
919         gtk_misc_set_alignment (GTK_MISC (label_name), 0, 0.5);
920
921         label_author = gtk_label_new ("");
922         gtk_widget_show (label_author);
923         gtk_table_attach (GTK_TABLE (table1), label_author, 1, 2, 1, 2,
924                         (GtkAttachOptions) (GTK_FILL),
925                         (GtkAttachOptions) (0), 0, 0);
926         gtk_misc_set_alignment (GTK_MISC (label_author), 0, 0.5);
927
928         label_url = gtk_label_new ("");
929         gtk_widget_show (label_url);
930         gtk_table_attach (GTK_TABLE (table1), label_url, 1, 2, 2, 3,
931                         (GtkAttachOptions) (GTK_FILL),
932                         (GtkAttachOptions) (0), 0, 0);
933         gtk_misc_set_alignment (GTK_MISC (label_url), 0, 0.5);
934
935         label4 = gtk_label_new (_("Status:"));
936         gtk_widget_show (label4);
937         gtk_table_attach (GTK_TABLE (table1), label4, 0, 1, 3, 4,
938                         (GtkAttachOptions) (GTK_FILL),
939                         (GtkAttachOptions) (0), 8, 2);
940         gtk_misc_set_alignment (GTK_MISC (label4), 0, 0.5);
941
942         label_status = gtk_label_new ("");
943         gtk_widget_show (label_status);
944         gtk_table_attach (GTK_TABLE (table1), label_status, 1, 2, 3, 4,
945                         (GtkAttachOptions) (GTK_FILL),
946                         (GtkAttachOptions) (0), 0, 0);
947         gtk_misc_set_alignment (GTK_MISC (label_status), 0, 0.5);
948
949         PACK_FRAME(vbox1, frame_preview, _("Preview"));
950
951         hbox1 = gtk_hbox_new (FALSE, 0);
952         gtk_widget_show (hbox1);
953         gtk_container_add (GTK_CONTAINER (frame_preview), hbox1);
954
955         icon_1 = create_pixmap (vbox1, NULL);
956         gtk_widget_show (icon_1);
957         gtk_box_pack_start (GTK_BOX (hbox1), icon_1, TRUE, TRUE, 2);
958         gtk_misc_set_padding (GTK_MISC (icon_1), 0, 5);
959
960         icon_2 = create_pixmap (vbox1, NULL);
961         gtk_widget_show (icon_2);
962         gtk_box_pack_start (GTK_BOX (hbox1), icon_2, TRUE, TRUE, 2);
963         gtk_misc_set_padding (GTK_MISC (icon_2), 0, 5);
964
965         icon_3 = create_pixmap (vbox1, NULL);
966         gtk_widget_show (icon_3);
967         gtk_box_pack_start (GTK_BOX (hbox1), icon_3, TRUE, TRUE, 2);
968         gtk_misc_set_padding (GTK_MISC (icon_3), 0, 5);
969
970         icon_4 = create_pixmap (vbox1, NULL);
971         gtk_widget_show (icon_4);
972         gtk_box_pack_start (GTK_BOX (hbox1), icon_4, TRUE, TRUE, 2);
973         gtk_misc_set_padding (GTK_MISC (icon_4), 0, 5);
974
975         icon_5 = create_pixmap (vbox1, NULL);
976         gtk_widget_show (icon_5);
977         gtk_box_pack_start (GTK_BOX (hbox1), icon_5, TRUE, TRUE, 2);
978         gtk_misc_set_padding (GTK_MISC (icon_5), 0, 5);
979
980         icon_6 = create_pixmap (vbox1, NULL);
981         gtk_widget_show (icon_6);
982         gtk_box_pack_start (GTK_BOX (hbox1), icon_6, TRUE, TRUE, 0);
983         gtk_misc_set_padding (GTK_MISC (icon_6), 0, 5);
984
985         icon_7 = create_pixmap (vbox1, NULL);
986         gtk_widget_show (icon_7);
987         gtk_box_pack_start (GTK_BOX (hbox1), icon_7, TRUE, TRUE, 0);
988         gtk_misc_set_padding (GTK_MISC (icon_7), 0, 5);
989
990         PACK_FRAME(vbox1, frame_buttons, _("Actions"));
991
992         hbuttonbox1 = gtk_hbutton_box_new ();
993         gtk_widget_show (hbuttonbox1);
994         gtk_container_add (GTK_CONTAINER (frame_buttons), hbuttonbox1);
995         gtk_container_set_border_width (GTK_CONTAINER (hbuttonbox1), 5);
996         gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox1), GTK_BUTTONBOX_START);
997         gtk_box_set_spacing (GTK_BOX (hbuttonbox1), 5);
998
999         btn_use = gtk_button_new_with_label (_("Use this"));
1000         gtk_widget_show (btn_use);
1001         gtk_container_add (GTK_CONTAINER (hbuttonbox1), btn_use);
1002         GTK_WIDGET_SET_FLAGS (btn_use, GTK_CAN_DEFAULT);
1003
1004         btn_remove = gtk_button_new_with_label (_("Remove"));
1005         gtk_widget_show (btn_remove);
1006         gtk_container_add (GTK_CONTAINER (hbuttonbox1), btn_remove);
1007         GTK_WIDGET_SET_FLAGS (btn_remove, GTK_CAN_DEFAULT);
1008
1009         g_signal_connect(G_OBJECT(btn_use), "clicked",
1010                          G_CALLBACK(prefs_themes_btn_use_clicked_cb),
1011                          NULL);
1012         g_signal_connect(G_OBJECT(btn_remove), "clicked",
1013                          G_CALLBACK(prefs_themes_btn_remove_clicked_cb),
1014                          NULL);
1015         g_signal_connect(G_OBJECT(btn_install), "clicked",
1016                          G_CALLBACK(prefs_themes_btn_install_clicked_cb),
1017                          NULL);
1018
1019         prefs_themes->window = GTK_WIDGET(window);
1020         
1021         prefs_themes->name   = label_name;
1022         prefs_themes->author = label_author;
1023         prefs_themes->url    = label_url;
1024         prefs_themes->status = label_status;
1025         prefs_themes->global = label_global_status;
1026
1027         prefs_themes->icons[0] = icon_1;
1028         prefs_themes->icons[1] = icon_2;
1029         prefs_themes->icons[2] = icon_3;
1030         prefs_themes->icons[3] = icon_4;
1031         prefs_themes->icons[4] = icon_5;
1032         prefs_themes->icons[5] = icon_6;
1033         prefs_themes->icons[6] = icon_7;
1034         
1035         prefs_themes->btn_use     = btn_use;
1036         prefs_themes->btn_remove  = btn_remove;
1037         prefs_themes->btn_install = btn_install;
1038         prefs_themes->btn_more    = btn_more;
1039
1040         prefs_themes->op_menu     = menu_themes;
1041
1042         prefs_themes->page.widget = vbox1;
1043         
1044         prefs_themes_set_themes_menu(GTK_COMBO_BOX(menu_themes), tdata);
1045         renderer = gtk_cell_renderer_text_new();
1046         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(menu_themes), renderer, TRUE);
1047         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(menu_themes), renderer,
1048                                         "text", 0, NULL);
1049         
1050         prefs_themes_get_theme_info(tdata);
1051         prefs_themes_display_global_stats(tdata);
1052 }
1053
1054 static void prefs_themes_destroy_widget(PrefsPage *page)
1055 {
1056         /* ThemesPage *theme = (ThemesPage *)page; */
1057 }
1058
1059 static void prefs_themes_save(PrefsPage *page)
1060 {
1061         /* ThemesPage *theme = (ThemesPage *)page; */
1062 }
1063