* Added a new account key called type for server accounts
[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_modest_account_mgr_instance (fact);
161
162         account_name = modest_account_view_get_selected_account (priv->account_view);
163
164         if (account_name) {
165                 gboolean removed;
166                 GError *err = NULL;
167                 GtkWidget *dialog;
168                 gchar *txt;
169
170                 dialog = gtk_dialog_new_with_buttons (_("Confirmation dialog"),
171                                                       GTK_WINDOW (self),
172                                                       GTK_DIALOG_MODAL,
173                                                       GTK_STOCK_CANCEL,
174                                                       GTK_RESPONSE_REJECT,
175                                                       GTK_STOCK_OK,
176                                                       GTK_RESPONSE_ACCEPT,
177                                                       NULL);
178                 txt = g_strdup_printf (_("Do you really want to delete the account %s?"), account_name);
179                 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), 
180                                     gtk_label_new (txt), FALSE, FALSE, 0);
181                 gtk_widget_show_all (GTK_WIDGET(GTK_DIALOG(dialog)->vbox));
182                 g_free (txt);
183
184                 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
185                         /* Remove account. If succeeded it removes also 
186                            the account from the ModestAccountView */
187                         removed = modest_account_mgr_remove_account (account_mgr,
188                                                                      account_name,
189                                                                      FALSE,
190                                                                      &err);
191                         if (removed) {
192                                 /* Show confirmation dialog ??? */
193                         } else {
194                                 /* Show error dialog ??? */
195                                 if (err)
196                                         g_error_free (err);
197                         }
198                 }
199                 gtk_widget_destroy (dialog);
200         }
201 }
202
203 static void
204 on_edit_button_clicked (GtkWidget *button, ModestAccountViewWindow *self)
205 {
206         g_message (__FUNCTION__);
207 }
208
209 static void
210 on_add_button_clicked (GtkWidget *button, ModestAccountViewWindow *self)
211 {
212         GtkWidget *assistant;
213         ModestAccountViewWindowPrivate *priv;
214         TnyPlatformFactory *fact;
215         ModestAccountMgr *account_mgr;
216         
217         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
218         fact = modest_tny_platform_factory_get_instance ();
219         account_mgr = modest_tny_platform_factory_get_modest_account_mgr_instance (fact);
220
221         assistant = modest_account_assistant_new (account_mgr,
222                                                   priv->widget_factory);
223         gtk_window_set_transient_for (GTK_WINDOW(assistant),
224                                       GTK_WINDOW(self));
225
226         gtk_widget_show (GTK_WIDGET(assistant));
227 }
228
229
230 static void
231 on_close_button_clicked (GtkWidget *button, ModestAccountViewWindow *self)
232 {
233         gtk_widget_destroy (GTK_WIDGET(self));
234 }
235
236
237
238 static GtkWidget*
239 button_box_new (ModestAccountViewWindow *self)
240 {
241
242         GtkWidget *button_box;
243         GtkWidget *add_button, *remove_button, *edit_button;
244         ModestAccountViewWindowPrivate *priv;
245         
246         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
247         
248         button_box   = gtk_vbutton_box_new ();
249         gtk_button_box_set_spacing (GTK_BUTTON_BOX (button_box), 6);
250         gtk_button_box_set_layout (GTK_BUTTON_BOX (button_box), 
251                                    GTK_BUTTONBOX_START);
252
253         add_button    = gtk_button_new_from_stock(GTK_STOCK_ADD);
254         remove_button = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
255         edit_button   = gtk_button_new_from_stock(GTK_STOCK_EDIT);
256
257         g_signal_connect (G_OBJECT(add_button), "clicked",
258                           G_CALLBACK(on_add_button_clicked),
259                           self);
260         g_signal_connect (G_OBJECT(remove_button), "clicked",
261                           G_CALLBACK(on_remove_button_clicked),
262                           self);
263         g_signal_connect (G_OBJECT(edit_button), "clicked",
264                           G_CALLBACK(on_edit_button_clicked),
265                           self);
266
267         gtk_box_pack_start (GTK_BOX(button_box), add_button, FALSE, FALSE,2);
268         gtk_box_pack_start (GTK_BOX(button_box), remove_button, FALSE, FALSE,2);
269         gtk_box_pack_start (GTK_BOX(button_box), edit_button, FALSE, FALSE,2);
270
271         gtk_widget_set_sensitive (edit_button, FALSE);
272         gtk_widget_set_sensitive (remove_button, FALSE);        
273
274         /* remember these, so we can deactivate them when nothing is
275          * selected */
276         priv->remove_button = remove_button;
277         priv->edit_button   = edit_button;
278         
279         return button_box;
280 }
281
282
283 static GtkWidget*
284 window_vbox_new (ModestAccountViewWindow *self)
285 {
286         ModestAccountViewWindowPrivate *priv;
287         GtkTreeSelection *sel;
288         GtkWidget *main_hbox, *main_vbox, *button_box;
289         GtkWidget *close_button;
290         GtkWidget *close_hbox;
291         ModestAccountView *account_view;
292
293         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
294
295         main_vbox     = gtk_vbox_new (FALSE, 6);
296         main_hbox     = gtk_hbox_new (FALSE, 6);
297         
298         account_view = modest_widget_factory_get_account_view (priv->widget_factory);
299         priv->account_view = account_view;
300         gtk_widget_set_size_request (GTK_WIDGET(account_view), 300, 400);
301
302         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(account_view));
303         g_signal_connect (G_OBJECT(sel), "changed",  G_CALLBACK(on_selection_changed),
304                           self);
305         
306         button_box = button_box_new (self);
307         
308         gtk_box_pack_start (GTK_BOX(main_hbox), GTK_WIDGET(account_view), TRUE, TRUE, 2);
309         gtk_box_pack_start (GTK_BOX(main_hbox), button_box, FALSE, FALSE,2);
310
311         gtk_box_pack_start (GTK_BOX(main_vbox), main_hbox, TRUE, TRUE, 2);
312
313
314         close_button = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
315         g_signal_connect (G_OBJECT(close_button), "clicked",
316                           G_CALLBACK(on_close_button_clicked),
317                           self);
318         
319         close_hbox = gtk_hbox_new (FALSE, 2);
320         gtk_box_pack_end (GTK_BOX(close_hbox),
321                           close_button, FALSE, FALSE,2);
322         gtk_box_pack_end (GTK_BOX(main_vbox), close_hbox, FALSE, FALSE,2);
323
324         gtk_widget_show_all (main_vbox);
325         return main_vbox;
326 }
327
328
329 GtkWidget*
330 modest_account_view_window_new (ModestWidgetFactory *factory)
331 {
332         GObject *obj;
333         ModestAccountViewWindowPrivate *priv;
334
335         g_return_val_if_fail (factory, NULL);
336         
337         obj  = g_object_new(MODEST_TYPE_ACCOUNT_VIEW_WINDOW, NULL);
338         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(obj);
339
340         g_object_ref (G_OBJECT(factory));
341         priv->widget_factory = factory;
342
343         gtk_window_set_resizable (GTK_WINDOW(obj), FALSE);
344
345         gtk_window_set_title (GTK_WINDOW(obj), _("Accounts"));
346         gtk_window_set_type_hint (GTK_WINDOW(obj), GDK_WINDOW_TYPE_HINT_DIALOG);
347         
348         gtk_window_set_modal (GTK_WINDOW(obj), TRUE);
349                 
350         gtk_container_add (GTK_CONTAINER(obj),
351                            window_vbox_new (MODEST_ACCOUNT_VIEW_WINDOW(obj)));
352                 
353         return GTK_WIDGET(obj);
354 }