Partly fix handling of transport-info messages
[emacs-jabber-tox.git] / tox.c
blobf96f931732527c0247cad60d75765b16634bfdea
1 #include <dbus/dbus-glib.h>
2 #include <glib/gprintf.h>
3 #include "tox.h"
4 #include "tox-session.h"
6 typedef struct _ToxObjectPrivate {
7 guint session_counter;
8 DBusGConnection *connection;
9 } ToxObjectPrivate;
11 G_DEFINE_TYPE(ToxObject, tox_object, G_TYPE_OBJECT)
13 gboolean tox_create_session(ToxObject *obj, guint8 dir, char **ret, GError **error);
15 /* properties */
16 enum {
17 TOX_DBUS_CONNECTION = 1,
20 static void tox_set_property(GObject *obj, guint property_id, const GValue *value, GParamSpec *pspec);
21 static void tox_get_property(GObject *obj, guint property_id, GValue *value, GParamSpec *pspec);
23 #include "tox-object-glue.h"
25 void
26 tox_object_init(ToxObject *obj)
28 obj->priv = g_new0(ToxObjectPrivate, 1);
31 void
32 tox_object_class_init(ToxObjectClass *klass)
34 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
35 GParamSpec *connection_param_spec;
37 gobject_class->set_property = tox_set_property;
38 gobject_class->get_property = tox_get_property;
40 connection_param_spec = g_param_spec_boxed("dbus-connection",
41 "D-Bus connection",
42 "Connection where new sessions are registered",
43 DBUS_TYPE_G_CONNECTION,
44 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
45 g_object_class_install_property(gobject_class,
46 TOX_DBUS_CONNECTION,
47 connection_param_spec);
49 dbus_g_object_type_install_info(TOX_TYPE_OBJECT, &dbus_glib_tox_object_info);
53 static void
54 tox_set_property(GObject *obj, guint property_id, const GValue *value, GParamSpec *pspec)
56 ToxObject *self = (ToxObject *)obj;
58 switch(property_id) {
59 case TOX_DBUS_CONNECTION:
60 /* XXX: this should be a weak ref */
61 /* hm? */
62 /* if (self->priv->connection) */
63 /* dbus_g_connection_unref(self->priv->connection); */
64 self->priv->connection = (DBusGConnection *)g_value_get_boxed(value);
65 /* dbus_g_connection_ref(self->priv->connection); */
66 break;
67 default:
68 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, property_id, pspec);
72 static void
73 tox_get_property(GObject *obj, guint property_id, GValue *value, GParamSpec *pspec)
75 ToxObject *self = (ToxObject *)obj;
77 switch(property_id) {
78 case TOX_DBUS_CONNECTION:
79 g_value_set_boxed(value, self->priv->connection);
80 break;
81 default:
82 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, property_id, pspec);
86 gboolean
87 tox_create_session(ToxObject *obj, guint8 dir, char **ret, GError **error)
89 guint session_number = ++obj->priv->session_counter;
90 DBusGConnection *connection = obj->priv->connection;
91 ToxSession *session;
93 if (dir < 1 || dir > 3) {
94 /* XXX: how to set the D-Bus error name? */
95 g_set_error(error, DBUS_GERROR,
96 DBUS_GERROR_REMOTE_EXCEPTION,
97 "Direction out of range (was %u, should be 1 <= dir <= 3)",
98 dir);
99 return FALSE;
102 session = g_object_new(TOX_TYPE_SESSION, "direction", dir, NULL);
104 *ret = g_strdup_printf("/net/sourceforge/emacs_jabber/Tox/%u", session_number);
105 dbus_g_connection_register_g_object(connection, *ret, G_OBJECT(session));
107 g_debug("CreateSession called, returning %s\n", *ret);
109 return TRUE;