2007-01-03 [paul] 2.6.1cvs96
[claws.git] / src / prefs_themes.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2003-2007 Hiroyuki Yamamoto & the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 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_menu_item_activated_cb (GtkWidget *widget, gpointer data);
135
136 static void prefs_themes_update_buttons         (const ThemesData *tdata);
137 static void prefs_themes_display_global_stats   (const ThemesData *tdata);
138 static void prefs_themes_get_theme_info         (ThemesData *tdata);
139 static void prefs_themes_display_theme_info     (ThemesData *tdata, const ThemeInfo *info);
140 static void prefs_themes_get_themes_and_names   (ThemesData *tdata);
141 static int prefs_themes_cmp_name(gconstpointer a, gconstpointer b);
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 (strcmp(base, ".") != 0 && strcmp(base, "..") != 0)
189                         g_warning("prefs_themes_file_remove(): subdir in theme dir skipped: '%s'.\n",
190                                                 base);
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 (strcmp(base, ".") != 0 && strcmp(base, "..") !=0 )
209                         g_warning("prefs_themes_file_install(): subdir in theme dir skipped: '%s'.\n",
210                                                 base);
211         }
212         else {
213                 gchar *fulldest;
214                 
215                 fulldest = g_strconcat(ci->dest, G_DIR_SEPARATOR_S, base, NULL);
216                 
217                 if (0 != copy_file(filename, fulldest, FALSE)) {
218                         ci->status = g_strdup(filename);
219                 }
220                 g_free(fulldest);
221         }
222         g_free(base);
223 }
224
225 static void prefs_themes_foreach_file(const gchar *dirname, const FileFunc func, gpointer data)
226 {
227         struct dirent *d;
228         DIR           *dp;
229
230         g_return_if_fail(dirname != NULL);
231         g_return_if_fail(func != NULL);
232         
233         if ((dp = opendir(dirname)) == NULL) {
234                 debug_print("directory %s not found", dirname);
235                 return;
236         }
237
238         while ((d = readdir(dp)) != NULL) {
239                 gchar *entry;
240                 gchar *fullentry;
241
242                 entry     = d->d_name;
243                 fullentry = g_strconcat(dirname, G_DIR_SEPARATOR_S, entry, NULL);
244
245                 (*func)(fullentry, data);
246                 
247                 g_free(fullentry);
248         }       
249 }
250
251 static gboolean prefs_themes_is_system_theme(const gchar *dirname)
252 {
253         gint len;
254         
255         g_return_val_if_fail(dirname != NULL, FALSE);
256
257         len = strlen(PACKAGE_DATA_DIR);
258         if (strlen(dirname) > len && 0 == strncmp(dirname, PACKAGE_DATA_DIR, len))
259                 return TRUE;
260         
261         return FALSE;
262 }
263
264 static void prefs_themes_set_themes_menu(GtkOptionMenu *opmenu, const ThemesData *tdata)
265 {
266         GList     *themes = tdata->names;
267         GtkWidget *menu;
268         GtkWidget *item;
269         gint       i = 0, active = 0;
270         GList     *sorted_list = NULL;
271
272         g_return_if_fail(opmenu != NULL);
273
274         gtk_option_menu_remove_menu(opmenu);
275
276         /* sort theme data list by data name */
277         menu = gtk_menu_new ();
278         while (themes != NULL) {
279                 ThemeName *tname = (ThemeName *)(themes->data);
280
281                 sorted_list = g_list_insert_sorted(sorted_list, (gpointer)(tname),
282                                                                                 (GCompareFunc)prefs_themes_cmp_name);
283
284                 themes = g_list_next(themes);
285         }
286
287         /* feed gtk_menu w/ sorted themes names */
288         themes = sorted_list;
289         while (themes != NULL) {
290                 ThemeName *tname = (ThemeName *)(themes->data);
291                 gchar     *tpath = (gchar *)(tname->item->data);
292
293                 item = gtk_menu_item_new_with_label(tname->name);
294                 gtk_widget_show(item);
295                 g_signal_connect(G_OBJECT(item), "activate",
296                                  G_CALLBACK(prefs_themes_menu_item_activated_cb),
297                                  tname->item->data);
298                 gtk_menu_append(GTK_MENU(menu), item);
299
300                 if (tdata->displayed != NULL && tdata->displayed == tpath)
301                         active = i;
302                 ++i;
303
304                 themes = g_list_next(themes);
305         }
306
307         g_list_free(sorted_list);
308
309         gtk_menu_set_active(GTK_MENU(menu), active);
310         gtk_option_menu_set_menu (opmenu, menu);
311 }
312
313 static int prefs_themes_cmp_name(gconstpointer a_p, gconstpointer b_p)
314 {
315         /* compare two ThemeData structures by their name attribute */
316         return strcmp2((gchar *)(((ThemeName*)a_p)->name),
317                                         (gchar *)(((ThemeName*)b_p)->name));
318 }
319
320 static void prefs_themes_get_themes_and_names(ThemesData *tdata)
321 {
322         GList *tpaths;
323         
324         g_return_if_fail(tdata != NULL);
325         
326         if (tdata->themes != NULL)
327                 stock_pixmap_themes_list_free(tdata->themes);
328         if (tdata->names != NULL)
329                 prefs_themes_free_names(tdata);
330         
331         tdata->themes = stock_pixmap_themes_list_new();
332         
333         tpaths = tdata->themes;
334         while (tpaths != NULL) {
335                 ThemeName *name = g_new0(ThemeName, 1);
336                 gchar *sname = g_path_get_basename((const gchar *)(tpaths->data));
337                 
338                 if (IS_INTERNAL_THEME(sname))
339                         name->name = g_strdup(_("Default internal theme"));
340                 else
341                         name->name = g_strdup(sname);
342                 name->item = tpaths;
343                         
344                 tdata->names = g_list_append(tdata->names, name);
345                 if (!strcmp2(tpaths->data, prefs_common.pixmap_theme_path)) {
346                         tdata->displayed = (gchar *)tpaths->data;
347                 }
348                 tpaths = g_list_next(tpaths);
349                 g_free(sname);  
350         }
351 }
352
353 void prefs_themes_init(void)
354 {
355         ThemesData   *tdata;
356         ThemesPage   *page;
357         GList        *tpaths;
358         static gchar *path[3];
359
360         path[0] = _("Display");
361         path[1] = _("Themes");
362         path[2] = NULL;
363
364         debug_print("Creating preferences for themes...\n");
365         
366         tdata = g_new0(ThemesData, 1);
367         prefs_themes_data = tdata;
368
369         prefs_themes_get_themes_and_names(tdata);
370         
371         page = g_new0(ThemesPage, 1);
372         
373         page->page.path = path;
374         page->page.create_widget = prefs_themes_create_widget;
375         page->page.destroy_widget = prefs_themes_destroy_widget;
376         page->page.save_page = prefs_themes_save;
377         page->page.weight = 130.0;
378         prefs_gtk_register_page((PrefsPage *) page);
379
380         tdata->page = page;
381
382         tpaths = g_list_first(tdata->themes);
383         if (tdata->displayed == NULL)
384                 tdata->displayed = (gchar *)(tpaths->data);
385 }
386
387 static void prefs_themes_free_names(ThemesData *tdata)
388 {
389         GList *names;
390         
391         names = tdata->names;
392         while (names != NULL) {
393                 ThemeName *tn = (ThemeName *)(names->data);
394                 
395                 tn->item = NULL;
396                 g_free(tn->name);
397                 g_free(tn);
398                 
399                 names = g_list_next(names);
400         }
401         g_list_free(names);
402         tdata->names = NULL;
403 }
404
405 void prefs_themes_done(void)
406 {
407         ThemesData *tdata = prefs_themes_data;
408
409         debug_print("Finished preferences for themes.\n");
410         
411         stock_pixmap_themes_list_free(tdata->themes);
412         prefs_themes_free_names(tdata); 
413         g_free(tdata->page);
414         g_free(tdata);
415 }
416
417 static void prefs_themes_btn_use_clicked_cb(GtkWidget *widget, gpointer data)
418 {
419         ThemesData *tdata = prefs_themes_data;
420         gchar      *theme_str;
421
422         theme_str = tdata->displayed;
423         
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_NO, GTK_STOCK_YES, NULL);
462         g_free(alert_title);
463
464         if (G_ALERTALTERNATE == 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_NO, GTK_STOCK_YES, NULL);
517                 if (G_ALERTALTERNATE != 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_NO, GTK_STOCK_YES, NULL);
524                 switch (val) {
525                 case G_ALERTALTERNATE:
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_ALERTDEFAULT:
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         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_menu_item_activated_cb(GtkWidget *widget, gpointer data)
581 {
582         ThemesData *tdata = prefs_themes_data;
583         gchar      *path = (gchar *)data;
584         
585         g_return_if_fail(path != NULL);
586         
587         tdata->displayed = path;
588         prefs_themes_get_theme_info(tdata);
589 }
590
591 static void prefs_themes_update_buttons(const ThemesData *tdata)
592 {
593         ThemesPage *theme = tdata->page;
594         gboolean    can_rem, can_use;
595
596         can_use = !IS_CURRENT_THEME(tdata->displayed);
597         can_rem = can_use && !IS_INTERNAL_THEME(tdata->displayed);
598         
599         if (theme->btn_use != NULL)
600                 gtk_widget_set_sensitive(theme->btn_use, can_use);
601         if (theme->btn_remove != NULL)
602                 gtk_widget_set_sensitive(theme->btn_remove, can_rem);
603 }
604
605 /* placeholders may already be utf8 (i18n) */
606 #define SET_LABEL_TEXT_UTF8(label, text)                                \
607 {                                                                       \
608         gchar *tmpstr;                                                  \
609                                                                         \
610         if (!g_utf8_validate(text, -1, NULL))                           \
611                 tmpstr = conv_codeset_strdup(text,                      \
612                         conv_get_locale_charset_str(),  CS_UTF_8);      \
613         else                                                            \
614                 tmpstr = g_strdup(text);                                \
615                                                                         \
616         gtk_label_set_text(GTK_LABEL(label), tmpstr);                   \
617         gtk_label_set_selectable(GTK_LABEL(label), TRUE);               \
618         g_free(tmpstr);                                                 \
619 }
620 static void prefs_themes_display_theme_info(ThemesData *tdata, const ThemeInfo *info)
621 {
622         ThemesPage *theme = tdata->page;
623         gchar *save_prefs_path;
624         gint   i;
625
626         SET_LABEL_TEXT_UTF8(theme->name,        info->name);
627         SET_LABEL_TEXT_UTF8(theme->author,      info->author);
628         SET_LABEL_TEXT_UTF8(theme->url,         info->url);
629         SET_LABEL_TEXT_UTF8(theme->status,      info->status);
630
631         save_prefs_path = prefs_common.pixmap_theme_path;
632         prefs_common.pixmap_theme_path = tdata->displayed;
633         for (i = 0; i < PREVIEW_ICONS; ++i) {
634                 stock_pixmap_gdk(theme->window, prefs_themes_icons[i], 
635                                 &(theme->pixmaps[i]), &(theme->masks[i]));
636                 gtk_image_set_from_pixmap(GTK_IMAGE(theme->icons[i]),
637                                 theme->pixmaps[i], theme->masks[i]);
638         }
639         prefs_common.pixmap_theme_path = save_prefs_path;
640
641         prefs_themes_update_buttons(tdata);
642 }
643 #undef SET_LABEL_TEXT_UTF8
644
645 static void prefs_themes_display_global_stats(const ThemesData *tdata)
646 {
647         ThemesPage *theme = tdata->page;
648         GList      *tnames = tdata->names;
649         gchar      *gstats;
650         gint        sys = 0;
651         gint        usr = 0;
652         gint        all = 0;
653
654         while (tnames != NULL) {
655                 ThemeName *tname = (ThemeName *)(tnames->data);
656                 gchar     *tpath = (gchar *)(tname->item->data);
657                 
658                 if (IS_SYSTEM_THEME(tpath)) 
659                         ++sys;
660                 else if (!IS_INTERNAL_THEME(tpath)) 
661                         ++usr;
662                 ++all;
663                 tnames = g_list_next(tnames);
664         }
665
666         gstats = g_strdup_printf(_("%d themes available (%d user, %d system, 1 internal)"),
667                                  all, usr, sys);
668         gtk_label_set_text(GTK_LABEL(theme->global), gstats);
669         gtk_label_set_justify (GTK_LABEL (theme->global), GTK_JUSTIFY_LEFT);
670         gtkut_widget_set_small_font_size (theme->global);
671         g_free(gstats);
672 }
673
674 #define INFOFILE_LINE_LEN 80
675
676 #define FGETS_INFOFILE_LINE() \
677         line[0] = '\0'; \
678         fgets(line, INFOFILE_LINE_LEN, finfo); \
679         if ((len = strlen(line)) > 0) { \
680                 if (line[len - 1] == '\n') line[len - 1] = '\0'; \
681         } \
682         else { \
683                 strcpy(line, _("Unknown")); \
684         }
685
686 static void prefs_themes_get_theme_info(ThemesData *tdata)
687 {
688         FILE  *finfo;
689         gchar *sinfo;
690         gchar *path;
691         gchar  line[INFOFILE_LINE_LEN];
692         gint   len;
693         ThemeInfo *info;
694         ThemesPage *theme = tdata->page;
695
696         g_return_if_fail(theme != NULL);
697         path = tdata->displayed;
698         g_return_if_fail(path != NULL);
699
700         debug_print("Getting theme info for %s\n", path);
701         
702         info = g_new0(ThemeInfo, 1);
703         
704         if (IS_INTERNAL_THEME(path)) {
705                 info->name = g_strdup(_("Default internal theme"));
706                 info->author = g_strdup(_("The Claws Mail Team"));
707                 info->url = g_strdup(HOMEPAGE_URI);
708                 info->status = g_strdup_printf(_("Internal theme has %d icons"), N_STOCK_PIXMAPS);
709         }
710         else {
711                 sinfo = g_strconcat(path, G_DIR_SEPARATOR_S, THEMEINFO_FILE, NULL);
712                 finfo = g_fopen(sinfo, "r");
713                 if (finfo == NULL) {
714                         info->name = g_strdup(_("No info file available for this theme"));
715                         info->author = g_strdup(_("Unknown"));
716                         info->url = g_strdup(_("Unknown"));
717                 }
718                 else {
719                         FGETS_INFOFILE_LINE()
720                         info->name = g_strdup(line);
721                         FGETS_INFOFILE_LINE()
722                         info->author = g_strdup(line);
723                         FGETS_INFOFILE_LINE()
724                         info->url = g_strdup(line);
725                 
726                         fclose(finfo);
727                 }
728                 g_free(sinfo);
729
730                 info->status = prefs_themes_get_theme_stats(path);
731                 if (info->status == NULL) {
732                         info->status = g_strdup(_("Error: couldn't get theme status"));
733                 }
734         }
735
736         prefs_themes_display_theme_info(tdata, info);
737
738         g_free(info->name);
739         g_free(info->author);
740         g_free(info->url);
741         g_free(info->status);
742         
743         g_free(info);
744 }
745
746 #undef FGETS_INFOFILE_LINE
747
748 static gchar *prefs_themes_get_theme_stats(const gchar *dirname)
749 {
750         gchar   *stats;
751         DirInfo *dinfo;
752
753         dinfo = g_new0(DirInfo, 1);
754         
755         prefs_themes_foreach_file(dirname, prefs_themes_file_stats, dinfo);
756         stats = g_strdup_printf(_("%d files (%d icons), size: %s"), 
757                                 dinfo->files, dinfo->pixms, to_human_readable(dinfo->bytes));
758         
759         g_free(dinfo);
760         return stats;
761 }
762
763 /* BEGIN GLADE CODE */
764 /* This is a dummy pixmap we use when a pixmap can't be found. */
765 static char *dummy_pixmap_xpm[] = {
766         /* columns rows colors chars-per-pixel */
767         "1 1 1 1",
768         "  c None",
769         /* pixels */
770         " "
771 };
772
773 /* This is an internally used function to create pixmaps. */
774 static GtkWidget* create_dummy_pixmap(GtkWidget *widget)
775 {
776         GdkColormap *colormap;
777         GdkPixmap *gdkpixmap;
778         GdkBitmap *mask;
779         GtkWidget *pixmap;
780
781         colormap = gtk_widget_get_colormap (widget);
782         gdkpixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL, colormap, &mask,
783                         NULL, dummy_pixmap_xpm);
784         if (gdkpixmap == NULL)
785                 g_error ("Couldn't create replacement pixmap.");
786         pixmap = gtk_image_new_from_pixmap(gdkpixmap, mask);
787         g_object_unref (gdkpixmap);
788         g_object_unref (mask);
789         return pixmap;
790 }
791 /* END GLADE CODE */
792
793 /* glade generates some calls to a create_pixmap support function 
794  * we don't really need. */
795 #define create_pixmap(widget,filename) create_dummy_pixmap(widget)
796
797 static void prefs_themes_create_widget(PrefsPage *page, GtkWindow *window, gpointer data)
798 {
799         ThemesPage *prefs_themes = (ThemesPage *)page;
800         ThemesData *tdata = prefs_themes_data;
801
802         GtkWidget *vbox1;
803         GtkWidget *frame1;
804         GtkWidget *vbox2;
805         GtkWidget *hbox3;
806         GtkWidget *menu_themes;
807         GtkWidget *menu_themes_menu;
808         GtkWidget *glade_menuitem;
809         GtkWidget *btn_install;
810         GtkWidget *btn_more;
811         GtkWidget *label_global_status;
812         GtkWidget *frame_info;
813         GtkWidget *table1;
814         GtkWidget *label1;
815         GtkWidget *label2;
816         GtkWidget *label3;
817         GtkWidget *label_name;
818         GtkWidget *label_author;
819         GtkWidget *label_url;
820         GtkWidget *label4;
821         GtkWidget *label_status;
822         GtkWidget *frame_preview;
823         GtkWidget *hbox1;
824         GtkWidget *icon_1;
825         GtkWidget *icon_2;
826         GtkWidget *icon_3;
827         GtkWidget *icon_4;
828         GtkWidget *icon_5;
829         GtkWidget *icon_6;
830         GtkWidget *icon_7;
831         GtkWidget *frame_buttons;
832         GtkWidget *hbuttonbox1;
833         GtkWidget *btn_use;
834         GtkWidget *btn_remove;
835
836         vbox1 = gtk_vbox_new (FALSE, VSPACING);
837         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
838         gtk_widget_show (vbox1);
839
840         PACK_FRAME (vbox1, frame1, _("Selector"));
841
842         vbox2 = gtk_vbox_new (FALSE, VSPACING);
843         gtk_container_set_border_width (GTK_CONTAINER (vbox2), VBOX_BORDER);
844         gtk_widget_show (vbox2);
845         gtk_container_add (GTK_CONTAINER (frame1), vbox2);
846
847         hbox3 = gtk_hbox_new (FALSE, 5);
848         gtk_widget_show (hbox3);
849         gtk_box_pack_start (GTK_BOX (vbox2), hbox3, FALSE, FALSE, 0);
850         gtk_container_set_border_width (GTK_CONTAINER (hbox3), 5);
851
852         menu_themes = gtk_option_menu_new ();
853         gtk_widget_show (menu_themes);
854         gtk_box_pack_start (GTK_BOX (hbox3), menu_themes, FALSE, FALSE, 0);
855         menu_themes_menu = gtk_menu_new ();
856         glade_menuitem = gtk_menu_item_new_with_label ("");
857         gtk_widget_show (glade_menuitem);
858         gtk_menu_append (GTK_MENU (menu_themes_menu), glade_menuitem);
859         gtk_option_menu_set_menu (GTK_OPTION_MENU (menu_themes), menu_themes_menu);
860
861         btn_install = gtk_button_new_with_label (_("Install new..."));
862         gtk_widget_show (btn_install);
863         gtk_box_pack_start (GTK_BOX (hbox3), btn_install, FALSE, FALSE, 0);
864         GTK_WIDGET_SET_FLAGS (btn_install, GTK_CAN_DEFAULT);
865
866         btn_more = gtkut_get_link_btn((GtkWidget *)window, THEMES_URI, _("Get more..."));
867         gtk_widget_show (btn_more);
868         gtk_box_pack_start (GTK_BOX (hbox3), btn_more, FALSE, FALSE, 0);
869
870         label_global_status = gtk_label_new ("");
871         gtk_widget_show (label_global_status);
872         gtk_box_pack_start (GTK_BOX (vbox2), label_global_status, FALSE, FALSE, 0);
873         gtk_label_set_justify (GTK_LABEL (label_global_status), GTK_JUSTIFY_LEFT);
874         gtk_misc_set_alignment (GTK_MISC (label_global_status), 0, 0.5);
875         gtk_misc_set_padding (GTK_MISC (label_global_status), 6, 0);
876
877         PACK_FRAME(vbox1, frame_info, _("Information"));
878
879         table1 = gtk_table_new (4, 2, FALSE);
880         gtk_widget_show (table1);
881         gtk_container_add (GTK_CONTAINER (frame_info), table1);
882
883         label1 = gtk_label_new (_("Name: "));
884         gtk_widget_show (label1);
885         gtk_table_attach (GTK_TABLE (table1), label1, 0, 1, 0, 1,
886                         (GtkAttachOptions) (GTK_FILL),
887                         (GtkAttachOptions) (0), 8, 2);
888         gtk_label_set_justify (GTK_LABEL (label1), GTK_JUSTIFY_LEFT);
889         gtk_misc_set_alignment (GTK_MISC (label1), 0, 0.5);
890
891         label2 = gtk_label_new (_("Author: "));
892         gtk_widget_show (label2);
893         gtk_table_attach (GTK_TABLE (table1), label2, 0, 1, 1, 2,
894                         (GtkAttachOptions) (GTK_FILL),
895                         (GtkAttachOptions) (0), 8, 2);
896         gtk_label_set_justify (GTK_LABEL (label2), GTK_JUSTIFY_LEFT);
897         gtk_misc_set_alignment (GTK_MISC (label2), 0, 0.5);
898
899         label3 = gtk_label_new (_("URL:"));
900         gtk_widget_show (label3);
901         gtk_table_attach (GTK_TABLE (table1), label3, 0, 1, 2, 3,
902                         (GtkAttachOptions) (GTK_FILL),
903                         (GtkAttachOptions) (0), 8, 2);
904         gtk_misc_set_alignment (GTK_MISC (label3), 0, 0.5);
905
906         label_name = gtk_label_new ("");
907         gtk_widget_show (label_name);
908         gtk_table_attach (GTK_TABLE (table1), label_name, 1, 2, 0, 1,
909                         (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
910                         (GtkAttachOptions) (0), 0, 0);
911         gtk_misc_set_alignment (GTK_MISC (label_name), 0, 0.5);
912
913         label_author = gtk_label_new ("");
914         gtk_widget_show (label_author);
915         gtk_table_attach (GTK_TABLE (table1), label_author, 1, 2, 1, 2,
916                         (GtkAttachOptions) (GTK_FILL),
917                         (GtkAttachOptions) (0), 0, 0);
918         gtk_misc_set_alignment (GTK_MISC (label_author), 0, 0.5);
919
920         label_url = gtk_label_new ("");
921         gtk_widget_show (label_url);
922         gtk_table_attach (GTK_TABLE (table1), label_url, 1, 2, 2, 3,
923                         (GtkAttachOptions) (GTK_FILL),
924                         (GtkAttachOptions) (0), 0, 0);
925         gtk_misc_set_alignment (GTK_MISC (label_url), 0, 0.5);
926
927         label4 = gtk_label_new (_("Status:"));
928         gtk_widget_show (label4);
929         gtk_table_attach (GTK_TABLE (table1), label4, 0, 1, 3, 4,
930                         (GtkAttachOptions) (GTK_FILL),
931                         (GtkAttachOptions) (0), 8, 2);
932         gtk_misc_set_alignment (GTK_MISC (label4), 0, 0.5);
933
934         label_status = gtk_label_new ("");
935         gtk_widget_show (label_status);
936         gtk_table_attach (GTK_TABLE (table1), label_status, 1, 2, 3, 4,
937                         (GtkAttachOptions) (GTK_FILL),
938                         (GtkAttachOptions) (0), 0, 0);
939         gtk_misc_set_alignment (GTK_MISC (label_status), 0, 0.5);
940
941         PACK_FRAME(vbox1, frame_preview, _("Preview"));
942
943         hbox1 = gtk_hbox_new (FALSE, 0);
944         gtk_widget_show (hbox1);
945         gtk_container_add (GTK_CONTAINER (frame_preview), hbox1);
946
947         icon_1 = create_pixmap (vbox1, NULL);
948         gtk_widget_show (icon_1);
949         gtk_box_pack_start (GTK_BOX (hbox1), icon_1, TRUE, TRUE, 2);
950         gtk_misc_set_padding (GTK_MISC (icon_1), 0, 5);
951
952         icon_2 = create_pixmap (vbox1, NULL);
953         gtk_widget_show (icon_2);
954         gtk_box_pack_start (GTK_BOX (hbox1), icon_2, TRUE, TRUE, 2);
955         gtk_misc_set_padding (GTK_MISC (icon_2), 0, 5);
956
957         icon_3 = create_pixmap (vbox1, NULL);
958         gtk_widget_show (icon_3);
959         gtk_box_pack_start (GTK_BOX (hbox1), icon_3, TRUE, TRUE, 2);
960         gtk_misc_set_padding (GTK_MISC (icon_3), 0, 5);
961
962         icon_4 = create_pixmap (vbox1, NULL);
963         gtk_widget_show (icon_4);
964         gtk_box_pack_start (GTK_BOX (hbox1), icon_4, TRUE, TRUE, 2);
965         gtk_misc_set_padding (GTK_MISC (icon_4), 0, 5);
966
967         icon_5 = create_pixmap (vbox1, NULL);
968         gtk_widget_show (icon_5);
969         gtk_box_pack_start (GTK_BOX (hbox1), icon_5, TRUE, TRUE, 2);
970         gtk_misc_set_padding (GTK_MISC (icon_5), 0, 5);
971
972         icon_6 = create_pixmap (vbox1, NULL);
973         gtk_widget_show (icon_6);
974         gtk_box_pack_start (GTK_BOX (hbox1), icon_6, TRUE, TRUE, 0);
975         gtk_misc_set_padding (GTK_MISC (icon_6), 0, 5);
976
977         icon_7 = create_pixmap (vbox1, NULL);
978         gtk_widget_show (icon_7);
979         gtk_box_pack_start (GTK_BOX (hbox1), icon_7, TRUE, TRUE, 0);
980         gtk_misc_set_padding (GTK_MISC (icon_7), 0, 5);
981
982         PACK_FRAME(vbox1, frame_buttons, _("Actions"));
983
984         hbuttonbox1 = gtk_hbutton_box_new ();
985         gtk_widget_show (hbuttonbox1);
986         gtk_container_add (GTK_CONTAINER (frame_buttons), hbuttonbox1);
987         gtk_container_set_border_width (GTK_CONTAINER (hbuttonbox1), 5);
988         gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox1), GTK_BUTTONBOX_START);
989         gtk_box_set_spacing (GTK_BOX (hbuttonbox1), 5);
990
991         btn_use = gtk_button_new_with_label (_("Use this"));
992         gtk_widget_show (btn_use);
993         gtk_container_add (GTK_CONTAINER (hbuttonbox1), btn_use);
994         GTK_WIDGET_SET_FLAGS (btn_use, GTK_CAN_DEFAULT);
995
996         btn_remove = gtk_button_new_with_label (_("Remove"));
997         gtk_widget_show (btn_remove);
998         gtk_container_add (GTK_CONTAINER (hbuttonbox1), btn_remove);
999         GTK_WIDGET_SET_FLAGS (btn_remove, GTK_CAN_DEFAULT);
1000
1001         g_signal_connect(G_OBJECT(btn_use), "clicked",
1002                          G_CALLBACK(prefs_themes_btn_use_clicked_cb),
1003                          NULL);
1004         g_signal_connect(G_OBJECT(btn_remove), "clicked",
1005                          G_CALLBACK(prefs_themes_btn_remove_clicked_cb),
1006                          NULL);
1007         g_signal_connect(G_OBJECT(btn_install), "clicked",
1008                          G_CALLBACK(prefs_themes_btn_install_clicked_cb),
1009                          NULL);
1010
1011         prefs_themes->window = GTK_WIDGET(window);
1012         
1013         prefs_themes->name   = label_name;
1014         prefs_themes->author = label_author;
1015         prefs_themes->url    = label_url;
1016         prefs_themes->status = label_status;
1017         prefs_themes->global = label_global_status;
1018
1019         prefs_themes->icons[0] = icon_1;
1020         prefs_themes->icons[1] = icon_2;
1021         prefs_themes->icons[2] = icon_3;
1022         prefs_themes->icons[3] = icon_4;
1023         prefs_themes->icons[4] = icon_5;
1024         prefs_themes->icons[5] = icon_6;
1025         prefs_themes->icons[6] = icon_7;
1026         
1027         prefs_themes->btn_use     = btn_use;
1028         prefs_themes->btn_remove  = btn_remove;
1029         prefs_themes->btn_install = btn_install;
1030         prefs_themes->btn_more    = btn_more;
1031
1032         prefs_themes->op_menu     = menu_themes;
1033
1034         prefs_themes->page.widget = vbox1;
1035
1036         prefs_themes_set_themes_menu(GTK_OPTION_MENU(prefs_themes->op_menu), tdata);
1037         
1038         prefs_themes_get_theme_info(tdata);
1039         prefs_themes_display_global_stats(tdata);
1040 }
1041
1042 static void prefs_themes_destroy_widget(PrefsPage *page)
1043 {
1044         /* ThemesPage *theme = (ThemesPage *)page; */
1045 }
1046
1047 static void prefs_themes_save(PrefsPage *page)
1048 {
1049         /* ThemesPage *theme = (ThemesPage *)page; */
1050 }
1051