* So, now I think I finally got all changes from Arne and myself together.
[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 #include <gtk/gtk.h>
12
13 static void install_basic_conf_settings (ModestConf *conf);
14 static void install_test_account        (ModestConf *conf);
15
16
17 int
18 main (int argc, char *argv[])
19 {
20         GOptionContext   *context        = NULL;
21         ModestConf       *modest_conf    = NULL;
22         ModestUI         *modest_ui      = NULL;
23
24         GError *err = NULL;
25         int retval  = 0;
26
27         static gboolean update, debug, reinstall;
28         static gchar *mailto, *subject, *bcc, *cc, *body;
29
30         static GOptionEntry options[] = {
31                 { "debug",  'd', 0, G_OPTION_ARG_NONE, &debug,
32                   "Run in debug mode" },
33                 { "update", 'u', 0, G_OPTION_ARG_NONE, &update,
34                   "Send/receive all accounts and exit"},
35                 { "mailto", 'm', 0, G_OPTION_ARG_STRING, &mailto,
36                   "Start writing a new email to <addresses>"},
37                 { "subject", 's', 0, G_OPTION_ARG_STRING, &subject,
38                   "Subject for a new mail"},
39                 { "body", 'b', 0, G_OPTION_ARG_STRING, &body,
40                   "Body for a new email"},
41                 { "cc",  0, 0, G_OPTION_ARG_STRING, &cc,
42                   "CC-addresses for a new mail (comma-separated)"},
43                 { "bcc", 0, 0, G_OPTION_ARG_STRING, &bcc,
44                   "BCC-adresses for a new mail (comma-separated)"},
45                 { "reinstall-factory-settings", 0, 0, G_OPTION_ARG_NONE, &reinstall,
46                   "Delete all settings and start over (*DESTRUCTIVE*)"
47                 },
48                 { NULL }
49         };
50
51
52         g_type_init ();
53
54         context = g_option_context_new (NULL);
55         g_option_context_add_main_entries (context, options, NULL);
56
57         if (!g_option_context_parse (context, &argc, &argv, &err)) {
58                 g_printerr ("modest: error in command line parameter(s): %s\n",
59                          err ? err->message : "");
60                 retval = 1;
61                 goto cleanup;
62         }
63
64         if (debug) {
65                 g_log_set_always_fatal (G_LOG_LEVEL_WARNING);
66         }
67
68         modest_conf = MODEST_CONF(modest_conf_new());
69         if (!modest_conf) {
70                 g_warning ("failed to initialize config system");
71                 goto cleanup;
72         }
73
74         if (reinstall) {
75                 modest_conf_remove_key (modest_conf, MODEST_CONF_NAMESPACE, NULL);
76                 install_basic_conf_settings (modest_conf);
77                 install_test_account (modest_conf);
78                 goto cleanup;
79         }
80
81
82
83         gtk_init (&argc, &argv);
84         modest_ui = MODEST_UI(modest_ui_new (modest_conf));
85         if (!modest_ui) {
86                 g_warning ("failed to initialize ui");
87                 goto cleanup;
88         }
89
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
111
112 cleanup:
113         if (err)
114                 g_error_free (err);
115
116         if (context)
117                 g_option_context_free (context);
118
119         if (modest_ui)
120                 g_object_unref (modest_ui);
121
122         if (modest_conf)
123                 g_object_unref (modest_conf);
124
125         return retval;
126 }
127
128
129
130 static void
131 install_basic_conf_settings (ModestConf *conf)
132 {
133         g_return_if_fail (conf);
134
135         /* main window size */
136         modest_conf_set_int (conf, MODEST_CONF_MAIN_WINDOW_WIDTH,
137                              MODEST_CONF_MAIN_WINDOW_WIDTH_DEFAULT, NULL);
138         modest_conf_set_int (conf, MODEST_CONF_MAIN_WINDOW_HEIGHT,
139                              MODEST_CONF_MAIN_WINDOW_HEIGHT_DEFAULT, NULL);
140
141         /* edit window size */
142         modest_conf_set_int (conf, MODEST_CONF_EDIT_WINDOW_WIDTH,
143                              MODEST_CONF_EDIT_WINDOW_WIDTH_DEFAULT, NULL);
144         modest_conf_set_int (conf, MODEST_CONF_EDIT_WINDOW_HEIGHT,
145                              MODEST_CONF_EDIT_WINDOW_HEIGHT_DEFAULT, NULL);
146
147         g_print ("modest: returned to factory settings\n");
148 }
149
150
151 static void
152 install_test_account (ModestConf *conf)
153 {
154         ModestAccountMgr *acc_mgr;
155         const gchar *acc_name = "test";
156         g_return_if_fail (conf);
157
158         acc_mgr = MODEST_ACCOUNT_MGR(modest_account_mgr_new (conf));
159         if (!acc_mgr) {
160                 g_warning ("failed to instantiate account mgr");
161                 return;
162         }
163
164         if (modest_account_mgr_account_exists (acc_mgr, acc_name, NULL)) {
165                 if (!modest_account_mgr_remove_account(acc_mgr, acc_name, NULL)) {
166                         g_warning ("could not delete existing account");
167                 }
168         }
169
170         if (!modest_account_mgr_add_account (acc_mgr, acc_name, "mystore", "mytransport", NULL))
171                 g_warning ("failed to add test account");
172         else
173         {
174                 modest_account_mgr_add_server_account (acc_mgr, "mystore", "localhost", "djcb",
175                                                        NULL, "imap");
176                 modest_account_mgr_add_server_account (acc_mgr, "mytransport", "localhost", NULL,
177                                                        NULL, "smtp");
178         }
179         g_object_unref (G_OBJECT(acc_mgr));
180 }
181