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