* big commit, mainly cleanups:
[modest] / src / gnome / 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
55         ModestProtocol proto;
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         ModestPairList *protos;
199         ModestStoreWidgetPrivate *priv;
200         GtkWidget *label, *box, *hbox;
201         
202         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
203         box = gtk_vbox_new (FALSE, 6);
204         
205         label = gtk_label_new (NULL);
206         gtk_label_set_markup (GTK_LABEL(label),_("<b>Server configuration</b>"));
207         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
208         
209         hbox    = gtk_hbox_new (FALSE, 6);
210         label   = gtk_label_new (_("Username:"));
211         if (!priv->username)
212                 priv->username = gtk_entry_new_with_max_length (40);
213         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
214         gtk_box_pack_start (GTK_BOX(hbox), priv->username,FALSE, FALSE, 6);
215         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);       
216
217         hbox    = gtk_hbox_new (FALSE, 6);
218         label   = gtk_label_new (_("Server:"));
219         if (!priv->servername)
220                 priv->servername = gtk_entry_new_with_max_length (40);
221         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
222         gtk_box_pack_start (GTK_BOX(hbox), priv->servername,FALSE, FALSE, 6);
223         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);       
224
225         label = gtk_label_new(NULL);
226         gtk_label_set_markup (GTK_LABEL(label),_("<b>Security</b>"));
227         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 0);
228
229         protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_SECURITY);
230         priv->security = modest_combo_box_new (protos, g_str_equal);
231         modest_pair_list_free (protos);
232         
233         hbox = gtk_hbox_new (FALSE, 6);
234         label = gtk_label_new(NULL);
235         gtk_label_set_text (GTK_LABEL(label),_("Connection type:"));
236         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
237         gtk_box_pack_start (GTK_BOX(hbox),  priv->security, FALSE, FALSE,0);
238         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);
239         
240         hbox = gtk_hbox_new (FALSE, 6);
241         label = gtk_label_new(NULL);
242
243         gtk_label_set_text (GTK_LABEL(label),_("Authentication:"));
244         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 6);
245         
246         protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_AUTH);
247         priv->auth =  modest_combo_box_new (protos, g_str_equal);
248         modest_pair_list_free (protos);
249
250         gtk_box_pack_start (GTK_BOX(hbox), priv->auth, FALSE, FALSE, 0);
251         
252         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);
253
254         /* Handle entry modifications */
255         g_signal_connect (priv->username, "changed", G_CALLBACK (on_entry_changed), self);
256         g_signal_connect (priv->servername, "changed", G_CALLBACK (on_entry_changed), self);
257
258         return box;
259 }
260
261
262 static void
263 modest_store_widget_finalize (GObject *obj)
264 {
265         G_OBJECT_CLASS(parent_class)->finalize (obj);
266 }
267
268
269
270 GtkWidget*
271 modest_store_widget_new (ModestProtocol proto)
272 {
273         GObject *obj;
274         GtkWidget *w;
275         ModestStoreWidget *self;
276         ModestStoreWidgetPrivate *priv;
277         
278         g_return_val_if_fail (proto, NULL);
279
280         obj = g_object_new(MODEST_TYPE_STORE_WIDGET, NULL);
281         self = MODEST_STORE_WIDGET(obj);
282         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
283
284         priv->proto = proto;
285         
286         if (proto == MODEST_PROTOCOL_STORE_POP || proto == MODEST_PROTOCOL_STORE_IMAP)
287                 w = imap_pop_configuration (self);
288         else if (proto == MODEST_PROTOCOL_STORE_MAILDIR) 
289                 w = maildir_configuration (self);
290         else if (proto == MODEST_PROTOCOL_STORE_MBOX)
291                 w = mbox_configuration (self);
292         else
293                 w = gtk_label_new ("");
294         
295         gtk_widget_show_all (w);
296         gtk_box_pack_start (GTK_BOX(self), w, FALSE, FALSE, 2);
297
298         return GTK_WIDGET(self);
299 }
300
301 const gchar*
302 modest_store_widget_get_username (ModestStoreWidget *self)
303 {
304         ModestStoreWidgetPrivate *priv;
305
306         g_return_val_if_fail (self, NULL);
307         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
308
309         if (GTK_IS_ENTRY(priv->username)) 
310                 return gtk_entry_get_text (GTK_ENTRY(priv->username));
311         else
312                 return NULL;
313 }
314
315 const gchar*
316 modest_store_widget_get_servername (ModestStoreWidget *self)
317 {
318         ModestStoreWidgetPrivate *priv;
319
320         g_return_val_if_fail (self, NULL);
321         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
322         
323         if (GTK_IS_ENTRY(priv->servername)) 
324                 return gtk_entry_get_text (GTK_ENTRY(priv->servername));
325         else
326                 return NULL;
327 }
328
329
330 ModestProtocol
331 modest_store_widget_get_proto (ModestStoreWidget *self)
332 {
333         ModestStoreWidgetPrivate *priv;
334
335         g_return_val_if_fail (self, MODEST_PROTOCOL_UNKNOWN);
336         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
337
338         return priv->proto;
339 }
340
341
342 gchar *
343 modest_store_widget_get_path (ModestStoreWidget *self)
344 {
345         ModestStoreWidgetPrivate *priv;
346         
347         g_return_val_if_fail (self, MODEST_PROTOCOL_UNKNOWN);
348         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
349
350         if (GTK_IS_FILE_CHOOSER(priv->chooser))
351                 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(priv->chooser));
352         else
353                 return NULL;
354 }
355
356 static ModestProtocol
357 get_protocol_from_combo (GtkWidget *combo)
358 {
359         gchar *chosen;
360
361         if (!combo)
362                 return MODEST_PROTOCOL_UNKNOWN;
363
364         chosen = gtk_combo_box_get_active_text (GTK_COMBO_BOX (combo));
365
366         return modest_protocol_info_get_protocol (chosen);
367
368 }
369
370 ModestProtocol
371 modest_store_widget_get_auth (ModestStoreWidget *self)
372 {
373         ModestStoreWidgetPrivate *priv; 
374
375         g_return_val_if_fail (self, MODEST_PROTOCOL_UNKNOWN);
376         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
377
378         return get_protocol_from_combo (priv->auth);
379 }
380
381 ModestProtocol
382 modest_store_widget_get_security (ModestStoreWidget *self)
383 {
384         ModestStoreWidgetPrivate *priv; 
385
386         g_return_val_if_fail (self, MODEST_PROTOCOL_UNKNOWN);
387         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
388
389         return get_protocol_from_combo (priv->security);
390 }