a34aac5a153e75dcbd908909be7b09adcb76e1aa
[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         /* signal definitions go here, e.g.: */
96 /*      signals[MY_SIGNAL_1] = */
97 /*              g_signal_new ("my_signal_1",....); */
98 /*      signals[MY_SIGNAL_2] = */
99 /*              g_signal_new ("my_signal_2",....); */
100 /*      etc. */
101 }
102
103
104 static void
105 modest_ui_init (ModestUI *obj)
106 {
107         ModestUIPrivate *priv = MODEST_UI_GET_PRIVATE(obj);
108
109         priv->modest_acc_mgr    = NULL;
110         priv->modest_id_mgr     = NULL;
111         priv->modest_conf       = NULL;
112         priv->modest_window_mgr = NULL;
113         priv->glade_xml         = NULL;
114         priv->folder_view       = NULL;
115         priv->header_view       = NULL;
116         priv->message_view      = NULL;
117         priv->current_folder    = NULL;
118 }
119
120
121 static void
122 modest_ui_finalize (GObject *obj)
123 {
124         ModestUIPrivate *priv = MODEST_UI_GET_PRIVATE(obj);
125
126         if (priv->modest_acc_mgr)
127                 g_object_unref (priv->modest_acc_mgr);
128         priv->modest_acc_mgr = NULL;
129
130         if (priv->modest_id_mgr)
131                 g_object_unref (priv->modest_id_mgr);
132         priv->modest_id_mgr = NULL;
133
134         if (priv->modest_conf)
135                 g_object_unref (priv->modest_conf);
136         priv->modest_conf = NULL;
137
138         if (priv->modest_window_mgr)
139                 g_object_unref (priv->modest_window_mgr);
140         priv->modest_window_mgr = NULL;
141 }
142
143
144 GObject*
145 modest_ui_new (ModestConf *modest_conf)
146 {
147         GObject *obj;
148         ModestUIPrivate *priv;
149         ModestAccountMgr *modest_acc_mgr;
150         ModestIdentityMgr *modest_id_mgr;
151         TnyAccountStoreIface *account_store_iface;
152         GSList *account_names_list;
153         GSList *identities_list;
154
155         g_return_val_if_fail (modest_conf, NULL);
156
157         obj = g_object_new(MODEST_TYPE_UI, NULL);
158         priv = MODEST_UI_GET_PRIVATE(obj);
159
160         modest_acc_mgr = MODEST_ACCOUNT_MGR(modest_account_mgr_new (modest_conf));
161         if (!modest_acc_mgr) {
162                 g_warning ("could not create ModestAccountMgr instance");
163                 g_object_unref (obj);
164                 return NULL;
165         }
166
167         modest_id_mgr = MODEST_IDENTITY_MGR(modest_identity_mgr_new (modest_conf));
168         if (!modest_id_mgr) {
169                 g_warning ("could not create ModestIdentityMgr instance");
170                 g_object_unref (obj);
171                 return NULL;
172         }
173
174         account_store_iface =
175                 TNY_ACCOUNT_STORE_IFACE(modest_tny_account_store_new (modest_acc_mgr));
176         if (!account_store_iface) {
177                 g_warning ("could not initialze ModestTnyAccountStore");
178                 return NULL;
179         }
180         g_signal_connect (account_store_iface, "password_requested",
181                           G_CALLBACK(on_password_requested),
182                           NULL);
183
184         glade_init ();
185         priv->glade_xml = glade_xml_new (MODEST_GLADE, NULL, NULL);
186         if (!priv->glade_xml) {
187                 g_warning ("failed to do glade stuff");
188                 g_object_unref (obj);
189                 return NULL;
190         }
191
192         /* FIXME: could be used, but doesn't work atm.
193          * glade_xml_signal_autoconnect(priv->glade_xml);
194          */
195
196         priv->modest_acc_mgr = modest_acc_mgr;
197         priv->modest_id_mgr  = modest_id_mgr;
198         g_object_ref (priv->modest_conf = modest_conf);
199
200         priv->account_store = account_store_iface;
201
202         priv->modest_window_mgr = MODEST_WINDOW_MGR(modest_window_mgr_new());
203         g_signal_connect (priv->modest_window_mgr, "last_window_closed",
204                           G_CALLBACK(modest_ui_last_window_closed),
205                           NULL);
206
207         account_names_list = modest_account_mgr_server_account_names(modest_acc_mgr,
208                                         NULL, MODEST_PROTO_TYPE_ANY, NULL, FALSE);
209         identities_list = modest_identity_mgr_identity_names(modest_id_mgr, NULL);
210         if (!(account_names_list != NULL || identities_list != NULL))
211                 wizard_account_dialog(MODEST_UI(obj));
212         g_slist_free(account_names_list);
213         g_slist_free(identities_list);
214
215         return obj;
216 }
217
218
219 static void
220 modest_ui_last_window_closed (GObject *obj, gpointer data)
221 {
222         /* FIXME: Other cleanups todo? Finalize Tinymail? */
223         gtk_main_quit ();
224 }
225
226
227 static void
228 on_password_requested (ModestTnyAccountStore *account_store,
229                        const gchar *account_name, gpointer user_data)
230 {
231
232         GtkWidget *passdialog;
233         GtkWidget *vbox;
234         GtkWidget *infolabel;
235         GtkWidget *passentry;
236         gint retval;
237         const gchar *infostring = g_strconcat(_("Please enter the password for "), account_name, ".", NULL);
238
239         passdialog = gtk_dialog_new_with_buttons(_("Password"),
240                                                  NULL,
241                                                  GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
242                                                  GTK_STOCK_OK,
243                                                  GTK_RESPONSE_ACCEPT,
244                                                  GTK_STOCK_CANCEL,
245                                                  GTK_RESPONSE_REJECT,
246                                                  NULL);
247
248         vbox = gtk_vbox_new(FALSE, 0);
249
250         infolabel = gtk_label_new(infostring);
251         passentry = gtk_entry_new();
252
253         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(passdialog)->vbox), infolabel, FALSE, FALSE, 0);
254         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(passdialog)->vbox), passentry, FALSE, FALSE, 0);
255         gtk_widget_show_all(passdialog);
256
257         retval = gtk_dialog_run (GTK_DIALOG(passdialog));
258
259         switch (retval) {
260                 case GTK_RESPONSE_ACCEPT:
261                         modest_account_mgr_set_server_account_string(modest_tny_account_store_get_accout_mgr(account_store),
262                                                              account_name,
263                                                              "password",
264                                                              gtk_entry_get_text(GTK_ENTRY(passentry)),
265                                                              NULL);
266                         break;
267                 case GTK_RESPONSE_CANCEL:
268                         /* FIXME:
269                          * What happens, if canceled?"
270                          */
271                         break;
272         }
273
274         gtk_widget_destroy (passdialog);
275 }
276
277
278 void
279 on_account_selector_selection_changed (GtkWidget *widget, gpointer user_data)
280 {
281         GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
282         GtkTreeIter iter;
283
284         gchar *account_name;
285
286         if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(widget), &iter)) {
287                 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
288                                    0, &account_name, -1);
289         } else {
290                 account_name="empty";
291         }
292
293         g_message("Value: '%s'\n", account_name);
294
295         free(account_name);
296 }
297
298