Bug 452614 nsIAbCard needs a method of searching all email addresses (well both of them). r/sr=bienvenu
authorMark Banner <bugzilla@standard8.plus.com>
Thu, 28 Aug 2008 17:50:27 +0100
changeset 211 8e89694ee82fe4ee8550b80b8abc93e6b77fd97b
parent 210 d80415828244cfe2676ec30770a52c82f16f4e9d
child 212 f483e0bf894c9415790b6ca7721a0df3cd0d1cc8
push idunknown
push userunknown
push dateunknown
bugs452614
Bug 452614 nsIAbCard needs a method of searching all email addresses (well both of them). r/sr=bienvenu
mailnews/addrbook/public/nsIAbCard.idl
mailnews/addrbook/src/nsAbCardProperty.cpp
mailnews/addrbook/src/nsAbOSXDirectory.mm
mailnews/addrbook/test/unit/test_basic_nsIAbCard.js
--- a/mailnews/addrbook/public/nsIAbCard.idl
+++ b/mailnews/addrbook/public/nsIAbCard.idl
@@ -82,17 +82,17 @@ interface nsIAbPreferMailFormat {
  * - Notes
  * - Integral properties:
  *   - LastModifiedDate
  *   - PopularityIndex
  *   - PreferMailFormat (see nsIAbPreferMailFormat)
  * - Boolean properties:
  *   - AllowRemoteContent
  */
-[scriptable, uuid(f5646fba-3d20-4fdf-90fb-9f2ee9899dbb)]
+[scriptable, uuid(fdac4023-cd19-4e75-9a88-8e48881076ea)]
 interface nsIAbCard : nsIAbItem {
   /**
    * A list of all the properties that this card has as an enumerator, whose
    * members are all nsIProperty objects.
    */
   readonly attribute nsISimpleEnumerator properties;
 
   /**
@@ -175,16 +175,29 @@ interface nsIAbCard : nsIAbItem {
    */
   attribute AString firstName;
   attribute AString lastName;
   attribute AString displayName;
   attribute AString primaryEmail;
   /** @} */
 
   /**
+   * Determines whether or not a card has the supplied email address in either
+   * of its PrimaryEmail or SecondEmail attributes.
+   *
+   * Note: This function is likely to be temporary whilst we work out proper
+   * APIs for multi-valued attributes in bug 118665.
+   *
+   * @param  aEmailAddress The email address to attempt to match against.
+   * @return               True if aEmailAddress matches any of the email
+   *                       addresses stored in the card.
+   */
+  boolean hasEmailAddress(in AUTF8String aEmailAddress);
+
+  /**
    * Translates a card into a specific format.
    * The following types are supported:
    * - base64xml
    * - xml
    * - vcard
    *
    * @param  aType          The type of item to translate the card into.
    * @return                A string containing the translated card.
--- a/mailnews/addrbook/src/nsAbCardProperty.cpp
+++ b/mailnews/addrbook/src/nsAbCardProperty.cpp
@@ -361,16 +361,50 @@ NS_IMETHODIMP nsAbCardProperty::GetPrima
   return rv;
 }
 
 NS_IMETHODIMP nsAbCardProperty::SetPrimaryEmail(const nsAString &aString)
 {
   return SetPropertyAsAString(kPriEmailProperty, aString);
 }
 
+NS_IMETHODIMP nsAbCardProperty::HasEmailAddress(const nsACString &aEmailAddress,
+                                                PRBool *aResult)
+{
+  NS_ENSURE_ARG_POINTER(aResult);
+
+  *aResult = PR_FALSE;
+
+  nsCString emailAddress;
+  nsresult rv = GetPropertyAsAUTF8String(kPriEmailProperty, emailAddress);
+#ifdef MOZILLA_INTERNAL_API
+  if (rv != NS_ERROR_NOT_AVAILABLE &&
+      emailAddress.Equals(aEmailAddress, nsCaseInsensitiveCStringComparator()))
+#else
+  if (rv != NS_ERROR_NOT_AVAILABLE &&
+      emailAddress.Equals(aEmailAddress, CaseInsensitiveCompare))
+#endif
+  {
+    *aResult = PR_TRUE;
+    return NS_OK;
+  }
+
+  rv = GetPropertyAsAUTF8String(k2ndEmailProperty, emailAddress);
+#ifdef MOZILLA_INTERNAL_API
+  if (rv != NS_ERROR_NOT_AVAILABLE &&
+      emailAddress.Equals(aEmailAddress, nsCaseInsensitiveCStringComparator()))
+#else
+  if (rv != NS_ERROR_NOT_AVAILABLE &&
+      emailAddress.Equals(aEmailAddress, CaseInsensitiveCompare))
+#endif
+    *aResult = PR_TRUE;
+
+  return NS_OK;
+}
+
 // This function may be overriden by derived classes for
 // nsAb*Card specific implementations.
 NS_IMETHODIMP nsAbCardProperty::Copy(nsIAbCard* srcCard)
 {
   NS_ENSURE_ARG_POINTER(srcCard);
 
   nsCOMPtr<nsISimpleEnumerator> properties;
   nsresult rv = srcCard->GetProperties(getter_AddRefs(properties));
--- a/mailnews/addrbook/src/nsAbOSXDirectory.mm
+++ b/mailnews/addrbook/src/nsAbOSXDirectory.mm
@@ -862,40 +862,21 @@ nsAbOSXDirectory::CardForEmailAddress(co
 
   nsCOMPtr<nsIAbCard> card;
 
   for (PRUint32 i = 0; i < length && !*aResult; ++i)
   {
     card = do_QueryElementAt(list, i, &rv);
     if (NS_SUCCEEDED(rv))
     {
-      nsCString email;
-      rv = card->GetPropertyAsAUTF8String(kPriEmailProperty, email);
-      if (NS_FAILED(rv))
-        continue;
+      PRBool hasEmailAddress = PR_FALSE;
 
-#ifdef MOZILLA_INTERNAL_API
-      if (email.Equals(aEmailAddress, nsCaseInsensitiveCStringComparator()))
-#else
-      if (email.Equals(aEmailAddress, CaseInsensitiveCompare))
-#endif
+      rv = card->HasEmailAddress(aEmailAddress, &hasEmailAddress);
+      if (NS_SUCCEEDED(rv) && hasEmailAddress)
         NS_IF_ADDREF(*aResult = card);
-      else
-      {
-        rv = card->GetPropertyAsAUTF8String(k2ndEmailProperty, email);
-        if (NS_FAILED(rv))
-          continue;
-
-#ifdef MOZILLA_INTERNAL_API
-        if (email.Equals(aEmailAddress, nsCaseInsensitiveCStringComparator()))
-#else
-          if (email.Equals(aEmailAddress, CaseInsensitiveCompare))
-#endif
-          NS_IF_ADDREF(*aResult = card);
-      }
     }
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsAbOSXDirectory::HasCard(nsIAbCard *aCard, PRBool *aHasCard)
 {
--- a/mailnews/addrbook/test/unit/test_basic_nsIAbCard.js
+++ b/mailnews/addrbook/test/unit/test_basic_nsIAbCard.js
@@ -3,16 +3,18 @@
  * Test suite for basic nsIAbCard functions.
  */
 
 // Intersperse these with UTF-8 values to check we handle them correctly.
 const kFNValue = "testFirst\u00D0";
 const kLNValue = "testLast";
 const kDNValue = "testDisplay\u00D1";
 const kEmailValue = "testEmail\u00D2@invalid.com";
+const kEmailValueLC = "testemail\u00D2@invalid.com";
+const kEmailValue2 = "test@test.invalid.com";
 // Email without the @ or anything after it.
 const kEmailReducedValue = "testEmail\u00D2";
 
 function run_test() {
   // Create a new card
   var card = Components.classes["@mozilla.org/addressbook/cardproperty;1"]
                          .createInstance(Components.interfaces.nsIAbCard);
 
@@ -92,9 +94,36 @@ function run_test() {
   card.setProperty("PhoneticLastName", "");
   do_check_eq(card.generatePhoneticName(false), kFNValue);
   do_check_eq(card.generatePhoneticName(true), kFNValue);
 
   card.setProperty("PhoneticFirstName", "");
   card.setProperty("PhoneticLastName", kLNValue);
   do_check_eq(card.generatePhoneticName(false), kLNValue);
   do_check_eq(card.generatePhoneticName(true), kLNValue);
+
+  // Test - hasEmailAddress
+
+  card.deleteProperty("PrimaryEmail");
+  card.deleteProperty("SecondEmail");
+
+  do_check_eq(card.hasEmailAddress(kEmailValue), false);
+  do_check_eq(card.hasEmailAddress(kEmailValueLC), false);
+  do_check_eq(card.hasEmailAddress(kEmailValue2), false);
+
+  card.setProperty("PrimaryEmail", kEmailValue);
+
+  do_check_eq(card.hasEmailAddress(kEmailValue), true);
+  do_check_eq(card.hasEmailAddress(kEmailValueLC), true);
+  do_check_eq(card.hasEmailAddress(kEmailValue2), false);
+
+  card.setProperty("SecondEmail", kEmailValue2);
+
+  do_check_eq(card.hasEmailAddress(kEmailValue), true);
+  do_check_eq(card.hasEmailAddress(kEmailValueLC), true);
+  do_check_eq(card.hasEmailAddress(kEmailValue2), true);
+
+  card.deleteProperty("PrimaryEmail");
+
+  do_check_eq(card.hasEmailAddress(kEmailValue), false);
+  do_check_eq(card.hasEmailAddress(kEmailValueLC), false);
+  do_check_eq(card.hasEmailAddress(kEmailValue2), true);
 }