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