No issues found
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2 of the License, or (at your option) version 3.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with the program; if not, see <http://www.gnu.org/licenses/>
14 *
15 *
16 * Authors:
17 * Michael Zucchi <notzed@ximian.com>
18 *
19 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
20 *
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include <gtk/gtk.h>
31
32 #include "em-config.h"
33 #include "em-utils.h"
34 #include "em-composer-utils.h"
35
36 #include <e-util/e-util.h>
37
38 G_DEFINE_TYPE (EMConfig, em_config, E_TYPE_CONFIG)
39
40 static void
41 em_config_set_target (EConfig *ep,
42 EConfigTarget *t)
43 {
44 /* Chain up to parent's set_target() method. */
45 E_CONFIG_CLASS (em_config_parent_class)->set_target (ep, t);
46
47 if (t) {
48 switch (t->type) {
49 case EM_CONFIG_TARGET_FOLDER: {
50 /*EMConfigTargetFolder *s = (EMConfigTargetFolder *)t;*/
51 break; }
52 case EM_CONFIG_TARGET_PREFS: {
53 /*EMConfigTargetPrefs *s = (EMConfigTargetPrefs *)t;*/
54 break; }
55 case EM_CONFIG_TARGET_SETTINGS: {
56 EMConfigTargetSettings *s = (EMConfigTargetSettings *) t;
57
58 em_config_target_update_settings (
59 ep, s,
60 s->email_address,
61 s->storage_protocol,
62 s->storage_settings,
63 s->transport_protocol,
64 s->transport_settings);
65 break; }
66 }
67 }
68 }
69
70 static void
71 em_config_target_free (EConfig *ep,
72 EConfigTarget *t)
73 {
74 if (ep->target == t) {
75 switch (t->type) {
76 case EM_CONFIG_TARGET_FOLDER:
77 break;
78 case EM_CONFIG_TARGET_PREFS:
79 break;
80 case EM_CONFIG_TARGET_SETTINGS: {
81 EMConfigTargetSettings *s = (EMConfigTargetSettings *) t;
82
83 em_config_target_update_settings (
84 ep, s, NULL, NULL, NULL, NULL, NULL);
85 break; }
86 }
87 }
88
89 switch (t->type) {
90 case EM_CONFIG_TARGET_FOLDER: {
91 EMConfigTargetFolder *s = (EMConfigTargetFolder *) t;
92
93 g_object_unref (s->folder);
94 break; }
95 case EM_CONFIG_TARGET_PREFS: {
96 /* EMConfigTargetPrefs *s = (EMConfigTargetPrefs *) t; */
97 break; }
98 case EM_CONFIG_TARGET_SETTINGS: {
99 EMConfigTargetSettings *s = (EMConfigTargetSettings *) t;
100
101 g_free (s->email_address);
102 if (s->storage_settings != NULL)
103 g_object_unref (s->storage_settings);
104 if (s->transport_settings != NULL)
105 g_object_unref (s->transport_settings);
106 break; }
107 }
108
109 /* Chain up to parent's target_free() method. */
110 E_CONFIG_CLASS (em_config_parent_class)->target_free (ep, t);
111 }
112
113 static void
114 em_config_class_init (EMConfigClass *class)
115 {
116 EConfigClass *config_class;
117
118 config_class = E_CONFIG_CLASS (class);
119 config_class->set_target = em_config_set_target;
120 config_class->target_free = em_config_target_free;
121 }
122
123 static void
124 em_config_init (EMConfig *emp)
125 {
126 }
127
128 EMConfig *
129 em_config_new (gint type,
130 const gchar *menuid)
131 {
132 EMConfig *emp;
133
134 emp = g_object_new (EM_TYPE_CONFIG, NULL);
135 e_config_construct (&emp->config, type, menuid);
136
137 return emp;
138 }
139
140 EMConfigTargetFolder *
141 em_config_target_new_folder (EMConfig *emp,
142 CamelFolder *folder)
143 {
144 EMConfigTargetFolder *t;
145
146 t = e_config_target_new (
147 &emp->config, EM_CONFIG_TARGET_FOLDER, sizeof (*t));
148
149 t->folder = g_object_ref (folder);
150
151 return t;
152 }
153
154 EMConfigTargetPrefs *
155 em_config_target_new_prefs (EMConfig *emp)
156 {
157 EMConfigTargetPrefs *t;
158
159 t = e_config_target_new (
160 &emp->config, EM_CONFIG_TARGET_PREFS, sizeof (*t));
161
162 return t;
163 }
164
165 EMConfigTargetSettings *
166 em_config_target_new_settings (EMConfig *emp,
167 const gchar *email_address,
168 const gchar *storage_protocol,
169 CamelSettings *storage_settings,
170 const gchar *transport_protocol,
171 CamelSettings *transport_settings)
172 {
173 EMConfigTargetSettings *target;
174
175 target = e_config_target_new (
176 &emp->config, EM_CONFIG_TARGET_SETTINGS, sizeof (*target));
177
178 if (storage_protocol != NULL)
179 storage_protocol = g_intern_string (storage_protocol);
180
181 if (storage_settings != NULL)
182 g_object_ref (storage_settings);
183
184 if (transport_protocol != NULL)
185 transport_protocol = g_intern_string (transport_protocol);
186
187 if (transport_settings != NULL)
188 g_object_ref (transport_settings);
189
190 target->email_address = g_strdup (email_address);
191
192 target->storage_protocol = storage_protocol;
193 target->storage_settings = storage_settings;
194
195 target->transport_protocol = transport_protocol;
196 target->transport_settings = transport_settings;
197
198 return target;
199 }
200
201 void
202 em_config_target_update_settings (EConfig *ep,
203 EMConfigTargetSettings *target,
204 const gchar *email_address,
205 const gchar *storage_protocol,
206 CamelSettings *storage_settings,
207 const gchar *transport_protocol,
208 CamelSettings *transport_settings)
209 {
210 gchar *tmp;
211
212 g_return_if_fail (ep != NULL);
213 g_return_if_fail (target != NULL);
214
215 if (storage_protocol != NULL)
216 storage_protocol = g_intern_string (storage_protocol);
217
218 if (storage_settings != NULL)
219 g_object_ref (storage_settings);
220
221 if (transport_protocol != NULL)
222 transport_protocol = g_intern_string (transport_protocol);
223
224 if (transport_settings != NULL)
225 g_object_ref (transport_settings);
226
227 if (target->storage_settings != NULL)
228 g_object_unref (target->storage_settings);
229
230 if (target->transport_settings != NULL)
231 g_object_unref (target->transport_settings);
232
233 /* the pointers can be same, thus avoid use-after-free */
234 tmp = g_strdup (email_address);
235 g_free (target->email_address);
236 target->email_address = tmp;
237
238 target->storage_protocol = storage_protocol;
239 target->storage_settings = storage_settings;
240
241 target->transport_protocol = transport_protocol;
242 target->transport_settings = transport_settings;
243 }