c066febe0267c0eb01c12229866c68f5b1201b42
[modest] / src / modest-widget-factory.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 <gdk/gdkkeysyms.h>
32 #include <tny-gtk-account-list-model.h>
33 #include <tny-gtk-folder-store-tree-model.h>
34 #include <tny-account-store.h>
35 #include <tny-simple-list.h>
36 #include <tny-device.h>
37 #include <tny-folder-store-query.h>
38 #include "modest-widget-factory.h"
39 #include "modest-widget-memory.h"
40 #include <modest-protocol-info.h>
41 #include "modest-tny-platform-factory.h"
42
43 #include <modest-account-mgr.h>
44 #include <modest-account-mgr-helpers.h>
45
46 #include "modest-mail-operation.h"
47 #include "widgets/modest-header-view-priv.h"
48
49 /* 'private'/'protected' functions */
50 static void modest_widget_factory_class_init    (ModestWidgetFactoryClass *klass);
51 static void modest_widget_factory_init          (ModestWidgetFactory *obj);
52 static void modest_widget_factory_finalize      (GObject *obj);
53
54
55 /* list my signals */
56 enum {
57         /* MY_SIGNAL_1, */
58         /* MY_SIGNAL_2, */
59         LAST_SIGNAL
60 };
61
62 typedef struct _ModestWidgetFactoryPrivate ModestWidgetFactoryPrivate;
63 struct _ModestWidgetFactoryPrivate {
64         
65         TnyPlatformFactory          *fact;
66         TnyAccountStore             *account_store;
67         
68         ModestHeaderView            *header_view;
69         ModestFolderView            *folder_view;
70         ModestMsgView               *msg_preview;
71
72         GtkWidget                   *progress_bar;
73         GtkWidget                   *status_bar;
74         GtkWidget                   *folder_info_label;
75
76         GtkWidget                   *online_toggle;
77 };
78 #define MODEST_WIDGET_FACTORY_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
79                                                    MODEST_TYPE_WIDGET_FACTORY, \
80                                                    ModestWidgetFactoryPrivate))
81 /* globals */
82 static GObjectClass *parent_class = NULL;
83
84 /* uncomment the following if you have defined any signals */
85 /* static guint signals[LAST_SIGNAL] = {0}; */
86
87 GType
88 modest_widget_factory_get_type (void)
89 {
90         static GType my_type = 0;
91         if (!my_type) {
92                 static const GTypeInfo my_info = {
93                         sizeof(ModestWidgetFactoryClass),
94                         NULL,           /* base init */
95                         NULL,           /* base finalize */
96                         (GClassInitFunc) modest_widget_factory_class_init,
97                         NULL,           /* class finalize */
98                         NULL,           /* class data */
99                         sizeof(ModestWidgetFactory),
100                         1,              /* n_preallocs */
101                         (GInstanceInitFunc) modest_widget_factory_init,
102                         NULL
103                 };
104                 my_type = g_type_register_static (G_TYPE_OBJECT,
105                                                   "ModestWidgetFactory",
106                                                   &my_info, 0);
107         }
108         return my_type;
109 }
110
111 static void
112 modest_widget_factory_class_init (ModestWidgetFactoryClass *klass)
113 {
114         GObjectClass *gobject_class;
115         gobject_class = (GObjectClass*) klass;
116
117         parent_class            = g_type_class_peek_parent (klass);
118         gobject_class->finalize = modest_widget_factory_finalize;
119
120         g_type_class_add_private (gobject_class, sizeof(ModestWidgetFactoryPrivate));
121 }
122
123 static void
124 modest_widget_factory_init (ModestWidgetFactory *obj)
125 {
126         ModestWidgetFactoryPrivate *priv;
127         priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(obj);
128
129         priv->fact          = modest_tny_platform_factory_get_instance ();
130         priv->account_store = tny_platform_factory_new_account_store (priv->fact);
131         
132         priv->progress_bar = gtk_progress_bar_new ();
133         gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(priv->progress_bar),
134                                        1.0);
135         priv->status_bar   = gtk_statusbar_new ();
136         gtk_statusbar_set_has_resize_grip (GTK_STATUSBAR(priv->status_bar),
137                                            FALSE);
138 }
139
140
141 static void
142 modest_widget_factory_finalize (GObject *obj)
143 {
144         ModestWidgetFactoryPrivate *priv;
145         priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(obj);
146
147         G_OBJECT_CLASS(parent_class)->finalize (obj);
148 }
149
150
151 static gboolean
152 init_widgets (ModestWidgetFactory *self)
153 {
154         ModestWidgetFactoryPrivate *priv;
155         TnyFolderStoreQuery *query;
156
157         priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
158
159         /* folder view */
160         query = tny_folder_store_query_new ();
161         tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
162         if (!(priv->folder_view =
163               MODEST_FOLDER_VIEW(modest_folder_view_new (MODEST_TNY_ACCOUNT_STORE (priv->account_store),
164                                                          query)))) {
165                 g_printerr ("modest: cannot instantiate folder view\n");
166                 return FALSE;
167         }
168         g_object_unref (G_OBJECT (query));
169
170         /* header view */
171         if (!(priv->header_view =
172               MODEST_HEADER_VIEW(modest_header_view_new (NULL,MODEST_HEADER_VIEW_STYLE_DETAILS)))) {
173                 g_printerr ("modest: cannot instantiate header view\n");
174                 return FALSE;
175         }
176                 
177         /* msg preview */
178         if (!(priv->msg_preview = MODEST_MSG_VIEW(modest_msg_view_new (NULL)))) {
179                 g_printerr ("modest: cannot instantiate header view\n");
180                 return FALSE;
181         }
182
183
184
185         /* online/offline combo */
186         priv->online_toggle = gtk_toggle_button_new ();
187
188         /* label with number of items, unread items for 
189            the current folder */
190         priv->folder_info_label = gtk_label_new (NULL);
191         
192 /*      init_signals (self); */
193         
194         return TRUE;
195 }
196
197
198 ModestWidgetFactory*
199 modest_widget_factory_new (void)
200 {
201         GObject *obj;
202         ModestWidgetFactoryPrivate *priv;
203
204         obj  = g_object_new (MODEST_TYPE_WIDGET_FACTORY, NULL); 
205         priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(obj);
206         
207         if (!init_widgets (MODEST_WIDGET_FACTORY(obj))) {
208                 g_printerr ("modest: widget factory failed to init widgets\n");
209                 g_object_unref (obj);
210                 return NULL;
211         }
212         
213         return MODEST_WIDGET_FACTORY(obj);
214 }
215
216
217
218
219 ModestFolderView*
220 modest_widget_factory_get_folder_view (ModestWidgetFactory *self)
221 {
222         g_return_val_if_fail (self, NULL);
223         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->folder_view;
224 }
225
226
227 ModestHeaderView*
228 modest_widget_factory_get_header_view (ModestWidgetFactory *self)
229 {
230         g_return_val_if_fail (self, NULL);
231         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->header_view;
232 }
233
234
235 ModestMsgView*
236 modest_widget_factory_get_msg_preview (ModestWidgetFactory *self)
237 {
238         g_return_val_if_fail (self, NULL);
239         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->msg_preview;
240 }
241
242
243 ModestAccountView*
244 modest_widget_factory_get_account_view (ModestWidgetFactory *self)
245 {
246         ModestWidgetFactoryPrivate *priv;
247         ModestAccountMgr *account_mgr;
248         
249         g_return_val_if_fail (self, NULL);
250         priv =  MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
251
252         account_mgr = modest_tny_platform_factory_get_account_mgr_instance
253                 (MODEST_TNY_PLATFORM_FACTORY(priv->fact));
254         return modest_account_view_new (account_mgr);
255 }
256
257
258
259 GtkWidget*
260 modest_widget_factory_get_progress_bar (ModestWidgetFactory *self)
261 {
262         g_return_val_if_fail (self, NULL);
263         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->progress_bar;
264 }
265
266
267 GtkWidget*
268 modest_widget_factory_get_status_bar (ModestWidgetFactory *self)
269 {
270         g_return_val_if_fail (self, NULL);
271         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->status_bar;
272 }
273
274
275
276 static const GSList*
277 get_transports (ModestWidgetFactory *self)
278 {
279         ModestWidgetFactoryPrivate *priv;
280         ModestAccountMgr *account_mgr;
281         GSList *transports = NULL;
282         GSList *cursor, *accounts;
283         
284         priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
285
286         account_mgr =
287                 modest_tny_platform_factory_get_account_mgr_instance
288                 (MODEST_TNY_PLATFORM_FACTORY(priv->fact));
289         cursor = accounts = modest_account_mgr_account_names (account_mgr, NULL);
290         while (cursor) {
291                 ModestAccountData *data;
292                 gchar *account_name = (gchar*)cursor->data;
293
294                 data = modest_account_mgr_get_account_data (account_mgr, account_name);
295                 if (data && data->transport_account) {
296                         ModestPair *pair;
297                         gchar *display_name = g_strdup_printf ("%s (%s)", data->email, account_name);
298                         pair = modest_pair_new ((gpointer) data,
299                                                 (gpointer) display_name , TRUE);
300                         transports = g_slist_prepend (transports, pair);
301                 }
302                 /* don't free account name; it's freed when the transports list is freed */
303                 cursor = cursor->next;
304         }
305         g_slist_free (accounts);
306         
307         return transports;
308 }
309
310
311 #if 0
312 static const GSList*
313 get_stores (ModestWidgetFactory *self, gboolean only_remote)
314 {
315         ModestWidgetFactoryPrivate *priv;
316         TnyAccountStore *account_store;
317         TnyList *stores;
318         TnyIterator *iter;
319         
320         priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
321
322         account_store =
323                 tny_platform_factory_new_account_store (priv->fact);                    
324
325         stores = tny_simple_list_new ();
326         tny_account_store_get_accounts (account_store, stores,
327                                          TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
328
329         /* simply return all the stores */
330         if (!only_remote)
331                 return stores;
332
333         /*  remove the non-remote stores from the list */
334         if (only_remote) {
335                 iter = tny_list_create_iterator (stores);
336                 while (!tny_iterator_is_done (iter)) {
337                         TnyAccount *acc = (TnyAccount*)tny_iterator_get_current(iter);
338                         /* is it a local account? if so, remove */
339                         ModestProtocol proto = modest_protocol_info_get_protocol (tny_account_get_proto(acc));
340                         if (modest_protocol_info_protocol_is_local_store(proto))
341                                 tny_list_remove (stores, acc); /* FIXME: iter still valid? */
342                         tny_iterator_next (iter);
343                 }
344                 g_object_unref (G_OBJECT(iter));
345         }
346         return stores;          
347 }
348 #endif
349
350 GtkWidget*
351 modest_widget_factory_get_combo_box (ModestWidgetFactory *self, ModestComboBoxType type)
352 {
353         ModestWidgetFactoryPrivate *priv;
354         ModestPairList *protos = NULL;
355         GtkWidget* combo_box;
356         
357         g_return_val_if_fail (self, NULL);
358
359         priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
360         
361         switch (type) {
362         case MODEST_COMBO_BOX_TYPE_STORE_PROTOS:
363                 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_STORE);
364                 break;
365         case MODEST_COMBO_BOX_TYPE_TRANSPORT_PROTOS:
366                 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_TRANSPORT);
367                 break;
368         case MODEST_COMBO_BOX_TYPE_SECURITY_PROTOS:
369                 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_SECURITY);
370                 break;
371         case MODEST_COMBO_BOX_TYPE_AUTH_PROTOS:
372                 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_AUTH);
373                 break;
374         case MODEST_COMBO_BOX_TYPE_TRANSPORTS:
375                 protos = (ModestPairList *) get_transports (self);
376                 break;
377 /*      case MODEST_COMBO_BOX_TYPE_REMOTE_STORES: */
378 /*              // FIXME */
379 /*              list = get_stores (self, TRUE); /\* get all *remote* stores *\/ */
380 /*              combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL(list)); */
381 /*              g_object_unref (G_OBJECT(list)); */
382 /*              //return combo_box; */
383         default:
384                 g_warning ("invalid combo box type: %d", type);
385                 return NULL;
386         }
387
388         combo_box = modest_combo_box_new (protos);
389         g_slist_free (protos);
390         
391         gtk_combo_box_set_active (GTK_COMBO_BOX(combo_box), 0);
392         
393         return combo_box;
394 }
395
396
397
398 GtkWidget*
399 modest_widget_factory_get_online_toggle (ModestWidgetFactory *self)
400 {
401         g_return_val_if_fail (self, NULL);
402         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->online_toggle;
403 }
404
405
406
407 GtkWidget*
408 modest_widget_factory_get_folder_info_label (ModestWidgetFactory *self)
409 {
410         g_return_val_if_fail (self, NULL);
411         return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->folder_info_label;
412 }