* remove unneeded files
[modest] / src / gtk2 / 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 "modest-store-widget.h"
31 #include <string.h>
32
33 /* 'private'/'protected' functions */
34 static void modest_store_widget_class_init (ModestStoreWidgetClass *klass);
35 static void modest_store_widget_init       (ModestStoreWidget *obj);
36 static void modest_store_widget_finalize   (GObject *obj);
37 /* list my signals  */
38 enum {
39         /* MY_SIGNAL_1, */
40         /* MY_SIGNAL_2, */
41         LAST_SIGNAL
42 };
43
44 typedef struct _ModestStoreWidgetPrivate ModestStoreWidgetPrivate;
45 struct _ModestStoreWidgetPrivate {
46         
47         gchar* proto;
48         
49         GtkWidget *servername;
50         GtkWidget *username;
51         GtkWidget *security;
52         GtkWidget *auth;
53         GtkWidget *chooser;
54         GtkWidget *remember_pwd;
55         
56         ModestWidgetFactory *factory;
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[MY_SIGNAL_1] = */
104 /*              g_signal_new ("my_signal_1",....); */
105 /*      signals[MY_SIGNAL_2] = */
106 /*              g_signal_new ("my_signal_2",....); */
107 /*      etc. */
108 }
109
110 static void
111 modest_store_widget_init (ModestStoreWidget *obj)
112 {
113         ModestStoreWidgetPrivate *priv;
114         
115         priv = MODEST_STORE_WIDGET_GET_PRIVATE(obj); 
116
117         priv->proto = NULL;
118 }
119
120
121
122 static GtkWidget *
123 maildir_configuration (ModestStoreWidget *self)
124 {
125         ModestStoreWidgetPrivate *priv;
126         GtkWidget *label, *box, *hbox;
127         
128         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
129         box = gtk_vbox_new (FALSE, 6);
130         
131         label = gtk_label_new (NULL);
132         gtk_label_set_markup (GTK_LABEL(label),
133                               _("<b>Maildir configuration</b>"));       
134         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
135         label = gtk_label_new (NULL);
136         gtk_label_set_markup (GTK_LABEL(label),
137                               _("Please select the path to your Maildir below"));       
138         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
139         
140         label = gtk_label_new (_("Path:"));
141
142         priv->chooser = 
143                 gtk_file_chooser_button_new (_("(none)"),
144                                              GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
145
146         hbox = gtk_hbox_new (FALSE, 6);
147         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 6);
148         gtk_box_pack_start (GTK_BOX(hbox), priv->chooser, FALSE, FALSE, 6);
149
150         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 6);       
151
152         return box;
153 }
154
155
156 static GtkWidget*
157 mbox_configuration (ModestStoreWidget *self)
158 {
159         ModestStoreWidgetPrivate *priv;
160         GtkWidget *label, *box, *hbox;
161         
162         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
163         box = gtk_vbox_new (FALSE, 6);
164         
165         label = gtk_label_new (NULL);
166         gtk_label_set_markup (GTK_LABEL(label),
167                               _("<b>Mbox configuration</b>"));  
168         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
169         label = gtk_label_new (NULL);
170         gtk_label_set_markup (GTK_LABEL(label),
171                               _("Please select your mbox file below")); 
172         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
173         
174         label = gtk_label_new (_("mbox:"));
175
176         priv->chooser =
177                 gtk_file_chooser_button_new (_("(none)"),
178                                              GTK_FILE_CHOOSER_ACTION_OPEN);
179         hbox = gtk_hbox_new (FALSE, 6);
180         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 6);
181         gtk_box_pack_start (GTK_BOX(hbox), priv->chooser, FALSE, FALSE, 6);
182         
183         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 6);       
184
185         return box;
186 }
187
188
189 static GtkWidget*
190 imap_pop_configuration (ModestStoreWidget *self)
191 {
192         ModestStoreWidgetPrivate *priv;
193         GtkWidget *label, *box, *hbox;
194
195         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
196         box = gtk_vbox_new (FALSE, 6);
197         
198         label = gtk_label_new (NULL);
199         gtk_label_set_markup (GTK_LABEL(label),_("<b>Server configuration</b>"));
200         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 6);
201         
202         hbox    = gtk_hbox_new (FALSE, 6);
203         label   = gtk_label_new (_("Username:"));
204         if (!priv->username)
205                 priv->username = gtk_entry_new_with_max_length (40);
206         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
207         gtk_box_pack_start (GTK_BOX(hbox), priv->username,FALSE, FALSE, 6);
208         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);       
209
210         hbox    = gtk_hbox_new (FALSE, 6);
211         label   = gtk_label_new (_("Server:"));
212         if (!priv->servername)
213                 priv->servername = gtk_entry_new_with_max_length (40);
214         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
215         gtk_box_pack_start (GTK_BOX(hbox), priv->servername,FALSE, FALSE, 6);
216         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);       
217
218         label = gtk_label_new(NULL);
219         gtk_label_set_markup (GTK_LABEL(label),_("<b>Security</b>"));
220         gtk_box_pack_start (GTK_BOX(box), label, FALSE, FALSE, 0);
221
222         hbox = gtk_hbox_new (FALSE, 6);
223         label = gtk_label_new(NULL);
224         gtk_label_set_text (GTK_LABEL(label),_("Connection type:"));
225         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
226         gtk_box_pack_start (GTK_BOX(hbox),  modest_widget_factory_get_combo_box
227                             (priv->factory, MODEST_COMBO_BOX_TYPE_SECURITY_PROTOS),
228                             FALSE, FALSE,0);
229         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);
230         
231         hbox = gtk_hbox_new (FALSE, 6);
232         label = gtk_label_new(NULL);
233
234         gtk_label_set_text (GTK_LABEL(label),_("Authentication:"));
235         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 6);
236         gtk_box_pack_start (GTK_BOX(hbox),   modest_widget_factory_get_combo_box
237                             (priv->factory, MODEST_COMBO_BOX_TYPE_AUTH_PROTOS),
238                             FALSE, FALSE, 0);
239
240         priv->remember_pwd =
241                 gtk_check_button_new_with_label (_("Remember password"));
242         gtk_box_pack_start (GTK_BOX(hbox),priv->remember_pwd,
243                             FALSE, FALSE, 0);
244         
245         gtk_box_pack_start (GTK_BOX(box), hbox, FALSE, FALSE, 0);
246
247         return box;
248 }
249
250
251 static void
252 modest_store_widget_finalize (GObject *obj)
253 {
254         ModestStoreWidgetPrivate *priv;
255         priv = MODEST_STORE_WIDGET_GET_PRIVATE(obj);
256         
257         if (priv->factory) {
258                 g_object_unref (priv->factory);
259                 priv->factory = NULL;
260         }
261
262         if (priv->proto) {
263                 g_free (priv->proto);
264                 priv->proto = NULL;
265         }
266
267         G_OBJECT_CLASS(parent_class)->finalize (obj);
268 }
269
270
271
272 GtkWidget*
273 modest_store_widget_new (ModestWidgetFactory *factory, const gchar *proto)
274 {
275         GObject *obj;
276         GtkWidget *w;
277         ModestStoreWidget *self;
278         ModestStoreWidgetPrivate *priv;
279         
280         g_return_val_if_fail (proto, NULL);
281         g_return_val_if_fail (factory, NULL);
282
283         obj = g_object_new(MODEST_TYPE_STORE_WIDGET, NULL);
284         self = MODEST_STORE_WIDGET(obj);
285         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
286
287         g_object_ref (factory);
288         priv->factory = factory;
289
290         priv->proto = g_strdup (proto);
291         
292         if (strcmp (proto, MODEST_PROTO_POP) == 0 ||
293             strcmp (proto, MODEST_PROTO_IMAP) == 0) {
294                 w = imap_pop_configuration (self);
295         } else if (strcmp (proto, MODEST_PROTO_MAILDIR) == 0) {
296                 w = maildir_configuration (self);
297         }  else if (strcmp (proto, MODEST_PROTO_MBOX) == 0) {
298                 w = mbox_configuration (self);
299         } else
300                 w = gtk_label_new ("");
301         
302         gtk_widget_show_all (w);
303         gtk_box_pack_start (GTK_BOX(self), w, FALSE, FALSE, 2);
304
305         return GTK_WIDGET(self);
306 }
307
308
309 gboolean
310 modest_store_widget_get_remember_password (ModestStoreWidget *self)
311 {
312         ModestStoreWidgetPrivate *priv;
313
314         g_return_val_if_fail (self, FALSE);
315         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
316
317         return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(priv->remember_pwd));
318 }
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         return gtk_entry_get_text (GTK_ENTRY(priv->username));
330 }
331
332 const gchar*
333 modest_store_widget_get_servername (ModestStoreWidget *self)
334 {
335         ModestStoreWidgetPrivate *priv;
336
337         g_return_val_if_fail (self, NULL);
338         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
339         
340         return gtk_entry_get_text (GTK_ENTRY(priv->servername));
341 }
342
343
344 const gchar*
345 modest_store_widget_get_proto (ModestStoreWidget *self)
346 {
347         ModestStoreWidgetPrivate *priv;
348
349         g_return_val_if_fail (self, FALSE);
350         priv = MODEST_STORE_WIDGET_GET_PRIVATE(self);
351
352         return priv->proto;
353 }
354