4146aa6f305c5080d071894b91f365d54ff7a958
[modest] / src / gtk / modest-ui.c
1 /* modest-ui.c */
2
3 /* insert (c)/licensing information) */
4
5 #include <gtk/gtk.h>
6
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif /*HAVE_CONFIG_H*/
10
11 #include "modest-main-window.h"
12 #include "modest-edit-window.h"
13
14 #include "../modest-ui.h"
15 #include "../modest-window-mgr.h"
16 #include "../modest-account-mgr.h"
17
18
19 /* include other impl specific header files */
20
21 /* 'private'/'protected' functions */
22 static void   modest_ui_class_init     (ModestUIClass *klass);
23 static void   modest_ui_init           (ModestUI *obj);
24 static void   modest_ui_finalize       (GObject *obj);
25
26 static void    modest_ui_window_destroy    (GtkWidget *win, gpointer data);
27 static void    modest_ui_last_window_closed (GObject *obj, gpointer data);
28
29
30
31 /* list my signals */
32 enum {
33         /* MY_SIGNAL_1, */
34         /* MY_SIGNAL_2, */
35         LAST_SIGNAL
36 };
37
38
39 typedef struct _ModestUIPrivate ModestUIPrivate;
40 struct _ModestUIPrivate {
41         
42         ModestConf           *modest_conf;
43         ModestAccountMgr     *modest_acc_mgr;
44         ModestWindowMgr      *modest_window_mgr;
45         
46         GtkWindow            *main_window;
47         GSList*              *edit_window_list;      
48
49 };
50 #define MODEST_UI_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
51                                        MODEST_TYPE_UI, \
52                                        ModestUIPrivate))
53 /* globals */
54 static GObjectClass *parent_class = NULL;
55
56 /* uncomment the following if you have defined any signals */
57 /* static guint signals[LAST_SIGNAL] = {0}; */
58
59 GType
60 modest_ui_get_type (void)
61 {
62         static GType my_type = 0;
63         if (!my_type) {
64                 static const GTypeInfo my_info = {
65                         sizeof(ModestUIClass),
66                         NULL,           /* base init */
67                         NULL,           /* base finalize */
68                         (GClassInitFunc) modest_ui_class_init,
69                         NULL,           /* class finalize */
70                         NULL,           /* class data */
71                         sizeof(ModestUI),
72                         1,              /* n_preallocs */
73                         (GInstanceInitFunc) modest_ui_init,
74                 };
75                 my_type = g_type_register_static (G_TYPE_OBJECT,
76                                                   "ModestUI",
77                                                   &my_info, 0);
78         }
79         return my_type;
80 }
81
82 static void
83 modest_ui_class_init (ModestUIClass *klass)
84 {
85         GObjectClass *gobject_class;
86         gobject_class = (GObjectClass*) klass;
87
88         parent_class            = g_type_class_peek_parent (klass);
89         gobject_class->finalize = modest_ui_finalize;
90
91         g_type_class_add_private (gobject_class, sizeof(ModestUIPrivate));
92
93         /* signal definitions go here, e.g.: */
94 /*      signals[MY_SIGNAL_1] = */
95 /*              g_signal_new ("my_signal_1",....); */
96 /*      signals[MY_SIGNAL_2] = */
97 /*              g_signal_new ("my_signal_2",....); */
98 /*      etc. */
99 }
100
101 static void
102 modest_ui_init (ModestUI *obj)
103 {
104         ModestUIPrivate *priv = MODEST_UI_GET_PRIVATE(obj);
105
106         priv->modest_acc_mgr    = NULL;
107         priv->modest_conf       = NULL;
108         priv->modest_window_mgr = NULL;
109
110 }
111
112 static void
113 modest_ui_finalize (GObject *obj)
114 {
115         ModestUIPrivate *priv = MODEST_UI_GET_PRIVATE(obj);     
116         
117         if (priv->modest_acc_mgr)
118                 g_object_unref (priv->modest_acc_mgr);
119         priv->modest_acc_mgr = NULL;
120         
121         if (priv->modest_conf)
122                 g_object_unref (priv->modest_conf);
123         priv->modest_conf = NULL;
124         
125         if (priv->modest_window_mgr)
126                 g_object_unref (priv->modest_window_mgr);
127         priv->modest_window_mgr = NULL;
128 }
129         
130 GObject*
131 modest_ui_new (ModestConf *modest_conf)
132 {
133         GObject *obj;
134         ModestUIPrivate *priv;
135         ModestAccountMgr *modest_acc_mgr;
136
137         g_return_val_if_fail (modest_conf, NULL);
138
139         obj = g_object_new(MODEST_TYPE_UI, NULL);       
140         priv = MODEST_UI_GET_PRIVATE(obj);
141
142         modest_acc_mgr = MODEST_ACCOUNT_MGR(modest_account_mgr_new (modest_conf));
143         if (!modest_acc_mgr) {
144                 g_warning ("could not create ModestAccountMgr instance");
145                 g_object_unref (obj);
146                 return NULL;
147         }
148         
149         priv->modest_acc_mgr = modest_acc_mgr;
150         g_object_ref (priv->modest_conf = modest_conf);
151
152         priv->modest_window_mgr = MODEST_WINDOW_MGR(modest_window_mgr_new());
153         g_signal_connect (priv->modest_window_mgr, "last_window_closed",
154                           G_CALLBACK(modest_ui_last_window_closed),
155                           NULL);
156         return obj;
157 }
158
159
160 gboolean
161 modest_ui_show_main_window (ModestUI *modest_ui)
162 {
163         GtkWidget       *win;
164         int              height, width;
165         ModestUIPrivate *priv;
166
167         priv = MODEST_UI_GET_PRIVATE(modest_ui);
168         
169         height = modest_conf_get_int (priv->modest_conf,
170                                       MODEST_CONF_MAIN_WINDOW_HEIGHT,NULL);
171         width  = modest_conf_get_int (priv->modest_conf,
172                                       MODEST_CONF_MAIN_WINDOW_WIDTH,NULL);
173         
174         win = modest_main_window_new (priv->modest_conf,
175                                       priv->modest_acc_mgr);
176         if (!win) {
177                 g_warning ("could not create main window");
178                 return FALSE;
179         }
180
181         modest_window_mgr_register (priv->modest_window_mgr,
182                                     G_OBJECT(win), MODEST_MAIN_WINDOW, 0);
183         
184         g_signal_connect (win, "destroy", G_CALLBACK(modest_ui_window_destroy),
185                           modest_ui);
186         
187         gtk_widget_set_usize (GTK_WIDGET(win), height, width);
188         gtk_window_set_title (GTK_WINDOW(win), PACKAGE_STRING);
189         
190         gtk_widget_show (win);
191         return TRUE;
192 }
193
194
195 gboolean
196 modest_ui_show_edit_window (ModestUI *modest_ui, const gchar* to,
197                             const gchar* cc, const gchar* bcc,
198                             const gchar* subject, const gchar *body,
199                             const GSList* att)
200 {
201         GtkWidget       *win;
202         ModestUIPrivate *priv;
203
204         priv = MODEST_UI_GET_PRIVATE(modest_ui);
205         int height = modest_conf_get_int (priv->modest_conf,
206                                           MODEST_CONF_EDIT_WINDOW_HEIGHT,NULL);
207         int width  = modest_conf_get_int (priv->modest_conf,
208                                           MODEST_CONF_EDIT_WINDOW_WIDTH,NULL);
209         
210         win = modest_edit_window_new (to, cc, bcc, subject, body, att);
211         if (!win) {
212                 g_warning ("could not create edit window");
213                 return FALSE;
214         }
215         
216         modest_window_mgr_register (priv->modest_window_mgr,
217                                     G_OBJECT(win), MODEST_EDIT_WINDOW, 0);
218
219         g_signal_connect (win, "destroy", G_CALLBACK(modest_ui_window_destroy),
220                           modest_ui);
221
222         gtk_widget_set_usize (GTK_WIDGET(win), height, width);
223         gtk_window_set_title (GTK_WINDOW(win),
224                               subject ? subject : "Untitled");
225
226         gtk_widget_show (win);
227
228         return TRUE;
229 }
230
231
232 static void
233 modest_ui_window_destroy (GtkWidget *win, gpointer data)
234 {
235         ModestUIPrivate *priv;
236
237         g_return_if_fail (data);
238
239         priv = MODEST_UI_GET_PRIVATE((ModestUI*)data);
240         if (!modest_window_mgr_unregister (priv->modest_window_mgr, G_OBJECT(win)))
241                 g_warning ("modest window mgr: failed to unregister %p",
242                            G_OBJECT(win));
243 }
244
245
246 static void
247 modest_ui_last_window_closed (GObject *obj, gpointer data)
248 {
249         gtk_main_quit ();
250 }