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 <libmissioncontrol/mc-enum-types.h>
31 #include "empathy-presence.h"
32 #include "empathy-time.h"
34 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_PRESENCE, EmpathyPresencePriv))
36 typedef struct _EmpathyPresencePriv EmpathyPresencePriv;
38 struct _EmpathyPresencePriv {
41 EmpathyTime timestamp;
44 static void presence_finalize (GObject *object);
45 static void presence_get_property (GObject *object,
49 static void presence_set_property (GObject *object,
60 G_DEFINE_TYPE (EmpathyPresence, empathy_presence, G_TYPE_OBJECT);
63 empathy_presence_class_init (EmpathyPresenceClass *class)
65 GObjectClass *object_class;
67 object_class = G_OBJECT_CLASS (class);
69 object_class->finalize = presence_finalize;
70 object_class->get_property = presence_get_property;
71 object_class->set_property = presence_set_property;
73 g_object_class_install_property (object_class,
75 g_param_spec_enum ("state",
77 "The current state of the presence",
79 MC_PRESENCE_AVAILABLE,
81 g_object_class_install_property (object_class,
83 g_param_spec_string ("status",
85 "Status string set on presence",
89 g_type_class_add_private (object_class, sizeof (EmpathyPresencePriv));
93 empathy_presence_init (EmpathyPresence *presence)
95 EmpathyPresencePriv *priv;
97 priv = GET_PRIV (presence);
99 priv->state = MC_PRESENCE_AVAILABLE;
101 priv->timestamp = empathy_time_get_current ();
105 presence_finalize (GObject *object)
107 EmpathyPresencePriv *priv;
109 priv = GET_PRIV (object);
111 g_free (priv->status);
113 (G_OBJECT_CLASS (empathy_presence_parent_class)->finalize) (object);
117 presence_get_property (GObject *object,
122 EmpathyPresencePriv *priv;
124 priv = GET_PRIV (object);
128 g_value_set_enum (value, priv->state);
131 g_value_set_string (value,
132 empathy_presence_get_status (EMPATHY_PRESENCE (object)));
135 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
140 presence_set_property (GObject *object,
145 EmpathyPresencePriv *priv;
147 priv = GET_PRIV (object);
151 priv->state = g_value_get_enum (value);
154 empathy_presence_set_status (EMPATHY_PRESENCE (object),
155 g_value_get_string (value));
158 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
164 empathy_presence_new (void)
166 return g_object_new (EMPATHY_TYPE_PRESENCE, NULL);
170 empathy_presence_new_full (McPresence state,
173 return g_object_new (EMPATHY_TYPE_PRESENCE,
180 empathy_presence_get_status (EmpathyPresence *presence)
182 EmpathyPresencePriv *priv;
184 g_return_val_if_fail (EMPATHY_IS_PRESENCE (presence),
187 priv = GET_PRIV (presence);
193 empathy_presence_get_state (EmpathyPresence *presence)
195 EmpathyPresencePriv *priv;
197 g_return_val_if_fail (EMPATHY_IS_PRESENCE (presence),
198 MC_PRESENCE_AVAILABLE);
200 priv = GET_PRIV (presence);
206 empathy_presence_set_state (EmpathyPresence *presence,
209 EmpathyPresencePriv *priv;
211 g_return_if_fail (EMPATHY_IS_PRESENCE (presence));
213 priv = GET_PRIV (presence);
217 g_object_notify (G_OBJECT (presence), "state");
221 empathy_presence_set_status (EmpathyPresence *presence,
224 EmpathyPresencePriv *priv;
226 priv = GET_PRIV (presence);
227 g_return_if_fail (EMPATHY_IS_PRESENCE (presence));
229 g_free (priv->status);
232 priv->status = g_strdup (status);
237 g_object_notify (G_OBJECT (presence), "status");
241 empathy_presence_sort_func (gconstpointer a,
244 EmpathyPresencePriv *priv_a;
245 EmpathyPresencePriv *priv_b;
248 g_return_val_if_fail (EMPATHY_IS_PRESENCE (a), 0);
249 g_return_val_if_fail (EMPATHY_IS_PRESENCE (b), 0);
251 priv_a = GET_PRIV (a);
252 priv_b = GET_PRIV (b);
255 diff = priv_a->state - priv_b->state;
257 return diff < 1 ? -1 : +1;
260 /* 3. Time (newest first) */
261 diff = priv_b->timestamp - priv_a->timestamp;
263 return diff < 1 ? -1 : +1;
266 /* No real difference */
271 empathy_presence_state_get_default_status (McPresence state)
274 case MC_PRESENCE_AVAILABLE:
275 return _("Available");
276 case MC_PRESENCE_DO_NOT_DISTURB:
278 case MC_PRESENCE_AWAY:
279 case MC_PRESENCE_EXTENDED_AWAY:
281 case MC_PRESENCE_HIDDEN:
282 case MC_PRESENCE_OFFLINE:
283 case MC_PRESENCE_UNSET:
286 g_assert_not_reached ();
293 empathy_presence_state_to_str (McPresence state)
296 case MC_PRESENCE_AVAILABLE:
298 case MC_PRESENCE_DO_NOT_DISTURB:
300 case MC_PRESENCE_AWAY:
302 case MC_PRESENCE_EXTENDED_AWAY:
304 case MC_PRESENCE_HIDDEN:
306 case MC_PRESENCE_OFFLINE:
308 case MC_PRESENCE_UNSET:
311 g_assert_not_reached ();
318 empathy_presence_state_from_str (const gchar *str)
320 if (strcmp (str, "available") == 0) {
321 return MC_PRESENCE_AVAILABLE;
322 } else if ((strcmp (str, "dnd") == 0) || (strcmp (str, "busy") == 0)) {
323 return MC_PRESENCE_DO_NOT_DISTURB;
324 } else if ((strcmp (str, "away") == 0) || (strcmp (str, "brb") == 0)) {
325 return MC_PRESENCE_AWAY;
326 } else if ((strcmp (str, "xa") == 0) || (strcmp (str, "ext_away") == 0)) {
327 return MC_PRESENCE_EXTENDED_AWAY;
328 } else if (strcmp (str, "hidden") == 0) {
329 return MC_PRESENCE_HIDDEN;
330 } else if (strcmp (str, "offline") == 0) {
331 return MC_PRESENCE_OFFLINE;
332 } else if (strcmp (str, "unset") == 0) {
333 return MC_PRESENCE_UNSET;
336 return MC_PRESENCE_AVAILABLE;