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