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