tp-lowlevel.c 8.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * Copyright (C) 2007-2010 Collabora Ltd.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Travis Reitter <travis.reitter@collabora.co.uk>
 *       Xavier Claessens <xavier.claessens@collabora.co.uk>
 */

22
23
24
25
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

26
#include <glib.h>
27
#include <glib/gi18n.h>
28
#include <gio/gio.h>
29
#include <telepathy-glib/telepathy-glib.h>
30

31
#include "tp-lowlevel.h"
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
static void
connection_get_alias_flags_cb (TpConnection *conn,
    guint flags,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);

  if (error != NULL)
    {
      g_simple_async_result_set_from_error (simple, error);
    }
  else
    {
      g_simple_async_result_set_op_res_gpointer (simple,
          GUINT_TO_POINTER (flags), NULL);
    }

  g_simple_async_result_complete (simple);
  g_object_unref (simple);
}

void
folks_tp_lowlevel_connection_get_alias_flags_async (
    TpConnection *conn,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  GSimpleAsyncResult *result;

  result = g_simple_async_result_new (G_OBJECT (conn), callback, user_data,
      folks_tp_lowlevel_connection_get_alias_flags_finish);

  tp_cli_connection_interface_aliasing_call_get_alias_flags (conn, -1,
      connection_get_alias_flags_cb, result, NULL, G_OBJECT (conn));
}

/**
 * folks_tp_lowlevel_connection_get_alias_flags_finish:
 * @result: a #GAsyncResult
 * @error: return location for a #GError, or %NULL
 *
 * Determine the alias-related capabilities of the #TpConnection.
 *
 * Returns: the #TpConnectionAliasFlags
 */
TpConnectionAliasFlags
folks_tp_lowlevel_connection_get_alias_flags_finish (
    GAsyncResult *result,
    GError **error)
{
  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
  TpConnection *conn;

  g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);

  conn = TP_CONNECTION (g_async_result_get_source_object (result));
  g_return_val_if_fail (TP_IS_CONNECTION (conn), FALSE);

  if (g_simple_async_result_propagate_error (simple, error))
    return 0;

  g_return_val_if_fail (g_simple_async_result_is_valid (result,
      G_OBJECT (conn), folks_tp_lowlevel_connection_get_alias_flags_finish),
      0);

  return (TpConnectionAliasFlags) (g_simple_async_result_get_op_res_gpointer (
      G_SIMPLE_ASYNC_RESULT (result)));
}

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
static void
get_contacts_by_id_cb (TpConnection *conn,
    guint n_contacts,
    TpContact * const *contacts,
    const gchar * const *requested_ids,
    GHashTable *failed_id_errors,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);

  if (error != NULL)
    {
      g_simple_async_result_set_from_error (simple, error);
    }
  else
    {
      GList *contact_list = NULL;
      guint i;

      for (i = 0; i < n_contacts; i++)
        contact_list = g_list_prepend (contact_list,
            g_object_ref (contacts[i]));

129
      g_simple_async_result_set_op_res_gpointer (simple, contact_list, NULL);
130
131
132
133
134
135
    }

  g_simple_async_result_complete (simple);
  g_object_unref (simple);
}

136
137
138
139
140
141
142
143
144
145
146
147
148
/**
 * folks_tp_lowlevel_connection_get_contacts_by_id_async:
 * @conn: the connection to use
 * @contact_ids: (array length=contact_ids_length) (element-type utf8): the
 * contact IDs to get
 * @contact_ids_length: number of IDs in @contact_ids
 * @features: (array length=features_length): the features to use
 * @features_length: number of features in @features
 * @callback: function to call on completion
 * @user_data: user data to pass to @callback
 *
 * Get an array of #TpContact<!-- -->s for the given contact IDs.
 */
149
150
151
152
153
void
folks_tp_lowlevel_connection_get_contacts_by_id_async (
    TpConnection *conn,
    const char **contact_ids,
    guint contact_ids_length,
154
    guint *features,
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    guint features_length,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  GSimpleAsyncResult *result;

  result = g_simple_async_result_new (G_OBJECT (conn), callback, user_data,
      folks_tp_lowlevel_connection_get_contacts_by_id_finish);

  tp_connection_get_contacts_by_id (conn,
      contact_ids_length,
      contact_ids,
      features_length,
      features,
      get_contacts_by_id_cb,
      result,
      NULL,
      G_OBJECT (conn));
}

175
176
177
178
179
180
181
182
183
184
185
/**
 * folks_tp_lowlevel_connection_get_contacts_by_id_finish:
 * @result: the async result
 * @error: a #GError, or %NULL
 *
 * Finish an operation started with
 * folks_tp_lowlevel_connection_get_contacts_by_id_async().
 *
 * Return value: (element-type TelepathyGLib.Contact) (transfer full): a list of
 * #TpContact<!-- -->s
 */
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
GList *
folks_tp_lowlevel_connection_get_contacts_by_id_finish (
    GAsyncResult *result,
    GError **error)
{
  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
  TpConnection *conn;

  g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);

  conn = TP_CONNECTION (g_async_result_get_source_object (result));
  g_return_val_if_fail (TP_IS_CONNECTION (conn), FALSE);

  if (g_simple_async_result_propagate_error (simple, error))
    return NULL;

  g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (conn),
        folks_tp_lowlevel_connection_get_contacts_by_id_finish), NULL);

  return g_simple_async_result_get_op_res_gpointer (
      G_SIMPLE_ASYNC_RESULT (result));
}

209
210
211
212
213
214
static void
set_contact_alias_cb (TpConnection *conn,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
215
216
  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);

217
218
  if (error != NULL)
    {
219
      g_simple_async_result_set_from_error (simple, error);
220
    }
221
222

  g_simple_async_result_complete (simple);
223
224
}

225
226
227
228
229
230
231
232
233
234
/**
 * folks_tp_lowlevel_connection_set_contact_alias_async:
 * @conn: the connection to use
 * @handle: handle of the contact whose alias is to be changed
 * @alias: new human-readable alias for the contact
 * @callback: function to call on completion
 * @user_data: user data to pass to @callback
 *
 * Change the alias of the contact identified by @handle to @alias.
 */
235
void
236
folks_tp_lowlevel_connection_set_contact_alias_async (
237
    TpConnection *conn,
238
    guint handle,
239
240
241
    const gchar *alias,
    GAsyncReadyCallback callback,
    gpointer user_data)
242
{
243
244
245
246
  GSimpleAsyncResult *result;
  GHashTable *ht;

  ht = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
247
248
  g_hash_table_insert (ht, GUINT_TO_POINTER (handle), g_strdup (alias));

249
250
251
  result = g_simple_async_result_new (G_OBJECT (conn), callback, user_data,
      folks_tp_lowlevel_connection_set_contact_alias_finish);

252
  tp_cli_connection_interface_aliasing_call_set_aliases (conn, -1,
253
254
      ht, set_contact_alias_cb, g_object_ref (result), g_object_unref,
      G_OBJECT (conn));
255

256
  g_object_unref (result);
257
258
  g_hash_table_destroy (ht);
}
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

/**
 * folks_tp_lowlevel_connection_set_contact_alias_finish:
 * @result: a #GAsyncResult
 * @error: return location for a #GError, or %NULL
 *
 * Finish an asynchronous call to
 * folks_tp_lowlevel_connection-set_contact_alias_async().
 */
void
folks_tp_lowlevel_connection_set_contact_alias_finish (
    GAsyncResult *result,
    GError **error)
{
  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
  TpConnection *conn;

  g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));

  conn = TP_CONNECTION (g_async_result_get_source_object (result));
  g_return_if_fail (TP_IS_CONNECTION (conn));

  g_return_if_fail (g_simple_async_result_is_valid (result,
      G_OBJECT (conn),
      folks_tp_lowlevel_connection_set_contact_alias_finish));

  g_simple_async_result_propagate_error (simple, error);
}