| 560 |
560 |
| 561 /****************************************************************************** |
561 /****************************************************************************** |
| 562 * XmlNode Helpers |
562 * XmlNode Helpers |
| 563 *****************************************************************************/ |
563 *****************************************************************************/ |
| 564 static PurpleXmlNode * |
564 static PurpleXmlNode * |
| 565 status_attribute_to_xmlnode(PurpleStatus *status, PurpleStatusType *type, |
|
| 566 PurpleStatusAttribute *attr) |
|
| 567 { |
|
| 568 PurpleXmlNode *node; |
|
| 569 const char *id; |
|
| 570 char *value = NULL; |
|
| 571 PurpleStatusAttribute *default_attr; |
|
| 572 GValue *default_value; |
|
| 573 GType attr_type; |
|
| 574 GValue *attr_value; |
|
| 575 |
|
| 576 id = purple_status_attribute_get_id(attr); |
|
| 577 g_return_val_if_fail(id, NULL); |
|
| 578 |
|
| 579 attr_value = purple_status_get_attr_value(status, id); |
|
| 580 g_return_val_if_fail(attr_value, NULL); |
|
| 581 attr_type = G_VALUE_TYPE(attr_value); |
|
| 582 |
|
| 583 /* |
|
| 584 * If attr_value is a different type than it should be |
|
| 585 * then don't write it to the file. |
|
| 586 */ |
|
| 587 default_attr = purple_status_type_get_attr(type, id); |
|
| 588 default_value = purple_status_attribute_get_value(default_attr); |
|
| 589 if (attr_type != G_VALUE_TYPE(default_value)) |
|
| 590 return NULL; |
|
| 591 |
|
| 592 /* |
|
| 593 * If attr_value is the same as the default for this status |
|
| 594 * then there is no need to write it to the file. |
|
| 595 */ |
|
| 596 if (attr_type == G_TYPE_STRING) |
|
| 597 { |
|
| 598 const char *string_value = g_value_get_string(attr_value); |
|
| 599 const char *default_string_value = g_value_get_string(default_value); |
|
| 600 if (purple_strequal(string_value, default_string_value)) |
|
| 601 return NULL; |
|
| 602 value = g_value_dup_string(attr_value); |
|
| 603 } |
|
| 604 else if (attr_type == G_TYPE_INT) |
|
| 605 { |
|
| 606 int int_value = g_value_get_int(attr_value); |
|
| 607 if (int_value == g_value_get_int(default_value)) |
|
| 608 return NULL; |
|
| 609 value = g_strdup_printf("%d", int_value); |
|
| 610 } |
|
| 611 else if (attr_type == G_TYPE_BOOLEAN) |
|
| 612 { |
|
| 613 gboolean boolean_value = g_value_get_boolean(attr_value); |
|
| 614 if (boolean_value == g_value_get_boolean(default_value)) |
|
| 615 return NULL; |
|
| 616 value = g_strdup(boolean_value ? |
|
| 617 "true" : "false"); |
|
| 618 } |
|
| 619 else |
|
| 620 { |
|
| 621 return NULL; |
|
| 622 } |
|
| 623 |
|
| 624 g_return_val_if_fail(value, NULL); |
|
| 625 |
|
| 626 node = purple_xmlnode_new("attribute"); |
|
| 627 |
|
| 628 purple_xmlnode_set_attrib(node, "id", id); |
|
| 629 purple_xmlnode_set_attrib(node, "value", value); |
|
| 630 |
|
| 631 g_free(value); |
|
| 632 |
|
| 633 return node; |
|
| 634 } |
|
| 635 |
|
| 636 static PurpleXmlNode * |
|
| 637 status_attrs_to_xmlnode(PurpleStatus *status) |
|
| 638 { |
|
| 639 PurpleStatusType *type = purple_status_get_status_type(status); |
|
| 640 PurpleXmlNode *node, *child; |
|
| 641 GList *attrs, *attr; |
|
| 642 |
|
| 643 node = purple_xmlnode_new("attributes"); |
|
| 644 |
|
| 645 attrs = purple_status_type_get_attrs(type); |
|
| 646 for (attr = attrs; attr != NULL; attr = attr->next) |
|
| 647 { |
|
| 648 child = status_attribute_to_xmlnode(status, type, (PurpleStatusAttribute *)attr->data); |
|
| 649 if (child) |
|
| 650 purple_xmlnode_insert_child(node, child); |
|
| 651 } |
|
| 652 |
|
| 653 return node; |
|
| 654 } |
|
| 655 |
|
| 656 static PurpleXmlNode * |
|
| 657 status_to_xmlnode(PurpleStatus *status) |
|
| 658 { |
|
| 659 PurpleXmlNode *node, *child; |
|
| 660 |
|
| 661 node = purple_xmlnode_new("status"); |
|
| 662 purple_xmlnode_set_attrib(node, "type", purple_status_get_id(status)); |
|
| 663 if (purple_status_get_name(status) != NULL) |
|
| 664 purple_xmlnode_set_attrib(node, "name", purple_status_get_name(status)); |
|
| 665 purple_xmlnode_set_attrib(node, "active", purple_status_is_active(status) ? "true" : "false"); |
|
| 666 |
|
| 667 child = status_attrs_to_xmlnode(status); |
|
| 668 purple_xmlnode_insert_child(node, child); |
|
| 669 |
|
| 670 return node; |
|
| 671 } |
|
| 672 |
|
| 673 static PurpleXmlNode * |
|
| 674 statuses_to_xmlnode(PurplePresence *presence) |
|
| 675 { |
|
| 676 PurpleXmlNode *node, *child; |
|
| 677 GList *statuses; |
|
| 678 PurpleStatus *status; |
|
| 679 |
|
| 680 node = purple_xmlnode_new("statuses"); |
|
| 681 |
|
| 682 statuses = purple_presence_get_statuses(presence); |
|
| 683 for (; statuses != NULL; statuses = statuses->next) |
|
| 684 { |
|
| 685 status = statuses->data; |
|
| 686 if (purple_status_type_is_saveable(purple_status_get_status_type(status))) |
|
| 687 { |
|
| 688 child = status_to_xmlnode(status); |
|
| 689 purple_xmlnode_insert_child(node, child); |
|
| 690 } |
|
| 691 } |
|
| 692 |
|
| 693 return node; |
|
| 694 } |
|
| 695 |
|
| 696 static PurpleXmlNode * |
|
| 697 proxy_settings_to_xmlnode(const PurpleProxyInfo *proxy_info) |
565 proxy_settings_to_xmlnode(const PurpleProxyInfo *proxy_info) |
| 698 { |
566 { |
| 699 PurpleXmlNode *node, *child; |
567 PurpleXmlNode *node, *child; |
| 700 PurpleProxyType proxy_type; |
568 PurpleProxyType proxy_type; |
| 701 const char *value; |
569 const char *value; |