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