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