37be55908742df50e58f6b3d565dbb276757313d
[modest] / src / gtk2 / 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 "modest-account-view-window.h"
31 #include "modest-account-wizard.h"
32
33 /* 'private'/'protected' functions */
34 static void                            modest_account_view_window_class_init   (ModestAccountViewWindowClass *klass);
35 static void                            modest_account_view_window_init         (ModestAccountViewWindow *obj);
36 static void                            modest_account_view_window_finalize     (GObject *obj);
37
38 /* list my signals */
39 enum {
40         /* MY_SIGNAL_1, */
41         /* MY_SIGNAL_2, */
42         LAST_SIGNAL
43 };
44
45
46 typedef struct _ModestAccountViewWindowPrivate ModestAccountViewWindowPrivate;
47 struct _ModestAccountViewWindowPrivate {
48         ModestConf          *conf;
49         ModestWidgetFactory *widget_factory;
50         GtkWidget           *edit_button, *remove_button;
51 };
52 #define MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
53                                                         MODEST_TYPE_ACCOUNT_VIEW_WINDOW, \
54                                                         ModestAccountViewWindowPrivate))
55 /* globals */
56 static GtkWindowClass *parent_class = NULL;
57
58 /* uncomment the following if you have defined any signals */
59 /* static guint signals[LAST_SIGNAL] = {0}; */
60
61 GType
62 modest_account_view_window_get_type (void)
63 {
64         static GType my_type = 0;
65         if (!my_type) {
66                 static const GTypeInfo my_info = {
67                         sizeof(ModestAccountViewWindowClass),
68                         NULL,           /* base init */
69                         NULL,           /* base finalize */
70                         (GClassInitFunc) modest_account_view_window_class_init,
71                         NULL,           /* class finalize */
72                         NULL,           /* class data */
73                         sizeof(ModestAccountViewWindow),
74                         1,              /* n_preallocs */
75                         (GInstanceInitFunc) modest_account_view_window_init,
76                 };
77                 my_type = g_type_register_static (GTK_TYPE_WINDOW,
78                                                   "ModestAccountViewWindow",
79                                                   &my_info, 0);
80         }
81         return my_type;
82 }
83
84 static void
85 modest_account_view_window_class_init (ModestAccountViewWindowClass *klass)
86 {
87         GObjectClass *gobject_class;
88         gobject_class = (GObjectClass*) klass;
89
90         parent_class            = g_type_class_peek_parent (klass);
91         gobject_class->finalize = modest_account_view_window_finalize;
92
93         g_type_class_add_private (gobject_class, sizeof(ModestAccountViewWindowPrivate));
94
95         /* signal definitions go here, e.g.: */
96 /*      signals[MY_SIGNAL_1] = */
97 /*              g_signal_new ("my_signal_1",....); */
98 /*      signals[MY_SIGNAL_2] = */
99 /*              g_signal_new ("my_signal_2",....); */
100 /*      etc. */
101 }
102
103 static void
104 modest_account_view_window_init (ModestAccountViewWindow *obj)
105 {
106
107 }
108
109 static void
110 modest_account_view_window_finalize (GObject *obj)
111 {
112         ModestAccountViewWindowPrivate *priv;
113                 
114         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(obj);
115
116         if (priv->conf) {
117                 g_object_unref (G_OBJECT(priv->conf));
118                 priv->conf = NULL;
119         }
120         
121         if (priv->widget_factory) {
122                 g_object_unref (G_OBJECT(priv->widget_factory));
123                 priv->widget_factory = NULL;
124         }
125 }
126
127
128 static void
129 on_selection_changed (GtkTreeSelection *sel, ModestAccountViewWindow *self)
130 {
131         ModestAccountViewWindowPrivate *priv;
132         GtkTreeModel                   *model;
133         GtkTreeIter                    iter;
134         gboolean                       has_selection;
135
136         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
137
138         has_selection =
139                 gtk_tree_selection_get_selected (sel, &model, &iter);
140
141         gtk_widget_set_sensitive (priv->edit_button, has_selection);
142         gtk_widget_set_sensitive (priv->remove_button, has_selection);  
143 }
144
145
146
147 static void
148 on_remove_button_clicked (GtkWidget *button, ModestAccountViewWindow *self)
149 {
150         g_message (__FUNCTION__);
151 }
152
153 static void
154 on_edit_button_clicked (GtkWidget *button, ModestAccountViewWindow *self)
155 {
156         g_message (__FUNCTION__);
157 }
158
159 static void
160 on_add_button_clicked (GtkWidget *button, ModestAccountViewWindow *self)
161 {
162         GtkWidget *wizard;
163         
164         wizard = modest_account_wizard_new ();
165         
166         gtk_widget_show (GTK_WIDGET(wizard));
167 }
168
169
170 static void
171 on_close_button_clicked (GtkWidget *button, ModestAccountViewWindow *self)
172 {
173         gtk_widget_destroy (GTK_WIDGET(self));
174 }
175
176
177
178 static GtkWidget*
179 button_box_new (ModestAccountViewWindow *self)
180 {
181
182         GtkWidget *button_box;
183         GtkWidget *add_button, *remove_button, *edit_button, *close_button;
184         ModestAccountViewWindowPrivate *priv;
185         
186         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
187         
188         button_box   = gtk_vbutton_box_new ();
189
190         add_button    = gtk_button_new_from_stock(GTK_STOCK_ADD);
191         remove_button = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
192         edit_button   = gtk_button_new_from_stock(GTK_STOCK_EDIT);
193
194         g_signal_connect (G_OBJECT(add_button), "clicked",
195                           G_CALLBACK(on_add_button_clicked),
196                           self);
197         g_signal_connect (G_OBJECT(remove_button), "clicked",
198                           G_CALLBACK(on_remove_button_clicked),
199                           self);
200         g_signal_connect (G_OBJECT(edit_button), "clicked",
201                           G_CALLBACK(on_edit_button_clicked),
202                           self);
203
204         gtk_box_pack_start (GTK_BOX(button_box), add_button, FALSE, FALSE,2);
205         gtk_box_pack_start (GTK_BOX(button_box), remove_button, FALSE, FALSE,2);
206         gtk_box_pack_start (GTK_BOX(button_box), edit_button, FALSE, FALSE,2);
207
208         gtk_widget_set_sensitive (edit_button, FALSE);
209         gtk_widget_set_sensitive (remove_button, FALSE);        
210
211         /* remember these, so we can deactivate them when nothing i
212          * selected */
213         priv->remove_button = remove_button;
214         priv->edit_button   = edit_button;
215         
216         return button_box;
217 }
218
219
220 static GtkWidget*
221 window_vbox_new (ModestAccountViewWindow *self)
222 {
223         ModestAccountViewWindowPrivate *priv;
224         GtkTreeSelection *sel;
225         GtkWidget *main_hbox, *main_vbox, *button_box;
226         GtkWidget *close_button;
227         GtkWidget *close_hbox;
228         ModestAccountView *account_view;
229
230         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(self);
231
232         main_vbox     = gtk_vbox_new (FALSE, 6);
233         main_hbox     = gtk_hbox_new (FALSE, 6);
234         
235         account_view = modest_widget_factory_get_account_view_widget (priv->widget_factory);
236
237         sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(account_view));
238         g_signal_connect (G_OBJECT(sel), "changed",  G_CALLBACK(on_selection_changed),
239                           self);
240         
241         button_box = button_box_new (self);
242         
243         gtk_box_pack_start (GTK_BOX(main_hbox), GTK_WIDGET(account_view), TRUE, TRUE, 2);
244         gtk_box_pack_start (GTK_BOX(main_hbox), button_box, FALSE, FALSE,2);
245
246         gtk_box_pack_start (GTK_BOX(main_vbox), main_hbox, TRUE, TRUE, 2);
247
248
249         close_button = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
250         g_signal_connect (G_OBJECT(close_button), "clicked",
251                           G_CALLBACK(on_close_button_clicked),
252                           self);
253         
254         close_hbox = gtk_hbox_new (FALSE, 2);
255         gtk_box_pack_end (GTK_BOX(close_hbox),
256                           close_button, FALSE, FALSE,2);
257         gtk_box_pack_end (GTK_BOX(main_vbox), close_hbox, FALSE, FALSE,2);
258
259         gtk_widget_show_all (main_vbox);
260         return main_vbox;
261 }
262
263
264 GtkWidget*
265 modest_account_view_window_new (ModestConf *conf, ModestWidgetFactory *factory)
266 {
267         GObject *obj;
268         ModestAccountViewWindowPrivate *priv;
269
270         g_return_val_if_fail (conf, NULL);
271         g_return_val_if_fail (factory, NULL);
272         
273         obj  = g_object_new(MODEST_TYPE_ACCOUNT_VIEW_WINDOW, NULL);
274         priv = MODEST_ACCOUNT_VIEW_WINDOW_GET_PRIVATE(obj);
275
276         g_object_ref (G_OBJECT(conf));
277         priv->conf = conf;
278         
279         g_object_ref (G_OBJECT(factory));
280         priv->widget_factory = factory;
281
282         gtk_window_set_resizable (GTK_WINDOW(obj), TRUE);
283         gtk_window_set_default_size (GTK_WINDOW(obj),
284                                      MODEST_ACCOUNT_VIEW_WINDOW_WIDTH_DEFAULT,
285                                      MODEST_ACCOUNT_VIEW_WINDOW_HEIGHT_DEFAULT);
286         gtk_window_set_resizable (GTK_WINDOW(obj), FALSE);
287
288         gtk_window_set_title (GTK_WINDOW(obj), _("Accounts"));
289         gtk_window_set_type_hint (GTK_WINDOW(obj), GDK_WINDOW_TYPE_HINT_DIALOG);
290         
291         gtk_window_set_modal (GTK_WINDOW(obj), TRUE);
292         
293         
294         gtk_container_add (GTK_CONTAINER(obj),
295                            window_vbox_new (MODEST_ACCOUNT_VIEW_WINDOW(obj)));
296                 
297         return GTK_WIDGET(obj);
298 }