a24349e9aebdf30665d0a88ac3e630209ef086f6
[claws.git] / src / plugins / gdata / cm_gdata_prefs.c
1 /* GData plugin for Claws-Mail
2  * Copyright (C) 2011 Holger Berndt
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #  include "claws-features.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26
27 #include "cm_gdata_prefs.h"
28 #include "gdata_plugin.h"
29 #include "cm_gdata_contacts.h"
30
31 #include "prefs_gtk.h"
32 #include "main.h"
33
34 #include <gtk/gtk.h>
35
36
37 typedef struct
38 {
39   PrefsPage page;
40   GtkWidget *entry_username;
41   GtkWidget *spin_max_num_results;
42   GtkWidget *spin_max_cache_age;
43 } CmGDataPage;
44
45 CmGDataPrefs cm_gdata_config;
46 CmGDataPage gdata_page;
47
48 PrefParam cm_gdata_param[] =
49 {
50     {"username", NULL, &cm_gdata_config.username, P_STRING,
51         &gdata_page.entry_username, prefs_set_data_from_entry, prefs_set_entry},
52
53     { "max_num_results", "1000", &cm_gdata_config.max_num_results, P_INT,
54         &gdata_page.spin_max_num_results, prefs_set_data_from_spinbtn, prefs_set_spinbtn},
55
56     { "max_cache_age", "300", &cm_gdata_config.max_cache_age, P_INT,
57         &gdata_page.spin_max_cache_age, prefs_set_data_from_spinbtn, prefs_set_spinbtn},
58
59     {"oauth2_refresh_token", NULL, &cm_gdata_config.oauth2_refresh_token, P_PASSWORD,
60         NULL, NULL, NULL},
61
62     {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL }
63 };
64
65 static void gdata_create_prefs_page(PrefsPage *page, GtkWindow *window, gpointer data)
66 {
67   GtkWidget *vbox;
68   GtkWidget *frame;
69   GtkWidget *spinner;
70   GtkWidget *table;
71   GtkWidget *label;
72   GtkWidget *entry;
73
74   vbox = gtk_vbox_new(FALSE, 0);
75
76   /* auth frame */
77   frame = gtk_frame_new(_("Authentication"));
78   gtk_container_set_border_width(GTK_CONTAINER(frame), 5);
79   gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 0);
80
81   /* username */
82   table = gtk_table_new(2, 2, FALSE);
83   label = gtk_label_new(_("Username:"));
84   gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
85   gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_FILL, GTK_FILL, 4, 4);
86   entry = gtk_entry_new();
87   gtk_widget_set_size_request(entry, 250, -1);
88   gtk_table_attach(GTK_TABLE(table), entry, 1, 2, 0, 1, GTK_FILL, GTK_FILL, 4, 4);
89   gdata_page.entry_username = entry;
90   gtk_container_add(GTK_CONTAINER(frame), table);
91
92   table = gtk_table_new(2, 2, FALSE);
93   gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
94   label = gtk_label_new(_("Polling interval (seconds):"));
95   gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_FILL, GTK_FILL, 4, 4);
96   gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
97   spinner = gtk_spin_button_new_with_range(10, 10000, 10);
98   gtk_table_attach(GTK_TABLE(table), spinner, 1, 2, 0, 1, GTK_FILL, GTK_FILL, 4, 4);
99   gdata_page.spin_max_cache_age = spinner;
100
101   label = gtk_label_new(_("Maximum number of results:"));
102   gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2, GTK_FILL, GTK_FILL, 4, 4);
103   gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
104   spinner = gtk_spin_button_new_with_range(0, G_MAXINT, 50);
105   gtk_table_attach(GTK_TABLE(table), spinner, 1, 2, 1, 2, GTK_FILL, GTK_FILL, 4, 4);
106   gdata_page.spin_max_num_results = spinner;
107
108   gtk_widget_show_all(vbox);
109   page->widget = vbox;
110
111   prefs_set_dialog(cm_gdata_param);
112 }
113
114 static void gdata_destroy_prefs_page(PrefsPage *page)
115 {
116 }
117
118 static void gdata_save_prefs(PrefsPage *page)
119 {
120   int old_max_cache_age = cm_gdata_config.max_cache_age;
121
122   if (!page->page_open)
123     return;
124
125   prefs_set_data_from_dialog(cm_gdata_param);
126
127   cm_gdata_update_contacts_cache();
128   if(old_max_cache_age != cm_gdata_config.max_cache_age)
129     cm_gdata_update_contacts_update_timer();
130 }
131
132 void cm_gdata_prefs_init(void)
133 {
134   static gchar *path[3];
135
136   path[0] = _("Plugins");
137   path[1] = _("GData");
138   path[2] = NULL;
139
140   gdata_page.page.path = path;
141   gdata_page.page.create_widget = gdata_create_prefs_page;
142   gdata_page.page.destroy_widget = gdata_destroy_prefs_page;
143   gdata_page.page.save_page = gdata_save_prefs;
144   gdata_page.page.weight = 40.0;
145   prefs_gtk_register_page((PrefsPage*) &gdata_page);
146 }
147
148 void cm_gdata_prefs_done(void)
149 {
150   if(!claws_is_exiting()) {
151     prefs_gtk_unregister_page((PrefsPage*) &gdata_page);
152   }
153 }
154
155 void cm_gdata_master_password_change(const gchar *oldp, const gchar *newp) {
156         gchar *pass;
157         int i;
158
159         pass = password_decrypt(cm_gdata_config.oauth2_refresh_token, oldp);
160         if (pass != NULL) {
161                 g_free(cm_gdata_config.oauth2_refresh_token);
162                 cm_gdata_config.oauth2_refresh_token = password_encrypt(pass, newp);
163                 memset(pass, 0, strlen(pass));
164         }
165         g_free(pass);
166 }