Do ignore empty recipients when checking names
[modest] / src / modest-server-account-settings.c
1 /* Copyright (c) 2007, 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 <modest-protocol-registry.h>
31 #include <modest-server-account-settings.h>
32 #include <strings.h>
33 #include <string.h>
34
35 /* 'private'/'protected' functions */
36 static void   modest_server_account_settings_class_init (ModestServerAccountSettingsClass *klass);
37 static void   modest_server_account_settings_finalize   (GObject *obj);
38 static void   modest_server_account_settings_instance_init (ModestServerAccountSettings *obj);
39
40 typedef struct _ModestServerAccountSettingsPrivate ModestServerAccountSettingsPrivate;
41 struct _ModestServerAccountSettingsPrivate {
42         gchar *hostname;
43         guint port;
44         ModestProtocolType protocol;
45         gchar *username;
46         gchar *password;
47         ModestProtocolType security_protocol;
48         ModestProtocolType auth_protocol;
49         gchar *account_name;
50         gchar *uri;
51 };
52
53 #define MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE(o)     (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
54                                                            MODEST_TYPE_SERVER_ACCOUNT_SETTINGS, \
55                                                            ModestServerAccountSettingsPrivate))
56
57 /* globals */
58 static GObjectClass *parent_class = NULL;
59
60 GType
61 modest_server_account_settings_get_type (void)
62 {
63         static GType my_type = 0;
64
65         if (!my_type) {
66                 static const GTypeInfo my_info = {
67                         sizeof(ModestServerAccountSettingsClass),
68                         NULL,   /* base init */
69                         NULL,   /* base finalize */
70                         (GClassInitFunc) modest_server_account_settings_class_init,
71                         NULL,   /* class finalize */
72                         NULL,   /* class data */
73                         sizeof(ModestServerAccountSettings),
74                         0,      /* n_preallocs */
75                         (GInstanceInitFunc) modest_server_account_settings_instance_init,
76                         NULL
77                 };
78
79                 my_type = g_type_register_static (G_TYPE_OBJECT,
80                                                   "ModestServerAccountSettings",
81                                                   &my_info, 0);
82         }
83         return my_type;
84 }
85
86 static void
87 modest_server_account_settings_class_init (ModestServerAccountSettingsClass *klass)
88 {
89         GObjectClass *gobject_class;
90         gobject_class = (GObjectClass *) klass;
91
92         parent_class = g_type_class_peek_parent (klass);
93         gobject_class->finalize = modest_server_account_settings_finalize;
94
95         g_type_class_add_private (gobject_class,
96                                   sizeof(ModestServerAccountSettingsPrivate));
97 }
98
99 static void
100 modest_server_account_settings_instance_init (ModestServerAccountSettings *obj)
101 {
102         ModestServerAccountSettingsPrivate *priv;
103
104         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (obj);
105
106         priv->hostname = NULL;
107         priv->protocol = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
108         priv->port = 0;
109         priv->username = NULL;
110         priv->password = NULL;
111         priv->security_protocol = MODEST_PROTOCOLS_CONNECTION_NONE;
112         priv->auth_protocol = MODEST_PROTOCOLS_AUTH_NONE;
113         priv->account_name = NULL;
114         priv->uri = NULL;
115 }
116
117 static void   
118 modest_server_account_settings_finalize   (GObject *obj)
119 {
120         ModestServerAccountSettings *settings = MODEST_SERVER_ACCOUNT_SETTINGS (obj);
121         ModestServerAccountSettingsPrivate *priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);
122         g_free (priv->hostname);
123         priv->hostname = NULL;
124         g_free (priv->username);
125         priv->username = NULL;
126
127         if (priv->password) {
128                 bzero (priv->password, strlen (priv->password));
129                 g_free (priv->password);
130         }
131         priv->password = NULL;
132
133         priv->protocol = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
134         priv->port = 0;
135         priv->security_protocol = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
136         priv->auth_protocol = MODEST_PROTOCOL_REGISTRY_TYPE_INVALID;
137         g_free (priv->account_name);
138         priv->account_name = NULL;
139         g_free (priv->uri);
140         priv->uri = NULL;
141
142         G_OBJECT_CLASS (parent_class)->finalize (obj);
143 }
144
145 ModestServerAccountSettings*
146 modest_server_account_settings_new (void)
147 {
148         return g_object_new (MODEST_TYPE_SERVER_ACCOUNT_SETTINGS, NULL);
149 }
150
151 const gchar* 
152 modest_server_account_settings_get_hostname (ModestServerAccountSettings *settings)
153 {
154         ModestServerAccountSettingsPrivate *priv;
155
156         g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), NULL);
157
158         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);   
159         return priv->hostname;
160 }
161
162 void         
163 modest_server_account_settings_set_hostname (ModestServerAccountSettings *settings,
164                                              const gchar *hostname)
165 {
166         ModestServerAccountSettingsPrivate *priv;
167
168         g_return_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings));
169
170         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);
171         g_free (priv->hostname);
172         priv->hostname = g_strdup (hostname);
173 }
174
175 const gchar* 
176 modest_server_account_settings_get_uri (ModestServerAccountSettings *settings)
177 {
178         ModestServerAccountSettingsPrivate *priv;
179
180         g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), NULL);
181
182         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);   
183         return priv->uri;
184 }
185
186 void         
187 modest_server_account_settings_set_uri (ModestServerAccountSettings *settings,
188                                         const gchar *uri)
189 {
190         ModestServerAccountSettingsPrivate *priv;
191         
192         g_return_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings));
193
194         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);
195         g_free (priv->uri);
196         priv->uri = g_strdup (uri);
197
198 }
199
200 const gchar* 
201 modest_server_account_settings_get_username (ModestServerAccountSettings *settings)
202 {
203         ModestServerAccountSettingsPrivate *priv;
204
205         g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), NULL);
206
207         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);   
208         return priv->username;
209 }
210
211 void         
212 modest_server_account_settings_set_username (ModestServerAccountSettings *settings,
213                                              const gchar *username)
214 {
215         ModestServerAccountSettingsPrivate *priv;
216
217         g_return_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings));
218
219         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);
220         g_free (priv->username);
221         priv->username = g_strdup (username);
222 }
223
224 const gchar* 
225 modest_server_account_settings_get_password (ModestServerAccountSettings *settings)
226 {
227         ModestServerAccountSettingsPrivate *priv;
228
229         g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), NULL);
230
231         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);   
232         return priv->password;
233 }
234
235 void         
236 modest_server_account_settings_set_password (ModestServerAccountSettings *settings,
237                                              const gchar *password)
238 {
239         ModestServerAccountSettingsPrivate *priv;
240
241         g_return_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings));
242
243         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);
244         if (priv->password) {
245                 bzero (priv->password, strlen (priv->password));
246                 g_free (priv->password);
247         }
248         priv->password = g_strdup (password);
249 }
250
251 const gchar* 
252 modest_server_account_settings_get_account_name (ModestServerAccountSettings *settings)
253 {
254         ModestServerAccountSettingsPrivate *priv;
255
256         g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), NULL);
257
258         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);   
259         return priv->account_name;
260 }
261
262 void         
263 modest_server_account_settings_set_account_name (ModestServerAccountSettings *settings,
264                                                  const gchar *account_name)
265 {
266         ModestServerAccountSettingsPrivate *priv;
267
268         /* be careful. This method should only be used internally in #ModestAccountMgr and
269          * #ModestAccountSettings. */
270
271         g_return_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings));
272
273         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);
274         g_free (priv->account_name);
275         priv->account_name = g_strdup (account_name);
276 }
277
278 ModestProtocolType
279 modest_server_account_settings_get_protocol (ModestServerAccountSettings *settings)
280 {
281         ModestServerAccountSettingsPrivate *priv;
282
283         g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), MODEST_PROTOCOL_REGISTRY_TYPE_INVALID);
284
285         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);
286         return priv->protocol;
287 }
288
289 void                          
290 modest_server_account_settings_set_protocol (ModestServerAccountSettings *settings,
291                                              ModestProtocolType protocol)
292 {
293         ModestServerAccountSettingsPrivate *priv;
294
295         g_return_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings));
296
297         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);
298         priv->protocol = protocol;
299         
300 }
301
302 guint  
303 modest_server_account_settings_get_port (ModestServerAccountSettings *settings)
304 {
305         ModestServerAccountSettingsPrivate *priv;
306
307         g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), 0);
308
309         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);
310         return priv->port;
311 }
312
313 void   
314 modest_server_account_settings_set_port (ModestServerAccountSettings *settings,
315                                          guint port)
316 {
317         ModestServerAccountSettingsPrivate *priv;
318
319         g_return_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings));
320
321         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);
322         priv->port = port;
323 }
324
325 ModestProtocolType
326 modest_server_account_settings_get_security_protocol (ModestServerAccountSettings *settings)
327 {
328         ModestServerAccountSettingsPrivate *priv;
329
330         g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), MODEST_PROTOCOL_REGISTRY_TYPE_INVALID);
331
332         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);
333         return priv->security_protocol;
334 }
335
336 void   
337 modest_server_account_settings_set_security_protocol (ModestServerAccountSettings *settings,
338                                                       ModestProtocolType security_protocol)
339 {
340         ModestServerAccountSettingsPrivate *priv;
341
342         g_return_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings));
343
344         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);
345         priv->security_protocol = security_protocol;
346 }
347
348 ModestProtocolType
349 modest_server_account_settings_get_auth_protocol (ModestServerAccountSettings *settings)
350 {
351         ModestServerAccountSettingsPrivate *priv;
352
353         g_return_val_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings), MODEST_PROTOCOL_REGISTRY_TYPE_INVALID);
354
355         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);
356         return priv->auth_protocol;
357 }
358
359 void   
360 modest_server_account_settings_set_auth_protocol (ModestServerAccountSettings *settings,
361                                                   ModestProtocolType auth_protocol)
362 {
363         ModestServerAccountSettingsPrivate *priv;
364
365         g_return_if_fail (MODEST_IS_SERVER_ACCOUNT_SETTINGS (settings));
366
367         priv = MODEST_SERVER_ACCOUNT_SETTINGS_GET_PRIVATE (settings);
368         priv->auth_protocol = auth_protocol;
369 }
370