9a5f0ba943de1993b297ccde419192b37e74b952
[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
41 typedef struct {
42         ModestHeaderViewColumn col;
43         guint                  width;
44 } FolderCols;
45
46 static const FolderCols INBOX_COLUMNS_DETAILS[] = {
47         {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
48         {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40},
49         {MODEST_HEADER_VIEW_COLUMN_FROM,    80},
50         {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80},
51         {MODEST_HEADER_VIEW_COLUMN_RECEIVED_DATE, 60},
52         {MODEST_HEADER_VIEW_COLUMN_SIZE, 50}
53 };
54 static const FolderCols INBOX_COLUMNS_TWOLINES[] = {
55         {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
56         {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40},
57         {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN, 200}
58 };
59
60 static const FolderCols OUTBOX_COLUMNS_DETAILS[] = {
61          {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
62          {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40},
63          {MODEST_HEADER_VIEW_COLUMN_TO,    80},
64          {MODEST_HEADER_VIEW_COLUMN_SUBJECT, 80},
65          {MODEST_HEADER_VIEW_COLUMN_SENT_DATE, 80},
66          {MODEST_HEADER_VIEW_COLUMN_SIZE, 50}
67 };
68 static const FolderCols OUTBOX_COLUMNS_TWOLINES[] = {
69          {MODEST_HEADER_VIEW_COLUMN_MSGTYPE, 40},
70          {MODEST_HEADER_VIEW_COLUMN_ATTACH,  40},
71          {MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT,200},
72 };
73          
74 static const ModestLocalFolderType LOCAL_FOLDERS[] = {
75         MODEST_LOCAL_FOLDER_TYPE_OUTBOX,
76         MODEST_LOCAL_FOLDER_TYPE_DRAFTS,
77         MODEST_LOCAL_FOLDER_TYPE_SENT,
78         MODEST_LOCAL_FOLDER_TYPE_ARCHIVE        
79 };
80
81
82 static ModestTnyPlatformFactory*
83 get_platform_factory (void)
84 {
85         TnyPlatformFactory *fact =
86                 modest_tny_platform_factory_get_instance ();
87
88         if (!fact) {
89                 g_printerr ("modest: cannot get platform factory instance\n");
90                 return NULL;
91         }
92
93         return MODEST_TNY_PLATFORM_FACTORY(fact);
94 }
95
96
97 static ModestConf*
98 get_modest_conf (void)
99 {
100         ModestTnyPlatformFactory *fact =
101                 get_platform_factory ();
102         ModestConf *conf =
103                 modest_tny_platform_factory_get_conf_instance (fact);
104         if (!conf) {
105                 g_printerr ("modest: cannot get modest conf instance\n");
106                 return NULL;
107         }
108         return conf;
109 }
110
111
112 /* NOTE: the exact details of this format are important, as they
113  * are also used in modest-widget-memory. FIXME: make a shared function
114  * for this with widget-memory
115  */
116 static gboolean
117 save_header_settings (ModestConf *conf, TnyFolderType type,
118                       ModestHeaderViewStyle style,  const FolderCols* cols,
119                       guint col_num, gboolean overwrite)
120 {
121         int i;
122         gchar *key;
123         GString *str;
124
125         g_return_val_if_fail (cols, FALSE);
126
127         key = _modest_widget_memory_get_keyname_with_double_type ("header-view",
128                                                                   type, style,
129                                                                   MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
130         /* if we're not in overwrite mode, only write stuff it
131          * there was nothing before */
132         if (!overwrite &&  modest_conf_key_exists(conf, key, NULL)) {
133                 g_free (key);
134                 return TRUE;
135         }
136
137         /* the format is necessarily the same as the one in modest-widget-memory */
138         str = g_string_new (NULL);
139         for (i = 0; i != col_num; ++i) 
140                 g_string_append_printf (str, "%d:%d ",
141                                         cols[i].col, cols[i].width); 
142
143         modest_conf_set_string (conf, key, str->str, NULL);
144         g_free (key);
145         g_string_free (str, TRUE);
146         
147         return TRUE;
148 }
149
150 /* we set the the defaults here for all folder types */
151 gboolean
152 modest_init_header_columns (gboolean overwrite)
153 {
154         ModestConf *conf;
155         int folder_type;
156
157         conf = get_modest_conf ();
158         if (!conf) {
159                 g_printerr ("modest: cannot get modest conf\n");
160                 return FALSE;
161         }
162         
163         for (folder_type = TNY_FOLDER_TYPE_UNKNOWN;
164              folder_type <= TNY_FOLDER_TYPE_CALENDAR; ++folder_type) {          
165                 
166                 switch (folder_type) {
167                 case TNY_FOLDER_TYPE_OUTBOX:
168                 case TNY_FOLDER_TYPE_SENT:
169                 case TNY_FOLDER_TYPE_DRAFTS:
170                 save_header_settings (conf, folder_type,
171                                       MODEST_HEADER_VIEW_STYLE_DETAILS,
172                                       OUTBOX_COLUMNS_DETAILS,
173                                       G_N_ELEMENTS(OUTBOX_COLUMNS_DETAILS),
174                                       overwrite);
175                 save_header_settings (conf, folder_type,
176                                       MODEST_HEADER_VIEW_STYLE_TWOLINES,
177                                       OUTBOX_COLUMNS_TWOLINES,
178                                       G_N_ELEMENTS(OUTBOX_COLUMNS_TWOLINES),
179                                       overwrite);
180                 break;
181
182                 default:
183                 save_header_settings (conf, folder_type,
184                                       MODEST_HEADER_VIEW_STYLE_DETAILS,
185                                       INBOX_COLUMNS_DETAILS,
186                                       G_N_ELEMENTS(INBOX_COLUMNS_DETAILS),
187                                       overwrite);
188                 save_header_settings (conf, folder_type,
189                                       MODEST_HEADER_VIEW_STYLE_TWOLINES,
190                                       INBOX_COLUMNS_TWOLINES,
191                                       G_N_ELEMENTS(INBOX_COLUMNS_TWOLINES),
192                                       overwrite);
193                 };
194         }
195         return TRUE;
196 }
197
198 gboolean
199 modest_init_local_folders  (void)
200 {
201         int i;
202         gchar *maildir_path;
203         static const gchar* maildirs[] = {
204                 "cur", "new", "tmp"
205         };
206         
207         maildir_path = modest_local_folder_info_get_maildir_path ();
208
209         for (i = 0; i != G_N_ELEMENTS(LOCAL_FOLDERS); ++i) {
210                 int j;
211                 for (j = 0; j != G_N_ELEMENTS(maildirs); ++j) {
212                         gchar *dir;
213                         dir = g_build_filename (maildir_path,
214                                                 modest_local_folder_info_get_type_name(LOCAL_FOLDERS[i]),
215                                                 maildirs[j],
216                                                 NULL);
217                         if (g_mkdir_with_parents (dir, 0755) < 0) {
218                                 g_printerr ("modest: failed to create %s\n", dir);
219                                 g_free (dir);
220                                 g_free (maildir_path);
221                                 return FALSE;
222                         }
223                         g_free(dir);
224                 }
225         }
226         
227         g_free (maildir_path);
228         return TRUE;
229 }