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