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.
|
10738
|
44 *
|
|
45 * TODO: This should probably just use a GaimStatus...
|
10418
|
46 */
|
10419
|
47 struct _GaimSavedStatus
|
10418
|
48 {
|
11651
|
49 /**
|
|
50 * A "transient" status is one that was used recently by
|
|
51 * a Gaim user, but was not explicitly created using the
|
|
52 * saved status UI. For example, Gaim's previous status
|
|
53 * is saved in the status.xml file, but should not show
|
|
54 * up in the UI.
|
|
55 */
|
|
56 gboolean transient;
|
|
57
|
10418
|
58 char *title;
|
|
59 GaimStatusPrimitive type;
|
|
60 char *message;
|
|
61
|
10419
|
62 GList *substatuses; /**< A list of GaimSavedStatusSub's. */
|
10418
|
63 };
|
|
64
|
|
65 /*
|
|
66 * TODO: If an account is deleted, need to also delete any associated
|
10419
|
67 * GaimSavedStatusSub's.
|
10418
|
68 * TODO: If a GaimStatusType is deleted, need to also delete any
|
10419
|
69 * associated GaimSavedStatusSub's?
|
10418
|
70 */
|
10419
|
71 struct _GaimSavedStatusSub
|
10418
|
72 {
|
|
73 GaimAccount *account;
|
|
74 const GaimStatusType *type;
|
|
75 char *message;
|
|
76 };
|
|
77
|
10423
|
78 static GList *saved_statuses = NULL;
|
10428
|
79 static guint save_timer = 0;
|
10423
|
80 static gboolean statuses_loaded = FALSE;
|
10418
|
81
|
10427
|
82
|
10428
|
83 /*********************************************************************
|
|
84 * Private utility functions *
|
|
85 *********************************************************************/
|
10418
|
86
|
|
87 static void
|
10419
|
88 free_statussavedsub(GaimSavedStatusSub *substatus)
|
10418
|
89 {
|
|
90 g_return_if_fail(substatus != NULL);
|
|
91
|
|
92 g_free(substatus->message);
|
|
93 g_free(substatus);
|
|
94 }
|
|
95
|
|
96 static void
|
10419
|
97 free_statussaved(GaimSavedStatus *status)
|
10418
|
98 {
|
|
99 g_return_if_fail(status != NULL);
|
|
100
|
|
101 g_free(status->title);
|
|
102 g_free(status->message);
|
|
103
|
|
104 while (status->substatuses != NULL)
|
|
105 {
|
10419
|
106 GaimSavedStatusSub *substatus = status->substatuses->data;
|
10418
|
107 status->substatuses = g_list_remove(status->substatuses, substatus);
|
|
108 free_statussavedsub(substatus);
|
|
109 }
|
|
110
|
|
111 g_free(status);
|
|
112 }
|
|
113
|
10428
|
114 /*********************************************************************
|
10429
|
115 * Writing to disk *
|
10428
|
116 *********************************************************************/
|
10418
|
117
|
|
118 static xmlnode *
|
10419
|
119 substatus_to_xmlnode(GaimSavedStatusSub *substatus)
|
10418
|
120 {
|
|
121 xmlnode *node, *child;
|
|
122
|
|
123 node = xmlnode_new("substatus");
|
|
124
|
10424
|
125 child = xmlnode_new_child(node, "account");
|
|
126 xmlnode_set_attrib(child, "protocol", gaim_account_get_protocol_id(substatus->account));
|
|
127 xmlnode_insert_data(child, gaim_account_get_username(substatus->account), -1);
|
10418
|
128
|
10424
|
129 child = xmlnode_new_child(node, "state");
|
10418
|
130 xmlnode_insert_data(child, gaim_status_type_get_id(substatus->type), -1);
|
|
131
|
|
132 if (substatus->message != NULL)
|
|
133 {
|
10424
|
134 child = xmlnode_new_child(node, "message");
|
10418
|
135 xmlnode_insert_data(child, substatus->message, -1);
|
|
136 }
|
|
137
|
|
138 return node;
|
|
139 }
|
|
140
|
|
141 static xmlnode *
|
10419
|
142 status_to_xmlnode(GaimSavedStatus *status)
|
10418
|
143 {
|
|
144 xmlnode *node, *child;
|
11651
|
145 char transient[2];
|
10418
|
146 GList *cur;
|
|
147
|
11651
|
148 snprintf(transient, sizeof(transient), "%d", status->transient);
|
|
149
|
10418
|
150 node = xmlnode_new("status");
|
11651
|
151 xmlnode_set_attrib(node, "transient", transient);
|
10418
|
152 xmlnode_set_attrib(node, "name", status->title);
|
|
153
|
10424
|
154 child = xmlnode_new_child(node, "state");
|
|
155 xmlnode_insert_data(child, gaim_primitive_get_id_from_type(status->type), -1);
|
10418
|
156
|
11651
|
157 if (status->message != NULL)
|
|
158 {
|
|
159 child = xmlnode_new_child(node, "message");
|
|
160 xmlnode_insert_data(child, status->message, -1);
|
|
161 }
|
10418
|
162
|
|
163 for (cur = status->substatuses; cur != NULL; cur = cur->next)
|
|
164 {
|
|
165 child = substatus_to_xmlnode(cur->data);
|
|
166 xmlnode_insert_child(node, child);
|
|
167 }
|
|
168
|
|
169 return node;
|
|
170 }
|
|
171
|
|
172 static xmlnode *
|
|
173 statuses_to_xmlnode(void)
|
|
174 {
|
|
175 xmlnode *node, *child;
|
|
176 GList *cur;
|
|
177
|
|
178 node = xmlnode_new("statuses");
|
10423
|
179 xmlnode_set_attrib(node, "version", "1.0");
|
10418
|
180
|
|
181 for (cur = saved_statuses; cur != NULL; cur = cur->next)
|
|
182 {
|
|
183 child = status_to_xmlnode(cur->data);
|
|
184 xmlnode_insert_child(node, child);
|
|
185 }
|
|
186
|
|
187 return node;
|
|
188 }
|
|
189
|
|
190 static void
|
|
191 sync_statuses(void)
|
|
192 {
|
10423
|
193 xmlnode *node;
|
10418
|
194 char *data;
|
|
195
|
10428
|
196 if (!statuses_loaded)
|
|
197 {
|
10418
|
198 gaim_debug_error("status", "Attempted to save statuses before they "
|
|
199 "were read!\n");
|
|
200 return;
|
|
201 }
|
|
202
|
10423
|
203 node = statuses_to_xmlnode();
|
|
204 data = xmlnode_to_formatted_str(node, NULL);
|
10418
|
205 gaim_util_write_data_to_file("status.xml", data, -1);
|
|
206 g_free(data);
|
10423
|
207 xmlnode_free(node);
|
10418
|
208 }
|
|
209
|
|
210 static gboolean
|
10428
|
211 save_cb(gpointer data)
|
10418
|
212 {
|
|
213 sync_statuses();
|
10428
|
214 save_timer = 0;
|
10418
|
215 return FALSE;
|
|
216 }
|
|
217
|
|
218 static void
|
|
219 schedule_save(void)
|
|
220 {
|
10428
|
221 if (save_timer == 0)
|
|
222 save_timer = gaim_timeout_add(5000, save_cb, NULL);
|
10418
|
223 }
|
|
224
|
|
225
|
10428
|
226 /*********************************************************************
|
|
227 * Reading from disk *
|
|
228 *********************************************************************/
|
|
229
|
10419
|
230 static GaimSavedStatusSub *
|
10418
|
231 parse_substatus(xmlnode *substatus)
|
|
232 {
|
10419
|
233 GaimSavedStatusSub *ret;
|
10418
|
234 xmlnode *node;
|
10425
|
235 char *data;
|
10418
|
236
|
10419
|
237 ret = g_new0(GaimSavedStatusSub, 1);
|
10418
|
238
|
|
239 /* Read the account */
|
|
240 node = xmlnode_get_child(substatus, "account");
|
|
241 if (node != NULL)
|
|
242 {
|
|
243 char *acct_name;
|
|
244 const char *protocol;
|
|
245 acct_name = xmlnode_get_data(node);
|
|
246 protocol = xmlnode_get_attrib(node, "protocol");
|
|
247 if ((acct_name != NULL) && (protocol != NULL))
|
|
248 ret->account = gaim_accounts_find(acct_name, protocol);
|
|
249 g_free(acct_name);
|
|
250 }
|
|
251
|
|
252 if (ret->account == NULL)
|
|
253 {
|
|
254 g_free(ret);
|
|
255 return NULL;
|
|
256 }
|
|
257
|
|
258 /* Read the state */
|
|
259 node = xmlnode_get_child(substatus, "state");
|
10426
|
260 if ((node != NULL) && ((data = xmlnode_get_data(node)) != NULL))
|
10425
|
261 {
|
10418
|
262 ret->type = gaim_status_type_find_with_id(
|
10425
|
263 ret->account->status_types, data);
|
10418
|
264 g_free(data);
|
|
265 }
|
|
266
|
|
267 /* Read the message */
|
|
268 node = xmlnode_get_child(substatus, "message");
|
10426
|
269 if ((node != NULL) && ((data = xmlnode_get_data(node)) != NULL))
|
10425
|
270 {
|
10418
|
271 ret->message = data;
|
10425
|
272 }
|
10418
|
273
|
|
274 return ret;
|
|
275 }
|
|
276
|
|
277 /**
|
|
278 * Parse a saved status and add it to the saved_statuses linked list.
|
|
279 *
|
|
280 * Here's an example of the XML for a saved status:
|
|
281 * <status name="Girls">
|
|
282 * <state>away</state>
|
|
283 * <message>I like the way that they walk
|
|
284 * And it's chill to hear them talk
|
|
285 * And I can always make them smile
|
|
286 * From White Castle to the Nile</message>
|
|
287 * <substatus>
|
|
288 * <account protocol='prpl-oscar'>markdoliner</account>
|
|
289 * <state>available</state>
|
|
290 * <message>The ladies man is here to answer your queries.</message>
|
|
291 * </substatus>
|
|
292 * <substatus>
|
|
293 * <account protocol='prpl-oscar'>giantgraypanda</account>
|
|
294 * <state>away</state>
|
|
295 * <message>A.C. ain't in charge no more.</message>
|
|
296 * </substatus>
|
|
297 * </status>
|
|
298 *
|
|
299 * I know. Moving, huh?
|
|
300 */
|
10419
|
301 static GaimSavedStatus *
|
10418
|
302 parse_status(xmlnode *status)
|
|
303 {
|
10419
|
304 GaimSavedStatus *ret;
|
10418
|
305 xmlnode *node;
|
|
306 const char *attrib;
|
10425
|
307 char *data;
|
10418
|
308 int i;
|
|
309
|
10419
|
310 ret = g_new0(GaimSavedStatus, 1);
|
10418
|
311
|
11651
|
312 /* Read the transient property */
|
|
313 attrib = xmlnode_get_attrib(status, "transient");
|
|
314 if ((attrib != NULL) && (attrib[0] == '1'))
|
|
315 ret->transient = TRUE;
|
|
316
|
10418
|
317 /* Read the title */
|
|
318 attrib = xmlnode_get_attrib(status, "name");
|
|
319 if (attrib == NULL)
|
|
320 attrib = "No Title";
|
|
321 /* Ensure the title is unique */
|
|
322 ret->title = g_strdup(attrib);
|
|
323 i = 2;
|
10419
|
324 while (gaim_savedstatus_find(ret->title) != NULL)
|
10418
|
325 {
|
|
326 g_free(ret->title);
|
|
327 ret->title = g_strdup_printf("%s %d", attrib, i);
|
|
328 i++;
|
|
329 }
|
|
330
|
|
331 /* Read the primitive status type */
|
|
332 node = xmlnode_get_child(status, "state");
|
10426
|
333 if ((node != NULL) && ((data = xmlnode_get_data(node)) != NULL))
|
10425
|
334 {
|
10419
|
335 ret->type = gaim_primitive_get_type_from_id(data);
|
10418
|
336 g_free(data);
|
|
337 }
|
|
338
|
|
339 /* Read the message */
|
|
340 node = xmlnode_get_child(status, "message");
|
10426
|
341 if ((node != NULL) && ((data = xmlnode_get_data(node)) != NULL))
|
10425
|
342 {
|
10418
|
343 ret->message = data;
|
10425
|
344 }
|
10418
|
345
|
|
346 /* Read substatuses */
|
|
347 for (node = xmlnode_get_child(status, "status"); node != NULL;
|
|
348 node = xmlnode_get_next_twin(node))
|
|
349 {
|
10419
|
350 GaimSavedStatusSub *new;
|
10418
|
351 new = parse_substatus(node);
|
|
352 if (new != NULL)
|
|
353 ret->substatuses = g_list_append(ret->substatuses, new);
|
|
354 }
|
|
355
|
|
356 return ret;
|
|
357 }
|
|
358
|
|
359 /**
|
|
360 * Read the saved statuses from a file in the Gaim user dir.
|
|
361 *
|
|
362 * @return TRUE on success, FALSE on failure (if the file can not
|
|
363 * be opened, or if it contains invalid XML).
|
|
364 */
|
10425
|
365 static void
|
|
366 load_statuses(void)
|
10418
|
367 {
|
|
368 xmlnode *statuses, *status;
|
|
369
|
10426
|
370 statuses_loaded = TRUE;
|
|
371
|
10425
|
372 statuses = gaim_util_read_xml_from_file("status.xml", _("saved statuses"));
|
10418
|
373
|
|
374 if (statuses == NULL)
|
10425
|
375 return;
|
10418
|
376
|
|
377 for (status = xmlnode_get_child(statuses, "status"); status != NULL;
|
|
378 status = xmlnode_get_next_twin(status))
|
|
379 {
|
10419
|
380 GaimSavedStatus *new;
|
10418
|
381 new = parse_status(status);
|
|
382 saved_statuses = g_list_append(saved_statuses, new);
|
|
383 }
|
|
384
|
|
385 xmlnode_free(statuses);
|
|
386 }
|
|
387
|
|
388
|
|
389 /**************************************************************************
|
|
390 * Saved status API
|
|
391 **************************************************************************/
|
10419
|
392 GaimSavedStatus *
|
|
393 gaim_savedstatus_new(const char *title, GaimStatusPrimitive type)
|
10418
|
394 {
|
10419
|
395 GaimSavedStatus *status;
|
10418
|
396
|
10420
|
397 g_return_val_if_fail(gaim_savedstatus_find(title) == NULL, NULL);
|
|
398
|
10419
|
399 status = g_new0(GaimSavedStatus, 1);
|
10418
|
400 status->title = g_strdup(title);
|
|
401 status->type = type;
|
|
402
|
|
403 saved_statuses = g_list_append(saved_statuses, status);
|
|
404
|
|
405 schedule_save();
|
|
406
|
|
407 return status;
|
|
408 }
|
|
409
|
10420
|
410 void
|
11651
|
411 gaim_savedstatus_set_type(GaimSavedStatus *status, GaimStatusPrimitive type)
|
|
412 {
|
|
413 g_return_if_fail(status != NULL);
|
|
414
|
|
415 status->type = type;
|
|
416
|
|
417 schedule_save();
|
|
418 }
|
|
419
|
|
420 void
|
10420
|
421 gaim_savedstatus_set_message(GaimSavedStatus *status, const char *message)
|
|
422 {
|
|
423 g_return_if_fail(status != NULL);
|
|
424
|
|
425 g_free(status->message);
|
|
426 status->message = g_strdup(message);
|
|
427
|
|
428 schedule_save();
|
|
429 }
|
|
430
|
10418
|
431 gboolean
|
10419
|
432 gaim_savedstatus_delete(const char *title)
|
10418
|
433 {
|
10419
|
434 GaimSavedStatus *status;
|
10418
|
435
|
10419
|
436 status = gaim_savedstatus_find(title);
|
10418
|
437
|
|
438 if (status == NULL)
|
|
439 return FALSE;
|
|
440
|
|
441 saved_statuses = g_list_remove(saved_statuses, status);
|
|
442 free_statussaved(status);
|
|
443
|
|
444 schedule_save();
|
|
445
|
|
446 return TRUE;
|
|
447 }
|
|
448
|
|
449 const GList *
|
|
450 gaim_savedstatuses_get_all(void)
|
|
451 {
|
|
452 return saved_statuses;
|
|
453 }
|
|
454
|
10419
|
455 GaimSavedStatus *
|
|
456 gaim_savedstatus_find(const char *title)
|
10418
|
457 {
|
|
458 GList *l;
|
10419
|
459 GaimSavedStatus *status;
|
10418
|
460
|
|
461 for (l = saved_statuses; l != NULL; l = g_list_next(l))
|
|
462 {
|
10419
|
463 status = (GaimSavedStatus *)l->data;
|
10418
|
464 if (!strcmp(status->title, title))
|
|
465 return status;
|
|
466 }
|
|
467
|
|
468 return NULL;
|
|
469 }
|
|
470
|
11651
|
471 gboolean
|
|
472 gaim_savedstatus_is_transient(const GaimSavedStatus *saved_status)
|
|
473 {
|
|
474 return saved_status->transient;
|
|
475 }
|
|
476
|
10418
|
477 const char *
|
10419
|
478 gaim_savedstatus_get_title(const GaimSavedStatus *saved_status)
|
10418
|
479 {
|
|
480 return saved_status->title;
|
|
481 }
|
|
482
|
|
483 GaimStatusPrimitive
|
10419
|
484 gaim_savedstatus_get_type(const GaimSavedStatus *saved_status)
|
10418
|
485 {
|
|
486 return saved_status->type;
|
|
487 }
|
|
488
|
|
489 const char *
|
10419
|
490 gaim_savedstatus_get_message(const GaimSavedStatus *saved_status)
|
10418
|
491 {
|
|
492 return saved_status->message;
|
|
493 }
|
|
494
|
11651
|
495 gboolean
|
|
496 gaim_savedstatus_has_substatuses(const GaimSavedStatus *saved_status)
|
|
497 {
|
|
498 return (saved_status->substatuses != NULL);
|
|
499 }
|
|
500
|
11724
|
501 void
|
|
502 gaim_savedstatus_activate(const GaimSavedStatus *saved_status)
|
|
503 {
|
11733
|
504 GList *accounts, *node;
|
11724
|
505
|
11727
|
506 g_return_if_fail(saved_status != NULL);
|
|
507
|
11724
|
508 accounts = gaim_accounts_get_all_active();
|
|
509
|
11733
|
510 for (node = accounts; node != NULL; node = node->next)
|
11724
|
511 {
|
|
512 GaimAccount *account;
|
|
513
|
11733
|
514 account = node->data;
|
11724
|
515 gaim_savedstatus_activate_for_account(saved_status, account);
|
|
516 }
|
11733
|
517
|
|
518 g_list_free(accounts);
|
11724
|
519 }
|
|
520
|
|
521 void
|
|
522 gaim_savedstatus_activate_for_account(const GaimSavedStatus *saved_status,
|
|
523 GaimAccount *account)
|
|
524 {
|
|
525 GaimStatusType *status_type;
|
|
526
|
11727
|
527 g_return_if_fail(saved_status != NULL);
|
|
528 g_return_if_fail(account != NULL);
|
|
529
|
11739
|
530 status_type = gaim_account_get_status_type_with_primitive(account, saved_status->type);
|
|
531
|
|
532 if (status_type != NULL)
|
11724
|
533 {
|
11739
|
534 if (saved_status->message != NULL)
|
|
535 gaim_account_set_status(account, gaim_status_type_get_id(status_type),
|
|
536 TRUE, "message", saved_status->message, NULL);
|
|
537 else
|
|
538 gaim_account_set_status(account, gaim_status_type_get_id(status_type),
|
|
539 TRUE, NULL);
|
11724
|
540 }
|
|
541 }
|
|
542
|
11318
|
543 void *
|
|
544 gaim_savedstatuses_get_handle(void)
|
|
545 {
|
|
546 static int handle;
|
|
547
|
|
548 return &handle;
|
|
549 }
|
|
550
|
10418
|
551 void
|
|
552 gaim_savedstatuses_init(void)
|
|
553 {
|
|
554 load_statuses();
|
|
555 }
|
|
556
|
|
557 void
|
|
558 gaim_savedstatuses_uninit(void)
|
|
559 {
|
10428
|
560 if (save_timer != 0)
|
10418
|
561 {
|
10428
|
562 gaim_timeout_remove(save_timer);
|
|
563 save_timer = 0;
|
10418
|
564 sync_statuses();
|
|
565 }
|
|
566
|
|
567 while (saved_statuses != NULL) {
|
10419
|
568 GaimSavedStatus *status = saved_statuses->data;
|
10418
|
569 saved_statuses = g_list_remove(saved_statuses, status);
|
|
570 free_statussaved(status);
|
|
571 }
|
|
572 }
|