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 "empathy-presence.h"
30 #include "empathy-time.h"
32 /* FIXME mission-control does not install libmissioncontrol/mc-enum-types.h so
33 * we have to define MC_TYPE_PRESENCE here. See sf.net bug #1768235,
34 * https://sf.net/tracker/?func=detail&atid=932444&aid=1768235&group_id=190214 */
35 #ifndef MC_TYPE_PRESENCE
36 GType mc_presence_get_type (void) G_GNUC_CONST;
37 #define MC_TYPE_PRESENCE (mc_presence_get_type())
40 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_PRESENCE, EmpathyPresencePriv))
42 typedef struct _EmpathyPresencePriv EmpathyPresencePriv;
44 struct _EmpathyPresencePriv {
47 EmpathyTime timestamp;
50 static void presence_finalize (GObject *object);
51 static void presence_get_property (GObject *object,
55 static void presence_set_property (GObject *object,
66 G_DEFINE_TYPE (EmpathyPresence, empathy_presence, G_TYPE_OBJECT);
69 empathy_presence_class_init (EmpathyPresenceClass *class)
71 GObjectClass *object_class;
73 object_class = G_OBJECT_CLASS (class);
75 object_class->finalize = presence_finalize;
76 object_class->get_property = presence_get_property;
77 object_class->set_property = presence_set_property;
79 g_object_class_install_property (object_class,
81 g_param_spec_enum ("state",
83 "The current state of the presence",
85 MC_PRESENCE_AVAILABLE,
87 g_object_class_install_property (object_class,
89 g_param_spec_string ("status",
91 "Status string set on presence",
95 g_type_class_add_private (object_class, sizeof (EmpathyPresencePriv));
99 empathy_presence_init (EmpathyPresence *presence)
101 EmpathyPresencePriv *priv;
103 priv = GET_PRIV (presence);
105 priv->state = MC_PRESENCE_AVAILABLE;
107 priv->timestamp = empathy_time_get_current ();
111 presence_finalize (GObject *object)
113 EmpathyPresencePriv *priv;
115 priv = GET_PRIV (object);
117 g_free (priv->status);
119 (G_OBJECT_CLASS (empathy_presence_parent_class)->finalize) (object);
123 presence_get_property (GObject *object,
128 EmpathyPresencePriv *priv;
130 priv = GET_PRIV (object);
134 g_value_set_enum (value, priv->state);
137 g_value_set_string (value,
138 empathy_presence_get_status (EMPATHY_PRESENCE (object)));
141 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
146 presence_set_property (GObject *object,
151 EmpathyPresencePriv *priv;
153 priv = GET_PRIV (object);
157 priv->state = g_value_get_enum (value);
160 empathy_presence_set_status (EMPATHY_PRESENCE (object),
161 g_value_get_string (value));
164 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
170 empathy_presence_new (void)
172 return g_object_new (EMPATHY_TYPE_PRESENCE, NULL);
176 empathy_presence_new_full (McPresence state,
179 return g_object_new (EMPATHY_TYPE_PRESENCE,
186 empathy_presence_get_status (EmpathyPresence *presence)
188 EmpathyPresencePriv *priv;
190 g_return_val_if_fail (EMPATHY_IS_PRESENCE (presence),
193 priv = GET_PRIV (presence);
199 empathy_presence_get_state (EmpathyPresence *presence)
201 EmpathyPresencePriv *priv;
203 g_return_val_if_fail (EMPATHY_IS_PRESENCE (presence),
204 MC_PRESENCE_AVAILABLE);
206 priv = GET_PRIV (presence);
212 empathy_presence_set_state (EmpathyPresence *presence,
215 EmpathyPresencePriv *priv;
217 g_return_if_fail (EMPATHY_IS_PRESENCE (presence));
219 priv = GET_PRIV (presence);
223 g_object_notify (G_OBJECT (presence), "state");
227 empathy_presence_set_status (EmpathyPresence *presence,
230 EmpathyPresencePriv *priv;
232 priv = GET_PRIV (presence);
233 g_return_if_fail (EMPATHY_IS_PRESENCE (presence));
235 g_free (priv->status);
238 priv->status = g_strdup (status);
243 g_object_notify (G_OBJECT (presence), "status");
247 empathy_presence_sort_func (gconstpointer a,
250 EmpathyPresencePriv *priv_a;
251 EmpathyPresencePriv *priv_b;
254 g_return_val_if_fail (EMPATHY_IS_PRESENCE (a), 0);
255 g_return_val_if_fail (EMPATHY_IS_PRESENCE (b), 0);
257 priv_a = GET_PRIV (a);
258 priv_b = GET_PRIV (b);
261 diff = priv_a->state - priv_b->state;
263 return diff < 1 ? -1 : +1;
266 /* 3. Time (newest first) */
267 diff = priv_b->timestamp - priv_a->timestamp;
269 return diff < 1 ? -1 : +1;
272 /* No real difference */
277 empathy_presence_state_get_default_status (McPresence state)
280 case MC_PRESENCE_AVAILABLE:
281 return _("Available");
282 case MC_PRESENCE_DO_NOT_DISTURB:
284 case MC_PRESENCE_AWAY:
285 case MC_PRESENCE_EXTENDED_AWAY:
287 case MC_PRESENCE_HIDDEN:
288 case MC_PRESENCE_OFFLINE:
289 case MC_PRESENCE_UNSET:
292 g_assert_not_reached ();
299 empathy_presence_state_to_str (McPresence state)
302 case MC_PRESENCE_AVAILABLE:
304 case MC_PRESENCE_DO_NOT_DISTURB:
306 case MC_PRESENCE_AWAY:
308 case MC_PRESENCE_EXTENDED_AWAY:
310 case MC_PRESENCE_HIDDEN:
312 case MC_PRESENCE_OFFLINE:
314 case MC_PRESENCE_UNSET:
317 g_assert_not_reached ();
324 empathy_presence_state_from_str (const gchar *str)
326 if (strcmp (str, "available") == 0) {
327 return MC_PRESENCE_AVAILABLE;
328 } else if ((strcmp (str, "dnd") == 0) || (strcmp (str, "busy") == 0)) {
329 return MC_PRESENCE_DO_NOT_DISTURB;
330 } else if ((strcmp (str, "away") == 0) || (strcmp (str, "brb") == 0)) {
331 return MC_PRESENCE_AWAY;
332 } else if ((strcmp (str, "xa") == 0) || (strcmp (str, "ext_away") == 0)) {
333 return MC_PRESENCE_EXTENDED_AWAY;
334 } else if (strcmp (str, "hidden") == 0) {
335 return MC_PRESENCE_HIDDEN;
336 } else if (strcmp (str, "offline") == 0) {
337 return MC_PRESENCE_OFFLINE;
338 } else if (strcmp (str, "unset") == 0) {
339 return MC_PRESENCE_UNSET;
342 return MC_PRESENCE_AVAILABLE;