summaryrefslogtreecommitdiff
path: root/src/connection-aliasing.c
blob: 5a5a276a51574c41cdd48ed61c1e6fbdda999576 (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * connection-aliasing.c - Aliasing interface implementation of HazeConnection
 * Copyright (C) 2007 Will Thompson
 * Copyright (C) 2007-2008 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "connection-aliasing.h"

#include <telepathy-glib/contacts-mixin.h>
#include <telepathy-glib/gtypes.h>
#include <telepathy-glib/handle.h>
#include <telepathy-glib/interfaces.h>

#include "connection.h"
#include "debug.h"

static gboolean
can_alias (HazeConnection *conn)
{
    PurplePluginProtocolInfo *prpl;

    g_assert (!purple_account_is_disconnected (conn->account));

    prpl = HAZE_CONNECTION_GET_PRPL_INFO (conn);

    return (prpl->alias_buddy != NULL);
}

static void
haze_connection_get_alias_flags (TpSvcConnectionInterfaceAliasing *self,
                                 DBusGMethodInvocation *context)
{
    TpBaseConnection *base = TP_BASE_CONNECTION (self);
    HazeConnection *conn = HAZE_CONNECTION (base);
    guint flags = 0;

    TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED (base, context);

    if (can_alias (conn))
    {
        flags = TP_CONNECTION_ALIAS_FLAG_USER_SET;
    }

    DEBUG ("alias flags: %u", flags);
    tp_svc_connection_interface_aliasing_return_from_get_alias_flags (
            context, flags);
}

static const gchar *
get_alias (HazeConnection *self,
           TpHandle handle)
{
    TpBaseConnection *base = TP_BASE_CONNECTION (self);
    TpHandleRepoIface *contact_handles =
        tp_base_connection_get_handles (base, TP_HANDLE_TYPE_CONTACT);
    const gchar *bname = tp_handle_inspect (contact_handles, handle);
    const gchar *alias;

    if (handle == base->self_handle)
    {
        alias = purple_connection_get_display_name (self->account->gc);

        if (alias == NULL)
        {
            DEBUG ("self (%s) has no display_name", bname);
            alias = bname;
        }
    }
    else
    {
        PurpleBuddy *buddy = purple_find_buddy (self->account, bname);

        if (buddy != NULL)
        {
            alias = purple_buddy_get_alias (buddy);
        }
        else
        {
            DEBUG ("%s not on blist", bname);
            alias = bname;
        }
    }
    DEBUG ("%s has alias \"%s\"", bname, alias);
    return alias;
}

static void
haze_connection_get_aliases (TpSvcConnectionInterfaceAliasing *self,
                             const GArray *contacts,
                             DBusGMethodInvocation *context)
{
    HazeConnection *conn = HAZE_CONNECTION (self);
    TpBaseConnection *base = TP_BASE_CONNECTION (conn);
    TpHandleRepoIface *contact_handles =
        tp_base_connection_get_handles (base, TP_HANDLE_TYPE_CONTACT);
    guint i;
    GError *error = NULL;
    GHashTable *aliases;

    if (!tp_handles_are_valid (contact_handles, contacts, FALSE, &error))
    {
        dbus_g_method_return_error (context, error);
        g_error_free (error);
        return;
    }

    aliases = g_hash_table_new (NULL, NULL);

    for (i = 0; i < contacts->len; i++)
    {
        TpHandle handle = g_array_index (contacts, TpHandle, i);

        g_hash_table_insert (aliases, GUINT_TO_POINTER (handle),
            (gchar *) get_alias (conn, handle));
    }

    tp_svc_connection_interface_aliasing_return_from_get_aliases (
        context, aliases);
    g_hash_table_destroy (aliases);
}

static void
haze_connection_request_aliases (TpSvcConnectionInterfaceAliasing *self,
                                 const GArray *contacts,
                                 DBusGMethodInvocation *context)
{
    HazeConnection *conn = HAZE_CONNECTION (self);
    TpBaseConnection *base = TP_BASE_CONNECTION (conn);
    TpHandleRepoIface *contact_handles =
        tp_base_connection_get_handles (base, TP_HANDLE_TYPE_CONTACT);
    guint i;
    GError *error = NULL;
    const gchar **aliases;

    if (!tp_handles_are_valid (contact_handles, contacts, FALSE, &error))
    {
        dbus_g_method_return_error (context, error);
        g_error_free (error);
        return;
    }

    aliases = g_new0 (const gchar *, contacts->len + 1);

    for (i = 0; i < contacts->len; i++)
    {
        TpHandle handle = g_array_index (contacts, TpHandle, i);

        aliases[i] = get_alias (conn, handle);
    }

    tp_svc_connection_interface_aliasing_return_from_request_aliases (
        context, aliases);
    g_free (aliases);
}

struct _g_hash_table_foreach_all_in_my_brain
{
    HazeConnection *conn;
    TpHandleRepoIface *contact_handles;
    GError **error;
};

static void
set_alias_success_cb (PurpleAccount *account,
                       const char *new_alias)
{
    TpBaseConnection *base_conn;
    GPtrArray *aliases;
    GValue entry = {0, };

    DEBUG ("purple_account_set_public_alias succeeded, new alias %s",
        new_alias);

    base_conn = ACCOUNT_GET_TP_BASE_CONNECTION (account);

    g_value_init (&entry, TP_STRUCT_TYPE_ALIAS_PAIR);
    g_value_take_boxed (&entry,
        dbus_g_type_specialized_construct (TP_STRUCT_TYPE_ALIAS_PAIR));

    dbus_g_type_struct_set (&entry,
        0, base_conn->self_handle,
        1, new_alias,
        G_MAXUINT);

    aliases = g_ptr_array_sized_new (1);
    g_ptr_array_add (aliases, g_value_get_boxed (&entry));

    tp_svc_connection_interface_aliasing_emit_aliases_changed (base_conn,
        aliases);

    g_value_unset (&entry);
    g_ptr_array_free (aliases, TRUE);
}

static void
set_alias_failure_cb (PurpleAccount *account,
                      const char *error)
{
    DEBUG ("couldn't set alias: %s\n", error);
}

static void
set_aliases_foreach (gpointer key,
                     gpointer value,
                     gpointer user_data)
{
    struct _g_hash_table_foreach_all_in_my_brain *data =
        (struct _g_hash_table_foreach_all_in_my_brain *)user_data;
    GError *error = NULL;
    TpHandle handle = GPOINTER_TO_INT (key);
    gchar *new_alias = (gchar *)value;

    g_assert (can_alias (data->conn));

    if (!tp_handle_is_valid (data->contact_handles, handle, &error))
    {
        /* stop already */
    }
    else if (handle == TP_BASE_CONNECTION (data->conn)->self_handle)
    {
        DEBUG ("setting alias for myself to \"%s\"", new_alias);
        purple_account_set_public_alias (data->conn->account,
                                         new_alias,
                                         set_alias_success_cb,
                                         set_alias_failure_cb);
    }
    else
    {
        const gchar *bname = tp_handle_inspect (data->contact_handles, handle);
        PurpleBuddy *buddy = purple_find_buddy (data->conn->account, bname);

        if (buddy == NULL)
        {
            DEBUG ("can't set alias for %s to \"%s\": not on contact list",
                bname, new_alias);
            g_set_error (&error, TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED,
                "can't set alias for %s to \"%s\": not on contact list",
                bname, new_alias);
        }
        else
        {
            DEBUG ("setting alias for %s to \"%s\"", bname, new_alias);
            purple_blist_alias_buddy (buddy, new_alias);
            serv_alias_buddy (buddy);
        }
    }

    if (error) {
        if (*(data->error) == NULL)
        {
            /* No previous error. */
            *(data->error) = error;
        }
        else
        {
            g_error_free (error);
        }
    }

    return;
}

static void
haze_connection_set_aliases (TpSvcConnectionInterfaceAliasing *self,
                             GHashTable *aliases,
                             DBusGMethodInvocation *context)
{
    HazeConnection *conn = HAZE_CONNECTION (self);
    TpBaseConnection *base = TP_BASE_CONNECTION (conn);
    GError *error = NULL;
    struct _g_hash_table_foreach_all_in_my_brain data = { conn, NULL, &error };
    data.contact_handles =
        tp_base_connection_get_handles (base, TP_HANDLE_TYPE_CONTACT);

    if (!can_alias (conn))
    {
        g_set_error (&error, TP_ERRORS, TP_ERROR_NOT_AVAILABLE,
            "You can't set aliases on this protocol");
        dbus_g_method_return_error (context, error);
        g_error_free (error);
        return;
    }

    g_hash_table_foreach (aliases, set_aliases_foreach, &data);

    if (error)
    {
        dbus_g_method_return_error (context, error);
        g_error_free (error);
    }
    else
    {
        tp_svc_connection_interface_aliasing_return_from_set_aliases (context);
    }

}

void
haze_connection_aliasing_iface_init (gpointer g_iface,
                                     gpointer iface_data)
{
    TpSvcConnectionInterfaceAliasingClass *klass =
        (TpSvcConnectionInterfaceAliasingClass *) g_iface;

#define IMPLEMENT(x) tp_svc_connection_interface_aliasing_implement_##x (\
    klass, haze_connection_##x)
    IMPLEMENT(get_alias_flags);
    IMPLEMENT(get_aliases);
    IMPLEMENT(request_aliases);
    IMPLEMENT(set_aliases);
#undef IMPLEMENT
}

static void
blist_node_aliased_cb (PurpleBlistNode *node,
                       const char *old_alias,
                       gpointer unused)
{
    PurpleBuddy *buddy;
    TpBaseConnection *base_conn;
    GPtrArray *aliases;
    TpHandle handle;
    TpHandleRepoIface *contact_handles;

    if (!PURPLE_BLIST_NODE_IS_BUDDY (node))
        return;

    buddy = (PurpleBuddy *)node;
    base_conn = ACCOUNT_GET_TP_BASE_CONNECTION (buddy->account);
    contact_handles =
        tp_base_connection_get_handles (base_conn, TP_HANDLE_TYPE_CONTACT);
    handle = tp_handle_ensure (contact_handles, buddy->name, NULL, NULL);

    aliases = g_ptr_array_sized_new (1);
    g_ptr_array_add (aliases, tp_value_array_build (2,
          G_TYPE_UINT, handle,
          G_TYPE_STRING, purple_buddy_get_alias (buddy),
          G_TYPE_INVALID));

    tp_svc_connection_interface_aliasing_emit_aliases_changed (base_conn,
        aliases);

    g_ptr_array_free (aliases, TRUE);
    tp_handle_unref (contact_handles, handle);
}

void
haze_connection_aliasing_class_init (GObjectClass *object_class)
{
    void *blist_handle = purple_blist_get_handle ();

    purple_signal_connect (blist_handle, "blist-node-aliased", object_class,
        PURPLE_CALLBACK (blist_node_aliased_cb), NULL);
}

static void
fill_contact_attributes (GObject *object,
                         const GArray *contacts,
                         GHashTable *attributes_hash)
{
    HazeConnection *self = HAZE_CONNECTION (object);
    guint i;

    for (i = 0; i < contacts->len; i++)
    {
        TpHandle handle = g_array_index (contacts, guint, i);
        GValue *value = tp_g_value_slice_new (G_TYPE_STRING);

        g_value_set_string (value, get_alias (self, handle));

        /* this steals the GValue */
        tp_contacts_mixin_set_contact_attribute (attributes_hash, handle,
            TP_IFACE_CONNECTION_INTERFACE_ALIASING "/alias", value);
    }
}

void
haze_connection_aliasing_init (GObject *object)
{
    tp_contacts_mixin_add_contact_attributes_iface (object,
        TP_IFACE_CONNECTION_INTERFACE_ALIASING,
        fill_contact_attributes);
}