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