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