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;
42 static void presence_finalize (GObject *object);
43 static void presence_get_property (GObject *object,
47 static void presence_set_property (GObject *object,
58 G_DEFINE_TYPE (GossipPresence, gossip_presence, G_TYPE_OBJECT);
61 gossip_presence_class_init (GossipPresenceClass *class)
63 GObjectClass *object_class;
65 object_class = G_OBJECT_CLASS (class);
67 object_class->finalize = presence_finalize;
68 object_class->get_property = presence_get_property;
69 object_class->set_property = presence_set_property;
71 g_object_class_install_property (object_class,
73 g_param_spec_int ("state",
75 "The current state of the presence",
76 GOSSIP_PRESENCE_STATE_AVAILABLE,
77 GOSSIP_PRESENCE_STATE_EXT_AWAY,
78 GOSSIP_PRESENCE_STATE_AVAILABLE,
80 g_object_class_install_property (object_class,
82 g_param_spec_string ("status",
84 "Status string set on presence",
88 g_type_class_add_private (object_class, sizeof (GossipPresencePriv));
92 gossip_presence_init (GossipPresence *presence)
94 GossipPresencePriv *priv;
96 priv = GET_PRIV (presence);
98 priv->state = GOSSIP_PRESENCE_STATE_AVAILABLE;
100 priv->timestamp = gossip_time_get_current ();
104 presence_finalize (GObject *object)
106 GossipPresencePriv *priv;
108 priv = GET_PRIV (object);
110 g_free (priv->status);
112 (G_OBJECT_CLASS (gossip_presence_parent_class)->finalize) (object);
116 presence_get_property (GObject *object,
121 GossipPresencePriv *priv;
123 priv = GET_PRIV (object);
127 g_value_set_int (value, priv->state);
130 g_value_set_string (value,
131 gossip_presence_get_status (GOSSIP_PRESENCE (object)));
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
139 presence_set_property (GObject *object,
144 GossipPresencePriv *priv;
146 priv = GET_PRIV (object);
150 priv->state = g_value_get_int (value);
153 gossip_presence_set_status (GOSSIP_PRESENCE (object),
154 g_value_get_string (value));
157 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
163 gossip_presence_new (void)
165 return g_object_new (GOSSIP_TYPE_PRESENCE, NULL);
169 gossip_presence_new_full (GossipPresenceState state,
172 return g_object_new (GOSSIP_TYPE_PRESENCE,
179 gossip_presence_get_status (GossipPresence *presence)
181 GossipPresencePriv *priv;
183 g_return_val_if_fail (GOSSIP_IS_PRESENCE (presence),
186 priv = GET_PRIV (presence);
192 gossip_presence_get_state (GossipPresence *presence)
194 GossipPresencePriv *priv;
196 g_return_val_if_fail (GOSSIP_IS_PRESENCE (presence),
197 GOSSIP_PRESENCE_STATE_AVAILABLE);
199 priv = GET_PRIV (presence);
205 gossip_presence_set_state (GossipPresence *presence,
206 GossipPresenceState state)
208 GossipPresencePriv *priv;
210 g_return_if_fail (GOSSIP_IS_PRESENCE (presence));
212 priv = GET_PRIV (presence);
216 g_object_notify (G_OBJECT (presence), "state");
220 gossip_presence_set_status (GossipPresence *presence,
223 GossipPresencePriv *priv;
225 priv = GET_PRIV (presence);
226 g_return_if_fail (GOSSIP_IS_PRESENCE (presence));
228 g_free (priv->status);
231 priv->status = g_strdup (status);
236 g_object_notify (G_OBJECT (presence), "status");
240 gossip_presence_sort_func (gconstpointer a,
243 GossipPresencePriv *priv_a;
244 GossipPresencePriv *priv_b;
247 g_return_val_if_fail (GOSSIP_IS_PRESENCE (a), 0);
248 g_return_val_if_fail (GOSSIP_IS_PRESENCE (b), 0);
250 priv_a = GET_PRIV (a);
251 priv_b = GET_PRIV (b);
254 diff = priv_a->state - priv_b->state;
256 return diff < 1 ? -1 : +1;
259 /* 3. Time (newest first) */
260 diff = priv_b->timestamp - priv_a->timestamp;
262 return diff < 1 ? -1 : +1;
265 /* No real difference */
270 gossip_presence_state_get_default_status (GossipPresenceState state)
273 case GOSSIP_PRESENCE_STATE_AVAILABLE:
274 return _("Available");
277 case GOSSIP_PRESENCE_STATE_BUSY:
281 case GOSSIP_PRESENCE_STATE_AWAY:
282 case GOSSIP_PRESENCE_STATE_EXT_AWAY:
286 case GOSSIP_PRESENCE_STATE_HIDDEN:
287 case GOSSIP_PRESENCE_STATE_UNAVAILABLE:
288 return _("Unavailable");
291 return _("Available");