* initial code dump into SVN; needs some work
[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         g_type_init ();
52         
53         context = g_option_context_new (NULL);
54         g_option_context_add_main_entries (context, options, NULL);
55         
56         if (!g_option_context_parse (context, &argc, &argv, &err)) {
57                 g_printerr ("modest: error in command line parameter(s): %s\n",
58                          err ? err->message : "");
59                 retval = 1;
60                 goto cleanup;
61         }
62
63         modest_conf = MODEST_CONF(modest_conf_new());
64         if (!modest_conf) {
65                 g_warning ("failed to initialize config system");
66                 goto cleanup;
67         }
68
69         if (reinstall) {
70                 modest_conf_remove_key (modest_conf, MODEST_CONF_NAMESPACE, NULL);
71                 install_basic_conf_settings (modest_conf);
72                 install_test_account (modest_conf);
73                 goto cleanup;
74         }
75
76         gtk_init (&argc, &argv);                
77         modest_ui = MODEST_UI(modest_ui_new (modest_conf));
78         if (!modest_ui) {
79                 g_warning ("failed to initialize ui");
80                 goto cleanup;
81         }
82         
83         {
84                 gboolean ok;
85                 gtk_init (&argc, &argv);
86                 
87                 if (mailto||cc||bcc||subject||body)
88                         ok = modest_ui_show_edit_window (modest_ui,
89                                                          mailto,  /* to */
90                                                          cc,      /* cc */
91                                                          bcc,     /* bcc */
92                                                          subject,    /* subject */
93                                                          body,    /* body */
94                                                          NULL);   /* attachments */
95                 else
96                         ok = modest_ui_show_main_window (modest_ui);
97
98                 if (!ok)
99                         g_warning ("showing window failed");
100                 else
101                         gtk_main();
102         }
103
104         
105 cleanup:
106         if (err)
107                 g_error_free (err);
108
109         if (context)
110                 g_option_context_free (context);
111
112         if (modest_ui)
113                 g_object_unref (modest_ui);
114         
115         if (modest_conf)
116                 g_object_unref (modest_conf);
117         
118         return retval;
119 }
120
121
122
123 static void
124 install_basic_conf_settings (ModestConf *conf)
125 {
126         g_return_if_fail (conf);
127
128         /* main window size */
129         modest_conf_set_int (conf, MODEST_CONF_MAIN_WINDOW_WIDTH,
130                              MODEST_CONF_MAIN_WINDOW_WIDTH_DEFAULT, NULL);
131         modest_conf_set_int (conf, MODEST_CONF_MAIN_WINDOW_HEIGHT,
132                              MODEST_CONF_MAIN_WINDOW_HEIGHT_DEFAULT, NULL);
133
134         /* edit window size */
135         modest_conf_set_int (conf, MODEST_CONF_EDIT_WINDOW_WIDTH,
136                              MODEST_CONF_EDIT_WINDOW_WIDTH_DEFAULT, NULL);
137         modest_conf_set_int (conf, MODEST_CONF_EDIT_WINDOW_HEIGHT,
138                              MODEST_CONF_EDIT_WINDOW_HEIGHT_DEFAULT, NULL);
139         
140         g_print ("modest: returned to factory settings\n");
141 }
142
143
144 static void
145 install_test_account (ModestConf *conf)
146 {
147         ModestAccountMgr *acc_mgr;
148         const gchar *acc_name = "test";
149         g_return_if_fail (conf);
150
151         acc_mgr = MODEST_ACCOUNT_MGR(modest_account_mgr_new (conf));
152         if (!acc_mgr) {
153                 g_warning ("failed to instantiate account mgr");
154                 return;
155         }
156
157         if (modest_account_mgr_account_exists (acc_mgr, acc_name, NULL)) {
158                 if (!modest_account_mgr_remove_account(acc_mgr, acc_name, NULL)) {
159                         g_warning ("could not delete existing account");
160                 }
161         }
162                                         
163         if (!modest_account_mgr_add_account (acc_mgr, acc_name, "mystore", "mytransport", NULL))
164                 g_warning ("failed to add test account");
165         
166         modest_account_mgr_add_server_account (acc_mgr, "mystore", "localhost", "djcb",
167                                                NULL, "pop");
168         modest_account_mgr_add_server_account (acc_mgr, "mytransport", "localhost", NULL,
169                                                NULL, "smtp");
170         g_object_unref (G_OBJECT(acc_mgr));
171 }