summaryrefslogtreecommitdiff
path: root/libempathy/empathy-chandler.c
blob: adc415a933cde92ad757c338c4745be18f55c96d (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2007 Xavier Claessens <xclaesse@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

#include <config.h>

#include <dbus/dbus-glib.h>

#include <libtelepathy/tp-helpers.h>
#include <libtelepathy/tp-conn.h>
#include <libtelepathy/tp-chan.h>

#include "empathy-chandler.h"
#include "gossip-debug.h"
#include "empathy-marshal.h"

#define DEBUG_DOMAIN "EmpathyChandler"

static gboolean empathy_chandler_handle_channel (EmpathyChandler *chandler,
						 const gchar     *bus_name,
						 const gchar     *connection,
						 const gchar     *channel_type,
						 const gchar     *channel,
						 guint            handle_type,
						 guint            handle,
						 GError         **error);

#include "empathy-chandler-glue.h"

enum {
	NEW_CHANNEL,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

G_DEFINE_TYPE (EmpathyChandler, empathy_chandler, G_TYPE_OBJECT)

static void
empathy_chandler_class_init (EmpathyChandlerClass *klass)
{
	signals[NEW_CHANNEL] =
		g_signal_new ("new-channel",
			      G_OBJECT_CLASS_TYPE (klass),
			      G_SIGNAL_RUN_LAST,
			      0,
			      NULL, NULL,
			      empathy_marshal_VOID__OBJECT_OBJECT,
			      G_TYPE_NONE,
			      2, TELEPATHY_CONN_TYPE, TELEPATHY_CHAN_TYPE);
}

static void
empathy_chandler_init (EmpathyChandler *chandler)
{
}

EmpathyChandler *
empathy_chandler_new (const gchar *bus_name,
		      const gchar *object_path)
{
	static gboolean  initialized = FALSE;
	EmpathyChandler *chandler;
	DBusGProxy      *proxy;
	guint            result;
	GError          *error = NULL;

	if (!initialized) {
		dbus_g_object_type_install_info (EMPATHY_TYPE_CHANDLER,
						 &dbus_glib_empathy_chandler_object_info);
		initialized = TRUE;
	}

	proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
					   DBUS_SERVICE_DBUS,
					   DBUS_PATH_DBUS,
					   DBUS_INTERFACE_DBUS);

	if (!dbus_g_proxy_call (proxy, "RequestName", &error,
				G_TYPE_STRING, bus_name,
				G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
				G_TYPE_INVALID,
				G_TYPE_UINT, &result,
				G_TYPE_INVALID)) {
		gossip_debug (DEBUG_DOMAIN,
			      "Failed to request name: %s",
			      error ? error->message : "No error given");
		g_clear_error (&error);

		return NULL;
	}
	g_object_unref (proxy);

	chandler = g_object_new (EMPATHY_TYPE_CHANDLER, NULL);
	dbus_g_connection_register_g_object (tp_get_bus (),
					     object_path,
					     G_OBJECT (chandler));

	return chandler;
}

static gboolean
empathy_chandler_handle_channel (EmpathyChandler  *chandler,
				 const gchar      *bus_name,
				 const gchar      *connection,
				 const gchar      *channel_type,
				 const gchar      *channel,
				 guint             handle_type,
				 guint             handle,
				 GError          **error)
{
	TpChan *tp_chan;
	TpConn *tp_conn;

	tp_conn = tp_conn_new (tp_get_bus (),
			       bus_name,
			       connection);

	tp_chan = tp_chan_new (tp_get_bus(),
			       bus_name,
			       channel,
			       channel_type,
			       handle_type,
			       handle);

	g_signal_emit (chandler, signals[NEW_CHANNEL], 0, tp_conn, tp_chan);

	g_object_unref (tp_chan);
	g_object_unref (tp_conn);

	return TRUE;
}