1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005-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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301 USA
20 * Author: Martyn Russell <martyn@imendio.com>
24 #include "empathy-status-presets.h"
27 #include <tp-account-widgets/tpaw-utils.h>
29 #include "empathy-utils.h"
31 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
32 #include "empathy-debug.h"
34 #define STATUS_PRESETS_XML_FILENAME "status-presets.xml"
35 #define STATUS_PRESETS_DTD_RESOURCENAME "/org/gnome/Empathy/empathy-status-presets.dtd"
36 #define STATUS_PRESETS_MAX_EACH 15
40 TpConnectionPresenceType state
;
43 static StatusPreset
*status_preset_new (TpConnectionPresenceType state
,
45 static void status_preset_free (StatusPreset
*status
);
46 static void status_presets_file_parse (const gchar
*filename
);
47 const gchar
* status_presets_get_state_as_str (TpConnectionPresenceType state
);
48 static gboolean
status_presets_file_save (void);
49 static void status_presets_set_default (TpConnectionPresenceType state
,
52 static GList
*presets
= NULL
;
53 static StatusPreset
*default_preset
= NULL
;
56 status_preset_new (TpConnectionPresenceType state
,
61 preset
= g_new0 (StatusPreset
, 1);
63 preset
->status
= g_strdup (status
);
64 preset
->state
= state
;
70 status_preset_free (StatusPreset
*preset
)
72 g_free (preset
->status
);
77 status_presets_file_parse (const gchar
*filename
)
79 xmlParserCtxtPtr ctxt
;
81 xmlNodePtr presets_node
;
84 DEBUG ("Attempting to parse file:'%s'...", filename
);
86 ctxt
= xmlNewParserCtxt ();
88 /* Parse and validate the file. */
89 doc
= xmlCtxtReadFile (ctxt
, filename
, NULL
, 0);
91 g_warning ("Failed to parse file:'%s'", filename
);
92 xmlFreeParserCtxt (ctxt
);
96 if (!tpaw_xml_validate_from_resource (doc
, STATUS_PRESETS_DTD_RESOURCENAME
)) {
97 g_warning ("Failed to validate file:'%s'", filename
);
99 xmlFreeParserCtxt (ctxt
);
103 /* The root node, presets. */
104 presets_node
= xmlDocGetRootElement (doc
);
106 node
= presets_node
->children
;
108 if (strcmp ((gchar
*) node
->name
, "status") == 0 ||
109 strcmp ((gchar
*) node
->name
, "default") == 0) {
110 TpConnectionPresenceType state
;
113 StatusPreset
*preset
;
114 gboolean is_default
= FALSE
;
116 if (strcmp ((gchar
*) node
->name
, "default") == 0) {
120 status
= (gchar
*) xmlNodeGetContent (node
);
121 state_str
= (gchar
*) xmlGetProp (node
, (const xmlChar
*) "presence");
124 state
= empathy_presence_from_str (state_str
);
125 if (empathy_status_presets_is_valid (state
)) {
127 DEBUG ("Default status preset state is:"
128 " '%s', status:'%s'", state_str
,
131 status_presets_set_default (state
, status
);
133 preset
= status_preset_new (state
, status
);
134 presets
= g_list_append (presets
, preset
);
146 /* Use the default if not set */
147 if (!default_preset
) {
148 status_presets_set_default (TP_CONNECTION_PRESENCE_TYPE_OFFLINE
, NULL
);
151 DEBUG ("Parsed %d status presets", g_list_length (presets
));
154 xmlFreeParserCtxt (ctxt
);
158 empathy_status_presets_get_all (void)
161 gchar
*file_with_path
;
163 /* If already set up clean up first. */
165 g_list_foreach (presets
, (GFunc
) status_preset_free
, NULL
);
166 g_list_free (presets
);
170 dir
= g_build_filename (g_get_user_config_dir (), PACKAGE_NAME
, NULL
);
171 g_mkdir_with_parents (dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
);
172 file_with_path
= g_build_filename (dir
, STATUS_PRESETS_XML_FILENAME
, NULL
);
175 if (g_file_test (file_with_path
, G_FILE_TEST_EXISTS
)) {
176 status_presets_file_parse (file_with_path
);
179 g_free (file_with_path
);
183 status_presets_file_save (void)
190 gint count
[TP_NUM_CONNECTION_PRESENCE_TYPES
];
193 for (i
= 0; i
< TP_NUM_CONNECTION_PRESENCE_TYPES
; i
++) {
197 dir
= g_build_filename (g_get_user_config_dir (), PACKAGE_NAME
, NULL
);
198 g_mkdir_with_parents (dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
);
199 file
= g_build_filename (dir
, STATUS_PRESETS_XML_FILENAME
, NULL
);
202 doc
= xmlNewDoc ((const xmlChar
*) "1.0");
203 root
= xmlNewNode (NULL
, (const xmlChar
*) "presets");
204 xmlDocSetRootElement (doc
, root
);
206 if (default_preset
) {
210 state
= (xmlChar
*) empathy_presence_to_str (default_preset
->state
);
212 subnode
= xmlNewTextChild (root
, NULL
, (const xmlChar
*) "default",
213 (const xmlChar
*) default_preset
->status
);
214 xmlNewProp (subnode
, (const xmlChar
*) "presence", state
);
217 for (l
= presets
; l
; l
= l
->next
) {
223 state
= (xmlChar
*) empathy_presence_to_str (sp
->state
);
226 if (count
[sp
->state
] > STATUS_PRESETS_MAX_EACH
) {
230 subnode
= xmlNewTextChild (root
, NULL
,
231 (const xmlChar
*) "status", (const xmlChar
*) sp
->status
);
232 xmlNewProp (subnode
, (const xmlChar
*) "presence", state
);
235 /* Make sure the XML is indented properly */
236 xmlIndentTreeOutput
= 1;
238 DEBUG ("Saving file:'%s'", file
);
239 xmlSaveFormatFileEnc (file
, doc
, "utf-8", 1);
248 empathy_status_presets_get (TpConnectionPresenceType state
,
256 for (l
= presets
; l
; l
= l
->next
) {
261 if (sp
->state
!= state
) {
265 list
= g_list_append (list
, sp
->status
);
268 if (max_number
!= -1 && i
>= max_number
) {
277 empathy_status_presets_set_last (TpConnectionPresenceType state
,
281 StatusPreset
*preset
;
284 /* Check if duplicate */
285 for (l
= presets
; l
; l
= l
->next
) {
288 if (state
== preset
->state
&&
289 !tp_strdiff (status
, preset
->status
)) {
294 preset
= status_preset_new (state
, status
);
295 presets
= g_list_prepend (presets
, preset
);
298 for (l
= presets
; l
; l
= l
->next
) {
301 if (state
!= preset
->state
) {
307 if (num
> STATUS_PRESETS_MAX_EACH
) {
308 status_preset_free (preset
);
309 presets
= g_list_delete_link (presets
, l
);
314 status_presets_file_save ();
318 empathy_status_presets_remove (TpConnectionPresenceType state
,
321 StatusPreset
*preset
;
324 for (l
= presets
; l
; l
= l
->next
) {
327 if (state
== preset
->state
&&
328 !tp_strdiff (status
, preset
->status
)) {
329 status_preset_free (preset
);
330 presets
= g_list_delete_link (presets
, l
);
331 status_presets_file_save ();
338 empathy_status_presets_reset (void)
340 g_list_foreach (presets
, (GFunc
) status_preset_free
, NULL
);
341 g_list_free (presets
);
345 status_presets_set_default (TP_CONNECTION_PRESENCE_TYPE_AVAILABLE
, NULL
);
347 status_presets_file_save ();
350 TpConnectionPresenceType
351 empathy_status_presets_get_default_state (void)
353 if (!default_preset
) {
354 return TP_CONNECTION_PRESENCE_TYPE_OFFLINE
;
357 return default_preset
->state
;
361 empathy_status_presets_get_default_status (void)
363 if (!default_preset
||
364 !default_preset
->status
) {
368 return default_preset
->status
;
372 status_presets_set_default (TpConnectionPresenceType state
,
375 if (default_preset
) {
376 status_preset_free (default_preset
);
379 default_preset
= status_preset_new (state
, status
);
383 empathy_status_presets_set_default (TpConnectionPresenceType state
,
386 status_presets_set_default (state
, status
);
387 status_presets_file_save ();
391 empathy_status_presets_clear_default (void)
393 if (default_preset
) {
394 status_preset_free (default_preset
);
395 default_preset
= NULL
;
398 status_presets_file_save ();
402 * empathy_status_presets_is_valid:
403 * @state: a #TpConnectionPresenceType
405 * Check if a presence type can be used as a preset.
407 * Returns: %TRUE if the presence type can be used as a preset.
410 empathy_status_presets_is_valid (TpConnectionPresenceType state
)
413 case TP_CONNECTION_PRESENCE_TYPE_UNSET
:
414 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE
:
415 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN
:
416 case TP_CONNECTION_PRESENCE_TYPE_ERROR
:
419 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE
:
420 case TP_CONNECTION_PRESENCE_TYPE_AWAY
:
421 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY
:
422 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN
:
423 case TP_CONNECTION_PRESENCE_TYPE_BUSY
: