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