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