55000b60d663fbbb68ae6a4f9851e8ab4bcbf400
[modest] / src / gtk / 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 "modest-account-view-window.h"
32 #include "modest-account-assistant.h"
33 #include "modest-tny-platform-factory.h"
34
35 /* 'private'/'protected' functions */
36 static void                            modest_account_view_window_class_init   (ModestAccountViewWindowClass *klass);
37 static void                            modest_account_view_window_init         (ModestAccountViewWindow *obj);
38 static void                            modest_account_view_window_finalize     (GObject *obj);
39
40 /* list my signals */
41 enum {
42         /* MY_SIGNAL_1, */
43         /* MY_SIGNAL_2, */
44         LAST_SIGNAL
45 };
46
47
48 typedef struct _ModestAccountViewWindowPrivate ModestAccountViewWindowPrivate;
49 struct _ModestAccountViewWindowPrivate {
50         ModestWidgetFactory *widget_factory;
51         GtkWidget           *edit_button;
52         GtkWidget           *remove_button;
53         ModestAccountView   *account_view;
54 };
55 #define MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
56                                                         MODEST_TYPE_ACCOUNT_VIEW_WINDOW, \
57                                                         ModestAccountViewWindowPrivate))
58 /* globals */
59 static GtkWindowClass *parent_class = NULL;
60
61 /* uncomment the following if you have defined any signals */
62 /* static guint signals[LAST_SIGNAL] = {0}; */
63
64 GType
65 modest_account_view_window_get_type (void)
66 {
67         static GType my_type = 0;
68         if (!my_type) {
69                 static const GTypeInfo my_info = {
70                         sizeof(ModestAccountViewWindowClass),
71                         NULL,           /* base init */
72                         NULL,           /* base finalize */
73                         (GClassInitFunc) modest_account_view_window_class_init,
74                         NULL,           /* class finalize */
75                         NULL,           /* class data */
76                         sizeof(ModestAccountViewWindow),
77                         1,              /* n_preallocs */
78                         (GInstanceInitFunc) modest_account_view_window_init,
79                         NULL
80                 };
81                 my_type = g_type_register_static (GTK_TYPE_WINDOW,
82                                                   "ModestAccountViewWindow",
83                                                   &my_info, 0);
84         }
85         return my_type;
86 }
87
88 static void
89 modest_account_view_window_class_init (ModestAccountViewWindowClass *klass)
90 {
91         GObjectClass *gobject_class;
92         gobject_class = (GObjectClass*) klass;
93
94         parent_class            = g_type_class_peek_parent (klass);
95         gobject_class->finalize = modest_account_view_window_finalize;
96
97         g_type_class_add_private (gobject_class, sizeof(ModestAccountViewWindowPrivate));
98
99         /* signal definitions go here, e.g.: */
100 /*      signals[MY_SIGNAL_1] = */
101 /*              g_signal_new ("my_signal_1",....); */
102 /*      signals[MY_SIGNAL_2] = */
103 /*              g_signal_new ("my_signal_2",....); */
104 /*      etc. */
105 }
106
107 static void
108 modest_account_view_window_init (ModestAccountViewWindow *obj)
109 {
110         ModestAccountViewWindowPrivate *priv;
111                 
112         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(obj);
113
114         priv->widget_factory = NULL;
115 }
116
117 static void
118 modest_account_view_window_finalize (GObject *obj)
119 {
120         ModestAccountViewWindowPrivate *priv;
121                 
122         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(obj);
123
124         if (priv->widget_factory) {
125                 g_object_unref (G_OBJECT(priv->widget_factory));
126                 priv->widget_factory = NULL;
127         }
128
129         G_OBJECT_CLASS(parent_class)->finalize (obj);
130 }
131
132
133 static void
134 on_selection_changed (GtkTreeSelection *sel, ModestAccountViewWindow *self)
135 {
136         ModestAccountViewWindowPrivate *priv;
137         GtkTreeModel                   *model;
138         GtkTreeIter                    iter;
139         gboolean                       has_selection;
140
141         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
142
143         has_selection =
144                 gtk_tree_selection_get_selected (sel, &model, &iter);
145
146         gtk_widget_set_sensitive (priv->edit_button, has_selection);
147         gtk_widget_set_sensitive (priv->remove_button, has_selection);  
148 }
149
150 static void
151 on_remove_button_clicked (GtkWidget *button, ModestAccountViewWindow *self)
152 {
153         TnyPlatformFactory *fact;
154         ModestAccountViewWindowPrivate *priv;
155         ModestAccountMgr *account_mgr;
156         const gchar *account_name;
157         
158         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
159         fact = modest_tny_platform_factory_get_instance ();
160         account_mgr = modest_tny_platform_factory_get_account_mgr_instance
161                 (MODEST_TNY_PLATFORM_FACTORY(fact));
162
163         account_name = modest_account_view_get_selected_account (priv->account_view);
164
165         if (account_name) {
166                 gboolean removed;
167                 GError *err = NULL;
168                 GtkWidget *dialog;
169                 gchar *txt;
170
171                 dialog = gtk_dialog_new_with_buttons (_("Confirmation dialog"),
172                                                       GTK_WINDOW (self),
173                                                       GTK_DIALOG_MODAL,
174                                                       GTK_STOCK_CANCEL,
175                                                       GTK_RESPONSE_REJECT,
176                                                       GTK_STOCK_OK,
177                                                       GTK_RESPONSE_ACCEPT,
178                                                       NULL);
179                 txt = g_strdup_printf (_("Do you really want to delete the account %s?"), account_name);
180                 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), 
181                                     gtk_label_new (txt), FALSE, FALSE, 0);
182                 gtk_widget_show_all (GTK_WIDGET(GTK_DIALOG(dialog)->vbox));
183                 g_free (txt);
184
185                 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
186                         /* Remove account. If succeeded it removes also 
187                            the account from the ModestAccountView */
188                         removed = modest_account_mgr_remove_account (account_mgr,
189                                                                      account_name,
190                                                                      FALSE,
191                                                                      &err);
192                         if (removed) {
193                                 /* Show confirmation dialog ??? */
194                         } else {
195                                 /* Show error dialog ??? */
196                                 if (err)
197                                         g_error_free (err);
198                         }
199                 }
200                 gtk_widget_destroy (dialog);
201         }
202 }
203
204 static void
205 on_edit_button_clicked (GtkWidget *button, ModestAccountViewWindow *self)
206 {
207         g_message (__FUNCTION__);
208 }
209
210 static void
211 on_add_button_clicked (GtkWidget *button, ModestAccountViewWindow *self)
212 {
213         GtkWidget *assistant;
214         ModestAccountViewWindowPrivate *priv;
215         TnyPlatformFactory *fact;
216         ModestAccountMgr *account_mgr;
217         
218         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
219         fact = modest_tny_platform_factory_get_instance ();
220         account_mgr = modest_tny_platform_factory_get_account_mgr_instance
221                 (MODEST_TNY_PLATFORM_FACTORY(fact));
222
223         assistant = modest_account_assistant_new (account_mgr,
224                                                   priv->widget_factory);
225         gtk_window_set_transient_for (GTK_WINDOW(assistant),
226                                       GTK_WINDOW(self));
227
228         gtk_widget_show (GTK_WIDGET(assistant));
229 }
230
231
232 static void
233 on_close_button_clicked (GtkWidget *button, ModestAccountViewWindow *self)
234 {
235         gtk_widget_destroy (GTK_WIDGET(self));
236 }
237
238
239
240 static GtkWidget*
241 button_box_new (ModestAccountViewWindow *self)
242 {
243
244         GtkWidget *button_box;
245         GtkWidget *add_button, *remove_button, *edit_button;
246         ModestAccountViewWindowPrivate *priv;
247         
248         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
249         
250         button_box   = gtk_vbutton_box_new ();
251         gtk_button_box_set_spacing (GTK_BUTTON_BOX (button_box), 6);
252         gtk_button_box_set_layout (GTK_BUTTON_BOX (button_box), 
253                                    GTK_BUTTONBOX_START);
254
255         add_button    = gtk_button_new_from_stock(GTK_STOCK_ADD);
256         remove_button = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
257         edit_button   = gtk_button_new_from_stock(GTK_STOCK_EDIT);
258
259         g_signal_connect (G_OBJECT(add_button), "clicked",
260                           G_CALLBACK(on_add_button_clicked),
261                           self);
262         g_signal_connect (G_OBJECT(remove_button), "clicked",
263                           G_CALLBACK(on_remove_button_clicked),
264                           self);
265         g_signal_connect (G_OBJECT(edit_button), "clicked",
266                           G_CALLBACK(on_edit_button_clicked),
267                           self);
268
269         gtk_box_pack_start (GTK_BOX(button_box), add_button, FALSE, FALSE,2);
270         gtk_box_pack_start (GTK_BOX(button_box), remove_button, FALSE, FALSE,2);
271         gtk_box_pack_start (GTK_BOX(button_box), edit_button, FALSE, FALSE,2);
272
273         gtk_widget_set_sensitive (edit_button, FALSE);
274         gtk_widget_set_sensitive (remove_button, FALSE);        
275
276         /* remember these, so we can deactivate them when nothing is
277          * selected */
278         priv->remove_button = remove_button;
279         priv->edit_button   = edit_button;
280         
281         return button_box;
282 }
283
284
285 static GtkWidget*
286 window_vbox_new (ModestAccountViewWindow *self)
287 {
288         ModestAccountViewWindowPrivate *priv;
289         GtkTreeSelection *sel;
290         GtkWidget *main_hbox, *main_vbox, *button_box;
291         GtkWidget *close_button;
292         GtkWidget *close_hbox;
293         ModestAccountView *account_view;
294
295         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
296
297         main_vbox     = gtk_vbox_new (FALSE, 6);
298         main_hbox     = gtk_hbox_new (FALSE, 6);
299         
300         account_view = modest_widget_factory_get_account_view (priv->widget_factory);
301         priv->account_view = account_view;
302         gtk_widget_set_size_request (GTK_WIDGET(account_view), 300, 400);
303
304         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(account_view));
305         g_signal_connect (G_OBJECT(sel), "changed",  G_CALLBACK(on_selection_changed),
306                           self);
307         
308         button_box = button_box_new (self);
309         
310         gtk_box_pack_start (GTK_BOX(main_hbox), GTK_WIDGET(account_view), TRUE, TRUE, 2);
311         gtk_box_pack_start (GTK_BOX(main_hbox), button_box, FALSE, FALSE,2);
312
313         gtk_box_pack_start (GTK_BOX(main_vbox), main_hbox, TRUE, TRUE, 2);
314
315
316         close_button = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
317         g_signal_connect (G_OBJECT(close_button), "clicked",
318                           G_CALLBACK(on_close_button_clicked),
319                           self);
320         
321         close_hbox = gtk_hbox_new (FALSE, 2);
322         gtk_box_pack_end (GTK_BOX(close_hbox),
323                           close_button, FALSE, FALSE,2);
324         gtk_box_pack_end (GTK_BOX(main_vbox), close_hbox, FALSE, FALSE,2);
325
326         gtk_widget_show_all (main_vbox);
327         return main_vbox;
328 }
329
330
331 GtkWidget*
332 modest_account_view_window_new (ModestWidgetFactory *factory)
333 {
334         GObject *obj;
335         ModestAccountViewWindowPrivate *priv;
336
337         g_return_val_if_fail (factory, NULL);
338         
339         obj  = g_object_new(MODEST_TYPE_ACCOUNT_VIEW_WINDOW, NULL);
340         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(obj);
341
342         g_object_ref (G_OBJECT(factory));
343         priv->widget_factory = factory;
344
345         gtk_window_set_resizable (GTK_WINDOW(obj), FALSE);
346
347         gtk_window_set_title (GTK_WINDOW(obj), _("Accounts"));
348         gtk_window_set_type_hint (GTK_WINDOW(obj), GDK_WINDOW_TYPE_HINT_DIALOG);
349         
350         gtk_window_set_modal (GTK_WINDOW(obj), TRUE);
351                 
352         gtk_container_add (GTK_CONTAINER(obj),
353                            window_vbox_new (MODEST_ACCOUNT_VIEW_WINDOW(obj)));
354                 
355         return GTK_WIDGET(obj);
356 }