* all:
[modest] / src / maemo / modest-store-widget.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <glib/gi18n.h>
31 #include "modest-store-widget.h"
32 #include <string.h>
33
34 /* 'private'/'protected' functions */
35 static void modest_store_widget_class_init (ModestStoreWidgetClass *klass);
36 static void modest_store_widget_init       (ModestStoreWidget *obj);
37 static void modest_store_widget_finalize   (GObject *obj);
38 /* list my signals  */
39 enum {
40         /* MY_SIGNAL_1, */
41         /* MY_SIGNAL_2, */
42         LAST_SIGNAL
43 };
44
45 typedef struct _ModestStoreWidgetPrivate ModestStoreWidgetPrivate;
46 struct _ModestStoreWidgetPrivate {
47         
48         gchar* proto;
49         
50         GtkWidget *servername;
51         GtkWidget *username;
52         GtkWidget *security;
53         GtkWidget *auth;
54         GtkWidget *chooser;
55         GtkWidget *remember_pwd;
56         
57         ModestWidgetFactory *factory;
58 };
59 #define MODEST_STORE_WIDGET_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
60                                                  MODEST_TYPE_STORE_WIDGET, \
61                                                  ModestStoreWidgetPrivate))
62 /* globals */
63 static GtkContainerClass *parent_class = NULL;
64
65 /* uncomment the following if you have defined any signals */
66 /* static guint signals[LAST_SIGNAL] = {0}; */
67
68 GType
69 modest_store_widget_get_type (void)
70 {
71         static GType my_type = 0;
72         if (!my_type) {
73                 static const GTypeInfo my_info = {
74                         sizeof(ModestStoreWidgetClass),
75                         NULL,           /* base init */
76                         NULL,           /* base finalize */
77                         (GClassInitFunc) modest_store_widget_class_init,
78                         NULL,           /* class finalize */
79                         NULL,           /* class data */
80                         sizeof(ModestStoreWidget),
81                         1,              /* n_preallocs */
82                         (GInstanceInitFunc) modest_store_widget_init,
83                         NULL
84                 };
85                 my_type = g_type_register_static (GTK_TYPE_VBOX,
86                                                   "ModestStoreWidget",
87                                                   &my_info, 0);
88         }
89         return my_type;
90 }
91
92 static void
93 modest_store_widget_class_init (ModestStoreWidgetClass *klass)
94 {
95         GObjectClass *gobject_class;
96         gobject_class = (GObjectClass*) klass;
97
98         parent_class            = g_type_class_peek_parent (klass);
99         gobject_class->finalize = modest_store_widget_finalize;
100
101         g_type_class_add_private (gobject_class, sizeof(ModestStoreWidgetPrivate));
102
103         /* signal definitions go here, e.g.: */
104 /*      signals[MY_SIGNAL_1] = */
105 /*              g_signal_new ("my_signal_1",....); */
106 /*      signals[MY_SIGNAL_2] = */
107 /*              g_signal_new ("my_signal_2",....); */
108 /*      etc. */
109 }
110
111 static void
112 modest_store_widget_init (ModestStoreWidget *obj)
113 {
114         ModestStoreWidgetPrivate *priv;
115         
116         priv = MODEST_STORE_WIDGET_GET_PRIVATE(obj); 
117
118         priv->proto = NULL;
119 }
120
121
122
123 static GtkWidget *
124 maildir_configuration (ModestStoreWidget *self)
125 {
126         ModestStoreWidgetPrivate *priv;
127         GtkWidget *label, *box, *hbox;
128         
129         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
130         box = gtk_vbox_new (FALSE, 6);
131         
132         label = gtk_label_new (NULL);
133         gtk_label_set_markup (GTK_LABEL(label),
134                               _("<b>Maildir configuration</b>"));       
135         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
136         label = gtk_label_new (NULL);
137         gtk_label_set_markup (GTK_LABEL(label),
138                               _("Please select the path to your Maildir below"));       
139         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
140         
141         label = gtk_label_new (_("Path:"));
142
143         priv->chooser = 
144                 gtk_file_chooser_button_new (_("(none)"),
145                                              GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
146
147         hbox = gtk_hbox_new (FALSE, 6);
148         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 6);
149         gtk_box_pack_start (GTK_BOX(hbox), priv->chooser, FALSE, FALSE, 6);
150
151         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 6);       
152
153         return box;
154 }
155
156
157 static GtkWidget*
158 mbox_configuration (ModestStoreWidget *self)
159 {
160         ModestStoreWidgetPrivate *priv;
161         GtkWidget *label, *box, *hbox;
162         
163         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
164         box = gtk_vbox_new (FALSE, 6);
165         
166         label = gtk_label_new (NULL);
167         gtk_label_set_markup (GTK_LABEL(label),
168                               _("<b>Mbox configuration</b>"));  
169         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
170         label = gtk_label_new (NULL);
171         gtk_label_set_markup (GTK_LABEL(label),
172                               _("Please select your mbox file below")); 
173         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
174         
175         label = gtk_label_new (_("mbox:"));
176
177         priv->chooser =
178                 gtk_file_chooser_button_new (_("(none)"),
179                                              GTK_FILE_CHOOSER_ACTION_OPEN);
180         hbox = gtk_hbox_new (FALSE, 6);
181         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 6);
182         gtk_box_pack_start (GTK_BOX(hbox), priv->chooser, FALSE, FALSE, 6);
183         
184         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 6);       
185
186         return box;
187 }
188
189
190 static GtkWidget*
191 imap_pop_configuration (ModestStoreWidget *self)
192 {
193         ModestStoreWidgetPrivate *priv;
194         GtkWidget *label, *box, *hbox;
195
196         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
197         box = gtk_vbox_new (FALSE, 6);
198         
199         label = gtk_label_new (NULL);
200         gtk_label_set_markup (GTK_LABEL(label),_("<b>Server configuration</b>"));
201         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
202         
203         hbox    = gtk_hbox_new (FALSE, 6);
204         label   = gtk_label_new (_("Username:"));
205         if (!priv->username)
206                 priv->username = gtk_entry_new_with_max_length (40);
207         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
208         gtk_box_pack_start (GTK_BOX(hbox), priv->username,FALSE, FALSE, 6);
209         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);       
210
211         hbox    = gtk_hbox_new (FALSE, 6);
212         label   = gtk_label_new (_("Server:"));
213         if (!priv->servername)
214                 priv->servername = gtk_entry_new_with_max_length (40);
215         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
216         gtk_box_pack_start (GTK_BOX(hbox), priv->servername,FALSE, FALSE, 6);
217         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);       
218
219         label = gtk_label_new(NULL);
220         gtk_label_set_markup (GTK_LABEL(label),_("<b>Security</b>"));
221         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 0);
222
223         hbox = gtk_hbox_new (FALSE, 6);
224         label = gtk_label_new(NULL);
225         gtk_label_set_text (GTK_LABEL(label),_("Connection type:"));
226         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
227         gtk_box_pack_start (GTK_BOX(hbox),  modest_widget_factory_get_combo_box
228                             (priv->factory, MODEST_COMBO_BOX_TYPE_SECURITY_PROTOS),
229                             FALSE, FALSE,0);
230         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);
231         
232         hbox = gtk_hbox_new (FALSE, 6);
233         label = gtk_label_new(NULL);
234
235         gtk_label_set_text (GTK_LABEL(label),_("Authentication:"));
236         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 6);
237         gtk_box_pack_start (GTK_BOX(hbox),   modest_widget_factory_get_combo_box
238                             (priv->factory, MODEST_COMBO_BOX_TYPE_AUTH_PROTOS),
239                             FALSE, FALSE, 0);
240
241         priv->remember_pwd =
242                 gtk_check_button_new_with_label (_("Remember password"));
243         gtk_box_pack_start (GTK_BOX(hbox),priv->remember_pwd,
244                             FALSE, FALSE, 0);
245         
246         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);
247
248         return box;
249 }
250
251
252 static void
253 modest_store_widget_finalize (GObject *obj)
254 {
255         ModestStoreWidgetPrivate *priv;
256         priv = MODEST_STORE_WIDGET_GET_PRIVATE(obj);
257         
258         if (priv->factory) {
259                 g_object_unref (priv->factory);
260                 priv->factory = NULL;
261         }
262
263         if (priv->proto) {
264                 g_free (priv->proto);
265                 priv->proto = NULL;
266         }
267
268         G_OBJECT_CLASS(parent_class)->finalize (obj);
269 }
270
271
272
273 GtkWidget*
274 modest_store_widget_new (ModestWidgetFactory *factory, const gchar *proto)
275 {
276         GObject *obj;
277         GtkWidget *w;
278         ModestStoreWidget *self;
279         ModestStoreWidgetPrivate *priv;
280         
281         g_return_val_if_fail (proto, NULL);
282         g_return_val_if_fail (factory, NULL);
283
284         obj = g_object_new(MODEST_TYPE_STORE_WIDGET, NULL);
285         self = MODEST_STORE_WIDGET(obj);
286         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
287
288         g_object_ref (factory);
289         priv->factory = factory;
290
291         priv->proto = g_strdup (proto);
292         
293         if (strcmp (proto, MODEST_PROTOCOL_STORE_POP) == 0 ||
294             strcmp (proto, MODEST_PROTOCOL_STORE_IMAP) == 0) {
295                 w = imap_pop_configuration (self);
296         } else if (strcmp (proto, MODEST_PROTOCOL_STORE_MAILDIR) == 0) {
297                 w = maildir_configuration (self);
298         }  else if (strcmp (proto, MODEST_PROTOCOL_STORE_MBOX) == 0) {
299                 w = mbox_configuration (self);
300         } else
301                 w = gtk_label_new ("");
302         
303         gtk_widget_show_all (w);
304         gtk_box_pack_start (GTK_BOX(self), w, FALSE, FALSE, 2);
305
306         return GTK_WIDGET(self);
307 }
308
309
310 gboolean
311 modest_store_widget_get_remember_password (ModestStoreWidget *self)
312 {
313         ModestStoreWidgetPrivate *priv;
314
315         g_return_val_if_fail (self, FALSE);
316         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
317
318         return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(priv->remember_pwd));
319 }
320
321
322 const gchar*
323 modest_store_widget_get_username (ModestStoreWidget *self)
324 {
325         ModestStoreWidgetPrivate *priv;
326
327         g_return_val_if_fail (self, NULL);
328         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
329         
330         return gtk_entry_get_text (GTK_ENTRY(priv->username));
331 }
332
333 const gchar*
334 modest_store_widget_get_servername (ModestStoreWidget *self)
335 {
336         ModestStoreWidgetPrivate *priv;
337
338         g_return_val_if_fail (self, NULL);
339         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
340         
341         return gtk_entry_get_text (GTK_ENTRY(priv->servername));
342 }
343
344
345 const gchar*
346 modest_store_widget_get_proto (ModestStoreWidget *self)
347 {
348         ModestStoreWidgetPrivate *priv;
349
350         g_return_val_if_fail (self, FALSE);
351         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
352
353         return priv->proto;
354 }
355