* Add update method to ModestTnyFolderTreeView.
[modest] / src / gtk-glade / 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 static void   on_password_requested (ModestTnyAccountStore *account_store, const gchar *account_name, gpointer user_data);
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         
145         modest_tny_folder_tree_view_update_model(priv->folder_view, account_store);
146 }
147
148
149 GObject*
150 modest_ui_new (ModestConf *modest_conf)
151 {
152         GObject *obj;
153         ModestUIPrivate *priv;
154         ModestAccountMgr *modest_acc_mgr;
155         ModestIdentityMgr *modest_id_mgr;
156         TnyAccountStoreIface *account_store_iface;
157         GSList *account_names_list;
158         GSList *identities_list;
159
160         g_return_val_if_fail (modest_conf, NULL);
161
162         obj = g_object_new(MODEST_TYPE_UI, NULL);
163         priv = MODEST_UI_GET_PRIVATE(obj);
164
165         modest_acc_mgr = MODEST_ACCOUNT_MGR(modest_account_mgr_new (modest_conf));
166         if (!modest_acc_mgr) {
167                 g_warning ("could not create ModestAccountMgr instance");
168                 g_object_unref (obj);
169                 return NULL;
170         }
171
172         modest_id_mgr = MODEST_IDENTITY_MGR(modest_identity_mgr_new (modest_conf));
173         if (!modest_id_mgr) {
174                 g_warning ("could not create ModestIdentityMgr instance");
175                 g_object_unref (obj);
176                 return NULL;
177         }
178
179         account_store_iface =
180                 TNY_ACCOUNT_STORE_IFACE(modest_tny_account_store_new (modest_acc_mgr));
181         if (!account_store_iface) {
182                 g_warning ("could not initialze ModestTnyAccountStore");
183                 return NULL;
184         }
185         g_signal_connect (account_store_iface, "password_requested",
186                           G_CALLBACK(on_password_requested),
187                           NULL);
188         g_signal_connect (account_store_iface, "accounts_reloaded",
189                           G_CALLBACK(on_accounts_reloaded), priv);
190
191         glade_init ();
192         priv->glade_xml = glade_xml_new (MODEST_GLADE, NULL, NULL);
193         if (!priv->glade_xml) {
194                 g_warning ("failed to do glade stuff");
195                 g_object_unref (obj);
196                 return NULL;
197         }
198
199         /* FIXME: could be used, but doesn't work atm.
200          * glade_xml_signal_autoconnect(priv->glade_xml);
201          */
202
203         priv->modest_acc_mgr = modest_acc_mgr;
204         priv->modest_id_mgr  = modest_id_mgr;
205         g_object_ref (priv->modest_conf = modest_conf);
206
207         priv->account_store = account_store_iface;
208
209         priv->modest_window_mgr = MODEST_WINDOW_MGR(modest_window_mgr_new());
210         g_signal_connect (priv->modest_window_mgr, "last_window_closed",
211                           G_CALLBACK(modest_ui_last_window_closed),
212                           NULL);
213
214         account_names_list = modest_account_mgr_server_account_names(modest_acc_mgr,
215                                         NULL, MODEST_PROTO_TYPE_ANY, NULL, FALSE);
216         identities_list = modest_identity_mgr_identity_names(modest_id_mgr, NULL);
217         if (!(account_names_list != NULL || identities_list != NULL))
218                 wizard_account_dialog(MODEST_UI(obj));
219         g_slist_free(account_names_list);
220         g_slist_free(identities_list);
221
222         return obj;
223 }
224
225
226 static void
227 modest_ui_last_window_closed (GObject *obj, gpointer data)
228 {
229         /* FIXME: Other cleanups todo? Finalize Tinymail? */
230         gtk_main_quit ();
231 }
232
233
234 static void
235 on_password_requested (ModestTnyAccountStore *account_store,
236                        const gchar *account_name, gpointer user_data)
237 {
238
239         GtkWidget *passdialog;
240         GtkWidget *vbox;
241         GtkWidget *infolabel;
242         GtkWidget *passentry;
243         gint retval;
244         const gchar *infostring = g_strconcat(_("Please enter the password for "), account_name, ".", NULL);
245
246         passdialog = gtk_dialog_new_with_buttons(_("Password"),
247                                                  NULL,
248                                                  GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
249                                                  GTK_STOCK_OK,
250                                                  GTK_RESPONSE_ACCEPT,
251                                                  GTK_STOCK_CANCEL,
252                                                  GTK_RESPONSE_REJECT,
253                                                  NULL);
254
255         vbox = gtk_vbox_new(FALSE, 0);
256
257         infolabel = gtk_label_new(infostring);
258         passentry = gtk_entry_new();
259
260         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(passdialog)->vbox), infolabel, FALSE, FALSE, 0);
261         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(passdialog)->vbox), passentry, FALSE, FALSE, 0);
262         gtk_widget_show_all(passdialog);
263
264         retval = gtk_dialog_run (GTK_DIALOG(passdialog));
265
266         switch (retval) {
267                 case GTK_RESPONSE_ACCEPT:
268                         modest_account_mgr_set_server_account_string(modest_tny_account_store_get_accout_mgr(account_store),
269                                                              account_name,
270                                                              "password",
271                                                              gtk_entry_get_text(GTK_ENTRY(passentry)),
272                                                              NULL);
273                         break;
274                 case GTK_RESPONSE_CANCEL:
275                         /* FIXME:
276                          * What happens, if canceled?"
277                          */
278                         break;
279         }
280
281         gtk_widget_destroy (passdialog);
282 }
283
284
285 void
286 on_account_selector_selection_changed (GtkWidget *widget, gpointer user_data)
287 {
288         GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
289         GtkTreeIter iter;
290
291         gchar *account_name;
292
293         if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(widget), &iter)) {
294                 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
295                                    0, &account_name, -1);
296         } else {
297                 account_name="empty";
298         }
299
300         g_message("Value: '%s'\n", account_name);
301
302         free(account_name);
303 }
304
305