* modest-tny-account-store.[ch]:
[modest] / src / modest-init.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <config.h>
31 #include <modest-defs.h>
32 #include <widgets/modest-header-view.h>
33 #include <widgets/modest-folder-view.h>
34 #include <modest-tny-platform-factory.h>
35 #include <modest-widget-memory.h>
36 #include <modest-widget-memory-priv.h>
37 #include <modest-local-folder-info.h>
38 #include <modest-init.h>
39 #include <glib/gstdio.h>
40 #include <modest-account-mgr.h>
41 #include <modest-account-mgr-helpers.h>
42
43 typedef struct {
44         ModestHeaderViewColumn col;
45         guint                  width;
46 } FolderCols;
47
48 static const FolderCols INBOX_COLUMNS_DETAILS[] = {
49         {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
50         {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40},
51         {MODEST_HEADER_VIEW_COLUMN_FROM,    80},
52         {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80},
53         {MODEST_HEADER_VIEW_COLUMN_RECEIVED_DATE, 60},
54         {MODEST_HEADER_VIEW_COLUMN_SIZE, 50}
55 };
56 static const FolderCols INBOX_COLUMNS_TWOLINES[] = {
57         {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
58         {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40},
59         {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN, 200}
60 };
61
62 static const FolderCols OUTBOX_COLUMNS_DETAILS[] = {
63          {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
64          {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40},
65          {MODEST_HEADER_VIEW_COLUMN_TO,    80},
66          {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80},
67          {MODEST_HEADER_VIEW_COLUMN_SENT_DATE, 80},
68          {MODEST_HEADER_VIEW_COLUMN_SIZE, 50}
69 };
70 static const FolderCols OUTBOX_COLUMNS_TWOLINES[] = {
71          {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
72          {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40},
73          {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,200},
74 };
75          
76 static const TnyFolderType LOCAL_FOLDERS[] = {
77         TNY_FOLDER_TYPE_OUTBOX,
78         TNY_FOLDER_TYPE_DRAFTS,
79         TNY_FOLDER_TYPE_SENT,
80         TNY_FOLDER_TYPE_ARCHIVE 
81 };
82
83 static ModestTnyPlatformFactory*
84 get_platform_factory (void)
85 {
86         TnyPlatformFactory *fact =
87                 modest_tny_platform_factory_get_instance ();
88
89         if (!fact) {
90                 g_printerr ("modest: cannot get platform factory instance\n");
91                 return NULL;
92         }
93
94         return MODEST_TNY_PLATFORM_FACTORY(fact);
95 }
96
97
98 static ModestConf*
99 get_modest_conf (void)
100 {
101         ModestTnyPlatformFactory *fact =
102                 get_platform_factory ();
103         ModestConf *conf =
104                 modest_tny_platform_factory_get_conf_instance (fact);
105         if (!conf) {
106                 g_printerr ("modest: cannot get modest conf instance\n");
107                 return NULL;
108         }
109         return conf;
110 }
111
112
113 static ModestAccountMgr*
114 get_account_mgr (void)
115 {
116         ModestTnyPlatformFactory *fact =
117                 get_platform_factory ();
118         ModestAccountMgr *acc_mgr =
119                 modest_tny_platform_factory_get_account_mgr_instance (fact);
120         if (!acc_mgr) {
121                 g_printerr ("modest: cannot get modest account mgr instance\n");
122                 return NULL;
123         }
124         return acc_mgr;
125 }
126
127
128 /* NOTE: the exact details of this format are important, as they
129  * are also used in modest-widget-memory. FIXME: make a shared function
130  * for this with widget-memory
131  */
132 static gboolean
133 save_header_settings (ModestConf *conf, TnyFolderType type,
134                       ModestHeaderViewStyle style,  const FolderCols* cols,
135                       guint col_num, gboolean overwrite)
136 {
137         int i;
138         gchar *key;
139         GString *str;
140
141         g_return_val_if_fail (cols, FALSE);
142
143         key = _modest_widget_memory_get_keyname_with_double_type ("header-view",
144                                                                   type, style,
145                                                                   MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
146         /* if we're not in overwrite mode, only write stuff it
147          * there was nothing before */
148         if (!overwrite &&  modest_conf_key_exists(conf, key, NULL)) {
149                 g_free (key);
150                 return TRUE;
151         }
152
153         /* the format is necessarily the same as the one in modest-widget-memory */
154         str = g_string_new (NULL);
155         for (i = 0; i != col_num; ++i) 
156                 g_string_append_printf (str, "%d:%d ",
157                                         cols[i].col, cols[i].width); 
158
159         modest_conf_set_string (conf, key, str->str, NULL);
160         g_free (key);
161         g_string_free (str, TRUE);
162         
163         return TRUE;
164 }
165
166 /* we set the the defaults here for all folder types */
167 gboolean
168 modest_init_header_columns (gboolean overwrite)
169 {
170         ModestConf *conf;
171         int folder_type;
172
173         conf = get_modest_conf ();
174         if (!conf) {
175                 g_printerr ("modest: cannot get modest conf\n");
176                 return FALSE;
177         }
178         
179         for (folder_type = TNY_FOLDER_TYPE_UNKNOWN;
180              folder_type <= TNY_FOLDER_TYPE_CALENDAR; ++folder_type) {          
181                 
182                 switch (folder_type) {
183                 case TNY_FOLDER_TYPE_OUTBOX:
184                 case TNY_FOLDER_TYPE_SENT:
185                 case TNY_FOLDER_TYPE_DRAFTS:
186                 save_header_settings (conf, folder_type,
187                                       MODEST_HEADER_VIEW_STYLE_DETAILS,
188                                       OUTBOX_COLUMNS_DETAILS,
189                                       G_N_ELEMENTS(OUTBOX_COLUMNS_DETAILS),
190                                       overwrite);
191                 save_header_settings (conf, folder_type,
192                                       MODEST_HEADER_VIEW_STYLE_TWOLINES,
193                                       OUTBOX_COLUMNS_TWOLINES,
194                                       G_N_ELEMENTS(OUTBOX_COLUMNS_TWOLINES),
195                                       overwrite);
196                 break;
197
198                 default:
199                 save_header_settings (conf, folder_type,
200                                       MODEST_HEADER_VIEW_STYLE_DETAILS,
201                                       INBOX_COLUMNS_DETAILS,
202                                       G_N_ELEMENTS(INBOX_COLUMNS_DETAILS),
203                                       overwrite);
204                 save_header_settings (conf, folder_type,
205                                       MODEST_HEADER_VIEW_STYLE_TWOLINES,
206                                       INBOX_COLUMNS_TWOLINES,
207                                       G_N_ELEMENTS(INBOX_COLUMNS_TWOLINES),
208                                       overwrite);
209                 };
210         }
211         return TRUE;
212 }
213
214 gboolean
215 modest_init_local_folders  (void)
216 {
217         int i;
218         gchar *maildir_path;
219         static const gchar* maildirs[] = {
220                 "cur", "new", "tmp"
221         };
222         
223         maildir_path = modest_local_folder_info_get_maildir_path ();
224
225         for (i = 0; i != G_N_ELEMENTS(LOCAL_FOLDERS); ++i) {
226                 int j;
227                 for (j = 0; j != G_N_ELEMENTS(maildirs); ++j) {
228                         gchar *dir;
229                         dir = g_build_filename (maildir_path,
230                                                 modest_local_folder_info_get_type_name(LOCAL_FOLDERS[i]),
231                                                 maildirs[j],
232                                                 NULL);
233                         if (g_mkdir_with_parents (dir, 0755) < 0) {
234                                 g_printerr ("modest: failed to create %s\n", dir);
235                                 g_free (dir);
236                                 g_free (maildir_path);
237                                 return FALSE;
238                         }
239                         g_free(dir);
240                 }
241         }
242         
243         g_free (maildir_path);
244         return TRUE;
245 }
246
247
248
249 static void
250 free_element (gpointer data, gpointer user_data)
251 {
252         g_free (data);
253 }
254
255
256 gboolean
257 modest_init_default_account_maybe  (void)
258 {
259         ModestAccountMgr *acc_mgr;
260
261         GSList *all_accounts = NULL;
262         gchar *default_account;
263         gboolean retval = TRUE;
264         
265         acc_mgr = get_account_mgr ();
266         if (!acc_mgr) {
267                 g_printerr ("modest: cannot get modest account mgr\n");
268                 return FALSE;
269         }
270
271         all_accounts = modest_account_mgr_account_names (acc_mgr, NULL);
272         if (all_accounts) { /* if there are any accounts, there should be a default one */
273                 default_account = 
274                         modest_account_mgr_get_default_account (acc_mgr);
275                 if (!default_account) {
276                         gchar *first_account;
277                         g_printerr ("modest: no default account defined\n");
278                         first_account = (gchar*)all_accounts->data;
279                         if ((retval = modest_account_mgr_set_default_account (acc_mgr, first_account)))
280                                 g_printerr ("modest: set '%s' as the default account\n",
281                                             first_account);
282                         else
283                                 g_printerr ("modest: failed to set '%s' as the default account\n",
284                                             first_account);
285                         g_free (default_account);
286                 }
287                 g_slist_foreach (all_accounts, free_element, NULL);
288                 g_slist_free    (all_accounts);
289         }
290         return retval;
291 }