1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
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"
43 #include <modest-account-mgr.h>
44 #include <modest-account-mgr-helpers.h>
45 #include <modest-runtime.h>
47 #include "modest-mail-operation.h"
48 #include "widgets/modest-header-view-priv.h"
50 /* 'private'/'protected' functions */
51 static void modest_widget_factory_class_init (ModestWidgetFactoryClass *klass);
52 static void modest_widget_factory_init (ModestWidgetFactory *obj);
53 static void modest_widget_factory_finalize (GObject *obj);
63 typedef struct _ModestWidgetFactoryPrivate ModestWidgetFactoryPrivate;
64 struct _ModestWidgetFactoryPrivate {
66 ModestTnyAccountStore *account_store;
68 ModestHeaderView *header_view;
69 ModestFolderView *folder_view;
70 ModestMsgView *msg_preview;
72 GtkWidget *progress_bar;
73 GtkWidget *status_bar;
74 GtkWidget *folder_info_label;
76 GtkWidget *online_toggle;
78 #define MODEST_WIDGET_FACTORY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
79 MODEST_TYPE_WIDGET_FACTORY, \
80 ModestWidgetFactoryPrivate))
82 static GObjectClass *parent_class = NULL;
84 /* uncomment the following if you have defined any signals */
85 /* static guint signals[LAST_SIGNAL] = {0}; */
88 modest_widget_factory_get_type (void)
90 static GType my_type = 0;
92 static const GTypeInfo my_info = {
93 sizeof(ModestWidgetFactoryClass),
95 NULL, /* base finalize */
96 (GClassInitFunc) modest_widget_factory_class_init,
97 NULL, /* class finalize */
98 NULL, /* class data */
99 sizeof(ModestWidgetFactory),
101 (GInstanceInitFunc) modest_widget_factory_init,
104 my_type = g_type_register_static (G_TYPE_OBJECT,
105 "ModestWidgetFactory",
112 modest_widget_factory_class_init (ModestWidgetFactoryClass *klass)
114 GObjectClass *gobject_class;
115 gobject_class = (GObjectClass*) klass;
117 parent_class = g_type_class_peek_parent (klass);
118 gobject_class->finalize = modest_widget_factory_finalize;
120 g_type_class_add_private (gobject_class, sizeof(ModestWidgetFactoryPrivate));
124 modest_widget_factory_init (ModestWidgetFactory *obj)
126 ModestWidgetFactoryPrivate *priv;
127 priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(obj);
129 priv->account_store = NULL;
131 priv->progress_bar = gtk_progress_bar_new ();
132 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(priv->progress_bar),
134 priv->status_bar = gtk_statusbar_new ();
135 gtk_statusbar_set_has_resize_grip (GTK_STATUSBAR(priv->status_bar),
141 modest_widget_factory_finalize (GObject *obj)
143 /* no need to unref account_store; we don't own the reference */
145 G_OBJECT_CLASS(parent_class)->finalize (obj);
150 init_widgets (ModestWidgetFactory *self)
152 ModestWidgetFactoryPrivate *priv;
153 TnyFolderStoreQuery *query;
155 priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
158 query = tny_folder_store_query_new ();
159 tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
161 priv->folder_view = MODEST_FOLDER_VIEW(modest_folder_view_new (priv->account_store,query));
162 if (!priv->folder_view) {
163 g_printerr ("modest: cannot instantiate folder view\n");
166 g_object_unref (G_OBJECT (query));
170 MODEST_HEADER_VIEW(modest_header_view_new (NULL, MODEST_HEADER_VIEW_STYLE_DETAILS));
171 if (!priv->header_view) {
172 g_printerr ("modest: cannot instantiate header view\n");
177 priv->msg_preview = MODEST_MSG_VIEW(modest_msg_view_new (NULL));
178 if (!priv->msg_preview) {
179 g_printerr ("modest: cannot instantiate header view\n");
183 /* online/offline combo */
184 priv->online_toggle = gtk_toggle_button_new ();
186 /* label with number of items, unread items for
187 the current folder */
188 priv->folder_info_label = gtk_label_new (NULL);
195 modest_widget_factory_new (ModestTnyAccountStore *account_store)
198 ModestWidgetFactoryPrivate *priv;
200 g_return_val_if_fail (account_store, NULL);
202 obj = g_object_new (MODEST_TYPE_WIDGET_FACTORY, NULL);
203 priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(obj);
205 priv->account_store = account_store;
207 if (!init_widgets (MODEST_WIDGET_FACTORY(obj))) {
208 g_printerr ("modest: widget factory failed to init widgets\n");
209 g_object_unref (obj);
213 return MODEST_WIDGET_FACTORY(obj);
220 modest_widget_factory_get_folder_view (ModestWidgetFactory *self)
222 g_return_val_if_fail (self, NULL);
223 return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->folder_view;
228 modest_widget_factory_get_header_view (ModestWidgetFactory *self)
230 g_return_val_if_fail (self, NULL);
231 return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->header_view;
236 modest_widget_factory_get_msg_preview (ModestWidgetFactory *self)
238 g_return_val_if_fail (self, NULL);
239 return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->msg_preview;
244 modest_widget_factory_get_account_view (ModestWidgetFactory *self)
246 return modest_account_view_new (modest_runtime_get_account_mgr());
252 modest_widget_factory_get_progress_bar (ModestWidgetFactory *self)
254 g_return_val_if_fail (self, NULL);
255 return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->progress_bar;
260 modest_widget_factory_get_status_bar (ModestWidgetFactory *self)
262 g_return_val_if_fail (self, NULL);
263 return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->status_bar;
269 get_transports (ModestWidgetFactory *self)
271 ModestWidgetFactoryPrivate *priv;
272 ModestAccountMgr *account_mgr;
273 GSList *transports = NULL;
274 GSList *cursor, *accounts;
276 priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
278 account_mgr = modest_runtime_get_account_mgr();
279 cursor = accounts = modest_account_mgr_account_names (account_mgr, NULL);
281 ModestAccountData *data;
282 gchar *account_name = (gchar*)cursor->data;
284 data = modest_account_mgr_get_account_data (account_mgr, account_name);
285 if (data && data->transport_account) {
287 gchar *display_name = g_strdup_printf ("%s (%s)", data->email, account_name);
288 pair = modest_pair_new ((gpointer) data,
289 (gpointer) display_name , TRUE);
290 transports = g_slist_prepend (transports, pair);
292 /* don't free account name; it's freed when the transports list is freed */
293 cursor = cursor->next;
295 g_slist_free (accounts);
303 get_stores (ModestWidgetFactory *self, gboolean only_remote)
305 ModestWidgetFactoryPrivate *priv;
306 TnyAccountStore *account_store;
310 priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
313 tny_platform_factory_new_account_store (priv->fact);
315 stores = tny_simple_list_new ();
316 tny_account_store_get_accounts (account_store, stores,
317 TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
319 /* simply return all the stores */
323 /* remove the non-remote stores from the list */
325 iter = tny_list_create_iterator (stores);
326 while (!tny_iterator_is_done (iter)) {
327 TnyAccount *acc = (TnyAccount*)tny_iterator_get_current(iter);
328 /* is it a local account? if so, remove */
329 ModestProtocol proto = modest_protocol_info_get_protocol (tny_account_get_proto(acc));
330 if (modest_protocol_info_protocol_is_local_store(proto))
331 tny_list_remove (stores, acc); /* FIXME: iter still valid? */
332 tny_iterator_next (iter);
334 g_object_unref (G_OBJECT(iter));
341 modest_widget_factory_get_combo_box (ModestWidgetFactory *self, ModestComboBoxType type)
343 ModestWidgetFactoryPrivate *priv;
344 ModestPairList *protos = NULL;
345 GtkWidget* combo_box;
347 g_return_val_if_fail (self, NULL);
349 priv = MODEST_WIDGET_FACTORY_GET_PRIVATE(self);
352 case MODEST_COMBO_BOX_TYPE_STORE_PROTOS:
353 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_STORE);
355 case MODEST_COMBO_BOX_TYPE_TRANSPORT_PROTOS:
356 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_TRANSPORT);
358 case MODEST_COMBO_BOX_TYPE_SECURITY_PROTOS:
359 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_SECURITY);
361 case MODEST_COMBO_BOX_TYPE_AUTH_PROTOS:
362 protos = modest_protocol_info_get_protocol_pair_list (MODEST_PROTOCOL_TYPE_AUTH);
364 case MODEST_COMBO_BOX_TYPE_TRANSPORTS:
365 protos = (ModestPairList *) get_transports (self);
367 /* case MODEST_COMBO_BOX_TYPE_REMOTE_STORES: */
369 /* list = get_stores (self, TRUE); /\* get all *remote* stores *\/ */
370 /* combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL(list)); */
371 /* g_object_unref (G_OBJECT(list)); */
372 /* //return combo_box; */
374 g_warning ("invalid combo box type: %d", type);
378 combo_box = modest_combo_box_new (protos);
379 g_slist_free (protos);
381 gtk_combo_box_set_active (GTK_COMBO_BOX(combo_box), 0);
389 modest_widget_factory_get_online_toggle (ModestWidgetFactory *self)
391 g_return_val_if_fail (self, NULL);
392 return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->online_toggle;
398 modest_widget_factory_get_folder_info_label (ModestWidgetFactory *self)
400 g_return_val_if_fail (self, NULL);
401 return MODEST_WIDGET_FACTORY_GET_PRIVATE(self)->folder_info_label;