66b258627bbcfc5701a4b2c3ca61b6f59ee1caba
[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         GtkWidget *servername;
48         GtkWidget *username;
49         GtkWidget *security;
50         GtkWidget *auth;
51         GtkWidget *chooser;
52         GtkWidget *remember_pwd;
53
54         ModestProtocol proto;
55         ModestWidgetFactory *factory;
56 };
57 #define MODEST_STORE_WIDGET_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
58                                                  MODEST_TYPE_STORE_WIDGET, \
59                                                  ModestStoreWidgetPrivate))
60 /* globals */
61 static GtkContainerClass *parent_class = NULL;
62
63 /* uncomment the following if you have defined any signals */
64 static guint signals[LAST_SIGNAL] = {0};
65
66 GType
67 modest_store_widget_get_type (void)
68 {
69         static GType my_type = 0;
70         if (!my_type) {
71                 static const GTypeInfo my_info = {
72                         sizeof(ModestStoreWidgetClass),
73                         NULL,           /* base init */
74                         NULL,           /* base finalize */
75                         (GClassInitFunc) modest_store_widget_class_init,
76                         NULL,           /* class finalize */
77                         NULL,           /* class data */
78                         sizeof(ModestStoreWidget),
79                         1,              /* n_preallocs */
80                         (GInstanceInitFunc) modest_store_widget_init,
81                         NULL
82                 };
83                 my_type = g_type_register_static (GTK_TYPE_VBOX,
84                                                   "ModestStoreWidget",
85                                                   &my_info, 0);
86         }
87         return my_type;
88 }
89
90 static void
91 modest_store_widget_class_init (ModestStoreWidgetClass *klass)
92 {
93         GObjectClass *gobject_class;
94         gobject_class = (GObjectClass*) klass;
95
96         parent_class            = g_type_class_peek_parent (klass);
97         gobject_class->finalize = modest_store_widget_finalize;
98
99         g_type_class_add_private (gobject_class, sizeof(ModestStoreWidgetPrivate));
100
101         /* signal definitions go here, e.g.: */
102         signals[DATA_CHANGED_SIGNAL] =
103                 g_signal_new ("data_changed",
104                               G_TYPE_FROM_CLASS (klass),
105                               G_SIGNAL_RUN_FIRST,
106                               G_STRUCT_OFFSET(ModestStoreWidgetClass, data_changed),
107                               NULL, NULL,
108                               g_cclosure_marshal_VOID__VOID,
109                               G_TYPE_NONE, 0);
110 }
111
112 static void
113 modest_store_widget_init (ModestStoreWidget *obj)
114 {
115         ModestStoreWidgetPrivate *priv;
116         
117         priv = MODEST_STORE_WIDGET_GET_PRIVATE(obj); 
118         priv->proto = MODEST_PROTOCOL_UNKNOWN;
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 static void
190 on_entry_changed (GtkEntry *entry, gpointer user_data)
191 {
192         g_signal_emit (MODEST_STORE_WIDGET (user_data), signals[DATA_CHANGED_SIGNAL], 0);
193 }
194
195 static GtkWidget*
196 imap_pop_configuration (ModestStoreWidget *self)
197 {
198         ModestStoreWidgetPrivate *priv;
199         GtkWidget *label, *box, *hbox;
200
201         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
202         box = gtk_vbox_new (FALSE, 6);
203         
204         label = gtk_label_new (NULL);
205         gtk_label_set_markup (GTK_LABEL(label),_("<b>Server configuration</b>"));
206         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
207         
208         hbox    = gtk_hbox_new (FALSE, 6);
209         label   = gtk_label_new (_("Username:"));
210         if (!priv->username)
211                 priv->username = gtk_entry_new_with_max_length (40);
212         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
213         gtk_box_pack_start (GTK_BOX(hbox), priv->username,FALSE, FALSE, 6);
214         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);       
215
216         hbox    = gtk_hbox_new (FALSE, 6);
217         label   = gtk_label_new (_("Server:"));
218         if (!priv->servername)
219                 priv->servername = gtk_entry_new_with_max_length (40);
220         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
221         gtk_box_pack_start (GTK_BOX(hbox), priv->servername,FALSE, FALSE, 6);
222         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);       
223
224         label = gtk_label_new(NULL);
225         gtk_label_set_markup (GTK_LABEL(label),_("<b>Security</b>"));
226         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 0);
227
228         priv->security = modest_widget_factory_get_combo_box (priv->factory, 
229                                                               MODEST_COMBO_BOX_TYPE_SECURITY_PROTOS);
230         hbox = gtk_hbox_new (FALSE, 6);
231         label = gtk_label_new(NULL);
232         gtk_label_set_text (GTK_LABEL(label),_("Connection type:"));
233         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
234         gtk_box_pack_start (GTK_BOX(hbox),  priv->security, FALSE, FALSE,0);
235         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);
236         
237         hbox = gtk_hbox_new (FALSE, 6);
238         label = gtk_label_new(NULL);
239
240         gtk_label_set_text (GTK_LABEL(label),_("Authentication:"));
241         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 6);
242         gtk_box_pack_start (GTK_BOX(hbox),   modest_widget_factory_get_combo_box
243                             (priv->factory, MODEST_COMBO_BOX_TYPE_AUTH_PROTOS),
244                             FALSE, FALSE, 0);
245
246         priv->remember_pwd =
247                 gtk_check_button_new_with_label (_("Remember password"));
248         gtk_box_pack_start (GTK_BOX(hbox),priv->remember_pwd,
249                             FALSE, FALSE, 0);
250         
251         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);
252
253         /* Handle entry modifications */
254         g_signal_connect (priv->username, "changed", G_CALLBACK (on_entry_changed), self);
255         g_signal_connect (priv->servername, "changed", G_CALLBACK (on_entry_changed), self);
256
257         return box;
258 }
259
260
261 static void
262 modest_store_widget_finalize (GObject *obj)
263 {
264         ModestStoreWidgetPrivate *priv;
265         priv = MODEST_STORE_WIDGET_GET_PRIVATE(obj);
266         
267         if (priv->factory) {
268                 g_object_unref (priv->factory);
269                 priv->factory = NULL;
270         }
271
272         G_OBJECT_CLASS(parent_class)->finalize (obj);
273 }
274
275
276
277 GtkWidget*
278 modest_store_widget_new (ModestWidgetFactory *factory, ModestProtocol proto)
279 {
280         GObject *obj;
281         GtkWidget *w;
282         ModestStoreWidget *self;
283         ModestStoreWidgetPrivate *priv;
284         
285         g_return_val_if_fail (proto, NULL);
286         g_return_val_if_fail (factory, NULL);
287
288         obj = g_object_new(MODEST_TYPE_STORE_WIDGET, NULL);
289         self = MODEST_STORE_WIDGET(obj);
290         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
291
292         g_object_ref (factory);
293         priv->factory = factory;
294
295         priv->proto = proto;
296         
297         if (proto == MODEST_PROTOCOL_STORE_POP || proto == MODEST_PROTOCOL_STORE_IMAP)
298                 w = imap_pop_configuration (self);
299         else if (proto == MODEST_PROTOCOL_STORE_MAILDIR) 
300                 w = maildir_configuration (self);
301         else if (proto == MODEST_PROTOCOL_STORE_MBOX)
302                 w = mbox_configuration (self);
303         else
304                 w = gtk_label_new ("");
305         
306         gtk_widget_show_all (w);
307         gtk_box_pack_start (GTK_BOX(self), w, FALSE, FALSE, 2);
308
309         return GTK_WIDGET(self);
310 }
311
312 gboolean
313 modest_store_widget_get_remember_password (ModestStoreWidget *self)
314 {
315         ModestStoreWidgetPrivate *priv;
316
317         g_return_val_if_fail (self, FALSE);
318         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
319
320         if (GTK_IS_TOGGLE_BUTTON(priv->remember_pwd)) 
321                 return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(priv->remember_pwd));
322         else
323                 return FALSE;
324 }
325
326 const gchar*
327 modest_store_widget_get_username (ModestStoreWidget *self)
328 {
329         ModestStoreWidgetPrivate *priv;
330
331         g_return_val_if_fail (self, NULL);
332         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
333
334         if (GTK_IS_ENTRY(priv->username)) 
335                 return gtk_entry_get_text (GTK_ENTRY(priv->username));
336         else
337                 return NULL;
338 }
339
340 const gchar*
341 modest_store_widget_get_servername (ModestStoreWidget *self)
342 {
343         ModestStoreWidgetPrivate *priv;
344
345         g_return_val_if_fail (self, NULL);
346         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
347         
348         if (GTK_IS_ENTRY(priv->servername)) 
349                 return gtk_entry_get_text (GTK_ENTRY(priv->servername));
350         else
351                 return NULL;
352 }
353
354
355 ModestProtocol
356 modest_store_widget_get_proto (ModestStoreWidget *self)
357 {
358         ModestStoreWidgetPrivate *priv;
359
360         g_return_val_if_fail (self, MODEST_PROTOCOL_UNKNOWN);
361         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
362
363         return priv->proto;
364 }
365
366
367 gchar *
368 modest_store_widget_get_path (ModestStoreWidget *self)
369 {
370         ModestStoreWidgetPrivate *priv;
371         
372         g_return_val_if_fail (self, MODEST_PROTOCOL_UNKNOWN);
373         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
374
375         if (GTK_IS_FILE_CHOOSER(priv->chooser))
376                 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(priv->chooser));
377         else
378                 return NULL;
379 }