summaryrefslogtreecommitdiff
path: root/src/tpl-channel.c
blob: 9373ea96665b5d4b5fb371f92e738d258dc5e9d6 (plain)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2009 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Cosimo Alfarano <cosimo.alfarano@collabora.co.uk>
 */

#include <glib.h>
#include <tpl-observer.h>
#include <tpl-channel.h>
#include <tpl-text-channel-context.h>

G_DEFINE_TYPE (TplChannel, tpl_channel, G_TYPE_OBJECT)

static void tpl_channel_dispose (GObject *obj);
static void tpl_channel_finalize (GObject *obj);

static void tpl_channel_class_init (TplChannelClass* klass)
{
	GObjectClass* object_class = G_OBJECT_CLASS (klass);

	object_class->dispose = tpl_channel_dispose;
	object_class->finalize = tpl_channel_finalize;
}


static void tpl_channel_init (TplChannel* self)
{
	/* Init TplChannel's members to zero/NULL */
#define TPL_SET_NULL(x) tpl_channel_set_##x(self, NULL)
	TPL_SET_NULL(channel);
	TPL_SET_NULL(channel_path);
	TPL_SET_NULL(channel_type);
	TPL_SET_NULL(channel_properties);
	TPL_SET_NULL(account);
	TPL_SET_NULL(account_path);
	TPL_SET_NULL(connection);
	TPL_SET_NULL(connection_path);
	TPL_SET_NULL(observer);
#undef TPL_SET_NULL
}

static void tpl_channel_dispose (GObject* obj)
{
	TplChannel *self = TPL_CHANNEL(obj);

	g_debug("TplChannel dispose start\n");

	tpl_object_unref_if_not_null (self->channel);
	self->channel = NULL;

	if(self->channel_properties != NULL)
	       g_hash_table_unref (self->channel_properties);
	self->channel_properties = NULL;

	tpl_object_unref_if_not_null (self->account);
	self->account = NULL;

	tpl_object_unref_if_not_null (self->connection);
	self->connection = NULL;

	tpl_object_unref_if_not_null (self->observer);
	self->observer = NULL;

	G_OBJECT_CLASS (tpl_channel_parent_class)->dispose (obj);
	g_debug("TplChannel dispose end\n");
}

static void tpl_channel_finalize (GObject* obj)
{
	TplChannel *self = TPL_CHANNEL(obj);
	g_free ((gchar*) self->channel_path);
	g_free ((gchar*) self->channel_type);
	g_free ((gchar*) self->account_path);
	g_free ((gchar*) self->connection_path);

	G_OBJECT_CLASS (tpl_channel_parent_class)->finalize (obj);

	g_debug("TplChannel instance finalized\n");
}



TplChannel* tpl_channel_new(TpSvcClientObserver* observer)
{
	g_return_val_if_fail(TP_IS_SVC_CLIENT_OBSERVER(observer), NULL);

	TplChannel *ret = g_object_new(TPL_TYPE_CHANNEL,NULL);
	tpl_channel_set_observer(ret, observer);
	return ret;
}

TpSvcClientObserver* tpl_channel_get_observer(TplChannel *self)
{
	g_return_val_if_fail(TPL_IS_CHANNEL(self), NULL);
	return self->observer;
}

TpAccount *tpl_channel_get_account(TplChannel *self)
{
	g_return_val_if_fail(TPL_IS_CHANNEL(self), NULL);
	return self->account;
}

const gchar *tpl_channel_get_account_path(TplChannel *self)
{
	g_return_val_if_fail(TPL_IS_CHANNEL(self), NULL);
	return self->account_path;
}

TpConnection *tpl_channel_get_connection(TplChannel *self)
{
	g_return_val_if_fail(TPL_IS_CHANNEL(self), NULL);
	return self->connection;
}

const gchar *tpl_channel_get_connection_path(TplChannel *self)
{
	g_return_val_if_fail(TPL_IS_CHANNEL(self), NULL);
	return self->connection_path;
}

TpChannel *tpl_channel_get_channel(TplChannel *self)
{
	g_return_val_if_fail(TPL_IS_CHANNEL(self), NULL);
	return self->channel;
}

const gchar *tpl_channel_get_channel_path(TplChannel *self)
{
	g_return_val_if_fail(TPL_IS_CHANNEL(self), NULL);
	return self->channel_path;
}

const gchar *tpl_channel_get_channel_type(TplChannel *self)
{
	g_return_val_if_fail(TPL_IS_CHANNEL(self), NULL);
	return self->channel_type;
}

GHashTable *tpl_channel_get_channel_properties(TplChannel *self)
{
	g_return_val_if_fail(TPL_IS_CHANNEL(self), NULL);
	return self->channel_properties;
}



void tpl_channel_set_observer(TplChannel *self,
		TpSvcClientObserver *data)
{
	g_return_if_fail(TPL_IS_CHANNEL(self));
	g_return_if_fail(TP_IS_SVC_CLIENT_OBSERVER(data)||data==NULL);

	tpl_object_unref_if_not_null(self->observer);
	self->observer = data;
	tpl_object_ref_if_not_null(data);
}

void tpl_channel_set_account(TplChannel *self, TpAccount *data)
{
	g_return_if_fail(TPL_IS_CHANNEL(self));
	g_return_if_fail(TP_IS_ACCOUNT(data)||data==NULL);

	tpl_object_unref_if_not_null(self->account);
	self->account = data;
	tpl_object_ref_if_not_null(data);
}

void tpl_channel_set_account_path(TplChannel *self, const gchar *data)
{
	g_return_if_fail(TPL_IS_CHANNEL(self));
	// TODO check validity of data

	g_free ((gchar*) self->account_path);
	self->account_path = g_strdup (data);
}

void tpl_channel_set_connection(TplChannel *self, TpConnection *data)
{
	g_return_if_fail(TPL_IS_CHANNEL(self));
	g_return_if_fail(TP_IS_CONNECTION(data)||data==NULL);

	tpl_object_unref_if_not_null(self->connection);
	self->connection = data;
	tpl_object_ref_if_not_null(data);
}

void tpl_channel_set_connection_path(TplChannel *self, const gchar *data)
{
	g_return_if_fail(TPL_IS_CHANNEL(self));
	// TODO check validity of data

	g_free((gchar*) self->connection_path);
	self->connection_path = g_strdup (data);
}
void tpl_channel_set_channel(TplChannel *self, TpChannel *data)
{
	g_return_if_fail(TPL_IS_CHANNEL(self));
	g_return_if_fail(TP_IS_CHANNEL(data)||data==NULL);

	tpl_object_unref_if_not_null(self->channel);
	self->channel = data;
	tpl_object_ref_if_not_null(data);
}
void tpl_channel_set_channel_path(TplChannel *self, const gchar *data)
{
	g_return_if_fail(TPL_IS_CHANNEL(self));
	// TODO check validity of data

	g_free((gchar*) self->channel_path);
	self->channel_path = g_strdup (data);
}

void tpl_channel_set_channel_type(TplChannel *self, const gchar *data)
{
	g_return_if_fail(TPL_IS_CHANNEL(self));
	// TODO check validity of data

	g_free((gchar*) self->channel_type);
	self->channel_type = g_strdup (data);
}

void tpl_channel_set_channel_properties(TplChannel *self, GHashTable *data)
{
	g_return_if_fail(TPL_IS_CHANNEL(self));
	// TODO check validity of data

	if (self->channel_properties != NULL)
		g_hash_table_unref(self->channel_properties);
	self->channel_properties = data;
	if (data!=NULL)
		g_hash_table_ref(data);
}


gboolean 
tpl_channel_register_to_observer(TplChannel *self)
{
	TplObserver *obs = TPL_OBSERVER(tpl_channel_get_observer(self));
	GHashTable *glob_map = tpl_observer_get_channel_map(obs);
	gchar *key;

	g_return_val_if_fail( TPL_IS_CHANNEL(self), FALSE);
	g_return_val_if_fail( glob_map != NULL, FALSE);

	key = g_strdup (tpl_channel_get_channel_path (self));
	
	if (g_hash_table_lookup(glob_map, key) != NULL) {
		g_error("Channel path found, replacing %s\n", key);
		g_hash_table_remove(glob_map, key);
	} else {
		g_debug("Channel path not found, registering %s\n", key);
	}
	
	// Instantiate and delegate channel handling to the right object
	if (0==g_strcmp0 (TP_IFACE_CHAN_TEXT,
				tpl_channel_get_channel_type(self))) {
		// when removed, automatically frees the Key and unrefs
		// its Value
		TplTextChannel *chan_text = tpl_text_channel_new(self);
		g_hash_table_insert(glob_map, key, chan_text);
	} else {
		g_warning("%s: channel type not handled by this logger", 
			tpl_channel_get_channel_type(self));
	}

	g_object_unref(self);

	return TRUE;
}

gboolean 
tpl_channel_unregister_from_observer(TplChannel *self)
{
	TplObserver *obs = TPL_OBSERVER(tpl_channel_get_observer(self));
	GHashTable *glob_map = tpl_observer_get_channel_map(obs);
	const gchar *key;

	g_assert (TPL_IS_CHANNEL(self));
	g_assert (glob_map != NULL);
	g_return_val_if_fail (TPL_IS_CHANNEL(self), FALSE);
	g_return_val_if_fail (glob_map != NULL, FALSE);

	key = tpl_channel_get_channel_path (self);
	g_debug ("Unregistering channel path %s\n", key);

	// this will destroy the associated value object: at this point
	// the hash table reference should be the only one for the
	// value's object
	return g_hash_table_remove(glob_map, key); 
}