~verterok/ubuntuone-client/fix-987376

617.3.2 by Rodrigo Moya
ubuntu-sso now returns a dict for credentials
1
/*
2
 * Syncdaemon API
3
 *
4
 * Authors: Rodrigo Moya <rodrigo.moya@canonical.com>
5
 *
1222.1.1 by Rodney Dawes
Add required OpenSSL license exception text
6
 * Copyright 2010-2012 Canonical Ltd.
617.3.2 by Rodrigo Moya
ubuntu-sso now returns a dict for credentials
7
 *
8
 * This program is free software: you can redistribute it and/or modify it
9
 * under the terms of the GNU General Public License version 3, as published
10
 * by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful, but
13
 * WITHOUT ANY WARRANTY; without even the implied warranties of
14
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
15
 * PURPOSE.  See the GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along
18
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
1222.1.1 by Rodney Dawes
Add required OpenSSL license exception text
20
 * In addition, as a special exception, the copyright holders give
21
 * permission to link the code of portions of this program with the
22
 * OpenSSL library under certain conditions as described in each
23
 * individual source file, and distribute linked combinations
24
 * including the two.
25
 * You must obey the GNU General Public License in all respects
26
 * for all of the code used other than OpenSSL.  If you modify
27
 * file(s) with this exception, you may extend this exception to your
28
 * version of the file(s), but you are not obligated to do so.  If you
29
 * do not wish to do so, delete this exception statement from your
30
 * version.  If you delete this exception statement from all source
31
 * files in the program, then also delete it here.
32
 *
617.3.2 by Rodrigo Moya
ubuntu-sso now returns a dict for credentials
33
 */
34
35
#include "syncdaemon-credentials.h"
36
37
G_DEFINE_TYPE(SyncdaemonCredentials, syncdaemon_credentials, G_TYPE_OBJECT)
38
39
struct _SyncdaemonCredentialsPrivate {
40
	gchar *consumer_key;
41
	gchar *consumer_secret;
42
	gchar *token;
43
	gchar *token_secret;
44
};
45
46
static void
47
syncdaemon_credentials_finalize (GObject *object)
48
{
49
	SyncdaemonCredentials *credentials = SYNCDAEMON_CREDENTIALS (object);
50
51
	if (credentials->priv != NULL) {
52
		if (credentials->priv->consumer_key != NULL)
53
			g_free (credentials->priv->consumer_key);
54
		if (credentials->priv->consumer_secret != NULL)
55
			g_free (credentials->priv->consumer_secret);
56
		if (credentials->priv->token != NULL)
57
			g_free (credentials->priv->token);
58
		if (credentials->priv->token_secret != NULL)
59
			g_free (credentials->priv->token_secret);
60
61
		g_free (credentials->priv);
62
	}
63
64
	G_OBJECT_CLASS (syncdaemon_credentials_parent_class)->finalize (object);
65
}
66
67
static void
68
syncdaemon_credentials_class_init (SyncdaemonCredentialsClass *klass)
69
{
70
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
71
72
	object_class->finalize = syncdaemon_credentials_finalize;
73
}
74
75
static void
76
syncdaemon_credentials_init (SyncdaemonCredentials *credentials)
77
{
78
	credentials->priv = g_new0 (SyncdaemonCredentialsPrivate, 1);
79
}
80
81
/**
82
 * syncdaemon_credentials_new:
83
 */
84
SyncdaemonCredentials *
85
syncdaemon_credentials_new (void)
86
{
87
	return g_object_new (SYNCDAEMON_TYPE_CREDENTIALS, NULL);
88
}
89
90
/**
91
 * syncdaemon_credentials_new_from_hash_table:
92
 */
93
SyncdaemonCredentials *
94
syncdaemon_credentials_new_from_hash_table (GHashTable *hash)
95
{
96
	SyncdaemonCredentials *credentials;
97
98
	credentials = g_object_new (SYNCDAEMON_TYPE_CREDENTIALS, NULL);
99
	if (hash != NULL) {
100
		syncdaemon_credentials_set_consumer_key (
101
			credentials,
102
			g_hash_table_lookup (hash, "consumer_key"));
103
		syncdaemon_credentials_set_consumer_secret (
104
			credentials,
105
			g_hash_table_lookup (hash, "consumer_secret"));
106
		syncdaemon_credentials_set_token (
107
			credentials,
108
			g_hash_table_lookup (hash, "token"));
109
		syncdaemon_credentials_set_token_secret (
110
			credentials,
111
			g_hash_table_lookup (hash, "token_secret"));
112
	}
113
114
	return credentials;
115
}
116
117
/**
118
 * syncdaemon_credentials_get_oauth_consumer_token:
119
 */
120
const gchar *
121
syncdaemon_credentials_get_consumer_key (SyncdaemonCredentials *credentials)
122
{
123
	g_return_val_if_fail (SYNCDAEMON_IS_CREDENTIALS (credentials), NULL);
124
125
	return (const gchar *) credentials->priv->consumer_key;
126
}
127
128
/**
129
 * syncdaemon_credentials_set_consumer_key:
130
 */
131
void
132
syncdaemon_credentials_set_consumer_key (SyncdaemonCredentials *credentials, const gchar *consumer_key)
133
{
134
	g_return_if_fail (SYNCDAEMON_IS_CREDENTIALS (credentials));
135
136
	if (credentials->priv->consumer_key != NULL)
137
		g_free (credentials->priv->consumer_key);
138
139
	credentials->priv->consumer_key = g_strdup (consumer_key);
140
}
141
142
/**
143
 * syncdaemon_credentials_get_consumer_secret:
144
 */
145
const gchar *
146
syncdaemon_credentials_get_consumer_secret (SyncdaemonCredentials *credentials)
147
{
148
	g_return_val_if_fail (SYNCDAEMON_IS_CREDENTIALS (credentials), NULL);
149
150
	return (const gchar *) credentials->priv->consumer_secret;
151
}
152
153
/**
154
 * syncdaemon_credentials_set_consumer_secret:
155
 */
156
void
157
syncdaemon_credentials_set_consumer_secret (SyncdaemonCredentials *credentials, const gchar *consumer_secret)
158
{
159
	g_return_if_fail (SYNCDAEMON_IS_CREDENTIALS (credentials));
160
161
	if (credentials->priv->consumer_secret != NULL)
162
		g_free (credentials->priv->consumer_secret);
163
164
	credentials->priv->consumer_secret = g_strdup (consumer_secret);
165
}
166
167
/**
168
 * syncdaemon_credentials_get_token:
169
 */
170
const gchar *
171
syncdaemon_credentials_get_token (SyncdaemonCredentials *credentials)
172
{
173
	g_return_val_if_fail (SYNCDAEMON_IS_CREDENTIALS (credentials), NULL);
174
175
	return (const gchar *) credentials->priv->token;
176
}
177
178
/**
179
 * syncdaemon_credentials_set_token:
180
 */
181
void
182
syncdaemon_credentials_set_token (SyncdaemonCredentials *credentials, const gchar *token)
183
{
184
	g_return_if_fail (SYNCDAEMON_IS_CREDENTIALS (credentials));
185
186
	if (credentials->priv->token != NULL)
187
		g_free (credentials->priv->token);
188
189
	credentials->priv->token = g_strdup (token);
190
}
191
192
/**
193
 * syncdaemon_credentials_get_token_secret:
194
 */
195
const gchar *
196
syncdaemon_credentials_get_token_secret (SyncdaemonCredentials *credentials)
197
{
198
	g_return_val_if_fail (SYNCDAEMON_IS_CREDENTIALS (credentials), NULL);
199
200
	return (const gchar *) credentials->priv->token_secret;
201
}
202
203
/**
204
 * syncdaemon_credentials_set_token_secret:
205
 */
206
void
207
syncdaemon_credentials_set_token_secret (SyncdaemonCredentials *credentials, const gchar *token_secret)
208
{
209
	g_return_if_fail (SYNCDAEMON_IS_CREDENTIALS (credentials));
210
211
	if (credentials->priv->token_secret != NULL)
212
		g_free (credentials->priv->token_secret);
213
214
	credentials->priv->token_secret = g_strdup (token_secret);
215
}