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