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