[PATCH] Fix issues with emails category

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From a537ade2e108a91552e0d9a6c4ff71ca35388e10 Mon Sep 17 00:00:00 2001
From: Lukasz Pawlik <lucas.pawlik@xxxxxxxxx>
Date: Mon, 23 Aug 2010 12:57:40 +0200
Subject: [PATCH] Fix issues with emails category

Previously all emails sent during phonebook pull had the same
category INTERNET. Now email are sent with valid category name
(INTERNET;HOME or INTERNET;WORK)
---
 plugins/phonebook-tracker.c |   30 +++++++++++++++++-------
 plugins/vcard.c             |   53 ++++++++++++++++++++++++++++++++++---------
 plugins/vcard.h             |   11 +++++++++
 3 files changed, 74 insertions(+), 20 deletions(-)

diff --git a/plugins/phonebook-tracker.c b/plugins/phonebook-tracker.c
index 3f63dcb..069e324 100644
--- a/plugins/phonebook-tracker.c
+++ b/plugins/phonebook-tracker.c
@@ -687,27 +687,39 @@ static void add_phone_number(struct phonebook_contact *contact,
 	contact->numbers = g_slist_append(contact->numbers, number);
 }
 
-static gchar *find_email(GSList *emails, const char *email)
+static struct phonebook_email *find_email(GSList *emails, const char *email,
+										int type)
 {
 	GSList *l;
 
-	for (l = emails; l; l = l->next)
-		if (g_strcmp0(l->data, email) == 0)
-			return l->data;
+	struct phonebook_email *email_addr;
+
+	for (l = emails; l; l = l->next) {
+		email_addr = l->data;
+		if (g_strcmp0(email_addr->email, email) == 0
+			&& email_addr->type == type)
+			return email_addr;
+	}
 
 	return NULL;
 }
 
-static void add_email(struct phonebook_contact *contact, const char *email)
+static void add_email(struct phonebook_contact *contact, const char *email,
+					int type)
 {
+	struct phonebook_email *email_addr;
 	if (email == NULL || strlen(email) == 0)
 		return;
 
 	/* Not adding email if there is already added with the same value */
-	if (find_email(contact->emails, email))
+	if (find_email(contact->emails, email, type))
 		return;
 
-	contact->emails = g_slist_append(contact->emails, g_strdup(email));
+	email_addr = g_new0(struct phonebook_email, 1);
+	email_addr->email = g_strdup(email);
+	email_addr->type = type;
+
+	contact->emails = g_slist_append(contact->emails, email_addr);
 }
 
 static GString *gen_vcards(GSList *contacts,
@@ -817,8 +829,8 @@ add_numbers:
 	add_phone_number(contact, reply[COL_FAX_NUMBER], TEL_TYPE_FAX);
 
 	/* Adding emails */
-	add_email(contact, reply[COL_HOME_EMAIL]);
-	add_email(contact, reply[COL_WORK_EMAIL]);
+	add_email(contact, reply[COL_HOME_EMAIL], EMAIL_TYPE_HOME);
+	add_email(contact, reply[COL_WORK_EMAIL], EMAIL_TYPE_WORK);
 
 	DBG("contact %p", contact);
 
diff --git a/plugins/vcard.c b/plugins/vcard.c
index 0eed8ae..425ac71 100644
--- a/plugins/vcard.c
+++ b/plugins/vcard.c
@@ -306,19 +306,39 @@ static void vcard_printf_slash_tag(GString *vcards, const char *tag,
 	vcard_printf(vcards, "%s:%s", buf, field);
 }
 
-static void vcard_printf_email(GString *vcards, const char *email)
+static void vcard_printf_email(GString *vcards, uint8_t format,
+								const char *email,
+								enum phonebook_email_type category)
 {
+	const char *category_string = "";
+	char field[LEN_MAX];
 	int len = 0;
 
-	if (email)
-		len = strlen(email);
+	if (!email || !(len = strlen(email)))
+		return;
 
-	if (len) {
-		char field[LEN_MAX];
-		add_slash(field, email, LEN_MAX, len);
-		vcard_printf(vcards,
-				"EMAIL;TYPE=INTERNET:%s", field);
+	switch (category){
+	case EMAIL_TYPE_HOME:
+		if (format == FORMAT_VCARD21)
+			category_string = "INTERNET;HOME";
+		else if (format == FORMAT_VCARD30)
+			category_string = "TYPE=INTERNET;TYPE=HOME";
+		break;
+	case EMAIL_TYPE_WORK:
+		if (format == FORMAT_VCARD21)
+			category_string = "INTERNET;WORK";
+		else if (format == FORMAT_VCARD30)
+			category_string = "TYPE=INTERNET;TYPE=WORK";
+		break;
+	default:
+		if (format == FORMAT_VCARD21)
+			category_string = "INTERNET";
+		else if (format == FORMAT_VCARD30)
+			category_string = "TYPE=INTERNET";
 	}
+
+	add_slash(field, email, LEN_MAX, len);
+	vcard_printf(vcards,"EMAIL;%s:%s", category_string, field);
 }
 
 static gboolean org_fields_present(struct phonebook_contact *contact)
@@ -421,8 +441,11 @@ void phonebook_add_contact(GString *vcards, struct phonebook_contact *contact,
 	if (filter & FILTER_EMAIL) {
 		GSList *l;
 
-		for (l = contact->emails; l; l = l->next)
-			vcard_printf_email(vcards, l->data);
+		for (l = contact->emails; l; l = l->next){
+			struct phonebook_email *email = l->data;
+
+			vcard_printf_email(vcards, format, email->email, email->type);
+		}
 	}
 
 	if (filter & FILTER_ADR)
@@ -459,6 +482,14 @@ static void number_free(gpointer data, gpointer user_data)
 	g_free(number);
 }
 
+static void email_free(gpointer data, gpointer user_data)
+{
+	struct phonebook_email *email = data;
+
+	g_free(email->email);
+	g_free(email);
+}
+
 void phonebook_contact_free(struct phonebook_contact *contact)
 {
 	if (contact == NULL)
@@ -467,7 +498,7 @@ void phonebook_contact_free(struct phonebook_contact *contact)
 	g_slist_foreach(contact->numbers, number_free, NULL);
 	g_slist_free(contact->numbers);
 
-	g_slist_foreach(contact->emails, (GFunc) g_free, NULL);
+	g_slist_foreach(contact->emails, email_free, NULL);
 	g_slist_free(contact->emails);
 
 	g_free(contact->fullname);
diff --git a/plugins/vcard.h b/plugins/vcard.h
index 0f52425..41423e0 100644
--- a/plugins/vcard.h
+++ b/plugins/vcard.h
@@ -27,6 +27,12 @@ enum phonebook_number_type {
 	TEL_TYPE_OTHER,
 };
 
+enum phonebook_email_type {
+	EMAIL_TYPE_HOME,
+	EMAIL_TYPE_WORK,
+	EMAIL_TYPE_OTHER,
+};
+
 enum phonebook_call_type {
 	CALL_TYPE_NOT_A_CALL,
 	CALL_TYPE_MISSED,
@@ -39,6 +45,11 @@ struct phonebook_number {
 	int type;
 };
 
+struct phonebook_email {
+	char *email;
+	int  type;
+};
+
 struct phonebook_contact {
 	char *fullname;
 	char *given;
-- 
1.7.0.4


[Index of Archives]     [Bluez Devel]     [Linux Wireless Networking]     [Linux Wireless Personal Area Networking]     [Linux ATH6KL]     [Linux USB Devel]     [Linux Media Drivers]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux