]> git.0d.be Git - empathy.git/blob - libempathy/empathy-channel-factory.c
dec1dfade8a22e98006bb6fb6aca200f907c0efe
[empathy.git] / libempathy / empathy-channel-factory.c
1 /*
2  * Copyright (C) 2010 Collabora Ltd.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA  02110-1301  USA
18  *
19  * Authors: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
20  */
21
22 #include "empathy-channel-factory.h"
23
24 #include "empathy-tp-chat.h"
25 #include "empathy-utils.h"
26
27 #include <telepathy-glib/telepathy-glib.h>
28
29 static void factory_iface_init (gpointer, gpointer);
30
31 G_DEFINE_TYPE_WITH_CODE (EmpathyChannelFactory, empathy_channel_factory,
32     G_TYPE_OBJECT,
33     G_IMPLEMENT_INTERFACE (TP_TYPE_CLIENT_CHANNEL_FACTORY, factory_iface_init))
34
35 struct _EmpathyChannelFactoryPrivate
36 {
37   TpClientChannelFactory *automatic_factory;
38 };
39
40 static void
41 empathy_channel_factory_dispose (GObject *object)
42 {
43   EmpathyChannelFactory *self = EMPATHY_CHANNEL_FACTORY (object);
44   void (*dispose) (GObject *) =
45     G_OBJECT_CLASS (empathy_channel_factory_parent_class)->dispose;
46
47   tp_clear_object (&self->priv->automatic_factory);
48
49   if (dispose != NULL)
50     dispose (object);
51 }
52
53 static void
54 empathy_channel_factory_class_init (EmpathyChannelFactoryClass *cls)
55 {
56   GObjectClass *object_class = G_OBJECT_CLASS (cls);
57
58   g_type_class_add_private (cls, sizeof (EmpathyChannelFactoryPrivate));
59
60   object_class->dispose = empathy_channel_factory_dispose;
61 }
62
63 static void
64 empathy_channel_factory_init (EmpathyChannelFactory *self)
65 {
66   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
67       EMPATHY_TYPE_CHANNEL_FACTORY, EmpathyChannelFactoryPrivate);
68
69   self->priv->automatic_factory = TP_CLIENT_CHANNEL_FACTORY (
70       tp_automatic_proxy_factory_dup ());
71 }
72
73 EmpathyChannelFactory *
74 empathy_channel_factory_new (void)
75 {
76     return g_object_new (EMPATHY_TYPE_CHANNEL_FACTORY,
77         NULL);
78 }
79
80 EmpathyChannelFactory *
81 empathy_channel_factory_dup (void)
82 {
83   static EmpathyChannelFactory *singleton = NULL;
84
85   if (singleton != NULL)
86     return g_object_ref (singleton);
87
88   singleton = empathy_channel_factory_new ();
89
90   g_object_add_weak_pointer (G_OBJECT (singleton), (gpointer) &singleton);
91
92   return singleton;
93 }
94
95 static TpChannel *
96 empathy_channel_factory_create_channel (
97     TpClientChannelFactory *factory,
98     TpConnection *conn,
99     const gchar *path,
100     GHashTable *properties,
101     GError **error)
102 {
103   EmpathyChannelFactory *self = (EmpathyChannelFactory *) factory;
104   const gchar *chan_type;
105
106   chan_type = tp_asv_get_string (properties, TP_PROP_CHANNEL_CHANNEL_TYPE);
107
108   if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
109     {
110       TpAccount *account;
111
112       account = empathy_get_account_for_connection (conn);
113
114       return TP_CHANNEL (empathy_tp_chat_new (account, conn, path, properties));
115     }
116
117   return tp_client_channel_factory_create_channel (
118       self->priv->automatic_factory, conn, path, properties, error);
119 }
120
121 static GArray *
122 empathy_channel_factory_dup_channel_features (
123     TpClientChannelFactory *factory,
124     TpChannel *channel)
125 {
126   EmpathyChannelFactory *self = (EmpathyChannelFactory *) factory;
127   GArray *features;
128   GQuark feature;
129
130   features =  tp_client_channel_factory_dup_channel_features (
131       self->priv->automatic_factory, channel);
132
133   if (EMPATHY_IS_TP_CHAT (channel))
134     {
135       feature = TP_CHANNEL_FEATURE_CHAT_STATES;
136       g_array_append_val (features, feature);
137     }
138
139   return features;
140 }
141
142 static void
143 factory_iface_init (gpointer g_iface,
144     gpointer unused G_GNUC_UNUSED)
145 {
146   TpClientChannelFactoryInterface *iface = g_iface;
147
148   iface->obj_create_channel = empathy_channel_factory_create_channel;
149   iface->obj_dup_channel_features =
150     empathy_channel_factory_dup_channel_features;
151 }