1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2005-2007 Imendio AB
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Author: Martyn Russell <martyn@imendio.com>
*/
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <telepathy-glib/util.h>
#include "empathy-utils.h"
#include "empathy-status-presets.h"
#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
#include "empathy-debug.h"
#define STATUS_PRESETS_XML_FILENAME "status-presets.xml"
#define STATUS_PRESETS_DTD_FILENAME "empathy-status-presets.dtd"
#define STATUS_PRESETS_MAX_EACH 15
typedef struct {
gchar *status;
McPresence state;
} StatusPreset;
static StatusPreset *status_preset_new (McPresence state,
const gchar *status);
static void status_preset_free (StatusPreset *status);
static void status_presets_file_parse (const gchar *filename);
const gchar * status_presets_get_state_as_str (McPresence state);
static gboolean status_presets_file_save (void);
static void status_presets_set_default (McPresence state,
const gchar *status);
static GList *presets = NULL;
static StatusPreset *default_preset = NULL;
static StatusPreset *
status_preset_new (McPresence state,
const gchar *status)
{
StatusPreset *preset;
preset = g_new0 (StatusPreset, 1);
preset->status = g_strdup (status);
preset->state = state;
return preset;
}
static void
status_preset_free (StatusPreset *preset)
{
g_free (preset->status);
g_free (preset);
}
static void
status_presets_file_parse (const gchar *filename)
{
xmlParserCtxtPtr ctxt;
xmlDocPtr doc;
xmlNodePtr presets_node;
xmlNodePtr node;
DEBUG ("Attempting to parse file:'%s'...", filename);
ctxt = xmlNewParserCtxt ();
/* Parse and validate the file. */
doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
if (!doc) {
g_warning ("Failed to parse file:'%s'", filename);
xmlFreeParserCtxt (ctxt);
return;
}
if (!empathy_xml_validate (doc, STATUS_PRESETS_DTD_FILENAME)) {
g_warning ("Failed to validate file:'%s'", filename);
xmlFreeDoc (doc);
xmlFreeParserCtxt (ctxt);
return;
}
/* The root node, presets. */
presets_node = xmlDocGetRootElement (doc);
node = presets_node->children;
while (node) {
if (strcmp ((gchar *) node->name, "status") == 0 ||
strcmp ((gchar *) node->name, "default") == 0) {
McPresence state;
gchar *status;
gchar *state_str;
StatusPreset *preset;
gboolean is_default = FALSE;
if (strcmp ((gchar *) node->name, "default") == 0) {
is_default = TRUE;
}
status = (gchar *) xmlNodeGetContent (node);
state_str = (gchar *) xmlGetProp (node, "presence");
if (state_str) {
state = empathy_presence_from_str (state_str);
if (is_default) {
DEBUG ("Default status preset state is:"
" '%s', status:'%s'", state_str,
status);
status_presets_set_default (state, status);
} else {
preset = status_preset_new (state, status);
presets = g_list_append (presets, preset);
}
}
xmlFree (status);
xmlFree (state_str);
}
node = node->next;
}
/* Use the default if not set */
if (!default_preset) {
status_presets_set_default (MC_PRESENCE_OFFLINE, NULL);
}
DEBUG ("Parsed %d status presets", g_list_length (presets));
xmlFreeDoc (doc);
xmlFreeParserCtxt (ctxt);
}
void
empathy_status_presets_get_all (void)
{
gchar *dir;
gchar *file_with_path;
/* If already set up clean up first. */
if (presets) {
g_list_foreach (presets, (GFunc) status_preset_free, NULL);
g_list_free (presets);
presets = NULL;
}
dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
file_with_path = g_build_filename (dir, STATUS_PRESETS_XML_FILENAME, NULL);
g_free (dir);
if (g_file_test (file_with_path, G_FILE_TEST_EXISTS)) {
status_presets_file_parse (file_with_path);
}
g_free (file_with_path);
}
static gboolean
status_presets_file_save (void)
{
xmlDocPtr doc;
xmlNodePtr root;
GList *l;
gchar *dir;
gchar *file;
gint count[LAST_MC_PRESENCE];
gint i;
for (i = 0; i < LAST_MC_PRESENCE; i++) {
count[i] = 0;
}
dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
file = g_build_filename (dir, STATUS_PRESETS_XML_FILENAME, NULL);
g_free (dir);
doc = xmlNewDoc ("1.0");
root = xmlNewNode (NULL, "presets");
xmlDocSetRootElement (doc, root);
if (default_preset) {
xmlNodePtr subnode;
xmlChar *state;
state = (gchar *) empathy_presence_to_str (default_preset->state);
subnode = xmlNewTextChild (root, NULL, "default",
default_preset->status);
xmlNewProp (subnode, "presence", state);
}
for (l = presets; l; l = l->next) {
StatusPreset *sp;
xmlNodePtr subnode;
xmlChar *state;
sp = l->data;
state = (gchar *) empathy_presence_to_str (sp->state);
count[sp->state]++;
if (count[sp->state] > STATUS_PRESETS_MAX_EACH) {
continue;
}
subnode = xmlNewTextChild (root, NULL,
"status", sp->status);
xmlNewProp (subnode, "presence", state);
}
/* Make sure the XML is indented properly */
xmlIndentTreeOutput = 1;
DEBUG ("Saving file:'%s'", file);
xmlSaveFormatFileEnc (file, doc, "utf-8", 1);
xmlFreeDoc (doc);
g_free (file);
return TRUE;
}
GList *
empathy_status_presets_get (McPresence state,
gint max_number)
{
GList *list = NULL;
GList *l;
gint i;
i = 0;
for (l = presets; l; l = l->next) {
StatusPreset *sp;
sp = l->data;
if (sp->state != state) {
continue;
}
list = g_list_append (list, sp->status);
i++;
if (max_number != -1 && i >= max_number) {
break;
}
}
return list;
}
void
empathy_status_presets_set_last (McPresence state,
const gchar *status)
{
GList *l;
StatusPreset *preset;
gint num;
/* Check if duplicate */
for (l = presets; l; l = l->next) {
preset = l->data;
if (state == preset->state &&
!tp_strdiff (status, preset->status)) {
return;
}
}
preset = status_preset_new (state, status);
presets = g_list_prepend (presets, preset);
num = 0;
for (l = presets; l; l = l->next) {
preset = l->data;
if (state != preset->state) {
continue;
}
num++;
if (num > STATUS_PRESETS_MAX_EACH) {
status_preset_free (preset);
presets = g_list_delete_link (presets, l);
break;
}
}
status_presets_file_save ();
}
void
empathy_status_presets_remove (McPresence state,
const gchar *status)
{
StatusPreset *preset;
GList *l;
for (l = presets; l; l = l->next) {
preset = l->data;
if (state == preset->state &&
!tp_strdiff (status, preset->status)) {
status_preset_free (preset);
presets = g_list_delete_link (presets, l);
status_presets_file_save ();
break;
}
}
}
void
empathy_status_presets_reset (void)
{
g_list_foreach (presets, (GFunc) status_preset_free, NULL);
g_list_free (presets);
presets = NULL;
status_presets_set_default (MC_PRESENCE_AVAILABLE, NULL);
status_presets_file_save ();
}
McPresence
empathy_status_presets_get_default_state (void)
{
if (!default_preset) {
return MC_PRESENCE_OFFLINE;
}
return default_preset->state;
}
const gchar *
empathy_status_presets_get_default_status (void)
{
if (!default_preset ||
!default_preset->status) {
return NULL;
}
return default_preset->status;
}
static void
status_presets_set_default (McPresence state,
const gchar *status)
{
if (default_preset) {
status_preset_free (default_preset);
}
default_preset = status_preset_new (state, status);
}
void
empathy_status_presets_set_default (McPresence state,
const gchar *status)
{
status_presets_set_default (state, status);
status_presets_file_save ();
}
void
empathy_status_presets_clear_default (void)
{
if (default_preset) {
status_preset_free (default_preset);
default_preset = NULL;
}
status_presets_file_save ();
}
|