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