Closed Bug 408180 Opened 17 years ago Closed 17 years ago

Generating a name from a card should be a function of nsIAbCard and not of nsIAddrBookSession

Categories

(MailNews Core :: Address Book, defect)

defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: standard8, Assigned: standard8)

Details

(Keywords: memory-footprint, perf)

Attachments

(1 file, 3 obsolete files)

On nsIAddrBookSession we currently have:

wstring generateNameFromCard(in nsIAbCard card, in long generateFormat);
wstring generatePhoneticNameFromCard(in nsIAbCard aCard, in boolean aLastNameFirst);

These don't use anything from the addr book session to generate the result. Additionally, all they are doing is calling functions in nsIAbCard to complete the result.

The wstring also means we have to do more work than necessary in some areas of the code.

Therefore we should move these to the nsIAbCard and generate the name from there which is much more logical anyway (and also saves us getting the addr book session service each time as nsAbView::GetCardValue currently does).

I'm also wondering if it would be worth providing a version of the function that can take the string formatting from the string bundle as an argument (http://bonsai.mozilla.org/cvsblame.cgi?file=/mozilla/mailnews/addrbook/src/nsAddrBookSession.cpp&rev=1.34&mark=222-226#202), so that things like nsAbView which may have to call the function for each card in the list can just pass in the string bundle format and hence we only have to get the bundle once for each time we display all cards in an ab (or less if we cache it).
Attached patch WIP v1 (obsolete) — Splinter Review
Although this will work, I'm not sure if this is what I'll go for yet.
Attached patch The fix (obsolete) — Splinter Review
I think I'm happy with this patch. This simplifies things over what we currently do. The caller can cache the string bundle if they wish, but they don't have to, and it also means we don't have to try and do management of a static member in nsAbCardProperty. Unit tests also included.
Attachment #293391 - Flags: review?(neil)
Comment on attachment 293391 [details] [diff] [review]
The fix

Excellent though it is, there is a possibility of improvment - apparently idl supports optional arguments these days, so consider making the bundle optional.
Attachment #293391 - Flags: review?(neil) → review+
Attached patch The fix v2 (obsolete) — Splinter Review
Slightly better version:

- Merged GenerateName and GenerateNameWithBundle into one function with an optional parameter.
- Reused constant for the string bundle uri in nsAbCardProperty
- included test case that I'd somehow forgotten to put in the previous two patches.

Also requesting sr this time at the same time.
Attachment #293010 - Attachment is obsolete: true
Attachment #293391 - Attachment is obsolete: true
Attachment #293538 - Flags: superreview?(bienvenu)
Attachment #293538 - Flags: review?(neil)
Comment on attachment 293538 [details] [diff] [review]
The fix v2

>+  // No need to check for aBundle present straight away, only do that if we're
>+  // actually going to use it.
>+  if (aGenerateFormat == kDisplayName) {
>+    aResult = m_DisplayName;
>+  }
>+  else if (!m_LastName.IsEmpty() && !m_FirstName.IsEmpty()) {
>+    nsCOMPtr<nsIStringBundle> bundle(aBundle);
>+    if (!bundle) {
>+      // As we know that generateNameWithBundle doesn't use the string
>+      // bundle in a display name format, we can just skip getting the service.
>+      if (aGenerateFormat != kDisplayName) {
>+        nsresult rv;
>+        nsCOMPtr<nsIStringBundleService> stringBundleService =
>+          do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv); 
>+        NS_ENSURE_SUCCESS(rv, rv);
>+        
>+        rv = stringBundleService->CreateBundle(sAddrbookProperties,
>+                                               getter_AddRefs(bundle));
>+        NS_ENSURE_SUCCESS(rv, rv);
>+      }
>+    }
>+    nsresult rv;
Nit: Too many rvs, just one will do.
You're checking aGenerateFormat against kDisplayName twice.

>+  else {
>+    if (m_LastName.Length())
>+      aResult = m_LastName;
>+    else {
>+      // name may be empty here, but that's ok.
>+      aResult = m_FirstName;
>+    }
>+  }
I meant to mention these last time, but forgot :-(
Nit: Please use !m_lastName.IsEmpty()
Nit: Please use else if without the extra {}s
You might like this way of writing the ifs which seems slightly simpler:
if (aGenerateFormat == kDisplayName) {
  aResult = m_DisplayName;
}
else if (m_LastName.IsEmpty()) {
  aResult = m_FirstName;
}
else if (m_FirstName.IsEmpty()) {
  aResult = m_LastName;
}
else {
  /* bundle the rest of the code in here */
}
Attachment #293538 - Flags: review?(neil) → review+
Attached patch The fix v3Splinter Review
Addressed Neil's comments. Carrying forward his r, requesting sr.
Attachment #293538 - Attachment is obsolete: true
Attachment #293727 - Flags: superreview?(bienvenu)
Attachment #293727 - Flags: review+
Attachment #293538 - Flags: superreview?(bienvenu)
Comment on attachment 293727 [details] [diff] [review]
The fix v3

thx, Mark.
Attachment #293727 - Flags: superreview?(bienvenu) → superreview+
Patch checked in -> fixed.
Status: NEW → RESOLVED
Closed: 17 years ago
Resolution: --- → FIXED
Flags: in-testsuite+
Product: Core → MailNews Core
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: