Mercurial > pidgin.yaz
annotate src/savedstatuses.c @ 13737:2b9aa05b6fa1
[gaim-migrate @ 16148]
Don't set the statusbox icon before it's created
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Sun, 07 May 2006 21:19:46 +0000 |
parents | c4be649e2aca |
children | ec858948d292 |
rev | line source |
---|---|
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 /** | |
13681 | 42 * The default message to use when the user becomes auto-away. |
43 */ | |
44 #define DEFAULT_AUTOAWAY_MESSAGE _("I'm not here right now") | |
45 | |
46 /** | |
12125 | 47 * The information stores a snap-shot of the statuses of all |
10418 | 48 * your accounts. Basically these are your saved away messages. |
49 * There is an overall status and message that applies to | |
50 * all your accounts, and then each individual account can | |
51 * optionally have a different custom status and message. | |
52 * | |
53 * The changes to status.xml caused by the new status API | |
54 * are fully backward compatible. The new status API just | |
55 * adds the optional sub-statuses to the XML file. | |
56 */ | |
10419 | 57 struct _GaimSavedStatus |
10418 | 58 { |
59 char *title; | |
60 GaimStatusPrimitive type; | |
61 char *message; | |
62 | |
12125 | 63 /** The timestamp when this saved status was created. This must be unique. */ |
64 time_t creation_time; | |
65 | |
66 time_t lastused; | |
67 | |
12688 | 68 unsigned int usage_count; |
69 | |
10419 | 70 GList *substatuses; /**< A list of GaimSavedStatusSub's. */ |
10418 | 71 }; |
72 | |
73 /* | |
74 * TODO: If a GaimStatusType is deleted, need to also delete any | |
10419 | 75 * associated GaimSavedStatusSub's? |
10418 | 76 */ |
10419 | 77 struct _GaimSavedStatusSub |
10418 | 78 { |
79 GaimAccount *account; | |
80 const GaimStatusType *type; | |
81 char *message; | |
82 }; | |
83 | |
12125 | 84 static GList *saved_statuses = NULL; |
85 static guint save_timer = 0; | |
86 static gboolean statuses_loaded = FALSE; | |
87 | |
88 /* | |
89 * This hash table keeps track of which timestamps we've | |
90 * used so that we don't have two saved statuses with the | |
91 * same 'creation_time' timestamp. The 'created' timestamp | |
92 * is used as a unique identifier. | |
93 * | |
94 * So the key in this hash table is the creation_time and | |
95 * the value is a pointer to the GaimSavedStatus. | |
96 */ | |
97 static GHashTable *creation_times; | |
10418 | 98 |
12327 | 99 static void schedule_save(void); |
10427 | 100 |
10428 | 101 /********************************************************************* |
102 * Private utility functions * | |
103 *********************************************************************/ | |
10418 | 104 |
105 static void | |
12724 | 106 free_saved_status_sub(GaimSavedStatusSub *substatus) |
10418 | 107 { |
108 g_return_if_fail(substatus != NULL); | |
109 | |
110 g_free(substatus->message); | |
111 g_free(substatus); | |
112 } | |
113 | |
114 static void | |
12724 | 115 free_saved_status(GaimSavedStatus *status) |
10418 | 116 { |
117 g_return_if_fail(status != NULL); | |
118 | |
119 g_free(status->title); | |
120 g_free(status->message); | |
121 | |
122 while (status->substatuses != NULL) | |
123 { | |
10419 | 124 GaimSavedStatusSub *substatus = status->substatuses->data; |
10418 | 125 status->substatuses = g_list_remove(status->substatuses, substatus); |
12724 | 126 free_saved_status_sub(substatus); |
10418 | 127 } |
128 | |
129 g_free(status); | |
130 } | |
131 | |
12125 | 132 /* |
133 * Set the timestamp for when this saved status was created, and | |
134 * make sure it is unique. | |
135 */ | |
136 static void | |
137 set_creation_time(GaimSavedStatus *status, time_t creation_time) | |
138 { | |
139 g_return_if_fail(status != NULL); | |
140 | |
141 /* Avoid using 0 because it's an invalid hash key */ | |
142 status->creation_time = creation_time != 0 ? creation_time : 1; | |
143 | |
144 while (g_hash_table_lookup(creation_times, &status->creation_time) != NULL) | |
145 status->creation_time++; | |
146 | |
147 g_hash_table_insert(creation_times, | |
148 &status->creation_time, | |
149 status); | |
150 } | |
151 | |
12724 | 152 /** |
153 * A magic number is calcuated for each status, and then the | |
154 * statuses are ordered by the magic number. The magic number | |
155 * is the date the status was last used offset by one day for | |
156 * each time the status has been used (but only by 10 days at | |
157 * the most). | |
158 * | |
159 * The goal is to have recently used statuses at the top of | |
160 * the list, but to also keep frequently used statuses near | |
161 * the top. | |
162 */ | |
12327 | 163 static gint |
12724 | 164 saved_statuses_sort_func(gconstpointer a, gconstpointer b) |
12327 | 165 { |
12724 | 166 const GaimSavedStatus *saved_status_a = a; |
167 const GaimSavedStatus *saved_status_b = b; | |
168 time_t time_a = saved_status_a->lastused + | |
169 (MIN(saved_status_a->usage_count, 10) * 86400); | |
170 time_t time_b = saved_status_b->lastused + | |
171 (MIN(saved_status_b->usage_count, 10) * 86400); | |
172 if (time_a > time_b) | |
12327 | 173 return -1; |
12724 | 174 if (time_a < time_b) |
12327 | 175 return 1; |
176 return 0; | |
177 } | |
178 | |
179 /** | |
180 * Transient statuses are added and removed automatically by | |
181 * Gaim. If they're not used for a certain length of time then | |
182 * they'll expire and be automatically removed. This function | |
183 * does the expiration. | |
184 */ | |
185 static void | |
186 remove_old_transient_statuses() | |
187 { | |
188 GList *l, *next; | |
13197 | 189 GaimSavedStatus *saved_status, *current_status; |
12724 | 190 int count; |
191 time_t creation_time; | |
12327 | 192 |
13197 | 193 current_status = gaim_savedstatus_get_current(); |
194 | |
12724 | 195 /* |
196 * Iterate through the list of saved statuses. Delete all | |
197 * transient statuses except for the first MAX_TRANSIENTS | |
198 * (remember, the saved statuses are already sorted by popularity). | |
199 */ | |
200 count = 0; | |
12327 | 201 for (l = saved_statuses; l != NULL; l = next) |
202 { | |
203 next = l->next; | |
204 saved_status = l->data; | |
12724 | 205 if (gaim_savedstatus_is_transient(saved_status)) |
12327 | 206 { |
12724 | 207 if (count == MAX_TRANSIENTS) |
208 { | |
13197 | 209 if (saved_status != current_status) |
210 { | |
211 saved_statuses = g_list_remove(saved_statuses, saved_status); | |
212 creation_time = gaim_savedstatus_get_creation_time(saved_status); | |
213 g_hash_table_remove(creation_times, &creation_time); | |
214 free_saved_status(saved_status); | |
215 } | |
12724 | 216 } |
217 else | |
218 count++; | |
12327 | 219 } |
220 } | |
12724 | 221 |
222 if (count == MAX_TRANSIENTS) | |
223 schedule_save(); | |
12327 | 224 } |
225 | |
10428 | 226 /********************************************************************* |
10429 | 227 * Writing to disk * |
10428 | 228 *********************************************************************/ |
10418 | 229 |
230 static xmlnode * | |
10419 | 231 substatus_to_xmlnode(GaimSavedStatusSub *substatus) |
10418 | 232 { |
233 xmlnode *node, *child; | |
234 | |
235 node = xmlnode_new("substatus"); | |
236 | |
10424 | 237 child = xmlnode_new_child(node, "account"); |
238 xmlnode_set_attrib(child, "protocol", gaim_account_get_protocol_id(substatus->account)); | |
239 xmlnode_insert_data(child, gaim_account_get_username(substatus->account), -1); | |
10418 | 240 |
10424 | 241 child = xmlnode_new_child(node, "state"); |
10418 | 242 xmlnode_insert_data(child, gaim_status_type_get_id(substatus->type), -1); |
243 | |
244 if (substatus->message != NULL) | |
245 { | |
10424 | 246 child = xmlnode_new_child(node, "message"); |
10418 | 247 xmlnode_insert_data(child, substatus->message, -1); |
248 } | |
249 | |
250 return node; | |
251 } | |
252 | |
253 static xmlnode * | |
10419 | 254 status_to_xmlnode(GaimSavedStatus *status) |
10418 | 255 { |
256 xmlnode *node, *child; | |
12125 | 257 char buf[21]; |
10418 | 258 GList *cur; |
259 | |
12125 | 260 node = xmlnode_new("status"); |
261 if (status->title != NULL) | |
12283 | 262 { |
12125 | 263 xmlnode_set_attrib(node, "name", status->title); |
12283 | 264 } |
265 else | |
266 { | |
267 /* | |
268 * Gaim 1.5.0 and earlier require a name to be set, so we | |
269 * do this little hack to maintain backward compatability | |
12309 | 270 * in the status.xml file. Eventually this should be removed |
12283 | 271 * and we should determine if a status is transient by |
272 * whether the "name" attribute is set to something or if | |
273 * it does not exist at all. | |
274 */ | |
275 xmlnode_set_attrib(node, "name", "Auto-Cached"); | |
276 xmlnode_set_attrib(node, "transient", "true"); | |
277 } | |
11651 | 278 |
12125 | 279 snprintf(buf, sizeof(buf), "%lu", status->creation_time); |
280 xmlnode_set_attrib(node, "created", buf); | |
281 | |
282 snprintf(buf, sizeof(buf), "%lu", status->lastused); | |
283 xmlnode_set_attrib(node, "lastused", buf); | |
10418 | 284 |
12688 | 285 snprintf(buf, sizeof(buf), "%u", status->usage_count); |
286 xmlnode_set_attrib(node, "usage_count", buf); | |
287 | |
10424 | 288 child = xmlnode_new_child(node, "state"); |
289 xmlnode_insert_data(child, gaim_primitive_get_id_from_type(status->type), -1); | |
10418 | 290 |
11651 | 291 if (status->message != NULL) |
292 { | |
293 child = xmlnode_new_child(node, "message"); | |
294 xmlnode_insert_data(child, status->message, -1); | |
295 } | |
10418 | 296 |
297 for (cur = status->substatuses; cur != NULL; cur = cur->next) | |
298 { | |
299 child = substatus_to_xmlnode(cur->data); | |
300 xmlnode_insert_child(node, child); | |
301 } | |
302 | |
303 return node; | |
304 } | |
305 | |
306 static xmlnode * | |
307 statuses_to_xmlnode(void) | |
308 { | |
309 xmlnode *node, *child; | |
310 GList *cur; | |
311 | |
312 node = xmlnode_new("statuses"); | |
10423 | 313 xmlnode_set_attrib(node, "version", "1.0"); |
10418 | 314 |
315 for (cur = saved_statuses; cur != NULL; cur = cur->next) | |
316 { | |
317 child = status_to_xmlnode(cur->data); | |
318 xmlnode_insert_child(node, child); | |
319 } | |
320 | |
321 return node; | |
322 } | |
323 | |
324 static void | |
325 sync_statuses(void) | |
326 { | |
10423 | 327 xmlnode *node; |
10418 | 328 char *data; |
329 | |
10428 | 330 if (!statuses_loaded) |
331 { | |
10418 | 332 gaim_debug_error("status", "Attempted to save statuses before they " |
333 "were read!\n"); | |
334 return; | |
335 } | |
336 | |
10423 | 337 node = statuses_to_xmlnode(); |
338 data = xmlnode_to_formatted_str(node, NULL); | |
10418 | 339 gaim_util_write_data_to_file("status.xml", data, -1); |
340 g_free(data); | |
10423 | 341 xmlnode_free(node); |
10418 | 342 } |
343 | |
344 static gboolean | |
10428 | 345 save_cb(gpointer data) |
10418 | 346 { |
347 sync_statuses(); | |
10428 | 348 save_timer = 0; |
10418 | 349 return FALSE; |
350 } | |
351 | |
352 static void | |
353 schedule_save(void) | |
354 { | |
10428 | 355 if (save_timer == 0) |
356 save_timer = gaim_timeout_add(5000, save_cb, NULL); | |
10418 | 357 } |
358 | |
359 | |
10428 | 360 /********************************************************************* |
361 * Reading from disk * | |
362 *********************************************************************/ | |
363 | |
10419 | 364 static GaimSavedStatusSub * |
10418 | 365 parse_substatus(xmlnode *substatus) |
366 { | |
10419 | 367 GaimSavedStatusSub *ret; |
10418 | 368 xmlnode *node; |
10425 | 369 char *data; |
10418 | 370 |
10419 | 371 ret = g_new0(GaimSavedStatusSub, 1); |
10418 | 372 |
373 /* Read the account */ | |
374 node = xmlnode_get_child(substatus, "account"); | |
375 if (node != NULL) | |
376 { | |
377 char *acct_name; | |
378 const char *protocol; | |
379 acct_name = xmlnode_get_data(node); | |
380 protocol = xmlnode_get_attrib(node, "protocol"); | |
381 if ((acct_name != NULL) && (protocol != NULL)) | |
382 ret->account = gaim_accounts_find(acct_name, protocol); | |
383 g_free(acct_name); | |
384 } | |
385 | |
386 if (ret->account == NULL) | |
387 { | |
388 g_free(ret); | |
389 return NULL; | |
390 } | |
391 | |
392 /* Read the state */ | |
393 node = xmlnode_get_child(substatus, "state"); | |
10426 | 394 if ((node != NULL) && ((data = xmlnode_get_data(node)) != NULL)) |
10425 | 395 { |
10418 | 396 ret->type = gaim_status_type_find_with_id( |
10425 | 397 ret->account->status_types, data); |
10418 | 398 g_free(data); |
399 } | |
400 | |
401 /* Read the message */ | |
402 node = xmlnode_get_child(substatus, "message"); | |
10426 | 403 if ((node != NULL) && ((data = xmlnode_get_data(node)) != NULL)) |
10425 | 404 { |
10418 | 405 ret->message = data; |
10425 | 406 } |
10418 | 407 |
408 return ret; | |
409 } | |
410 | |
411 /** | |
412 * Parse a saved status and add it to the saved_statuses linked list. | |
413 * | |
414 * Here's an example of the XML for a saved status: | |
415 * <status name="Girls"> | |
416 * <state>away</state> | |
417 * <message>I like the way that they walk | |
418 * And it's chill to hear them talk | |
419 * And I can always make them smile | |
420 * From White Castle to the Nile</message> | |
421 * <substatus> | |
422 * <account protocol='prpl-oscar'>markdoliner</account> | |
423 * <state>available</state> | |
424 * <message>The ladies man is here to answer your queries.</message> | |
425 * </substatus> | |
426 * <substatus> | |
427 * <account protocol='prpl-oscar'>giantgraypanda</account> | |
428 * <state>away</state> | |
429 * <message>A.C. ain't in charge no more.</message> | |
430 * </substatus> | |
431 * </status> | |
432 * | |
433 * I know. Moving, huh? | |
434 */ | |
10419 | 435 static GaimSavedStatus * |
10418 | 436 parse_status(xmlnode *status) |
437 { | |
10419 | 438 GaimSavedStatus *ret; |
10418 | 439 xmlnode *node; |
440 const char *attrib; | |
10425 | 441 char *data; |
10418 | 442 int i; |
443 | |
10419 | 444 ret = g_new0(GaimSavedStatus, 1); |
10418 | 445 |
12283 | 446 attrib = xmlnode_get_attrib(status, "transient"); |
447 if ((attrib == NULL) || (strcmp(attrib, "true"))) | |
448 { | |
449 /* Read the title */ | |
450 attrib = xmlnode_get_attrib(status, "name"); | |
451 ret->title = g_strdup(attrib); | |
452 } | |
12125 | 453 |
454 if (ret->title != NULL) | |
10418 | 455 { |
12125 | 456 /* Ensure the title is unique */ |
457 i = 2; | |
458 while (gaim_savedstatus_find(ret->title) != NULL) | |
459 { | |
460 g_free(ret->title); | |
461 ret->title = g_strdup_printf("%s %d", attrib, i); | |
462 i++; | |
463 } | |
10418 | 464 } |
465 | |
12125 | 466 /* Read the creation time */ |
467 attrib = xmlnode_get_attrib(status, "created"); | |
468 set_creation_time(ret, (attrib != NULL ? atol(attrib) : 0)); | |
469 | |
470 /* Read the last used time */ | |
471 attrib = xmlnode_get_attrib(status, "lastused"); | |
472 ret->lastused = (attrib != NULL ? atol(attrib) : 0); | |
473 | |
12688 | 474 /* Read the usage count */ |
475 attrib = xmlnode_get_attrib(status, "usage_count"); | |
476 ret->usage_count = (attrib != NULL ? atol(attrib) : 0); | |
477 | |
10418 | 478 /* Read the primitive status type */ |
479 node = xmlnode_get_child(status, "state"); | |
10426 | 480 if ((node != NULL) && ((data = xmlnode_get_data(node)) != NULL)) |
10425 | 481 { |
10419 | 482 ret->type = gaim_primitive_get_type_from_id(data); |
10418 | 483 g_free(data); |
484 } | |
485 | |
486 /* Read the message */ | |
487 node = xmlnode_get_child(status, "message"); | |
10426 | 488 if ((node != NULL) && ((data = xmlnode_get_data(node)) != NULL)) |
10425 | 489 { |
10418 | 490 ret->message = data; |
10425 | 491 } |
10418 | 492 |
493 /* Read substatuses */ | |
12056 | 494 for (node = xmlnode_get_child(status, "substatus"); node != NULL; |
10418 | 495 node = xmlnode_get_next_twin(node)) |
496 { | |
10419 | 497 GaimSavedStatusSub *new; |
10418 | 498 new = parse_substatus(node); |
499 if (new != NULL) | |
12056 | 500 ret->substatuses = g_list_prepend(ret->substatuses, new); |
10418 | 501 } |
502 | |
503 return ret; | |
504 } | |
505 | |
506 /** | |
507 * Read the saved statuses from a file in the Gaim user dir. | |
508 * | |
509 * @return TRUE on success, FALSE on failure (if the file can not | |
510 * be opened, or if it contains invalid XML). | |
511 */ | |
10425 | 512 static void |
513 load_statuses(void) | |
10418 | 514 { |
515 xmlnode *statuses, *status; | |
516 | |
10426 | 517 statuses_loaded = TRUE; |
518 | |
10425 | 519 statuses = gaim_util_read_xml_from_file("status.xml", _("saved statuses")); |
10418 | 520 |
521 if (statuses == NULL) | |
10425 | 522 return; |
10418 | 523 |
524 for (status = xmlnode_get_child(statuses, "status"); status != NULL; | |
525 status = xmlnode_get_next_twin(status)) | |
526 { | |
10419 | 527 GaimSavedStatus *new; |
10418 | 528 new = parse_status(status); |
12056 | 529 saved_statuses = g_list_prepend(saved_statuses, new); |
10418 | 530 } |
12724 | 531 saved_statuses = g_list_sort(saved_statuses, saved_statuses_sort_func); |
10418 | 532 |
533 xmlnode_free(statuses); | |
534 } | |
535 | |
536 | |
537 /************************************************************************** | |
538 * Saved status API | |
539 **************************************************************************/ | |
10419 | 540 GaimSavedStatus * |
541 gaim_savedstatus_new(const char *title, GaimStatusPrimitive type) | |
10418 | 542 { |
10419 | 543 GaimSavedStatus *status; |
10418 | 544 |
12056 | 545 /* Make sure we don't already have a saved status with this title. */ |
12125 | 546 if (title != NULL) |
547 g_return_val_if_fail(gaim_savedstatus_find(title) == NULL, NULL); | |
10420 | 548 |
10419 | 549 status = g_new0(GaimSavedStatus, 1); |
10418 | 550 status->title = g_strdup(title); |
551 status->type = type; | |
12125 | 552 set_creation_time(status, time(NULL)); |
10418 | 553 |
12724 | 554 saved_statuses = g_list_insert_sorted(saved_statuses, status, saved_statuses_sort_func); |
10418 | 555 |
556 schedule_save(); | |
557 | |
558 return status; | |
559 } | |
560 | |
10420 | 561 void |
12056 | 562 gaim_savedstatus_set_title(GaimSavedStatus *status, const char *title) |
563 { | |
564 g_return_if_fail(status != NULL); | |
565 | |
566 /* Make sure we don't already have a saved status with this title. */ | |
567 g_return_if_fail(gaim_savedstatus_find(title) == NULL); | |
568 | |
569 g_free(status->title); | |
570 status->title = g_strdup(title); | |
571 | |
572 schedule_save(); | |
573 } | |
574 | |
575 void | |
11651 | 576 gaim_savedstatus_set_type(GaimSavedStatus *status, GaimStatusPrimitive type) |
577 { | |
578 g_return_if_fail(status != NULL); | |
579 | |
580 status->type = type; | |
581 | |
582 schedule_save(); | |
583 } | |
584 | |
585 void | |
10420 | 586 gaim_savedstatus_set_message(GaimSavedStatus *status, const char *message) |
587 { | |
588 g_return_if_fail(status != NULL); | |
589 | |
590 g_free(status->message); | |
13246 | 591 if ((message != NULL) && (*message == '\0')) |
592 status->message = NULL; | |
593 else | |
594 status->message = g_strdup(message); | |
10420 | 595 |
596 schedule_save(); | |
597 } | |
598 | |
12056 | 599 void |
12080 | 600 gaim_savedstatus_set_substatus(GaimSavedStatus *saved_status, |
601 const GaimAccount *account, | |
602 const GaimStatusType *type, | |
603 const char *message) | |
12056 | 604 { |
605 GaimSavedStatusSub *substatus; | |
606 | |
607 g_return_if_fail(saved_status != NULL); | |
608 g_return_if_fail(account != NULL); | |
609 g_return_if_fail(type != NULL); | |
610 | |
611 /* Find an existing substatus or create a new one */ | |
12080 | 612 substatus = gaim_savedstatus_get_substatus(saved_status, account); |
12056 | 613 if (substatus == NULL) |
614 { | |
615 substatus = g_new0(GaimSavedStatusSub, 1); | |
616 substatus->account = (GaimAccount *)account; | |
617 saved_status->substatuses = g_list_prepend(saved_status->substatuses, substatus); | |
618 } | |
619 | |
620 substatus->type = type; | |
621 g_free(substatus->message); | |
622 substatus->message = g_strdup(message); | |
623 | |
624 schedule_save(); | |
625 } | |
626 | |
627 void | |
12080 | 628 gaim_savedstatus_unset_substatus(GaimSavedStatus *saved_status, |
629 const GaimAccount *account) | |
12056 | 630 { |
631 GList *iter; | |
632 GaimSavedStatusSub *substatus; | |
633 | |
634 g_return_if_fail(saved_status != NULL); | |
635 g_return_if_fail(account != NULL); | |
636 | |
637 for (iter = saved_status->substatuses; iter != NULL; iter = iter->next) | |
638 { | |
639 substatus = iter->data; | |
640 if (substatus->account == account) | |
641 { | |
642 saved_status->substatuses = g_list_delete_link(saved_status->substatuses, iter); | |
643 g_free(substatus->message); | |
644 g_free(substatus); | |
645 return; | |
646 } | |
647 } | |
648 } | |
649 | |
10418 | 650 gboolean |
10419 | 651 gaim_savedstatus_delete(const char *title) |
10418 | 652 { |
10419 | 653 GaimSavedStatus *status; |
12125 | 654 time_t creation_time, current, idleaway; |
10418 | 655 |
10419 | 656 status = gaim_savedstatus_find(title); |
10418 | 657 |
658 if (status == NULL) | |
659 return FALSE; | |
660 | |
661 saved_statuses = g_list_remove(saved_statuses, status); | |
12125 | 662 creation_time = gaim_savedstatus_get_creation_time(status); |
663 g_hash_table_remove(creation_times, &creation_time); | |
12724 | 664 free_saved_status(status); |
10418 | 665 |
666 schedule_save(); | |
667 | |
12125 | 668 /* |
669 * If we just deleted our current status or our idleaway status, | |
670 * then set the appropriate pref back to 0. | |
671 */ | |
672 current = gaim_prefs_get_int("/core/savedstatus/current"); | |
673 if (current == creation_time) | |
674 gaim_prefs_set_int("/core/savedstatus/current", 0); | |
675 | |
676 idleaway = gaim_prefs_get_int("/core/savedstatus/idleaway"); | |
677 if (idleaway == creation_time) | |
678 gaim_prefs_set_int("/core/savedstatus/idleaway", 0); | |
679 | |
10418 | 680 return TRUE; |
681 } | |
682 | |
683 const GList * | |
684 gaim_savedstatuses_get_all(void) | |
685 { | |
686 return saved_statuses; | |
687 } | |
688 | |
12688 | 689 GList * |
690 gaim_savedstatuses_get_popular(unsigned int how_many) | |
691 { | |
13673 | 692 GList *popular = NULL; |
12688 | 693 GList *cur; |
694 int i; | |
13675 | 695 GaimSavedStatus *current, *next; |
13673 | 696 |
697 /* We don't want the current status to be in the GList */ | |
698 current = gaim_savedstatus_get_current(); | |
12688 | 699 |
700 /* Copy 'how_many' elements to a new list */ | |
13673 | 701 i = 0; |
702 cur = saved_statuses; | |
703 while ((i < how_many) && (cur != NULL)) | |
12688 | 704 { |
13675 | 705 next = cur->data; |
706 if ((next != current) && (!gaim_savedstatus_is_transient(next) | |
707 || gaim_savedstatus_get_message(next) != NULL)) | |
13673 | 708 { |
709 popular = g_list_prepend(popular, cur->data); | |
710 i++; | |
711 } | |
12688 | 712 cur = cur->next; |
713 } | |
714 | |
13673 | 715 popular = g_list_reverse(popular); |
716 | |
717 return popular; | |
12688 | 718 } |
719 | |
10419 | 720 GaimSavedStatus * |
12125 | 721 gaim_savedstatus_get_current() |
722 { | |
723 int creation_time; | |
12197 | 724 GaimSavedStatus *saved_status = NULL; |
12125 | 725 |
726 creation_time = gaim_prefs_get_int("/core/savedstatus/current"); | |
727 | |
12197 | 728 if (creation_time != 0) |
729 saved_status = g_hash_table_lookup(creation_times, &creation_time); | |
730 | |
731 if (saved_status == NULL) | |
12125 | 732 { |
733 /* | |
13681 | 734 * We don't have a current saved status! This is either a new |
12197 | 735 * Gaim user or someone upgrading from Gaim 1.5.0 or older, or |
736 * possibly someone who deleted the status they were currently | |
737 * using? In any case, add a default status. | |
12125 | 738 */ |
739 saved_status = gaim_savedstatus_new(NULL, GAIM_STATUS_AVAILABLE); | |
13246 | 740 gaim_prefs_set_int("/core/savedstatus/current", |
741 gaim_savedstatus_get_creation_time(saved_status)); | |
12125 | 742 } |
743 | |
744 return saved_status; | |
745 } | |
746 | |
747 GaimSavedStatus * | |
748 gaim_savedstatus_get_idleaway() | |
749 { | |
750 int creation_time; | |
13681 | 751 GaimSavedStatus *saved_status = NULL; |
12125 | 752 |
753 creation_time = gaim_prefs_get_int("/core/savedstatus/idleaway"); | |
754 | |
13681 | 755 if (creation_time != 0) |
756 saved_status = g_hash_table_lookup(creation_times, &creation_time); | |
757 | |
758 if (saved_status == NULL) | |
12125 | 759 { |
13681 | 760 /* We don't have a specified "idle" status! Weird. */ |
761 saved_status = gaim_savedstatus_find_transient_by_type_and_message( | |
762 GAIM_STATUS_AWAY, DEFAULT_AUTOAWAY_MESSAGE); | |
763 | |
764 if (saved_status == NULL) | |
765 { | |
766 saved_status = gaim_savedstatus_new(NULL, GAIM_STATUS_AWAY); | |
767 gaim_savedstatus_set_message(saved_status, DEFAULT_AUTOAWAY_MESSAGE); | |
768 gaim_prefs_set_int("/core/savedstatus/idleaway", | |
769 gaim_savedstatus_get_creation_time(saved_status)); | |
770 } | |
12125 | 771 } |
772 | |
773 return saved_status; | |
774 } | |
775 | |
776 GaimSavedStatus * | |
13681 | 777 gaim_savedstatus_get_startup() |
778 { | |
779 int creation_time; | |
780 GaimSavedStatus *saved_status = NULL; | |
781 | |
782 creation_time = gaim_prefs_get_int("/core/savedstatus/startup"); | |
783 | |
784 if (creation_time != 0) | |
785 saved_status = g_hash_table_lookup(creation_times, &creation_time); | |
786 | |
787 if (saved_status == NULL) | |
788 { | |
789 /* | |
790 * We don't have a status to apply. | |
791 * This may be the first login, or the user wants to | |
792 * restore the "current" status. | |
793 */ | |
794 saved_status = gaim_savedstatus_get_current(); | |
795 } | |
796 | |
797 return saved_status; | |
798 } | |
799 | |
800 | |
801 GaimSavedStatus * | |
10419 | 802 gaim_savedstatus_find(const char *title) |
10418 | 803 { |
12056 | 804 GList *iter; |
10419 | 805 GaimSavedStatus *status; |
10418 | 806 |
11977 | 807 g_return_val_if_fail(title != NULL, NULL); |
808 | |
12056 | 809 for (iter = saved_statuses; iter != NULL; iter = iter->next) |
10418 | 810 { |
12056 | 811 status = (GaimSavedStatus *)iter->data; |
12125 | 812 if ((status->title != NULL) && !strcmp(status->title, title)) |
10418 | 813 return status; |
814 } | |
815 | |
816 return NULL; | |
817 } | |
818 | |
12690 | 819 GaimSavedStatus * |
820 gaim_savedstatus_find_by_creation_time(time_t creation_time) | |
821 { | |
822 GList *iter; | |
823 GaimSavedStatus *status; | |
824 | |
825 for (iter = saved_statuses; iter != NULL; iter = iter->next) | |
826 { | |
827 status = (GaimSavedStatus *)iter->data; | |
828 if (status->creation_time == creation_time) | |
829 return status; | |
830 } | |
831 | |
832 return NULL; | |
833 } | |
834 | |
13012 | 835 GaimSavedStatus * |
13244 | 836 gaim_savedstatus_find_transient_by_type_and_message(GaimStatusPrimitive type, |
837 const char *message) | |
13012 | 838 { |
839 GList *iter; | |
840 GaimSavedStatus *status; | |
841 | |
842 for (iter = saved_statuses; iter != NULL; iter = iter->next) | |
843 { | |
844 status = (GaimSavedStatus *)iter->data; | |
13244 | 845 if ((status->type == type) && gaim_savedstatus_is_transient(status) && |
13012 | 846 (((status->message == NULL) && (message == NULL)) || |
847 ((status->message != NULL) && (message != NULL) && !strcmp(status->message, message)))) | |
848 { | |
849 return status; | |
850 } | |
851 } | |
852 | |
853 return NULL; | |
854 } | |
855 | |
11651 | 856 gboolean |
857 gaim_savedstatus_is_transient(const GaimSavedStatus *saved_status) | |
858 { | |
12197 | 859 g_return_val_if_fail(saved_status != NULL, TRUE); |
860 | |
12125 | 861 return (saved_status->title == NULL); |
11651 | 862 } |
863 | |
10418 | 864 const char * |
10419 | 865 gaim_savedstatus_get_title(const GaimSavedStatus *saved_status) |
10418 | 866 { |
12690 | 867 const char *message; |
868 | |
12197 | 869 g_return_val_if_fail(saved_status != NULL, NULL); |
870 | |
12690 | 871 /* If we have a title then return it */ |
872 if (saved_status->title != NULL) | |
873 return saved_status->title; | |
874 | |
875 /* Otherwise, this is a transient status and we make up a title on the fly */ | |
876 message = gaim_savedstatus_get_message(saved_status); | |
12688 | 877 |
12781 | 878 if ((message == NULL) || (*message == '\0')) |
12690 | 879 { |
880 GaimStatusPrimitive primitive; | |
881 primitive = gaim_savedstatus_get_type(saved_status); | |
12814
f88f145884c0
[gaim-migrate @ 15162]
Richard Laager <rlaager@wiktel.com>
parents:
12781
diff
changeset
|
882 return gaim_primitive_get_name_from_type(primitive); |
12690 | 883 } |
884 else | |
885 { | |
886 char *stripped; | |
887 static char buf[64]; | |
888 stripped = gaim_markup_strip_html(message); | |
889 gaim_util_chrreplace(stripped, '\n', ' '); | |
890 strncpy(buf, stripped, sizeof(buf)); | |
891 buf[sizeof(buf) - 1] = '\0'; | |
892 if ((strlen(stripped) + 1) > sizeof(buf)) | |
12688 | 893 { |
12690 | 894 /* Truncate and ellipsize */ |
895 char *tmp = g_utf8_find_prev_char(buf, &buf[sizeof(buf) - 4]); | |
896 strcpy(tmp, "..."); | |
12688 | 897 } |
12690 | 898 g_free(stripped); |
899 return buf; | |
12688 | 900 } |
10418 | 901 } |
902 | |
903 GaimStatusPrimitive | |
10419 | 904 gaim_savedstatus_get_type(const GaimSavedStatus *saved_status) |
10418 | 905 { |
12197 | 906 g_return_val_if_fail(saved_status != NULL, GAIM_STATUS_OFFLINE); |
907 | |
10418 | 908 return saved_status->type; |
909 } | |
910 | |
911 const char * | |
10419 | 912 gaim_savedstatus_get_message(const GaimSavedStatus *saved_status) |
10418 | 913 { |
12197 | 914 g_return_val_if_fail(saved_status != NULL, NULL); |
915 | |
10418 | 916 return saved_status->message; |
917 } | |
918 | |
12125 | 919 time_t |
920 gaim_savedstatus_get_creation_time(const GaimSavedStatus *saved_status) | |
921 { | |
12197 | 922 g_return_val_if_fail(saved_status != NULL, 0); |
923 | |
12125 | 924 return saved_status->creation_time; |
925 } | |
926 | |
11651 | 927 gboolean |
928 gaim_savedstatus_has_substatuses(const GaimSavedStatus *saved_status) | |
929 { | |
12197 | 930 g_return_val_if_fail(saved_status != NULL, FALSE); |
931 | |
11651 | 932 return (saved_status->substatuses != NULL); |
933 } | |
934 | |
12056 | 935 GaimSavedStatusSub * |
12080 | 936 gaim_savedstatus_get_substatus(const GaimSavedStatus *saved_status, |
937 const GaimAccount *account) | |
12056 | 938 { |
939 GList *iter; | |
940 GaimSavedStatusSub *substatus; | |
941 | |
942 g_return_val_if_fail(saved_status != NULL, NULL); | |
943 g_return_val_if_fail(account != NULL, NULL); | |
944 | |
945 for (iter = saved_status->substatuses; iter != NULL; iter = iter->next) | |
946 { | |
947 substatus = iter->data; | |
948 if (substatus->account == account) | |
949 return substatus; | |
950 } | |
951 | |
952 return NULL; | |
953 } | |
954 | |
955 const GaimStatusType * | |
956 gaim_savedstatus_substatus_get_type(const GaimSavedStatusSub *substatus) | |
957 { | |
958 g_return_val_if_fail(substatus != NULL, NULL); | |
959 | |
960 return substatus->type; | |
961 } | |
962 | |
963 const char * | |
964 gaim_savedstatus_substatus_get_message(const GaimSavedStatusSub *substatus) | |
965 { | |
966 g_return_val_if_fail(substatus != NULL, NULL); | |
967 | |
968 return substatus->message; | |
969 } | |
970 | |
11724 | 971 void |
12125 | 972 gaim_savedstatus_activate(GaimSavedStatus *saved_status) |
11724 | 973 { |
11733 | 974 GList *accounts, *node; |
13387
d9dbb874a30c
[gaim-migrate @ 15760]
Richard Laager <rlaager@wiktel.com>
parents:
13246
diff
changeset
|
975 GaimSavedStatus *old = gaim_savedstatus_get_current(); |
11724 | 976 |
11727 | 977 g_return_if_fail(saved_status != NULL); |
978 | |
12729 | 979 /* Make sure our list of saved statuses remains sorted */ |
980 saved_status->lastused = time(NULL); | |
981 saved_status->usage_count++; | |
982 saved_statuses = g_list_remove(saved_statuses, saved_status); | |
983 saved_statuses = g_list_insert_sorted(saved_statuses, saved_status, saved_statuses_sort_func); | |
984 | |
11724 | 985 accounts = gaim_accounts_get_all_active(); |
11733 | 986 for (node = accounts; node != NULL; node = node->next) |
11724 | 987 { |
988 GaimAccount *account; | |
989 | |
11733 | 990 account = node->data; |
12857
e5f780a6137b
[gaim-migrate @ 15208]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12814
diff
changeset
|
991 |
11724 | 992 gaim_savedstatus_activate_for_account(saved_status, account); |
993 } | |
11733 | 994 |
995 g_list_free(accounts); | |
11954 | 996 |
12125 | 997 gaim_prefs_set_int("/core/savedstatus/current", |
998 gaim_savedstatus_get_creation_time(saved_status)); | |
13387
d9dbb874a30c
[gaim-migrate @ 15760]
Richard Laager <rlaager@wiktel.com>
parents:
13246
diff
changeset
|
999 |
d9dbb874a30c
[gaim-migrate @ 15760]
Richard Laager <rlaager@wiktel.com>
parents:
13246
diff
changeset
|
1000 gaim_signal_emit(gaim_savedstatuses_get_handle(), "savedstatus-changed", |
d9dbb874a30c
[gaim-migrate @ 15760]
Richard Laager <rlaager@wiktel.com>
parents:
13246
diff
changeset
|
1001 saved_status, old); |
d9dbb874a30c
[gaim-migrate @ 15760]
Richard Laager <rlaager@wiktel.com>
parents:
13246
diff
changeset
|
1002 |
11724 | 1003 } |
1004 | |
1005 void | |
1006 gaim_savedstatus_activate_for_account(const GaimSavedStatus *saved_status, | |
1007 GaimAccount *account) | |
1008 { | |
12056 | 1009 const GaimStatusType *status_type; |
1010 const GaimSavedStatusSub *substatus; | |
1011 const char *message = NULL; | |
11724 | 1012 |
11727 | 1013 g_return_if_fail(saved_status != NULL); |
1014 g_return_if_fail(account != NULL); | |
1015 | |
12080 | 1016 substatus = gaim_savedstatus_get_substatus(saved_status, account); |
12056 | 1017 if (substatus != NULL) |
1018 { | |
1019 status_type = substatus->type; | |
1020 message = substatus->message; | |
1021 } | |
1022 else | |
11724 | 1023 { |
12056 | 1024 status_type = gaim_account_get_status_type_with_primitive(account, saved_status->type); |
1025 if (status_type == NULL) | |
1026 return; | |
1027 message = saved_status->message; | |
1028 } | |
1029 | |
1030 if ((message != NULL) && | |
1031 (gaim_status_type_get_attr(status_type, "message"))) | |
1032 { | |
1033 gaim_account_set_status(account, gaim_status_type_get_id(status_type), | |
1034 TRUE, "message", message, NULL); | |
1035 } | |
1036 else | |
1037 { | |
1038 gaim_account_set_status(account, gaim_status_type_get_id(status_type), | |
1039 TRUE, NULL); | |
11724 | 1040 } |
1041 } | |
1042 | |
11318 | 1043 void * |
1044 gaim_savedstatuses_get_handle(void) | |
1045 { | |
1046 static int handle; | |
1047 | |
1048 return &handle; | |
1049 } | |
1050 | |
10418 | 1051 void |
1052 gaim_savedstatuses_init(void) | |
1053 { | |
13387
d9dbb874a30c
[gaim-migrate @ 15760]
Richard Laager <rlaager@wiktel.com>
parents:
13246
diff
changeset
|
1054 void *handle = gaim_savedstatuses_get_handle(); |
d9dbb874a30c
[gaim-migrate @ 15760]
Richard Laager <rlaager@wiktel.com>
parents:
13246
diff
changeset
|
1055 |
12125 | 1056 creation_times = g_hash_table_new(g_int_hash, g_int_equal); |
11975 | 1057 |
12125 | 1058 /* |
1059 * Using 0 as the creation_time is a special case. | |
1060 * If someone calls gaim_savedstatus_get_current() or | |
1061 * gaim_savedstatus_get_idleaway() and either of those functions | |
1062 * sees a creation_time of 0, then it will create a default | |
1063 * saved status and return that to the user. | |
1064 */ | |
1065 gaim_prefs_add_none("/core/savedstatus"); | |
1066 gaim_prefs_add_int("/core/savedstatus/current", 0); | |
12857
e5f780a6137b
[gaim-migrate @ 15208]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12814
diff
changeset
|
1067 gaim_prefs_add_int("/core/savedstatus/startup", 0); |
e5f780a6137b
[gaim-migrate @ 15208]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12814
diff
changeset
|
1068 gaim_prefs_add_bool("/core/savedstatus/startup_current_status", TRUE); |
12125 | 1069 gaim_prefs_add_int("/core/savedstatus/idleaway", 0); |
11975 | 1070 |
12125 | 1071 load_statuses(); |
13387
d9dbb874a30c
[gaim-migrate @ 15760]
Richard Laager <rlaager@wiktel.com>
parents:
13246
diff
changeset
|
1072 |
d9dbb874a30c
[gaim-migrate @ 15760]
Richard Laager <rlaager@wiktel.com>
parents:
13246
diff
changeset
|
1073 gaim_signal_register(handle, "savedstatus-changed", |
d9dbb874a30c
[gaim-migrate @ 15760]
Richard Laager <rlaager@wiktel.com>
parents:
13246
diff
changeset
|
1074 gaim_marshal_VOID__POINTER_POINTER, NULL, 2, |
d9dbb874a30c
[gaim-migrate @ 15760]
Richard Laager <rlaager@wiktel.com>
parents:
13246
diff
changeset
|
1075 gaim_value_new(GAIM_TYPE_SUBTYPE, |
d9dbb874a30c
[gaim-migrate @ 15760]
Richard Laager <rlaager@wiktel.com>
parents:
13246
diff
changeset
|
1076 GAIM_SUBTYPE_SAVEDSTATUS), |
d9dbb874a30c
[gaim-migrate @ 15760]
Richard Laager <rlaager@wiktel.com>
parents:
13246
diff
changeset
|
1077 gaim_value_new(GAIM_TYPE_SUBTYPE, |
d9dbb874a30c
[gaim-migrate @ 15760]
Richard Laager <rlaager@wiktel.com>
parents:
13246
diff
changeset
|
1078 GAIM_SUBTYPE_SAVEDSTATUS)); |
10418 | 1079 } |
1080 | |
1081 void | |
1082 gaim_savedstatuses_uninit(void) | |
1083 { | |
12327 | 1084 remove_old_transient_statuses(); |
1085 | |
10428 | 1086 if (save_timer != 0) |
10418 | 1087 { |
10428 | 1088 gaim_timeout_remove(save_timer); |
1089 save_timer = 0; | |
10418 | 1090 sync_statuses(); |
1091 } | |
1092 | |
1093 while (saved_statuses != NULL) { | |
12056 | 1094 GaimSavedStatus *saved_status = saved_statuses->data; |
1095 saved_statuses = g_list_remove(saved_statuses, saved_status); | |
12724 | 1096 free_saved_status(saved_status); |
10418 | 1097 } |
12125 | 1098 |
1099 g_hash_table_destroy(creation_times); | |
13387
d9dbb874a30c
[gaim-migrate @ 15760]
Richard Laager <rlaager@wiktel.com>
parents:
13246
diff
changeset
|
1100 |
d9dbb874a30c
[gaim-migrate @ 15760]
Richard Laager <rlaager@wiktel.com>
parents:
13246
diff
changeset
|
1101 gaim_signals_unregister_by_instance(gaim_savedstatuses_get_handle()); |
10418 | 1102 } |
12056 | 1103 |