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