* Change the default accounts view dialog, to allow cancelling pressing
[modest] / src / hildon2 / modest-account-view-window.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
32
33 #include <widgets/modest-account-view-window.h>
34 #include <widgets/modest-account-view.h>
35
36 #include <modest-runtime.h>
37 #include "modest-platform.h"
38 #include "modest-account-protocol.h"
39 #include <modest-account-mgr-helpers.h>
40 #include <string.h>
41 #include "modest-tny-platform-factory.h"
42 #include "modest-easysetup-wizard-dialog.h"
43 #include "modest-account-settings-dialog.h"
44 #include <modest-utils.h>
45 #include "widgets/modest-ui-constants.h"
46
47 /* 'private'/'protected' functions */
48 static void                            modest_account_view_window_class_init   (ModestAccountViewWindowClass *klass);
49 static void                            modest_account_view_window_init         (ModestAccountViewWindow *obj);
50 static void                            modest_account_view_window_finalize     (GObject *obj);
51
52 /* list my signals */
53 enum {
54         /* MY_SIGNAL_1, */
55         /* MY_SIGNAL_2, */
56         LAST_SIGNAL
57 };
58
59 typedef struct _ModestAccountViewWindowPrivate ModestAccountViewWindowPrivate;
60 struct _ModestAccountViewWindowPrivate {
61         GtkWidget           *new_button;
62         GtkWidget           *edit_button;
63         GtkWidget           *delete_button;
64         GtkWidget           *close_button;
65         ModestAccountView   *account_view;
66         guint acc_removed_handler;
67 };
68 #define MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
69                                                         MODEST_TYPE_ACCOUNT_VIEW_WINDOW, \
70                                                         ModestAccountViewWindowPrivate))
71 /* globals */
72 static GtkDialogClass *parent_class = NULL;
73
74 /* uncomment the following if you have defined any signals */
75 /* static guint signals[LAST_SIGNAL] = {0}; */
76
77 GType
78 modest_account_view_window_get_type (void)
79 {
80         static GType my_type = 0;
81         if (!my_type) {
82                 static const GTypeInfo my_info = {
83                         sizeof(ModestAccountViewWindowClass),
84                         NULL,           /* base init */
85                         NULL,           /* base finalize */
86                         (GClassInitFunc) modest_account_view_window_class_init,
87                         NULL,           /* class finalize */
88                         NULL,           /* class data */
89                         sizeof(ModestAccountViewWindow),
90                         1,              /* n_preallocs */
91                         (GInstanceInitFunc) modest_account_view_window_init,
92                         NULL
93                 };
94                 my_type = g_type_register_static (GTK_TYPE_DIALOG,
95                                                   "ModestAccountViewWindow",
96                                                   &my_info, 0);
97         }
98         return my_type;
99 }
100
101 static void
102 modest_account_view_window_class_init (ModestAccountViewWindowClass *klass)
103 {
104         GObjectClass *gobject_class;
105         gobject_class = (GObjectClass*) klass;
106
107         parent_class            = g_type_class_peek_parent (klass);
108         gobject_class->finalize = modest_account_view_window_finalize;
109
110         g_type_class_add_private (gobject_class, sizeof(ModestAccountViewWindowPrivate));
111 }
112
113 static void
114 modest_account_view_window_finalize (GObject *self)
115 {
116         ModestAccountViewWindowPrivate *priv;
117         ModestAccountMgr *mgr;
118         
119         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE (self);
120         mgr = modest_runtime_get_account_mgr ();
121
122         if (g_signal_handler_is_connected (mgr, priv->acc_removed_handler))
123                 g_signal_handler_disconnect (mgr, priv->acc_removed_handler);
124         priv->acc_removed_handler = 0;
125
126         G_OBJECT_CLASS(parent_class)->finalize (self);
127 }
128
129
130 static void
131 on_selection_changed (GtkTreeSelection *sel, ModestAccountViewWindow *self)
132 {
133         ModestAccountViewWindowPrivate *priv;
134         GtkTreeModel                   *model;
135         GtkTreeIter                     iter;
136         gboolean                        has_selection;
137         
138         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
139
140         has_selection =
141                 gtk_tree_selection_get_selected (sel, &model, &iter);
142
143         /* Set the status of the buttons */
144         gtk_widget_set_sensitive (priv->edit_button, has_selection);
145         gtk_widget_set_sensitive (priv->delete_button, has_selection);
146 }
147
148 /** Check whether any connections are active, and cancel them if 
149  * the user wishes.
150  * Returns TRUE is there was no problem, 
151  * or if an operation was cancelled so we can continue.
152  * Returns FALSE if the user chose to cancel his request instead.
153  */
154 static gboolean
155 check_for_active_account (ModestAccountViewWindow *self, const gchar* account_name)
156 {
157         ModestTnySendQueue *send_queue;
158         ModestTnyAccountStore *acc_store;
159         ModestMailOperationQueue* queue;
160         TnyConnectionStatus store_conn_status;
161         TnyAccount *store_account = NULL, *transport_account = NULL;
162         gboolean retval = TRUE, sending = FALSE;
163
164         acc_store = modest_runtime_get_account_store ();
165         queue = modest_runtime_get_mail_operation_queue ();
166
167         store_account = 
168                 modest_tny_account_store_get_server_account (acc_store,
169                                                              account_name,
170                                                              TNY_ACCOUNT_TYPE_STORE);
171
172         /* This could happen if the account was deleted before the
173            call to this function */
174         if (!store_account)
175                 return FALSE;
176
177         transport_account = 
178                 modest_tny_account_store_get_server_account (acc_store,
179                                                              account_name,
180                                                              TNY_ACCOUNT_TYPE_TRANSPORT);
181
182         /* This could happen if the account was deleted before the
183            call to this function */
184         if (!transport_account) {
185                 g_object_unref (store_account);
186                 return FALSE;
187         }
188
189         /* If the transport account was not used yet, then the send
190            queue could not exist (it's created on demand) */
191         send_queue = modest_runtime_get_send_queue (TNY_TRANSPORT_ACCOUNT (transport_account), FALSE);
192         if (TNY_IS_SEND_QUEUE (send_queue))
193                 sending = modest_tny_send_queue_sending_in_progress (send_queue);
194
195         store_conn_status = tny_account_get_connection_status (store_account);
196         if (store_conn_status == TNY_CONNECTION_STATUS_CONNECTED || sending) {
197                 gint response;
198
199                 response = modest_platform_run_confirmation_dialog (GTK_WINDOW (self), 
200                                                                 _("emev_nc_disconnect_account"));
201                 if (response == GTK_RESPONSE_OK) {
202                         retval = TRUE;
203                 } else {
204                         retval = FALSE;
205                 }
206         }
207
208         if (retval) {
209
210                 /* FIXME: We should only cancel those of this account */
211                 modest_mail_operation_queue_cancel_all (queue);
212
213                 /* Also disconnect the account */
214                 if ((tny_account_get_connection_status (store_account) != TNY_CONNECTION_STATUS_DISCONNECTED) &&
215                     (tny_account_get_connection_status (store_account) != TNY_CONNECTION_STATUS_DISCONNECTED_BROKEN)) {
216                         tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (store_account),
217                                                       FALSE, NULL, NULL);
218                 }
219                 if (sending) {
220                         tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (transport_account),
221                                                       FALSE, NULL, NULL);
222                 }
223         }
224                 
225         /* Frees */
226         g_object_unref (store_account);
227         g_object_unref (transport_account);
228         
229         return retval;
230 }
231
232 static void
233 on_delete_button_clicked (GtkWidget *button, ModestAccountViewWindow *self)
234 {
235         ModestAccountViewWindowPrivate *priv;
236         ModestAccountMgr *account_mgr;
237         gchar *account_title = NULL, *account_name = NULL;
238         
239         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
240
241         account_mgr = modest_runtime_get_account_mgr(); 
242         account_name = modest_account_view_get_selected_account (priv->account_view);
243         if(!account_name)
244                 return;
245
246         account_title = modest_account_mgr_get_display_name(account_mgr, account_name);
247         /* This could happen if the account is being deleted */
248         if (!account_title)
249                 return;
250         
251         if (check_for_active_account (self, account_name)) {
252                 /* The warning text depends on the account type: */
253                 gchar *txt = NULL;      
254                 gint response;
255                 ModestProtocol *protocol;
256
257                 protocol = modest_protocol_registry_get_protocol_by_type (modest_runtime_get_protocol_registry (),
258                                                                           modest_account_mgr_get_store_protocol (account_mgr, account_name));
259                 txt = modest_protocol_get_translation (protocol, MODEST_PROTOCOL_TRANSLATION_DELETE_MAILBOX, account_title);
260                 if (txt == NULL) {
261                         txt = g_strdup_printf (_("emev_nc_delete_mailbox"), 
262                                                account_title);
263                 }
264                 
265                 response = modest_platform_run_confirmation_dialog (GTK_WINDOW (self), txt);
266                 g_free (txt);
267                 txt = NULL;
268                 
269                 if (response == GTK_RESPONSE_OK) {
270                         /* Remove account. If it succeeds then it also removes
271                            the account from the ModestAccountView: */                             
272                         gboolean is_default = FALSE;
273                         gchar *default_account_name = modest_account_mgr_get_default_account (account_mgr);
274                         if (default_account_name && (strcmp (default_account_name, account_name) == 0))
275                                 is_default = TRUE;
276                         g_free (default_account_name);
277                         
278                                 gboolean removed = modest_account_mgr_remove_account (account_mgr, account_name);
279                                 if (!removed) {
280                                         g_warning ("%s: modest_account_mgr_remove_account() failed.\n", __FUNCTION__);
281                                 }
282
283                 }
284                 g_free (account_title);
285         }               
286         g_free (account_name);
287 }
288
289 static void
290 on_account_settings_dialog_response (GtkDialog *dialog,
291                                      gint response,
292                                      gpointer user_data)
293 {
294         TnyAccount *store_account = NULL;
295         gchar* account_name = NULL;
296         ModestAccountViewWindowPrivate *priv = NULL;
297
298         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE (user_data);
299         account_name = modest_account_view_get_selected_account (priv->account_view);
300         store_account = modest_tny_account_store_get_server_account (modest_runtime_get_account_store (),
301                                                                      account_name,
302                                                                      TNY_ACCOUNT_TYPE_STORE);
303        
304         /* Reconnect the store account, no need to reconnect the
305            transport account because it will connect when needed */
306         if (tny_account_get_connection_status (store_account) == 
307             TNY_CONNECTION_STATUS_DISCONNECTED)
308                 tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (store_account),
309                                               TRUE, NULL, NULL);
310
311         /* Disconnect this handler */
312         g_signal_handlers_disconnect_by_func (dialog, on_account_settings_dialog_response, user_data);
313
314         /* Free */
315         g_free (account_name);
316         g_object_unref (store_account);
317 }
318
319 static void
320 on_edit_button_clicked (GtkWidget *button, ModestAccountViewWindow *self)
321 {
322         ModestAccountViewWindowPrivate *priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE (self);
323         
324         gchar* account_name = modest_account_view_get_selected_account (priv->account_view);
325         if (!account_name)
326                 return;
327                 
328         /* Check whether any connections are active, and cancel them if 
329          * the user wishes.
330          */
331         if (check_for_active_account (self, account_name)) {
332                 ModestAccountProtocol *proto;
333                 ModestProtocolType proto_type;
334
335                 /* Get proto */
336                 proto_type = modest_account_mgr_get_store_protocol (modest_runtime_get_account_mgr (), 
337                                                                     account_name);
338                 proto = (ModestAccountProtocol *)
339                         modest_protocol_registry_get_protocol_by_type (modest_runtime_get_protocol_registry (), 
340                                                                        proto_type);
341
342                 /* Create and show the dialog */
343                 if (proto && MODEST_IS_ACCOUNT_PROTOCOL (proto)) {
344                         ModestAccountSettingsDialog *dialog =
345                                 modest_account_protocol_get_account_settings_dialog (proto, account_name);
346                         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (dialog), GTK_WINDOW (self));
347                         gtk_widget_show (GTK_WIDGET (dialog));
348                 }
349         }
350         g_free (account_name);
351 }
352
353 static void
354 on_wizard_response (GtkDialog *dialog, 
355                     gint response, 
356                     gpointer user_data)
357 {       
358         /* The response has already been handled by the wizard dialog itself,
359          * creating the new account.
360          */      
361         if (dialog)
362                 gtk_widget_destroy (GTK_WIDGET (dialog));
363
364         /* Re-focus the account list view widget */
365         if (MODEST_IS_ACCOUNT_VIEW_WINDOW (user_data)) {
366                 ModestAccountViewWindowPrivate *priv;
367                 priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE (user_data);
368                 gtk_widget_grab_focus (GTK_WIDGET (priv->account_view));
369         }
370 }
371
372 static void
373 on_new_button_clicked (GtkWidget *button, ModestAccountViewWindow *self)
374 {
375         GtkDialog *wizard;
376
377         /* there is no such wizard yet */
378         wizard = GTK_DIALOG (modest_easysetup_wizard_dialog_new ());
379         modest_window_mgr_set_modal (modest_runtime_get_window_mgr(), 
380                                      GTK_WINDOW (wizard), GTK_WINDOW (self));
381
382         gtk_window_set_modal (GTK_WINDOW (wizard), TRUE);
383         gtk_window_set_transient_for (GTK_WINDOW (wizard), GTK_WINDOW (self));
384         /* Destroy the dialog when it is closed: */
385         g_signal_connect (G_OBJECT (wizard), "response", G_CALLBACK
386                           (on_wizard_response), self);
387         gtk_widget_show (GTK_WIDGET (wizard));
388 }
389
390 static void
391 on_close_button_clicked (GtkWidget *button, gpointer user_data)
392 {               
393         ModestAccountViewWindow *self = MODEST_ACCOUNT_VIEW_WINDOW (user_data);
394
395         gtk_dialog_response (GTK_DIALOG (self), GTK_RESPONSE_OK);
396 }
397
398 static void
399 setup_button_box (ModestAccountViewWindow *self, GtkButtonBox *box)
400 {
401         ModestAccountViewWindowPrivate *priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
402         
403         gtk_button_box_set_spacing (GTK_BUTTON_BOX (box), 6);
404         gtk_button_box_set_layout (GTK_BUTTON_BOX (box), 
405                                    GTK_BUTTONBOX_START);
406         
407         priv->new_button     = gtk_button_new_from_stock(_("mcen_bd_new"));
408         priv->edit_button = gtk_button_new_with_label(_("mcen_bd_edit"));
409         priv->delete_button  = gtk_button_new_from_stock(_("mcen_bd_delete"));
410         priv->close_button    = gtk_button_new_from_stock(_("mcen_bd_close"));
411         
412         g_signal_connect (G_OBJECT(priv->new_button), "clicked",
413                           G_CALLBACK(on_new_button_clicked),
414                           self);
415         g_signal_connect (G_OBJECT(priv->delete_button), "clicked",
416                           G_CALLBACK(on_delete_button_clicked),
417                           self);
418         g_signal_connect (G_OBJECT(priv->edit_button), "clicked",
419                           G_CALLBACK(on_edit_button_clicked),
420                           self);
421         g_signal_connect (G_OBJECT(priv->close_button), "clicked",
422                           G_CALLBACK(on_close_button_clicked),
423                           self);
424
425         gtk_box_pack_start (GTK_BOX(box), priv->new_button, FALSE, FALSE,2);
426         gtk_box_pack_start (GTK_BOX(box), priv->edit_button, FALSE, FALSE,2);
427         gtk_box_pack_start (GTK_BOX(box), priv->delete_button, FALSE, FALSE,2);
428         gtk_box_pack_start (GTK_BOX(box), priv->close_button, FALSE, FALSE,2);
429
430         /* Should has been created by window_vbox_new */
431         if (priv->account_view) {
432                 GtkTreeSelection *sel;
433                 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(priv->account_view));
434                 if (gtk_tree_selection_count_selected_rows (sel) == 0) {
435                         gtk_widget_set_sensitive (priv->edit_button, FALSE);
436                         gtk_widget_set_sensitive (priv->delete_button, FALSE);  
437                 }
438         }
439
440         gtk_widget_show_all (GTK_WIDGET (box));
441 }
442
443 static GtkWidget*
444 window_vbox_new (ModestAccountViewWindow *self)
445 {
446         ModestAccountViewWindowPrivate *priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
447
448         GtkWidget *main_vbox     = gtk_vbox_new (FALSE, 6);
449         GtkWidget *main_hbox     = gtk_hbox_new (FALSE, 6);
450         
451         priv->account_view = modest_account_view_new (modest_runtime_get_account_mgr());
452
453         /* Only force the height, the width of the widget will depend
454            on the size of the column titles */
455         gtk_widget_set_size_request (GTK_WIDGET(priv->account_view), -1, 320);
456         gtk_widget_show (GTK_WIDGET (priv->account_view));
457
458         GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(priv->account_view));
459         g_signal_connect (G_OBJECT(sel), "changed",  G_CALLBACK(on_selection_changed),
460                           self);
461                           
462         GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
463         gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), MODEST_MARGIN_DEFAULT);
464         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_NEVER, 
465                 GTK_POLICY_AUTOMATIC);
466         gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (priv->account_view));
467         gtk_widget_show (GTK_WIDGET (scrolled_window));
468         
469         gtk_box_pack_start (GTK_BOX(main_hbox), GTK_WIDGET(scrolled_window), TRUE, TRUE, 2);
470         
471         gtk_box_pack_start (GTK_BOX(main_vbox), main_hbox, TRUE, TRUE, 2);
472         gtk_widget_show (GTK_WIDGET (main_hbox));
473         gtk_widget_show (GTK_WIDGET (main_vbox));
474
475         return main_vbox;
476 }
477
478
479 static void
480 modest_account_view_window_init (ModestAccountViewWindow *self)
481 {
482         ModestAccountViewWindowPrivate *priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
483
484         priv->acc_removed_handler = 0;
485 }
486
487 static void
488 on_account_removed (ModestAccountMgr *acc_mgr, 
489                     const gchar *account,
490                     gpointer user_data)
491 {
492         ModestAccountViewWindowPrivate *priv;
493
494         /* If there is no account left then close the window */
495         if (!modest_account_mgr_has_accounts (acc_mgr, TRUE)) {
496                 gboolean ret_value;
497                 g_signal_emit_by_name (G_OBJECT (user_data), "delete-event", NULL, &ret_value);
498         } else {                
499                 /* Re-focus the account list view widget */
500                 priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE (user_data);
501                 gtk_widget_grab_focus (GTK_WIDGET (priv->account_view));
502         }
503 }
504
505 GtkWidget*
506 modest_account_view_window_new (void)
507 {
508         GObject *self = g_object_new(MODEST_TYPE_ACCOUNT_VIEW_WINDOW, NULL);
509         ModestAccountViewWindowPrivate *priv;
510         ModestAccountMgr *account_mgr = modest_runtime_get_account_mgr ();
511
512         /* Add widgets */
513         gtk_box_pack_start (GTK_BOX((GTK_DIALOG (self)->vbox)), 
514                             window_vbox_new (MODEST_ACCOUNT_VIEW_WINDOW (self)), 
515                             TRUE, TRUE, 2);
516         
517         setup_button_box (MODEST_ACCOUNT_VIEW_WINDOW (self), GTK_BUTTON_BOX (GTK_DIALOG (self)->action_area));
518
519         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_emailsetup_accounts"));
520
521         /* Connect signals */
522         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
523         priv->acc_removed_handler = g_signal_connect (G_OBJECT(account_mgr), "account_removed",
524                                                       G_CALLBACK (on_account_removed), self);
525         
526         return GTK_WIDGET (self);
527 }