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