* Added window manager calls
[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 "modest-window-mgr.h"
31 /* include other impl specific header files */
32
33 /* 'private'/'protected' functions */
34 static void modest_window_mgr_class_init (ModestWindowMgrClass *klass);
35 static void modest_window_mgr_init       (ModestWindowMgr *obj);
36 static void modest_window_mgr_finalize   (GObject *obj);
37
38 static void on_window_destroy            (ModestWindow *window, 
39                                           ModestWindowMgr *self);
40
41 /* list my signals  */
42 enum {
43         /* MY_SIGNAL_1, */
44         /* MY_SIGNAL_2, */
45         LAST_SIGNAL
46 };
47
48 typedef struct _ModestWindowMgrPrivate ModestWindowMgrPrivate;
49 struct _ModestWindowMgrPrivate {
50         GList *window_list;
51 };
52 #define MODEST_WINDOW_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
53                                                MODEST_TYPE_WINDOW_MGR, \
54                                                ModestWindowMgrPrivate))
55 /* globals */
56 static GObjectClass *parent_class = NULL;
57
58 /* uncomment the following if you have defined any signals */
59 /* static guint signals[LAST_SIGNAL] = {0}; */
60
61 GType
62 modest_window_mgr_get_type (void)
63 {
64         static GType my_type = 0;
65         if (!my_type) {
66                 static const GTypeInfo my_info = {
67                         sizeof(ModestWindowMgrClass),
68                         NULL,           /* base init */
69                         NULL,           /* base finalize */
70                         (GClassInitFunc) modest_window_mgr_class_init,
71                         NULL,           /* class finalize */
72                         NULL,           /* class data */
73                         sizeof(ModestWindowMgr),
74                         1,              /* n_preallocs */
75                         (GInstanceInitFunc) modest_window_mgr_init,
76                         NULL
77                 };
78                 my_type = g_type_register_static (G_TYPE_OBJECT,
79                                                   "ModestWindowMgr",
80                                                   &my_info, 0);
81         }
82         return my_type;
83 }
84
85 static void
86 modest_window_mgr_class_init (ModestWindowMgrClass *klass)
87 {
88         GObjectClass *gobject_class;
89         gobject_class = (GObjectClass*) klass;
90
91         parent_class            = g_type_class_peek_parent (klass);
92         gobject_class->finalize = modest_window_mgr_finalize;
93
94         g_type_class_add_private (gobject_class, sizeof(ModestWindowMgrPrivate));
95 }
96
97 static void
98 modest_window_mgr_init (ModestWindowMgr *obj)
99 {
100         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE(obj);
101
102         priv->window_list = NULL;
103 }
104
105 static void
106 modest_window_mgr_finalize (GObject *obj)
107 {
108         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE(obj);
109
110         if (priv->window_list) {
111                 GList *iter = priv->window_list;
112                 /* unregister pending windows */
113                 while (iter) {
114                         modest_window_mgr_unregister_window (MODEST_WINDOW_MGR (obj), 
115                                                              MODEST_WINDOW (iter->data));
116                         iter = g_list_next (iter);
117                 }
118                 g_list_free (priv->window_list);
119                 priv->window_list = NULL;
120         }
121
122         G_OBJECT_CLASS(parent_class)->finalize (obj);
123 }
124
125 ModestWindowMgr*
126 modest_window_mgr_new (void)
127 {
128         return MODEST_WINDOW_MGR(g_object_new(MODEST_TYPE_WINDOW_MGR, NULL));
129 }
130
131 void 
132 modest_window_mgr_register_window (ModestWindowMgr *self, 
133                                    ModestWindow *window)
134 {
135         GList *win;
136         ModestWindowMgrPrivate *priv;
137
138         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
139         g_return_if_fail (MODEST_IS_WINDOW (window));
140
141         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
142
143         win = g_list_find (priv->window_list, window);
144         if (win) {
145                 g_warning ("Trying to register an already registered window");
146                 return;
147         }
148
149         /* Add to list. Keep a reference to the window */
150         g_object_ref (window);
151         priv->window_list = g_list_prepend (priv->window_list, window);
152
153         /* Listen to object destruction */
154         g_signal_connect (window, "destroy", G_CALLBACK (on_window_destroy), self);
155 }
156
157 static void
158 on_window_destroy (ModestWindow *window, ModestWindowMgr *self)
159 {
160         modest_window_mgr_unregister_window (self, window);
161 }
162
163 void 
164 modest_window_mgr_unregister_window (ModestWindowMgr *self, 
165                                      ModestWindow *window)
166 {
167         GList *win;
168         ModestWindowMgrPrivate *priv;
169
170         g_return_if_fail (MODEST_IS_WINDOW_MGR (self));
171         g_return_if_fail (MODEST_IS_WINDOW (window));
172
173         priv = MODEST_WINDOW_MGR_GET_PRIVATE (self);
174
175         win = g_list_find (priv->window_list, window);
176         if (!win) {
177                 g_warning ("Trying to unregister a window that has not being registered yet");
178                 return;
179         }
180
181         /* Remove from list. Remove the reference to the window */
182         g_object_unref (win->data);
183         priv->window_list = g_list_remove_link (priv->window_list, win);
184 }
185
186 ModestWindow * 
187 modest_window_mgr_find_window_for_msgid (ModestWindowMgr *self, 
188                                          gchar *msgid, 
189                                          GType modest_window_type)
190 {
191         /* TODO */
192
193         return NULL;
194 }