549b6b2e2ecc1eecc3dcadc0de8d3ab4a89b8cb5
[modest] / src / hildon / modest-ui-account-setup.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 <strings.h>
31 #include <gtk/gtk.h>
32 #include <glade/glade.h>
33 #include <glib/gi18n.h>
34 #include <string.h>
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif /*HAVE_CONFIG_H*/
39
40 #include "../modest-account-mgr.h"
41 #include "../modest-identity-mgr.h"
42
43 #include "modest-ui-glade.h"
44 #include "modest-ui-account-setup.h"
45
46 enum {
47         IDENTITY_NAME,
48         IDENTITY_ADDRESS,
49         IDENTITY_VIA,
50         IDENTITY_COLUMNS
51 };
52
53 enum {
54         ACCOUNT_NAME,
55         ACCOUNT_HOST,
56         ACCOUNT_PROT,
57         ACCOUNT_COLUMNS
58 };
59
60 typedef struct _CallbackData CallbackData;
61
62 struct _CallbackData {
63         GtkTreeView     *id_tree_view;
64         GtkTreeView     *acc_tree_view;
65         ModestUI        *modest_ui;
66         GladeXML        *glade_xml;
67 };
68
69 static void
70 identity_setup_dialog (ModestUI *, GtkTreeModel *, gchar *);
71
72 static void
73 account_setup_dialog (ModestUI *, gchar *);
74
75 static void
76 missing_notification(GtkWindow *, gchar *);
77
78 static GtkTreeModel *
79 create_identities_model(ModestIdentityMgr *);
80
81 static GtkTreeModel *
82 create_accounts_model(ModestAccountMgr *);
83
84 static void
85 refresh_identities(ModestIdentityMgr *,
86                    GladeXML *);
87
88 static void
89 refresh_accounts(ModestAccountMgr *, GladeXML *glade_xml);
90
91 /* CALLBACKS */
92
93 static gboolean
94 filter_transports (GtkTreeModel *model,
95                    GtkTreeIter *iter,
96                    gpointer userdata) {
97
98         gchar *name;
99         gboolean retval;
100
101         gtk_tree_model_get(model,
102                            iter,
103                            ACCOUNT_PROT, &name,
104                            -1);
105
106         retval = strcasecmp(name, "SMTP")==0;
107         g_free(name);
108         return retval;
109 }
110
111
112 static void
113 account_edit_action(GtkWidget *button,
114                     gpointer userdata) {
115
116         CallbackData *cb_data;
117         GtkTreeModel *acc_liststore;
118         GtkTreeSelection *selection;
119         GtkTreeIter selected_iter;
120         gchar *account_name;
121
122         cb_data = (CallbackData *) userdata;
123         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(cb_data->acc_tree_view));
124
125         gtk_tree_selection_get_selected(selection,
126                                         &acc_liststore,
127                                         &selected_iter);
128         gtk_tree_model_get(GTK_TREE_MODEL(acc_liststore),
129                            &selected_iter,
130                            ACCOUNT_NAME, &account_name,
131                            -1);
132
133         account_setup_dialog (cb_data->modest_ui, account_name);
134         g_free(account_name);
135 }
136
137 static void
138 account_create_action(GtkWidget *button,
139                       gpointer userdata) {
140         CallbackData *cb_data;
141
142         cb_data = (CallbackData *) userdata;
143
144         account_setup_dialog(cb_data->modest_ui, NULL);
145 }
146
147 static void
148 account_delete_action(GtkWidget *button,
149                       gpointer userdata) {
150         CallbackData *cb_data;
151         GtkTreeSelection *selection;
152         GtkTreeIter selected_iter;
153         GtkTreeModel *acc_liststore;
154         GtkWidget *confirmation_dialog;
155         GtkWidget *confirmation_message;
156         ModestUIPrivate *priv;
157         gchar *account_name;
158         gchar *message;
159         gint result;
160
161         cb_data = (CallbackData *) userdata;
162         priv = MODEST_UI_GET_PRIVATE(cb_data->modest_ui);
163         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(cb_data->acc_tree_view));
164
165         gtk_tree_selection_get_selected(selection,
166                                         &acc_liststore,
167                                         &selected_iter);
168         gtk_tree_model_get(GTK_TREE_MODEL(acc_liststore),
169                            &selected_iter,
170                            ACCOUNT_NAME, &account_name,
171                            -1);
172
173         confirmation_dialog = gtk_dialog_new_with_buttons ("Confirm removal of account",
174                                                            GTK_WINDOW(gtk_widget_get_toplevel(button)),
175                                                            GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
176                                                            GTK_STOCK_OK,
177                                                            GTK_RESPONSE_OK,
178                                                            GTK_STOCK_CANCEL,
179                                                            GTK_RESPONSE_CANCEL,
180                                                            NULL);
181
182         message = g_strdup_printf("Remove selected account '%s'?", account_name);
183         confirmation_message = gtk_label_new_with_mnemonic (message);
184
185         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(confirmation_dialog)->vbox), confirmation_message, FALSE, FALSE, 10);
186
187         gtk_widget_show_all(confirmation_dialog);
188
189         result=gtk_dialog_run(GTK_DIALOG(confirmation_dialog));
190         if (result==GTK_RESPONSE_OK) {
191                 modest_account_mgr_remove_account(priv->modest_acc_mgr,
192                                                   account_name, TRUE,
193                                                   NULL);
194         }
195
196         gtk_widget_destroy(confirmation_dialog);
197         g_free(account_name);
198         g_free(message);
199 }
200
201
202 static void
203 identity_edit_action(GtkWidget *button,
204                      gpointer userdata)
205 {
206         CallbackData *cb_data;
207         GtkTreeModel *acc_liststore;
208         GtkTreeModel *id_liststore;
209         GtkTreeSelection *selection;
210         GtkTreeIter selected_iter;
211         gchar *identity_name;
212
213         cb_data = (CallbackData *) userdata;
214         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(cb_data->id_tree_view));
215
216         gtk_tree_selection_get_selected(selection,
217                                         &id_liststore,
218                                         &selected_iter);
219         gtk_tree_model_get(GTK_TREE_MODEL(id_liststore),
220                            &selected_iter,
221                            IDENTITY_NAME, &identity_name,
222                            -1);
223         /* We use the available tree model from the accounts page to display a selection
224          * of transports in the identities. Since we only want the transport accounts,
225          * we derive a gtk_tree_model_filter and apply filter_transports function.
226          */
227         acc_liststore = gtk_tree_model_filter_new(gtk_tree_view_get_model(cb_data->acc_tree_view),
228                                                   NULL);
229         gtk_tree_model_filter_set_visible_func(GTK_TREE_MODEL_FILTER(acc_liststore),
230                                                filter_transports,
231                                                NULL,
232                                                NULL);
233
234         identity_setup_dialog (cb_data->modest_ui, acc_liststore, identity_name);
235         g_free(identity_name);
236 }
237
238 static void
239 identity_create_action(GtkWidget *button,
240                        gpointer userdata)
241 {
242         CallbackData *cb_data;
243         GtkTreeModel *acc_liststore;
244
245         cb_data = (CallbackData *) userdata;
246
247         /* Works as in identity_edit_action. */
248         acc_liststore = gtk_tree_model_filter_new(gtk_tree_view_get_model(cb_data->acc_tree_view),
249                                                   NULL);
250         gtk_tree_model_filter_set_visible_func(GTK_TREE_MODEL_FILTER(acc_liststore),
251                                                filter_transports,
252                                                NULL,
253                                                NULL);
254
255         identity_setup_dialog(cb_data->modest_ui, acc_liststore, NULL);
256         g_object_unref(acc_liststore);
257 }
258
259 static void
260 identity_delete_action(GtkWidget *button,
261                        gpointer userdata)
262 {
263         CallbackData *cb_data;
264         GtkTreeSelection *selection;
265         GtkTreeIter selected_iter;
266         GtkTreeModel *id_liststore;
267         GtkWidget *confirmation_dialog;
268         GtkWidget *confirmation_message;
269         ModestUIPrivate *priv;
270         gchar *identity_name;
271         gchar *message;
272         gint result;
273
274         cb_data = (CallbackData *) userdata;
275         priv = MODEST_UI_GET_PRIVATE(cb_data->modest_ui);
276         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(cb_data->id_tree_view));
277
278         gtk_tree_selection_get_selected(selection,
279                                         &id_liststore,
280                                         &selected_iter);
281         gtk_tree_model_get(GTK_TREE_MODEL(id_liststore),
282                            &selected_iter,
283                            ACCOUNT_NAME, &identity_name,
284                            -1);
285
286         confirmation_dialog = gtk_dialog_new_with_buttons ("Confirm removal of identity",
287                                                            GTK_WINDOW(gtk_widget_get_toplevel(button)),
288                                                            GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
289                                                            GTK_STOCK_OK,
290                                                            GTK_RESPONSE_OK,
291                                                            GTK_STOCK_CANCEL,
292                                                            GTK_RESPONSE_CANCEL,
293                                                            NULL);
294
295         message = g_strdup_printf("Remove selected identity '%s'?", identity_name);
296         confirmation_message = gtk_label_new_with_mnemonic (message);
297
298         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(confirmation_dialog)->vbox), confirmation_message, FALSE, FALSE, 10);
299
300         gtk_widget_show_all(confirmation_dialog);
301
302         result=gtk_dialog_run(GTK_DIALOG(confirmation_dialog));
303         if (result==GTK_RESPONSE_OK)
304         {
305                 modest_identity_mgr_remove_identity(priv->modest_id_mgr,
306                                                     identity_name,
307                                                     NULL);
308         }
309
310         gtk_widget_destroy(confirmation_dialog);
311         g_free(identity_name);
312         g_free(message);
313 }
314
315 static void
316 activate_buttons_on_identity(GtkTreeView *tree_view,
317                              gpointer user_data)
318 {
319         GtkWidget *button;
320         GladeXML *glade_xml;
321
322         glade_xml=(GladeXML *) user_data;
323
324         button = glade_xml_get_widget(GLADE_XML(glade_xml), "IdentityEditButton");
325         gtk_widget_set_sensitive(GTK_WIDGET(button), TRUE);
326         button = glade_xml_get_widget(GLADE_XML(glade_xml), "IdentityDeleteButton");
327         gtk_widget_set_sensitive(GTK_WIDGET(button), TRUE);
328 }
329
330 static void
331 activate_buttons_on_account(GtkTreeView *tree_view,
332                             gpointer user_data)
333 {
334         GtkWidget *button;
335         GladeXML *glade_xml;
336
337         glade_xml=(GladeXML *) user_data;
338
339         button = glade_xml_get_widget(GLADE_XML(glade_xml), "AccountEditButton");
340         gtk_widget_set_sensitive(GTK_WIDGET(button), TRUE);
341         button = glade_xml_get_widget(GLADE_XML(glade_xml), "AccountDeleteButton");
342         gtk_widget_set_sensitive(GTK_WIDGET(button), TRUE);
343 }
344
345 static void
346 use_sig_toggled(GtkToggleButton *button,
347                 gpointer userdata) {
348
349         GtkWidget *awidget;
350         GladeXML *glade_xml = (GladeXML *) userdata;
351
352         awidget=glade_xml_get_widget(glade_xml, "ISSignatureTextView");
353         gtk_widget_set_sensitive(awidget, gtk_toggle_button_get_active(button));
354 }
355
356 static void
357 refresh_accounts_on_add(ModestAccountMgr *modest_acc_mgr,
358                         void *nu1,
359                         gpointer userdata)
360 {
361         refresh_accounts(modest_acc_mgr, (GladeXML *) userdata);
362 }
363
364 static void
365 refresh_accounts_on_remove(ModestAccountMgr *modest_acc_mgr,
366                         void *nu1,
367                         gpointer userdata) {
368
369         GladeXML *glade_xml = (GladeXML *) userdata;
370         GtkWidget *button;
371
372         refresh_accounts(modest_acc_mgr, (GladeXML *) userdata);
373         /* Since we loose the selection through the delete operation, we need to
374          * change the buttons sensitivity .
375          */
376         button = glade_xml_get_widget(GLADE_XML(glade_xml), "AccountEditButton");
377         gtk_widget_set_sensitive(GTK_WIDGET(button), FALSE);
378         button = glade_xml_get_widget(GLADE_XML(glade_xml), "AccountDeleteButton");
379         gtk_widget_set_sensitive(GTK_WIDGET(button), FALSE);
380 }
381
382 static void
383 refresh_accounts_on_change(ModestAccountMgr *modest_acc_mgr,
384                            void *nu1,
385                            void *nu2,
386                            void *nu3,
387                            gpointer userdata)
388 {
389         refresh_accounts(modest_acc_mgr, (GladeXML *) userdata);
390 }
391
392 static void
393 refresh_identities_on_add(ModestIdentityMgr *modest_id_mgr,
394                           void *nu1,
395                           gpointer userdata) {
396         refresh_identities(modest_id_mgr, (GladeXML *) userdata);
397 }
398
399 static void
400 refresh_identities_on_remove(ModestIdentityMgr *modest_id_mgr,
401                              void *nu1,
402                              gpointer userdata) {
403
404         GladeXML *glade_xml = (GladeXML *) userdata;
405         GtkWidget *button;
406
407         refresh_identities(modest_id_mgr, glade_xml);
408
409         /* Since we loose the selection through the delete operation, we need to
410          * change the buttons sensitivity .
411          */
412         button = glade_xml_get_widget(GLADE_XML(glade_xml), "IdentityEditButton");
413         gtk_widget_set_sensitive(GTK_WIDGET(button), FALSE);
414         button = glade_xml_get_widget(GLADE_XML(glade_xml), "IdentityDeleteButton");
415         gtk_widget_set_sensitive(GTK_WIDGET(button), FALSE);
416 }
417
418 static void
419 refresh_identities_on_change(ModestIdentityMgr *modest_id_mgr,
420                              void *nu1,
421                              void *nu2,
422                              void *nu3,
423                              gpointer userdata) {
424         refresh_identities(modest_id_mgr, (GladeXML *) userdata);
425 }
426
427 /* METHODS */
428
429 static gboolean
430 search_model_column_for_string_advanced(GtkTreeModel *model, GtkTreeIter *iter, gint ColNum, gchar *search, gboolean mcase) {
431
432         gchar *tmptext;
433         gboolean iter_true;
434
435         iter_true = gtk_tree_model_get_iter_first(model, iter);
436         while (iter_true) {
437                 gtk_tree_model_get(model,
438                                    iter,
439                                    ColNum, &tmptext,
440                                    -1);
441                 if ((mcase && strcasecmp(tmptext, search)==0)
442                     || strcmp(tmptext, search)==0) {
443                         g_free(tmptext);
444                         break;
445                 }
446                 g_free(tmptext);
447                 iter_true = gtk_tree_model_iter_next(model, iter);
448                 if (!iter_true) {
449                         break;
450                 }
451         }
452         return iter_true;
453 }
454
455 static gboolean
456 search_model_column_for_string(GtkTreeModel *model, GtkTreeIter *iter, gint ColNum, gchar *search) {
457         return search_model_column_for_string_advanced(model, iter, ColNum, search, FALSE);
458 }
459
460 static gboolean
461 case_search_model_column_for_string(GtkTreeModel *model, GtkTreeIter *iter, gint ColNum, gchar *search) {
462         return search_model_column_for_string_advanced(model, iter, ColNum, search, TRUE);
463 }
464
465 static void
466 refresh_identities(ModestIdentityMgr *modest_id_mgr,
467                    GladeXML *glade_xml) {
468
469         GtkTreeModel *id_liststore;
470         GtkTreeView *id_treeview;
471
472         id_treeview = GTK_TREE_VIEW(glade_xml_get_widget(glade_xml, "IdentitiesTreeview"));
473
474         id_liststore=create_identities_model(modest_id_mgr);
475         gtk_tree_view_set_model(GTK_TREE_VIEW(id_treeview), id_liststore);
476 }
477
478 static void
479 refresh_accounts(ModestAccountMgr *modest_acc_mgr,
480                  GladeXML *glade_xml) {
481
482         GtkTreeModel *acc_liststore;
483         GtkTreeView *acc_treeview;
484
485         acc_treeview = GTK_TREE_VIEW(glade_xml_get_widget(glade_xml, "AccountsTreeview"));
486
487         acc_liststore=create_accounts_model(modest_acc_mgr);
488         gtk_tree_view_set_model(GTK_TREE_VIEW(acc_treeview), acc_liststore);
489 }
490
491 static void
492 missing_notification(GtkWindow *parent, gchar *info_message) {
493
494         GtkWidget *DenyDialog;
495
496         DenyDialog=gtk_message_dialog_new(parent,
497                                           GTK_DIALOG_MODAL,
498                                           GTK_MESSAGE_INFO,
499                                           GTK_BUTTONS_OK,
500                                           "%s",
501                                           info_message);
502
503         gtk_dialog_run(GTK_DIALOG(DenyDialog));
504
505         gtk_widget_destroy(DenyDialog);
506 }
507
508 static gboolean
509 write_identity(GladeXML *glade_xml, ModestUI *modest_ui, GtkTreeModel *accounts_model, gboolean newidentity) {
510
511         GtkTextBuffer *sigbuff;
512         GtkTextIter start_iter;
513         GtkTextIter end_iter;
514         GtkTreeIter transport_iter;
515         ModestUIPrivate *priv;
516         const gchar *identity;
517         gchar *reply_to;
518         gchar *signature;
519         gchar *transport;
520
521         priv = MODEST_UI_GET_PRIVATE(MODEST_UI(modest_ui));
522
523         reply_to = g_strdup_printf("%s", gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "ISReplyToEntry"))));
524
525         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(glade_xml_get_widget(glade_xml, "ISUseSignatureCheckButton"))))
526         {
527                 sigbuff = gtk_text_view_get_buffer(GTK_TEXT_VIEW(glade_xml_get_widget(glade_xml, "ISSignatureTextView")));
528                 gtk_text_buffer_get_bounds(sigbuff,
529                                            &start_iter,
530                                            &end_iter);
531                 signature = gtk_text_buffer_get_text(sigbuff,
532                                                      &start_iter,
533                                                      &end_iter,
534                                                      FALSE);
535         }
536         else
537                 signature = NULL;
538
539         if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(glade_xml_get_widget(glade_xml, "ISOutServerComboBox")), &transport_iter)) {
540                 gtk_tree_model_get(GTK_TREE_MODEL(accounts_model),
541                                    &transport_iter,
542                                    ACCOUNT_NAME, &transport,
543                                    -1);
544         }
545         else {
546                 missing_notification(NULL, "Please select an outgoing server!");
547                 return FALSE;
548         }
549
550         identity = gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "ISIdentityEntry")));
551
552         if (newidentity) {
553
554                 if (modest_identity_mgr_add_identity (priv->modest_id_mgr,
555                                                       identity,
556                                                       NULL,
557                                                       gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "ISEMailAddress"))),
558                                                       NULL,
559                                                       NULL,
560                                                       FALSE,
561                                                       NULL,
562                                                       FALSE));
563                 else
564                         return FALSE;
565         }
566         if (!modest_identity_mgr_set_identity_string(priv->modest_id_mgr,
567                                                      identity,
568                                                      MODEST_IDENTITY_REALNAME,
569                                                      gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "ISNameEntry"))),
570                                                      NULL))
571                 return FALSE;
572         if (!modest_identity_mgr_set_identity_string(priv->modest_id_mgr,
573                                                      identity,
574                                                      MODEST_IDENTITY_EMAIL,
575                                                      gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "ISEMailAddress"))),
576                                                      NULL))
577                 return FALSE;
578         if (!modest_identity_mgr_set_identity_string(priv->modest_id_mgr,
579                                                      identity,
580                                                      MODEST_IDENTITY_REPLYTO,
581                                                      reply_to,
582                                                      NULL))
583                 return FALSE;
584         if (!modest_identity_mgr_set_identity_string(priv->modest_id_mgr,
585                                                      identity,
586                                                      MODEST_IDENTITY_SIGNATURE,
587                                                      signature,
588                                                      NULL))
589                 return FALSE;
590         if (!modest_identity_mgr_set_identity_bool(priv->modest_id_mgr,
591                                                    identity,
592                                                    MODEST_IDENTITY_USE_SIGNATURE,
593                                                    gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(glade_xml_get_widget(glade_xml,
594                                                                                                                        "ISUseSignatureCheckButton"))),
595                                                    NULL))
596                 return FALSE;
597         if (!modest_identity_mgr_set_identity_string(priv->modest_id_mgr,
598                                                      identity,
599                                                      MODEST_IDENTITY_ID_VIA,
600                                                      transport,
601                                                      NULL))
602                 return FALSE;
603         if (!modest_identity_mgr_set_identity_bool(priv->modest_id_mgr,
604                                                    identity,
605                                                    MODEST_IDENTITY_USE_ID_VIA,
606                                                    TRUE, /* FIXME: for now */
607                                                    NULL))
608                 return FALSE;
609         g_free(transport);
610         return TRUE;
611 }
612
613 static gboolean
614 write_account(GladeXML *glade_xml, ModestUI *modest_ui, gboolean newaccount) {
615
616         ModestUIPrivate *priv;
617         const gchar *account;
618         gchar *protocol;
619         gchar *tmptext;
620         gint retval;
621
622         priv = MODEST_UI_GET_PRIVATE(MODEST_UI(modest_ui));
623
624         account = gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "ASDisplaynameEntry")));
625
626         if (newaccount) {
627                 tmptext = gtk_combo_box_get_active_text(GTK_COMBO_BOX(glade_xml_get_widget(glade_xml, "ASProtocolComboBox")));
628                 protocol = g_utf8_strdown(tmptext, -1);
629                 g_free(tmptext);
630
631                 retval = modest_account_mgr_add_server_account (priv->modest_acc_mgr,
632                                                                 account,
633                                                                 gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "ASHostnameEntry"))),
634                                                                 gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "ASUsernameEntry"))),
635                                                                 gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "ASPasswordEntry"))),
636                                                                 protocol);
637                 g_free(protocol);
638                 return retval;
639         }
640         if (!modest_account_mgr_set_string(priv->modest_acc_mgr,
641                                            account,
642                                            MODEST_ACCOUNT_HOSTNAME,
643                                            gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "ASHostnameEntry"))),
644                                            TRUE,
645                                            NULL))
646                 
647                 return FALSE;
648
649         if (!modest_account_mgr_set_string(priv->modest_acc_mgr,
650                                            account,
651                                            MODEST_ACCOUNT_USERNAME,
652                                            gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "ASUsernameEntry"))),
653                                            TRUE,
654                                            NULL))
655                 return FALSE;
656         
657         if (!modest_account_mgr_set_string(priv->modest_acc_mgr,
658                                            account,
659                                            MODEST_ACCOUNT_PASSWORD,
660                                            gtk_entry_get_text(GTK_ENTRY(glade_xml_get_widget(glade_xml, "ASPasswordEntry"))),
661                                            TRUE,
662                                            NULL))
663                 return FALSE;
664         
665         return TRUE;
666 }
667
668 static void
669 identity_setup_dialog (ModestUI *modest_ui, GtkTreeModel *accounts_model, gchar *identity)
670 {
671
672         GladeXML *glade_xml;
673         GtkWidget *id_dialog;
674         GtkWidget *outgoing_server;
675         GtkWidget *awidget;
676         GtkCellRenderer *renderer;
677         ModestIdentityMgr *id_mgr;
678         GtkTextBuffer *sigbuff;
679         GtkTreeIter out_iter;
680         gchar *outacc_name;
681         gchar *tmptext;
682         gint identity_added_successfully;
683         gint result;
684         gboolean newidentity;
685
686         glade_xml = glade_xml_new(MODEST_GLADE, "IdentitySetupDialog", NULL);
687         id_dialog = glade_xml_get_widget(glade_xml, "IdentitySetupDialog");
688
689         outgoing_server = glade_xml_get_widget(glade_xml, "ISOutServerComboBox");
690         gtk_combo_box_set_model(GTK_COMBO_BOX(outgoing_server), GTK_TREE_MODEL(accounts_model));
691         renderer = gtk_cell_renderer_text_new ();
692         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (outgoing_server), renderer, TRUE);
693         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (outgoing_server), renderer,
694                                         "text", 0,
695                                         NULL);
696         awidget=glade_xml_get_widget(glade_xml, "ISUseSignatureCheckButton");
697         g_signal_connect(awidget,
698                          "toggled",
699                          G_CALLBACK(use_sig_toggled),
700                          glade_xml);
701
702         newidentity = identity==NULL;
703         if (!newidentity) {
704                 id_mgr = MODEST_UI_GET_PRIVATE(modest_ui)->modest_id_mgr;
705
706                 outacc_name = modest_identity_mgr_get_identity_string(id_mgr,
707                                                                       identity,
708                                                                       MODEST_IDENTITY_ID_VIA,
709                                                                       NULL);
710                 if (search_model_column_for_string(GTK_TREE_MODEL(accounts_model),
711                                                    &out_iter,
712                                                    ACCOUNT_NAME,
713                                                    outacc_name))
714                         gtk_combo_box_set_active_iter(GTK_COMBO_BOX(outgoing_server), &out_iter);
715                 
716                 awidget=glade_xml_get_widget(glade_xml, "ISIdentityEntry");
717                 gtk_widget_set_sensitive(awidget, FALSE);
718                 gtk_entry_set_text(GTK_ENTRY(awidget), identity);
719                 tmptext = modest_identity_mgr_get_identity_string(id_mgr,
720                                                                   identity,
721                                                                   MODEST_IDENTITY_EMAIL,
722                                                                   NULL);
723                 awidget=glade_xml_get_widget(glade_xml, "ISEMailAddress");
724                 gtk_entry_set_text(GTK_ENTRY(awidget), tmptext);
725                 g_free(tmptext);
726
727                 if (modest_identity_mgr_get_identity_bool(id_mgr,
728                                                           identity,
729                                                           MODEST_IDENTITY_USE_SIGNATURE,
730                                                           NULL)) {
731                         awidget=glade_xml_get_widget(glade_xml, "ISUseSignatureCheckButton");
732                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(awidget), TRUE);
733                         awidget=glade_xml_get_widget(glade_xml, "ISSignatureTextView");
734                         gtk_widget_set_sensitive(awidget, TRUE);
735                 }
736
737                 sigbuff=gtk_text_buffer_new(NULL);
738                 tmptext = modest_identity_mgr_get_identity_string(id_mgr,
739                                                                   identity,
740                                                                   MODEST_IDENTITY_SIGNATURE,
741                                                                   NULL),
742                 gtk_text_buffer_set_text(sigbuff, tmptext, -1);
743                 gtk_text_view_set_buffer(GTK_TEXT_VIEW(glade_xml_get_widget(glade_xml, "ISSignatureTextView")),
744                                                        sigbuff);
745                 g_object_unref(sigbuff);
746                 g_free(tmptext);
747
748                 tmptext = modest_identity_mgr_get_identity_string(id_mgr,
749                                                                   identity,
750                                                                   MODEST_IDENTITY_EMAIL,
751                                                                   NULL);
752                 awidget=glade_xml_get_widget(glade_xml, "ISEMailAddress");
753                 gtk_entry_set_text(GTK_ENTRY(awidget), tmptext);
754                 g_free(tmptext);
755
756                 tmptext = modest_identity_mgr_get_identity_string(id_mgr,
757                                                                   identity,
758                                                                   MODEST_IDENTITY_REALNAME,
759                                                                   NULL);
760                 awidget=glade_xml_get_widget(glade_xml, "ISNameEntry");
761                 gtk_entry_set_text(GTK_ENTRY(awidget), tmptext);
762                 g_free(tmptext);
763
764                 tmptext = modest_identity_mgr_get_identity_string(id_mgr,
765                                                                   identity,
766                                                                   MODEST_IDENTITY_REPLYTO,
767                                                                   NULL);
768                 awidget=glade_xml_get_widget(glade_xml, "ISReplyToEntry");
769                 gtk_entry_set_text(GTK_ENTRY(awidget), tmptext);
770
771                 g_free(tmptext);
772                 g_free(outacc_name);
773         }
774
775         gtk_widget_show_all(id_dialog);
776
777         do {
778                 result=gtk_dialog_run(GTK_DIALOG(id_dialog));
779
780                 switch (result) {
781                 case GTK_RESPONSE_OK:
782                         identity_added_successfully = write_identity(glade_xml, modest_ui, accounts_model, newidentity);
783                         break;
784                 default:
785                         identity_added_successfully = FALSE;
786                         break;
787                 }
788         } while(result!=GTK_RESPONSE_DELETE_EVENT && result!=GTK_RESPONSE_CANCEL && identity_added_successfully!=TRUE);
789
790         gtk_widget_destroy(id_dialog);
791         g_object_unref(glade_xml);
792 }
793
794 static void
795 account_setup_dialog (ModestUI *modest_ui, gchar *account) {
796
797         GladeXML *glade_xml;
798         GtkWidget *acc_dialog;
799         GtkWidget *awidget;
800         ModestAccountMgr *acc_mgr;
801         GtkTreeModel *typemodel;
802         GtkTreeIter proto_iter;
803         gchar *tmptext;
804         gint account_added_successfully;
805         gint result;
806         gboolean newaccount;
807
808         glade_xml = glade_xml_new(MODEST_GLADE, "AccountSetupDialog", NULL);
809         acc_dialog = glade_xml_get_widget(glade_xml, "AccountSetupDialog");
810
811         newaccount = account==NULL;
812         if (!newaccount) {
813                 acc_mgr = MODEST_UI_GET_PRIVATE(modest_ui)->modest_acc_mgr;
814
815                 awidget=glade_xml_get_widget(glade_xml, "ASDisplaynameEntry");
816                 gtk_widget_set_sensitive(awidget, FALSE);
817                 gtk_entry_set_text(GTK_ENTRY(awidget), account);
818
819                 tmptext = modest_account_mgr_get_string(acc_mgr,
820                                                         account,
821                                                         MODEST_ACCOUNT_PROTO,
822                                                         TRUE, NULL);
823                 awidget=glade_xml_get_widget(glade_xml, "ASProtocolComboBox");
824                 gtk_widget_set_sensitive(awidget, FALSE);
825                 typemodel = gtk_combo_box_get_model(GTK_COMBO_BOX(awidget));
826                 if (case_search_model_column_for_string(typemodel, &proto_iter, 0, tmptext))
827                         gtk_combo_box_set_active_iter(GTK_COMBO_BOX(awidget), &proto_iter);
828
829                 g_free(tmptext);
830
831                 tmptext = modest_account_mgr_get_string(acc_mgr,
832                                                         account,
833                                                         MODEST_ACCOUNT_HOSTNAME,
834                                                         TRUE, NULL);
835                 awidget=glade_xml_get_widget(glade_xml, "ASHostnameEntry");
836                 gtk_entry_set_text(GTK_ENTRY(awidget), tmptext);
837                 g_free(tmptext);
838
839                 tmptext = modest_account_mgr_get_string(acc_mgr,
840                                                         account,
841                                                         MODEST_ACCOUNT_USERNAME,
842                                                         TRUE, NULL);
843                 awidget=glade_xml_get_widget(glade_xml, "ASUsernameEntry");
844                 gtk_entry_set_text(GTK_ENTRY(awidget), tmptext);
845                 g_free(tmptext);
846
847                 tmptext = modest_account_mgr_get_string(acc_mgr,
848                                                         account,
849                                                         MODEST_ACCOUNT_PASSWORD,
850                                                         TRUE, NULL);
851                 awidget=glade_xml_get_widget(glade_xml, "ASPasswordEntry");
852                 gtk_entry_set_text(GTK_ENTRY(awidget), tmptext);
853                 g_free(tmptext);
854         }
855
856         gtk_widget_show_all(acc_dialog);
857
858         do {
859                 result=gtk_dialog_run(GTK_DIALOG(acc_dialog));
860
861                 switch (result) {
862                 case GTK_RESPONSE_OK:
863                         account_added_successfully = write_account(glade_xml, modest_ui, newaccount);
864
865                         break;
866                 default:
867                         account_added_successfully = FALSE;
868                         break;
869                 }
870         } while(result!=GTK_RESPONSE_DELETE_EVENT && result!=GTK_RESPONSE_CANCEL && account_added_successfully!=TRUE);
871
872         gtk_widget_destroy(acc_dialog);
873         g_object_unref(glade_xml);
874 }
875
876
877 static CallbackData *
878 setup_callback_data(GtkTreeView *id_tree_view,
879                     GtkTreeView *acc_tree_view,
880                     ModestUI *modest_ui,
881                     GladeXML *glade_xml)
882 {
883         CallbackData *self;
884         self = g_malloc(sizeof(CallbackData));
885         self->modest_ui=modest_ui;
886         self->id_tree_view=id_tree_view;
887         self->acc_tree_view=acc_tree_view;
888         self->glade_xml=glade_xml;
889         return self;
890 }
891
892 static void
893 free_callback_data(CallbackData *data)
894 {
895         g_free(data);
896 }
897
898 static GtkTreeModel *
899 create_identities_model(ModestIdentityMgr *id_mgr) {
900
901         GSList *id_names_list;
902         GSList *id_names_list_iter;
903         GtkListStore *id_list_store;
904         GtkTreeIter id_list_store_iter;
905         gchar *tmptext1;
906         gchar *tmptext2;
907
908         id_names_list = modest_identity_mgr_identity_names(id_mgr, NULL);
909         id_list_store = gtk_list_store_new(IDENTITY_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
910
911         for (id_names_list_iter=id_names_list;
912              id_names_list_iter!=NULL;
913              id_names_list_iter=g_slist_next(id_names_list_iter)) {
914                 gtk_list_store_append(id_list_store, &id_list_store_iter);
915                 tmptext1=modest_identity_mgr_get_identity_string(id_mgr,
916                                                                  id_names_list_iter->data,
917                                                                  MODEST_IDENTITY_EMAIL,
918                                                                  NULL);
919                 tmptext2=modest_identity_mgr_get_identity_string(id_mgr,
920                                                                  id_names_list_iter->data,
921                                                                  MODEST_IDENTITY_ID_VIA,
922                                                                  NULL);
923                 gtk_list_store_set(id_list_store, &id_list_store_iter,
924                                    IDENTITY_NAME, id_names_list_iter->data,
925                                    IDENTITY_ADDRESS, tmptext1,
926                                    IDENTITY_VIA, tmptext2,
927                                    -1);
928                 g_free(tmptext1);
929                 g_free(tmptext2);
930         }
931
932         g_slist_free(id_names_list);
933
934         return GTK_TREE_MODEL(id_list_store);
935 }
936
937 static GtkTreeModel *
938 create_accounts_model(ModestAccountMgr *acc_mgr) {
939
940         GSList *acc_names_list;
941         GSList *acc_names_list_iter;
942         GtkListStore *acc_list_store;
943         GtkTreeIter acc_list_store_iter;
944         gchar *hostname;
945         gchar *protocol;
946
947         acc_names_list = modest_account_mgr_search_server_accounts (acc_mgr,
948                                                                     NULL,
949                                                                     MODEST_PROTO_TYPE_ANY,
950                                                                     NULL,
951                                                                     FALSE);
952         acc_list_store = gtk_list_store_new(ACCOUNT_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
953
954         for (acc_names_list_iter=acc_names_list;
955              acc_names_list_iter!=NULL;
956              acc_names_list_iter=g_slist_next(acc_names_list_iter)) {
957                 gtk_list_store_append(acc_list_store, &acc_list_store_iter);
958                 hostname=modest_account_mgr_get_string(acc_mgr,
959                                                        acc_names_list_iter->data,
960                                                        MODEST_ACCOUNT_HOSTNAME,
961                                                        TRUE,
962                                                        NULL);
963                 protocol=modest_account_mgr_get_string(acc_mgr,
964                                                        acc_names_list_iter->data,
965                                                        MODEST_ACCOUNT_PROTO,
966                                                        TRUE,
967                                                        NULL);
968                 gtk_list_store_set(acc_list_store, &acc_list_store_iter,
969                                    ACCOUNT_NAME, acc_names_list_iter->data,
970                                    ACCOUNT_HOST, hostname,
971                                    ACCOUNT_PROT, protocol,
972                                    -1);
973                 g_free(hostname);
974                 g_free(protocol);
975         }
976
977         g_slist_free(acc_names_list);
978
979         return GTK_TREE_MODEL(acc_list_store);
980 }
981
982
983 static void
984 accounts_and_identities_dialog (gpointer user_data)
985 {
986         GladeXML *glade_xml;
987         GtkWidget *main_dialog;
988         GtkTreeView *identities_tree_view;
989         GtkTreeView *accounts_tree_view;
990         ModestUI *modest_ui;
991         ModestUIPrivate *priv;
992         gint sig_coll[6];
993         gint retval;
994         GtkCellRenderer *renderer;
995         GtkTreeViewColumn *column;
996         CallbackData *cb_data;
997         GtkWidget *abutton;
998
999         g_return_if_fail(MODEST_IS_UI(user_data));
1000         modest_ui = (ModestUI *) user_data;
1001         priv = MODEST_UI_GET_PRIVATE(modest_ui);
1002
1003         glade_xml = glade_xml_new(MODEST_GLADE, "IdentitiesAndAccountsDialog", NULL);
1004         main_dialog = glade_xml_get_widget(glade_xml, "IdentitiesAndAccountsDialog");
1005
1006         /* Accounts */
1007         accounts_tree_view = GTK_TREE_VIEW(glade_xml_get_widget(glade_xml, "AccountsTreeview"));
1008         /* Account -> Accountname */
1009         renderer = gtk_cell_renderer_text_new ();
1010         column = gtk_tree_view_column_new_with_attributes ("Account",
1011                                                            renderer,
1012                                                            "text", ACCOUNT_NAME,
1013                                                            NULL);
1014         gtk_tree_view_append_column (accounts_tree_view, column);
1015         /* Account -> Hostname */
1016         renderer = gtk_cell_renderer_text_new ();
1017         column = gtk_tree_view_column_new_with_attributes ("Hostname",
1018                                                            renderer,
1019                                                            "text", ACCOUNT_HOST,
1020                                                            NULL);
1021         gtk_tree_view_append_column (accounts_tree_view, column);
1022
1023         /* Identities */
1024         identities_tree_view = GTK_TREE_VIEW(glade_xml_get_widget(glade_xml, "IdentitiesTreeview"));
1025
1026         /* Identities -> Identityname */
1027         renderer = gtk_cell_renderer_text_new ();
1028         column = gtk_tree_view_column_new_with_attributes ("Identity",
1029                                                            renderer,
1030                                                            "text", IDENTITY_NAME,
1031                                                            NULL);
1032         gtk_tree_view_append_column (identities_tree_view, column);
1033
1034         /* Identities -> E-mail address */
1035         renderer = gtk_cell_renderer_text_new ();
1036         column = gtk_tree_view_column_new_with_attributes ("E-mail address",
1037                                                            renderer,
1038                                                            "text", IDENTITY_ADDRESS,
1039                                                            NULL);
1040         gtk_tree_view_append_column (identities_tree_view, column);
1041
1042         /* Identities -> Relay */
1043         renderer = gtk_cell_renderer_text_new ();
1044         column = gtk_tree_view_column_new_with_attributes ("Transport",
1045                                                            renderer,
1046                                                            "text", IDENTITY_VIA,
1047                                                            NULL);
1048         gtk_tree_view_append_column (identities_tree_view, column);
1049
1050         cb_data=setup_callback_data(identities_tree_view, accounts_tree_view, modest_ui, glade_xml);
1051
1052         refresh_accounts(priv->modest_acc_mgr,
1053                          glade_xml);
1054
1055         refresh_identities(priv->modest_id_mgr,
1056                            glade_xml);
1057
1058         /* Identities */
1059         abutton=glade_xml_get_widget(glade_xml, "IdentityCreateButton");
1060         g_signal_connect(abutton,
1061                          "clicked",
1062                          G_CALLBACK(identity_create_action),
1063                          cb_data);
1064         abutton=glade_xml_get_widget(glade_xml, "IdentityEditButton");
1065         g_signal_connect(abutton,
1066                          "clicked",
1067                          G_CALLBACK(identity_edit_action),
1068                          cb_data);
1069         abutton=glade_xml_get_widget(glade_xml, "IdentityDeleteButton");
1070         g_signal_connect(abutton,
1071                          "clicked",
1072                          G_CALLBACK(identity_delete_action),
1073                          cb_data);
1074
1075         /* Accounts */
1076         abutton=glade_xml_get_widget(glade_xml, "AccountCreateButton");
1077         g_signal_connect(abutton,
1078                          "clicked",
1079                          G_CALLBACK(account_create_action),
1080                          cb_data);
1081         abutton=glade_xml_get_widget(glade_xml, "AccountEditButton");
1082         g_signal_connect(abutton,
1083                          "clicked",
1084                          G_CALLBACK(account_edit_action),
1085                          cb_data);
1086         abutton=glade_xml_get_widget(glade_xml, "AccountDeleteButton");
1087         g_signal_connect(abutton,
1088                          "clicked",
1089                          G_CALLBACK(account_delete_action),
1090                          cb_data);
1091
1092         g_signal_connect(glade_xml_get_widget(glade_xml, "IdentitiesTreeview"),
1093                          "cursor-changed",
1094                          G_CALLBACK(activate_buttons_on_identity),
1095                          glade_xml);
1096         g_signal_connect(glade_xml_get_widget(glade_xml, "AccountsTreeview"),
1097                          "cursor-changed",
1098                          G_CALLBACK(activate_buttons_on_account),
1099                          glade_xml);
1100
1101         sig_coll[0] = g_signal_connect(priv->modest_id_mgr,
1102                                        "identity-change",
1103                                        G_CALLBACK(refresh_identities_on_change),
1104                                        glade_xml);
1105         sig_coll[1] = g_signal_connect(priv->modest_id_mgr,
1106                                        "identity-add",
1107                                        G_CALLBACK(refresh_identities_on_add),
1108                                        glade_xml);
1109         sig_coll[2] = g_signal_connect(priv->modest_id_mgr,
1110                                        "identity-remove",
1111                                        G_CALLBACK(refresh_identities_on_remove),
1112                                        glade_xml);
1113
1114         sig_coll[3] = g_signal_connect(priv->modest_acc_mgr,
1115                                        "account-change",
1116                                        G_CALLBACK(refresh_accounts_on_change),
1117                                        glade_xml);
1118         sig_coll[4] = g_signal_connect(priv->modest_acc_mgr,
1119                                        "account-add",
1120                                        G_CALLBACK(refresh_accounts_on_add),
1121                                        glade_xml);
1122         sig_coll[5] = g_signal_connect(priv->modest_acc_mgr,
1123                                        "account-remove",
1124                                        G_CALLBACK(refresh_accounts_on_remove),
1125                                        glade_xml);
1126
1127         gtk_widget_show_all(GTK_WIDGET(main_dialog));
1128
1129         retval=gtk_dialog_run(GTK_DIALOG(main_dialog));
1130
1131         g_signal_handler_disconnect(priv->modest_id_mgr, sig_coll[0]);
1132         g_signal_handler_disconnect(priv->modest_id_mgr, sig_coll[1]);
1133         g_signal_handler_disconnect(priv->modest_id_mgr, sig_coll[2]);
1134         g_signal_handler_disconnect(priv->modest_acc_mgr, sig_coll[3]);
1135         g_signal_handler_disconnect(priv->modest_acc_mgr, sig_coll[4]);
1136         g_signal_handler_disconnect(priv->modest_acc_mgr, sig_coll[5]);
1137
1138         gtk_widget_destroy(GTK_WIDGET(main_dialog));
1139         free_callback_data(cb_data);
1140         g_object_unref(glade_xml);
1141 }
1142
1143 void account_settings (GtkWidget *widget,
1144                        gpointer user_data)
1145 {
1146         accounts_and_identities_dialog (MODEST_UI(user_data));
1147 }
1148