~mandel/ubuntuone-client/donot-add-watches-parents

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 * Syncdaemon API
 *
 * Authors: Rodrigo Moya <rodrigo.moya@canonical.com>
 *
 * Copyright 2010 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "config.h"
#ifdef HAVE_GDBUS
#else
#include <dbus/dbus-glib.h>
#endif
#include "syncdaemon-config-interface.h"

G_DEFINE_TYPE(SyncdaemonConfigInterface, syncdaemon_config_interface, SYNCDAEMON_TYPE_INTERFACE)

struct _SyncdaemonConfigInterfacePrivate {
	GObject *proxy;
};

static void
syncdaemon_config_interface_finalize (GObject *object)
{
	SyncdaemonConfigInterface *interface = SYNCDAEMON_CONFIG_INTERFACE (object);

	if (interface->priv != NULL) {
		g_free (interface->priv);
	}

	G_OBJECT_CLASS (syncdaemon_config_interface_parent_class)->finalize (object);
}

static void
syncdaemon_config_interface_class_init (SyncdaemonConfigInterfaceClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = syncdaemon_config_interface_finalize;
}

static void
syncdaemon_config_interface_init (SyncdaemonConfigInterface *interface)
{
	interface->priv = g_new0 (SyncdaemonConfigInterfacePrivate, 1);

	/* Setup DBus proxy */
	interface->priv->proxy = syncdaemon_interface_setup_proxy (SYNCDAEMON_INTERFACE (interface),
								   "com.ubuntuone.SyncDaemon",
								   "/config", "com.ubuntuone.SyncDaemon.Config");
}

/**
 * syncdaemon_config_interface_new:
 */
SyncdaemonConfigInterface *
syncdaemon_config_interface_new (SyncdaemonDaemon *daemon)
{
	g_return_val_if_fail (SYNCDAEMON_IS_DAEMON (daemon), NULL);

	return g_object_new (SYNCDAEMON_TYPE_CONFIG_INTERFACE, "daemon", daemon, NULL);
}

static gboolean
get_boolean_method (SyncdaemonConfigInterface *interface, const gchar *method)
{
	gboolean enabled;
	GError *error = NULL;

	if (!dbus_g_proxy_call (DBUS_G_PROXY (interface->priv->proxy), method, &error,
				G_TYPE_INVALID,
				G_TYPE_BOOLEAN, &enabled,
				G_TYPE_INVALID)) {
		g_warning ("Failed calling %s: %s", method, error->message);
		g_error_free (error);

		return FALSE;
	}

	return enabled;
}

static void
enable_setting_method (SyncdaemonConfigInterface *interface,
		       const gchar *enable_method,
		       const gchar *disable_method,
		       gboolean enabled)
{
	GError *error = NULL;

	if (enabled) {
		dbus_g_proxy_call (DBUS_G_PROXY (interface->priv->proxy), enable_method, &error,
					    G_TYPE_INVALID,
					    G_TYPE_INVALID);
	} else {
		dbus_g_proxy_call (DBUS_G_PROXY (interface->priv->proxy), disable_method, &error,
					    G_TYPE_INVALID,
					    G_TYPE_INVALID);
	}

	if (error) {
	    g_warning ("Failed calling %s: %s", enabled ? enable_method : disable_method, error->message);
		g_error_free (error);
	}
}

/**
 * syncdaemon_config_interface_get_bandwidth_throttling:
 */
gboolean
syncdaemon_config_interface_get_bandwidth_throttling (SyncdaemonConfigInterface *interface)
{
	g_return_val_if_fail (SYNCDAEMON_IS_CONFIG_INTERFACE (interface), FALSE);

	return get_boolean_method (interface, "bandwidth_throttling_enabled");
}

/**
 * syncdaemon_config_interface_set_bandwidth_throttling:
 */
void
syncdaemon_config_interface_set_bandwidth_throttling (SyncdaemonConfigInterface *interface, gboolean enabled)
{
    	g_return_if_fail (SYNCDAEMON_IS_CONFIG_INTERFACE (interface));

	enable_setting_method (interface, "enable_bandwidth_throttling", "disable_bandwidth_throttling", enabled);
}

/**
 * syncdaemon_config_interface_get_files_sync:
 */
gboolean
syncdaemon_config_interface_get_files_sync (SyncdaemonConfigInterface *interface)
{
	gboolean enabled;
	GError *error = NULL;

	g_return_val_if_fail (SYNCDAEMON_IS_CONFIG_INTERFACE (interface), FALSE);

	if (!dbus_g_proxy_call (DBUS_G_PROXY (interface->priv->proxy), "files_sync_enabled", &error,
				G_TYPE_INVALID,
				G_TYPE_BOOLEAN, &enabled,
				G_TYPE_INVALID)) {
		g_warning ("Failed calling files_sync_enabled: %s", error->message);
		g_error_free (error);

		return FALSE;
	}

	return enabled;
}

/**
 * syncdaemon_config_interface_set_files_sync:
 */
void
syncdaemon_config_interface_set_files_sync (SyncdaemonConfigInterface *interface, gboolean enabled)
{
	GError *error = NULL;

	g_return_if_fail (SYNCDAEMON_IS_CONFIG_INTERFACE (interface));

	if (!dbus_g_proxy_call (DBUS_G_PROXY (interface->priv->proxy), "set_files_sync_enabled", &error,
				G_TYPE_BOOLEAN, enabled,
				G_TYPE_INVALID,
				G_TYPE_INVALID)) {
		g_warning ("Failed calling set_files_sync_enabled: %s", error->message);
		g_error_free (error);
	}
}

/**
 * syncdaemon_config_interface_get_throttling_limits:
 */
void
syncdaemon_config_interface_get_throttling_limits (SyncdaemonConfigInterface *interface,
						   gint *download,
						   gint *upload)
{
	GHashTable *result;
	GError *error = NULL;

	g_return_if_fail (SYNCDAEMON_IS_CONFIG_INTERFACE (interface));

	if (!dbus_g_proxy_call (DBUS_G_PROXY (interface->priv->proxy), "get_throttling_limits", &error,
				G_TYPE_INVALID,
				dbus_g_type_get_map ("GHashTable",
						     G_TYPE_STRING,
						     G_TYPE_INT), &result,
				G_TYPE_INVALID)) {
		g_warning ("Failed calling get_throttling_limits: %s", error->message);
		g_error_free (error);
	} else {
		gint *download_got, *upload_got;

		download_got = g_hash_table_lookup (result, "download");
		upload_got = g_hash_table_lookup (result, "upload");

		if (download != NULL)
			*download = *download_got;
		if (upload != NULL)
			*upload = *upload_got;

		g_hash_table_destroy (result);
	}
}

/**
 * syncdaemon_config_interface_set_throttling_limits:
 */
void
syncdaemon_config_interface_set_throttling_limits (SyncdaemonConfigInterface *interface,
						   gint download,
						   gint upload)
{
	GError *error = NULL;

	g_return_if_fail (SYNCDAEMON_IS_CONFIG_INTERFACE (interface));

	if (!dbus_g_proxy_call (DBUS_G_PROXY (interface->priv->proxy), "set_throttling_limits", &error,
				G_TYPE_INT, download,
				G_TYPE_INT, upload,
				G_TYPE_INVALID,
				G_TYPE_INVALID)) {
		g_warning ("Failed calling set_throttling_limits: %s", error->message);
		g_error_free (error);
	}
}

/**
 * syncdaemon_config_interface_get_udf_autosubscribe:
 */
gboolean
syncdaemon_config_interface_get_udf_autosubscribe (SyncdaemonConfigInterface *interface)
{
	g_return_val_if_fail (SYNCDAEMON_IS_CONFIG_INTERFACE (interface), FALSE);

	return get_boolean_method (interface, "udf_autosubscribe_enabled");
}

/**
 * syncdaemon_config_interface_set_bandwidth_throttling_enabled:
 */
void
syncdaemon_config_interface_set_udf_autosubscribe (SyncdaemonConfigInterface *interface, gboolean enabled)
{
      	g_return_if_fail (SYNCDAEMON_IS_CONFIG_INTERFACE (interface));

	enable_setting_method (interface, "enable_udf_autosubscribe", "disable_udf_autosubscribe", enabled);
}