* (gtk|maemo)/modest-main-window.c:
[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
68         GtkWidget *toolbar;
69         GtkWidget *msg_paned;
70         GtkWidget *main_paned;
71         
72         ModestWidgetFactory *widget_factory;
73         TnyPlatformFactory *factory;
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 }
152
153 static void
154 modest_main_window_finalize (GObject *obj)
155 {
156         ModestMainWindowPrivate *priv;  
157         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
158         if (priv->widget_factory) {
159                 g_object_unref (G_OBJECT(priv->widget_factory));
160                 priv->widget_factory = NULL;
161         }
162
163         G_OBJECT_CLASS(parent_class)->finalize (obj);
164 }
165
166
167 static ModestHeaderView*
168 header_view_new (ModestMainWindow *self)
169 {
170         int i;
171         ModestHeaderView *header_view;
172         ModestMainWindowPrivate *priv;
173         
174         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
175         header_view = modest_widget_factory_get_header_view (priv->widget_factory);
176         modest_header_view_set_style (header_view, 0); /* don't show headers */
177         
178         return header_view;
179 }
180
181
182 static void
183 restore_sizes (ModestMainWindow *self)
184 {
185         ModestConf *conf;
186         ModestMainWindowPrivate *priv;
187         
188         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
189         conf = modest_tny_platform_factory_get_modest_conf_instance (priv->factory);
190
191         modest_widget_memory_restore (conf,G_OBJECT(self),
192                                       "modest-main-window");
193         modest_widget_memory_restore (conf, G_OBJECT(priv->main_paned),
194                                       "modest-main-paned");
195         modest_widget_memory_restore (conf, G_OBJECT(priv->header_view),
196                                       "header-view");
197 }
198
199
200 static void
201 save_sizes (ModestMainWindow *self)
202 {
203         ModestMainWindowPrivate *priv;
204         ModestConf *conf;
205         
206         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
207         conf = modest_tny_platform_factory_get_modest_conf_instance (priv->factory);
208         
209         modest_widget_memory_save (conf,G_OBJECT(self), "modest-main-window");
210         modest_widget_memory_save (conf, G_OBJECT(priv->main_paned),
211                                    "modest-main-paned");
212         modest_widget_memory_save (conf, G_OBJECT(priv->header_view), "header-view");
213 }
214
215 static GtkWidget*
216 wrapped_in_scrolled_window (GtkWidget *widget, gboolean needs_viewport)
217 {
218         GtkWidget *win;
219
220         win = gtk_scrolled_window_new (NULL, NULL);
221         gtk_scrolled_window_set_policy
222                 (GTK_SCROLLED_WINDOW (win),GTK_POLICY_NEVER,
223                  GTK_POLICY_AUTOMATIC);
224         
225         if (needs_viewport)
226                 gtk_scrolled_window_add_with_viewport
227                         (GTK_SCROLLED_WINDOW(win), widget);
228         else
229                 gtk_container_add (GTK_CONTAINER(win),
230                                    widget);
231
232         return win;
233 }
234
235
236 static gboolean
237 on_delete_event (GtkWidget *widget, GdkEvent  *event, ModestMainWindow *self)
238 {
239         save_sizes (self);
240         return FALSE;
241 }
242
243
244 GtkWidget*
245 modest_main_window_new (ModestWidgetFactory *widget_factory,
246                         GtkUIManager *ui_manager)
247 {
248         GObject *obj;
249         ModestMainWindowPrivate *priv;
250         GtkWidget *main_vbox;
251         GtkWidget *status_hbox;
252         GtkWidget *header_win, *folder_win;
253         
254         g_return_val_if_fail (widget_factory, NULL);
255
256         obj  = g_object_new(MODEST_TYPE_MAIN_WINDOW, NULL);
257         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
258
259         priv->widget_factory = g_object_ref (widget_factory);
260         priv->ui_manager = g_object_ref (ui_manager);
261
262         /* Add accelerators */
263         gtk_window_add_accel_group (GTK_WINDOW (obj), 
264                                     gtk_ui_manager_get_accel_group (priv->ui_manager));
265
266
267         /* Toolbar / Menubar */
268         priv->toolbar = gtk_ui_manager_get_widget (priv->ui_manager, "/ToolBar");
269         gtk_toolbar_set_tooltips (GTK_TOOLBAR (priv->toolbar), TRUE);
270
271         /* widgets from factory */
272         priv->folder_view = modest_widget_factory_get_folder_view (widget_factory);
273         priv->header_view = header_view_new (MODEST_MAIN_WINDOW(obj));
274         
275         folder_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->folder_view),
276                                                  FALSE);
277         header_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->header_view),
278                                                  FALSE);                           
279
280         /* paned */
281         priv->main_paned = gtk_hpaned_new ();
282         gtk_paned_add1 (GTK_PANED(priv->main_paned), folder_win);
283         gtk_paned_add2 (GTK_PANED(priv->main_paned), header_win);
284         gtk_widget_show (GTK_WIDGET(priv->header_view));
285         
286         gtk_tree_view_columns_autosize (GTK_TREE_VIEW(priv->header_view));
287
288         
289         /* status bar / progress */
290         status_hbox = gtk_hbox_new (FALSE, 0);
291         gtk_box_pack_start (GTK_BOX(status_hbox),
292                             modest_widget_factory_get_folder_info_label (widget_factory),
293                             FALSE,FALSE, 6);
294         gtk_box_pack_start (GTK_BOX(status_hbox),
295                             modest_widget_factory_get_status_bar(widget_factory),
296                             TRUE, TRUE, 0);
297         gtk_box_pack_start (GTK_BOX(status_hbox),
298                             modest_widget_factory_get_progress_bar(widget_factory),
299                             FALSE, FALSE, 0);
300         gtk_box_pack_start (GTK_BOX(status_hbox),
301                           modest_widget_factory_get_online_toggle(widget_factory),
302                           FALSE, FALSE, 0);
303
304         /* putting it all together... */
305         main_vbox = gtk_vbox_new (FALSE, 6);
306         gtk_box_pack_start (GTK_BOX(main_vbox), priv->main_paned, TRUE, TRUE,0);
307         gtk_box_pack_start (GTK_BOX(main_vbox), priv->toolbar, FALSE, FALSE, 0);
308         gtk_box_pack_start (GTK_BOX(main_vbox), status_hbox, FALSE, FALSE, 0);
309
310         
311         gtk_container_add (GTK_CONTAINER(obj), main_vbox);
312         restore_sizes (MODEST_MAIN_WINDOW(obj));        
313
314         gtk_window_set_title (GTK_WINDOW(obj), _("Modest"));
315         gtk_window_set_icon  (GTK_WINDOW(obj),
316                               modest_icon_factory_get_icon (MODEST_APP_ICON));
317         
318         gtk_widget_show_all (main_vbox);
319
320         g_signal_connect (G_OBJECT(obj), "delete-event",
321                           G_CALLBACK(on_delete_event), obj);
322         
323         return GTK_WIDGET(obj);
324 }