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