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 /**
|
12327
|
35 * The maximum number of transient statuses to save. This
|
|
36 * is used during the shutdown process to clean out old
|
|
37 * transient statuses.
|
|
38 */
|
|
39 #define MAX_TRANSIENTS 5
|
|
40
|
|
41 /**
|
12125
|
42 * The information stores a snap-shot of the statuses of all
|
10418
|
43 * your accounts. Basically these are your saved away messages.
|
|
44 * There is an overall status and message that applies to
|
|
45 * all your accounts, and then each individual account can
|
|
46 * optionally have a different custom status and message.
|
|
47 *
|
|
48 * The changes to status.xml caused by the new status API
|
|
49 * are fully backward compatible. The new status API just
|
|
50 * adds the optional sub-statuses to the XML file.
|
|
51 */
|
10419
|
52 struct _GaimSavedStatus
|
10418
|
53 {
|
|
54 char *title;
|
|
55 GaimStatusPrimitive type;
|
|
56 char *message;
|
|
57
|
12125
|
58 /** The timestamp when this saved status was created. This must be unique. */
|
|
59 time_t creation_time;
|
|
60
|
|
61 time_t lastused;
|
|
62
|
10419
|
63 GList *substatuses; /**< A list of GaimSavedStatusSub's. */
|
10418
|
64 };
|
|
65
|
|
66 /*
|
|
67 * TODO: If a GaimStatusType is deleted, need to also delete any
|
10419
|
68 * associated GaimSavedStatusSub's?
|
10418
|
69 */
|
10419
|
70 struct _GaimSavedStatusSub
|
10418
|
71 {
|
|
72 GaimAccount *account;
|
|
73 const GaimStatusType *type;
|
|
74 char *message;
|
|
75 };
|
|
76
|
12125
|
77 static GList *saved_statuses = NULL;
|
|
78 static guint save_timer = 0;
|
|
79 static gboolean statuses_loaded = FALSE;
|
|
80
|
|
81 /*
|
|
82 * This hash table keeps track of which timestamps we've
|
|
83 * used so that we don't have two saved statuses with the
|
|
84 * same 'creation_time' timestamp. The 'created' timestamp
|
|
85 * is used as a unique identifier.
|
|
86 *
|
|
87 * So the key in this hash table is the creation_time and
|
|
88 * the value is a pointer to the GaimSavedStatus.
|
|
89 */
|
|
90 static GHashTable *creation_times;
|
10418
|
91
|
12327
|
92 static void schedule_save(void);
|
10427
|
93
|
10428
|
94 /*********************************************************************
|
|
95 * Private utility functions *
|
|
96 *********************************************************************/
|
10418
|
97
|
|
98 static void
|
10419
|
99 free_statussavedsub(GaimSavedStatusSub *substatus)
|
10418
|
100 {
|
|
101 g_return_if_fail(substatus != NULL);
|
|
102
|
|
103 g_free(substatus->message);
|
|
104 g_free(substatus);
|
|
105 }
|
|
106
|
|
107 static void
|
10419
|
108 free_statussaved(GaimSavedStatus *status)
|
10418
|
109 {
|
|
110 g_return_if_fail(status != NULL);
|
|
111
|
|
112 g_free(status->title);
|
|
113 g_free(status->message);
|
|
114
|
|
115 while (status->substatuses != NULL)
|
|
116 {
|
10419
|
117 GaimSavedStatusSub *substatus = status->substatuses->data;
|
10418
|
118 status->substatuses = g_list_remove(status->substatuses, substatus);
|
|
119 free_statussavedsub(substatus);
|
|
120 }
|
|
121
|
|
122 g_free(status);
|
|
123 }
|
|
124
|
12125
|
125 /*
|
|
126 * Set the timestamp for when this saved status was created, and
|
|
127 * make sure it is unique.
|
|
128 */
|
|
129 static void
|
|
130 set_creation_time(GaimSavedStatus *status, time_t creation_time)
|
|
131 {
|
|
132 g_return_if_fail(status != NULL);
|
|
133
|
|
134 /* Avoid using 0 because it's an invalid hash key */
|
|
135 status->creation_time = creation_time != 0 ? creation_time : 1;
|
|
136
|
|
137 while (g_hash_table_lookup(creation_times, &status->creation_time) != NULL)
|
|
138 status->creation_time++;
|
|
139
|
|
140 g_hash_table_insert(creation_times,
|
|
141 &status->creation_time,
|
|
142 status);
|
|
143 }
|
|
144
|
12327
|
145 static gint
|
|
146 compare_times(gconstpointer a, gconstpointer b)
|
|
147 {
|
|
148 const time_t timea = *((time_t *)a);
|
|
149 const time_t timeb = *((time_t *)b);
|
|
150 if (timea > timeb)
|
|
151 return -1;
|
|
152 if (timea < timeb)
|
|
153 return 1;
|
|
154 return 0;
|
|
155 }
|
|
156
|
|
157 /**
|
|
158 * Transient statuses are added and removed automatically by
|
|
159 * Gaim. If they're not used for a certain length of time then
|
|
160 * they'll expire and be automatically removed. This function
|
|
161 * does the expiration.
|
|
162 */
|
|
163 static void
|
|
164 remove_old_transient_statuses()
|
|
165 {
|
|
166 GList *lastused;
|
|
167 time_t threshold;
|
|
168 time_t creation_time;
|
|
169 GList *l, *next;
|
|
170 GaimSavedStatus *saved_status;
|
|
171 int i;
|
|
172
|
|
173 /* Construct a GList containing the lastused times from each transient status */
|
|
174 lastused = NULL;
|
|
175 for (l = saved_statuses; l != NULL; l = l->next)
|
|
176 {
|
|
177 saved_status = l->data;
|
|
178 if (gaim_savedstatus_is_transient(saved_status))
|
|
179 lastused = g_list_insert_sorted(lastused, &saved_status->lastused, compare_times);
|
|
180 }
|
|
181
|
|
182 /* Find the 5th most recently used transient status */
|
|
183 l = lastused;
|
|
184 i = 0;
|
|
185 while ((l != NULL) && (i < MAX_TRANSIENTS))
|
|
186 {
|
|
187 l = l->next;
|
|
188 i++;
|
|
189 }
|
|
190 if (l != NULL)
|
|
191 threshold = *((time_t *)l->data);
|
|
192 else
|
|
193 threshold = 0;
|
|
194 g_list_free(lastused);
|
|
195
|
|
196 if (threshold == 0)
|
|
197 /* We have 5 or fewer transient statuses, so there is nothing to delete */
|
|
198 return;
|
|
199
|
|
200 /* Delete all transient statuses older than the threshold */
|
|
201 for (l = saved_statuses; l != NULL; l = next)
|
|
202 {
|
|
203 next = l->next;
|
|
204 saved_status = l->data;
|
|
205 if (gaim_savedstatus_is_transient(saved_status) && (saved_status->lastused <= threshold))
|
|
206 {
|
|
207 saved_statuses = g_list_remove(saved_statuses, saved_status);
|
|
208 creation_time = gaim_savedstatus_get_creation_time(saved_status);
|
|
209 g_hash_table_remove(creation_times, &creation_time);
|
|
210 free_statussaved(saved_status);
|
|
211 }
|
|
212 }
|
|
213 schedule_save();
|
|
214 }
|
|
215
|
10428
|
216 /*********************************************************************
|
10429
|
217 * Writing to disk *
|
10428
|
218 *********************************************************************/
|
10418
|
219
|
|
220 static xmlnode *
|
10419
|
221 substatus_to_xmlnode(GaimSavedStatusSub *substatus)
|
10418
|
222 {
|
|
223 xmlnode *node, *child;
|
|
224
|
|
225 node = xmlnode_new("substatus");
|
|
226
|
10424
|
227 child = xmlnode_new_child(node, "account");
|
|
228 xmlnode_set_attrib(child, "protocol", gaim_account_get_protocol_id(substatus->account));
|
|
229 xmlnode_insert_data(child, gaim_account_get_username(substatus->account), -1);
|
10418
|
230
|
10424
|
231 child = xmlnode_new_child(node, "state");
|
10418
|
232 xmlnode_insert_data(child, gaim_status_type_get_id(substatus->type), -1);
|
|
233
|
|
234 if (substatus->message != NULL)
|
|
235 {
|
10424
|
236 child = xmlnode_new_child(node, "message");
|
10418
|
237 xmlnode_insert_data(child, substatus->message, -1);
|
|
238 }
|
|
239
|
|
240 return node;
|
|
241 }
|
|
242
|
|
243 static xmlnode *
|
10419
|
244 status_to_xmlnode(GaimSavedStatus *status)
|
10418
|
245 {
|
|
246 xmlnode *node, *child;
|
12125
|
247 char buf[21];
|
10418
|
248 GList *cur;
|
|
249
|
12125
|
250 node = xmlnode_new("status");
|
|
251 if (status->title != NULL)
|
12283
|
252 {
|
12125
|
253 xmlnode_set_attrib(node, "name", status->title);
|
12283
|
254 }
|
|
255 else
|
|
256 {
|
|
257 /*
|
|
258 * Gaim 1.5.0 and earlier require a name to be set, so we
|
|
259 * do this little hack to maintain backward compatability
|
12309
|
260 * in the status.xml file. Eventually this should be removed
|
12283
|
261 * and we should determine if a status is transient by
|
|
262 * whether the "name" attribute is set to something or if
|
|
263 * it does not exist at all.
|
|
264 */
|
|
265 xmlnode_set_attrib(node, "name", "Auto-Cached");
|
|
266 xmlnode_set_attrib(node, "transient", "true");
|
|
267 }
|
11651
|
268
|
12125
|
269 snprintf(buf, sizeof(buf), "%lu", status->creation_time);
|
|
270 xmlnode_set_attrib(node, "created", buf);
|
|
271
|
|
272 snprintf(buf, sizeof(buf), "%lu", status->lastused);
|
|
273 xmlnode_set_attrib(node, "lastused", buf);
|
10418
|
274
|
10424
|
275 child = xmlnode_new_child(node, "state");
|
|
276 xmlnode_insert_data(child, gaim_primitive_get_id_from_type(status->type), -1);
|
10418
|
277
|
11651
|
278 if (status->message != NULL)
|
|
279 {
|
|
280 child = xmlnode_new_child(node, "message");
|
|
281 xmlnode_insert_data(child, status->message, -1);
|
|
282 }
|
10418
|
283
|
|
284 for (cur = status->substatuses; cur != NULL; cur = cur->next)
|
|
285 {
|
|
286 child = substatus_to_xmlnode(cur->data);
|
|
287 xmlnode_insert_child(node, child);
|
|
288 }
|
|
289
|
|
290 return node;
|
|
291 }
|
|
292
|
|
293 static xmlnode *
|
|
294 statuses_to_xmlnode(void)
|
|
295 {
|
|
296 xmlnode *node, *child;
|
|
297 GList *cur;
|
|
298
|
|
299 node = xmlnode_new("statuses");
|
10423
|
300 xmlnode_set_attrib(node, "version", "1.0");
|
10418
|
301
|
|
302 for (cur = saved_statuses; cur != NULL; cur = cur->next)
|
|
303 {
|
|
304 child = status_to_xmlnode(cur->data);
|
|
305 xmlnode_insert_child(node, child);
|
|
306 }
|
|
307
|
|
308 return node;
|
|
309 }
|
|
310
|
|
311 static void
|
|
312 sync_statuses(void)
|
|
313 {
|
10423
|
314 xmlnode *node;
|
10418
|
315 char *data;
|
|
316
|
10428
|
317 if (!statuses_loaded)
|
|
318 {
|
10418
|
319 gaim_debug_error("status", "Attempted to save statuses before they "
|
|
320 "were read!\n");
|
|
321 return;
|
|
322 }
|
|
323
|
10423
|
324 node = statuses_to_xmlnode();
|
|
325 data = xmlnode_to_formatted_str(node, NULL);
|
10418
|
326 gaim_util_write_data_to_file("status.xml", data, -1);
|
|
327 g_free(data);
|
10423
|
328 xmlnode_free(node);
|
10418
|
329 }
|
|
330
|
|
331 static gboolean
|
10428
|
332 save_cb(gpointer data)
|
10418
|
333 {
|
|
334 sync_statuses();
|
10428
|
335 save_timer = 0;
|
10418
|
336 return FALSE;
|
|
337 }
|
|
338
|
|
339 static void
|
|
340 schedule_save(void)
|
|
341 {
|
10428
|
342 if (save_timer == 0)
|
|
343 save_timer = gaim_timeout_add(5000, save_cb, NULL);
|
10418
|
344 }
|
|
345
|
|
346
|
10428
|
347 /*********************************************************************
|
|
348 * Reading from disk *
|
|
349 *********************************************************************/
|
|
350
|
10419
|
351 static GaimSavedStatusSub *
|
10418
|
352 parse_substatus(xmlnode *substatus)
|
|
353 {
|
10419
|
354 GaimSavedStatusSub *ret;
|
10418
|
355 xmlnode *node;
|
10425
|
356 char *data;
|
10418
|
357
|
10419
|
358 ret = g_new0(GaimSavedStatusSub, 1);
|
10418
|
359
|
|
360 /* Read the account */
|
|
361 node = xmlnode_get_child(substatus, "account");
|
|
362 if (node != NULL)
|
|
363 {
|
|
364 char *acct_name;
|
|
365 const char *protocol;
|
|
366 acct_name = xmlnode_get_data(node);
|
|
367 protocol = xmlnode_get_attrib(node, "protocol");
|
|
368 if ((acct_name != NULL) && (protocol != NULL))
|
|
369 ret->account = gaim_accounts_find(acct_name, protocol);
|
|
370 g_free(acct_name);
|
|
371 }
|
|
372
|
|
373 if (ret->account == NULL)
|
|
374 {
|
|
375 g_free(ret);
|
|
376 return NULL;
|
|
377 }
|
|
378
|
|
379 /* Read the state */
|
|
380 node = xmlnode_get_child(substatus, "state");
|
10426
|
381 if ((node != NULL) && ((data = xmlnode_get_data(node)) != NULL))
|
10425
|
382 {
|
10418
|
383 ret->type = gaim_status_type_find_with_id(
|
10425
|
384 ret->account->status_types, data);
|
10418
|
385 g_free(data);
|
|
386 }
|
|
387
|
|
388 /* Read the message */
|
|
389 node = xmlnode_get_child(substatus, "message");
|
10426
|
390 if ((node != NULL) && ((data = xmlnode_get_data(node)) != NULL))
|
10425
|
391 {
|
10418
|
392 ret->message = data;
|
10425
|
393 }
|
10418
|
394
|
|
395 return ret;
|
|
396 }
|
|
397
|
|
398 /**
|
|
399 * Parse a saved status and add it to the saved_statuses linked list.
|
|
400 *
|
|
401 * Here's an example of the XML for a saved status:
|
|
402 * <status name="Girls">
|
|
403 * <state>away</state>
|
|
404 * <message>I like the way that they walk
|
|
405 * And it's chill to hear them talk
|
|
406 * And I can always make them smile
|
|
407 * From White Castle to the Nile</message>
|
|
408 * <substatus>
|
|
409 * <account protocol='prpl-oscar'>markdoliner</account>
|
|
410 * <state>available</state>
|
|
411 * <message>The ladies man is here to answer your queries.</message>
|
|
412 * </substatus>
|
|
413 * <substatus>
|
|
414 * <account protocol='prpl-oscar'>giantgraypanda</account>
|
|
415 * <state>away</state>
|
|
416 * <message>A.C. ain't in charge no more.</message>
|
|
417 * </substatus>
|
|
418 * </status>
|
|
419 *
|
|
420 * I know. Moving, huh?
|
|
421 */
|
10419
|
422 static GaimSavedStatus *
|
10418
|
423 parse_status(xmlnode *status)
|
|
424 {
|
10419
|
425 GaimSavedStatus *ret;
|
10418
|
426 xmlnode *node;
|
|
427 const char *attrib;
|
10425
|
428 char *data;
|
10418
|
429 int i;
|
|
430
|
10419
|
431 ret = g_new0(GaimSavedStatus, 1);
|
10418
|
432
|
12283
|
433 attrib = xmlnode_get_attrib(status, "transient");
|
|
434 if ((attrib == NULL) || (strcmp(attrib, "true")))
|
|
435 {
|
|
436 /* Read the title */
|
|
437 attrib = xmlnode_get_attrib(status, "name");
|
|
438 ret->title = g_strdup(attrib);
|
|
439 }
|
12125
|
440
|
|
441 if (ret->title != NULL)
|
10418
|
442 {
|
12125
|
443 /* Ensure the title is unique */
|
|
444 i = 2;
|
|
445 while (gaim_savedstatus_find(ret->title) != NULL)
|
|
446 {
|
|
447 g_free(ret->title);
|
|
448 ret->title = g_strdup_printf("%s %d", attrib, i);
|
|
449 i++;
|
|
450 }
|
10418
|
451 }
|
|
452
|
12125
|
453 /* Read the creation time */
|
|
454 attrib = xmlnode_get_attrib(status, "created");
|
|
455 set_creation_time(ret, (attrib != NULL ? atol(attrib) : 0));
|
|
456
|
|
457 /* Read the last used time */
|
|
458 attrib = xmlnode_get_attrib(status, "lastused");
|
|
459 ret->lastused = (attrib != NULL ? atol(attrib) : 0);
|
|
460
|
10418
|
461 /* Read the primitive status type */
|
|
462 node = xmlnode_get_child(status, "state");
|
10426
|
463 if ((node != NULL) && ((data = xmlnode_get_data(node)) != NULL))
|
10425
|
464 {
|
10419
|
465 ret->type = gaim_primitive_get_type_from_id(data);
|
10418
|
466 g_free(data);
|
|
467 }
|
|
468
|
|
469 /* Read the message */
|
|
470 node = xmlnode_get_child(status, "message");
|
10426
|
471 if ((node != NULL) && ((data = xmlnode_get_data(node)) != NULL))
|
10425
|
472 {
|
10418
|
473 ret->message = data;
|
10425
|
474 }
|
10418
|
475
|
|
476 /* Read substatuses */
|
12056
|
477 for (node = xmlnode_get_child(status, "substatus"); node != NULL;
|
10418
|
478 node = xmlnode_get_next_twin(node))
|
|
479 {
|
10419
|
480 GaimSavedStatusSub *new;
|
10418
|
481 new = parse_substatus(node);
|
|
482 if (new != NULL)
|
12056
|
483 ret->substatuses = g_list_prepend(ret->substatuses, new);
|
10418
|
484 }
|
|
485
|
|
486 return ret;
|
|
487 }
|
|
488
|
|
489 /**
|
|
490 * Read the saved statuses from a file in the Gaim user dir.
|
|
491 *
|
|
492 * @return TRUE on success, FALSE on failure (if the file can not
|
|
493 * be opened, or if it contains invalid XML).
|
|
494 */
|
10425
|
495 static void
|
|
496 load_statuses(void)
|
10418
|
497 {
|
|
498 xmlnode *statuses, *status;
|
|
499
|
10426
|
500 statuses_loaded = TRUE;
|
|
501
|
10425
|
502 statuses = gaim_util_read_xml_from_file("status.xml", _("saved statuses"));
|
10418
|
503
|
|
504 if (statuses == NULL)
|
10425
|
505 return;
|
10418
|
506
|
|
507 for (status = xmlnode_get_child(statuses, "status"); status != NULL;
|
|
508 status = xmlnode_get_next_twin(status))
|
|
509 {
|
10419
|
510 GaimSavedStatus *new;
|
10418
|
511 new = parse_status(status);
|
12056
|
512 saved_statuses = g_list_prepend(saved_statuses, new);
|
10418
|
513 }
|
|
514
|
|
515 xmlnode_free(statuses);
|
|
516 }
|
|
517
|
|
518
|
|
519 /**************************************************************************
|
|
520 * Saved status API
|
|
521 **************************************************************************/
|
10419
|
522 GaimSavedStatus *
|
|
523 gaim_savedstatus_new(const char *title, GaimStatusPrimitive type)
|
10418
|
524 {
|
10419
|
525 GaimSavedStatus *status;
|
10418
|
526
|
12056
|
527 /* Make sure we don't already have a saved status with this title. */
|
12125
|
528 if (title != NULL)
|
|
529 g_return_val_if_fail(gaim_savedstatus_find(title) == NULL, NULL);
|
10420
|
530
|
10419
|
531 status = g_new0(GaimSavedStatus, 1);
|
10418
|
532 status->title = g_strdup(title);
|
|
533 status->type = type;
|
12125
|
534 set_creation_time(status, time(NULL));
|
10418
|
535
|
12056
|
536 saved_statuses = g_list_prepend(saved_statuses, status);
|
10418
|
537
|
|
538 schedule_save();
|
|
539
|
|
540 return status;
|
|
541 }
|
|
542
|
10420
|
543 void
|
12056
|
544 gaim_savedstatus_set_title(GaimSavedStatus *status, const char *title)
|
|
545 {
|
|
546 g_return_if_fail(status != NULL);
|
|
547
|
|
548 /* Make sure we don't already have a saved status with this title. */
|
|
549 g_return_if_fail(gaim_savedstatus_find(title) == NULL);
|
|
550
|
|
551 g_free(status->title);
|
|
552 status->title = g_strdup(title);
|
|
553
|
|
554 schedule_save();
|
|
555 }
|
|
556
|
|
557 void
|
11651
|
558 gaim_savedstatus_set_type(GaimSavedStatus *status, GaimStatusPrimitive type)
|
|
559 {
|
|
560 g_return_if_fail(status != NULL);
|
|
561
|
|
562 status->type = type;
|
|
563
|
|
564 schedule_save();
|
|
565 }
|
|
566
|
|
567 void
|
10420
|
568 gaim_savedstatus_set_message(GaimSavedStatus *status, const char *message)
|
|
569 {
|
|
570 g_return_if_fail(status != NULL);
|
|
571
|
|
572 g_free(status->message);
|
|
573 status->message = g_strdup(message);
|
|
574
|
|
575 schedule_save();
|
|
576 }
|
|
577
|
12056
|
578 void
|
12080
|
579 gaim_savedstatus_set_substatus(GaimSavedStatus *saved_status,
|
|
580 const GaimAccount *account,
|
|
581 const GaimStatusType *type,
|
|
582 const char *message)
|
12056
|
583 {
|
|
584 GaimSavedStatusSub *substatus;
|
|
585
|
|
586 g_return_if_fail(saved_status != NULL);
|
|
587 g_return_if_fail(account != NULL);
|
|
588 g_return_if_fail(type != NULL);
|
|
589
|
|
590 /* Find an existing substatus or create a new one */
|
12080
|
591 substatus = gaim_savedstatus_get_substatus(saved_status, account);
|
12056
|
592 if (substatus == NULL)
|
|
593 {
|
|
594 substatus = g_new0(GaimSavedStatusSub, 1);
|
|
595 substatus->account = (GaimAccount *)account;
|
|
596 saved_status->substatuses = g_list_prepend(saved_status->substatuses, substatus);
|
|
597 }
|
|
598
|
|
599 substatus->type = type;
|
|
600 g_free(substatus->message);
|
|
601 substatus->message = g_strdup(message);
|
|
602
|
|
603 schedule_save();
|
|
604 }
|
|
605
|
|
606 void
|
12080
|
607 gaim_savedstatus_unset_substatus(GaimSavedStatus *saved_status,
|
|
608 const GaimAccount *account)
|
12056
|
609 {
|
|
610 GList *iter;
|
|
611 GaimSavedStatusSub *substatus;
|
|
612
|
|
613 g_return_if_fail(saved_status != NULL);
|
|
614 g_return_if_fail(account != NULL);
|
|
615
|
|
616 for (iter = saved_status->substatuses; iter != NULL; iter = iter->next)
|
|
617 {
|
|
618 substatus = iter->data;
|
|
619 if (substatus->account == account)
|
|
620 {
|
|
621 saved_status->substatuses = g_list_delete_link(saved_status->substatuses, iter);
|
|
622 g_free(substatus->message);
|
|
623 g_free(substatus);
|
|
624 return;
|
|
625 }
|
|
626 }
|
|
627 }
|
|
628
|
10418
|
629 gboolean
|
10419
|
630 gaim_savedstatus_delete(const char *title)
|
10418
|
631 {
|
10419
|
632 GaimSavedStatus *status;
|
12125
|
633 time_t creation_time, current, idleaway;
|
10418
|
634
|
10419
|
635 status = gaim_savedstatus_find(title);
|
10418
|
636
|
|
637 if (status == NULL)
|
|
638 return FALSE;
|
|
639
|
|
640 saved_statuses = g_list_remove(saved_statuses, status);
|
12125
|
641 creation_time = gaim_savedstatus_get_creation_time(status);
|
|
642 g_hash_table_remove(creation_times, &creation_time);
|
10418
|
643 free_statussaved(status);
|
|
644
|
|
645 schedule_save();
|
|
646
|
12125
|
647 /*
|
|
648 * If we just deleted our current status or our idleaway status,
|
|
649 * then set the appropriate pref back to 0.
|
|
650 */
|
|
651 current = gaim_prefs_get_int("/core/savedstatus/current");
|
|
652 if (current == creation_time)
|
|
653 gaim_prefs_set_int("/core/savedstatus/current", 0);
|
|
654
|
|
655 idleaway = gaim_prefs_get_int("/core/savedstatus/idleaway");
|
|
656 if (idleaway == creation_time)
|
|
657 gaim_prefs_set_int("/core/savedstatus/idleaway", 0);
|
|
658
|
10418
|
659 return TRUE;
|
|
660 }
|
|
661
|
|
662 const GList *
|
|
663 gaim_savedstatuses_get_all(void)
|
|
664 {
|
|
665 return saved_statuses;
|
|
666 }
|
|
667
|
10419
|
668 GaimSavedStatus *
|
12125
|
669 gaim_savedstatus_get_current()
|
|
670 {
|
|
671 int creation_time;
|
12197
|
672 GaimSavedStatus *saved_status = NULL;
|
12125
|
673
|
|
674 creation_time = gaim_prefs_get_int("/core/savedstatus/current");
|
|
675
|
12197
|
676 if (creation_time != 0)
|
|
677 saved_status = g_hash_table_lookup(creation_times, &creation_time);
|
|
678
|
|
679 if (saved_status == NULL)
|
12125
|
680 {
|
|
681 /*
|
|
682 * We don't have a current saved statuses! This is either a new
|
12197
|
683 * Gaim user or someone upgrading from Gaim 1.5.0 or older, or
|
|
684 * possibly someone who deleted the status they were currently
|
|
685 * using? In any case, add a default status.
|
12125
|
686 */
|
|
687 saved_status = gaim_savedstatus_new(NULL, GAIM_STATUS_AVAILABLE);
|
|
688 }
|
|
689
|
|
690 return saved_status;
|
|
691 }
|
|
692
|
|
693 GaimSavedStatus *
|
|
694 gaim_savedstatus_get_idleaway()
|
|
695 {
|
|
696 int creation_time;
|
|
697 GaimSavedStatus *saved_status;
|
|
698
|
|
699 creation_time = gaim_prefs_get_int("/core/savedstatus/idleaway");
|
|
700
|
|
701 if (creation_time == 0)
|
|
702 {
|
|
703 /*
|
|
704 * We don't have a current saved statuses! This is either a new
|
|
705 * Gaim user or someone upgrading from Gaim 1.5.0 or older. Add
|
|
706 * a default status.
|
|
707 */
|
|
708 saved_status = gaim_savedstatus_new(NULL, GAIM_STATUS_AWAY);
|
|
709 gaim_savedstatus_set_message(saved_status, _("I'm not here right now"));
|
|
710 }
|
|
711 else
|
|
712 {
|
|
713 saved_status = g_hash_table_lookup(creation_times, &creation_time);
|
|
714 }
|
|
715
|
|
716 return saved_status;
|
|
717 }
|
|
718
|
|
719 GaimSavedStatus *
|
10419
|
720 gaim_savedstatus_find(const char *title)
|
10418
|
721 {
|
12056
|
722 GList *iter;
|
10419
|
723 GaimSavedStatus *status;
|
10418
|
724
|
11977
|
725 g_return_val_if_fail(title != NULL, NULL);
|
|
726
|
12056
|
727 for (iter = saved_statuses; iter != NULL; iter = iter->next)
|
10418
|
728 {
|
12056
|
729 status = (GaimSavedStatus *)iter->data;
|
12125
|
730 if ((status->title != NULL) && !strcmp(status->title, title))
|
10418
|
731 return status;
|
|
732 }
|
|
733
|
|
734 return NULL;
|
|
735 }
|
|
736
|
11651
|
737 gboolean
|
|
738 gaim_savedstatus_is_transient(const GaimSavedStatus *saved_status)
|
|
739 {
|
12197
|
740 g_return_val_if_fail(saved_status != NULL, TRUE);
|
|
741
|
12125
|
742 return (saved_status->title == NULL);
|
11651
|
743 }
|
|
744
|
10418
|
745 const char *
|
10419
|
746 gaim_savedstatus_get_title(const GaimSavedStatus *saved_status)
|
10418
|
747 {
|
12197
|
748 g_return_val_if_fail(saved_status != NULL, NULL);
|
|
749
|
10418
|
750 return saved_status->title;
|
|
751 }
|
|
752
|
|
753 GaimStatusPrimitive
|
10419
|
754 gaim_savedstatus_get_type(const GaimSavedStatus *saved_status)
|
10418
|
755 {
|
12197
|
756 g_return_val_if_fail(saved_status != NULL, GAIM_STATUS_OFFLINE);
|
|
757
|
10418
|
758 return saved_status->type;
|
|
759 }
|
|
760
|
|
761 const char *
|
10419
|
762 gaim_savedstatus_get_message(const GaimSavedStatus *saved_status)
|
10418
|
763 {
|
12197
|
764 g_return_val_if_fail(saved_status != NULL, NULL);
|
|
765
|
10418
|
766 return saved_status->message;
|
|
767 }
|
|
768
|
12125
|
769 time_t
|
|
770 gaim_savedstatus_get_creation_time(const GaimSavedStatus *saved_status)
|
|
771 {
|
12197
|
772 g_return_val_if_fail(saved_status != NULL, 0);
|
|
773
|
12125
|
774 return saved_status->creation_time;
|
|
775 }
|
|
776
|
11651
|
777 gboolean
|
|
778 gaim_savedstatus_has_substatuses(const GaimSavedStatus *saved_status)
|
|
779 {
|
12197
|
780 g_return_val_if_fail(saved_status != NULL, FALSE);
|
|
781
|
11651
|
782 return (saved_status->substatuses != NULL);
|
|
783 }
|
|
784
|
12056
|
785 GaimSavedStatusSub *
|
12080
|
786 gaim_savedstatus_get_substatus(const GaimSavedStatus *saved_status,
|
|
787 const GaimAccount *account)
|
12056
|
788 {
|
|
789 GList *iter;
|
|
790 GaimSavedStatusSub *substatus;
|
|
791
|
|
792 g_return_val_if_fail(saved_status != NULL, NULL);
|
|
793 g_return_val_if_fail(account != NULL, NULL);
|
|
794
|
|
795 for (iter = saved_status->substatuses; iter != NULL; iter = iter->next)
|
|
796 {
|
|
797 substatus = iter->data;
|
|
798 if (substatus->account == account)
|
|
799 return substatus;
|
|
800 }
|
|
801
|
|
802 return NULL;
|
|
803 }
|
|
804
|
|
805 const GaimStatusType *
|
|
806 gaim_savedstatus_substatus_get_type(const GaimSavedStatusSub *substatus)
|
|
807 {
|
|
808 g_return_val_if_fail(substatus != NULL, NULL);
|
|
809
|
|
810 return substatus->type;
|
|
811 }
|
|
812
|
|
813 const char *
|
|
814 gaim_savedstatus_substatus_get_message(const GaimSavedStatusSub *substatus)
|
|
815 {
|
|
816 g_return_val_if_fail(substatus != NULL, NULL);
|
|
817
|
|
818 return substatus->message;
|
|
819 }
|
|
820
|
11724
|
821 void
|
12125
|
822 gaim_savedstatus_activate(GaimSavedStatus *saved_status)
|
11724
|
823 {
|
11733
|
824 GList *accounts, *node;
|
11724
|
825
|
11727
|
826 g_return_if_fail(saved_status != NULL);
|
|
827
|
11724
|
828 accounts = gaim_accounts_get_all_active();
|
|
829
|
11733
|
830 for (node = accounts; node != NULL; node = node->next)
|
11724
|
831 {
|
|
832 GaimAccount *account;
|
|
833
|
11733
|
834 account = node->data;
|
11724
|
835 gaim_savedstatus_activate_for_account(saved_status, account);
|
|
836 }
|
11733
|
837
|
|
838 g_list_free(accounts);
|
11954
|
839
|
12125
|
840 saved_status->lastused = time(NULL);
|
|
841 gaim_prefs_set_int("/core/savedstatus/current",
|
|
842 gaim_savedstatus_get_creation_time(saved_status));
|
11724
|
843 }
|
|
844
|
|
845 void
|
|
846 gaim_savedstatus_activate_for_account(const GaimSavedStatus *saved_status,
|
|
847 GaimAccount *account)
|
|
848 {
|
12056
|
849 const GaimStatusType *status_type;
|
|
850 const GaimSavedStatusSub *substatus;
|
|
851 const char *message = NULL;
|
11724
|
852
|
11727
|
853 g_return_if_fail(saved_status != NULL);
|
|
854 g_return_if_fail(account != NULL);
|
|
855
|
12080
|
856 substatus = gaim_savedstatus_get_substatus(saved_status, account);
|
12056
|
857 if (substatus != NULL)
|
|
858 {
|
|
859 status_type = substatus->type;
|
|
860 message = substatus->message;
|
|
861 }
|
|
862 else
|
11724
|
863 {
|
12056
|
864 status_type = gaim_account_get_status_type_with_primitive(account, saved_status->type);
|
|
865 if (status_type == NULL)
|
|
866 return;
|
|
867 message = saved_status->message;
|
|
868 }
|
|
869
|
|
870 if ((message != NULL) &&
|
|
871 (gaim_status_type_get_attr(status_type, "message")))
|
|
872 {
|
|
873 gaim_account_set_status(account, gaim_status_type_get_id(status_type),
|
|
874 TRUE, "message", message, NULL);
|
|
875 }
|
|
876 else
|
|
877 {
|
|
878 gaim_account_set_status(account, gaim_status_type_get_id(status_type),
|
|
879 TRUE, NULL);
|
11724
|
880 }
|
|
881 }
|
|
882
|
11318
|
883 void *
|
|
884 gaim_savedstatuses_get_handle(void)
|
|
885 {
|
|
886 static int handle;
|
|
887
|
|
888 return &handle;
|
|
889 }
|
|
890
|
10418
|
891 void
|
|
892 gaim_savedstatuses_init(void)
|
|
893 {
|
12125
|
894 creation_times = g_hash_table_new(g_int_hash, g_int_equal);
|
11975
|
895
|
12125
|
896 /*
|
|
897 * Using 0 as the creation_time is a special case.
|
|
898 * If someone calls gaim_savedstatus_get_current() or
|
|
899 * gaim_savedstatus_get_idleaway() and either of those functions
|
|
900 * sees a creation_time of 0, then it will create a default
|
|
901 * saved status and return that to the user.
|
|
902 */
|
|
903 gaim_prefs_add_none("/core/savedstatus");
|
|
904 gaim_prefs_add_int("/core/savedstatus/current", 0);
|
|
905 gaim_prefs_add_int("/core/savedstatus/idleaway", 0);
|
11975
|
906
|
12125
|
907 load_statuses();
|
10418
|
908 }
|
|
909
|
|
910 void
|
|
911 gaim_savedstatuses_uninit(void)
|
|
912 {
|
12327
|
913 remove_old_transient_statuses();
|
|
914
|
10428
|
915 if (save_timer != 0)
|
10418
|
916 {
|
10428
|
917 gaim_timeout_remove(save_timer);
|
|
918 save_timer = 0;
|
10418
|
919 sync_statuses();
|
|
920 }
|
|
921
|
|
922 while (saved_statuses != NULL) {
|
12056
|
923 GaimSavedStatus *saved_status = saved_statuses->data;
|
|
924 saved_statuses = g_list_remove(saved_statuses, saved_status);
|
|
925 free_statussaved(saved_status);
|
10418
|
926 }
|
12125
|
927
|
|
928 g_hash_table_destroy(creation_times);
|
10418
|
929 }
|
12056
|
930
|