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