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