ba92a2ab7967a926833c5fcb28fdd70db4f811f9
[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         GtkWidget *close_to_tray_checkbox;
99         GtkWidget *hide_when_iconified_checkbox;
100         CLAWS_TIP_DECL();
101
102         vbox = gtk_vbox_new(FALSE, 3);
103         gtk_container_set_border_width(GTK_CONTAINER(vbox), VBOX_BORDER);
104         gtk_widget_show(vbox);
105         
106         hide_at_startup_checkbox = gtk_check_button_new_with_label
107                                 (_("Hide at start-up"));
108         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(hide_at_startup_checkbox),
109                                      trayicon_prefs.hide_at_startup);
110         gtk_box_pack_start(GTK_BOX(vbox), hide_at_startup_checkbox, FALSE, FALSE, 0);
111         gtk_widget_show(hide_at_startup_checkbox);
112         CLAWS_SET_TIP(hide_at_startup_checkbox,
113                              _("Hide Claws Mail at start-up"));
114         
115         close_to_tray_checkbox = gtk_check_button_new_with_label
116                                 (_("Close to tray"));
117         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(close_to_tray_checkbox),
118                                      trayicon_prefs.close_to_tray);
119         gtk_box_pack_start(GTK_BOX(vbox), close_to_tray_checkbox, FALSE, FALSE, 0);
120         gtk_widget_show(close_to_tray_checkbox);
121         CLAWS_SET_TIP(close_to_tray_checkbox,
122                              _("Hide Claws Mail using the tray icon instead of closing it\nwhen the window close button is clicked"));
123
124         hide_when_iconified_checkbox = gtk_check_button_new_with_label
125                                 (_("Minimize to tray"));
126         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(hide_when_iconified_checkbox),
127                                      trayicon_prefs.hide_when_iconified);
128         gtk_box_pack_start(GTK_BOX(vbox), hide_when_iconified_checkbox, FALSE, FALSE, 0);
129         gtk_widget_show(hide_when_iconified_checkbox);
130         CLAWS_SET_TIP(hide_when_iconified_checkbox,
131                              _("Hide Claws Mail using the tray icon instead of minimizing it"));
132
133         prefs_page->hide_at_startup = hide_at_startup_checkbox;
134         prefs_page->close_to_tray = close_to_tray_checkbox;
135         prefs_page->hide_when_iconified = hide_when_iconified_checkbox;
136         prefs_page->page.widget = vbox;
137 }
138
139 static void destroy_trayicon_prefs_page(PrefsPage *page)
140 {
141         /* Do nothing! */
142 }
143
144 static void save_trayicon_prefs(PrefsPage *page)
145 {
146         TrayIconPage *prefs_page = (TrayIconPage *) page;
147         PrefFile *pref_file;
148         gchar *rc_file_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
149                                           COMMON_RC, NULL);
150         
151         trayicon_prefs.hide_at_startup = gtk_toggle_button_get_active
152                                 (GTK_TOGGLE_BUTTON(prefs_page->hide_at_startup));
153         trayicon_prefs.close_to_tray = gtk_toggle_button_get_active
154                                 (GTK_TOGGLE_BUTTON(prefs_page->close_to_tray));
155         trayicon_prefs.hide_when_iconified = gtk_toggle_button_get_active
156                                 (GTK_TOGGLE_BUTTON(prefs_page->hide_when_iconified));
157         
158         pref_file = prefs_write_open(rc_file_path);
159         g_free(rc_file_path);
160         
161         if (!(pref_file) ||
162             (prefs_set_block_label(pref_file, "TrayIcon") < 0))
163           return;
164         
165         if (prefs_write_param(param, pref_file->fp) < 0) {
166           g_warning("failed to write TrayIcon Plugin configuration\n");
167           prefs_file_close_revert(pref_file);
168           return;
169         }
170         if (fprintf(pref_file->fp, "\n") < 0) {
171                 FILE_OP_ERROR(rc_file_path, "fprintf");
172                 prefs_file_close_revert(pref_file);
173         } else
174                 prefs_file_close(pref_file);
175 }