summaryrefslogtreecommitdiff
path: root/src/plugin.c
blob: 783e721a476e0e225e6162a787f117d9143ed9ad (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
/*
 * plugin.c — API for telepathy-gabble plugins
 * Copyright © 2009 Collabora Ltd.
 * Copyright © 2009 Nokia Corporation
 *
 * 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
 */

#include "gabble/plugin.h"

#include <telepathy-glib/errors.h>
#include <telepathy-glib/presence-mixin.h>
#include <telepathy-glib/util.h>

#define DEBUG_FLAG GABBLE_DEBUG_PLUGINS
#include "debug.h"

GType
gabble_plugin_get_type (void)
{
  static GType type = 0;

  if (type == 0) {
    static const GTypeInfo info = {
      sizeof (GabblePluginInterface),
      NULL,   /* base_init */
      NULL,   /* base_finalize */
      NULL,   /* class_init */
      NULL,   /* class_finalize */
      NULL,   /* class_data */
      0,
      0,      /* n_preallocs */
      NULL    /* instance_init */
    };

    type = g_type_register_static (G_TYPE_INTERFACE, "GabblePlugin", &info, 0);
  }

  return type;
}

const gchar *
gabble_plugin_get_name (GabblePlugin *plugin)
{
  GabblePluginInterface *iface = GABBLE_PLUGIN_GET_INTERFACE (plugin);

  return iface->name;
}

const gchar *
gabble_plugin_get_version (GabblePlugin *plugin)
{
  GabblePluginInterface *iface = GABBLE_PLUGIN_GET_INTERFACE (plugin);

  return iface->version;
}

const gchar * const *
gabble_plugin_get_sidecar_interfaces (GabblePlugin *plugin)
{
  GabblePluginInterface *iface = GABBLE_PLUGIN_GET_INTERFACE (plugin);

  return iface->sidecar_interfaces;
}

gboolean
gabble_plugin_implements_sidecar (
    GabblePlugin *plugin,
    const gchar *sidecar_interface)
{
  GabblePluginInterface *iface = GABBLE_PLUGIN_GET_INTERFACE (plugin);

  return tp_strv_contains (iface->sidecar_interfaces, sidecar_interface);
}

/**
 * gabble_plugin_create_sidecar:
 * @plugin: a plugin
 * @sidecar_interface: the primary D-Bus interface implemented by the sidecar,
 *                     which must be a member of the list returned by
 *                     gabble_plugin_get_sidecar_interfaces ()
 * @callback: function to call when the new sidecar has been created, or an
 *            unrecoverable error has occured
 * @user_data: data to pass to @callback
 */
void
gabble_plugin_create_sidecar_async (
    GabblePlugin *plugin,
    const gchar *sidecar_interface,
    GabbleConnection *connection,
    WockySession *session,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  GabblePluginInterface *iface = GABBLE_PLUGIN_GET_INTERFACE (plugin);

  if (!gabble_plugin_implements_sidecar (plugin, sidecar_interface))
    g_simple_async_report_error_in_idle (G_OBJECT (plugin), callback,
        user_data, TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED,
        "Gabble is buggy: '%s' doesn't implement sidecar %s",
        iface->name, sidecar_interface);
  else if (iface->create_sidecar_async == NULL)
    g_simple_async_report_error_in_idle (G_OBJECT (plugin), callback,
        user_data, TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED,
        "'%s' is buggy: it claims to implement %s, but does not implement "
        "create_sidecar_async", iface->name, sidecar_interface);
  else if (iface->create_sidecar_finish == NULL)
    g_simple_async_report_error_in_idle (G_OBJECT (plugin), callback,
        user_data, TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED,
        "'%s' is buggy: does not imlement create_sidecar_finish",
        iface->name);
  else
    iface->create_sidecar_async (plugin, sidecar_interface, connection, session,
        callback, user_data);
}

GabbleSidecar *
gabble_plugin_create_sidecar_finish (
    GabblePlugin *plugin,
    GAsyncResult *result,
    GError **error)
{
  GabblePluginInterface *iface = GABBLE_PLUGIN_GET_INTERFACE (plugin);
  GabbleSidecar *sidecar;

  if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
          error))
    return NULL;

  if (iface->create_sidecar_finish == NULL) {
    WARNING ("'%s' is buggy: does not implement create_sidecar_finish", iface->name);
    return NULL;
  }

  sidecar = iface->create_sidecar_finish (plugin, result, error);

  return g_object_ref (sidecar);
}

const TpPresenceStatusSpec *
gabble_plugin_get_custom_presence_statuses (
    GabblePlugin *plugin)
{
  GabblePluginInterface *iface = GABBLE_PLUGIN_GET_INTERFACE (plugin);

  return iface->presence_statuses;
}

gboolean
gabble_plugin_implements_presence_status (
    GabblePlugin *plugin,
    const gchar *status)
{
  GabblePluginInterface *iface = GABBLE_PLUGIN_GET_INTERFACE (plugin);
  gint i;

  if (iface->presence_statuses == NULL)
    return FALSE;

  for (i = 0; iface->presence_statuses[i].name; i++)
    {
      if (!tp_strdiff (status, iface->presence_statuses[i].name))
        return TRUE;
    }

  return FALSE;
}

const gchar *
gabble_plugin_presence_status_for_privacy_list (
    GabblePlugin *plugin,
    const gchar *list_name)
{
  GabblePluginInterface *iface = GABBLE_PLUGIN_GET_INTERFACE (plugin);
  int i;

  if (iface->privacy_list_map == NULL)
    return NULL;

  for (i = 0; iface->privacy_list_map[i].privacy_list_name; i++)
    {
      if (!tp_strdiff (list_name,
              iface->privacy_list_map[i].privacy_list_name))
        {
          DEBUG ("Plugin %s links presence %s with privacy list %s",
            iface->name,
            iface->privacy_list_map[i].privacy_list_name,
            iface->privacy_list_map[i].presence_status_name);

          return iface->privacy_list_map[i].presence_status_name;
        }
    }

  DEBUG ("No plugins link presence to privacy list %s",
      list_name);

  return NULL;
}

GPtrArray *
gabble_plugin_create_channel_managers (GabblePlugin *plugin,
    TpBaseConnection *connection)
{
  GabblePluginInterface *iface = GABBLE_PLUGIN_GET_INTERFACE (plugin);
  GabblePluginCreateChannelManagersImpl func = iface->create_channel_managers;
  GPtrArray *out = NULL;

  if (func != NULL)
    out = func (plugin, connection);

  return out;
}