[gnome-contacts] Add and use is_set helper to check for non-empty strings



commit f55875d91b97ac7a8b51fdc584a83cb7dc19781b
Author: Alexander Larsson <alexl redhat com>
Date:   Tue Sep 13 16:17:00 2011 +0200

    Add and use is_set helper to check for non-empty strings

 src/contacts-contact-pane.vala |   29 +++++++------------
 src/contacts-contact.vala      |   61 +++++++++++++++------------------------
 src/contacts-utils.vala        |    7 ++++
 3 files changed, 42 insertions(+), 55 deletions(-)
---
diff --git a/src/contacts-contact-pane.vala b/src/contacts-contact-pane.vala
index 4f1bb57..c0ce537 100644
--- a/src/contacts-contact-pane.vala
+++ b/src/contacts-contact-pane.vala
@@ -690,7 +690,7 @@ public class Contacts.ContactPane : Grid {
     var name_details = persona as NameDetails;
     if (name_details != null) {
       var nick = name_details.nickname;
-      if (nick != null && nick != "") {
+      if (is_set (nick)) {
 	add_nickname_editor (nick_layout, nick);
       }
     }
@@ -701,7 +701,7 @@ public class Contacts.ContactPane : Grid {
     var alias_details = persona as AliasDetails;
     if (alias_details != null) {
       var alias = alias_details.alias;
-      if (alias != null && alias != "") {
+      if (is_set (alias)) {
 	add_alias_editor (alias_layout, alias);
       }
     }
@@ -848,8 +848,7 @@ public class Contacts.ContactPane : Grid {
 
       menu_button.popup.connect ( () => {
 	  if (nick_menu_item != null) {
-	    if (name_details.nickname != null &&
-		name_details.nickname != "")
+	    if (is_set (name_details.nickname))
 	      nick_menu_item.hide ();
 	    else
 	      nick_menu_item.show ();
@@ -1098,7 +1097,7 @@ public class Contacts.ContactPane : Grid {
 	view.get_buffer ().get_start_iter (out start);
 	view.get_buffer ().get_end_iter (out end);
 	var text = view.get_buffer ().get_text (start, end, true);
-	if (text.length > 0) {
+	if (is_set (text)) {
 	  var note = new NoteFieldDetails (text, null, uid);
 	  notes.add (note);
 	}
@@ -1335,7 +1334,7 @@ public class Contacts.ContactPane : Grid {
     contact.get_secondary_string (out secondary_sources);
 
     var nickname = contact.individual.nickname;
-    if (nickname != null && nickname != "" &&
+    if (is_set (nickname) &&
 	!("nickname" in secondary_sources))
       fields_layout.add_label_detail (_("Nickname"), nickname);
 
@@ -1448,8 +1447,7 @@ public class Contacts.ContactPane : Grid {
     var roles_details = contact.individual.roles;
     foreach (var role_detail in roles_details) {
       var role = role_detail.value;
-      if (role.organisation_name != null &&
-	  role.organisation_name != "" &&
+      if (is_set (role.organisation_name) &&
 	  !("organisation-name" in secondary_sources)) {
 	fields_layout.add_label (_("Company"));
 	fields_layout.add_detail (role.organisation_name);
@@ -1457,21 +1455,18 @@ public class Contacts.ContactPane : Grid {
       var org_units = role_detail.get_parameter_values ("org_unit");
       if (org_units != null) {
 	foreach (var org_unit in org_units) {
-	  if (org_unit != null &&
-	      org_unit != "") {
+	  if (is_set (org_unit)) {
 	    fields_layout.add_label (_("Department"));
 	    fields_layout.add_detail (org_unit);
 	  }
 	}
       }
-      if (role.role != null &&
-	  role.role != "" &&
+      if (is_set (role.role) &&
 	  !("role" in secondary_sources)) {
 	fields_layout.add_label (_("Profession"));
 	fields_layout.add_detail (role.role);
       }
-      if (role.title != null &&
-	  role.title != "" &&
+      if (is_set (role.title) &&
 	  !("title" in secondary_sources)) {
 	fields_layout.add_label (_("Title"));
 	fields_layout.add_detail (role.title);
@@ -1479,8 +1474,7 @@ public class Contacts.ContactPane : Grid {
       var managers = role_detail.get_parameter_values ("manager");
       if (managers != null) {
 	foreach (var manager in managers) {
-	  if (manager != null &&
-	      manager != "") {
+	  if (is_set (manager)) {
 	    fields_layout.add_label (_("Manager"));
 	    fields_layout.add_detail (manager);
 	  }
@@ -1489,8 +1483,7 @@ public class Contacts.ContactPane : Grid {
       var assistants = role_detail.get_parameter_values ("assistant");
       if (assistants != null) {
 	foreach (var assistant in assistants) {
-	  if (assistant != null &&
-	      assistant != "") {
+	  if (is_set (assistant)) {
 	    fields_layout.add_label (_("Assistant"));
 	    fields_layout.add_detail (assistant);
 	  }
diff --git a/src/contacts-contact.vala b/src/contacts-contact.vala
index 35fe266..aa3b357 100644
--- a/src/contacts-contact.vala
+++ b/src/contacts-contact.vala
@@ -137,27 +137,23 @@ public class Contacts.Contact : GLib.Object  {
   public string display_name {
     get {
       unowned string? name = individual.full_name;
-      if (name != null && name.length > 0)
+      if (is_set (name))
 	return name;
       unowned string? alias = individual.alias;
-      if (alias != null && alias.length > 0)
+      if (is_set (alias))
 	return alias;
       unowned string? nickname = individual.nickname;
-      if (nickname != null && nickname.length > 0)
+      if (is_set (nickname))
 	return nickname;
       foreach (var email in individual.email_addresses) {
 	string? e = email.value;
-	if (e != null && e.length > 0)
+	if (is_set (e))
 	  return email.value;
       }
       return "";
     }
   }
 
-  private static bool is_set (string? str) {
-    return str != null && str != "";
-  }
-
   // Synchronize with get_secondary_string_source ()
   public string? get_secondary_string (out string [] sources = null) {
     var nick = individual.nickname;
@@ -220,23 +216,23 @@ public class Contacts.Contact : GLib.Object  {
 
     if (name_details != null) {
       unowned string? name = name_details.full_name;
-      if (name != null && name.length > 0)
+      if (is_set (name))
 	return name;
     }
     if (alias_details != null) {
       unowned string? alias = alias_details.alias;
-      if (alias != null && alias.length > 0)
+      if (is_set (alias))
 	return alias;
     }
     if (name_details != null) {
       unowned string? nickname = name_details.nickname;
-      if (nickname != null && nickname.length > 0)
+      if (is_set (nickname))
 	return nickname;
     }
     if (email_details != null) {
       foreach (var email in email_details.email_addresses) {
 	string e = email.value;
-	if (e != null && e.length > 0)
+	if (is_set (e))
 	  return e;
       }
     }
@@ -562,39 +558,30 @@ public class Contacts.Contact : GLib.Object  {
 
   public static string[] format_address (PostalAddress addr) {
     string[] lines = {};
-    string str;
 
-    str = addr.street;
-    if (str != null && str.length > 0)
-      lines += str;
+    if (is_set (addr.street))
+      lines += addr.street;
 
-    str = addr.extension;
-    if (str != null && str.length > 0)
-      lines += str;
+    if (is_set (addr.extension))
+      lines += addr.extension;
 
-    str = addr.locality;
-    if (str != null && str.length > 0)
-      lines += str;
+    if (is_set (addr.locality))
+      lines += addr.locality;
 
-    str = addr.region;
-    if (str != null && str.length > 0)
-      lines += str;
+    if (is_set (addr.region))
+      lines += addr.region;
 
-    str = addr.postal_code;
-    if (str != null && str.length > 0)
-      lines += str;
+    if (is_set (addr.postal_code))
+      lines += addr.postal_code;
 
-    str = addr.po_box;
-    if (str != null && str.length > 0)
-      lines += str;
+    if (is_set (addr.po_box))
+      lines += addr.po_box;
 
-    str = addr.country;
-    if (str != null && str.length > 0)
-      lines += str;
+    if (is_set (addr.country))
+      lines += addr.country;
 
-    str = addr.address_format;
-    if (str != null && str.length > 0)
-      lines += str;
+    if (is_set (addr.address_format))
+      lines += addr.address_format;
 
     return lines;
   }
diff --git a/src/contacts-utils.vala b/src/contacts-utils.vala
index 4f748c1..dc5dd3d 100644
--- a/src/contacts-utils.vala
+++ b/src/contacts-utils.vala
@@ -21,6 +21,13 @@ using Folks;
 using Gee;
 using TelepathyGLib;
 
+namespace Contacts {
+  private static bool is_set (string? str) {
+    return str != null && str != "";
+  }
+}
+
+
 public class Contacts.Utils : Object {
   public static void compose_mail (string email) {
     try {



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