1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3
* Copyright (C) 2006 Eitan Isaacson <eitan@ascender.com>
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.
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.
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.
23
#include <libtelepathy/tp-conn.h>
24
#include <libtelepathy/tp-chan.h>
25
#include <libtelepathy/tp-conn-iface-presence-gen.h>
27
#include <libgossip/gossip-debug.h>
29
#include "gossip-telepathy-presence.h"
30
#include "gossip-telepathy.h"
31
#include "gossip-telepathy-private.h"
32
#include "gossip-telepathy-utils.h"
34
#define DEBUG_DOMAIN "TelepathyPresence"
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);
51
gossip_telepathy_presence_init (GossipTelepathy *telepathy)
53
DBusGProxy *presence_iface;
55
g_return_if_fail (GOSSIP_IS_TELEPATHY (telepathy));
57
presence_iface = telepathy_presence_get_iface (telepathy);
59
dbus_g_proxy_connect_signal (presence_iface,
61
G_CALLBACK (telepathy_presence_handler),
67
gossip_telepathy_presence_request (GossipTelepathy *telepathy,
70
DBusGProxy *presence_iface;
73
presence_iface = telepathy_presence_get_iface (telepathy);
74
if (!presence_iface) {
78
if (!tp_conn_iface_presence_request_presence (presence_iface,
80
gossip_debug (DEBUG_DOMAIN, "Could not request presences: %s",
82
g_clear_error (&error);
87
gossip_telepathy_presence_send (GossipTelepathy *telepathy,
88
GossipPresence *presence)
90
DBusGProxy *presence_iface;
91
GHashTable *status_ids;
92
GHashTable *status_options;
93
const gchar *status_id;
95
GossipPresenceState presence_state;
97
GValue value_message = {0, };
99
gossip_debug (DEBUG_DOMAIN, "gossip_telepathy_presence_send");
101
presence_iface = telepathy_presence_get_iface (telepathy);
102
if (!presence_iface) {
106
status_ids = g_hash_table_new_full (g_str_hash,
109
(GDestroyNotify) g_hash_table_destroy);
110
status_options = g_hash_table_new_full (g_str_hash,
113
(GDestroyNotify) g_value_unset);
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);
120
message = gossip_presence_state_get_default_status (presence_state);
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),
130
if (!tp_conn_iface_presence_set_status (presence_iface,
133
gossip_debug ("Could not set presence: %s", error->message);
134
g_clear_error (&error);
137
g_hash_table_destroy (status_ids);
141
telepathy_presence_handler (DBusGProxy *proxy,
142
GHashTable *handle_hash,
143
GossipTelepathy *telepathy)
145
gossip_debug (DEBUG_DOMAIN, "telepathy_presence_handler");
147
g_hash_table_foreach (handle_hash,
148
(GHFunc) telepathy_presence_parse_foreach,
153
telepathy_presence_parse_foreach (guint contact_handle,
154
GValueArray *presence_struct,
155
GossipTelepathy *telepathy)
157
GHashTable *presences_hash;
158
GossipContact *contact;
159
GossipPresence *presence;
160
GossipTelepathyContacts *contacts;
162
contacts = gossip_telepathy_get_contacts (telepathy);
163
contact = gossip_telepathy_contacts_get_from_handle (contacts,
170
presences_hash = g_value_get_boxed (g_value_array_get_nth (presence_struct, 1));
172
g_hash_table_foreach (presences_hash,
173
(GHFunc) telepathy_presences_foreach,
177
gossip_contact_add_presence (contact, presence);
182
presences = gossip_contact_get_presence_list (contact);
183
for (l = presences; l; l = l->next) {
190
gossip_contact_remove_presence (contact, presence);
196
telepathy_presences_foreach (const gchar *state_str,
197
GHashTable *presences_hash,
198
GossipPresence **presence)
200
GossipPresenceState state;
201
const GValue *message;
203
gossip_debug (DEBUG_DOMAIN, "presence state: %s", state_str);
205
state = telepathy_presence_state_from_str (state_str);
206
if (state == GOSSIP_PRESENCE_STATE_UNAVAILABLE) {
211
*presence = gossip_presence_new ();
212
gossip_presence_set_state (*presence, state);
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));
220
gossip_presence_set_resource (*presence, "");
224
telepathy_presence_get_iface (GossipTelepathy *telepathy)
228
tp_conn = gossip_telepathy_get_connection (telepathy);
229
return tp_conn_get_interface (tp_conn,
230
TELEPATHY_CONN_IFACE_PRESENCE_QUARK);
233
static GossipPresenceState
234
telepathy_presence_state_from_str (const gchar *str)
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;
253
return GOSSIP_PRESENCE_STATE_AVAILABLE;
257
telepathy_presence_state_to_str (GossipPresenceState presence_state)
259
switch (presence_state) {
260
case GOSSIP_PRESENCE_STATE_AVAILABLE:
262
case GOSSIP_PRESENCE_STATE_BUSY:
264
case GOSSIP_PRESENCE_STATE_AWAY:
266
case GOSSIP_PRESENCE_STATE_EXT_AWAY:
268
case GOSSIP_PRESENCE_STATE_HIDDEN:
270
case GOSSIP_PRESENCE_STATE_UNAVAILABLE: