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