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