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