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