10418
|
1 /**
|
|
2 * @file savedstatus.c Saved Status API
|
|
3 * @ingroup core
|
|
4 *
|
|
5 * gaim
|
|
6 *
|
|
7 * Gaim is the legal property of its developers, whose names are too numerous
|
|
8 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
9 * source distribution.
|
|
10 *
|
|
11 * This program is free software; you can redistribute it and/or modify
|
|
12 * it under the terms of the GNU General Public License as published by
|
|
13 * the Free Software Foundation; either version 2 of the License, or
|
|
14 * (at your option) any later version.
|
|
15 *
|
|
16 * This program is distributed in the hope that it will be useful,
|
|
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 * GNU General Public License for more details.
|
|
20 *
|
|
21 * You should have received a copy of the GNU General Public License
|
|
22 * along with this program; if not, write to the Free Software
|
|
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
24 */
|
|
25 #include "internal.h"
|
|
26
|
|
27 #include "debug.h"
|
|
28 #include "notify.h"
|
|
29 #include "savedstatuses.h"
|
|
30 #include "status.h"
|
|
31 #include "util.h"
|
|
32 #include "xmlnode.h"
|
|
33
|
|
34 /**
|
|
35 * The information of a snap-shot of the statuses of all
|
|
36 * your accounts. Basically these are your saved away messages.
|
|
37 * There is an overall status and message that applies to
|
|
38 * all your accounts, and then each individual account can
|
|
39 * optionally have a different custom status and message.
|
|
40 *
|
|
41 * The changes to status.xml caused by the new status API
|
|
42 * are fully backward compatible. The new status API just
|
|
43 * adds the optional sub-statuses to the XML file.
|
|
44 */
|
10419
|
45 struct _GaimSavedStatus
|
10418
|
46 {
|
|
47 char *title;
|
|
48 GaimStatusPrimitive type;
|
|
49 char *message;
|
|
50
|
10419
|
51 GList *substatuses; /**< A list of GaimSavedStatusSub's. */
|
10418
|
52 };
|
|
53
|
|
54 /*
|
|
55 * TODO: If an account is deleted, need to also delete any associated
|
10419
|
56 * GaimSavedStatusSub's.
|
10418
|
57 * TODO: If a GaimStatusType is deleted, need to also delete any
|
10419
|
58 * associated GaimSavedStatusSub's?
|
10418
|
59 */
|
10419
|
60 struct _GaimSavedStatusSub
|
10418
|
61 {
|
|
62 GaimAccount *account;
|
|
63 const GaimStatusType *type;
|
|
64 char *message;
|
|
65 };
|
|
66
|
|
67 static GList *saved_statuses = NULL;
|
|
68 gboolean have_read_saved_statuses = FALSE;
|
|
69 static guint statuses_save_timer = 0;
|
|
70
|
|
71 /**************************************************************************
|
|
72 * Helper functions
|
|
73 **************************************************************************/
|
|
74
|
|
75 static void
|
10419
|
76 free_statussavedsub(GaimSavedStatusSub *substatus)
|
10418
|
77 {
|
|
78 g_return_if_fail(substatus != NULL);
|
|
79
|
|
80 g_free(substatus->message);
|
|
81 g_free(substatus);
|
|
82 }
|
|
83
|
|
84 static void
|
10419
|
85 free_statussaved(GaimSavedStatus *status)
|
10418
|
86 {
|
|
87 g_return_if_fail(status != NULL);
|
|
88
|
|
89 g_free(status->title);
|
|
90 g_free(status->message);
|
|
91
|
|
92 while (status->substatuses != NULL)
|
|
93 {
|
10419
|
94 GaimSavedStatusSub *substatus = status->substatuses->data;
|
10418
|
95 status->substatuses = g_list_remove(status->substatuses, substatus);
|
|
96 free_statussavedsub(substatus);
|
|
97 }
|
|
98
|
|
99 g_free(status);
|
|
100 }
|
|
101
|
|
102
|
|
103 /**************************************************************************
|
|
104 * Saved status writting to disk
|
|
105 **************************************************************************/
|
|
106
|
|
107 static xmlnode *
|
10419
|
108 substatus_to_xmlnode(GaimSavedStatusSub *substatus)
|
10418
|
109 {
|
|
110 xmlnode *node, *child;
|
|
111
|
|
112 node = xmlnode_new("substatus");
|
|
113
|
|
114 child = xmlnode_new("account");
|
|
115 xmlnode_set_attrib(node, "protocol",
|
|
116 gaim_account_get_protocol_id(substatus->account));
|
|
117 xmlnode_insert_data(child,
|
|
118 gaim_account_get_username(substatus->account), -1);
|
|
119 xmlnode_insert_child(node, child);
|
|
120
|
|
121 child = xmlnode_new("state");
|
|
122 xmlnode_insert_data(child, gaim_status_type_get_id(substatus->type), -1);
|
|
123 xmlnode_insert_child(node, child);
|
|
124
|
|
125 if (substatus->message != NULL)
|
|
126 {
|
|
127 child = xmlnode_new("message");
|
|
128 xmlnode_insert_data(child, substatus->message, -1);
|
|
129 xmlnode_insert_child(node, child);
|
|
130 }
|
|
131
|
|
132 return node;
|
|
133 }
|
|
134
|
|
135 static xmlnode *
|
10419
|
136 status_to_xmlnode(GaimSavedStatus *status)
|
10418
|
137 {
|
|
138 xmlnode *node, *child;
|
|
139 GList *cur;
|
|
140
|
|
141 node = xmlnode_new("status");
|
|
142 xmlnode_set_attrib(node, "name", status->title);
|
|
143
|
|
144 child = xmlnode_new("state");
|
10419
|
145 xmlnode_insert_data(child, strdup(gaim_primitive_get_id_from_type(status->type)), -1);
|
10418
|
146 xmlnode_insert_child(node, child);
|
|
147
|
|
148 child = xmlnode_new("message");
|
|
149 xmlnode_insert_data(child, status->message, -1);
|
|
150 xmlnode_insert_child(node, child);
|
|
151
|
|
152 for (cur = status->substatuses; cur != NULL; cur = cur->next)
|
|
153 {
|
|
154 child = substatus_to_xmlnode(cur->data);
|
|
155 xmlnode_insert_child(node, child);
|
|
156 }
|
|
157
|
|
158 return node;
|
|
159 }
|
|
160
|
|
161 static xmlnode *
|
|
162 statuses_to_xmlnode(void)
|
|
163 {
|
|
164 xmlnode *node, *child;
|
|
165 GList *cur;
|
|
166
|
|
167 node = xmlnode_new("statuses");
|
|
168 xmlnode_set_attrib(node, "version", "1");
|
|
169
|
|
170 for (cur = saved_statuses; cur != NULL; cur = cur->next)
|
|
171 {
|
|
172 child = status_to_xmlnode(cur->data);
|
|
173 xmlnode_insert_child(node, child);
|
|
174 }
|
|
175
|
|
176 return node;
|
|
177 }
|
|
178
|
|
179 static void
|
|
180 sync_statuses(void)
|
|
181 {
|
|
182 xmlnode *statuses;
|
|
183 char *data;
|
|
184
|
|
185 if (!have_read_saved_statuses) {
|
|
186 gaim_debug_error("status", "Attempted to save statuses before they "
|
|
187 "were read!\n");
|
|
188 return;
|
|
189 }
|
|
190
|
|
191 statuses = statuses_to_xmlnode();
|
|
192 data = xmlnode_to_formatted_str(statuses, NULL);
|
|
193 gaim_util_write_data_to_file("status.xml", data, -1);
|
|
194 g_free(data);
|
|
195 xmlnode_free(statuses);
|
|
196 }
|
|
197
|
|
198 static gboolean
|
|
199 save_callback(gpointer data)
|
|
200 {
|
|
201 sync_statuses();
|
|
202 statuses_save_timer = 0;
|
|
203 return FALSE;
|
|
204 }
|
|
205
|
|
206 static void
|
|
207 schedule_save(void)
|
|
208 {
|
|
209 if (statuses_save_timer != 0)
|
|
210 gaim_timeout_remove(statuses_save_timer);
|
|
211 statuses_save_timer = gaim_timeout_add(1000, save_callback, NULL);
|
|
212 }
|
|
213
|
|
214
|
|
215 /**************************************************************************
|
|
216 * Saved status reading from disk
|
|
217 **************************************************************************/
|
10419
|
218 static GaimSavedStatusSub *
|
10418
|
219 parse_substatus(xmlnode *substatus)
|
|
220 {
|
10419
|
221 GaimSavedStatusSub *ret;
|
10418
|
222 xmlnode *node;
|
|
223 char *data = NULL;
|
|
224
|
10419
|
225 ret = g_new0(GaimSavedStatusSub, 1);
|
10418
|
226
|
|
227 /* Read the account */
|
|
228 node = xmlnode_get_child(substatus, "account");
|
|
229 if (node != NULL)
|
|
230 {
|
|
231 char *acct_name;
|
|
232 const char *protocol;
|
|
233 acct_name = xmlnode_get_data(node);
|
|
234 protocol = xmlnode_get_attrib(node, "protocol");
|
|
235 if ((acct_name != NULL) && (protocol != NULL))
|
|
236 ret->account = gaim_accounts_find(acct_name, protocol);
|
|
237 g_free(acct_name);
|
|
238 }
|
|
239
|
|
240 if (ret->account == NULL)
|
|
241 {
|
|
242 g_free(ret);
|
|
243 return NULL;
|
|
244 }
|
|
245
|
|
246 /* Read the state */
|
|
247 node = xmlnode_get_child(substatus, "state");
|
|
248 if (node != NULL)
|
|
249 data = xmlnode_get_data(node);
|
|
250 if (data != NULL) {
|
|
251 ret->type = gaim_status_type_find_with_id(
|
|
252 ret->account->status_types, data);
|
|
253 g_free(data);
|
|
254 data = NULL;
|
|
255 }
|
|
256
|
|
257 /* Read the message */
|
|
258 node = xmlnode_get_child(substatus, "message");
|
|
259 if (node != NULL)
|
|
260 data = xmlnode_get_data(node);
|
|
261 if (data != NULL)
|
|
262 ret->message = data;
|
|
263
|
|
264 return ret;
|
|
265 }
|
|
266
|
|
267 /**
|
|
268 * Parse a saved status and add it to the saved_statuses linked list.
|
|
269 *
|
|
270 * Here's an example of the XML for a saved status:
|
|
271 * <status name="Girls">
|
|
272 * <state>away</state>
|
|
273 * <message>I like the way that they walk
|
|
274 * And it's chill to hear them talk
|
|
275 * And I can always make them smile
|
|
276 * From White Castle to the Nile</message>
|
|
277 * <substatus>
|
|
278 * <account protocol='prpl-oscar'>markdoliner</account>
|
|
279 * <state>available</state>
|
|
280 * <message>The ladies man is here to answer your queries.</message>
|
|
281 * </substatus>
|
|
282 * <substatus>
|
|
283 * <account protocol='prpl-oscar'>giantgraypanda</account>
|
|
284 * <state>away</state>
|
|
285 * <message>A.C. ain't in charge no more.</message>
|
|
286 * </substatus>
|
|
287 * </status>
|
|
288 *
|
|
289 * I know. Moving, huh?
|
|
290 */
|
10419
|
291 static GaimSavedStatus *
|
10418
|
292 parse_status(xmlnode *status)
|
|
293 {
|
10419
|
294 GaimSavedStatus *ret;
|
10418
|
295 xmlnode *node;
|
|
296 const char *attrib;
|
|
297 char *data = NULL;
|
|
298 int i;
|
|
299
|
10419
|
300 ret = g_new0(GaimSavedStatus, 1);
|
10418
|
301
|
|
302 /* Read the title */
|
|
303 attrib = xmlnode_get_attrib(status, "name");
|
|
304 if (attrib == NULL)
|
|
305 attrib = "No Title";
|
|
306 /* Ensure the title is unique */
|
|
307 ret->title = g_strdup(attrib);
|
|
308 i = 2;
|
10419
|
309 while (gaim_savedstatus_find(ret->title) != NULL)
|
10418
|
310 {
|
|
311 g_free(ret->title);
|
|
312 ret->title = g_strdup_printf("%s %d", attrib, i);
|
|
313 i++;
|
|
314 }
|
|
315
|
|
316 /* Read the primitive status type */
|
|
317 node = xmlnode_get_child(status, "state");
|
|
318 if (node != NULL)
|
|
319 data = xmlnode_get_data(node);
|
|
320 if (data != NULL) {
|
10419
|
321 ret->type = gaim_primitive_get_type_from_id(data);
|
10418
|
322 g_free(data);
|
|
323 data = NULL;
|
|
324 }
|
|
325
|
|
326 /* Read the message */
|
|
327 node = xmlnode_get_child(status, "message");
|
|
328 if (node != NULL)
|
|
329 data = xmlnode_get_data(node);
|
|
330 if (data != NULL)
|
|
331 ret->message = data;
|
|
332
|
|
333 /* Read substatuses */
|
|
334 for (node = xmlnode_get_child(status, "status"); node != NULL;
|
|
335 node = xmlnode_get_next_twin(node))
|
|
336 {
|
10419
|
337 GaimSavedStatusSub *new;
|
10418
|
338 new = parse_substatus(node);
|
|
339 if (new != NULL)
|
|
340 ret->substatuses = g_list_append(ret->substatuses, new);
|
|
341 }
|
|
342
|
|
343 return ret;
|
|
344 }
|
|
345
|
|
346 /**
|
|
347 * Read the saved statuses from a file in the Gaim user dir.
|
|
348 *
|
|
349 * @return TRUE on success, FALSE on failure (if the file can not
|
|
350 * be opened, or if it contains invalid XML).
|
|
351 */
|
|
352 static gboolean
|
|
353 read_statuses(const char *filename)
|
|
354 {
|
|
355 GError *error;
|
|
356 gchar *contents = NULL;
|
|
357 gsize length;
|
|
358 xmlnode *statuses, *status;
|
|
359
|
|
360 gaim_debug_info("status", "Reading %s\n", filename);
|
|
361
|
|
362 if (!g_file_get_contents(filename, &contents, &length, &error))
|
|
363 {
|
|
364 gaim_debug_error("status", "Error reading statuses: %s\n",
|
|
365 error->message);
|
|
366 g_error_free(error);
|
|
367 return FALSE;
|
|
368 }
|
|
369
|
|
370 statuses = xmlnode_from_str(contents, length);
|
|
371
|
|
372 if (statuses == NULL)
|
|
373 {
|
|
374 FILE *backup;
|
|
375 gchar *name;
|
|
376 gaim_debug_error("status", "Error parsing statuses\n");
|
|
377 name = g_strdup_printf("%s~", filename);
|
|
378 if ((backup = fopen(name, "w")))
|
|
379 {
|
|
380 fwrite(contents, length, 1, backup);
|
|
381 fclose(backup);
|
|
382 chmod(name, S_IRUSR | S_IWUSR);
|
|
383 }
|
|
384 else
|
|
385 {
|
|
386 gaim_debug_error("status", "Unable to write backup %s\n", name);
|
|
387 }
|
|
388 g_free(name);
|
|
389 g_free(contents);
|
|
390 return FALSE;
|
|
391 }
|
|
392
|
|
393 g_free(contents);
|
|
394
|
|
395 for (status = xmlnode_get_child(statuses, "status"); status != NULL;
|
|
396 status = xmlnode_get_next_twin(status))
|
|
397 {
|
10419
|
398 GaimSavedStatus *new;
|
10418
|
399 new = parse_status(status);
|
|
400 saved_statuses = g_list_append(saved_statuses, new);
|
|
401 }
|
|
402
|
|
403 gaim_debug_info("status", "Finished reading statuses\n");
|
|
404
|
|
405 xmlnode_free(statuses);
|
|
406
|
|
407 return TRUE;
|
|
408 }
|
|
409
|
|
410 static void
|
|
411 load_statuses(void)
|
|
412 {
|
|
413 const char *user_dir = gaim_user_dir();
|
|
414 gchar *filename;
|
|
415 gchar *msg;
|
|
416
|
|
417 g_return_if_fail(user_dir != NULL);
|
|
418
|
|
419 have_read_saved_statuses = TRUE;
|
|
420
|
|
421 filename = g_build_filename(user_dir, "status.xml", NULL);
|
|
422
|
|
423 if (g_file_test(filename, G_FILE_TEST_EXISTS))
|
|
424 {
|
|
425 if (!read_statuses(filename))
|
|
426 {
|
|
427 msg = g_strdup_printf(_("An error was encountered parsing the "
|
|
428 "file containing your saved statuses (%s). They "
|
|
429 "have not been loaded, and the old file has been "
|
|
430 "renamed to status.xml~."), filename);
|
|
431 gaim_notify_error(NULL, NULL, _("Saved Statuses Error"), msg);
|
|
432 g_free(msg);
|
|
433 }
|
|
434 }
|
|
435
|
|
436 g_free(filename);
|
|
437 }
|
|
438
|
|
439
|
|
440 /**************************************************************************
|
|
441 * Saved status API
|
|
442 **************************************************************************/
|
10419
|
443 GaimSavedStatus *
|
|
444 gaim_savedstatus_new(const char *title, GaimStatusPrimitive type)
|
10418
|
445 {
|
10419
|
446 GaimSavedStatus *status;
|
10418
|
447
|
10420
|
448 g_return_val_if_fail(gaim_savedstatus_find(title) == NULL, NULL);
|
|
449
|
10419
|
450 status = g_new0(GaimSavedStatus, 1);
|
10418
|
451 status->title = g_strdup(title);
|
|
452 status->type = type;
|
|
453
|
|
454 saved_statuses = g_list_append(saved_statuses, status);
|
|
455
|
|
456 schedule_save();
|
|
457
|
|
458 return status;
|
|
459 }
|
|
460
|
10420
|
461 void
|
|
462 gaim_savedstatus_set_message(GaimSavedStatus *status, const char *message)
|
|
463 {
|
|
464 g_return_if_fail(status != NULL);
|
|
465
|
|
466 g_free(status->message);
|
|
467 status->message = g_strdup(message);
|
|
468
|
|
469 schedule_save();
|
|
470 }
|
|
471
|
10418
|
472 gboolean
|
10419
|
473 gaim_savedstatus_delete(const char *title)
|
10418
|
474 {
|
10419
|
475 GaimSavedStatus *status;
|
10418
|
476
|
10419
|
477 status = gaim_savedstatus_find(title);
|
10418
|
478
|
|
479 if (status == NULL)
|
|
480 return FALSE;
|
|
481
|
|
482 saved_statuses = g_list_remove(saved_statuses, status);
|
|
483 free_statussaved(status);
|
|
484
|
|
485 schedule_save();
|
|
486
|
|
487 return TRUE;
|
|
488 }
|
|
489
|
|
490 const GList *
|
|
491 gaim_savedstatuses_get_all(void)
|
|
492 {
|
|
493 return saved_statuses;
|
|
494 }
|
|
495
|
10419
|
496 GaimSavedStatus *
|
|
497 gaim_savedstatus_find(const char *title)
|
10418
|
498 {
|
|
499 GList *l;
|
10419
|
500 GaimSavedStatus *status;
|
10418
|
501
|
|
502 for (l = saved_statuses; l != NULL; l = g_list_next(l))
|
|
503 {
|
10419
|
504 status = (GaimSavedStatus *)l->data;
|
10418
|
505 if (!strcmp(status->title, title))
|
|
506 return status;
|
|
507 }
|
|
508
|
|
509 return NULL;
|
|
510 }
|
|
511
|
|
512 const char *
|
10419
|
513 gaim_savedstatus_get_title(const GaimSavedStatus *saved_status)
|
10418
|
514 {
|
|
515 return saved_status->title;
|
|
516 }
|
|
517
|
|
518 GaimStatusPrimitive
|
10419
|
519 gaim_savedstatus_get_type(const GaimSavedStatus *saved_status)
|
10418
|
520 {
|
|
521 return saved_status->type;
|
|
522 }
|
|
523
|
|
524 const char *
|
10419
|
525 gaim_savedstatus_get_message(const GaimSavedStatus *saved_status)
|
10418
|
526 {
|
|
527 return saved_status->message;
|
|
528 }
|
|
529
|
|
530 void
|
|
531 gaim_savedstatuses_init(void)
|
|
532 {
|
|
533 load_statuses();
|
|
534 }
|
|
535
|
|
536 void
|
|
537 gaim_savedstatuses_uninit(void)
|
|
538 {
|
|
539 if (statuses_save_timer != 0)
|
|
540 {
|
|
541 gaim_timeout_remove(statuses_save_timer);
|
|
542 statuses_save_timer = 0;
|
|
543 sync_statuses();
|
|
544 }
|
|
545
|
|
546 while (saved_statuses != NULL) {
|
10419
|
547 GaimSavedStatus *status = saved_statuses->data;
|
10418
|
548 saved_statuses = g_list_remove(saved_statuses, status);
|
|
549 free_statussaved(status);
|
|
550 }
|
|
551 }
|