Skip to contentSkip to conversation

Split pull_contacts to smaller functions

PATCH Superseded Bartosz Szatkowski 2 participants 2 messages Nov 16, 2010 Bluetooth drivers

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 2/2 Split pull_contacts to smaller functions

Parts of pull_contact responsible for filling contact fields moved to new functions.

---
 plugins/phonebook-tracker.c |  159 ++++++++++++++++++++++++------------------
 1 files changed, 91 insertions(+), 68 deletions(-)
plugins/phonebook-tracker.c +91 -68
index 71ed2f0..e7de001 100644
@@ -1395,6 +1395,92 @@ static void add_affiliation(char **field, const char *value)
 	*field = g_strdup(value);
 }
 
+static void contact_init(struct phonebook_contact **contact, char **reply)
+{
+	if (*contact == NULL)
+		*contact = g_new0(struct phonebook_contact, 1);
+
+	(*contact)->fullname = g_strdup(reply[COL_FULL_NAME]);
+	(*contact)->family = g_strdup(reply[COL_FAMILY_NAME]);
+	(*contact)->given = g_strdup(reply[COL_GIVEN_NAME]);
+	(*contact)->additional = g_strdup(reply[COL_ADDITIONAL_NAME]);
+	(*contact)->prefix = g_strdup(reply[COL_NAME_PREFIX]);
+	(*contact)->suffix = g_strdup(reply[COL_NAME_SUFFIX]);
+	(*contact)->birthday = g_strdup(reply[COL_BIRTH_DATE]);
+	(*contact)->nickname = g_strdup(reply[COL_NICKNAME]);
+	(*contact)->website = g_strdup(reply[COL_URL]);
+	(*contact)->photo = g_strdup(reply[COL_PHOTO]);
+	(*contact)->company = g_strdup(reply[COL_ORG_NAME]);
+	(*contact)->department = g_strdup(reply[COL_ORG_DEPARTMENT]);
+	(*contact)->role = g_strdup(reply[COL_ORG_ROLE]);
+	(*contact)->uid = g_strdup(reply[COL_UID]);
+	(*contact)->title = g_strdup(reply[COL_TITLE]);
+
+	set_call_type(*contact, reply[COL_DATE], reply[COL_SENT],
+							reply[COL_ANSWERED]);
+}
+
+static void contact_add_numbers(struct phonebook_contact **contact, char **reply)
+{
+	add_phone_number(*contact, reply[COL_HOME_NUMBER], TEL_TYPE_HOME);
+	add_phone_number(*contact, reply[COL_WORK_NUMBER], TEL_TYPE_WORK);
+	add_phone_number(*contact, reply[COL_FAX_NUMBER], TEL_TYPE_FAX);
+	add_phone_number(*contact, reply[COL_CELL_NUMBER], TEL_TYPE_MOBILE);
+	if ((g_strcmp0(reply[COL_OTHER_NUMBER], reply[COL_CELL_NUMBER]) != 0) &&
+			(g_strcmp0(reply[COL_OTHER_NUMBER], reply[COL_WORK_NUMBER]) != 0) &&
+			(g_strcmp0(reply[COL_OTHER_NUMBER], reply[COL_HOME_NUMBER]) != 0))
+		add_phone_number(*contact, reply[COL_OTHER_NUMBER], TEL_TYPE_OTHER);
+}
+
+static void contact_add_emails(struct phonebook_contact **contact, char **reply)
+{
+	add_email(*contact, reply[COL_HOME_EMAIL], EMAIL_TYPE_HOME);
+	add_email(*contact, reply[COL_WORK_EMAIL], EMAIL_TYPE_WORK);
+	add_email(*contact, reply[COL_OTHER_EMAIL], EMAIL_TYPE_OTHER);
+}
+
+static void contact_add_addresses(struct phonebook_contact **contact, char **reply)
+{
+
+	char *home_addr, *work_addr, *other_addr;
+
+	home_addr = g_strdup_printf("%s;%s;%s;%s;%s;%s;%s",
+				reply[COL_HOME_ADDR_POBOX], reply[COL_HOME_ADDR_EXT],
+				reply[COL_HOME_ADDR_STREET], reply[COL_HOME_ADDR_LOCALITY],
+				reply[COL_HOME_ADDR_REGION], reply[COL_HOME_ADDR_CODE],
+				reply[COL_HOME_ADDR_COUNTRY]);
+
+	work_addr = g_strdup_printf("%s;%s;%s;%s;%s;%s;%s",
+				reply[COL_WORK_ADDR_POBOX], reply[COL_WORK_ADDR_EXT],
+				reply[COL_WORK_ADDR_STREET], reply[COL_WORK_ADDR_LOCALITY],
+				reply[COL_WORK_ADDR_REGION], reply[COL_WORK_ADDR_CODE],
+				reply[COL_WORK_ADDR_COUNTRY]);
+
+	other_addr = g_strdup_printf("%s;%s;%s;%s;%s;%s;%s",
+				reply[COL_OTHER_ADDR_POBOX], reply[COL_OTHER_ADDR_EXT],
+				reply[COL_OTHER_ADDR_STREET], reply[COL_OTHER_ADDR_LOCALITY],
+				reply[COL_OTHER_ADDR_REGION], reply[COL_OTHER_ADDR_CODE],
+				reply[COL_OTHER_ADDR_COUNTRY]);
+
+	add_address(*contact, home_addr, ADDR_TYPE_HOME);
+	add_address(*contact, work_addr, ADDR_TYPE_WORK);
+	add_address(*contact, other_addr, ADDR_TYPE_OTHER);
+
+	g_free(home_addr);
+	g_free(work_addr);
+	g_free(other_addr);
+}
+
+static void contact_add_organization(struct phonebook_contact **contact, char **reply)
+{
+	/* Adding fields connected by nco:hasAffiliation - they may be in
+	 * separate replies */
+	add_affiliation(&(*contact)->title, reply[COL_TITLE]);
+	add_affiliation(&(*contact)->company, reply[COL_ORG_NAME]);
+	add_affiliation(&(*contact)->department, reply[COL_ORG_DEPARTMENT]);
+	add_affiliation(&(*contact)->role, reply[COL_ORG_ROLE]);
+}
+
 static void pull_contacts(char **reply, int num_fields, void *user_data)
 {
 	struct phonebook_data *data = user_data;
@@ -1404,7 +1490,6 @@ static void pull_contacts(char **reply, int num_fields, void *user_data)
 	GString *vcards;
 	int last_index, i;
 	gboolean cdata_present = FALSE;
-	char *home_addr, *work_addr, *other_addr;
 	static char *temp_id = NULL;
 
 	if (num_fields < 0) {
@@ -1455,75 +1540,13 @@ static void pull_contacts(char **reply, int num_fields, void *user_data)
 		return;
 
 add_entry:
-	contact = g_new0(struct phonebook_contact, 1);
-	contact->fullname = g_strdup(reply[COL_FULL_NAME]);
-	contact->family = g_strdup(reply[COL_FAMILY_NAME]);
-	contact->given = g_strdup(reply[COL_GIVEN_NAME]);
-	contact->additional = g_strdup(reply[COL_ADDITIONAL_NAME]);
-	contact->prefix = g_strdup(reply[COL_NAME_PREFIX]);
-	contact->suffix = g_strdup(reply[COL_NAME_SUFFIX]);
-	contact->birthday = g_strdup(reply[COL_BIRTH_DATE]);
-	contact->nickname = g_strdup(reply[COL_NICKNAME]);
-	contact->website = g_strdup(reply[COL_URL]);
-	contact->photo = g_strdup(reply[COL_PHOTO]);
-	contact->company = g_strdup(reply[COL_ORG_NAME]);
-	contact->department = g_strdup(reply[COL_ORG_DEPARTMENT]);
-	contact->role = g_strdup(reply[COL_ORG_ROLE]);
-	contact->uid = g_strdup(reply[COL_UID]);
-	contact->title = g_strdup(reply[COL_TITLE]);
-
-	set_call_type(contact, reply[COL_DATE], reply[COL_SENT],
-							reply[COL_ANSWERED]);
+	contact_init(&contact, reply);
 
 add_numbers:
-	/* Adding phone numbers to contact struct */
-	add_phone_number(contact, reply[COL_HOME_NUMBER], TEL_TYPE_HOME);
-	add_phone_number(contact, reply[COL_WORK_NUMBER], TEL_TYPE_WORK);
-	add_phone_number(contact, reply[COL_FAX_NUMBER], TEL_TYPE_FAX);
-	add_phone_number(contact, reply[COL_CELL_NUMBER], TEL_TYPE_MOBILE);
-	if ((g_strcmp0(reply[COL_OTHER_NUMBER], reply[COL_CELL_NUMBER]) != 0) &&
-			(g_strcmp0(reply[COL_OTHER_NUMBER], reply[COL_WORK_NUMBER]) != 0) &&
-			(g_strcmp0(reply[COL_OTHER_NUMBER], reply[COL_HOME_NUMBER]) != 0))
-		add_phone_number(contact, reply[COL_OTHER_NUMBER], TEL_TYPE_OTHER);
-
-	/* Adding emails */
-	add_email(contact, reply[COL_HOME_EMAIL], EMAIL_TYPE_HOME);
-	add_email(contact, reply[COL_WORK_EMAIL], EMAIL_TYPE_WORK);
-	add_email(contact, reply[COL_OTHER_EMAIL], EMAIL_TYPE_OTHER);
-
-	/* Adding addresses */
-	home_addr = g_strdup_printf("%s;%s;%s;%s;%s;%s;%s",
-				reply[COL_HOME_ADDR_POBOX], reply[COL_HOME_ADDR_EXT],
-				reply[COL_HOME_ADDR_STREET], reply[COL_HOME_ADDR_LOCALITY],
-				reply[COL_HOME_ADDR_REGION], reply[COL_HOME_ADDR_CODE],
-				reply[COL_HOME_ADDR_COUNTRY]);
-
-	work_addr = g_strdup_printf("%s;%s;%s;%s;%s;%s;%s",
-				reply[COL_WORK_ADDR_POBOX], reply[COL_WORK_ADDR_EXT],
-				reply[COL_WORK_ADDR_STREET], reply[COL_WORK_ADDR_LOCALITY],
-				reply[COL_WORK_ADDR_REGION], reply[COL_WORK_ADDR_CODE],
-				reply[COL_WORK_ADDR_COUNTRY]);
-
-	other_addr = g_strdup_printf("%s;%s;%s;%s;%s;%s;%s",
-				reply[COL_OTHER_ADDR_POBOX], reply[COL_OTHER_ADDR_EXT],
-				reply[COL_OTHER_ADDR_STREET], reply[COL_OTHER_ADDR_LOCALITY],
-				reply[COL_OTHER_ADDR_REGION], reply[COL_OTHER_ADDR_CODE],
-				reply[COL_OTHER_ADDR_COUNTRY]);
-
-	add_address(contact, home_addr, ADDR_TYPE_HOME);
-	add_address(contact, work_addr, ADDR_TYPE_WORK);
-	add_address(contact, other_addr, ADDR_TYPE_OTHER);
-
-	g_free(home_addr);
-	g_free(work_addr);
-	g_free(other_addr);
-
-	/* Adding fields connected by nco:hasAffiliation - they may be in
-	 * separate replies */
-	add_affiliation(&contact->title, reply[COL_TITLE]);
-	add_affiliation(&contact->company, reply[COL_ORG_NAME]);
-	add_affiliation(&contact->department, reply[COL_ORG_DEPARTMENT]);
-	add_affiliation(&contact->role, reply[COL_ORG_ROLE]);
+	contact_add_numbers(&contact, reply);
+	contact_add_emails(&contact, reply);
+	contact_add_addresses(&contact, reply);
+	contact_add_organization(&contact, reply);
 
 	DBG("contact %p", contact);
 
Signature · 1 line
1.7.0.4
#1 lore raw

Hi Bartosz,

plugins/phonebook-tracker.c static void add_affiliation(char **field, const char *value) · line 1401

Quoted 4 lines · Bartosz Szatkowski
plugins/phonebook-tracker.c +4 -0
+static void contact_init(struct phonebook_contact **contact, char **reply)
+{
+	if (*contact == NULL)
+		*contact = g_new0(struct phonebook_contact, 1);

Could you do this initialization before calling this function and pass just a simple pointer to it. I think it'd be a cleaner approach.

Quoted 1 line

+static void contact_add_numbers(struct phonebook_contact **contact, char **reply)

Over 79 column line. Please split it.

plugins/phonebook-tracker.c static void add_affiliation(char **field, const char *value) · line 1433

Quoted 5 lines
plugins/phonebook-tracker.c +5 -0
+	if ((g_strcmp0(reply[COL_OTHER_NUMBER], reply[COL_CELL_NUMBER]) != 0) &&
+			(g_strcmp0(reply[COL_OTHER_NUMBER], reply[COL_WORK_NUMBER]) != 0) &&
+			(g_strcmp0(reply[COL_OTHER_NUMBER], reply[COL_HOME_NUMBER]) != 0))
+		add_phone_number(*contact, reply[COL_OTHER_NUMBER], TEL_TYPE_OTHER);
+}

Same here. However, I think you can make it considerably more readable with something like:

        if (g_strcmp0(reply[COL_OTHER_NUMBER], reply[COL_CELL_NUMBER]) == 0)
                return;

        if (g_strcmp0(reply[COL_OTHER_NUMBER], reply[COL_WORK_NUMBER]) == 0)
                return;

        if (g_strcmp0(reply[COL_OTHER_NUMBER], reply[COL_HOME_NUMBER]) == 0)
                return;

        add_phone_number(*contact, reply[COL_OTHER_NUMBER], TEL_TYPE_OTHER);
Quoted 1 line

+static void contact_add_emails(struct phonebook_contact **contact, char **reply)

Over 79 column line.

plugins/phonebook-tracker.c static void add_affiliation(char **field, const char *value) · line 1440

Quoted 5 lines
plugins/phonebook-tracker.c +5 -0
+{
+	add_email(*contact, reply[COL_HOME_EMAIL], EMAIL_TYPE_HOME);
+	add_email(*contact, reply[COL_WORK_EMAIL], EMAIL_TYPE_WORK);
+	add_email(*contact, reply[COL_OTHER_EMAIL], EMAIL_TYPE_OTHER);
+}

Why does the function take a pointer to pointer when a simple pointer would be enough?

Quoted 1 line

+static void contact_add_addresses(struct phonebook_contact **contact, char **reply)

Over 79 columns and unnecessary ** pointer again.

Quoted 7 lines
plugins/phonebook-tracker.c +7 -0
+	home_addr = g_strdup_printf("%s;%s;%s;%s;%s;%s;%s",
+				reply[COL_HOME_ADDR_POBOX], reply[COL_HOME_ADDR_EXT],
+				reply[COL_HOME_ADDR_STREET], reply[COL_HOME_ADDR_LOCALITY],
+				reply[COL_HOME_ADDR_REGION], reply[COL_HOME_ADDR_CODE],
+				reply[COL_HOME_ADDR_COUNTRY]);
+
+	work_addr = g_strdup_printf("%s;%s;%s;%s;%s;%s;%s",

plugins/phonebook-tracker.c static void add_affiliation(char **field, const char *value) · line 1463

Quoted 10 lines
plugins/phonebook-tracker.c +10 -0
+				reply[COL_WORK_ADDR_POBOX], reply[COL_WORK_ADDR_EXT],
+				reply[COL_WORK_ADDR_STREET], reply[COL_WORK_ADDR_LOCALITY],
+				reply[COL_WORK_ADDR_REGION], reply[COL_WORK_ADDR_CODE],
+				reply[COL_WORK_ADDR_COUNTRY]);
+
+	other_addr = g_strdup_printf("%s;%s;%s;%s;%s;%s;%s",
+				reply[COL_OTHER_ADDR_POBOX], reply[COL_OTHER_ADDR_EXT],
+				reply[COL_OTHER_ADDR_STREET], reply[COL_OTHER_ADDR_LOCALITY],
+				reply[COL_OTHER_ADDR_REGION], reply[COL_OTHER_ADDR_CODE],
+				reply[COL_OTHER_ADDR_COUNTRY]);

All of these are over 79 columns.

Quoted 1 line

+static void contact_add_organization(struct phonebook_contact **contact, char **reply)

Same here. And a simple * pointer is enough.

Johan