* re-add the correct COPYING file.
[modest] / src / gtk / modest-ui.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 <gtk/gtk.h>
31 #include <glade/glade.h>
32 #include <glib/gi18n.h>
33 #include <string.h>
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif /*HAVE_CONFIG_H*/
38
39 #include "../modest-ui.h"
40 #include "../modest-account-mgr.h"
41 #include "../modest-widget-factory.h"
42 #include "modest-main-window.h"
43 #include "modest-tny-platform-factory.h"
44
45
46 /* 'private'/'protected' functions */
47 static void   modest_ui_class_init     (ModestUIClass *klass);
48 static void   modest_ui_init           (ModestUI *obj);
49 static void   modest_ui_finalize       (GObject *obj);
50
51 gchar *on_password_requested (TnyAccountIface *, const gchar *, gboolean *);
52
53
54 typedef struct _ModestUIPrivate ModestUIPrivate;
55 struct _ModestUIPrivate {
56         ModestWidgetFactory   *widget_factory;  
57
58         GtkWidget              *main_window;
59 };
60
61 #define MODEST_UI_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
62                                        MODEST_TYPE_UI, \
63                                        ModestUIPrivate))
64
65
66 /* list my signals */
67 enum {
68         /* MY_SIGNAL_1, */
69         /* MY_SIGNAL_2, */
70         LAST_SIGNAL
71 };
72
73 /* globals */
74 static GObjectClass *parent_class = NULL;
75
76
77 GType
78 modest_ui_get_type (void)
79 {
80         static GType my_type = 0;
81         if (!my_type) {
82                 static const GTypeInfo my_info = {
83                         sizeof(ModestUIClass),
84                         NULL,           /* base init */
85                         NULL,           /* base finalize */
86                         (GClassInitFunc) modest_ui_class_init,
87                         NULL,           /* class finalize */
88                         NULL,           /* class data */
89                         sizeof(ModestUI),
90                         1,              /* n_preallocs */
91                         (GInstanceInitFunc) modest_ui_init,
92                         NULL
93                 };
94                 my_type = g_type_register_static (G_TYPE_OBJECT,
95                                                   "ModestUI",
96                                                   &my_info, 0);
97         }
98         return my_type;
99 }
100
101
102 static void
103 modest_ui_class_init (ModestUIClass *klass)
104 {
105         GObjectClass *gobject_class;
106         gobject_class = (GObjectClass*) klass;
107
108         parent_class            = g_type_class_peek_parent (klass);
109         gobject_class->finalize = modest_ui_finalize;
110
111         g_type_class_add_private (gobject_class, sizeof(ModestUIPrivate));
112
113 }
114
115
116 static void
117 modest_ui_init (ModestUI *obj)
118 {
119         ModestUIPrivate *priv;
120
121         priv = MODEST_UI_GET_PRIVATE(obj);
122
123         priv->widget_factory = NULL;
124         priv->main_window    = NULL;
125 }
126
127
128 static void
129 modest_ui_finalize (GObject *obj)
130 {
131         
132         ModestUIPrivate *priv = MODEST_UI_GET_PRIVATE(obj);
133         
134         if (priv->widget_factory) {
135                 g_object_unref (G_OBJECT(priv->widget_factory));
136                 priv->widget_factory = NULL;
137         }
138         
139         G_OBJECT_CLASS(parent_class)->finalize (obj);
140 }
141
142
143 ModestUI*
144 modest_ui_new (void)
145 {
146         GObject *obj;
147         ModestUIPrivate *priv;
148         TnyPlatformFactory *fact;
149         ModestAccountMgr *account_mgr;
150         TnyAccountStore *account_store;
151
152         obj  = g_object_new(MODEST_TYPE_UI, NULL);
153         priv = MODEST_UI_GET_PRIVATE(obj);
154
155         /* Get the platform-dependent instances */
156         fact = modest_tny_platform_factory_get_instance ();
157         
158         account_mgr = modest_tny_platform_factory_get_modest_account_mgr_instance (fact);
159         if (!account_mgr) {
160                 g_printerr ("modest: could not create ModestAccountMgr instance\n");
161                 g_object_unref (obj);
162                 return NULL;
163         }
164
165         account_store = tny_platform_factory_new_account_store (fact);
166         if (!account_store) {
167                 g_printerr ("modest: could not initialze ModestTnyAccountStore instance\n");
168                 return NULL;
169         }
170
171         priv->widget_factory = modest_widget_factory_new ();
172         if (!priv->widget_factory) {
173                 g_printerr ("modest: could not initialize widget factory\n");
174                 return NULL;
175         }
176                 
177         return MODEST_UI(obj);
178 }
179
180 static gboolean
181 on_main_window_destroy (GtkObject *widget, ModestUI *self)
182 {
183         /* FIXME: check if there any viewer/editing windows opened */
184         gtk_main_quit ();
185         return FALSE;
186 }
187
188
189 GtkWidget*
190 modest_ui_main_window (ModestUI *modest_ui)
191 {
192         ModestUIPrivate *priv;
193
194         g_return_val_if_fail (modest_ui, NULL);
195         priv = MODEST_UI_GET_PRIVATE(modest_ui);
196
197         if (!priv->main_window) {
198                 priv->main_window =
199                         modest_main_window_new (priv->widget_factory);
200                 g_signal_connect (G_OBJECT(priv->main_window), "destroy",
201                                   G_CALLBACK(on_main_window_destroy), modest_ui);
202
203         }
204                 
205         if (!priv->main_window)
206                 g_printerr ("modest: could not create main window\n");
207         
208         return priv->main_window;
209 }