~ubuntu-branches/ubuntu/feisty/gossip-telepathy/feisty

« back to all changes in this revision

Viewing changes to protocols/telepathy/gossip-telepathy-presence.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-12-20 15:09:43 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20061220150943-01hnkptgzfvw5d6e
Tags: 0.21~cvs20061220-0ubuntu1
* New CVS checkout.
* debian/control: Build-Depend on libtelepathy-dev (>= 0.0.40-0ubuntu2).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
 
/*
3
 
 * Copyright (C) 2006 Eitan Isaacson <eitan@ascender.com>
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or
6
 
 * modify it under the terms of the GNU General Public License as
7
 
 * published by the Free Software Foundation; either version 2 of the
8
 
 * License, or (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 
 * General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public
16
 
 * License along with this program; if not, write to the
17
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
 
 * Boston, MA 02111-1307, USA.
19
 
 */
20
 
 
21
 
#include "string.h"
22
 
 
23
 
#include <libtelepathy/tp-conn.h>
24
 
#include <libtelepathy/tp-chan.h>
25
 
#include <libtelepathy/tp-conn-iface-presence-gen.h>
26
 
 
27
 
#include <libgossip/gossip-debug.h>
28
 
 
29
 
#include "gossip-telepathy-presence.h"
30
 
#include "gossip-telepathy.h"
31
 
#include "gossip-telepathy-private.h"
32
 
#include "gossip-telepathy-utils.h"
33
 
 
34
 
#define DEBUG_DOMAIN "TelepathyPresence"
35
 
 
36
 
static void                telepathy_presence_handler        (DBusGProxy           *proxy,
37
 
                                                              GHashTable           *handle_hash,
38
 
                                                              GossipTelepathy      *telepathy);
39
 
static void                telepathy_presence_parse_foreach  (guint                 contact_handle,
40
 
                                                              GValueArray          *presence_struct,
41
 
                                                              GossipTelepathy      *telepathy);
42
 
static void                telepathy_presences_foreach       (const gchar          *state_str,
43
 
                                                              GHashTable           *presences_hash,
44
 
                                                              GossipPresence      **presence);
45
 
static DBusGProxy *        telepathy_presence_get_iface      (GossipTelepathy      *telepathy);
46
 
static GossipPresenceState telepathy_presence_state_from_str (const gchar          *str);
47
 
static const gchar *       telepathy_presence_state_to_str   (GossipPresenceState   presence_state);
48
 
 
49
 
 
50
 
void
51
 
gossip_telepathy_presence_init (GossipTelepathy *telepathy)
52
 
{
53
 
        DBusGProxy *presence_iface;
54
 
 
55
 
        g_return_if_fail (GOSSIP_IS_TELEPATHY (telepathy));
56
 
 
57
 
        presence_iface = telepathy_presence_get_iface (telepathy);
58
 
        if (presence_iface) {
59
 
                dbus_g_proxy_connect_signal (presence_iface,
60
 
                                             "PresenceUpdate",
61
 
                                             G_CALLBACK (telepathy_presence_handler),
62
 
                                             telepathy, NULL);
63
 
        }
64
 
}
65
 
 
66
 
void
67
 
gossip_telepathy_presence_request (GossipTelepathy *telepathy,
68
 
                                   GArray          *handles)
69
 
{
70
 
        DBusGProxy *presence_iface;
71
 
        GError     *error = NULL;
72
 
 
73
 
        presence_iface = telepathy_presence_get_iface (telepathy);
74
 
        if (!presence_iface) {
75
 
                return;
76
 
        }
77
 
 
78
 
        if (!tp_conn_iface_presence_request_presence (presence_iface,
79
 
                                                      handles, &error)) {
80
 
                gossip_debug (DEBUG_DOMAIN, "Could not request presences: %s",
81
 
                              error->message);
82
 
                g_clear_error (&error);
83
 
        }
84
 
}
85
 
 
86
 
void
87
 
gossip_telepathy_presence_send (GossipTelepathy *telepathy,
88
 
                                GossipPresence  *presence)
89
 
{
90
 
        DBusGProxy          *presence_iface;
91
 
        GHashTable          *status_ids;
92
 
        GHashTable          *status_options;
93
 
        const gchar         *status_id;
94
 
        const gchar         *message;
95
 
        GossipPresenceState  presence_state;
96
 
        GError              *error = NULL;
97
 
        GValue               value_message = {0, };
98
 
 
99
 
        gossip_debug (DEBUG_DOMAIN, "gossip_telepathy_presence_send");
100
 
 
101
 
        presence_iface = telepathy_presence_get_iface (telepathy);
102
 
        if (!presence_iface) {
103
 
                return;
104
 
        }
105
 
 
106
 
        status_ids = g_hash_table_new_full (g_str_hash,
107
 
                                            g_str_equal,
108
 
                                            g_free, 
109
 
                                            (GDestroyNotify) g_hash_table_destroy);
110
 
        status_options = g_hash_table_new_full (g_str_hash,
111
 
                                                g_str_equal,
112
 
                                                NULL, 
113
 
                                                (GDestroyNotify) g_value_unset);
114
 
 
115
 
        presence_state = gossip_presence_get_state (presence);
116
 
        status_id = telepathy_presence_state_to_str (presence_state);
117
 
        message = gossip_presence_get_status (presence);
118
 
 
119
 
        if (!message) {
120
 
                message = gossip_presence_state_get_default_status (presence_state);
121
 
        }
122
 
 
123
 
        g_value_init (&value_message, G_TYPE_STRING);
124
 
        g_value_set_string (&value_message, message);
125
 
        g_hash_table_insert (status_options, "message", &value_message);
126
 
        g_hash_table_insert (status_ids,
127
 
                             g_strdup (status_id),
128
 
                             status_options);
129
 
 
130
 
        if (!tp_conn_iface_presence_set_status (presence_iface,
131
 
                                                status_ids,
132
 
                                                &error)) {
133
 
                gossip_debug ("Could not set presence: %s", error->message);
134
 
                g_clear_error (&error);
135
 
        }
136
 
 
137
 
        g_hash_table_destroy (status_ids);
138
 
}
139
 
 
140
 
static void
141
 
telepathy_presence_handler (DBusGProxy      *proxy,
142
 
                            GHashTable      *handle_hash,
143
 
                            GossipTelepathy *telepathy)
144
 
{
145
 
        gossip_debug (DEBUG_DOMAIN, "telepathy_presence_handler");
146
 
 
147
 
        g_hash_table_foreach (handle_hash,
148
 
                              (GHFunc) telepathy_presence_parse_foreach,
149
 
                              telepathy);
150
 
}
151
 
 
152
 
static void
153
 
telepathy_presence_parse_foreach (guint            contact_handle,
154
 
                                  GValueArray     *presence_struct,
155
 
                                  GossipTelepathy *telepathy)
156
 
{
157
 
        GHashTable              *presences_hash;
158
 
        GossipContact           *contact;
159
 
        GossipPresence          *presence;
160
 
        GossipTelepathyContacts *contacts;
161
 
 
162
 
        contacts = gossip_telepathy_get_contacts (telepathy);
163
 
        contact = gossip_telepathy_contacts_get_from_handle (contacts,
164
 
                                                             contact_handle,
165
 
                                                             NULL, NULL);
166
 
        if (!contact) {
167
 
                return;
168
 
        }
169
 
 
170
 
        presences_hash = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
171
 
 
172
 
        g_hash_table_foreach (presences_hash,
173
 
                              (GHFunc) telepathy_presences_foreach,
174
 
                              &presence);
175
 
 
176
 
        if (presence) {
177
 
                gossip_contact_add_presence (contact, presence);
178
 
        } else {
179
 
                GList *presences;
180
 
                GList *l;
181
 
                
182
 
                presences = gossip_contact_get_presence_list (contact);
183
 
                for (l = presences; l; l = l->next) {
184
 
                        presence = l->data;
185
 
 
186
 
                        if (!presence) {
187
 
                                continue;
188
 
                        }
189
 
 
190
 
                        gossip_contact_remove_presence (contact, presence);
191
 
                }
192
 
        }
193
 
}
194
 
 
195
 
static void
196
 
telepathy_presences_foreach (const gchar     *state_str,
197
 
                             GHashTable      *presences_hash,
198
 
                             GossipPresence **presence)
199
 
{
200
 
        GossipPresenceState  state;
201
 
        const GValue        *message;
202
 
 
203
 
        gossip_debug (DEBUG_DOMAIN, "presence state: %s", state_str);
204
 
 
205
 
        state = telepathy_presence_state_from_str (state_str);
206
 
        if (state == GOSSIP_PRESENCE_STATE_UNAVAILABLE) {
207
 
                *presence = NULL;
208
 
                return;
209
 
        }
210
 
 
211
 
        *presence = gossip_presence_new ();
212
 
        gossip_presence_set_state (*presence, state);
213
 
 
214
 
        message = g_hash_table_lookup (presences_hash, "message");
215
 
        if (message != NULL) {
216
 
                gossip_presence_set_status (*presence, 
217
 
                                            g_value_get_string (message));
218
 
        }
219
 
 
220
 
        gossip_presence_set_resource (*presence, "");
221
 
}
222
 
 
223
 
static DBusGProxy *
224
 
telepathy_presence_get_iface (GossipTelepathy *telepathy)
225
 
{
226
 
        TpConn     *tp_conn;
227
 
 
228
 
        tp_conn = gossip_telepathy_get_connection (telepathy);
229
 
        return tp_conn_get_interface (tp_conn,
230
 
                                      TELEPATHY_CONN_IFACE_PRESENCE_QUARK);
231
 
}
232
 
 
233
 
static GossipPresenceState
234
 
telepathy_presence_state_from_str (const gchar *str)
235
 
{
236
 
        if (strcmp (str, "available") == 0) {
237
 
                return GOSSIP_PRESENCE_STATE_AVAILABLE;
238
 
        } else if ((strcmp (str, "dnd") == 0) || (strcmp (str, "busy") == 0)) {
239
 
                return GOSSIP_PRESENCE_STATE_BUSY;
240
 
        } else if ((strcmp (str, "away") == 0) || (strcmp (str, "brb") == 0)) {
241
 
                return GOSSIP_PRESENCE_STATE_AWAY;
242
 
        } else if (strcmp (str, "xa") == 0) {
243
 
                return GOSSIP_PRESENCE_STATE_EXT_AWAY;
244
 
        } else if (strcmp (str, "hidden") == 0) {
245
 
                return GOSSIP_PRESENCE_STATE_HIDDEN;
246
 
        } else if (strcmp (str, "offline") == 0) {
247
 
                return GOSSIP_PRESENCE_STATE_UNAVAILABLE;
248
 
        } else if (strcmp (str, "chat") == 0) {
249
 
                /* We don't support chat, so treat it like available. */
250
 
                return GOSSIP_PRESENCE_STATE_AVAILABLE;
251
 
        }
252
 
 
253
 
        return GOSSIP_PRESENCE_STATE_AVAILABLE;
254
 
}
255
 
 
256
 
static const gchar *
257
 
telepathy_presence_state_to_str (GossipPresenceState presence_state)
258
 
{
259
 
        switch (presence_state) {
260
 
        case GOSSIP_PRESENCE_STATE_AVAILABLE:
261
 
                return "available";
262
 
        case GOSSIP_PRESENCE_STATE_BUSY:
263
 
                return "dnd";
264
 
        case GOSSIP_PRESENCE_STATE_AWAY:
265
 
                return "away";
266
 
        case GOSSIP_PRESENCE_STATE_EXT_AWAY:
267
 
                return "xa";
268
 
        case GOSSIP_PRESENCE_STATE_HIDDEN:
269
 
                return "hidden";
270
 
        case GOSSIP_PRESENCE_STATE_UNAVAILABLE:
271
 
                return "offline";
272
 
        default:
273
 
                return NULL;
274
 
        }
275
 
 
276
 
        return NULL;
277
 
}
278