~ubuntu-branches/ubuntu/wily/telepathy-glib/wily

« back to all changes in this revision

Viewing changes to examples/client/inspect-contact.c

  • Committer: Bazaar Package Importer
  • Author(s): Simon McVittie
  • Date: 2009-03-24 22:06:52 UTC
  • mfrom: (1.3.1 upstream) (17.1.10 sid)
  • Revision ID: james.westby@ubuntu.com-20090324220652-c8dvom0nsqomp23d
Tags: 0.7.28-1
* New upstream version (ABI, API added)
* Put the -dbg package in section debug, as per recent archive changes
* Remove obsolete Conflicts/Replaces with libtelepathy-glib-static-dev, which
  was never in a stable release (and probably never in Debian at all)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * telepathy-example-inspect-contact - inspect a contact on a connection
 
3
 *
 
4
 * Copyright (C) 2007-2008 Collabora Ltd. <http://www.collabora.co.uk/>
 
5
 * Copyright (C) 2007-2008 Nokia Corporation
 
6
 *
 
7
 * Copying and distribution of this file, with or without modification,
 
8
 * are permitted in any medium without royalty provided the copyright
 
9
 * notice and this notice are preserved.
 
10
 */
 
11
 
 
12
#include <stdio.h>
 
13
 
 
14
#include <telepathy-glib/contact.h>
 
15
#include <telepathy-glib/dbus.h>
 
16
#include <telepathy-glib/debug.h>
 
17
 
 
18
static void
 
19
display_contact (TpContact *contact)
 
20
{
 
21
  const gchar *avatar_token;
 
22
 
 
23
  g_message ("Handle %u, \"%s\":", tp_contact_get_handle (contact),
 
24
      tp_contact_get_identifier (contact));
 
25
  g_message ("\tAlias: \"%s\"", tp_contact_get_alias (contact));
 
26
 
 
27
  avatar_token = tp_contact_get_avatar_token (contact);
 
28
 
 
29
  if (avatar_token == NULL)
 
30
    g_message ("\tAvatar token not known");
 
31
  else
 
32
    g_message ("\tAvatar token: \"%s\"", avatar_token);
 
33
 
 
34
  g_message ("\tPresence: type #%i \"%s\": \"%s\"",
 
35
      tp_contact_get_presence_type (contact),
 
36
      tp_contact_get_presence_status (contact),
 
37
      tp_contact_get_presence_message (contact));
 
38
}
 
39
 
 
40
static void
 
41
got_contacts_by_handle (TpConnection *connection,
 
42
                        guint n_contacts,
 
43
                        TpContact * const *contacts,
 
44
                        guint n_invalid,
 
45
                        const TpHandle *invalid,
 
46
                        const GError *error,
 
47
                        gpointer user_data,
 
48
                        GObject *weak_object)
 
49
{
 
50
  GMainLoop *mainloop = user_data;
 
51
 
 
52
  if (error == NULL)
 
53
    {
 
54
      guint i;
 
55
 
 
56
      for (i = 0; i < n_contacts; i++)
 
57
        {
 
58
          display_contact (contacts[i]);
 
59
        }
 
60
 
 
61
      for (i = 0; i < n_invalid; i++)
 
62
        {
 
63
          g_warning ("Invalid handle %u", invalid[i]);
 
64
        }
 
65
    }
 
66
  else
 
67
    {
 
68
      g_warning ("Error getting contacts: %s", error->message);
 
69
    }
 
70
 
 
71
  g_main_loop_quit (mainloop);
 
72
}
 
73
 
 
74
static void
 
75
got_contacts_by_id (TpConnection *connection,
 
76
                    guint n_contacts,
 
77
                    TpContact * const *contacts,
 
78
                    const gchar * const *good_ids,
 
79
                    GHashTable *bad_ids,
 
80
                    const GError *error,
 
81
                    gpointer user_data,
 
82
                    GObject *weak_object)
 
83
{
 
84
  GMainLoop *mainloop = user_data;
 
85
 
 
86
  if (error == NULL)
 
87
    {
 
88
      guint i;
 
89
      GHashTableIter hash_iter;
 
90
      gpointer key, value;
 
91
 
 
92
      for (i = 0; i < n_contacts; i++)
 
93
        {
 
94
          display_contact (contacts[i]);
 
95
        }
 
96
 
 
97
      g_hash_table_iter_init (&hash_iter, bad_ids);
 
98
 
 
99
      while (g_hash_table_iter_next (&hash_iter, &key, &value))
 
100
        {
 
101
          gchar *id = key;
 
102
          GError *e = value;
 
103
 
 
104
          g_warning ("Invalid ID \"%s\": %s", id, e->message);
 
105
        }
 
106
    }
 
107
  else
 
108
    {
 
109
      g_warning ("Error getting contacts: %s", error->message);
 
110
    }
 
111
 
 
112
  g_main_loop_quit (mainloop);
 
113
}
 
114
 
 
115
int
 
116
main (int argc,
 
117
      char **argv)
 
118
{
 
119
  const gchar *bus_name, *object_path;
 
120
  TpConnection *connection = NULL;
 
121
  GMainLoop *mainloop = NULL;
 
122
  TpDBusDaemon *daemon = NULL;
 
123
  GError *error = NULL;
 
124
  static TpContactFeature features[] = {
 
125
      TP_CONTACT_FEATURE_ALIAS,
 
126
      TP_CONTACT_FEATURE_AVATAR_TOKEN,
 
127
      TP_CONTACT_FEATURE_PRESENCE
 
128
  };
 
129
  int ret = 1;
 
130
 
 
131
  g_type_init ();
 
132
  tp_debug_set_flags (g_getenv ("EXAMPLE_DEBUG"));
 
133
 
 
134
  if (argc < 2)
 
135
    {
 
136
      fputs ("Usage:\n"
 
137
          "    telepathy-example-inspect-connection OBJECT_PATH [CONTACT_ID]\n"
 
138
          "or\n"
 
139
          "    telepathy-example-inspect-connection BUS_NAME [CONTACT_ID]\n",
 
140
          stderr);
 
141
      return 2;
 
142
    }
 
143
 
 
144
  /* Cope with the first argument being a bus name or an object path */
 
145
  if (argv[1][0] == '/')
 
146
    {
 
147
      object_path = argv[1];
 
148
      bus_name = NULL;
 
149
    }
 
150
  else
 
151
    {
 
152
      object_path = NULL;
 
153
      bus_name = argv[1];
 
154
    }
 
155
 
 
156
  daemon = tp_dbus_daemon_dup (&error);
 
157
 
 
158
  if (daemon == NULL)
 
159
    {
 
160
      g_warning ("%s", error->message);
 
161
      goto out;
 
162
    }
 
163
 
 
164
  connection = tp_connection_new (daemon, bus_name, object_path, &error);
 
165
 
 
166
  if (connection == NULL ||
 
167
      !tp_connection_run_until_ready (connection, FALSE, &error, NULL))
 
168
    {
 
169
      g_warning ("%s", error->message);
 
170
      goto out;
 
171
    }
 
172
 
 
173
  g_message ("Connection ready\n");
 
174
 
 
175
  mainloop = g_main_loop_new (NULL, FALSE);
 
176
 
 
177
  if (argv[2] == NULL)
 
178
    {
 
179
      guint self_handle;
 
180
 
 
181
      if (!tp_cli_connection_run_get_self_handle (connection, -1,
 
182
            &self_handle, &error, NULL))
 
183
        {
 
184
          g_warning ("%s", error->message);
 
185
          goto out;
 
186
        }
 
187
 
 
188
      tp_connection_get_contacts_by_handle (connection,
 
189
          1, &self_handle,
 
190
          sizeof (features) / sizeof (features[0]), features,
 
191
          got_contacts_by_handle,
 
192
          g_main_loop_ref (mainloop),
 
193
          (GDestroyNotify) g_main_loop_unref, NULL);
 
194
    }
 
195
  else
 
196
    {
 
197
      const gchar *contacts[] = { argv[2], NULL };
 
198
 
 
199
      tp_connection_get_contacts_by_id (connection,
 
200
          1, contacts,
 
201
          sizeof (features) / sizeof (features[0]), features,
 
202
          got_contacts_by_id,
 
203
          g_main_loop_ref (mainloop),
 
204
          (GDestroyNotify) g_main_loop_unref, NULL);
 
205
    }
 
206
 
 
207
  g_main_loop_run (mainloop);
 
208
  ret = 0;
 
209
 
 
210
out:
 
211
  if (error != NULL)
 
212
    g_error_free (error);
 
213
 
 
214
  if (mainloop != NULL)
 
215
    g_main_loop_unref (mainloop);
 
216
 
 
217
  if (connection != NULL)
 
218
    g_object_unref (connection);
 
219
 
 
220
  if (daemon != NULL)
 
221
    g_object_unref (daemon);
 
222
 
 
223
  return ret;
 
224
}