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