* added account view widget, a listview with the current
[modest] / src / modest-window-mgr.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
31 /* modest-window-mgr.c */
32
33 #include "modest-window-mgr.h"
34 /* include other impl specific header files */
35
36 /* 'private'/'protected' functions */
37 static void                   modest_window_mgr_class_init    (ModestWindowMgrClass *klass);
38 static void                   modest_window_mgr_init          (ModestWindowMgr *obj);
39 static void                   modest_window_mgr_finalize      (GObject *obj);
40
41 /* list my signals */
42 enum {
43         /* MY_SIGNAL_1, */
44         /* MY_SIGNAL_2, */
45         LAST_WINDOW_CLOSED_SIGNAL,
46         LAST_SIGNAL
47 };
48
49 typedef struct _ModestWindowMgrPrivate ModestWindowMgrPrivate;
50 struct _ModestWindowMgrPrivate {
51         GSList *open_windows;
52         
53 };
54 #define MODEST_WINDOW_MGR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
55                                                MODEST_TYPE_WINDOW_MGR, \
56                                                ModestWindowMgrPrivate))
57 /* globals */
58 static GObjectClass *parent_class = NULL;
59
60 static guint signals[LAST_SIGNAL] = {0};
61
62 GType
63 modest_window_mgr_get_type (void)
64 {
65         static GType my_type = 0;
66         if (!my_type) {
67                 static const GTypeInfo my_info = {
68                         sizeof(ModestWindowMgrClass),
69                         NULL,           /* base init */
70                         NULL,           /* base finalize */
71                         (GClassInitFunc) modest_window_mgr_class_init,
72                         NULL,           /* class finalize */
73                         NULL,           /* class data */
74                         sizeof(ModestWindowMgr),
75                         1,              /* n_preallocs */
76                         (GInstanceInitFunc) modest_window_mgr_init,
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         signals[LAST_WINDOW_CLOSED_SIGNAL] =
97                 g_signal_new ("last_window_closed",
98                               G_TYPE_FROM_CLASS(gobject_class),
99                               G_SIGNAL_RUN_FIRST,
100                               G_STRUCT_OFFSET(ModestWindowMgrClass, last_window_closed),
101                               NULL, NULL,
102                               g_cclosure_marshal_VOID__VOID,
103                               G_TYPE_NONE, 0);
104 }
105
106 static void
107 modest_window_mgr_init (ModestWindowMgr *obj)
108 {
109         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE(obj);
110         priv->open_windows = NULL;
111 }
112
113 static void
114 modest_window_mgr_finalize (GObject *obj)
115 {
116         ModestWindowMgrPrivate *priv = MODEST_WINDOW_MGR_GET_PRIVATE(obj);
117         g_slist_free (priv->open_windows);
118         priv->open_windows = NULL;      
119 }
120
121 GObject*
122 modest_window_mgr_new (void)
123 {
124         return G_OBJECT(g_object_new(MODEST_TYPE_WINDOW_MGR, NULL));
125 }
126
127 /* insert many other interesting function implementations */
128 /* such as modest_window_mgr_do_something, or modest_window_mgr_has_foo */
129
130 gboolean
131 modest_window_mgr_register (ModestWindowMgr *self, GObject *win,
132                             ModestWindowType type,
133                             guint window_id)
134 {
135         ModestOpenWindow *openwin = NULL;
136         ModestWindowMgrPrivate *priv;
137
138         g_return_val_if_fail (self, FALSE);
139         g_return_val_if_fail (type==MODEST_MAIN_WINDOW || type==MODEST_EDIT_WINDOW
140                                 || type == MODEST_VIEW_WINDOW, FALSE);
141
142         priv = MODEST_WINDOW_MGR_GET_PRIVATE(self);
143
144         openwin = g_new (ModestOpenWindow, 1);
145         openwin->win  = win;
146         openwin->type = type;
147         openwin->id   = window_id;
148         
149         priv->open_windows = g_slist_prepend (priv->open_windows, openwin);
150
151         return TRUE;
152 }
153
154
155
156 gboolean
157 modest_window_mgr_unregister (ModestWindowMgr *self, GObject *win)
158 {
159         ModestWindowMgrPrivate *priv;
160         GSList *cursor;
161         gboolean found = FALSE;
162         
163         g_return_val_if_fail (self, FALSE);
164         g_return_val_if_fail (win, FALSE);
165
166         priv = MODEST_WINDOW_MGR_GET_PRIVATE(self);
167
168         cursor = priv->open_windows;
169         while (cursor) {
170                 if (((ModestOpenWindow*)cursor->data)->win == win) {
171                         priv->open_windows = g_slist_delete_link (priv->open_windows,
172                                                                   cursor);
173                         found = TRUE;
174                         break;
175                 }
176                 cursor = cursor->next;
177         }
178         if (found) {
179                 guint win_num = g_slist_length (priv->open_windows);
180                 if (win_num == 0) 
181                         g_signal_emit (self, signals[LAST_WINDOW_CLOSED_SIGNAL],
182                                        0);
183         }
184
185         return found;
186 }
187
188
189 GObject *
190 modest_window_mgr_find_by_type (ModestWindowMgr *self, ModestWindowType type)
191 {
192         ModestWindowMgrPrivate *priv;
193         GSList *cursor;
194
195         g_return_val_if_fail (self, NULL);
196         
197         priv = MODEST_WINDOW_MGR_GET_PRIVATE(self);
198         cursor = priv->open_windows;
199         while (cursor) {
200                 ModestOpenWindow *openwin = (ModestOpenWindow*)cursor->data;
201                 if (openwin->type == type)
202                         return openwin->win;
203                 cursor = cursor->next;
204         }
205         
206         return NULL;
207 }
208
209
210 GObject *
211 modest_window_mgr_find_by_id (ModestWindowMgr *self, gint window_id)
212 {
213         ModestWindowMgrPrivate *priv;
214         GSList *cursor;
215
216         g_return_val_if_fail (self, NULL);
217         
218         priv = MODEST_WINDOW_MGR_GET_PRIVATE(self);
219         cursor = priv->open_windows;
220         while (cursor) {
221                 ModestOpenWindow *openwin = (ModestOpenWindow*)cursor->data;
222                 if (openwin->id == window_id)
223                         return openwin->win;
224                 cursor = cursor->next;
225         }
226         return NULL;
227 }
228