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