aboutsummaryrefslogtreecommitdiffstats
path: root/libempathy/empathy-channel-factory.c
blob: d8b1cd941b3ce6f10c49f2e5b7b9c2e5e5a772ec (plain) (blame)
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
/*
 * Copyright (C) 2010 Collabora Ltd.
 *
 * 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., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 *
 * Authors: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
 */

#include <config.h>

#include "empathy-channel-factory.h"

#include "empathy-tp-chat.h"
#include "empathy-utils.h"

#include <telepathy-glib/telepathy-glib.h>

#include <telepathy-yell/telepathy-yell.h>

static void factory_iface_init (gpointer, gpointer);

G_DEFINE_TYPE_WITH_CODE (EmpathyChannelFactory, empathy_channel_factory,
    G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (TP_TYPE_CLIENT_CHANNEL_FACTORY, factory_iface_init))

struct _EmpathyChannelFactoryPrivate
{
  TpClientChannelFactory *automatic_factory;
};

static void
empathy_channel_factory_dispose (GObject *object)
{
  EmpathyChannelFactory *self = EMPATHY_CHANNEL_FACTORY (object);
  void (*dispose) (GObject *) =
    G_OBJECT_CLASS (empathy_channel_factory_parent_class)->dispose;

  tp_clear_object (&self->priv->automatic_factory);

  if (dispose != NULL)
    dispose (object);
}

static void
empathy_channel_factory_class_init (EmpathyChannelFactoryClass *cls)
{
  GObjectClass *object_class = G_OBJECT_CLASS (cls);

  g_type_class_add_private (cls, sizeof (EmpathyChannelFactoryPrivate));

  object_class->dispose = empathy_channel_factory_dispose;
}

static void
empathy_channel_factory_init (EmpathyChannelFactory *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      EMPATHY_TYPE_CHANNEL_FACTORY, EmpathyChannelFactoryPrivate);

  self->priv->automatic_factory = TP_CLIENT_CHANNEL_FACTORY (
      tp_automatic_proxy_factory_dup ());
}

EmpathyChannelFactory *
empathy_channel_factory_new (void)
{
    return g_object_new (EMPATHY_TYPE_CHANNEL_FACTORY,
        NULL);
}

EmpathyChannelFactory *
empathy_channel_factory_dup (void)
{
  static EmpathyChannelFactory *singleton = NULL;

  if (singleton != NULL)
    return g_object_ref (singleton);

  singleton = empathy_channel_factory_new ();

  g_object_add_weak_pointer (G_OBJECT (singleton), (gpointer) &singleton);

  return singleton;
}

static TpChannel *
empathy_channel_factory_create_channel (
    TpClientChannelFactory *factory,
    TpConnection *conn,
    const gchar *path,
    GHashTable *properties,
    GError **error)
{
  EmpathyChannelFactory *self = (EmpathyChannelFactory *) factory;
  const gchar *chan_type;

  chan_type = tp_asv_get_string (properties, TP_PROP_CHANNEL_CHANNEL_TYPE);

  if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
    {
      TpAccount *account;

      account = empathy_get_account_for_connection (conn);

      return TP_CHANNEL (empathy_tp_chat_new (account, conn, path, properties));
    }
  else if (!tp_strdiff (chan_type, TPY_IFACE_CHANNEL_TYPE_CALL))
    {
      return TP_CHANNEL (tpy_call_channel_new (conn, path, properties,
            error));
    }

  return tp_client_channel_factory_create_channel (
      self->priv->automatic_factory, conn, path, properties, error);
}

static GArray *
empathy_channel_factory_dup_channel_features (
    TpClientChannelFactory *factory,
    TpChannel *channel)
{
  EmpathyChannelFactory *self = (EmpathyChannelFactory *) factory;
  GArray *features;
  GQuark feature;

  features =  tp_client_channel_factory_dup_channel_features (
      self->priv->automatic_factory, channel);

  if (EMPATHY_IS_TP_CHAT (channel))
    {
      feature = TP_CHANNEL_FEATURE_CHAT_STATES;
      g_array_append_val (features, feature);

      feature = EMPATHY_TP_CHAT_FEATURE_READY;
      g_array_append_val (features, feature);
    }

  return features;
}

static void
factory_iface_init (gpointer g_iface,
    gpointer unused G_GNUC_UNUSED)
{
  TpClientChannelFactoryInterface *iface = g_iface;

  iface->obj_create_channel = empathy_channel_factory_create_channel;
  iface->obj_dup_channel_features =
    empathy_channel_factory_dup_channel_features;
}