* src/modest-ui-actions.[ch]:
[modest] / src / widgets / modest-window-mgr.c
1 /* Copyright (c) 2006,2007 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 <string.h>
31 #include "modest-window-mgr.h"
32 #include "widgets/modest-main-window.h"
33 #include "widgets/modest-msg-edit-window.h"
34 #include "widgets/modest-msg-view-window.h"
35 /* include other impl specific header files */
36
37 /* 'private'/'protected' functions */
38 static void modest_window_mgr_class_init (ModestWindowMgrClass *klass);
39 static void modest_window_mgr_init       (ModestWindowMgr *obj);
40 static void modest_window_mgr_finalize   (GObject *obj);
41
42 static void on_window_destroy            (ModestWindow *window, 
43                                           ModestWindowMgr *self);
44
45 /* list my signals  */
46 enum {
47         /* MY_SIGNAL_1, */
48         /* MY_SIGNAL_2, */
49         LAST_SIGNAL
50 };
51
52 typedef struct _ModestWindowMgrPrivate ModestWindowMgrPrivate;
53 struct _ModestWindowMgrPrivate {
54         GList *window_list;
55         gboolean fullscreen_mode;
56 };
57 #define MODEST_WINDOW_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
58                                                MODEST_TYPE_WINDOW_MGR, \
59                                                ModestWindowMgrPrivate))
60 /* globals */
61 static GObjectClass *parent_class = NULL;
62
63 /* uncomment the following if you have defined any signals */
64 /* static guint signals[LAST_SIGNAL] = {0}; */
65
66 GType
67 modest_window_mgr_get_type (void)
68 {
69         static GType my_type = 0;
70         if (!my_type) {
71                 static const GTypeInfo my_info = {
72                         sizeof(ModestWindowMgrClass),
73                         NULL,           /* base init */
74                         NULL,           /* base finalize */
75                         (GClassInitFunc) modest_window_mgr_class_init,
76                         NULL,           /* class finalize */
77                         NULL,           /* class data */
78                         sizeof(ModestWindowMgr),
79                         1,              /* n_preallocs */
80                         (GInstanceInitFunc) modest_window_mgr_init,
81                         NULL
82                 };
83                 my_type = g_type_register_static (G_TYPE_OBJECT,
84                                                   "ModestWindowMgr",
85                                                   &my_info, 0);
86         }
87         return my_type;
88 }
89
90 static void
91 modest_window_mgr_class_init (ModestWindowMgrClass *klass)
92 {
93         GObjectClass *gobject_class;
94         gobject_class = (GObjectClass*) klass;
95
96         parent_class            = g_type_class_peek_parent (klass);
97         gobject_class->finalize = modest_window_mgr_finalize;
98
99         g_type_class_add_private (gobject_class, sizeof(ModestWindowMgrPrivate));
100 }
101
102 static void
103 modest_window_mgr_init (ModestWindowMgr *obj)
104 {
105         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE(obj);
106
107         priv->window_list = NULL;
108         priv->fullscreen_mode = FALSE;
109 }
110
111 static void
112 modest_window_mgr_finalize (GObject *obj)
113 {
114         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE(obj);
115
116         if (priv->window_list) {
117                 GList *iter = priv->window_list;
118                 /* unregister pending windows */
119                 while (iter) {
120                         modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (obj), 
121                                                              MODEST_WINDOW (iter->data));
122                         iter = g_list_next (iter);
123                 }
124                 g_list_free (priv->window_list);
125                 priv->window_list = NULL;
126         }
127
128         G_OBJECT_CLASS(parent_class)->finalize (obj);
129 }
130
131 ModestWindowMgr*
132 modest_window_mgr_new (void)
133 {
134         return MODEST_WINDOW_MGR(g_object_new(MODEST_TYPE_WINDOW_MGR, NULL));
135 }
136
137 void 
138 modest_window_mgr_register_window (ModestWindowMgr *self, 
139                                    ModestWindow *window)
140 {
141         GList *win;
142         ModestWindowMgrPrivate *priv;
143
144         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
145         g_return_if_fail (MODEST_IS_WINDOW (window));
146
147         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
148
149         win = g_list_find (priv->window_list, window);
150         if (win) {
151                 g_warning ("Trying to register an already registered window");
152                 return;
153         }
154
155         /* Add to list. Keep a reference to the window */
156         g_object_ref (window);
157         priv->window_list = g_list_prepend (priv->window_list, window);
158
159         /* Listen to object destruction */
160         g_signal_connect (window, "destroy", G_CALLBACK (on_window_destroy), self);
161
162         /* Put into fullscreen if needed */
163         if (priv->fullscreen_mode)
164                 gtk_window_fullscreen (GTK_WINDOW (window));
165 }
166
167 static void
168 on_window_destroy (ModestWindow *window, ModestWindowMgr *self)
169 {
170         /* Specific stuff first */
171         if (MODEST_IS_MAIN_WINDOW (window)) {
172                 ModestWindowMgrPrivate *priv;
173                 priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
174
175                 /* If more than one window already opened */
176                 if (g_list_length (priv->window_list) > 1) {
177
178                         /* If the user wants to close all the windows */
179                         if (modest_main_window_close_all (MODEST_MAIN_WINDOW (window))) {
180                                 GList *iter = priv->window_list;
181                                 do {
182                                         if (iter->data != window) {
183                                                 GList *tmp = iter->next;
184                                                 on_window_destroy (MODEST_WINDOW (iter->data),
185                                                                    self);
186                                                 iter = tmp;
187                                         } else {
188                                                 iter = g_list_next (iter);
189                                         }
190                                 } while (iter);
191                         }
192                 }
193         } else {
194                 if (MODEST_IS_MSG_EDIT_WINDOW (window)) {
195                         /* TODO: Save currently edited message to Drafts
196                            folder */
197                 }
198         }
199
200         /* Unregister window */
201         modest_window_mgr_unregister_window (self, window);
202 }
203
204 void 
205 modest_window_mgr_unregister_window (ModestWindowMgr *self, 
206                                      ModestWindow *window)
207 {
208         GList *win;
209         ModestWindowMgrPrivate *priv;
210
211         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
212         g_return_if_fail (MODEST_IS_WINDOW (window));
213
214         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
215
216         win = g_list_find (priv->window_list, window);
217         if (!win) {
218                 g_warning ("Trying to unregister a window that has not being registered yet");
219                 return;
220         }
221
222         /* Remove from list. Remove the reference to the window */
223         g_object_unref (win->data);
224         priv->window_list = g_list_remove_link (priv->window_list, win);
225
226         /* If there are no more windows registered then exit program */
227         if (priv->window_list == NULL)
228                 gtk_main_quit ();
229 }
230
231 static gint
232 compare_msguids (ModestWindow *win,
233                  const gchar *uid)
234 {
235         const gchar *msg_uid;
236
237         if (!MODEST_IS_MSG_VIEW_WINDOW (win))
238                 return 1;
239
240         /* Get message uid from msg window */
241         msg_uid = modest_msg_view_window_get_message_uid (MODEST_MSG_VIEW_WINDOW (win));
242         if (msg_uid && !strcmp (msg_uid, uid))
243                 return 0;
244         else
245                 return 1;
246 }
247
248 ModestWindow*  
249 modest_window_mgr_find_window_by_msguid (ModestWindowMgr *self, 
250                                          const gchar *msguid)
251 {
252         ModestWindowMgrPrivate *priv;
253         GList *win = NULL;
254
255         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), NULL);
256         g_return_val_if_fail (msguid != NULL, NULL);
257
258         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
259
260         /* Look for the window */
261         if (priv->window_list)
262                 win = g_list_find_custom (priv->window_list, 
263                                           msguid, 
264                                           (GCompareFunc) compare_msguids);
265
266         if (win)
267                 return win->data;
268         else 
269                 return NULL;
270 }
271
272 void
273 modest_window_mgr_set_fullscreen_mode (ModestWindowMgr *self,
274                                        gboolean on)
275 {
276         ModestWindowMgrPrivate *priv;
277         GList *win = NULL;
278
279         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
280
281         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
282
283         /* If there is no change do nothing */
284         if (priv->fullscreen_mode == on)
285                 return;
286
287         priv->fullscreen_mode = on;
288
289         /* Update windows */
290         win = priv->window_list;
291         while (win) {
292                 if (on)
293                         gtk_window_fullscreen (GTK_WINDOW (win->data));
294                 else
295                         gtk_window_unfullscreen (GTK_WINDOW (win->data));
296                 win = g_list_next (win);
297         }
298 }
299
300 gboolean
301 modest_window_mgr_get_fullscreen_mode (ModestWindowMgr *self)
302 {
303         ModestWindowMgrPrivate *priv;
304
305         g_return_val_if_fail (MODEST_IS_WINDOW_MGR (self), FALSE);
306
307         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
308
309         return priv->fullscreen_mode;
310 }