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