[gnome-contacts] Extract field type sets into its own file



commit b770181c5814e293b91ae649dc9933deb7bca6b3
Author: Alexander Larsson <alexl redhat com>
Date:   Tue Jun 21 14:00:51 2011 +0200

    Extract field type sets into its own file

 src/Makefile.am                |    1 +
 src/contacts-contact-pane.vala |    8 +-
 src/contacts-contact.vala      |  146 -----------------------------------
 src/contacts-types.vala        |  164 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 169 insertions(+), 150 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index a5e1152..663e58b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -19,6 +19,7 @@ gnome_contacts_SOURCES = \
 	contacts-app.vala \
 	contacts-contact.vala \
 	contacts-contact-pane.vala \
+	contacts-types.vala \
 	contacts-list-pane.vala \
 	contacts-menu-button.vala \
 	contacts-store.vala \
diff --git a/src/contacts-contact-pane.vala b/src/contacts-contact-pane.vala
index 67d1620..7bc56b3 100644
--- a/src/contacts-contact-pane.vala
+++ b/src/contacts-contact-pane.vala
@@ -210,7 +210,7 @@ public class Contacts.ContactPane : EventBox {
       var emails = email_details.email_addresses;
       if (!emails.is_empty) {
 	foreach (var email in Contact.sort_fields (emails)) {
-	  var type = Contact.format_email_type (email);
+	  var type = TypeSet.general.format_type (email);
 	  layout.add_label_detail (type, email.value);
 	}
       }
@@ -235,7 +235,7 @@ public class Contacts.ContactPane : EventBox {
       var phone_numbers = phone_details.phone_numbers;
       if (!phone_numbers.is_empty) {
 	foreach (var p in Contact.sort_fields (phone_numbers)) {
-	  var type = Contact.format_phone_type (p);
+	  var type = TypeSet.phone.format_type (p);
 	  layout.add_label_detail (type, p.value);
 	}
       }
@@ -402,7 +402,7 @@ public class Contacts.ContactPane : EventBox {
     var emails = contact.individual.email_addresses;
     if (!emails.is_empty) {
       foreach (var email in Contact.sort_fields (emails)) {
-	var type = contact.format_email_type (email);
+	var type = TypeSet.general.format_type (email);
 	layout.add_label_detail (type, email.value);
 	var button = layout.add_button ("mail-unread-symbolic");
 	var email_addr = email.value;
@@ -437,7 +437,7 @@ public class Contacts.ContactPane : EventBox {
     var phone_numbers = contact.individual.phone_numbers;
     if (!phone_numbers.is_empty) {
       foreach (var p in Contact.sort_fields (phone_numbers)) {
-	var type = contact.format_phone_type (p);
+	var type = TypeSet.phone.format_type (p);
 	layout.add_label_detail (type, p.value);
       }
     }
diff --git a/src/contacts-contact.vala b/src/contacts-contact.vala
index 0ff5cdb..82985dd 100644
--- a/src/contacts-contact.vala
+++ b/src/contacts-contact.vala
@@ -278,152 +278,6 @@ public class Contacts.Contact : GLib.Object  {
     return int.parse (s);
   }
 
-  private struct PhoneData {
-    unowned string display_name;
-    unowned string types[2];
-  }
-
-  private static HashTable<unowned string, GLib.List> phone_types_hash;
-  public static string format_phone_type (FieldDetails detail) {
-    // List most specific first, always in upper case
-    const PhoneData[] data = {
-      { N_("Assistant"), { "X-EVOLUTION-ASSISTANT" } },
-      { N_("Work"), { "WORK", "VOICE" } },
-      // { N_("Business Phone 2"), { "WORK", "VOICE"},  1
-      { N_("Work Fax"), { "WORK", "FAX" } },
-      { N_("Callback"),   { "X-EVOLUTION-CALLBACK" } },
-      { N_("Car"),        { "CAR" } },
-      { N_("Company"),    { "X-EVOLUTION-COMPANY" } },
-      { N_("Home"),       { "HOME", "VOICE" } },
-      //{ N_("Home 2"),     { "HOME", "VOICE" } },  1),
-      { N_("Home Fax"),         { "HOME", "FAX" } },
-      { N_("ISDN"),             { "ISDN" } },
-      { N_("Mobile"),     { "CELL" } },
-      { N_("Other"),      { "VOICE" } },
-      { N_("Fax"),        { "FAX" } },
-      { N_("Pager"),            { "PAGER" } },
-      { N_("Radio"),            { "X-EVOLUTION-RADIO" } },
-      { N_("Telex"),            { "X-EVOLUTION-TELEX" } },
-      /* To translators: TTY is Teletypewriter */
-      { N_("TTY"),              { "X-EVOLUTION-TTYTDD" } },
-      { N_("Home"), { "HOME" } },
-      { N_("Work"), { "WORK" } }
-    };
-    if (detail.parameters.contains ("x-google-label")) {
-      return get_first_string (detail.parameters.get ("x-google-label"));
-    }
-    if (phone_types_hash == null) {
-      phone_types_hash = new HashTable<unowned string, GLib.List<PhoneData*> > (str_hash, str_equal);
-      for (int i = 0; i < data.length; i++) {
-	PhoneData *d = &data[i];
-	unowned GLib.List<PhoneData *> l = phone_types_hash.lookup (d.types[0]);
-	if (l != null) {
-	  l.append (d);
-	} else {
-	  GLib.List<PhoneData *> l2 = null;
-	  l2.append (d);
-	  phone_types_hash.insert (d.types[0], (owned) l2);
-	}
-      }
-    }
-
-    var i = detail.get_parameter_values ("type");
-    if (i == null || i.is_empty)
-      return _("Other");
-
-    var list = new Gee.ArrayList<string> ();
-    foreach (var s in detail.get_parameter_values ("type")) {
-      if (s.ascii_casecmp ("OTHER") == 0 ||
-	  s.ascii_casecmp ("PREF") == 0)
-	continue;
-      list.add (s.up ());
-    }
-
-    if (list.is_empty)
-      return _("Other");
-
-    list.sort ();
-
-    unowned GLib.List<PhoneData *>? l = phone_types_hash.lookup (list[0]);
-    foreach (var d in l) {
-      bool all_found = true;
-      for (int j = 0; j < 2 && d.types[j] != null; j++) {
-	if (!list.contains (d.types[j])) {
-	  all_found = false;
-	  break;
-	}
-      }
-      if (all_found)
-	return dgettext (Config.GETTEXT_PACKAGE, d.display_name);
-    }
-
-    return _("Other");
-  }
-
-  private struct EmailData {
-    unowned string display_name;
-    unowned string types[2];
-  }
-
-  private static HashTable<unowned string, GLib.List> email_types_hash;
-  public static string format_email_type (FieldDetails detail) {
-    // List most specific first, always in upper case
-    const EmailData[] data = {
-      { N_("Home"), { "HOME" } },
-      { N_("Work"), { "WORK" } }
-    };
-    if (detail.parameters.contains ("x-google-label")) {
-      return get_first_string (detail.parameters.get ("x-google-label"));
-    }
-    if (email_types_hash == null) {
-      email_types_hash = new HashTable<unowned string, GLib.List<EmailData*> > (str_hash, str_equal);
-      for (int i = 0; i < data.length; i++) {
-	EmailData *d = &data[i];
-	unowned GLib.List<EmailData *> l = email_types_hash.lookup (d.types[0]);
-	if (l != null) {
-	  l.append (d);
-	} else {
-	  GLib.List<EmailData *> l2 = null;
-	  l2.append (d);
-	  email_types_hash.insert (d.types[0], (owned) l2);
-	}
-      }
-    }
-
-    var i = detail.get_parameter_values ("type");
-    if (i == null || i.is_empty)
-      return _("Other");
-
-    var list = new Gee.ArrayList<string> ();
-    foreach (var s in detail.get_parameter_values ("type")) {
-      if (s.ascii_casecmp ("OTHER") == 0 ||
-	  s.ascii_casecmp ("INTERNET") == 0 ||
-	  s.ascii_casecmp ("PREF") == 0)
-	continue;
-      list.add (s.up ());
-    }
-
-    if (list.is_empty)
-      return _("Other");
-
-    list.sort ();
-
-    unowned GLib.List<EmailData *>? l = email_types_hash.lookup (list[0]);
-    foreach (var d in l) {
-      bool all_found = true;
-      for (int j = 0; j < 2 && d.types[j] != null; j++) {
-	if (!list.contains (d.types[j])) {
-	  all_found = false;
-	  break;
-	}
-      }
-      if (all_found)
-	return dgettext (Config.GETTEXT_PACKAGE, d.display_name);
-    }
-
-    return _("Other");
-  }
-
   public static GLib.List<FieldDetails> sort_fields (Set<FieldDetails> details) {
     GLib.List<FieldDetails> sorted = null;
     GLib.List<FieldDetails> pref = null;
diff --git a/src/contacts-types.vala b/src/contacts-types.vala
new file mode 100644
index 0000000..ef15aac
--- /dev/null
+++ b/src/contacts-types.vala
@@ -0,0 +1,164 @@
+/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */
+/*
+ * Copyright (C) 2011 Alexander Larsson <alexl redhat com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+using Gtk;
+using Gee;
+using Folks;
+
+public class Contacts.TypeCombo : Grid  {
+
+}
+
+public class Contacts.TypeSet : Object  {
+  const int MAX_TYPES = 3;
+  private struct InitData {
+    unowned string display_name;
+    unowned string types[3]; //MAX_TYPES
+  }
+
+  private static HashTable<unowned string, GLib.List<InitData *> > hash;
+
+  private TypeSet () {
+    hash = new HashTable<unowned string, GLib.List<InitData*> > (str_hash, str_equal);
+  }
+
+  private void add_data (InitData *data) {
+    unowned GLib.List<InitData *> l = hash.lookup (data.types[0]);
+    if (l != null) {
+      l.append (data);
+    } else {
+      GLib.List<InitData *> l2 = null;
+      l2.append (data);
+      hash.insert (data.types[0], (owned) l2);
+    }
+  }
+
+  private static TypeSet _general;
+  private static TypeSet _phone;
+
+  public static TypeSet general {
+    get {
+      const InitData[] data = {
+	// List most specific first, always in upper case
+	{ N_("Home"), { "HOME" } },
+	{ N_("Work"), { "WORK" } }
+      };
+
+      if (_general == null) {
+	_general = new TypeSet ();
+	for (int i = 0; i < data.length; i++) {
+	  _general.add_data (&data[i]);
+	}
+      }
+
+      return _general;
+    }
+  }
+
+  public static TypeSet phone {
+    get {
+      const InitData[] data = {
+	// List most specific first, always in upper case
+	{ N_("Assistant"), { "X-EVOLUTION-ASSISTANT" } },
+	{ N_("Work"), { "WORK", "VOICE" } },
+	// { N_("Business Phone 2"), { "WORK", "VOICE"},  1
+	{ N_("Work Fax"), { "WORK", "FAX" } },
+	{ N_("Callback"),   { "X-EVOLUTION-CALLBACK" } },
+	{ N_("Car"),        { "CAR" } },
+	{ N_("Company"),    { "X-EVOLUTION-COMPANY" } },
+	{ N_("Home"),       { "HOME", "VOICE" } },
+	//{ N_("Home 2"),     { "HOME", "VOICE" } },  1),
+	{ N_("Home Fax"),         { "HOME", "FAX" } },
+	{ N_("ISDN"),             { "ISDN" } },
+	{ N_("Mobile"),     { "CELL" } },
+	{ N_("Other"),      { "VOICE" } },
+	{ N_("Fax"),        { "FAX" } },
+	{ N_("Pager"),            { "PAGER" } },
+	{ N_("Radio"),            { "X-EVOLUTION-RADIO" } },
+	{ N_("Telex"),            { "X-EVOLUTION-TELEX" } },
+	/* To translators: TTY is Teletypewriter */
+	{ N_("TTY"),              { "X-EVOLUTION-TTYTDD" } },
+	{ N_("Home"), { "HOME" } },
+	{ N_("Work"), { "WORK" } }
+      };
+
+      if (_phone == null) {
+	_phone = new TypeSet ();
+	for (int i = 0; i < data.length; i++) {
+	  _phone.add_data (&data[i]);
+	}
+      }
+
+      return _phone;
+    }
+  }
+
+  private static string? get_first_string (Collection<string> collection) {
+    var i = collection.iterator();
+    if (i.next())
+      return i.get();
+    return null;
+  }
+
+  private static int get_first_string_as_int (Collection<string> collection) {
+    var s = get_first_string (collection);
+    if (s == null)
+      return int.MAX;
+    return int.parse (s);
+  }
+
+  public string format_type (FieldDetails detail) {
+    if (detail.parameters.contains ("x-google-label")) {
+      return get_first_string (detail.parameters.get ("x-google-label"));
+    }
+
+    var i = detail.get_parameter_values ("type");
+    if (i == null || i.is_empty)
+      return _("Other");
+
+    var list = new Gee.ArrayList<string> ();
+    foreach (var s in detail.get_parameter_values ("type")) {
+      if (s.ascii_casecmp ("OTHER") == 0 ||
+	  s.ascii_casecmp ("INTERNET") == 0 ||
+	  s.ascii_casecmp ("PREF") == 0)
+	continue;
+      list.add (s.up ());
+    }
+
+    if (list.is_empty)
+      return _("Other");
+
+    list.sort ();
+
+    unowned GLib.List<InitData *>? l = hash.lookup (list[0]);
+    foreach (var d in l) {
+      bool all_found = true;
+      for (int j = 0; j < MAX_TYPES && d.types[j] != null; j++) {
+	if (!list.contains (d.types[j])) {
+	  all_found = false;
+	  break;
+	}
+      }
+      if (all_found)
+	return dgettext (Config.GETTEXT_PACKAGE, d.display_name);
+    }
+
+    return _("Other");
+  }
+}



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