2005-01-03 [colin] 0.9.13cvs25
[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_DIR_CLOSE,
120         STOCK_PIXMAP_MAIL_SEND,
121         STOCK_PIXMAP_MAIL_RECEIVE, 
122         STOCK_PIXMAP_MAIL_ATTACH,
123         STOCK_PIXMAP_BOOK, 
124         STOCK_PIXMAP_MIME_TEXT_PLAIN, 
125         STOCK_PIXMAP_REPLIED
126 };
127
128 static void prefs_themes_btn_use_clicked_cb     (GtkWidget *widget, gpointer data);
129 static void prefs_themes_btn_remove_clicked_cb  (GtkWidget *widget, gpointer data);
130 static void prefs_themes_btn_install_clicked_cb (GtkWidget *widget, gpointer data);
131 static void prefs_themes_btn_more_clicked_cb    (GtkWidget *widget, gpointer data);
132 static void prefs_themes_menu_item_activated_cb (GtkWidget *widget, gpointer data);
133
134 static void prefs_themes_update_buttons         (const ThemesData *tdata);
135 static void prefs_themes_display_global_stats   (const ThemesData *tdata);
136 static void prefs_themes_get_theme_info         (ThemesData *tdata);
137 static void prefs_themes_display_theme_info     (ThemesData *tdata, const ThemeInfo *info);
138 static void prefs_themes_get_themes_and_names   (ThemesData *tdata);
139 static void prefs_themes_free_names             (ThemesData *tdata);
140
141 static void prefs_themes_set_themes_menu        (GtkOptionMenu *opmenu, const ThemesData *tdata);
142
143 static gchar *prefs_themes_get_theme_stats      (const gchar *dirname);
144 static gboolean prefs_themes_is_system_theme    (const gchar *dirname);
145
146 static void prefs_themes_create_widget          (PrefsPage *page, GtkWindow *window, gpointer data);
147 static void prefs_themes_destroy_widget         (PrefsPage *page);
148 static void prefs_themes_save                   (PrefsPage *page);
149
150 static void prefs_themes_foreach_file           (const gchar *dirname, const FileFunc func, gpointer data);
151 static void prefs_themes_file_stats             (const gchar *filename, gpointer data);
152 static void prefs_themes_file_remove            (const gchar *filename, gpointer data);
153 static void prefs_themes_file_install           (const gchar *filename, gpointer data);
154
155 static void prefs_themes_file_stats(const gchar *filename, gpointer data)
156 {
157         struct stat s;
158         DirInfo    *di = (DirInfo *)data;
159         gint        len;
160         
161         if (0 == stat(filename, &s) && 0 != S_ISREG(s.st_mode)) {
162                 di->bytes += s.st_size;
163                 di->files++;
164                 len = strlen(filename);
165                 if (len > 4) {
166                         if (filename[len - 1] == 'm' && filename[len - 2] == 'p' &&
167                             filename[len - 3] == 'x' && filename[len - 4] == '.')
168                                 di->pixms++;
169                 }
170         }
171 }
172         
173 static void prefs_themes_file_remove(const gchar *filename, gpointer data)
174 {
175         gchar **status = (gchar **)data;
176         gchar *name;
177         
178         if ((*status) != NULL)
179                 return;
180         
181         name = g_basename(filename);
182         if (TRUE == is_dir_exist(filename)) {
183                 if (!((name[0] == '.') || (name[0] == '.' && name[1] == '.')))
184                         g_warning("prefs_themes_file_remove(): subdir in theme dir skipped.\n");
185         }
186         else if (0 != unlink(filename)) {
187                 (*status) = g_strdup(filename);
188         }
189 }
190
191 static void prefs_themes_file_install(const gchar *filename, gpointer data)
192 {
193         CopyInfo *ci = (CopyInfo *)data;
194         gchar *name;
195         
196         if (ci->status != NULL)
197                 return;
198         
199         name = g_basename(filename);
200         if (TRUE == is_dir_exist(filename)) {
201                 if (!((name[0] == '.') || (name[0] == '.' && name[1] == '.')))
202                         g_warning("prefs_themes_file_install(): subdir in theme dir skipped.\n");
203         }
204         else {
205                 gchar *fulldest;
206                 
207                 fulldest = g_strconcat(ci->dest, G_DIR_SEPARATOR_S, name, NULL);
208                 
209                 if (0 != copy_file(filename, fulldest, FALSE)) {
210                         ci->status = g_strdup(filename);
211                 }
212                 g_free(fulldest);
213         }
214 }
215
216 static void prefs_themes_foreach_file(const gchar *dirname, const FileFunc func, gpointer data)
217 {
218         struct dirent *d;
219         DIR           *dp;
220
221         g_return_if_fail(dirname != NULL);
222         g_return_if_fail(func != NULL);
223         
224         if ((dp = opendir(dirname)) == NULL) {
225                 debug_print("directory %s not found", dirname);
226                 return;
227         }
228
229         while ((d = readdir(dp)) != NULL) {
230                 gchar *entry;
231                 gchar *fullentry;
232
233                 entry     = d->d_name;
234                 fullentry = g_strconcat(dirname, G_DIR_SEPARATOR_S, entry, NULL);
235
236                 (*func)(fullentry, data);
237                 
238                 g_free(fullentry);
239         }       
240 }
241
242 static gboolean prefs_themes_is_system_theme(const gchar *dirname)
243 {
244         gint len;
245         
246         g_return_val_if_fail(dirname != NULL, FALSE);
247
248         len = strlen(PACKAGE_DATA_DIR);
249         if (strlen(dirname) > len && 0 == strncmp(dirname, PACKAGE_DATA_DIR, len))
250                 return TRUE;
251         
252         return FALSE;
253 }
254
255 static void prefs_themes_set_themes_menu(GtkOptionMenu *opmenu, const ThemesData *tdata)
256 {
257         GList     *themes = tdata->names;
258         GtkWidget *menu;
259         GtkWidget *item;
260         gint       i = 0, active = 0;
261
262         g_return_if_fail(opmenu != NULL);
263         
264         gtk_option_menu_remove_menu(opmenu);
265         
266         menu = gtk_menu_new ();
267         while (themes != NULL) {
268                 ThemeName *tname = (ThemeName *)(themes->data);
269                 gchar     *tpath = (gchar *)(tname->item->data);
270                 
271                 item = gtk_menu_item_new_with_label(tname->name);
272                 gtk_widget_show(item);
273                 gtk_signal_connect(GTK_OBJECT(item), "activate",
274                                    GTK_SIGNAL_FUNC(prefs_themes_menu_item_activated_cb),
275                                    tname->item->data);
276                 gtk_menu_append(GTK_MENU(menu), item);
277
278                 if (tdata->displayed != NULL && tdata->displayed == tpath)
279                         active = i;
280
281                 themes = g_list_next(themes);
282                 ++i;
283         }
284         
285         gtk_menu_set_active(GTK_MENU(menu), active);
286         gtk_option_menu_set_menu (opmenu, menu);
287 }
288
289 static void prefs_themes_get_themes_and_names(ThemesData *tdata)
290 {
291         GList *tpaths;
292         
293         g_return_if_fail(tdata != NULL);
294         
295         if (tdata->themes != NULL)
296                 stock_pixmap_themes_list_free(tdata->themes);
297         if (tdata->names != NULL)
298                 prefs_themes_free_names(tdata);
299         
300         tdata->themes = stock_pixmap_themes_list_new();
301         
302         tpaths = tdata->themes;
303         while (tpaths != NULL) {
304                 ThemeName *name = g_new0(ThemeName, 1);
305                 gchar     *sname = g_basename((gchar *)(tpaths->data));
306                 
307                 if (IS_INTERNAL_THEME(sname))
308                         name->name = g_strdup(_("Default internal theme"));
309                 else
310                         name->name = g_strdup(sname);
311                 name->item = tpaths;
312                         
313                 tdata->names = g_list_append(tdata->names, name);
314                 tpaths = g_list_next(tpaths);
315         }       
316 }
317
318 void prefs_themes_init(void)
319 {
320         ThemesData   *tdata;
321         ThemesPage   *page;
322         GList        *tpaths;
323         static gchar *path[3];
324
325         path[0] = _("Display");
326         path[1] = _("Themes");
327         path[2] = NULL;
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 = path;
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(_("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 static void prefs_themes_display_theme_info(ThemesData *tdata, const ThemeInfo *info)
564 {
565         ThemesPage *theme = tdata->page;
566         gchar *save_prefs_path;
567         gint   i;
568         
569         gtk_label_set_text(GTK_LABEL(theme->name), info->name);
570         gtk_label_set_text(GTK_LABEL(theme->author), info->author);
571         gtk_label_set_text(GTK_LABEL(theme->url), info->url);   
572         gtk_label_set_text(GTK_LABEL(theme->status), info->status);
573
574         save_prefs_path = prefs_common.pixmap_theme_path;
575         prefs_common.pixmap_theme_path = tdata->displayed;
576         for (i = 0; i < PREVIEW_ICONS; ++i) {
577                 stock_pixmap_gdk(theme->window, prefs_themes_icons[i], 
578                                 &(theme->pixmaps[i]), &(theme->masks[i]));
579                 gtk_pixmap_set(GTK_PIXMAP(theme->icons[i]),
580                                 theme->pixmaps[i], theme->masks[i]);
581         }
582         prefs_common.pixmap_theme_path = save_prefs_path;
583
584         prefs_themes_update_buttons(tdata);
585 }
586
587 static void prefs_themes_display_global_stats(const ThemesData *tdata)
588 {
589         ThemesPage *theme = tdata->page;
590         GList      *tnames = tdata->names;
591         gchar      *gstats;
592         gint        sys = 0;
593         gint        usr = 0;
594         gint        all = 0;
595
596         while (tnames != NULL) {
597                 ThemeName *tname = (ThemeName *)(tnames->data);
598                 gchar     *tpath = (gchar *)(tname->item->data);
599                 
600                 if (IS_SYSTEM_THEME(tpath)) 
601                         ++sys;
602                 else if (!IS_INTERNAL_THEME(tpath)) 
603                         ++usr;
604                 ++all;
605                 tnames = g_list_next(tnames);
606         }
607
608         gstats = g_strdup_printf(_("%d themes available (%d user, %d system, 1 internal)"),
609                                  all, usr, sys);
610         gtk_label_set_text(GTK_LABEL(theme->global), gstats);
611         g_free(gstats);
612 }
613
614 #define INFOFILE_LINE_LEN 80
615
616 #define FGETS_INFOFILE_LINE() \
617         line[0] = '\0'; \
618         fgets(line, INFOFILE_LINE_LEN, finfo); \
619         if ((len = strlen(line)) > 0) { \
620                 if (line[len - 1] == '\n') line[len - 1] = '\0'; \
621         } \
622         else { \
623                 strcpy(line, _("Unknown")); \
624         }
625
626 static void prefs_themes_get_theme_info(ThemesData *tdata)
627 {
628         FILE  *finfo;
629         gchar *sinfo;
630         gchar *path;
631         gchar  line[INFOFILE_LINE_LEN];
632         gint   len;
633         ThemeInfo *info;
634         ThemesPage *theme = tdata->page;
635
636         g_return_if_fail(theme != NULL);
637         path = tdata->displayed;
638         g_return_if_fail(path != NULL);
639
640         debug_print("Getting theme info for %s\n", path);
641         
642         info = g_new0(ThemeInfo, 1);
643         
644         if (IS_INTERNAL_THEME(path)) {
645                 info->name = g_strdup(_("Default internal theme"));
646                 info->author = g_strdup(_("The Sylpheed Claws Team"));
647                 info->url = g_strdup(HOMEPAGE_URI);
648                 info->status = g_strdup_printf(_("Internal theme has %d icons"), N_STOCK_PIXMAPS);
649         }
650         else {
651                 sinfo = g_strconcat(path, G_DIR_SEPARATOR_S, THEMEINFO_FILE, NULL);
652                 finfo = fopen(sinfo, "r");
653                 if (finfo == NULL) {
654                         info->name = g_strdup(_("No info file available for this theme"));
655                         info->author = g_strdup(_("Unknown"));
656                         info->url = g_strdup(_("Unknown"));
657                 }
658                 else {
659                         FGETS_INFOFILE_LINE()
660                         info->name = g_strdup(line);
661                         FGETS_INFOFILE_LINE()
662                         info->author = g_strdup(line);
663                         FGETS_INFOFILE_LINE()
664                         info->url = g_strdup(line);
665                 
666                         fclose(finfo);
667                 }
668                 g_free(sinfo);
669
670                 info->status = prefs_themes_get_theme_stats(path);
671                 if (info->status == NULL) {
672                         info->status = g_strdup(_("Error: can't get theme status"));
673                 }
674         }
675
676         prefs_themes_display_theme_info(tdata, info);
677
678         g_free(info->name);
679         g_free(info->author);
680         g_free(info->url);
681         g_free(info->status);
682         
683         g_free(info);
684 }
685
686 #undef FGETS_INFOFILE_LINE
687
688 static gchar *prefs_themes_get_theme_stats(const gchar *dirname)
689 {
690         gchar   *stats;
691         DirInfo *dinfo;
692
693         dinfo = g_new0(DirInfo, 1);
694         
695         prefs_themes_foreach_file(dirname, prefs_themes_file_stats, dinfo);
696         stats = g_strdup_printf(_("%d files (%d icons), size: %s"), 
697                                 dinfo->files, dinfo->pixms, to_human_readable(dinfo->bytes));
698         
699         g_free(dinfo);
700         return stats;
701 }
702
703 /* BEGIN GLADE CODE */
704 /* This is a dummy pixmap we use when a pixmap can't be found. */
705 static char *dummy_pixmap_xpm[] = {
706         /* columns rows colors chars-per-pixel */
707         "1 1 1 1",
708         "  c None",
709         /* pixels */
710         " "
711 };
712
713 /* This is an internally used function to create pixmaps. */
714 static GtkWidget* create_dummy_pixmap(GtkWidget *widget)
715 {
716         GdkColormap *colormap;
717         GdkPixmap *gdkpixmap;
718         GdkBitmap *mask;
719         GtkWidget *pixmap;
720
721         colormap = gtk_widget_get_colormap (widget);
722         gdkpixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL, colormap, &mask,
723                         NULL, dummy_pixmap_xpm);
724         if (gdkpixmap == NULL)
725                 g_error ("Couldn't create replacement pixmap.");
726         pixmap = gtk_pixmap_new (gdkpixmap, mask);
727         gdk_pixmap_unref (gdkpixmap);
728         gdk_bitmap_unref (mask);
729         return pixmap;
730 }
731 /* END GLADE CODE */
732
733 /* glade generates some calls to a create_pixmap support function 
734  * we don't really need. */
735 #define create_pixmap(widget,filename) create_dummy_pixmap(widget)
736
737 static void prefs_themes_create_widget(PrefsPage *page, GtkWindow *window, gpointer data)
738 {
739         ThemesPage *prefs_themes = (ThemesPage *)page;
740         ThemesData *tdata = prefs_themes_data;
741         gchar *buf;
742         gint   i;
743         /* from gtk/about.c */
744         GtkStyle *style;
745         GdkColormap *cmap;
746         GdkColor uri_color[2] = {{0, 0, 0, 0xffff}, {0, 0xffff, 0, 0}};
747         gboolean success[2];
748
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         PACK_FRAME (vbox1, frame1, _("Selector"));
787         
788         vbox2 = gtk_vbox_new (FALSE, 0);
789         gtk_widget_show (vbox2);
790         gtk_container_add (GTK_CONTAINER (frame1), vbox2);
791
792         hbox3 = gtk_hbox_new (FALSE, 0);
793         gtk_widget_show (hbox3);
794         gtk_box_pack_start (GTK_BOX (vbox2), hbox3, TRUE, TRUE, 0);
795         gtk_container_set_border_width (GTK_CONTAINER (hbox3), 5);
796
797         menu_themes = gtk_option_menu_new ();
798         gtk_widget_show (menu_themes);
799         gtk_box_pack_start (GTK_BOX (hbox3), menu_themes, FALSE, FALSE, 0);
800         menu_themes_menu = gtk_menu_new ();
801         glade_menuitem = gtk_menu_item_new_with_label ("");
802         gtk_widget_show (glade_menuitem);
803         gtk_menu_append (GTK_MENU (menu_themes_menu), glade_menuitem);
804         gtk_option_menu_set_menu (GTK_OPTION_MENU (menu_themes), menu_themes_menu);
805
806         btn_install = gtk_button_new_with_label (_("Install new..."));
807         gtk_widget_show (btn_install);
808         gtk_box_pack_start (GTK_BOX (hbox3), btn_install, FALSE, FALSE, 0);
809         GTK_WIDGET_SET_FLAGS (btn_install, GTK_CAN_DEFAULT);
810
811         btn_more = gtk_button_new_with_label (_("Get more..."));
812         gtk_widget_show (btn_more);
813         gtk_box_pack_start (GTK_BOX (hbox3), btn_more, FALSE, FALSE, 0);
814         GTK_WIDGET_SET_FLAGS (btn_more, GTK_CAN_DEFAULT);
815         /* make it look like an uri */
816         gtk_button_set_relief(GTK_BUTTON(btn_more), GTK_RELIEF_NONE);
817         gtk_label_get(GTK_LABEL(GTK_BIN(btn_more)->child), &buf);
818         buf = g_strdup(buf);
819         for (i = 0; buf[i] != '\0'; buf[i++] = '_');
820         gtk_label_set_pattern(GTK_LABEL(GTK_BIN(btn_more)->child), buf);
821         g_free(buf);
822         cmap = gdk_window_get_colormap((mainwindow_get_mainwindow())->window->window);
823         gdk_colormap_alloc_colors(cmap, uri_color, 2, FALSE, TRUE, success);
824         if (success[0] == TRUE && success[1] == TRUE) {
825                 gtk_widget_ensure_style(GTK_BIN(btn_more)->child);
826                 style = gtk_style_copy
827                         (gtk_widget_get_style(GTK_BIN(btn_more)->child));
828                 style->fg[GTK_STATE_NORMAL]   = uri_color[0];
829                 style->fg[GTK_STATE_ACTIVE]   = uri_color[1];
830                 style->fg[GTK_STATE_PRELIGHT] = uri_color[0];
831                 gtk_widget_set_style(GTK_BIN(btn_more)->child, style);
832         } else
833                 g_warning("prefs_themes_create_widget(): color allocation failed.\n");
834
835
836         label_global_status = gtk_label_new ("");
837         gtk_widget_show (label_global_status);
838         gtk_box_pack_start (GTK_BOX (vbox2), label_global_status, FALSE, FALSE, 0);
839         gtk_label_set_justify (GTK_LABEL (label_global_status), GTK_JUSTIFY_LEFT);
840         gtk_misc_set_alignment (GTK_MISC (label_global_status), 0, 0.5);
841         gtk_misc_set_padding (GTK_MISC (label_global_status), 6, 0);
842
843         PACK_FRAME (vbox1, frame_info, _("Information"));
844         
845         table1 = gtk_table_new (4, 2, FALSE);
846         gtk_widget_show (table1);
847         gtk_container_add (GTK_CONTAINER (frame_info), table1);
848
849         label1 = gtk_label_new (_("Name: "));
850         gtk_widget_show (label1);
851         gtk_table_attach (GTK_TABLE (table1), label1, 0, 1, 0, 1,
852                         (GtkAttachOptions) (GTK_FILL),
853                         (GtkAttachOptions) (0), 8, 2);
854         gtk_label_set_justify (GTK_LABEL (label1), GTK_JUSTIFY_LEFT);
855         gtk_misc_set_alignment (GTK_MISC (label1), 0, 0.5);
856
857         label2 = gtk_label_new (_("Author: "));
858         gtk_widget_show (label2);
859         gtk_table_attach (GTK_TABLE (table1), label2, 0, 1, 1, 2,
860                         (GtkAttachOptions) (GTK_FILL),
861                         (GtkAttachOptions) (0), 8, 2);
862         gtk_label_set_justify (GTK_LABEL (label2), GTK_JUSTIFY_LEFT);
863         gtk_misc_set_alignment (GTK_MISC (label2), 0, 0.5);
864
865         label3 = gtk_label_new (_("URL:"));
866         gtk_widget_show (label3);
867         gtk_table_attach (GTK_TABLE (table1), label3, 0, 1, 2, 3,
868                         (GtkAttachOptions) (GTK_FILL),
869                         (GtkAttachOptions) (0), 8, 2);
870         gtk_misc_set_alignment (GTK_MISC (label3), 0, 0.5);
871
872         label_name = gtk_label_new ("");
873         gtk_widget_show (label_name);
874         gtk_table_attach (GTK_TABLE (table1), label_name, 1, 2, 0, 1,
875                         (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
876                         (GtkAttachOptions) (0), 0, 0);
877         gtk_misc_set_alignment (GTK_MISC (label_name), 0, 0.5);
878
879         label_author = gtk_label_new ("");
880         gtk_widget_show (label_author);
881         gtk_table_attach (GTK_TABLE (table1), label_author, 1, 2, 1, 2,
882                         (GtkAttachOptions) (GTK_FILL),
883                         (GtkAttachOptions) (0), 0, 0);
884         gtk_misc_set_alignment (GTK_MISC (label_author), 0, 0.5);
885
886         label_url = gtk_label_new ("");
887         gtk_widget_show (label_url);
888         gtk_table_attach (GTK_TABLE (table1), label_url, 1, 2, 2, 3,
889                         (GtkAttachOptions) (GTK_FILL),
890                         (GtkAttachOptions) (0), 0, 0);
891         gtk_misc_set_alignment (GTK_MISC (label_url), 0, 0.5);
892
893         label4 = gtk_label_new (_("Status:"));
894         gtk_widget_show (label4);
895         gtk_table_attach (GTK_TABLE (table1), label4, 0, 1, 3, 4,
896                         (GtkAttachOptions) (GTK_FILL),
897                         (GtkAttachOptions) (0), 8, 2);
898         gtk_misc_set_alignment (GTK_MISC (label4), 0, 0.5);
899
900         label_status = gtk_label_new ("");
901         gtk_widget_show (label_status);
902         gtk_table_attach (GTK_TABLE (table1), label_status, 1, 2, 3, 4,
903                         (GtkAttachOptions) (GTK_FILL),
904                         (GtkAttachOptions) (0), 0, 0);
905         gtk_misc_set_alignment (GTK_MISC (label_status), 0, 0.5);
906
907         PACK_FRAME (vbox1, frame_preview, _("Preview"));
908         
909         hbox1 = gtk_hbox_new (FALSE, 0);
910         gtk_widget_show (hbox1);
911         gtk_container_add (GTK_CONTAINER (frame_preview), hbox1);
912
913         icon_1 = create_pixmap (vbox1, NULL);
914         gtk_widget_show (icon_1);
915         gtk_box_pack_start (GTK_BOX (hbox1), icon_1, TRUE, TRUE, 2);
916         gtk_misc_set_padding (GTK_MISC (icon_1), 0, 5);
917
918         icon_2 = create_pixmap (vbox1, NULL);
919         gtk_widget_show (icon_2);
920         gtk_box_pack_start (GTK_BOX (hbox1), icon_2, TRUE, TRUE, 2);
921         gtk_misc_set_padding (GTK_MISC (icon_2), 0, 5);
922
923         icon_3 = create_pixmap (vbox1, NULL);
924         gtk_widget_show (icon_3);
925         gtk_box_pack_start (GTK_BOX (hbox1), icon_3, TRUE, TRUE, 2);
926         gtk_misc_set_padding (GTK_MISC (icon_3), 0, 5);
927
928         icon_4 = create_pixmap (vbox1, NULL);
929         gtk_widget_show (icon_4);
930         gtk_box_pack_start (GTK_BOX (hbox1), icon_4, TRUE, TRUE, 2);
931         gtk_misc_set_padding (GTK_MISC (icon_4), 0, 5);
932
933         icon_5 = create_pixmap (vbox1, NULL);
934         gtk_widget_show (icon_5);
935         gtk_box_pack_start (GTK_BOX (hbox1), icon_5, TRUE, TRUE, 2);
936         gtk_misc_set_padding (GTK_MISC (icon_5), 0, 5);
937
938         icon_6 = create_pixmap (vbox1, NULL);
939         gtk_widget_show (icon_6);
940         gtk_box_pack_start (GTK_BOX (hbox1), icon_6, TRUE, TRUE, 0);
941         gtk_misc_set_padding (GTK_MISC (icon_6), 0, 5);
942
943         icon_7 = create_pixmap (vbox1, NULL);
944         gtk_widget_show (icon_7);
945         gtk_box_pack_start (GTK_BOX (hbox1), icon_7, TRUE, TRUE, 0);
946         gtk_misc_set_padding (GTK_MISC (icon_7), 0, 5);
947
948         PACK_FRAME (vbox1, frame_buttons, _("Actions"));
949         
950         hbuttonbox1 = gtk_hbutton_box_new ();
951         gtk_widget_show (hbuttonbox1);
952         gtk_container_add (GTK_CONTAINER (frame_buttons), hbuttonbox1);
953         gtk_container_set_border_width (GTK_CONTAINER (hbuttonbox1), 5);
954         gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox1), GTK_BUTTONBOX_START);
955         gtk_button_box_set_spacing (GTK_BUTTON_BOX (hbuttonbox1), 5);
956         gtk_button_box_set_child_ipadding (GTK_BUTTON_BOX (hbuttonbox1), 5, 0);
957
958         btn_use = gtk_button_new_with_label (_("Use this"));
959         gtk_widget_show (btn_use);
960         gtk_container_add (GTK_CONTAINER (hbuttonbox1), btn_use);
961         GTK_WIDGET_SET_FLAGS (btn_use, GTK_CAN_DEFAULT);
962
963         btn_remove = gtk_button_new_with_label (_("Remove"));
964         gtk_widget_show (btn_remove);
965         gtk_container_add (GTK_CONTAINER (hbuttonbox1), btn_remove);
966         GTK_WIDGET_SET_FLAGS (btn_remove, GTK_CAN_DEFAULT);
967
968         gtk_signal_connect(GTK_OBJECT (btn_use), "clicked",
969                         GTK_SIGNAL_FUNC (prefs_themes_btn_use_clicked_cb),
970                         NULL);
971         gtk_signal_connect(GTK_OBJECT(btn_remove), "clicked",
972                         GTK_SIGNAL_FUNC(prefs_themes_btn_remove_clicked_cb),
973                         NULL);
974         gtk_signal_connect(GTK_OBJECT(btn_install), "clicked",
975                         GTK_SIGNAL_FUNC(prefs_themes_btn_install_clicked_cb),
976                         NULL);
977         gtk_signal_connect(GTK_OBJECT(btn_more), "clicked",
978                         GTK_SIGNAL_FUNC(prefs_themes_btn_more_clicked_cb),
979                         NULL);
980
981         gtk_widget_grab_default (btn_use);
982
983         prefs_themes->window = GTK_WIDGET(window);
984         
985         prefs_themes->name   = label_name;
986         prefs_themes->author = label_author;
987         prefs_themes->url    = label_url;
988         prefs_themes->status = label_status;
989         prefs_themes->global = label_global_status;
990
991         prefs_themes->icons[0] = icon_1;
992         prefs_themes->icons[1] = icon_2;
993         prefs_themes->icons[2] = icon_3;
994         prefs_themes->icons[3] = icon_4;
995         prefs_themes->icons[4] = icon_5;
996         prefs_themes->icons[5] = icon_6;
997         prefs_themes->icons[6] = icon_7;
998         
999         prefs_themes->btn_use     = btn_use;
1000         prefs_themes->btn_remove  = btn_remove;
1001         prefs_themes->btn_install = btn_install;
1002         prefs_themes->btn_more    = btn_more;
1003
1004         prefs_themes->op_menu     = menu_themes;
1005
1006         prefs_themes->page.widget = vbox1;
1007
1008         prefs_themes_set_themes_menu(GTK_OPTION_MENU(prefs_themes->op_menu), tdata);
1009         
1010         prefs_themes_get_theme_info(tdata);
1011         prefs_themes_display_global_stats(tdata);
1012 }
1013
1014 static void prefs_themes_destroy_widget(PrefsPage *page)
1015 {
1016         /* ThemesPage *theme = (ThemesPage *)page; */
1017 }
1018
1019 static void prefs_themes_save(PrefsPage *page)
1020 {
1021         /* ThemesPage *theme = (ThemesPage *)page; */
1022 }
1023