Bug 745664 - Get localized string from prefs service for adressbook description. r=Neil, a=rkent
authorHiroyuki Ikezoe <hiikezoe@mozilla-japan.org>
Sun, 26 Apr 2015 14:13:00 +0200
changeset 25997 080b115a0f696dd115dfbf76bab5cc9f8209df5b
parent 25996 62b5abd46440f9d83a6503c7b731a0f94aed85f3
child 25998 47791aa91427ab3ceac3eaed34b9e35a418d8092
push id1850
push userclokep@gmail.com
push dateWed, 08 Mar 2017 19:29:12 +0000
treeherdercomm-esr52@028df196b2d9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersNeil, rkent
bugs745664
Bug 745664 - Get localized string from prefs service for adressbook description. r=Neil, a=rkent
mailnews/addrbook/src/nsDirPrefs.cpp
--- a/mailnews/addrbook/src/nsDirPrefs.cpp
+++ b/mailnews/addrbook/src/nsDirPrefs.cpp
@@ -37,16 +37,17 @@
  */
 
 /* Default settings for site-configurable prefs */
 #define kDefaultPosition 1
 static bool dir_IsServerDeleted(DIR_Server * server);
 
 static char *DIR_GetStringPref(const char *prefRoot, const char *prefLeaf, const char *defaultValue);
 static int32_t DIR_GetIntPref(const char *prefRoot, const char *prefLeaf, int32_t defaultValue);
+static char *DIR_GetLocalizedStringPref(const char *prefRoot, const char *prefLeaf);
 
 static char * dir_ConvertDescriptionToPrefName(DIR_Server * server);
 
 void DIR_SetFileName(char** filename, const char* leafName);
 static void DIR_SetIntPref(const char *prefRoot, const char *prefLeaf, int32_t value, int32_t defaultValue);
 static DIR_Server *dir_MatchServerPrefToServer(nsVoidArray *wholeList, const char *pref);
 static bool dir_ValidateAndAddNewServer(nsVoidArray *wholeList, const char *fullprefname);
 static void DIR_DeleteServerList(nsVoidArray *wholeList);
@@ -125,19 +126,21 @@ NS_IMETHODIMP DirPrefObserver::Observe(n
       if (position != server->position)
       {
         server->position = position;
         if (dir_IsServerDeleted(server))
           DIR_SetServerPosition(dir_ServerList, server, DIR_POS_DELETE);
       }
     }
 
-    if (id == idDescription)
+    if (id == idDescription) {
       // Ensure the local copy of the description is kept up to date.
-      server->description = DIR_GetStringPref(prefname, "description", nullptr);
+      PR_FREEIF(server->description);
+      server->description = DIR_GetLocalizedStringPref(prefname, nullptr);
+    }
   }
   /* If the server is not in the unified list, we may need to add it.  Servers
    * are only added when the position, serverName and description are valid.
    */
   else if (id == idPosition || id == idType || id == idDescription)
   {
     dir_ValidateAndAddNewServer(dir_ServerList, prefname);
   }
@@ -497,17 +500,17 @@ static bool dir_ValidateAndAddNewServer(
       int32_t dirType;
       char *t1 = nullptr, *t2 = nullptr;
 
       PL_strncpyz(prefname, fullprefname, endname - fullprefname + 1);
 
       dirType = DIR_GetIntPref(prefname, "dirType", -1);
       if (dirType != -1 &&
           DIR_GetIntPref(prefname, "position", 0) != 0 &&
-          (t1 = DIR_GetStringPref(prefname, "description", nullptr)) != nullptr)
+          (t1 = DIR_GetLocalizedStringPref(prefname, "description")) != nullptr)
       {
         if (dirType == PABDirectory ||
            (t2 = DIR_GetStringPref(prefname, "serverName",  nullptr)) != nullptr)
         {
           DIR_Server *server = (DIR_Server *)PR_Malloc(sizeof(DIR_Server));
           if (server)
           {
             DIR_InitServer(server, (DirectoryType)dirType);
@@ -794,34 +797,36 @@ static char *DIR_GetStringPref(const cha
         }
     }
     else
         value = defaultValue; 
 
     return ToNewCString(value);
 }
 
-/*
-  Get localized unicode string pref from properties file, convert into an UTF8 string 
-  since address book prefs store as UTF8 strings.  So far there are 2 default 
-  prefs stored in addressbook.properties.
-  "ldap_2.servers.pab.description"
-  "ldap_2.servers.history.description"
-*/
-
-static char *DIR_GetDescription(const char *prefRoot)
+/**
+ * Get localized unicode string pref from properties file, convert into an UTF8 string
+ * since address book prefs store as UTF8 strings. So far there are 2 default
+ * prefs stored in addressbook.properties.
+ * "ldap_2.servers.pab.description"
+ * "ldap_2.servers.history.description"
+ */
+static char *DIR_GetLocalizedStringPref(const char *prefRoot, const char *prefLeaf)
 {
   nsresult rv;
   nsCOMPtr<nsIPrefBranch> pPref(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
 
   if (NS_FAILED(rv))
     return nullptr;
 
   nsAutoCString prefLocation(prefRoot);
-  prefLocation.AppendLiteral(".description");
+  if (prefLeaf) {
+    prefLocation.Append('.');
+    prefLocation.Append(prefLeaf);
+  }
 
   nsString wvalue;
   nsCOMPtr<nsIPrefLocalizedString> locStr;
 
   rv = pPref->GetComplexValue(prefLocation.get(), NS_GET_IID(nsIPrefLocalizedString), getter_AddRefs(locStr));
   if (NS_SUCCEEDED(rv))
     rv = locStr->ToString(getter_Copies(wvalue));
 
@@ -1087,18 +1092,18 @@ static void DIR_GetPrefsForOneServer(DIR
 
   // this call fills in tempstring with the position pref, and
   // we then check to see if it's locked.
   server->position = DIR_GetIntPref (prefstring, "position", kDefaultPosition);
 
   // For default address books, this will get the name from the chrome
   // file referenced, for other address books it'll just retrieve it from prefs
   // as normal.
-  server->description = DIR_GetDescription(prefstring);
-  
+  server->description = DIR_GetLocalizedStringPref(prefstring, "description");
+
   server->dirType = (DirectoryType)DIR_GetIntPref (prefstring, "dirType", LDAPDirectory);
 
   server->fileName = DIR_GetStringPref (prefstring, "filename", "");
   // if we don't have a file name try and get one
   if (!server->fileName || !*(server->fileName)) 
     DIR_SetServerFileName (server);
   if (server->fileName && *server->fileName)
     DIR_ConvertServerFileName(server);