GNOME version compiles again. It does not link yet though, because there are some...
[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 "modest-runtime.h"
35 #include <string.h>
36
37 /* 'private'/'protected' functions */
38 static void modest_store_widget_class_init (ModestStoreWidgetClass *klass);
39 static void modest_store_widget_init       (ModestStoreWidget *obj);
40 static void modest_store_widget_finalize   (GObject *obj);
41 /* list my signals  */
42 enum {
43         DATA_CHANGED_SIGNAL,
44         LAST_SIGNAL
45 };
46
47 typedef struct _ModestStoreWidgetPrivate ModestStoreWidgetPrivate;
48 struct _ModestStoreWidgetPrivate {
49         
50         GtkWidget *servername;
51         GtkWidget *username;
52         
53         ModestPairList *security_protos;
54         GtkWidget *security;
55         
56         ModestPairList *auth_protos;
57         GtkWidget *auth;
58         
59         GtkWidget *chooser;
60
61         ModestProtocolType 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_REGISTRY_TYPE_INVALID;
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         
207         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
208         box = gtk_vbox_new (FALSE, 6);
209         
210         label = gtk_label_new (NULL);
211         gtk_label_set_markup (GTK_LABEL(label),_("<b>Server configuration</b>"));
212         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
213         
214         hbox    = gtk_hbox_new (FALSE, 6);
215         label   = gtk_label_new (_("Username:"));
216         if (!priv->username)
217                 priv->username = gtk_entry_new_with_max_length (40);
218         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
219         gtk_box_pack_start (GTK_BOX(hbox), priv->username,FALSE, FALSE, 6);
220         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);       
221
222         hbox    = gtk_hbox_new (FALSE, 6);
223         label   = gtk_label_new (_("Server:"));
224         if (!priv->servername)
225                 priv->servername = gtk_entry_new_with_max_length (40);
226         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
227         gtk_box_pack_start (GTK_BOX(hbox), priv->servername,FALSE, FALSE, 6);
228         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);       
229
230         label = gtk_label_new(NULL);
231         gtk_label_set_markup (GTK_LABEL(label),_("<b>Security</b>"));
232         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 0);
233
234         /* Note: This ModestPairList* must exist for as long as the combo
235          * that uses it, because the ModestComboBox uses the ID opaquely, 
236          * so it can't know how to manage its memory. */ 
237         priv->security_protos = modest_protocol_info_get_connection_protocol_pair_list ();
238
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->auth_protos = modest_protocol_info_get_auth_protocol_pair_list ();
258         priv->auth =  modest_combo_box_new (priv->auth_protos, g_str_equal);
259
260         gtk_box_pack_start (GTK_BOX(hbox), priv->auth, FALSE, FALSE, 0);
261         
262         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);
263
264         /* Handle entry modifications */
265         g_signal_connect (priv->username, "changed", G_CALLBACK (on_entry_changed), self);
266         g_signal_connect (priv->servername, "changed", G_CALLBACK (on_entry_changed), self);
267
268         return box;
269 }
270
271
272 static void
273 modest_store_widget_finalize (GObject *obj)
274 {
275         ModestStoreWidgetPrivate *priv = MODEST_STORE_WIDGET_GET_PRIVATE(obj);
276         
277         /* These had to stay alive for as long as the comboboxes that used them: */
278         modest_pair_list_free (priv->security_protos);
279         modest_pair_list_free (priv->auth_protos);
280         
281         G_OBJECT_CLASS(parent_class)->finalize (obj);
282 }
283
284
285
286 GtkWidget*
287 modest_store_widget_new (ModestProtocolType proto)
288 {
289         GObject *obj;
290         GtkWidget *w;
291         ModestStoreWidget *self;
292         ModestStoreWidgetPrivate *priv;
293         
294         g_return_val_if_fail (proto, NULL);
295
296         obj = g_object_new(MODEST_TYPE_STORE_WIDGET, NULL);
297         self = MODEST_STORE_WIDGET(obj);
298         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
299
300         priv->proto = proto;
301         
302         if (proto == MODEST_PROTOCOLS_STORE_POP || proto == MODEST_PROTOCOLS_STORE_IMAP)
303                 w = imap_pop_configuration (self);
304         else if (proto == MODEST_PROTOCOLS_STORE_MAILDIR) 
305                 w = maildir_configuration (self);
306         else if (proto == MODEST_PROTOCOLS_STORE_MBOX)
307                 w = mbox_configuration (self);
308         else
309                 w = gtk_label_new ("");
310         
311         gtk_widget_show_all (w);
312         gtk_box_pack_start (GTK_BOX(self), w, FALSE, FALSE, 2);
313
314         return GTK_WIDGET(self);
315 }
316
317 const gchar*
318 modest_store_widget_get_username (ModestStoreWidget *self)
319 {
320         ModestStoreWidgetPrivate *priv;
321
322         g_return_val_if_fail (self, NULL);
323         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
324
325         if (GTK_IS_ENTRY(priv->username)) 
326                 return gtk_entry_get_text (GTK_ENTRY(priv->username));
327         else
328                 return NULL;
329 }
330
331 const gchar*
332 modest_store_widget_get_servername (ModestStoreWidget *self)
333 {
334         ModestStoreWidgetPrivate *priv;
335
336         g_return_val_if_fail (self, NULL);
337         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
338         
339         if (GTK_IS_ENTRY(priv->servername)) 
340                 return gtk_entry_get_text (GTK_ENTRY(priv->servername));
341         else
342                 return NULL;
343 }
344
345
346 ModestProtocolType
347 modest_store_widget_get_proto (ModestStoreWidget *self)
348 {
349         ModestStoreWidgetPrivate *priv;
350
351         g_return_val_if_fail (self, MODEST_PROTOCOL_REGISTRY_TYPE_INVALID);
352         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
353
354         return priv->proto;
355 }
356
357
358 gchar *
359 modest_store_widget_get_path (ModestStoreWidget *self)
360 {
361         ModestStoreWidgetPrivate *priv;
362         
363         g_return_val_if_fail (self, NULL);
364         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
365
366         if (GTK_IS_FILE_CHOOSER(priv->chooser))
367                 return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(priv->chooser));
368         else
369                 return NULL;
370 }
371
372 static ModestProtocolType
373 get_value_from_combo (GtkWidget *combo)
374 {
375         gchar *chosen;
376         ModestProtocol *proto;
377         const gchar *tag;
378
379         g_return_val_if_fail (combo, MODEST_PROTOCOL_REGISTRY_TYPE_INVALID);
380
381         chosen = gtk_combo_box_get_active_text (GTK_COMBO_BOX (combo));
382         tag = MODEST_PROTOCOL_REGISTRY_TRANSPORT_STORE_PROTOCOLS;
383         proto = modest_protocol_registry_get_protocol_by_name (modest_runtime_get_protocol_registry (),
384                                                                tag,
385                                                                chosen);
386
387         return modest_protocol_get_type_id (proto);
388
389 }
390
391 ModestProtocolType
392 modest_store_widget_get_auth (ModestStoreWidget *self)
393 {
394         ModestStoreWidgetPrivate *priv; 
395
396         g_return_val_if_fail (self, MODEST_PROTOCOLS_AUTH_NONE);
397         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
398
399         return get_value_from_combo (priv->auth);
400 }
401
402 ModestProtocolType
403 modest_store_widget_get_security (ModestStoreWidget *self)
404 {
405         ModestStoreWidgetPrivate *priv; 
406
407         g_return_val_if_fail (self, MODEST_PROTOCOLS_CONNECTION_NONE);
408         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
409
410         return get_value_from_combo (priv->security);
411 }