78b91db0a1f8dfe4d7d3c598fc8ab251db0f3e67
[modest] / src / hildon / modest-ui.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
31 /* modest-ui.c */
32
33 #include <gtk/gtk.h>
34 #include <glade/glade.h>
35 #include <glib/gi18n.h>
36 #include <string.h>
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif /*HAVE_CONFIG_H*/
41
42 /* TODO: put in auto* */
43 #include <tny-text-buffer-stream.h>
44 #include <tny-msg-folder.h>
45
46 #include "../modest-ui.h"
47 #include "../modest-window-mgr.h"
48 #include "../modest-account-mgr.h"
49 #include "../modest-account-mgr.h"
50 #include "../modest-identity-mgr.h"
51
52 #include "../modest-tny-account-store.h"
53 #include "../modest-tny-folder-tree-view.h"
54 #include "../modest-tny-header-tree-view.h"
55 #include "../modest-tny-msg-view.h"
56 #include "../modest-tny-transport-actions.h"
57 #include "../modest-tny-store-actions.h"
58
59 #include "../modest-text-utils.h"
60 #include "../modest-tny-msg-actions.h"
61
62 #include "../modest-editor-window.h"
63
64 #include "modest-ui-glade.h"
65 #include "modest-ui-wizard.h"
66
67 /* 'private'/'protected' functions */
68 static void   modest_ui_class_init     (ModestUIClass *klass);
69 static void   modest_ui_init           (ModestUI *obj);
70 static void   modest_ui_finalize       (GObject *obj);
71
72 static void   modest_ui_window_destroy    (GtkWidget *win, GdkEvent *event, gpointer data);
73 static void   modest_ui_last_window_closed (GObject *obj, gpointer data);
74
75 gchar *on_password_requested (TnyAccountIface *, const gchar *, gboolean *);
76
77 /* list my signals */
78 enum {
79         /* MY_SIGNAL_1, */
80         /* MY_SIGNAL_2, */
81         LAST_SIGNAL
82 };
83
84 /* globals */
85 static GObjectClass *parent_class = NULL;
86
87
88 GType
89 modest_ui_get_type (void)
90 {
91         static GType my_type = 0;
92         if (!my_type) {
93                 static const GTypeInfo my_info = {
94                         sizeof(ModestUIClass),
95                         NULL,           /* base init */
96                         NULL,           /* base finalize */
97                         (GClassInitFunc) modest_ui_class_init,
98                         NULL,           /* class finalize */
99                         NULL,           /* class data */
100                         sizeof(ModestUI),
101                         1,              /* n_preallocs */
102                         (GInstanceInitFunc) modest_ui_init,
103                 };
104                 my_type = g_type_register_static (G_TYPE_OBJECT,
105                                                   "ModestUI",
106                                                   &my_info, 0);
107         }
108         return my_type;
109 }
110
111
112 static void
113 modest_ui_class_init (ModestUIClass *klass)
114 {
115         GObjectClass *gobject_class;
116         gobject_class = (GObjectClass*) klass;
117
118         parent_class            = g_type_class_peek_parent (klass);
119         gobject_class->finalize = modest_ui_finalize;
120
121         g_type_class_add_private (gobject_class, sizeof(ModestUIPrivate));
122
123 }
124
125
126 static void
127 modest_ui_init (ModestUI *obj)
128 {
129         ModestUIPrivate *priv = MODEST_UI_GET_PRIVATE(obj);
130
131         priv->modest_acc_mgr    = NULL;
132         priv->modest_id_mgr     = NULL;
133         priv->modest_conf       = NULL;
134         priv->modest_window_mgr = NULL;
135         priv->glade_xml         = NULL;
136         priv->folder_view       = NULL;
137         priv->header_view       = NULL;
138         priv->message_view      = NULL;
139         priv->current_folder    = NULL;
140         priv->program           = NULL;
141 }
142
143
144 static void
145 modest_ui_finalize (GObject *obj)
146 {
147         ModestUIPrivate *priv = MODEST_UI_GET_PRIVATE(obj);
148
149         if (priv->modest_acc_mgr)
150                 g_object_unref (priv->modest_acc_mgr);
151         priv->modest_acc_mgr = NULL;
152
153         if (priv->modest_id_mgr)
154                 g_object_unref (priv->modest_id_mgr);
155         priv->modest_id_mgr = NULL;
156
157         if (priv->modest_conf)
158                 g_object_unref (priv->modest_conf);
159         priv->modest_conf = NULL;
160
161         if (priv->modest_window_mgr)
162                 g_object_unref (priv->modest_window_mgr);
163         priv->modest_window_mgr = NULL;
164 }
165
166
167 static void
168 on_accounts_reloaded (ModestTnyAccountStore *account_store, gpointer user_data)
169 {
170         ModestUIPrivate *priv = user_data;
171
172         g_return_if_fail (MODEST_IS_TNY_FOLDER_TREE_VIEW (priv->folder_view));
173         g_return_if_fail (MODEST_IS_TNY_HEADER_TREE_VIEW (priv->header_view));
174
175         modest_tny_header_tree_view_set_folder (priv->header_view, NULL);
176
177         modest_tny_folder_tree_view_update_model(priv->folder_view, account_store);
178 }
179
180
181 GObject*
182 modest_ui_new (ModestConf *modest_conf)
183 {
184         GObject *obj;
185         ModestUIPrivate *priv;
186         ModestAccountMgr *modest_acc_mgr;
187         ModestIdentityMgr *modest_id_mgr;
188         TnyAccountStoreIface *account_store_iface;
189         GSList *account_names_list;
190         GSList *identities_list;
191
192         g_return_val_if_fail (modest_conf, NULL);
193
194         obj = g_object_new(MODEST_TYPE_UI, NULL);
195         priv = MODEST_UI_GET_PRIVATE(obj);
196
197         modest_acc_mgr = MODEST_ACCOUNT_MGR(modest_account_mgr_new (modest_conf));
198         if (!modest_acc_mgr) {
199                 g_warning ("could not create ModestAccountMgr instance");
200                 g_object_unref (obj);
201                 return NULL;
202         }
203
204         modest_id_mgr = MODEST_IDENTITY_MGR(modest_identity_mgr_new (modest_conf));
205         if (!modest_id_mgr) {
206                 g_warning ("could not create ModestIdentityMgr instance");
207                 g_object_unref (obj);
208                 return NULL;
209         }
210
211         account_store_iface =
212                 TNY_ACCOUNT_STORE_IFACE(modest_tny_account_store_new (modest_acc_mgr));
213         if (!account_store_iface) {
214                 g_warning ("could not initialze ModestTnyAccountStore");
215                 return NULL;
216         }
217
218         modest_tny_account_store_set_get_pass_func(MODEST_TNY_ACCOUNT_STORE(account_store_iface),
219                                                    on_password_requested);
220
221         g_signal_connect (account_store_iface, "accounts_reloaded",
222                           G_CALLBACK(on_accounts_reloaded), priv);
223
224         glade_init ();
225         priv->glade_xml = glade_xml_new (MODEST_GLADE, NULL, NULL);
226         if (!priv->glade_xml) {
227                 g_warning ("failed to do glade stuff");
228                 g_object_unref (obj);
229                 return NULL;
230         }
231
232         /* FIXME: could be used, but doesn't work atm.
233          * glade_xml_signal_autoconnect(priv->glade_xml);
234          */
235
236         priv->modest_acc_mgr = modest_acc_mgr;
237         priv->modest_id_mgr  = modest_id_mgr;
238         g_object_ref (priv->modest_conf = modest_conf);
239
240         priv->account_store = account_store_iface;
241
242         priv->modest_window_mgr = MODEST_WINDOW_MGR(modest_window_mgr_new());
243         g_signal_connect (priv->modest_window_mgr, "last_window_closed",
244                           G_CALLBACK(modest_ui_last_window_closed),
245                           NULL);
246
247         account_names_list = modest_account_mgr_server_account_names(modest_acc_mgr,
248                                         NULL, MODEST_PROTO_TYPE_ANY, NULL, FALSE);
249         identities_list = modest_identity_mgr_identity_names(modest_id_mgr, NULL);
250         if (!(account_names_list != NULL || identities_list != NULL))
251                 wizard_account_dialog(MODEST_UI(obj));
252         g_slist_free(account_names_list);
253         g_slist_free(identities_list);
254
255         return obj;
256 }
257
258
259 static void
260 modest_ui_last_window_closed (GObject *obj, gpointer data)
261 {
262         /* FIXME: Other cleanups todo? Finalize Tinymail? */
263         gtk_main_quit ();
264 }
265
266
267 gchar *
268 on_password_requested (TnyAccountIface *account,
269                        const gchar *prompt,
270                        gboolean *cancel) {
271
272         GtkWidget *passdialog;
273         GtkWidget *vbox;
274         GtkWidget *infoscroll;
275         GtkWidget *infolabel;
276         GtkWidget *passentry;
277         GtkTextBuffer *infobuffer;
278         gchar *retval;
279         gint result;
280
281         passdialog = gtk_dialog_new_with_buttons(_("Password"),
282                                                  NULL,
283                                                  GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
284                                                  GTK_STOCK_OK,
285                                                  GTK_RESPONSE_ACCEPT,
286                                                  GTK_STOCK_CANCEL,
287                                                  GTK_RESPONSE_REJECT,
288                                                  NULL);
289
290         vbox = gtk_vbox_new(FALSE, 0);
291
292         infobuffer = gtk_text_buffer_new (NULL);
293         gtk_text_buffer_set_text(infobuffer, prompt, -1);
294         infoscroll = gtk_scrolled_window_new(NULL, NULL);
295         infolabel = gtk_text_view_new_with_buffer(infobuffer);
296         gtk_container_add(GTK_CONTAINER(infoscroll), infolabel);
297         passentry = gtk_entry_new();
298         gtk_entry_set_visibility(GTK_ENTRY(passentry), FALSE);
299
300         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(passdialog)->vbox), infoscroll, FALSE, FALSE, 0);
301         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(passdialog)->vbox), passentry, FALSE, FALSE, 0);
302         gtk_widget_show_all(passdialog);
303
304         result = gtk_dialog_run (GTK_DIALOG(passdialog));
305
306         switch (result) {
307         case GTK_RESPONSE_ACCEPT:
308                 retval = g_strdup(gtk_entry_get_text(GTK_ENTRY(passentry)));
309                 *cancel=FALSE;
310                 break;
311         default:
312                 retval = g_strdup("");;
313                 *cancel=TRUE;
314                 break;
315         }
316
317         gtk_widget_hide(passdialog);
318         gtk_widget_destroy (passdialog);
319         while (gtk_events_pending()){
320                 g_message("iterating over pending events");
321                 gtk_main_iteration();
322         }
323
324         return retval;
325 }
326
327
328 void
329 on_account_selector_selection_changed (GtkWidget *widget, gpointer user_data)
330 {
331         GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
332         GtkTreeIter iter;
333
334         gchar *account_name;
335
336         if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(widget), &iter)) {
337                 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
338                                    0, &account_name, -1);
339         } else {
340                 account_name="empty";
341         }
342
343         g_message("Value: '%s'\n", account_name);
344
345         free(account_name);
346 }