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