* fix for modest-icon-factory removal
[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 <hildon-widgets/hildon-window.h>
31
32 #include <glib/gi18n.h>
33 #include <gtk/gtktreeviewcolumn.h>
34 #include <modest-runtime.h>
35 #include <widgets/modest-main-window.h>
36 #include <widgets/modest-edit-msg-window.h>
37 #include <modest-widget-factory.h>
38 #include "modest-widget-memory.h"
39 #include "modest-window-priv.h"
40 #include "modest-icon-factory.h"
41 #include "modest-ui.h"
42 #include "modest-main-window-ui.h"
43 #include "modest-account-view-window.h"
44 #include "modest-account-mgr.h"
45 #include "modest-conf.h"
46
47 #include "modest-tny-platform-factory.h"
48 #include "modest-tny-msg-actions.h"
49 #include "modest-mail-operation.h"
50 #include "modest-icon-names.h"
51
52 /* 'private'/'protected' functions */
53 static void modest_main_window_class_init    (ModestMainWindowClass *klass);
54 static void modest_main_window_init          (ModestMainWindow *obj);
55 static void modest_main_window_finalize      (GObject *obj);
56
57 static void restore_sizes (ModestMainWindow *self);
58 static void save_sizes (ModestMainWindow *self);
59
60 /* list my signals */
61 enum {
62         /* MY_SIGNAL_1, */
63         /* MY_SIGNAL_2, */
64         LAST_SIGNAL
65 };
66
67 typedef struct _ModestMainWindowPrivate ModestMainWindowPrivate;
68 struct _ModestMainWindowPrivate {
69
70         GtkWidget *msg_paned;
71         GtkWidget *main_paned;
72         
73         ModestHeaderView *header_view;
74         ModestFolderView *folder_view;
75         ModestMsgView    *msg_preview;
76 };
77
78
79 #define MODEST_MAIN_WINDOW_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
80                                                 MODEST_TYPE_MAIN_WINDOW, \
81                                                 ModestMainWindowPrivate))
82
83 typedef struct _GetMsgAsyncHelper {
84         ModestMainWindowPrivate *main_window_private;
85         guint action;
86         ModestMailOperationReplyType reply_type;
87         ModestMailOperationForwardType forward_type;
88         gchar *from;
89         TnyIterator *iter;
90 } GetMsgAsyncHelper;
91
92 /* globals */
93 static GtkWindowClass *parent_class = NULL;
94
95 /* uncomment the following if you have defined any signals */
96 /* static guint signals[LAST_SIGNAL] = {0}; */
97
98 GType
99 modest_main_window_get_type (void)
100 {
101         static GType my_type = 0;
102         if (!my_type) {
103                 static const GTypeInfo my_info = {
104                         sizeof(ModestMainWindowClass),
105                         NULL,           /* base init */
106                         NULL,           /* base finalize */
107                         (GClassInitFunc) modest_main_window_class_init,
108                         NULL,           /* class finalize */
109                         NULL,           /* class data */
110                         sizeof(ModestMainWindow),
111                         1,              /* n_preallocs */
112                         (GInstanceInitFunc) modest_main_window_init,
113                         NULL
114                 };
115                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
116                                                   "ModestMainWindow",
117                                                   &my_info, 0);
118         }
119         return my_type;
120 }
121
122 static void
123 modest_main_window_class_init (ModestMainWindowClass *klass)
124 {
125         GObjectClass *gobject_class;
126         gobject_class = (GObjectClass*) klass;
127
128         parent_class            = g_type_class_peek_parent (klass);
129         gobject_class->finalize = modest_main_window_finalize;
130
131         g_type_class_add_private (gobject_class, sizeof(ModestMainWindowPrivate));
132 }
133
134 static void
135 modest_main_window_init (ModestMainWindow *obj)
136 {
137         ModestMainWindowPrivate *priv;
138
139         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
140
141         priv->msg_paned    = NULL;
142         priv->main_paned   = NULL;      
143         priv->header_view  = NULL;
144         priv->folder_view  = NULL;
145         priv->msg_preview  = NULL;      
146 }
147
148 static void
149 modest_main_window_finalize (GObject *obj)
150 {
151         G_OBJECT_CLASS(parent_class)->finalize (obj);
152 }
153
154
155 static ModestHeaderView*
156 header_view_new (ModestMainWindow *self)
157 {
158         ModestHeaderView *header_view;
159         
160         header_view = modest_widget_factory_get_header_view
161                 (modest_runtime_get_widget_factory());
162         modest_header_view_set_style (header_view, 0); /* don't show headers */
163                                                              
164         return header_view;
165 }
166
167
168 static void
169 restore_sizes (ModestMainWindow *self)
170 {
171         ModestConf *conf;
172         ModestMainWindowPrivate *priv;
173
174         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
175
176         conf = modest_runtime_get_conf ();
177         
178         modest_widget_memory_restore (conf,G_OBJECT(self),
179                                       "modest-main-window");
180         modest_widget_memory_restore (conf, G_OBJECT(priv->main_paned),
181                                       "modest-main-paned");
182         modest_widget_memory_restore (conf, G_OBJECT(priv->header_view),
183                                       "header-view");
184 }
185
186
187 static void
188 save_sizes (ModestMainWindow *self)
189 {
190         ModestConf *conf;
191         ModestMainWindowPrivate *priv;
192                 
193         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(self);
194         conf = modest_runtime_get_conf ();
195         
196         modest_widget_memory_save (conf,G_OBJECT(self), "modest-main-window");
197         modest_widget_memory_save (conf, G_OBJECT(priv->main_paned),
198                                    "modest-main-paned");
199         modest_widget_memory_save (conf, G_OBJECT(priv->header_view), "header-view");
200 }
201
202 static GtkWidget*
203 wrapped_in_scrolled_window (GtkWidget *widget, gboolean needs_viewport)
204 {
205         GtkWidget *win;
206
207         win = gtk_scrolled_window_new (NULL, NULL);
208         gtk_scrolled_window_set_policy
209                 (GTK_SCROLLED_WINDOW (win),GTK_POLICY_NEVER,
210                  GTK_POLICY_AUTOMATIC);
211         
212         if (needs_viewport)
213                 gtk_scrolled_window_add_with_viewport
214                         (GTK_SCROLLED_WINDOW(win), widget);
215         else
216                 gtk_container_add (GTK_CONTAINER(win),
217                                    widget);
218
219         return win;
220 }
221
222
223 static gboolean
224 on_delete_event (GtkWidget *widget, GdkEvent  *event, ModestMainWindow *self)
225 {
226         save_sizes (self);
227         return FALSE;
228 }
229
230 static GtkWidget *
231 menubar_to_menu (GtkUIManager *ui_manager)
232 {
233         GtkWidget *main_menu;
234         GtkWidget *menubar;
235         GList *iter;
236
237         /* Create new main menu */
238         main_menu = gtk_menu_new();
239
240         /* Get the menubar from the UI manager */
241         menubar = gtk_ui_manager_get_widget (ui_manager, "/MenuBar");
242
243         iter = gtk_container_get_children (GTK_CONTAINER (menubar));
244         while (iter) {
245                 GtkWidget *menu;
246
247                 menu = GTK_WIDGET (iter->data);
248                 gtk_widget_reparent(menu, main_menu);
249
250                 iter = g_list_next (iter);
251         }
252         return main_menu;
253 }
254
255 static GtkWidget*
256 get_toolbar (ModestMainWindow *self)
257 {
258         GtkWidget   *progress_bar,
259                     *toolbar, *progress_box, *progress_alignment;
260         GtkToolItem *progress_item;
261         ModestWindowPrivate *parent_priv;
262         ModestWidgetFactory *widget_factory;
263         GtkWidget   *stop_icon;
264         
265         parent_priv = MODEST_WINDOW_GET_PRIVATE(self);
266         widget_factory = modest_runtime_get_widget_factory();
267         
268         /* Toolbar */
269         progress_bar  = modest_widget_factory_get_progress_bar(widget_factory);
270         toolbar       = gtk_ui_manager_get_widget (parent_priv->ui_manager, "/ToolBar");
271
272         gtk_progress_bar_set_text (GTK_PROGRESS_BAR(progress_bar), "Connecting...");
273
274         progress_box        = gtk_hbox_new (FALSE, HILDON_MARGIN_DEFAULT);
275         progress_alignment  = gtk_alignment_new (0.5, 0.5, 1, 0);
276         
277         gtk_container_add  (GTK_CONTAINER(progress_alignment), progress_bar);
278         gtk_box_pack_start (GTK_BOX(progress_box), progress_alignment, TRUE, TRUE, 0);
279         
280         progress_item  = gtk_tool_item_new ();
281         gtk_container_add (GTK_CONTAINER(progress_item), progress_box);
282         gtk_tool_item_set_homogeneous (progress_item, FALSE);
283         gtk_tool_item_set_expand(progress_item, TRUE);
284         
285         stop_icon = gtk_image_new_from_icon_name("qgn_toolb_gene_stop", GTK_ICON_SIZE_BUTTON);
286
287         gtk_toolbar_insert (GTK_TOOLBAR(toolbar), gtk_tool_button_new(stop_icon, NULL),
288                             gtk_toolbar_get_n_items(GTK_TOOLBAR(toolbar)));
289
290         gtk_toolbar_insert (GTK_TOOLBAR(toolbar), progress_item,
291                             gtk_toolbar_get_n_items(GTK_TOOLBAR(toolbar)));
292         gtk_widget_show_all (toolbar);
293         return toolbar;
294 }
295         
296 ModestWindow*
297 modest_main_window_new (void)
298 {
299         GObject *obj;
300         ModestMainWindowPrivate *priv;
301         ModestWidgetFactory *widget_factory;
302         ModestWindowPrivate *parent_priv;
303         GtkWidget *main_vbox;
304         GtkWidget *header_win, *folder_win;
305         GtkActionGroup *action_group;
306         GError *error = NULL;
307
308         obj  = g_object_new(MODEST_TYPE_MAIN_WINDOW, NULL);
309
310         priv = MODEST_MAIN_WINDOW_GET_PRIVATE(obj);
311         parent_priv = MODEST_WINDOW_GET_PRIVATE(obj);
312
313         widget_factory = modest_runtime_get_widget_factory ();
314         
315         parent_priv->ui_manager = gtk_ui_manager_new();
316         action_group = gtk_action_group_new ("ModestMainWindowActions");
317
318         /* Add common actions */
319         gtk_action_group_add_actions (action_group,
320                                       modest_action_entries,
321                                       G_N_ELEMENTS (modest_action_entries),
322                                       obj);
323
324         gtk_ui_manager_insert_action_group (parent_priv->ui_manager, action_group, 0);
325         g_object_unref (action_group);
326
327         /* Load the UI definition */
328         gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager, MODEST_UIDIR "modest-ui.xml", &error);
329         if (error != NULL) {
330                 g_warning ("Could not merge modest-ui.xml: %s", error->message);
331                 g_error_free (error);
332                 error = NULL;
333         }
334
335         /* Add accelerators */
336         gtk_window_add_accel_group (GTK_WINDOW (obj), 
337                                     gtk_ui_manager_get_accel_group (parent_priv->ui_manager));
338
339         /* add the toolbar */
340         parent_priv->toolbar = get_toolbar(MODEST_MAIN_WINDOW(obj));
341         hildon_window_add_toolbar (HILDON_WINDOW (obj), GTK_TOOLBAR (parent_priv->toolbar));
342
343         /* Menubar */
344         parent_priv->menubar = menubar_to_menu (parent_priv->ui_manager);
345         hildon_window_set_menu (HILDON_WINDOW (obj), GTK_MENU (parent_priv->menubar));
346
347
348         /* widgets from factory */
349         priv->folder_view = modest_widget_factory_get_folder_view (widget_factory);
350         priv->header_view = header_view_new (MODEST_MAIN_WINDOW(obj));
351         priv->msg_preview = modest_widget_factory_get_msg_preview (widget_factory);
352         
353         folder_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->folder_view),
354                                                  FALSE);
355         header_win = wrapped_in_scrolled_window (GTK_WIDGET(priv->header_view),
356                                                  FALSE);                           
357
358         /* paned */
359         priv->main_paned = gtk_hpaned_new ();
360         gtk_paned_add1 (GTK_PANED(priv->main_paned), folder_win);
361         gtk_paned_add2 (GTK_PANED(priv->main_paned), header_win);
362         gtk_widget_show (GTK_WIDGET(priv->header_view));
363         
364         gtk_tree_view_columns_autosize (GTK_TREE_VIEW(priv->header_view));
365
366         /* putting it all together... */
367         main_vbox = gtk_vbox_new (FALSE, 6);
368         gtk_box_pack_start (GTK_BOX(main_vbox), priv->main_paned, TRUE, TRUE,0);
369
370         gtk_container_add (GTK_CONTAINER(obj), main_vbox);
371         restore_sizes (MODEST_MAIN_WINDOW(obj));        
372
373         gtk_window_set_title (GTK_WINDOW(obj), _("Modest"));
374         gtk_window_set_icon  (GTK_WINDOW(obj),
375                               modest_icon_factory_get_icon (MODEST_APP_ICON));
376         
377         gtk_widget_show_all (main_vbox);
378
379         g_signal_connect (G_OBJECT(obj), "delete-event",
380                           G_CALLBACK(on_delete_event), obj);
381         
382         return (ModestWindow *) obj;
383 }