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