* Window manager mimics the way close should work if main window implements the three...
[modest] / src / hildon2 / modest-folder-window.c
1 /* Copyright (c) 2008, 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-folder-window.h>
31 #include <modest-osso-state-saving.h>
32 #include <libosso.h>
33 #include <hildon/hildon-pannable-area.h>
34 #include <modest-window-mgr.h>
35 #include <modest-signal-mgr.h>
36 #include <modest-runtime.h>
37 #include <modest-platform.h>
38 #include <modest-maemo-utils.h>
39 #include <modest-icon-names.h>
40 #include <modest-ui-constants.h>
41 #include <modest-defs.h>
42 #include <hildon/hildon-program.h>
43 #include <hildon/hildon-banner.h>
44 #include <tny-account-store-view.h>
45
46 /* 'private'/'protected' functions */
47 static void modest_folder_window_class_init  (ModestFolderWindowClass *klass);
48 static void modest_folder_window_init        (ModestFolderWindow *obj);
49 static void modest_folder_window_finalize    (GObject *obj);
50
51 static void connect_signals (ModestFolderWindow *self);
52 static void modest_folder_window_disconnect_signals (ModestWindow *self);
53
54 static gboolean on_zoom_minus_plus_not_implemented (ModestWindow *window);
55
56 typedef struct _ModestFolderWindowPrivate ModestFolderWindowPrivate;
57 struct _ModestFolderWindowPrivate {
58
59         GtkWidget *folder_view;
60
61         /* signals */
62         GSList *sighandlers;
63
64         /* Display state */
65         osso_display_state_t display_state;
66 };
67 #define MODEST_FOLDER_WINDOW_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE((o), \
68                                                                           MODEST_TYPE_FOLDER_WINDOW, \
69                                                                           ModestFolderWindowPrivate))
70
71 /* globals */
72 static GtkWindowClass *parent_class = NULL;
73
74 /************************************************************************/
75
76 GType
77 modest_folder_window_get_type (void)
78 {
79         static GType my_type = 0;
80         if (!my_type) {
81                 static const GTypeInfo my_info = {
82                         sizeof(ModestFolderWindowClass),
83                         NULL,           /* base init */
84                         NULL,           /* base finalize */
85                         (GClassInitFunc) modest_folder_window_class_init,
86                         NULL,           /* class finalize */
87                         NULL,           /* class data */
88                         sizeof(ModestFolderWindow),
89                         1,              /* n_preallocs */
90                         (GInstanceInitFunc) modest_folder_window_init,
91                         NULL
92                 };
93                 my_type = g_type_register_static (MODEST_TYPE_WINDOW,
94                                                   "ModestFolderWindow",
95                                                   &my_info, 0);
96         }
97         return my_type;
98 }
99
100 static void
101 modest_folder_window_class_init (ModestFolderWindowClass *klass)
102 {
103         GObjectClass *gobject_class;
104         gobject_class = (GObjectClass*) klass;
105         ModestWindowClass *modest_window_class = (ModestWindowClass *) klass;
106
107         parent_class            = g_type_class_peek_parent (klass);
108         gobject_class->finalize = modest_folder_window_finalize;
109
110         g_type_class_add_private (gobject_class, sizeof(ModestFolderWindowPrivate));
111         
112         modest_window_class->zoom_minus_func = on_zoom_minus_plus_not_implemented;
113         modest_window_class->zoom_plus_func = on_zoom_minus_plus_not_implemented;
114         modest_window_class->disconnect_signals_func = modest_folder_window_disconnect_signals;
115 }
116
117 static void
118 modest_folder_window_init (ModestFolderWindow *obj)
119 {
120         ModestFolderWindowPrivate *priv;
121
122         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(obj);
123
124         priv->sighandlers = NULL;
125         priv->display_state = OSSO_DISPLAY_ON;
126         
127         priv->folder_view = NULL;
128         
129         modest_window_mgr_register_help_id (modest_runtime_get_window_mgr(),
130                                             GTK_WINDOW(obj),
131                                             "applications_email_folderview");
132 }
133
134 static void
135 modest_folder_window_finalize (GObject *obj)
136 {
137         ModestFolderWindowPrivate *priv;
138
139         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(obj);
140
141         /* Sanity check: shouldn't be needed, the window mgr should
142            call this function before */
143         modest_folder_window_disconnect_signals (MODEST_WINDOW (obj));  
144
145         G_OBJECT_CLASS(parent_class)->finalize (obj);
146 }
147
148 static void
149 modest_folder_window_disconnect_signals (ModestWindow *self)
150 {       
151         ModestFolderWindowPrivate *priv;        
152         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(self);
153
154         modest_signal_mgr_disconnect_all_and_destroy (priv->sighandlers);
155         priv->sighandlers = NULL;       
156 }
157
158 static void
159 connect_signals (ModestFolderWindow *self)
160 {       
161         ModestFolderWindowPrivate *priv;
162         
163         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(self);
164
165         /* folder view */
166
167         /* TODO: connect folder view activate */
168         
169         /* window */
170
171         /* we don't register this in sighandlers, as it should be run after disconnecting all signals,
172          * in destroy stage */
173
174         
175 }
176
177 static void 
178 osso_display_event_cb (osso_display_state_t state, 
179                        gpointer data)
180 {
181         ModestFolderWindowPrivate *priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (data);
182
183         priv->display_state = state;
184
185         /* Stop blinking if the screen becomes on */
186         if (priv->display_state == OSSO_DISPLAY_ON)
187                 modest_platform_remove_new_mail_notifications (TRUE);
188 }
189
190 ModestWindow *
191 modest_folder_window_new (TnyFolderStoreQuery *query)
192 {
193         ModestFolderWindow *self = NULL;        
194         ModestFolderWindowPrivate *priv = NULL;
195         HildonProgram *app;
196         GdkPixbuf *window_icon;
197         GtkWidget *pannable;
198         
199         self  = MODEST_FOLDER_WINDOW(g_object_new(MODEST_TYPE_FOLDER_WINDOW, NULL));
200         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE(self);
201         pannable = hildon_pannable_area_new ();
202         priv->folder_view  = modest_platform_create_folder_view (query);
203
204         /* Set account store */
205         tny_account_store_view_set_account_store (TNY_ACCOUNT_STORE_VIEW (priv->folder_view),
206                                                   TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
207
208         gtk_container_add (GTK_CONTAINER (pannable), priv->folder_view);
209         gtk_container_add (GTK_CONTAINER (self), pannable);
210
211         gtk_widget_show (priv->folder_view);
212         gtk_widget_show (pannable);
213
214         connect_signals (MODEST_FOLDER_WINDOW (self));
215
216         /* Load previous osso state, for instance if we are being restored from 
217          * hibernation:  */
218         modest_osso_load_state ();
219
220         /* Get device name */
221         modest_maemo_utils_get_device_name ();
222
223         app = hildon_program_get_instance ();
224         hildon_program_add_window (app, HILDON_WINDOW (self));
225         
226         /* Set window icon */
227         window_icon = modest_platform_get_icon (MODEST_APP_ICON, MODEST_ICON_SIZE_BIG);
228         if (window_icon) {
229                 gtk_window_set_icon (GTK_WINDOW (self), window_icon);
230                 g_object_unref (window_icon);
231         }
232
233         /* Listen for changes in the screen, we don't want to show a
234            led pattern when the display is on for example */
235         osso_hw_set_display_event_cb (modest_maemo_utils_get_osso_context (),
236                                       osso_display_event_cb,
237                                       self); 
238
239         /* Dont't restore settings here, 
240          * because it requires a gtk_widget_show(), 
241          * and we don't want to do that until later,
242          * so that the UI is not visible for non-menu D-Bus activation.
243          */
244
245         return MODEST_WINDOW(self);
246 }
247
248 static gboolean
249 on_zoom_minus_plus_not_implemented (ModestWindow *window)
250 {
251         g_return_val_if_fail (MODEST_IS_FOLDER_WINDOW (window), FALSE);
252
253         hildon_banner_show_information (NULL, NULL, dgettext("hildon-common-strings", "ckct_ib_cannot_zoom_here"));
254         return FALSE;
255
256 }
257
258 gboolean
259 modest_folder_window_screen_is_on (ModestFolderWindow *self)
260 {
261         ModestFolderWindowPrivate *priv = NULL;
262
263         g_return_val_if_fail (MODEST_IS_FOLDER_WINDOW(self), FALSE);
264
265         priv = MODEST_FOLDER_WINDOW_GET_PRIVATE (self);
266         
267         return (priv->display_state == OSSO_DISPLAY_ON) ? TRUE : FALSE;
268 }
269