ca3805fe44b8a5c00bfc318e8238655527cdb5a1
[modest] / src / modest-main.c
1 /* modest-main.c -- part of modest */
2 #include <glib.h>
3
4 #include "modest-conf.h"
5 #include "modest-account-mgr.h"
6 #include "modest-identity-mgr.h"
7 #include "modest-ui.h"
8 #include "modest-icon-factory.h"
9
10 #ifdef HAVE_CONFIG_H
11 #include <config.h>
12 #endif /*HAVE_CONFIG_H*/
13 #include <gtk/gtk.h>
14
15 #ifdef IS_HILDON /* Hildon includes */
16 #include <libosso.h>
17 #define APPLICATION_DBUS_SERVICE "modest"
18 #endif
19
20
21 static void install_basic_conf_settings (ModestConf *conf);
22 static void install_test_account        (ModestConf *conf);
23
24
25 int
26 main (int argc, char *argv[])
27 {
28         GOptionContext   *context        = NULL;
29         ModestConf       *modest_conf    = NULL;
30         ModestUI         *modest_ui      = NULL;
31
32         GError *err = NULL;
33         int retval  = 0;
34
35         static gboolean update, debug, reinstall;
36         static gchar *mailto, *subject, *bcc, *cc, *body;
37
38         static GOptionEntry options[] = {
39                 { "debug",  'd', 0, G_OPTION_ARG_NONE, &debug,
40                   "Run in debug mode" },
41                 { "update", 'u', 0, G_OPTION_ARG_NONE, &update,
42                   "Send/receive all accounts and exit"},
43                 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
44                   "Start writing a new email to <addresses>"},
45                 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
46                   "Subject for a new mail"},
47                 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
48                   "Body for a new email"},
49                 { "cc",  0, 0, G_OPTION_ARG_STRING, &cc,
50                   "CC-addresses for a new mail (comma-separated)"},
51                 { "bcc", 0, 0, G_OPTION_ARG_STRING, &bcc,
52                   "BCC-adresses for a new mail (comma-separated)"},
53                 { "reinstall-factory-settings", 0, 0, G_OPTION_ARG_NONE, &reinstall,
54                   "Delete all settings and start over (*DESTRUCTIVE*)"
55                 },
56                 { NULL }
57         };
58
59 #ifdef IS_HILDON
60         osso_context_t *osso_context;
61 #endif
62
63         g_type_init ();
64
65         context = g_option_context_new (NULL);
66         g_option_context_add_main_entries (context, options, NULL);
67
68         if (!g_option_context_parse (context, &argc, &argv, &err)) {
69                 g_printerr ("modest: error in command line parameter(s): %s\n",
70                          err ? err->message : "");
71                 retval = 1;
72                 goto cleanup;
73         }
74
75         if (debug) {
76                 g_log_set_always_fatal (G_LOG_LEVEL_WARNING);
77         }
78
79         modest_conf = MODEST_CONF(modest_conf_new());
80         if (!modest_conf) {
81                 g_warning ("failed to initialize config system");
82                 goto cleanup;
83         }
84
85         gtk_init (&argc, &argv);
86
87         modest_icon_factory_init ();
88
89         if (reinstall) {
90                 modest_conf_remove_key (modest_conf, MODEST_CONF_NAMESPACE, NULL);
91                 install_basic_conf_settings (modest_conf);
92                 goto cleanup;
93         }
94
95         modest_ui = MODEST_UI(modest_ui_new (modest_conf));
96         if (!modest_ui) {
97                 g_warning ("failed to initialize ui");
98                 goto cleanup;
99         }
100         
101 #ifdef IS_HILDON
102         /* Initialize maemo application */
103         osso_context = osso_initialize(APPLICATION_DBUS_SERVICE, "0.1", TRUE, NULL);
104
105         /* Check that initialization was ok */
106         if (osso_context == NULL)
107                 goto cleanup;
108 #endif
109
110         {
111                 gboolean ok;
112                 gtk_init (&argc, &argv);
113
114                 if (mailto||cc||bcc||subject||body) {
115 #if 0
116                         ok = modest_ui_new_edit_window (modest_ui,
117                                                          mailto,  /* to */
118                                                          cc,      /* cc */
119                                                          bcc,     /* bcc */
120                                                          subject,    /* subject */
121                                                          body,    /* body */
122                                                          NULL);   /* attachments */
123 #endif
124                 } else
125                         ok = modest_ui_show_main_window (modest_ui);
126
127                 if (!ok)
128                         g_warning ("showing window failed");
129                 else
130                         gtk_main();
131         }
132
133
134 cleanup:
135         if (err)
136                 g_error_free (err);
137
138         if (context)
139                 g_option_context_free (context);
140
141         if (modest_ui)
142                 g_object_unref (modest_ui);
143
144         if (modest_conf)
145                 g_object_unref (modest_conf);
146
147         modest_icon_factory_uninit ();
148
149         return retval;
150 }
151
152
153
154 static void
155 install_basic_conf_settings (ModestConf *conf)
156 {
157         g_return_if_fail (conf);
158
159         /* main window size */
160         modest_conf_set_int (conf, MODEST_CONF_MAIN_WINDOW_WIDTH,
161                              MODEST_CONF_MAIN_WINDOW_WIDTH_DEFAULT, NULL);
162         modest_conf_set_int (conf, MODEST_CONF_MAIN_WINDOW_HEIGHT,
163                              MODEST_CONF_MAIN_WINDOW_HEIGHT_DEFAULT, NULL);
164
165         /* edit window size */
166         modest_conf_set_int (conf, MODEST_CONF_EDIT_WINDOW_WIDTH,
167                              MODEST_CONF_EDIT_WINDOW_WIDTH_DEFAULT, NULL);
168         modest_conf_set_int (conf, MODEST_CONF_EDIT_WINDOW_HEIGHT,
169                              MODEST_CONF_EDIT_WINDOW_HEIGHT_DEFAULT, NULL);
170
171         g_print ("modest: returned to factory settings\n");
172 }
173
174
175 static void
176 install_test_account (ModestConf *conf)
177 {
178         ModestAccountMgr *acc_mgr;
179         ModestIdentityMgr *id_mgr;
180         const gchar *acc_name = "test";
181         g_return_if_fail (conf);
182
183         acc_mgr = MODEST_ACCOUNT_MGR(modest_account_mgr_new (conf));
184         if (!acc_mgr) {
185                 g_warning ("failed to instantiate account mgr");
186                 return;
187         }
188
189         if (modest_account_mgr_account_exists (acc_mgr, acc_name, NULL)) {
190                 if (!modest_account_mgr_remove_account(acc_mgr, acc_name, NULL)) {
191                         g_warning ("could not delete existing account");
192                 }
193         }
194
195         if (!modest_account_mgr_add_account (acc_mgr, acc_name, "mystore", "mytransport", NULL))
196                 g_warning ("failed to add test account");
197         else
198         {
199                 modest_account_mgr_add_server_account (acc_mgr, "mystore", "localhost", "djcb",
200                                                        NULL, "imap");
201                 modest_account_mgr_add_server_account (acc_mgr, "mytransport", "localhost", NULL,
202                                                        NULL, "smtp");
203
204         }
205         id_mgr = MODEST_IDENTITY_MGR(modest_identity_mgr_new (conf));
206         if (modest_identity_mgr_identity_exists(id_mgr, "myidentity", NULL)) {
207                 if (!modest_identity_mgr_remove_identity(id_mgr, "myidentity", NULL)) {
208                         g_warning ("could not delete existing identity");
209                 }
210         }
211         if (!modest_identity_mgr_add_identity (id_mgr,
212                                                MODEST_IDENTITY_DEFAULT_IDENTITY,
213                                                "Default User",
214                                                "user@localhost",
215                                                "", "", FALSE, NULL, FALSE ))
216                 g_warning ("failed to add test identity");
217
218         g_object_unref (G_OBJECT(acc_mgr));
219         g_object_unref (G_OBJECT(id_mgr));
220 }