1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2004-2007 Imendio AB
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.
20 * Authors: Mikael Hallendal <micke@imendio.com>
27 #include <glib/gi18n.h>
29 #include "gossip-presence.h"
30 #include "gossip-time.h"
32 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_PRESENCE, GossipPresencePriv))
34 typedef struct _GossipPresencePriv GossipPresencePriv;
36 struct _GossipPresencePriv {
37 GossipPresenceState state;
46 static void presence_finalize (GObject *object);
47 static void presence_get_property (GObject *object,
51 static void presence_set_property (GObject *object,
64 G_DEFINE_TYPE (GossipPresence, gossip_presence, G_TYPE_OBJECT);
67 gossip_presence_class_init (GossipPresenceClass *class)
69 GObjectClass *object_class;
71 object_class = G_OBJECT_CLASS (class);
73 object_class->finalize = presence_finalize;
74 object_class->get_property = presence_get_property;
75 object_class->set_property = presence_set_property;
77 g_object_class_install_property (object_class,
79 g_param_spec_int ("state",
81 "The current state of the presence",
82 GOSSIP_PRESENCE_STATE_AVAILABLE,
83 GOSSIP_PRESENCE_STATE_EXT_AWAY,
84 GOSSIP_PRESENCE_STATE_AVAILABLE,
86 g_object_class_install_property (object_class,
88 g_param_spec_string ("status",
90 "Status string set on presence",
93 g_object_class_install_property (object_class,
95 g_param_spec_string ("resource",
97 "Resource that this presence is for",
100 g_object_class_install_property (object_class,
102 g_param_spec_int ("priority",
104 "Priority value of presence",
110 g_type_class_add_private (object_class, sizeof (GossipPresencePriv));
114 gossip_presence_init (GossipPresence *presence)
116 GossipPresencePriv *priv;
118 priv = GET_PRIV (presence);
120 priv->state = GOSSIP_PRESENCE_STATE_AVAILABLE;
123 priv->resource = NULL;
127 priv->timestamp = gossip_time_get_current ();
131 presence_finalize (GObject *object)
133 GossipPresencePriv *priv;
135 priv = GET_PRIV (object);
137 g_free (priv->status);
138 g_free (priv->resource);
140 (G_OBJECT_CLASS (gossip_presence_parent_class)->finalize) (object);
144 presence_get_property (GObject *object,
149 GossipPresencePriv *priv;
151 priv = GET_PRIV (object);
155 g_value_set_int (value, priv->state);
158 g_value_set_string (value,
159 gossip_presence_get_status (GOSSIP_PRESENCE (object)));
162 g_value_set_string (value,
163 gossip_presence_get_resource (GOSSIP_PRESENCE (object)));
166 g_value_set_int (value, priv->priority);
169 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
174 presence_set_property (GObject *object,
179 GossipPresencePriv *priv;
181 priv = GET_PRIV (object);
185 priv->state = g_value_get_int (value);
188 gossip_presence_set_status (GOSSIP_PRESENCE (object),
189 g_value_get_string (value));
192 gossip_presence_set_resource (GOSSIP_PRESENCE (object),
193 g_value_get_string (value));
196 priv->priority = g_value_get_int (value);
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
205 gossip_presence_new (void)
207 return g_object_new (GOSSIP_TYPE_PRESENCE, NULL);
211 gossip_presence_new_full (GossipPresenceState state,
214 return g_object_new (GOSSIP_TYPE_PRESENCE,
221 gossip_presence_get_resource (GossipPresence *presence)
223 GossipPresencePriv *priv;
225 g_return_val_if_fail (GOSSIP_IS_PRESENCE (presence), NULL);
227 priv = GET_PRIV (presence);
229 if (priv->resource) {
230 return priv->resource;
237 gossip_presence_get_status (GossipPresence *presence)
239 GossipPresencePriv *priv;
241 g_return_val_if_fail (GOSSIP_IS_PRESENCE (presence),
244 priv = GET_PRIV (presence);
250 gossip_presence_get_priority (GossipPresence *presence)
252 GossipPresencePriv *priv;
254 priv = GET_PRIV (presence);
255 g_return_val_if_fail (GOSSIP_IS_PRESENCE (presence), 0);
257 return priv->priority;
261 gossip_presence_set_resource (GossipPresence *presence,
262 const gchar *resource)
264 GossipPresencePriv *priv;
266 g_return_if_fail (GOSSIP_IS_PRESENCE (presence));
267 g_return_if_fail (resource != NULL);
269 priv = GET_PRIV (presence);
271 g_free (priv->resource);
272 priv->resource = g_strdup (resource);
274 g_object_notify (G_OBJECT (presence), "resource");
278 gossip_presence_get_state (GossipPresence *presence)
280 GossipPresencePriv *priv;
282 g_return_val_if_fail (GOSSIP_IS_PRESENCE (presence),
283 GOSSIP_PRESENCE_STATE_AVAILABLE);
285 priv = GET_PRIV (presence);
291 gossip_presence_set_state (GossipPresence *presence,
292 GossipPresenceState state)
294 GossipPresencePriv *priv;
296 g_return_if_fail (GOSSIP_IS_PRESENCE (presence));
298 priv = GET_PRIV (presence);
302 g_object_notify (G_OBJECT (presence), "state");
306 gossip_presence_set_status (GossipPresence *presence,
309 GossipPresencePriv *priv;
311 priv = GET_PRIV (presence);
312 g_return_if_fail (GOSSIP_IS_PRESENCE (presence));
314 g_free (priv->status);
317 priv->status = g_strdup (status);
322 g_object_notify (G_OBJECT (presence), "status");
326 gossip_presence_set_priority (GossipPresence *presence,
329 GossipPresencePriv *priv;
331 g_return_if_fail (GOSSIP_IS_PRESENCE (presence));
333 priv = GET_PRIV (presence);
335 priv->priority = priority;
337 g_object_notify (G_OBJECT (presence), "priority");
341 gossip_presence_resource_equal (gconstpointer a,
344 GossipPresencePriv *priv1;
345 GossipPresencePriv *priv2;
347 g_return_val_if_fail (GOSSIP_IS_PRESENCE (a), FALSE);
348 g_return_val_if_fail (GOSSIP_IS_PRESENCE (b), FALSE);
350 priv1 = GET_PRIV (a);
351 priv2 = GET_PRIV (b);
353 if (!priv1->resource) {
354 if (!priv2->resource) {
361 if (!priv2->resource) {
365 if (strcmp (priv1->resource, priv2->resource) == 0) {
373 gossip_presence_sort_func (gconstpointer a,
376 GossipPresencePriv *priv_a;
377 GossipPresencePriv *priv_b;
380 g_return_val_if_fail (GOSSIP_IS_PRESENCE (a), 0);
381 g_return_val_if_fail (GOSSIP_IS_PRESENCE (b), 0);
383 /* We sort here by priority AND status, in theory, the
384 * priority would be enough for JUST Jabber contacts which
385 * actually abide to the protocol, but for other protocols and
386 * dodgy clients, we will sort by:
390 * 3. Time it was set (most recent first).
393 priv_a = GET_PRIV (a);
394 priv_b = GET_PRIV (b);
397 diff = priv_a->state - priv_b->state;
399 return diff < 1 ? -1 : +1;
403 diff = priv_a->priority - priv_b->priority;
405 return diff < 1 ? -1 : +1;
408 /* 3. Time (newest first) */
409 diff = priv_b->timestamp - priv_a->timestamp;
411 return diff < 1 ? -1 : +1;
414 /* No real difference, except maybe resource */
419 gossip_presence_state_get_default_status (GossipPresenceState state)
422 case GOSSIP_PRESENCE_STATE_AVAILABLE:
423 return _("Available");
426 case GOSSIP_PRESENCE_STATE_BUSY:
430 case GOSSIP_PRESENCE_STATE_AWAY:
431 case GOSSIP_PRESENCE_STATE_EXT_AWAY:
435 case GOSSIP_PRESENCE_STATE_HIDDEN:
436 case GOSSIP_PRESENCE_STATE_UNAVAILABLE:
437 return _("Unavailable");
440 return _("Available");