aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/backend/ebook/e-card.c
diff options
context:
space:
mode:
Diffstat (limited to 'addressbook/backend/ebook/e-card.c')
-rw-r--r--addressbook/backend/ebook/e-card.c212
1 files changed, 134 insertions, 78 deletions
diff --git a/addressbook/backend/ebook/e-card.c b/addressbook/backend/ebook/e-card.c
index 1b3ef694b6..216971c650 100644
--- a/addressbook/backend/ebook/e-card.c
+++ b/addressbook/backend/ebook/e-card.c
@@ -781,10 +781,9 @@ parse_file_as(ECard *card, VObject *vobj, char *default_charset)
static void
parse_name(ECard *card, VObject *vobj, char *default_charset)
{
- if ( card->name ) {
- e_card_name_free(card->name);
- }
- card->name = g_new(ECardName, 1);
+ e_card_name_unref(card->name);
+
+ card->name = e_card_name_new();
card->name->family = e_v_object_get_child_value (vobj, VCFamilyNameProp, default_charset);
card->name->given = e_v_object_get_child_value (vobj, VCGivenNameProp, default_charset);
@@ -832,7 +831,7 @@ parse_bday(ECard *card, VObject *vobj, char *default_charset)
static void
parse_phone(ECard *card, VObject *vobj, char *default_charset)
{
- ECardPhone *next_phone = g_new(ECardPhone, 1);
+ ECardPhone *next_phone = e_card_phone_new ();
EList *list;
assign_string(vobj, default_charset, &(next_phone->number));
@@ -842,13 +841,13 @@ parse_phone(ECard *card, VObject *vobj, char *default_charset)
"phone", &list,
NULL);
e_list_append(list, next_phone);
- e_card_phone_free (next_phone);
+ e_card_phone_unref (next_phone);
}
static void
parse_address(ECard *card, VObject *vobj, char *default_charset)
{
- ECardDeliveryAddress *next_addr = g_new(ECardDeliveryAddress, 1);
+ ECardDeliveryAddress *next_addr = e_card_delivery_address_new ();
EList *list;
next_addr->flags = get_address_flags (vobj);
@@ -864,13 +863,13 @@ parse_address(ECard *card, VObject *vobj, char *default_charset)
"address", &list,
NULL);
e_list_append(list, next_addr);
- e_card_delivery_address_free (next_addr);
+ e_card_delivery_address_unref (next_addr);
}
static void
parse_address_label(ECard *card, VObject *vobj, char *default_charset)
{
- ECardAddrLabel *next_addr = g_new(ECardAddrLabel, 1);
+ ECardAddrLabel *next_addr = e_card_address_label_new ();
EList *list;
next_addr->flags = get_address_flags (vobj);
@@ -880,7 +879,7 @@ parse_address_label(ECard *card, VObject *vobj, char *default_charset)
"address_label", &list,
NULL);
e_list_append(list, next_addr);
- e_card_address_label_free (next_addr);
+ e_card_address_label_unref (next_addr);
}
static void
@@ -1169,7 +1168,7 @@ parse_arbitrary(ECard *card, VObject *vobj, char *default_charset)
"arbitrary", &list,
NULL);
e_list_append(list, arbitrary);
- e_card_arbitrary_free(arbitrary);
+ e_card_arbitrary_unref(arbitrary);
}
static void
@@ -1337,6 +1336,7 @@ e_card_phone_new (void)
{
ECardPhone *newphone = g_new(ECardPhone, 1);
+ newphone->ref_count = 1;
newphone->number = NULL;
newphone->flags = 0;
@@ -1344,20 +1344,31 @@ e_card_phone_new (void)
}
void
-e_card_phone_free (ECardPhone *phone)
+e_card_phone_unref (ECardPhone *phone)
{
- if ( phone ) {
- g_free(phone->number);
-
- g_free(phone);
+ if (phone) {
+ phone->ref_count --;
+ if (phone->ref_count == 0) {
+ g_free(phone->number);
+ g_free(phone);
+ }
}
}
ECardPhone *
+e_card_phone_ref (const ECardPhone *phone)
+{
+ ECardPhone *phone_mutable = (ECardPhone *) phone;
+ if (phone_mutable)
+ phone_mutable->ref_count ++;
+ return phone_mutable;
+}
+
+ECardPhone *
e_card_phone_copy (const ECardPhone *phone)
{
if ( phone ) {
- ECardPhone *phone_copy = g_new(ECardPhone, 1);
+ ECardPhone *phone_copy = e_card_phone_new();
phone_copy->number = g_strdup(phone->number);
phone_copy->flags = phone->flags;
return phone_copy;
@@ -1370,6 +1381,7 @@ e_card_delivery_address_new (void)
{
ECardDeliveryAddress *newaddr = g_new(ECardDeliveryAddress, 1);
+ newaddr->ref_count = 1;
newaddr->po = NULL;
newaddr->ext = NULL;
newaddr->street = NULL;
@@ -1383,26 +1395,37 @@ e_card_delivery_address_new (void)
}
void
-e_card_delivery_address_free (ECardDeliveryAddress *addr)
+e_card_delivery_address_unref (ECardDeliveryAddress *addr)
{
if ( addr ) {
- g_free(addr->po);
- g_free(addr->ext);
- g_free(addr->street);
- g_free(addr->city);
- g_free(addr->region);
- g_free(addr->code);
- g_free(addr->country);
-
- g_free(addr);
+ addr->ref_count --;
+ if (addr->ref_count == 0) {
+ g_free(addr->po);
+ g_free(addr->ext);
+ g_free(addr->street);
+ g_free(addr->city);
+ g_free(addr->region);
+ g_free(addr->code);
+ g_free(addr->country);
+ g_free(addr);
+ }
}
}
ECardDeliveryAddress *
+e_card_delivery_address_ref (const ECardDeliveryAddress *addr)
+{
+ ECardDeliveryAddress *addr_mutable = (ECardDeliveryAddress *) addr;
+ if (addr_mutable)
+ addr_mutable->ref_count ++;
+ return addr_mutable;
+}
+
+ECardDeliveryAddress *
e_card_delivery_address_copy (const ECardDeliveryAddress *addr)
{
if ( addr ) {
- ECardDeliveryAddress *addr_copy = g_new(ECardDeliveryAddress, 1);
+ ECardDeliveryAddress *addr_copy = e_card_delivery_address_new ();
addr_copy->po = g_strdup(addr->po );
addr_copy->ext = g_strdup(addr->ext );
addr_copy->street = g_strdup(addr->street );
@@ -1431,7 +1454,7 @@ e_card_delivery_address_is_empty (const ECardDeliveryAddress *addr)
ECardDeliveryAddress *
e_card_delivery_address_from_label(const ECardAddrLabel *label)
{
- ECardDeliveryAddress *addr = g_new(ECardDeliveryAddress, 1);
+ ECardDeliveryAddress *addr = e_card_delivery_address_new ();
EAddressWestern *western = e_address_western_parse (label->data);
addr->po = g_strdup (western->po_box );
@@ -1507,6 +1530,7 @@ e_card_address_label_new (void)
{
ECardAddrLabel *newaddr = g_new(ECardAddrLabel, 1);
+ newaddr->ref_count = 1;
newaddr->data = NULL;
newaddr->flags = 0;
@@ -1514,22 +1538,33 @@ e_card_address_label_new (void)
}
void
-e_card_address_label_free (ECardAddrLabel *addr)
+e_card_address_label_unref (ECardAddrLabel *addr)
{
- if ( addr ) {
- g_free(addr->data);
-
- g_free(addr);
+ if (addr) {
+ addr->ref_count --;
+ if (addr->ref_count == 0) {
+ g_free(addr->data);
+ g_free(addr);
+ }
}
}
ECardAddrLabel *
+e_card_address_label_ref (const ECardAddrLabel *addr)
+{
+ ECardAddrLabel *addr_mutable = (ECardAddrLabel *) addr;
+ if (addr_mutable)
+ addr_mutable->ref_count ++;
+ return addr_mutable;
+}
+
+ECardAddrLabel *
e_card_address_label_copy (const ECardAddrLabel *addr)
{
if ( addr ) {
- ECardAddrLabel *addr_copy = g_new(ECardAddrLabel, 1);
- addr_copy->data = g_strdup(addr->data);
- addr_copy->flags = addr->flags;
+ ECardAddrLabel *addr_copy = e_card_address_label_new ();
+ addr_copy->data = g_strdup(addr->data);
+ addr_copy->flags = addr->flags;
return addr_copy;
} else
return NULL;
@@ -1537,8 +1572,9 @@ e_card_address_label_copy (const ECardAddrLabel *addr)
ECardName *e_card_name_new(void)
{
- ECardName *newname = g_new(ECardName, 1);
+ ECardName *newname = g_new(ECardName, 1);
+ newname->ref_count = 1;
newname->prefix = NULL;
newname->given = NULL;
newname->additional = NULL;
@@ -1549,29 +1585,36 @@ ECardName *e_card_name_new(void)
}
void
-e_card_name_free(ECardName *name)
+e_card_name_unref(ECardName *name)
{
if (name) {
- if ( name->prefix )
- g_free(name->prefix);
- if ( name->given )
- g_free(name->given);
- if ( name->additional )
- g_free(name->additional);
- if ( name->family )
- g_free(name->family);
- if ( name->suffix )
- g_free(name->suffix);
- g_free ( name );
+ name->ref_count --;
+ if (name->ref_count == 0) {
+ g_free (name->prefix);
+ g_free (name->given);
+ g_free (name->additional);
+ g_free (name->family);
+ g_free (name->suffix);
+ g_free (name);
+ }
}
}
ECardName *
+e_card_name_ref(const ECardName *name)
+{
+ ECardName *name_mutable = (ECardName *) name;
+ if (name_mutable)
+ name_mutable->ref_count ++;
+ return name_mutable;
+}
+
+ECardName *
e_card_name_copy(const ECardName *name)
{
if (name) {
- ECardName *newname = g_new(ECardName, 1);
-
+ ECardName *newname = e_card_name_new ();
+
newname->prefix = g_strdup(name->prefix);
newname->given = g_strdup(name->given);
newname->additional = g_strdup(name->additional);
@@ -1583,6 +1626,7 @@ e_card_name_copy(const ECardName *name)
return NULL;
}
+
char *
e_card_name_to_string(const ECardName *name)
{
@@ -1607,7 +1651,7 @@ e_card_name_to_string(const ECardName *name)
ECardName *
e_card_name_from_string(const char *full_name)
{
- ECardName *name = g_new(ECardName, 1);
+ ECardName *name = e_card_name_new ();
ENameWestern *western = e_name_western_parse (full_name);
name->prefix = g_strdup (western->prefix);
@@ -1625,17 +1669,32 @@ ECardArbitrary *
e_card_arbitrary_new(void)
{
ECardArbitrary *arbitrary = g_new(ECardArbitrary, 1);
+ arbitrary->ref_count = 1;
arbitrary->key = NULL;
arbitrary->type = NULL;
arbitrary->value = NULL;
return arbitrary;
}
+void
+e_card_arbitrary_unref(ECardArbitrary *arbitrary)
+{
+ if (arbitrary) {
+ arbitrary->ref_count --;
+ if (arbitrary->ref_count == 0) {
+ g_free(arbitrary->key);
+ g_free(arbitrary->type);
+ g_free(arbitrary->value);
+ g_free(arbitrary);
+ }
+ }
+}
+
ECardArbitrary *
e_card_arbitrary_copy(const ECardArbitrary *arbitrary)
{
if (arbitrary) {
- ECardArbitrary *arb_copy = g_new(ECardArbitrary, 1);
+ ECardArbitrary *arb_copy = e_card_arbitrary_new ();
arb_copy->key = g_strdup(arbitrary->key);
arb_copy->type = g_strdup(arbitrary->type);
arb_copy->value = g_strdup(arbitrary->value);
@@ -1644,15 +1703,13 @@ e_card_arbitrary_copy(const ECardArbitrary *arbitrary)
return NULL;
}
-void
-e_card_arbitrary_free(ECardArbitrary *arbitrary)
+ECardArbitrary *
+e_card_arbitrary_ref(const ECardArbitrary *arbitrary)
{
- if (arbitrary) {
- g_free(arbitrary->key);
- g_free(arbitrary->type);
- g_free(arbitrary->value);
- }
- g_free(arbitrary);
+ ECardArbitrary *arbitrary_mutable = (ECardArbitrary *) arbitrary;
+ if (arbitrary_mutable)
+ arbitrary_mutable->ref_count ++;
+ return arbitrary_mutable;
}
/* EMail matching */
@@ -1744,8 +1801,7 @@ e_card_destroy (GtkObject *object)
gtk_object_unref (GTK_OBJECT (card->book));
g_free(card->file_as);
g_free(card->fname);
- if ( card->name )
- e_card_name_free(card->name);
+ e_card_name_unref(card->name);
g_free(card->bday);
g_free(card->url);
@@ -1792,19 +1848,19 @@ e_card_set_arg (GtkObject *object, GtkArg *arg, guint arg_id)
if (card->file_as == NULL)
card->file_as = g_strdup("");
break;
+
case ARG_FULL_NAME:
g_free(card->fname);
card->fname = g_strdup(GTK_VALUE_STRING(*arg));
- if (card->name)
- e_card_name_free (card->name);
if (card->fname == NULL)
card->fname = g_strdup("");
+
+ e_card_name_unref (card->name);
card->name = e_card_name_from_string (card->fname);
break;
case ARG_NAME:
- if ( card->name )
- e_card_name_free(card->name);
- card->name = e_card_name_copy(GTK_VALUE_POINTER(*arg));
+ e_card_name_unref (card->name);
+ card->name = e_card_name_ref(GTK_VALUE_POINTER(*arg));
if (card->name == NULL)
card->name = e_card_name_new();
if (card->fname == NULL) {
@@ -1974,22 +2030,22 @@ e_card_get_arg (GtkObject *object, GtkArg *arg, guint arg_id)
break;
case ARG_ADDRESS:
if (!card->address)
- card->address = e_list_new((EListCopyFunc) e_card_delivery_address_copy,
- (EListFreeFunc) e_card_delivery_address_free,
+ card->address = e_list_new((EListCopyFunc) e_card_delivery_address_ref,
+ (EListFreeFunc) e_card_delivery_address_unref,
NULL);
GTK_VALUE_OBJECT(*arg) = GTK_OBJECT(card->address);
break;
case ARG_ADDRESS_LABEL:
if (!card->address_label)
- card->address_label = e_list_new((EListCopyFunc) e_card_address_label_copy,
- (EListFreeFunc) e_card_address_label_free,
+ card->address_label = e_list_new((EListCopyFunc) e_card_address_label_ref,
+ (EListFreeFunc) e_card_address_label_unref,
NULL);
GTK_VALUE_OBJECT(*arg) = GTK_OBJECT(card->address_label);
break;
case ARG_PHONE:
if (!card->phone)
- card->phone = e_list_new((EListCopyFunc) e_card_phone_copy,
- (EListFreeFunc) e_card_phone_free,
+ card->phone = e_list_new((EListCopyFunc) e_card_phone_ref,
+ (EListFreeFunc) e_card_phone_unref,
NULL);
GTK_VALUE_OBJECT(*arg) = GTK_OBJECT(card->phone);
break;
@@ -2086,8 +2142,8 @@ e_card_get_arg (GtkObject *object, GtkArg *arg, guint arg_id)
break;
case ARG_ARBITRARY:
if (!card->arbitrary)
- card->arbitrary = e_list_new((EListCopyFunc) e_card_arbitrary_copy,
- (EListFreeFunc) e_card_arbitrary_free,
+ card->arbitrary = e_list_new((EListCopyFunc) e_card_arbitrary_ref,
+ (EListFreeFunc) e_card_arbitrary_unref,
NULL);
GTK_VALUE_OBJECT(*arg) = GTK_OBJECT(card->arbitrary);
@@ -2731,7 +2787,7 @@ e_card_get_name (VObject *o)
VObject *vo;
char *the_str;
- name = g_new0 (ECardName, 1);
+ name = e_card_name_new ();
name->family = e_card_prop_get_substr (o, VCFamilyNameProp);
name->given = e_card_prop_get_substr (o, VCGivenNameProp);