2007-07-11 [colin] 2.10.0cvs16
[claws.git] / src / plugins / trayicon / trayicon_prefs.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2007 the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>. 
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #endif
22
23 #include "defs.h"
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <gtk/gtk.h>
28
29 #include "gtkutils.h"
30 #include "utils.h"
31 #include "prefs.h"
32 #include "prefs_gtk.h"
33 #include "prefswindow.h"
34
35 #include "trayicon_prefs.h"
36
37 TrayIconPrefs trayicon_prefs;
38
39 typedef struct _TrayIconPage TrayIconPage;
40
41 struct _TrayIconPage {
42         PrefsPage page;
43         GtkWidget *hide_at_startup;
44                 GtkWidget *close_to_tray;
45                 GtkWidget *hide_when_iconified;
46 };
47
48 static PrefParam param[] = {
49         {"hide_at_startup", "FALSE", &trayicon_prefs.hide_at_startup, P_BOOL, NULL, NULL, NULL},
50         {"close_to_tray", "FALSE", &trayicon_prefs.close_to_tray, P_BOOL, NULL, NULL, NULL},
51         {"hide_when_iconified", "FALSE", &trayicon_prefs.hide_when_iconified, P_BOOL, NULL, NULL, NULL},
52         {0,0,0,0,0,0,0}
53 };
54
55 static TrayIconPage prefs_page;
56
57 static void create_trayicon_prefs_page  (PrefsPage *page,
58                                          GtkWindow *window,
59                                          gpointer   data);
60 static void destroy_trayicon_prefs_page (PrefsPage *page);
61 static void save_trayicon_prefs         (PrefsPage *page);
62
63 void trayicon_prefs_init(void)
64 {
65         static gchar *path[3];
66         gchar *rcpath;
67
68         path[0] = _("Plugins");
69         path[1] = _("Trayicon");
70         path[2] = NULL;
71
72         prefs_set_default(param);
73         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
74         prefs_read_config(param, "TrayIcon", rcpath, NULL);
75         g_free(rcpath);
76         
77         prefs_page.page.path = path;
78         prefs_page.page.create_widget = create_trayicon_prefs_page;
79         prefs_page.page.destroy_widget = destroy_trayicon_prefs_page;
80         prefs_page.page.save_page = save_trayicon_prefs;
81
82         prefs_gtk_register_page((PrefsPage *) &prefs_page);
83 }
84
85 void trayicon_prefs_done(void)
86 {
87         prefs_gtk_unregister_page((PrefsPage *) &prefs_page);
88 }
89
90 static void create_trayicon_prefs_page(PrefsPage *page,
91                                     GtkWindow *window,
92                                     gpointer data)
93 {
94         TrayIconPage *prefs_page = (TrayIconPage *) page;
95
96         GtkWidget *vbox;
97         GtkWidget *hide_at_startup_checkbox;
98         GtkTooltips *hide_at_startup_tooltip;
99         GtkWidget *close_to_tray_checkbox;
100         GtkTooltips *close_to_tray_tooltip;
101         GtkWidget *hide_when_iconified_checkbox;
102         GtkTooltips *hide_when_iconified_tooltip;
103
104         vbox = gtk_vbox_new(FALSE, 3);
105         gtk_container_set_border_width(GTK_CONTAINER(vbox), VBOX_BORDER);
106         gtk_widget_show(vbox);
107         
108         hide_at_startup_tooltip = gtk_tooltips_new();
109         hide_at_startup_checkbox = gtk_check_button_new_with_label
110                                 (_("Hide at start-up"));
111         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(hide_at_startup_checkbox),
112                                      trayicon_prefs.hide_at_startup);
113         gtk_box_pack_start(GTK_BOX(vbox), hide_at_startup_checkbox, FALSE, FALSE, 0);
114         gtk_widget_show(hide_at_startup_checkbox);
115         gtk_tooltips_set_tip(GTK_TOOLTIPS(hide_at_startup_tooltip), hide_at_startup_checkbox,
116                              _("Hide Claws Mail at start-up"), NULL);
117         
118         close_to_tray_tooltip = gtk_tooltips_new();
119         close_to_tray_checkbox = gtk_check_button_new_with_label
120                                 (_("Close to tray"));
121         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(close_to_tray_checkbox),
122                                      trayicon_prefs.close_to_tray);
123         gtk_box_pack_start(GTK_BOX(vbox), close_to_tray_checkbox, FALSE, FALSE, 0);
124         gtk_widget_show(close_to_tray_checkbox);
125         gtk_tooltips_set_tip(GTK_TOOLTIPS(close_to_tray_tooltip), close_to_tray_checkbox,
126                              _("Hide Claws Mail using the tray icon instead of closing it\nwhen the window close button is clicked"), NULL);
127
128         hide_when_iconified_tooltip = gtk_tooltips_new();
129         hide_when_iconified_checkbox = gtk_check_button_new_with_label
130                                 (_("Minimize to tray"));
131         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(hide_when_iconified_checkbox),
132                                      trayicon_prefs.hide_when_iconified);
133         gtk_box_pack_start(GTK_BOX(vbox), hide_when_iconified_checkbox, FALSE, FALSE, 0);
134         gtk_widget_show(hide_when_iconified_checkbox);
135         gtk_tooltips_set_tip(GTK_TOOLTIPS(hide_when_iconified_tooltip), hide_when_iconified_checkbox,
136                              _("Hide Claws Mail using the tray icon instead of minimizing it"), NULL);
137
138         prefs_page->hide_at_startup = hide_at_startup_checkbox;
139         prefs_page->close_to_tray = close_to_tray_checkbox;
140         prefs_page->hide_when_iconified = hide_when_iconified_checkbox;
141         prefs_page->page.widget = vbox;
142 }
143
144 static void destroy_trayicon_prefs_page(PrefsPage *page)
145 {
146         /* Do nothing! */
147 }
148
149 static void save_trayicon_prefs(PrefsPage *page)
150 {
151         TrayIconPage *prefs_page = (TrayIconPage *) page;
152         PrefFile *pref_file;
153         gchar *rc_file_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
154                                           COMMON_RC, NULL);
155         
156         trayicon_prefs.hide_at_startup = gtk_toggle_button_get_active
157                                 (GTK_TOGGLE_BUTTON(prefs_page->hide_at_startup));
158         trayicon_prefs.close_to_tray = gtk_toggle_button_get_active
159                                 (GTK_TOGGLE_BUTTON(prefs_page->close_to_tray));
160         trayicon_prefs.hide_when_iconified = gtk_toggle_button_get_active
161                                 (GTK_TOGGLE_BUTTON(prefs_page->hide_when_iconified));
162         
163         pref_file = prefs_write_open(rc_file_path);
164         g_free(rc_file_path);
165         
166         if (!(pref_file) ||
167             (prefs_set_block_label(pref_file, "TrayIcon") < 0))
168           return;
169         
170         if (prefs_write_param(param, pref_file->fp) < 0) {
171           g_warning("failed to write TrayIcon Plugin configuration\n");
172           prefs_file_close_revert(pref_file);
173           return;
174         }
175         fprintf(pref_file->fp, "\n");
176         prefs_file_close(pref_file);
177 }