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