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