* Updated files to fit the last Modest APIs
[modest] / src / maemo / modest-main-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 <gtk/gtktreeviewcolumn.h>
32
33 #include <widgets/modest-main-window.h>
34 #include <widgets/modest-edit-msg-window.h>
35 #include <modest-widget-factory.h>
36 #include "modest-widget-memory.h"
37 #include "modest-icon-factory.h"
38 #include "modest-ui.h"
39 #include "modest-account-view-window.h"
40 #include "modest-account-mgr.h"
41 #include "modest-conf.h"
42
43 #include "modest-tny-platform-factory.h"
44 #include "modest-tny-msg-actions.h"
45 #include "modest-mail-operation.h"
46 #include "modest-icon-names.h"
47
48 /* 'private'/'protected' functions */
49 static void modest_main_window_class_init    (ModestMainWindowClass *klass);
50 static void modest_main_window_init          (ModestMainWindow *obj);
51 static void modest_main_window_finalize      (GObject *obj);
52
53 static void restore_sizes (ModestMainWindow *self);
54 static void save_sizes (ModestMainWindow *self);
55
56 /* list my signals */
57 enum {
58         /* MY_SIGNAL_1, */
59         /* MY_SIGNAL_2, */
60         LAST_SIGNAL
61 };
62
63 typedef struct _ModestMainWindowPrivate ModestMainWindowPrivate;
64 struct _ModestMainWindowPrivate {
65
66         GtkUIManager *ui_manager;
67         ModestWidgetFactory *widget_factory;
68         TnyPlatformFactory *factory;
69         ModestTnyAccountStore *account_store;
70
71         GtkWidget *toolbar;
72         GtkWidget *msg_paned;
73         GtkWidget *main_paned;
74         
75         ModestHeaderView *header_view;
76         ModestFolderView *folder_view;
77         ModestMsgView    *msg_preview;
78 };
79
80
81 #define MODEST_MAIN_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
82                                                 MODEST_TYPE_MAIN_WINDOW, \
83                                                 ModestMainWindowPrivate))
84
85 typedef struct _GetMsgAsyncHelper {
86         ModestMainWindowPrivate *main_window_private;
87         guint action;
88         ModestMailOperationReplyType reply_type;
89         ModestMailOperationForwardType forward_type;
90         gchar *from;
91         TnyIterator *iter;
92 } GetMsgAsyncHelper;
93
94 /* globals */
95 static GtkWindowClass *parent_class = NULL;
96
97 /* uncomment the following if you have defined any signals */
98 /* static guint signals[LAST_SIGNAL] = {0}; */
99
100 GType
101 modest_main_window_get_type (void)
102 {
103         static GType my_type = 0;
104         if (!my_type) {
105                 static const GTypeInfo my_info = {
106                         sizeof(ModestMainWindowClass),
107                         NULL,           /* base init */
108                         NULL,           /* base finalize */
109                         (GClassInitFunc) modest_main_window_class_init,
110                         NULL,           /* class finalize */
111                         NULL,           /* class data */
112                         sizeof(ModestMainWindow),
113                         1,              /* n_preallocs */
114                         (GInstanceInitFunc) modest_main_window_init,
115                         NULL
116                 };
117                 my_type = g_type_register_static (GTK_TYPE_WINDOW,
118                                                   "ModestMainWindow",
119                                                   &my_info, 0);
120         }
121         return my_type;
122 }
123
124 static void
125 modest_main_window_class_init (ModestMainWindowClass *klass)
126 {
127         GObjectClass *gobject_class;
128         gobject_class = (GObjectClass*) klass;
129
130         parent_class            = g_type_class_peek_parent (klass);
131         gobject_class->finalize = modest_main_window_finalize;
132
133         g_type_class_add_private (gobject_class, sizeof(ModestMainWindowPrivate));
134
135         /* signal definitions go here, e.g.: */
136 /*      signals[MY_SIGNAL_1] = */
137 /*              g_signal_new ("my_signal_1",....); */
138 /*      signals[MY_SIGNAL_2] = */
139 /*              g_signal_new ("my_signal_2",....); */
140 /*      etc. */
141 }
142
143 static void
144 modest_main_window_init (ModestMainWindow *obj)
145 {
146         ModestMainWindowPrivate *priv;
147
148         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
149         
150         priv->factory = modest_tny_platform_factory_get_instance ();
151         priv->widget_factory = NULL;
152         priv->ui_manager = NULL;
153         priv->account_store = NULL;
154 }
155
156 static void
157 modest_main_window_finalize (GObject *obj)
158 {
159         ModestMainWindowPrivate *priv;  
160         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
161         if (priv->widget_factory) {
162                 g_object_unref (G_OBJECT(priv->widget_factory));
163                 priv->widget_factory = NULL;
164         }
165         if (priv->ui_manager) {
166                 g_object_unref (G_OBJECT(priv->ui_manager));
167                 priv->ui_manager = NULL;
168         }
169         if (priv->account_store) {
170                 g_object_unref (G_OBJECT(priv->account_store));
171                 priv->account_store = NULL;
172         }
173         
174         G_OBJECT_CLASS(parent_class)->finalize (obj);
175 }
176
177
178 static ModestHeaderView*
179 header_view_new (ModestMainWindow *self)
180 {
181         ModestHeaderView *header_view;
182         ModestMainWindowPrivate *priv;
183         
184         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
185         header_view = modest_widget_factory_get_header_view (priv->widget_factory);
186         modest_header_view_set_style (header_view, 0); /* don't show headers */
187         
188         return header_view;
189 }
190
191
192 static void
193 restore_sizes (ModestMainWindow *self)
194 {
195         ModestConf *conf;
196         ModestMainWindowPrivate *priv;
197         
198         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
199         conf = modest_tny_platform_factory_get_conf_instance
200                 (MODEST_TNY_PLATFORM_FACTORY(priv->factory));
201
202         modest_widget_memory_restore (conf,G_OBJECT(self),
203                                       "modest-main-window");
204         modest_widget_memory_restore (conf, G_OBJECT(priv->main_paned),
205                                       "modest-main-paned");
206         modest_widget_memory_restore (conf, G_OBJECT(priv->header_view),
207                                       "header-view");
208 }
209
210
211 static void
212 save_sizes (ModestMainWindow *self)
213 {
214         ModestMainWindowPrivate *priv;
215         ModestConf *conf;
216         
217         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
218         conf = modest_tny_platform_factory_get_conf_instance
219                 (MODEST_TNY_PLATFORM_FACTORY(priv->factory));
220         
221         modest_widget_memory_save (conf,G_OBJECT(self), "modest-main-window");
222         modest_widget_memory_save (conf, G_OBJECT(priv->main_paned),
223                                    "modest-main-paned");
224         modest_widget_memory_save (conf, G_OBJECT(priv->header_view), "header-view");
225 }
226
227 static GtkWidget*
228 wrapped_in_scrolled_window (GtkWidget *widget, gboolean needs_viewport)
229 {
230         GtkWidget *win;
231
232         win = gtk_scrolled_window_new (NULL, NULL);
233         gtk_scrolled_window_set_policy
234                 (GTK_SCROLLED_WINDOW (win),GTK_POLICY_NEVER,
235                  GTK_POLICY_AUTOMATIC);
236         
237         if (needs_viewport)
238                 gtk_scrolled_window_add_with_viewport
239                         (GTK_SCROLLED_WINDOW(win), widget);
240         else
241                 gtk_container_add (GTK_CONTAINER(win),
242                                    widget);
243
244         return win;
245 }
246
247
248 static gboolean
249 on_delete_event (GtkWidget *widget, GdkEvent  *event, ModestMainWindow *self)
250 {
251         save_sizes (self);
252         return FALSE;
253 }
254
255
256 ModestWindow*
257 modest_main_window_new (ModestWidgetFactory *widget_factory,
258                         TnyAccountStore *account_store)
259 {
260         GObject *obj;
261         ModestMainWindowPrivate *priv;
262         GtkWidget *main_vbox;
263         GtkWidget *status_hbox;
264         GtkWidget *header_win, *folder_win;
265         
266         g_return_val_if_fail (widget_factory, NULL);
267
268         obj  = g_object_new(MODEST_TYPE_MAIN_WINDOW, NULL);
269         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
270
271         priv->widget_factory = g_object_ref (widget_factory);
272         priv->account_store = g_object_ref (account_store);
273         //priv->ui_manager = g_object_ref (ui_manager);
274
275         /* Add accelerators */
276         gtk_window_add_accel_group (GTK_WINDOW (obj), 
277                                     gtk_ui_manager_get_accel_group (priv->ui_manager));
278
279
280         /* Toolbar / Menubar */
281         priv->toolbar = gtk_ui_manager_get_widget (priv->ui_manager, "/ToolBar");
282         gtk_toolbar_set_tooltips (GTK_TOOLBAR (priv->toolbar), TRUE);
283
284         /* widgets from factory */
285         priv->folder_view = modest_widget_factory_get_folder_view (widget_factory);
286         priv->header_view = header_view_new (MODEST_MAIN_WINDOW(obj));
287         
288         folder_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->folder_view),
289                                                  FALSE);
290         header_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->header_view),
291                                                  FALSE);                           
292
293         /* paned */
294         priv->main_paned = gtk_hpaned_new ();
295         gtk_paned_add1 (GTK_PANED(priv->main_paned), folder_win);
296         gtk_paned_add2 (GTK_PANED(priv->main_paned), header_win);
297         gtk_widget_show (GTK_WIDGET(priv->header_view));
298         
299         gtk_tree_view_columns_autosize (GTK_TREE_VIEW(priv->header_view));
300
301         
302         /* status bar / progress */
303         status_hbox = gtk_hbox_new (FALSE, 0);
304         gtk_box_pack_start (GTK_BOX(status_hbox),
305                             modest_widget_factory_get_folder_info_label (widget_factory),
306                             FALSE,FALSE, 6);
307         gtk_box_pack_start (GTK_BOX(status_hbox),
308                             modest_widget_factory_get_status_bar(widget_factory),
309                             TRUE, TRUE, 0);
310         gtk_box_pack_start (GTK_BOX(status_hbox),
311                             modest_widget_factory_get_progress_bar(widget_factory),
312                             FALSE, FALSE, 0);
313         gtk_box_pack_start (GTK_BOX(status_hbox),
314                           modest_widget_factory_get_online_toggle(widget_factory),
315                           FALSE, FALSE, 0);
316
317         /* putting it all together... */
318         main_vbox = gtk_vbox_new (FALSE, 6);
319         gtk_box_pack_start (GTK_BOX(main_vbox), priv->main_paned, TRUE, TRUE,0);
320         gtk_box_pack_start (GTK_BOX(main_vbox), priv->toolbar, FALSE, FALSE, 0);
321         gtk_box_pack_start (GTK_BOX(main_vbox), status_hbox, FALSE, FALSE, 0);
322
323         
324         gtk_container_add (GTK_CONTAINER(obj), main_vbox);
325         restore_sizes (MODEST_MAIN_WINDOW(obj));        
326
327         gtk_window_set_title (GTK_WINDOW(obj), _("Modest"));
328         gtk_window_set_icon  (GTK_WINDOW(obj),
329                               modest_icon_factory_get_icon (MODEST_APP_ICON));
330         
331         gtk_widget_show_all (main_vbox);
332
333         g_signal_connect (G_OBJECT(obj), "delete-event",
334                           G_CALLBACK(on_delete_event), obj);
335         
336         return (ModestWindow *) obj;
337 }
338
339 ModestWidgetFactory *
340 modest_main_window_get_widget_factory (ModestMainWindow *main_window)
341 {
342         ModestMainWindowPrivate *priv;
343                         
344         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW (main_window), NULL);
345
346         priv = MODEST_MAIN_WINDOW_GET_PRIVATE (main_window);
347
348         return g_object_ref (priv->widget_factory);
349 }
350         
351 TnyAccountStore * 
352 modest_main_window_get_account_store (ModestMainWindow *main_window)
353 {
354         ModestMainWindowPrivate *priv;
355                         
356
357         g_return_val_if_fail (MODEST_IS_MAIN_WINDOW (main_window), NULL);
358
359         priv = MODEST_MAIN_WINDOW_GET_PRIVATE (main_window);
360
361         return g_object_ref (priv->account_store);
362 }