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