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