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