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