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