| 106 |
132 |
| 107 if (pounce->free != NULL && pounce->data != NULL) |
133 if (pounce->free != NULL && pounce->data != NULL) |
| 108 pounce->free(pounce->data); |
134 pounce->free(pounce->data); |
| 109 |
135 |
| 110 g_free(pounce); |
136 g_free(pounce); |
| |
137 |
| |
138 schedule_pounces_save(); |
| 111 } |
139 } |
| 112 |
140 |
| 113 void |
141 void |
| 114 gaim_pounce_set_events(GaimPounce *pounce, GaimPounceEvent events) |
142 gaim_pounce_set_events(GaimPounce *pounce, GaimPounceEvent events) |
| 115 { |
143 { |
| 116 g_return_if_fail(pounce != NULL); |
144 g_return_if_fail(pounce != NULL); |
| 117 g_return_if_fail(pounce != GAIM_POUNCE_NONE); |
145 g_return_if_fail(pounce != GAIM_POUNCE_NONE); |
| 118 |
146 |
| 119 pounce->events = events; |
147 pounce->events = events; |
| |
148 |
| |
149 schedule_pounces_save(); |
| 120 } |
150 } |
| 121 |
151 |
| 122 void |
152 void |
| 123 gaim_pounce_set_pouncer(GaimPounce *pounce, GaimAccount *pouncer) |
153 gaim_pounce_set_pouncer(GaimPounce *pounce, GaimAccount *pouncer) |
| 124 { |
154 { |
| 125 g_return_if_fail(pounce != NULL); |
155 g_return_if_fail(pounce != NULL); |
| 126 g_return_if_fail(pouncer != NULL); |
156 g_return_if_fail(pouncer != NULL); |
| 127 |
157 |
| 128 pounce->pouncer = pouncer; |
158 pounce->pouncer = pouncer; |
| |
159 |
| |
160 schedule_pounces_save(); |
| 129 } |
161 } |
| 130 |
162 |
| 131 void |
163 void |
| 132 gaim_pounce_set_pouncee(GaimPounce *pounce, const char *pouncee) |
164 gaim_pounce_set_pouncee(GaimPounce *pounce, const char *pouncee) |
| 133 { |
165 { |
| 321 |
363 |
| 322 g_return_val_if_fail(pouncer != NULL, NULL); |
364 g_return_val_if_fail(pouncer != NULL, NULL); |
| 323 g_return_val_if_fail(pouncee != NULL, NULL); |
365 g_return_val_if_fail(pouncee != NULL, NULL); |
| 324 g_return_val_if_fail(events != GAIM_POUNCE_NONE, NULL); |
366 g_return_val_if_fail(events != GAIM_POUNCE_NONE, NULL); |
| 325 |
367 |
| 326 for (l = gaim_get_pounces(); l != NULL; l = l->next) { |
368 for (l = gaim_pounces_get_all(); l != NULL; l = l->next) { |
| 327 pounce = (GaimPounce *)l->data; |
369 pounce = (GaimPounce *)l->data; |
| 328 |
370 |
| 329 if ((gaim_pounce_get_events(pounce) & events) && |
371 if ((gaim_pounce_get_events(pounce) & events) && |
| 330 (gaim_pounce_get_pouncer(pounce) == pouncer) && |
372 (gaim_pounce_get_pouncer(pounce) == pouncer) && |
| 331 !strcmp(gaim_pounce_get_pouncee(pounce), pouncee)) { |
373 !strcmp(gaim_pounce_get_pouncee(pounce), pouncee)) { |
| 335 } |
377 } |
| 336 |
378 |
| 337 return NULL; |
379 return NULL; |
| 338 } |
380 } |
| 339 |
381 |
| |
382 gboolean |
| |
383 gaim_pounces_load(void) |
| |
384 { |
| |
385 pounces_loaded = TRUE; |
| |
386 |
| |
387 return TRUE; |
| |
388 } |
| |
389 |
| |
390 static void |
| |
391 write_action_parameter(gpointer key, gpointer value, gpointer user_data) |
| |
392 { |
| |
393 const char *name, *param_value; |
| |
394 FILE *fp; |
| |
395 |
| |
396 param_value = (const char *)value; |
| |
397 name = (const char *)key; |
| |
398 fp = (FILE *)user_data; |
| |
399 |
| |
400 fprintf(fp, " <param name='%s'>%s</param>\n", name, param_value); |
| |
401 } |
| |
402 |
| |
403 static void |
| |
404 write_action_parameter_list(gpointer key, gpointer value, gpointer user_data) |
| |
405 { |
| |
406 GaimPounceActionData *action_data; |
| |
407 const char *action; |
| |
408 FILE *fp; |
| |
409 |
| |
410 action_data = (GaimPounceActionData *)value; |
| |
411 action = (const char *)key; |
| |
412 fp = (FILE *)user_data; |
| |
413 |
| |
414 if (!action_data->enabled) |
| |
415 return; |
| |
416 |
| |
417 fprintf(fp, " <action type='%s'", action); |
| |
418 |
| |
419 if (g_hash_table_size(action_data->atts) == 0) { |
| |
420 fprintf(fp, "/>\n"); |
| |
421 } |
| |
422 else { |
| |
423 fprintf(fp, ">\n"); |
| |
424 |
| |
425 g_hash_table_foreach(action_data->atts, write_action_parameter, fp); |
| |
426 |
| |
427 fprintf(fp, " </action>\n"); |
| |
428 } |
| |
429 } |
| |
430 |
| |
431 static void |
| |
432 gaim_pounces_write(FILE *fp, GaimPounce *pounce) |
| |
433 { |
| |
434 GaimAccount *pouncer; |
| |
435 GaimPounceEvent events; |
| |
436 char *pouncer_name; |
| |
437 |
| |
438 pouncer = gaim_pounce_get_pouncer(pounce); |
| |
439 events = gaim_pounce_get_events(pounce); |
| |
440 |
| |
441 pouncer_name = g_markup_escape_text(gaim_account_get_username(pouncer), -1); |
| |
442 |
| |
443 fprintf(fp, " <pounce ui='%s'>\n", pounce->ui_type); |
| |
444 fprintf(fp, " <account protocol='%s'>%s</account>\n", |
| |
445 pouncer->protocol_id, pouncer_name); |
| |
446 fprintf(fp, " <pouncee>%s</pouncee>\n", gaim_pounce_get_pouncee(pounce)); |
| |
447 fprintf(fp, " <events>\n"); |
| |
448 |
| |
449 if (events & GAIM_POUNCE_SIGNON) |
| |
450 fprintf(fp, " <event type='sign-on'>\n"); |
| |
451 if (events & GAIM_POUNCE_SIGNOFF) |
| |
452 fprintf(fp, " <event type='sign-off'>\n"); |
| |
453 if (events & GAIM_POUNCE_AWAY) |
| |
454 fprintf(fp, " <event type='away'>\n"); |
| |
455 if (events & GAIM_POUNCE_AWAY_RETURN) |
| |
456 fprintf(fp, " <event type='return-from-away'>\n"); |
| |
457 if (events & GAIM_POUNCE_IDLE) |
| |
458 fprintf(fp, " <event type='idle'>\n"); |
| |
459 if (events & GAIM_POUNCE_IDLE_RETURN) |
| |
460 fprintf(fp, " <event type='return-from-idle'>\n"); |
| |
461 if (events & GAIM_POUNCE_TYPING) |
| |
462 fprintf(fp, " <event type='start-typing'>\n"); |
| |
463 if (events & GAIM_POUNCE_TYPING_STOPPED) |
| |
464 fprintf(fp, " <event type='stop-typing'>\n"); |
| |
465 |
| |
466 fprintf(fp, " </events>\n"); |
| |
467 fprintf(fp, " <actions>\n"); |
| |
468 |
| |
469 g_hash_table_foreach(pounce->actions, write_action_parameter_list, fp); |
| |
470 |
| |
471 fprintf(fp, " </actions>\n"); |
| |
472 fprintf(fp, " </pounce>\n"); |
| |
473 |
| |
474 g_free(pouncer_name); |
| |
475 } |
| |
476 |
| |
477 void |
| |
478 gaim_pounces_sync(void) |
| |
479 { |
| |
480 FILE *fp; |
| |
481 const char *user_dir = gaim_user_dir(); |
| |
482 char *filename; |
| |
483 char *filename_real; |
| |
484 |
| |
485 if (!pounces_loaded) { |
| |
486 gaim_debug(GAIM_DEBUG_WARNING, "pounces", |
| |
487 "Writing pounces to disk.\n"); |
| |
488 schedule_pounces_save(); |
| |
489 return; |
| |
490 } |
| |
491 |
| |
492 if (user_dir == NULL) |
| |
493 return; |
| |
494 |
| |
495 gaim_debug(GAIM_DEBUG_INFO, "pounces", "Writing pounces to disk.\n"); |
| |
496 |
| |
497 fp = fopen(user_dir, "r"); |
| |
498 |
| |
499 if (fp == NULL) |
| |
500 mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR); |
| |
501 else |
| |
502 fclose(fp); |
| |
503 |
| |
504 filename = g_build_filename(user_dir, "pounces.xml.save", NULL); |
| |
505 |
| |
506 if ((fp = fopen(filename, "w")) != NULL) { |
| |
507 GList *l; |
| |
508 |
| |
509 fprintf(fp, "<?xml version='1.0' encoding='UTF-8' ?>\n\n"); |
| |
510 fprintf(fp, "<pounces>\n"); |
| |
511 |
| |
512 for (l = gaim_pounces_get_all(); l != NULL; l = l->next) |
| |
513 gaim_pounces_write(fp, l->data); |
| |
514 |
| |
515 fprintf(fp, "</pounces>\n"); |
| |
516 |
| |
517 fclose(fp); |
| |
518 chmod(filename, S_IRUSR | S_IWUSR); |
| |
519 } |
| |
520 else { |
| |
521 gaim_debug(GAIM_DEBUG_ERROR, "pounces", "Unable to write %s\n", |
| |
522 filename); |
| |
523 } |
| |
524 |
| |
525 filename_real = g_build_filename(user_dir, "pounces.xml", NULL); |
| |
526 |
| |
527 if (rename(filename, filename_real) < 0) { |
| |
528 gaim_debug(GAIM_DEBUG_ERROR, "pounces", "Error renaming %s to %s\n", |
| |
529 filename, filename_real); |
| |
530 } |
| |
531 |
| |
532 g_free(filename); |
| |
533 g_free(filename_real); |
| |
534 } |
| |
535 |
| 340 GList * |
536 GList * |
| 341 gaim_get_pounces(void) |
537 gaim_pounces_get_all(void) |
| 342 { |
538 { |
| 343 return pounces; |
539 return pounces; |
| 344 } |
540 } |