1 /*
2  * evolution-source-registry-migrate-proxies.c
3  *
4  * This library is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library. If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17 
18 #include "evolution-data-server-config.h"
19 
20 #include <glib/gi18n-lib.h>
21 
22 #include <libebackend/libebackend.h>
23 
24 #define NETWORK_CONFIG_SCHEMA_ID "org.gnome.evolution.shell.network-config"
25 
26 #include "evolution-source-registry-methods.h"
27 
28 void
evolution_source_registry_migrate_proxies(ESourceRegistryServer * server)29 evolution_source_registry_migrate_proxies (ESourceRegistryServer *server)
30 {
31 	GSettings *settings;
32 	ESource *source;
33 	ESourceProxy *extension;
34 	EProxyMethod method;
35 	const gchar *extension_name;
36 	const gchar *user_dir;
37 	gboolean system_proxy_exists;
38 	gboolean v_bool;
39 	gchar *filename;
40 	gchar *string;
41 	gchar **strv;
42 	gint v_int;
43 
44 	g_return_if_fail (E_IS_SOURCE_REGISTRY_SERVER (server));
45 
46 	/* If a 'system-proxy.source' file already exists, leave it alone.
47 	 * Otherwise, populate the built-in proxy profile from Evolution's
48 	 * so-called "network-config" in GSettings. */
49 
50 	user_dir = e_server_side_source_get_user_dir ();
51 	filename = g_build_filename (user_dir, "system-proxy.source", NULL);
52 	system_proxy_exists = g_file_test (filename, G_FILE_TEST_IS_REGULAR);
53 	g_free (filename);
54 
55 	if (system_proxy_exists)
56 		return;
57 
58 	source = e_source_registry_server_ref_source (server, "system-proxy");
59 	g_return_if_fail (source != NULL);
60 
61 	extension_name = E_SOURCE_EXTENSION_PROXY;
62 	extension = e_source_get_extension (source, extension_name);
63 
64 	settings = g_settings_new (NETWORK_CONFIG_SCHEMA_ID);
65 
66 	switch (g_settings_get_int (settings, "proxy-type")) {
67 		case 1:
68 			method = E_PROXY_METHOD_NONE;
69 			break;
70 		case 2:
71 			method = E_PROXY_METHOD_MANUAL;
72 			break;
73 		default:
74 			method = E_PROXY_METHOD_DEFAULT;
75 			break;
76 	}
77 
78 	e_source_proxy_set_method (extension, method);
79 
80 	/* Skip empty strings / zero values from GSettings and
81 	 * defer to the default values defined by ESourceProxy. */
82 
83 	string = g_settings_get_string (settings, "autoconfig-url");
84 	if (string != NULL && *string != '\0')
85 		e_source_proxy_set_autoconfig_url (extension, string);
86 	g_free (string);
87 
88 	strv = g_settings_get_strv (settings, "ignore-hosts");
89 	if (strv != NULL && *strv != NULL)
90 		e_source_proxy_set_ignore_hosts (
91 			extension, (const gchar * const *) strv);
92 	g_strfreev (strv);
93 
94 	string = g_settings_get_string (settings, "http-host");
95 	if (string != NULL && *string != '\0')
96 		e_source_proxy_set_http_host (extension, string);
97 	g_free (string);
98 
99 	v_int = g_settings_get_int (settings, "http-port");
100 	if (v_int > 0)
101 		e_source_proxy_set_http_port (extension, (guint16) v_int);
102 
103 	v_bool = g_settings_get_boolean (settings, "use-authentication");
104 	e_source_proxy_set_http_use_auth (extension, v_bool);
105 
106 	string = g_settings_get_string (settings, "authentication-user");
107 	if (string != NULL && *string != '\0')
108 		e_source_proxy_set_http_auth_user (extension, string);
109 	g_free (string);
110 
111 	string = g_settings_get_string (settings, "authentication-password");
112 	if (string != NULL && *string != '\0')
113 		e_source_proxy_set_http_auth_password (extension, string);
114 	g_free (string);
115 
116 	string = g_settings_get_string (settings, "secure-host");
117 	if (string != NULL && *string != '\0')
118 		e_source_proxy_set_https_host (extension, string);
119 	g_free (string);
120 
121 	v_int = g_settings_get_int (settings, "secure-port");
122 	if (v_int > 0)
123 		e_source_proxy_set_https_port (extension, (guint16) v_int);
124 
125 	string = g_settings_get_string (settings, "socks-host");
126 	if (string != NULL && *string != '\0')
127 		e_source_proxy_set_socks_host (extension, string);
128 	g_free (string);
129 
130 	v_int = g_settings_get_int (settings, "socks-port");
131 	if (v_int > 0)
132 		e_source_proxy_set_socks_port (extension, (guint16) v_int);
133 
134 	g_object_unref (settings);
135 }
136 
137