* Added a new account key called type for server accounts
[modest] / src / gtk / 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         DATA_CHANGED_SIGNAL,
41         LAST_SIGNAL
42 };
43
44 typedef struct _ModestStoreWidgetPrivate ModestStoreWidgetPrivate;
45 struct _ModestStoreWidgetPrivate {
46         
47         gchar* proto;
48         
49         GtkWidget *servername;
50         GtkWidget *username;
51         GtkWidget *security;
52         GtkWidget *auth;
53         GtkWidget *chooser;
54         GtkWidget *remember_pwd;
55         
56         ModestWidgetFactory *factory;
57 };
58 #define MODEST_STORE_WIDGET_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
59                                                  MODEST_TYPE_STORE_WIDGET, \
60                                                  ModestStoreWidgetPrivate))
61 /* globals */
62 static GtkContainerClass *parent_class = NULL;
63
64 /* uncomment the following if you have defined any signals */
65 static guint signals[LAST_SIGNAL] = {0};
66
67 GType
68 modest_store_widget_get_type (void)
69 {
70         static GType my_type = 0;
71         if (!my_type) {
72                 static const GTypeInfo my_info = {
73                         sizeof(ModestStoreWidgetClass),
74                         NULL,           /* base init */
75                         NULL,           /* base finalize */
76                         (GClassInitFunc) modest_store_widget_class_init,
77                         NULL,           /* class finalize */
78                         NULL,           /* class data */
79                         sizeof(ModestStoreWidget),
80                         1,              /* n_preallocs */
81                         (GInstanceInitFunc) modest_store_widget_init,
82                         NULL
83                 };
84                 my_type = g_type_register_static (GTK_TYPE_VBOX,
85                                                   "ModestStoreWidget",
86                                                   &my_info, 0);
87         }
88         return my_type;
89 }
90
91 static void
92 modest_store_widget_class_init (ModestStoreWidgetClass *klass)
93 {
94         GObjectClass *gobject_class;
95         gobject_class = (GObjectClass*) klass;
96
97         parent_class            = g_type_class_peek_parent (klass);
98         gobject_class->finalize = modest_store_widget_finalize;
99
100         g_type_class_add_private (gobject_class, sizeof(ModestStoreWidgetPrivate));
101
102         /* signal definitions go here, e.g.: */
103         signals[DATA_CHANGED_SIGNAL] =
104                 g_signal_new ("data_changed",
105                               G_TYPE_FROM_CLASS (klass),
106                               G_SIGNAL_RUN_FIRST,
107                               G_STRUCT_OFFSET(ModestStoreWidgetClass, data_changed),
108                               NULL, NULL,
109                               g_cclosure_marshal_VOID__VOID,
110                               G_TYPE_NONE, 0);
111 }
112
113 static void
114 modest_store_widget_init (ModestStoreWidget *obj)
115 {
116         ModestStoreWidgetPrivate *priv;
117         
118         priv = MODEST_STORE_WIDGET_GET_PRIVATE(obj); 
119
120         priv->proto = NULL;
121 }
122
123
124
125 static GtkWidget *
126 maildir_configuration (ModestStoreWidget *self)
127 {
128         ModestStoreWidgetPrivate *priv;
129         GtkWidget *label, *box, *hbox;
130         
131         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
132         box = gtk_vbox_new (FALSE, 6);
133         
134         label = gtk_label_new (NULL);
135         gtk_label_set_markup (GTK_LABEL(label),
136                               _("<b>Maildir configuration</b>"));       
137         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
138         label = gtk_label_new (NULL);
139         gtk_label_set_markup (GTK_LABEL(label),
140                               _("Please select the path to your Maildir below"));       
141         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
142         
143         label = gtk_label_new (_("Path:"));
144
145         priv->chooser = 
146                 gtk_file_chooser_button_new (_("(none)"),
147                                              GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
148
149         hbox = gtk_hbox_new (FALSE, 6);
150         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 6);
151         gtk_box_pack_start (GTK_BOX(hbox), priv->chooser, FALSE, FALSE, 6);
152
153         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 6);       
154
155         return box;
156 }
157
158
159 static GtkWidget*
160 mbox_configuration (ModestStoreWidget *self)
161 {
162         ModestStoreWidgetPrivate *priv;
163         GtkWidget *label, *box, *hbox;
164         
165         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
166         box = gtk_vbox_new (FALSE, 6);
167         
168         label = gtk_label_new (NULL);
169         gtk_label_set_markup (GTK_LABEL(label),
170                               _("<b>Mbox configuration</b>"));  
171         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
172         label = gtk_label_new (NULL);
173         gtk_label_set_markup (GTK_LABEL(label),
174                               _("Please select your mbox file below")); 
175         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
176         
177         label = gtk_label_new (_("mbox:"));
178
179         priv->chooser =
180                 gtk_file_chooser_button_new (_("(none)"),
181                                              GTK_FILE_CHOOSER_ACTION_OPEN);
182         hbox = gtk_hbox_new (FALSE, 6);
183         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 6);
184         gtk_box_pack_start (GTK_BOX(hbox), priv->chooser, FALSE, FALSE, 6);
185         
186         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 6);       
187
188         return box;
189 }
190
191 static void
192 on_entry_changed (GtkEntry *entry, gpointer user_data)
193 {
194         g_signal_emit (MODEST_STORE_WIDGET (user_data), signals[DATA_CHANGED_SIGNAL], 0);
195 }
196
197 static GtkWidget*
198 imap_pop_configuration (ModestStoreWidget *self)
199 {
200         ModestStoreWidgetPrivate *priv;
201         GtkWidget *label, *box, *hbox;
202
203         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
204         box = gtk_vbox_new (FALSE, 6);
205         
206         label = gtk_label_new (NULL);
207         gtk_label_set_markup (GTK_LABEL(label),_("<b>Server configuration</b>"));
208         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
209         
210         hbox    = gtk_hbox_new (FALSE, 6);
211         label   = gtk_label_new (_("Username:"));
212         if (!priv->username)
213                 priv->username = gtk_entry_new_with_max_length (40);
214         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
215         gtk_box_pack_start (GTK_BOX(hbox), priv->username,FALSE, FALSE, 6);
216         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);       
217
218         hbox    = gtk_hbox_new (FALSE, 6);
219         label   = gtk_label_new (_("Server:"));
220         if (!priv->servername)
221                 priv->servername = gtk_entry_new_with_max_length (40);
222         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
223         gtk_box_pack_start (GTK_BOX(hbox), priv->servername,FALSE, FALSE, 6);
224         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);       
225
226         label = gtk_label_new(NULL);
227         gtk_label_set_markup (GTK_LABEL(label),_("<b>Security</b>"));
228         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 0);
229
230         priv->security = modest_widget_factory_get_combo_box (priv->factory, 
231                                                               MODEST_COMBO_BOX_TYPE_SECURITY_PROTOS);
232         hbox = gtk_hbox_new (FALSE, 6);
233         label = gtk_label_new(NULL);
234         gtk_label_set_text (GTK_LABEL(label),_("Connection type:"));
235         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
236         gtk_box_pack_start (GTK_BOX(hbox),  priv->security, FALSE, FALSE,0);
237         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);
238         
239         hbox = gtk_hbox_new (FALSE, 6);
240         label = gtk_label_new(NULL);
241
242         gtk_label_set_text (GTK_LABEL(label),_("Authentication:"));
243         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 6);
244         gtk_box_pack_start (GTK_BOX(hbox),   modest_widget_factory_get_combo_box
245                             (priv->factory, MODEST_COMBO_BOX_TYPE_AUTH_PROTOS),
246                             FALSE, FALSE, 0);
247
248         priv->remember_pwd =
249                 gtk_check_button_new_with_label (_("Remember password"));
250         gtk_box_pack_start (GTK_BOX(hbox),priv->remember_pwd,
251                             FALSE, FALSE, 0);
252         
253         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);
254
255         /* Handle entry modifications */
256         g_signal_connect (priv->username, "changed", on_entry_changed, self);
257         g_signal_connect (priv->servername, "changed", on_entry_changed, self);
258
259         return box;
260 }
261
262
263 static void
264 modest_store_widget_finalize (GObject *obj)
265 {
266         ModestStoreWidgetPrivate *priv;
267         priv = MODEST_STORE_WIDGET_GET_PRIVATE(obj);
268         
269         if (priv->factory) {
270                 g_object_unref (priv->factory);
271                 priv->factory = NULL;
272         }
273
274         if (priv->proto) {
275                 g_free (priv->proto);
276                 priv->proto = NULL;
277         }
278
279         G_OBJECT_CLASS(parent_class)->finalize (obj);
280 }
281
282
283
284 GtkWidget*
285 modest_store_widget_new (ModestWidgetFactory *factory, const gchar *proto)
286 {
287         GObject *obj;
288         GtkWidget *w;
289         ModestStoreWidget *self;
290         ModestStoreWidgetPrivate *priv;
291         
292         g_return_val_if_fail (proto, NULL);
293         g_return_val_if_fail (factory, NULL);
294
295         obj = g_object_new(MODEST_TYPE_STORE_WIDGET, NULL);
296         self = MODEST_STORE_WIDGET(obj);
297         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
298
299         g_object_ref (factory);
300         priv->factory = factory;
301
302         priv->proto = g_strdup (proto);
303         
304         if (strcmp (proto, MODEST_PROTOCOL_STORE_POP) == 0 ||
305             strcmp (proto, MODEST_PROTOCOL_STORE_IMAP) == 0) {
306                 w = imap_pop_configuration (self);
307         } else if (strcmp (proto, MODEST_PROTOCOL_STORE_MAILDIR) == 0) {
308                 w = maildir_configuration (self);
309         }  else if (strcmp (proto, MODEST_PROTOCOL_STORE_MBOX) == 0) {
310                 w = mbox_configuration (self);
311         } else
312                 w = gtk_label_new ("");
313         
314         gtk_widget_show_all (w);
315         gtk_box_pack_start (GTK_BOX(self), w, FALSE, FALSE, 2);
316
317         return GTK_WIDGET(self);
318 }
319
320 gboolean
321 modest_store_widget_get_remember_password (ModestStoreWidget *self)
322 {
323         ModestStoreWidgetPrivate *priv;
324
325         g_return_val_if_fail (self, FALSE);
326         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
327
328         return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(priv->remember_pwd));
329 }
330
331
332 const gchar*
333 modest_store_widget_get_username (ModestStoreWidget *self)
334 {
335         ModestStoreWidgetPrivate *priv;
336
337         g_return_val_if_fail (self, NULL);
338         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
339         
340         return gtk_entry_get_text (GTK_ENTRY(priv->username));
341 }
342
343 const gchar*
344 modest_store_widget_get_servername (ModestStoreWidget *self)
345 {
346         ModestStoreWidgetPrivate *priv;
347
348         g_return_val_if_fail (self, NULL);
349         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
350         
351         return gtk_entry_get_text (GTK_ENTRY(priv->servername));
352 }
353
354
355 const gchar*
356 modest_store_widget_get_proto (ModestStoreWidget *self)
357 {
358         ModestStoreWidgetPrivate *priv;
359
360         g_return_val_if_fail (self, NULL);
361         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
362
363         return priv->proto;
364 }