e6d38a89da97084d03e679b9d9d7281a5d82fd65
[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         GtkWidget *security;
52         GtkWidget *auth;
53         GtkWidget *chooser;
54         GtkWidget *remember_pwd;
55
56         ModestProtocol proto;
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         priv->proto = MODEST_PROTOCOL_UNKNOWN;
120 }
121
122
123
124 static GtkWidget *
125 maildir_configuration (ModestStoreWidget *self)
126 {
127         ModestStoreWidgetPrivate *priv;
128         GtkWidget *label, *box, *hbox;
129         
130         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
131         box = gtk_vbox_new (FALSE, 6);
132         
133         label = gtk_label_new (NULL);
134         gtk_label_set_markup (GTK_LABEL(label),
135                               _("<b>Maildir configuration</b>"));       
136         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
137         label = gtk_label_new (NULL);
138         gtk_label_set_markup (GTK_LABEL(label),
139                               _("Please select the path to your Maildir below"));       
140         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
141         
142         label = gtk_label_new (_("Path:"));
143
144         priv->chooser = 
145                 gtk_file_chooser_button_new (_("(none)"),
146                                              GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
147
148         hbox = gtk_hbox_new (FALSE, 6);
149         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 6);
150         gtk_box_pack_start (GTK_BOX(hbox), priv->chooser, FALSE, FALSE, 6);
151
152         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 6);       
153
154         return box;
155 }
156
157
158 static GtkWidget*
159 mbox_configuration (ModestStoreWidget *self)
160 {
161         ModestStoreWidgetPrivate *priv;
162         GtkWidget *label, *box, *hbox;
163         
164         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
165         box = gtk_vbox_new (FALSE, 6);
166         
167         label = gtk_label_new (NULL);
168         gtk_label_set_markup (GTK_LABEL(label),
169                               _("<b>Mbox configuration</b>"));  
170         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
171         label = gtk_label_new (NULL);
172         gtk_label_set_markup (GTK_LABEL(label),
173                               _("Please select your mbox file below")); 
174         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
175         
176         label = gtk_label_new (_("mbox:"));
177
178         priv->chooser =
179                 gtk_file_chooser_button_new (_("(none)"),
180                                              GTK_FILE_CHOOSER_ACTION_OPEN);
181         hbox = gtk_hbox_new (FALSE, 6);
182         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 6);
183         gtk_box_pack_start (GTK_BOX(hbox), priv->chooser, FALSE, FALSE, 6);
184         
185         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 6);       
186
187         return box;
188 }
189
190 static void
191 on_entry_changed (GtkEntry *entry, gpointer user_data)
192 {
193         g_signal_emit (MODEST_STORE_WIDGET (user_data), signals[DATA_CHANGED_SIGNAL], 0);
194 }
195
196 static GtkWidget*
197 imap_pop_configuration (ModestStoreWidget *self)
198 {
199         ModestPairList *protos;
200         ModestStoreWidgetPrivate *priv;
201         GtkWidget *label, *box, *hbox;
202         GtkWidget *combo;
203         
204         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
205         box = gtk_vbox_new (FALSE, 6);
206         
207         label = gtk_label_new (NULL);
208         gtk_label_set_markup (GTK_LABEL(label),_("<b>Server configuration</b>"));
209         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
210         
211         hbox    = gtk_hbox_new (FALSE, 6);
212         label   = gtk_label_new (_("Username:"));
213         if (!priv->username)
214                 priv->username = 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->username,FALSE, FALSE, 6);
217         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);       
218
219         hbox    = gtk_hbox_new (FALSE, 6);
220         label   = gtk_label_new (_("Server:"));
221         if (!priv->servername)
222                 priv->servername = gtk_entry_new_with_max_length (40);
223         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
224         gtk_box_pack_start (GTK_BOX(hbox), priv->servername,FALSE, FALSE, 6);
225         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);       
226
227         label = gtk_label_new(NULL);
228         gtk_label_set_markup (GTK_LABEL(label),_("<b>Security</b>"));
229         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 0);
230
231         protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_SECURITY);
232         priv->security = modest_combo_box_new (protos, g_str_equal);
233         modest_pair_list_free (protos);
234         
235         hbox = gtk_hbox_new (FALSE, 6);
236         label = gtk_label_new(NULL);
237         gtk_label_set_text (GTK_LABEL(label),_("Connection type:"));
238         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
239         gtk_box_pack_start (GTK_BOX(hbox),  priv->security, FALSE, FALSE,0);
240         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);
241         
242         hbox = gtk_hbox_new (FALSE, 6);
243         label = gtk_label_new(NULL);
244
245         gtk_label_set_text (GTK_LABEL(label),_("Authentication:"));
246         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 6);
247         
248         protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_AUTH);
249         combo =  modest_combo_box_new (protos, g_str_equal);
250         modest_pair_list_free (protos);
251
252         gtk_box_pack_start (GTK_BOX(hbox), combo, FALSE, FALSE, 0);
253         priv->remember_pwd =
254                 gtk_check_button_new_with_label (_("Remember password"));
255         gtk_box_pack_start (GTK_BOX(hbox),priv->remember_pwd,
256                             FALSE, FALSE, 0);
257         
258         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);
259
260         /* Handle entry modifications */
261         g_signal_connect (priv->username, "changed", G_CALLBACK (on_entry_changed), self);
262         g_signal_connect (priv->servername, "changed", G_CALLBACK (on_entry_changed), self);
263
264         return box;
265 }
266
267
268 static void
269 modest_store_widget_finalize (GObject *obj)
270 {
271         G_OBJECT_CLASS(parent_class)->finalize (obj);
272 }
273
274
275
276 GtkWidget*
277 modest_store_widget_new (ModestProtocol proto)
278 {
279         GObject *obj;
280         GtkWidget *w;
281         ModestStoreWidget *self;
282         ModestStoreWidgetPrivate *priv;
283         
284         g_return_val_if_fail (proto, NULL);
285
286         obj = g_object_new(MODEST_TYPE_STORE_WIDGET, NULL);
287         self = MODEST_STORE_WIDGET(obj);
288         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
289
290         priv->proto = proto;
291         
292         if (proto == MODEST_PROTOCOL_STORE_POP || proto == MODEST_PROTOCOL_STORE_IMAP)
293                 w = imap_pop_configuration (self);
294         else if (proto == MODEST_PROTOCOL_STORE_MAILDIR) 
295                 w = maildir_configuration (self);
296         else if (proto == MODEST_PROTOCOL_STORE_MBOX)
297                 w = mbox_configuration (self);
298         else
299                 w = gtk_label_new ("");
300         
301         gtk_widget_show_all (w);
302         gtk_box_pack_start (GTK_BOX(self), w, FALSE, FALSE, 2);
303
304         return GTK_WIDGET(self);
305 }
306
307 gboolean
308 modest_store_widget_get_remember_password (ModestStoreWidget *self)
309 {
310         ModestStoreWidgetPrivate *priv;
311
312         g_return_val_if_fail (self, FALSE);
313         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
314
315         if (GTK_IS_TOGGLE_BUTTON(priv->remember_pwd)) 
316                 return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(priv->remember_pwd));
317         else
318                 return FALSE;
319 }
320
321 const gchar*
322 modest_store_widget_get_username (ModestStoreWidget *self)
323 {
324         ModestStoreWidgetPrivate *priv;
325
326         g_return_val_if_fail (self, NULL);
327         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
328
329         if (GTK_IS_ENTRY(priv->username)) 
330                 return gtk_entry_get_text (GTK_ENTRY(priv->username));
331         else
332                 return NULL;
333 }
334
335 const gchar*
336 modest_store_widget_get_servername (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->servername)) 
344                 return gtk_entry_get_text (GTK_ENTRY(priv->servername));
345         else
346                 return NULL;
347 }
348
349
350 ModestProtocol
351 modest_store_widget_get_proto (ModestStoreWidget *self)
352 {
353         ModestStoreWidgetPrivate *priv;
354
355         g_return_val_if_fail (self, MODEST_PROTOCOL_UNKNOWN);
356         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
357
358         return priv->proto;
359 }
360
361
362 gchar *
363 modest_store_widget_get_path (ModestStoreWidget *self)
364 {
365         ModestStoreWidgetPrivate *priv;
366         
367         g_return_val_if_fail (self, MODEST_PROTOCOL_UNKNOWN);
368         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
369
370         if (GTK_IS_FILE_CHOOSER(priv->chooser))
371                 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(priv->chooser));
372         else
373                 return NULL;
374 }