68b5f8d52fb956389d2bcd816281d346a7270385
[modest] / src / gnome / 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-account-mgr-helpers.h>
38 #include <string.h>
39 #include "modest-account-assistant.h"
40 #include "modest-account-protocol.h"
41 #include "modest-tny-platform-factory.h"
42 #include "modest-platform.h"
43
44 /* 'private'/'protected' functions */
45 static void                            modest_account_view_window_class_init   (ModestAccountViewWindowClass *klass);
46 static void                            modest_account_view_window_init         (ModestAccountViewWindow *obj);
47 static void                            modest_account_view_window_finalize     (GObject *obj);
48 static gboolean                        check_for_active_account                (ModestAccountViewWindow *self, 
49                                                                                 const gchar* account_name);
50 /* list my signals */
51 enum {
52         /* MY_SIGNAL_1, */
53         /* MY_SIGNAL_2, */
54         LAST_SIGNAL
55 };
56
57 typedef struct _ModestAccountViewWindowPrivate ModestAccountViewWindowPrivate;
58 struct _ModestAccountViewWindowPrivate {
59         GtkWidget           *add_button;
60         GtkWidget           *edit_button;
61         GtkWidget           *remove_button;
62         GtkWidget           *default_button;
63         ModestAccountView   *account_view;
64 };
65 #define MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
66                                                         MODEST_TYPE_ACCOUNT_VIEW_WINDOW, \
67                                                         ModestAccountViewWindowPrivate))
68 /* globals */
69 static GtkDialogClass *parent_class = NULL;
70
71 /* uncomment the following if you have defined any signals */
72 /* static guint signals[LAST_SIGNAL] = {0}; */
73
74 GType
75 modest_account_view_window_get_type (void)
76 {
77         static GType my_type = 0;
78         if (!my_type) {
79                 static const GTypeInfo my_info = {
80                         sizeof(ModestAccountViewWindowClass),
81                         NULL,           /* base init */
82                         NULL,           /* base finalize */
83                         (GClassInitFunc) modest_account_view_window_class_init,
84                         NULL,           /* class finalize */
85                         NULL,           /* class data */
86                         sizeof(ModestAccountViewWindow),
87                         1,              /* n_preallocs */
88                         (GInstanceInitFunc) modest_account_view_window_init,
89                         NULL
90                 };
91                 my_type = g_type_register_static (GTK_TYPE_DIALOG,
92                                                   "ModestAccountViewWindow",
93                                                   &my_info, 0);
94         }
95         return my_type;
96 }
97
98 static void
99 modest_account_view_window_class_init (ModestAccountViewWindowClass *klass)
100 {
101         GObjectClass *gobject_class;
102         gobject_class = (GObjectClass*) klass;
103
104         parent_class            = g_type_class_peek_parent (klass);
105         gobject_class->finalize = modest_account_view_window_finalize;
106
107         g_type_class_add_private (gobject_class, sizeof(ModestAccountViewWindowPrivate));
108 }
109
110 static void
111 modest_account_view_window_init (ModestAccountViewWindow *obj)
112 {
113         /* empty */
114 }
115
116 static void
117 modest_account_view_window_finalize (GObject *obj)
118 {
119         G_OBJECT_CLASS(parent_class)->finalize (obj);
120 }
121
122
123 static void
124 on_selection_changed (GtkTreeSelection *sel, ModestAccountViewWindow *self)
125 {
126         ModestAccountViewWindowPrivate *priv;
127         GtkTreeModel                   *model;
128         GtkTreeIter                    iter;
129         gboolean                       has_selection;
130         gchar                         *account_name;
131         gchar                         *default_account_name;
132         
133         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
134
135         has_selection =
136                 gtk_tree_selection_get_selected (sel, &model, &iter);
137
138         gtk_widget_set_sensitive (priv->edit_button, has_selection);
139         gtk_widget_set_sensitive (priv->remove_button, has_selection);  
140
141         account_name = modest_account_view_get_selected_account (priv->account_view);
142         default_account_name = modest_account_mgr_get_default_account(
143                 modest_runtime_get_account_mgr());
144         gtk_widget_set_sensitive (priv->default_button,
145                                   default_account_name == NULL || account_name == NULL ||
146                                   strcmp (default_account_name, account_name) != 0);
147         g_free (account_name);
148         g_free (default_account_name);
149 }
150
151 static void
152 on_remove_button_clicked (GtkWidget *button, ModestAccountViewWindow *self)
153 {
154         ModestAccountViewWindowPrivate *priv;
155         ModestAccountMgr *account_mgr;
156         gchar *account_name, *account_title;
157         
158         
159         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
160
161         account_mgr = modest_runtime_get_account_mgr(); 
162         account_name = modest_account_view_get_selected_account (priv->account_view);
163
164         if (!account_name)
165                 return;
166
167         account_title = modest_account_mgr_get_display_name(account_mgr, account_name);
168         if (!account_title)
169                 return;
170
171         if (check_for_active_account (self, account_name)) {
172                 gboolean removed;
173                 gchar *txt;
174                 gint response;
175
176                 if (modest_account_mgr_get_store_protocol (account_mgr, account_name) 
177                     == MODEST_PROTOCOLS_STORE_POP) {
178                                 txt = g_strdup_printf (_("emev_nc_delete_mailbox"), 
179                                                        account_title);
180                 } else {
181                         txt = g_strdup_printf (_("emev_nc_delete_mailboximap"), 
182                                                account_title);
183                 }
184                 
185                 response = modest_platform_run_confirmation_dialog (GTK_WINDOW (self), txt);
186                 g_free (txt);
187                 txt = NULL;
188
189                 if (response == GTK_RESPONSE_OK) {
190                         /* Remove account. If succeeded it removes also 
191                            the account from the ModestAccountView */
192                         removed = modest_account_mgr_remove_account (account_mgr,
193                                                                      account_name);
194                         if (removed) {
195                                 /* Show confirmation dialog ??? */
196                         } else {
197                                 /* Show error dialog ??? */
198                                 g_warning ("Error removing account %s", account_name);
199                         }
200                 }
201                 g_free (account_name);
202         }
203 }
204
205 /** Check whether any connections are active, and cancel them if 
206  * the user wishes.
207  * Returns TRUE is there was no problem, 
208  * or if an operation was cancelled so we can continue.
209  * Returns FALSE if the user chose to cancel his request instead.
210  */
211 static gboolean
212 check_for_active_account (ModestAccountViewWindow *self, const gchar* account_name)
213 {
214         ModestTnyAccountStore *acc_store;
215         ModestMailOperationQueue* queue;
216         TnyConnectionStatus store_conn_status, transport_conn_status;
217         TnyAccount *store_account = NULL, *transport_account = NULL;
218         gboolean retval = TRUE;
219
220         acc_store = modest_runtime_get_account_store ();
221         queue = modest_runtime_get_mail_operation_queue ();
222
223         store_account = 
224                 modest_tny_account_store_get_server_account (acc_store,
225                                                              account_name,
226                                                              TNY_ACCOUNT_TYPE_STORE);
227         transport_account = 
228                 modest_tny_account_store_get_server_account (acc_store,
229                                                              account_name,
230                                                              TNY_ACCOUNT_TYPE_TRANSPORT);
231
232         store_conn_status = tny_account_get_connection_status (store_account);
233         transport_conn_status = tny_account_get_connection_status (transport_account);
234
235         if (store_conn_status == TNY_CONNECTION_STATUS_CONNECTED ||
236             transport_conn_status == TNY_CONNECTION_STATUS_CONNECTED) {
237                 gint response;
238
239                 response = modest_platform_run_confirmation_dialog (GTK_WINDOW (self), 
240                                                                 _("emev_nc_disconnect_account"));
241                 if (response == GTK_RESPONSE_OK) {
242                         /* FIXME: We should only cancel those of this account */
243                         modest_mail_operation_queue_cancel_all (queue);
244
245                         /* Also disconnect the account */
246                         if (tny_account_get_connection_status (store_account) == TNY_CONNECTION_STATUS_CONNECTED) {
247                                 tny_account_cancel (store_account);
248                                 tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (store_account),
249                                                               FALSE, NULL, NULL);
250                         }
251                         if (tny_account_get_connection_status (transport_account) == TNY_CONNECTION_STATUS_CONNECTED) {
252                                 tny_account_cancel (transport_account);
253                                 tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (transport_account),
254                                                               FALSE, NULL, NULL);
255                         }
256                         retval = TRUE;
257                 } else {
258                         retval = FALSE;
259                 }
260         }
261
262         /* Frees */
263         g_object_unref (store_account);
264         g_object_unref (transport_account);
265         
266         return retval;
267 }
268
269 static void
270 on_edit_button_clicked (GtkWidget *button, ModestAccountViewWindow *self)
271 {
272         ModestAccountViewWindowPrivate *priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE (self);
273         
274         gchar* account_name = modest_account_view_get_selected_account (priv->account_view);
275         if (!account_name)
276                 return;
277                 
278         /* Check whether any connections are active, and cancel them if 
279          * the user wishes.
280          */
281         if (check_for_active_account (self, account_name)) {
282                 ModestAccountProtocol *proto;
283                 ModestProtocolType proto_type;
284
285                 /* Get proto */
286                 proto_type = modest_account_mgr_get_store_protocol (modest_runtime_get_account_mgr (), 
287                                                                     account_name);
288                 proto = (ModestAccountProtocol *)
289                         modest_protocol_registry_get_protocol_by_type (modest_runtime_get_protocol_registry (), 
290                                                                        proto_type);
291
292                 /* Create and show the dialog */
293                 if (proto && MODEST_IS_ACCOUNT_PROTOCOL (proto)) {
294                         ModestAccountSettingsDialog *dialog =
295                                 modest_account_protocol_get_account_settings_dialog (proto, account_name);
296                         gtk_widget_show (GTK_WIDGET (dialog));
297                 }
298         }
299         g_free (account_name);
300 }
301
302 static void
303 on_wizard_response (GtkDialog *dialog, 
304                     gint response, 
305                     gpointer user_data)
306 {       
307         /* The response has already been handled by the wizard dialog itself,
308          * creating the new account.
309          */      
310         if (dialog)
311                 gtk_widget_destroy (GTK_WIDGET (dialog));
312
313         /* Re-focus the account list view widget */
314         if (MODEST_IS_ACCOUNT_VIEW_WINDOW (user_data)) {
315                 ModestAccountViewWindowPrivate *priv;
316                 priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE (user_data);
317                 gtk_widget_grab_focus (GTK_WIDGET (priv->account_view));
318         }
319 }
320
321 static void
322 on_add_button_clicked (GtkWidget *button, ModestAccountViewWindow *self)
323 {
324         GtkDialog *wizard;
325         GtkWindow *dialog;
326
327         /* Show the easy-setup wizard: */       
328         dialog = modest_window_mgr_get_modal (modest_runtime_get_window_mgr());
329         if (dialog && MODEST_IS_ACCOUNT_ASSISTANT (dialog)) {
330                 /* old wizard is active already; 
331                  */
332                 gtk_window_present (dialog);
333                 return;
334         }
335         
336         /* there is no such wizard yet */
337         wizard = GTK_DIALOG (modest_account_assistant_new (modest_runtime_get_account_mgr ()));
338         modest_window_mgr_set_modal (modest_runtime_get_window_mgr(), 
339                                      GTK_WINDOW (wizard), self);
340
341         /* if there is already another modal dialog, make it non-modal */
342         if (dialog)
343                 gtk_window_set_modal (GTK_WINDOW(dialog), FALSE);
344         
345         gtk_window_set_modal (GTK_WINDOW (wizard), TRUE);
346         gtk_window_set_transient_for (GTK_WINDOW (wizard), GTK_WINDOW (self));
347         /* Destroy the dialog when it is closed: */
348         g_signal_connect (G_OBJECT (wizard), "response", G_CALLBACK
349                           (on_wizard_response), self);
350         gtk_widget_show (GTK_WIDGET (wizard));
351 }
352
353
354 static void
355 on_default_button_clicked (GtkWidget *button, ModestAccountViewWindow *self)
356 {
357         ModestAccountViewWindowPrivate *priv;
358         ModestAccountMgr *account_mgr;
359         gchar *account_name;
360         
361         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
362
363         account_mgr = modest_runtime_get_account_mgr(); 
364         account_name = modest_account_view_get_selected_account (priv->account_view);
365
366         modest_account_mgr_set_default_account (account_mgr, account_name);
367
368         g_free (account_name);
369 }
370
371
372 static GtkWidget*
373 button_box_new (ModestAccountViewWindow *self)
374 {
375
376         GtkWidget *button_box;
377         ModestAccountViewWindowPrivate *priv;
378         
379         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
380         
381         button_box   = gtk_vbutton_box_new ();
382         gtk_button_box_set_spacing (GTK_BUTTON_BOX (button_box), 6);
383         gtk_button_box_set_layout (GTK_BUTTON_BOX (button_box), 
384                                    GTK_BUTTONBOX_START);
385         
386         priv->add_button     = gtk_button_new_from_stock(GTK_STOCK_ADD);
387         priv->default_button = gtk_button_new_with_label(_("Make default"));
388         priv->remove_button  = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
389         priv->edit_button    = gtk_button_new_from_stock(GTK_STOCK_EDIT);
390         
391         g_signal_connect (G_OBJECT(priv->add_button), "clicked",
392                           G_CALLBACK(on_add_button_clicked),
393                           self);
394         g_signal_connect (G_OBJECT(priv->remove_button), "clicked",
395                           G_CALLBACK(on_remove_button_clicked),
396                           self);
397         g_signal_connect (G_OBJECT(priv->edit_button), "clicked",
398                           G_CALLBACK(on_edit_button_clicked),
399                           self);
400         g_signal_connect (G_OBJECT(priv->default_button), "clicked",
401                           G_CALLBACK(on_default_button_clicked),
402                           self);
403         
404         gtk_box_pack_start (GTK_BOX(button_box), priv->add_button, FALSE, FALSE,2);
405         gtk_box_pack_start (GTK_BOX(button_box), priv->default_button, FALSE, FALSE,2);
406         gtk_box_pack_start (GTK_BOX(button_box), priv->remove_button, FALSE, FALSE,2);
407         gtk_box_pack_start (GTK_BOX(button_box), priv->edit_button, FALSE, FALSE,2);
408
409         gtk_widget_set_sensitive (priv->edit_button, FALSE);
410         gtk_widget_set_sensitive (priv->remove_button, FALSE);  
411         gtk_widget_set_sensitive (priv->default_button, FALSE);
412         
413         return button_box;
414 }
415
416 static GtkWidget*
417 window_vbox_new (ModestAccountViewWindow *self)
418 {
419         ModestAccountViewWindowPrivate *priv;
420         GtkTreeSelection *sel;
421         GtkWidget *main_hbox, *main_vbox, *button_box;
422         GtkWidget *scrolled_window;
423
424         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
425
426         main_vbox     = gtk_vbox_new (FALSE, 0);
427         main_hbox     = gtk_hbox_new (FALSE, 12);
428         
429         button_box = button_box_new (self);
430         
431         priv->account_view = modest_account_view_new (modest_runtime_get_account_mgr());
432         gtk_widget_set_size_request (GTK_WIDGET(priv->account_view), 300, 400);
433
434         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(priv->account_view));
435         on_selection_changed (sel, self);
436         g_signal_connect (G_OBJECT(sel), "changed",  G_CALLBACK(on_selection_changed),
437                           self);
438         
439         scrolled_window = gtk_scrolled_window_new (NULL, NULL);
440         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
441         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_IN);
442         gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (priv->account_view));
443         gtk_box_pack_start (GTK_BOX(main_hbox), scrolled_window, TRUE, TRUE, 0);
444         gtk_box_pack_start (GTK_BOX(main_hbox), button_box, FALSE, FALSE,0);
445
446         gtk_box_pack_start (GTK_BOX(main_vbox), main_hbox, TRUE, TRUE, 0);
447
448         gtk_widget_show_all (main_vbox);
449         return main_vbox;
450 }
451
452
453 GtkWidget*
454 modest_account_view_window_new (void)
455 {
456         GObject *obj;
457         ModestAccountViewWindowPrivate *priv;
458         
459         obj  = g_object_new(MODEST_TYPE_ACCOUNT_VIEW_WINDOW, NULL);
460         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(obj);
461
462         gtk_window_set_resizable (GTK_WINDOW(obj), TRUE);
463         gtk_window_set_title (GTK_WINDOW(obj), _("mcen_ti_emailsetup_accounts"));
464         gtk_window_set_type_hint (GTK_WINDOW(obj), GDK_WINDOW_TYPE_HINT_DIALOG);
465         gtk_window_set_default_size (GTK_WINDOW (obj), 640, 480);
466         
467         gtk_window_set_modal (GTK_WINDOW(obj), TRUE);
468                                      
469         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(obj)->vbox),
470                             window_vbox_new (MODEST_ACCOUNT_VIEW_WINDOW(obj)),
471                             TRUE, TRUE, 12);
472
473         gtk_dialog_add_button (GTK_DIALOG (obj), GTK_STOCK_CLOSE, GTK_RESPONSE_OK);
474                 
475         return GTK_WIDGET(obj);
476 }