Added first experimental Modest code.
[modest] / experimental / 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-ui.h"
7
8 #ifdef HAVE_CONFIG_H                            
9 #include <config.h>
10 #endif /*HAVE_CONFIG_H*/
11
12 #if defined (MODEST_ENABLE_HILDON_UI) || defined (MODEST_ENABLE_GTK_UI)
13 #include <gtk/gtk.h>
14 #endif
15
16 static void install_basic_conf_settings (ModestConf *conf);
17 static void install_test_account        (ModestConf *conf);
18
19
20 int
21 main (int argc, char *argv[])
22 {
23         GOptionContext   *context        = NULL;
24         ModestConf       *modest_conf    = NULL;
25         ModestUI         *modest_ui      = NULL;
26
27         GError *err = NULL;
28         int retval  = 0;
29         
30         static gboolean update, debug, reinstall;
31         static gchar *mailto, *subject, *bcc, *cc, *body;
32         
33         static GOptionEntry options[] = {
34                 { "debug",  'd', 0, G_OPTION_ARG_NONE, &debug,
35                   "Run in debug mode" },
36                 { "update", 'u', 0, G_OPTION_ARG_NONE, &update,
37                   "Send/receive all accounts and exit"},
38                 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
39                   "Start writing a new email to <addresses>"},
40                 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
41                   "Subject for a new mail"},
42                 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
43                   "Body for a new email"},      
44 /*              { "bodyfile", 'f', 0, G_OPTION_ARG_STRING, &bodyfile, */
45 /*                "Body for a new email"},       */
46                 { "cc",  0, 0, G_OPTION_ARG_STRING, &cc,
47                   "CC-addresses for a new mail (comma-separated)"},             
48                 { "bcc", 0, 0, G_OPTION_ARG_STRING, &bcc,
49                   "BCC-adresses for a new mail (comma-separated)"},
50 /*              { "attach", 's', 0, G_OPTION_ARG_STRING, &attach, */
51 /*                "Attachements for a new email (comma-separated)"}, */
52                 { "reinstall-factory-settings", 0, 0, G_OPTION_ARG_NONE, &reinstall,
53                   "Delete all settings and start over (*DESTRUCTIVE*)"
54                 },
55                 { NULL }
56         };
57
58         g_type_init ();
59         
60         context = g_option_context_new (NULL);
61         g_option_context_add_main_entries (context, options, NULL);
62         
63         if (!g_option_context_parse (context, &argc, &argv, &err)) {
64                 g_printerr ("modest: error in command line parameter(s): %s\n",
65                          err ? err->message : "");
66                 retval = 1;
67                 goto cleanup;
68         }
69
70         modest_conf = MODEST_CONF(modest_conf_new());
71         if (!modest_conf) {
72                 g_warning ("failed to initialize config system");
73                 goto cleanup;
74         }
75
76         if (reinstall) {
77                 modest_conf_remove_key (modest_conf, MODEST_CONF_NAMESPACE, NULL);
78                 install_basic_conf_settings (modest_conf);
79                 install_test_account (modest_conf);
80                 goto cleanup;
81         }
82                 
83         modest_ui = MODEST_UI(modest_ui_new (modest_conf));
84         if (!modest_ui) {
85                 g_warning ("failed to initialize ui");
86                 goto cleanup;
87         }
88         
89 #if defined (MODEST_ENABLE_HILDON_UI) || defined (MODEST_ENABLE_GTK_UI)
90         {
91                 gboolean ok;
92                 gtk_init (&argc, &argv);
93                 
94                 if (mailto||cc||bcc||subject||body)
95                         ok = modest_ui_show_edit_window (modest_ui,
96                                                          mailto,  /* to */
97                                                          cc,      /* cc */
98                                                          bcc,     /* bcc */
99                                                          subject,    /* subject */
100                                                          body,    /* body */
101                                                          NULL);   /* attachments */
102                 else
103                         ok = modest_ui_show_main_window (modest_ui);
104
105                 if (!ok)
106                         g_warning ("showing window failed");
107                 else
108                         gtk_main();
109         }
110 #else
111         /* do something else... */
112 #endif
113
114         
115 cleanup:
116         if (err)
117                 g_error_free (err);
118
119         if (context)
120                 g_option_context_free (context);
121
122         if (modest_ui)
123                 g_object_unref (modest_ui);
124         
125         if (modest_conf)
126                 g_object_unref (modest_conf);
127         
128         return retval;
129 }
130
131
132
133 static void
134 install_basic_conf_settings (ModestConf *conf)
135 {
136         g_return_if_fail (conf);
137
138         /* main window size */
139         modest_conf_set_int (conf, MODEST_CONF_MAIN_WINDOW_WIDTH,
140                              MODEST_CONF_MAIN_WINDOW_WIDTH_DEFAULT, NULL);
141         modest_conf_set_int (conf, MODEST_CONF_MAIN_WINDOW_HEIGHT,
142                              MODEST_CONF_MAIN_WINDOW_HEIGHT_DEFAULT, NULL);
143
144         /* edit window size */
145         modest_conf_set_int (conf, MODEST_CONF_EDIT_WINDOW_WIDTH,
146                              MODEST_CONF_EDIT_WINDOW_WIDTH_DEFAULT, NULL);
147         modest_conf_set_int (conf, MODEST_CONF_EDIT_WINDOW_HEIGHT,
148                              MODEST_CONF_EDIT_WINDOW_HEIGHT_DEFAULT, NULL);
149         
150         g_print ("modest: returned to factory settings\n");
151 }
152
153
154
155 static void
156 install_test_account (ModestConf *conf)
157 {
158         ModestAccountMgr *acc_mgr;
159         const gchar *acc_name = "test";
160         g_return_if_fail (conf);
161
162         acc_mgr = MODEST_ACCOUNT_MGR(modest_account_mgr_new (conf));
163         if (!acc_mgr) {
164                 g_warning ("failed to instantiate account mgr");
165                 return;
166         }
167
168         if (modest_account_mgr_account_exists (acc_mgr, acc_name, NULL)) {
169                 if (!modest_account_mgr_remove_account(acc_mgr, acc_name, NULL)) {
170                         g_warning ("could not delete existing account");
171                         return;
172                 }
173         }
174                                         
175         if (!modest_account_mgr_add_account (acc_mgr, acc_name, NULL))
176                 g_warning ("failed to add test account");
177
178         
179         modest_account_mgr_set_account_string (acc_mgr, acc_name,
180                                                MODEST_ACCOUNT_TYPE,
181                                                MODEST_ACCOUNT_TYPE_STORE,
182                                                NULL);
183         
184         modest_account_mgr_set_account_string (acc_mgr, acc_name,
185                                                MODEST_ACCOUNT_PROTO,
186                                                MODEST_ACCOUNT_PROTO_IMAP,
187                                                NULL);
188         
189         modest_account_mgr_set_account_string (acc_mgr, acc_name,
190                                                MODEST_ACCOUNT_SERVER,
191                                                "localhost",
192                                                NULL);
193         
194         modest_account_mgr_set_account_string (acc_mgr, acc_name,
195                                                MODEST_ACCOUNT_USER,
196                                                "test",
197                                                NULL);
198
199         g_object_unref (G_OBJECT(acc_mgr));
200 }