]> git.0d.be Git - empathy.git/blob - libempathy/empathy-presence.c
b7beb3900c14c9eba37de953c4ed2847ccfaab91
[empathy.git] / libempathy / empathy-presence.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2004-2007 Imendio AB
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Authors: Mikael Hallendal <micke@imendio.com>
21  */
22
23 #include "config.h"
24
25 #include <string.h>
26
27 #include <glib/gi18n.h>
28
29 #include <libmissioncontrol/mc-enum-types.h>
30
31 #include "empathy-presence.h"
32 #include "empathy-time.h"
33
34 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_PRESENCE, EmpathyPresencePriv))
35
36 typedef struct _EmpathyPresencePriv EmpathyPresencePriv;
37
38 struct _EmpathyPresencePriv {
39         McPresence  state;
40         gchar      *status;
41         time_t      timestamp;
42 };
43
44 static void         presence_finalize     (GObject      *object);
45 static void         presence_get_property (GObject      *object,
46                                            guint         param_id,
47                                            GValue       *value,
48                                            GParamSpec   *pspec);
49 static void         presence_set_property (GObject      *object,
50                                            guint         param_id,
51                                            const GValue *value,
52                                            GParamSpec   *pspec);
53
54 enum {
55         PROP_0,
56         PROP_STATE,
57         PROP_STATUS
58 };
59
60 G_DEFINE_TYPE (EmpathyPresence, empathy_presence, G_TYPE_OBJECT);
61
62 static void
63 empathy_presence_class_init (EmpathyPresenceClass *class)
64 {
65         GObjectClass *object_class;
66
67         object_class = G_OBJECT_CLASS (class);
68
69         object_class->finalize     = presence_finalize;
70         object_class->get_property = presence_get_property;
71         object_class->set_property = presence_set_property;
72
73         g_object_class_install_property (object_class,
74                                          PROP_STATE,
75                                          g_param_spec_enum ("state",
76                                                             "Presence State",
77                                                             "The current state of the presence",
78                                                             MC_TYPE_PRESENCE,
79                                                             MC_PRESENCE_AVAILABLE,
80                                                             G_PARAM_READWRITE));
81         g_object_class_install_property (object_class,
82                                          PROP_STATUS,
83                                          g_param_spec_string ("status",
84                                                               "Presence Status",
85                                                               "Status string set on presence",
86                                                               NULL,
87                                                               G_PARAM_READWRITE));
88
89         g_type_class_add_private (object_class, sizeof (EmpathyPresencePriv));
90 }
91
92 static void
93 empathy_presence_init (EmpathyPresence *presence)
94 {
95         EmpathyPresencePriv *priv;
96
97         priv = GET_PRIV (presence);
98
99         priv->state = MC_PRESENCE_AVAILABLE;
100         priv->status = NULL;
101         priv->timestamp = empathy_time_get_current ();
102 }
103
104 static void
105 presence_finalize (GObject *object)
106 {
107         EmpathyPresencePriv *priv;
108
109         priv = GET_PRIV (object);
110
111         g_free (priv->status);
112
113         (G_OBJECT_CLASS (empathy_presence_parent_class)->finalize) (object);
114 }
115
116 static void
117 presence_get_property (GObject    *object,
118                        guint       param_id,
119                        GValue     *value,
120                        GParamSpec *pspec)
121 {
122         EmpathyPresencePriv *priv;
123
124         priv = GET_PRIV (object);
125
126         switch (param_id) {
127         case PROP_STATE:
128                 g_value_set_enum (value, priv->state);
129                 break;
130         case PROP_STATUS:
131                 g_value_set_string (value,
132                                     empathy_presence_get_status (EMPATHY_PRESENCE (object)));
133                 break;
134         default:
135                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
136                 break;
137         }
138 }
139 static void
140 presence_set_property (GObject      *object,
141                        guint         param_id,
142                        const GValue *value,
143                        GParamSpec   *pspec)
144 {
145         EmpathyPresencePriv *priv;
146
147         priv = GET_PRIV (object);
148
149         switch (param_id) {
150         case PROP_STATE:
151                 priv->state = g_value_get_enum (value);
152                 break;
153         case PROP_STATUS:
154                 empathy_presence_set_status (EMPATHY_PRESENCE (object),
155                                             g_value_get_string (value));
156                 break;
157         default:
158                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
159                 break;
160         }
161 }
162
163 EmpathyPresence *
164 empathy_presence_new (void)
165 {
166         return g_object_new (EMPATHY_TYPE_PRESENCE, NULL);
167 }
168
169 EmpathyPresence *
170 empathy_presence_new_full (McPresence   state,
171                           const gchar *status)
172 {
173         return g_object_new (EMPATHY_TYPE_PRESENCE,
174                              "state", state,
175                              "status", status,
176                              NULL);
177 }
178
179 const gchar *
180 empathy_presence_get_status (EmpathyPresence *presence)
181 {
182         EmpathyPresencePriv *priv;
183
184         g_return_val_if_fail (EMPATHY_IS_PRESENCE (presence),
185                               _("Offline"));
186
187         priv = GET_PRIV (presence);
188
189         return priv->status;
190 }
191
192 McPresence
193 empathy_presence_get_state (EmpathyPresence *presence)
194 {
195         EmpathyPresencePriv *priv;
196
197         g_return_val_if_fail (EMPATHY_IS_PRESENCE (presence),
198                               MC_PRESENCE_AVAILABLE);
199
200         priv = GET_PRIV (presence);
201
202         return priv->state;
203 }
204
205 void
206 empathy_presence_set_state (EmpathyPresence *presence,
207                            McPresence      state)
208 {
209         EmpathyPresencePriv *priv;
210
211         g_return_if_fail (EMPATHY_IS_PRESENCE (presence));
212
213         priv = GET_PRIV (presence);
214
215         priv->state = state;
216
217         g_object_notify (G_OBJECT (presence), "state");
218 }
219
220 void
221 empathy_presence_set_status (EmpathyPresence *presence,
222                             const gchar    *status)
223 {
224         EmpathyPresencePriv *priv;
225
226         priv = GET_PRIV (presence);
227         g_return_if_fail (EMPATHY_IS_PRESENCE (presence));
228
229         g_free (priv->status);
230
231         if (status) {
232                 priv->status = g_strdup (status);
233         } else {
234                 priv->status = NULL;
235         }
236
237         g_object_notify (G_OBJECT (presence), "status");
238 }
239
240 gint
241 empathy_presence_sort_func (gconstpointer a,
242                            gconstpointer b)
243 {
244         EmpathyPresencePriv *priv_a;
245         EmpathyPresencePriv *priv_b;
246         gint                diff;
247
248         g_return_val_if_fail (EMPATHY_IS_PRESENCE (a), 0);
249         g_return_val_if_fail (EMPATHY_IS_PRESENCE (b), 0);
250          
251         priv_a = GET_PRIV (a);
252         priv_b = GET_PRIV (b);
253
254         /* 1. State */
255         diff = priv_a->state - priv_b->state;
256         if (diff != 0) {
257                 return diff < 1 ? -1 : +1;
258         }
259
260         /* 3. Time (newest first) */
261         diff = priv_b->timestamp - priv_a->timestamp;
262         if (diff != 0) {
263                 return diff < 1 ? -1 : +1;
264         }
265                 
266         /* No real difference */
267         return 0;
268 }
269
270 const gchar *
271 empathy_presence_state_get_default_status (McPresence state)
272 {
273         switch (state) {
274         case MC_PRESENCE_AVAILABLE:
275                 return _("Available");
276         case MC_PRESENCE_DO_NOT_DISTURB:
277                 return _("Busy");
278         case MC_PRESENCE_AWAY:
279         case MC_PRESENCE_EXTENDED_AWAY:
280                 return _("Away");
281         case MC_PRESENCE_HIDDEN:
282                 return _("Hidden");
283         case MC_PRESENCE_OFFLINE:
284         case MC_PRESENCE_UNSET:
285                 return _("Offline");
286         default:
287                 g_assert_not_reached ();
288         }
289
290         return NULL;
291 }
292
293 const gchar *
294 empathy_presence_state_to_str (McPresence state)
295 {
296         switch (state) {
297         case MC_PRESENCE_AVAILABLE:
298                 return "available";
299         case MC_PRESENCE_DO_NOT_DISTURB:
300                 return "busy";
301         case MC_PRESENCE_AWAY:
302                 return "away";
303         case MC_PRESENCE_EXTENDED_AWAY:
304                 return "ext_away";
305         case MC_PRESENCE_HIDDEN:
306                 return "hidden";
307         case MC_PRESENCE_OFFLINE:
308                 return "offline";
309         case MC_PRESENCE_UNSET:
310                 return "unset";
311         default:
312                 g_assert_not_reached ();
313         }
314
315         return NULL;
316 }
317
318 McPresence
319 empathy_presence_state_from_str (const gchar *str)
320 {
321         if (strcmp (str, "available") == 0) {
322                 return MC_PRESENCE_AVAILABLE;
323         } else if ((strcmp (str, "dnd") == 0) || (strcmp (str, "busy") == 0)) {
324                 return MC_PRESENCE_DO_NOT_DISTURB;
325         } else if ((strcmp (str, "away") == 0) || (strcmp (str, "brb") == 0)) {
326                 return MC_PRESENCE_AWAY;
327         } else if ((strcmp (str, "xa") == 0) || (strcmp (str, "ext_away") == 0)) {
328                 return MC_PRESENCE_EXTENDED_AWAY;
329         } else if (strcmp (str, "hidden") == 0) {
330                 return MC_PRESENCE_HIDDEN;
331         } else if (strcmp (str, "offline") == 0) {
332                 return MC_PRESENCE_OFFLINE;
333         } else if (strcmp (str, "unset") == 0) {
334                 return MC_PRESENCE_UNSET;
335         }
336
337         return MC_PRESENCE_AVAILABLE;
338 }
339