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