Bug 497680 - Fix FolderUriFromDirInProfile to just try to match paths. r+sr=bienvenu
authorSiddharth Agarwal <sid.bugzilla@gmail.com>
Fri, 19 Jun 2009 22:42:43 +0530
changeset 2890 5201453c8e57cfc8e7e7750b2a3370b11c9bab25
parent 2889 56d5e9e1c5d7609fb538f1fdf1ed498fdc16419d
child 2891 1fbbd90413d9181fe0bfd16ac5a8341fc2bfe607
push idunknown
push userunknown
push dateunknown
bugs497680
Bug 497680 - Fix FolderUriFromDirInProfile to just try to match paths. r+sr=bienvenu
mailnews/base/util/nsMsgUtils.cpp
--- a/mailnews/base/util/nsMsgUtils.cpp
+++ b/mailnews/base/util/nsMsgUtils.cpp
@@ -77,16 +77,17 @@
 #include "nsIMsgAccountManager.h"
 #include "nsIOutputStream.h"
 #include "nsMsgFileStream.h"
 #include "nsIFileURL.h"
 #include "nsNetUtil.h"
 #include "nsIMsgDatabase.h"
 #include "nsIMutableArray.h"
 #include "nsIMsgMailNewsUrl.h"
+#include "nsArrayUtils.h"
 
 static NS_DEFINE_CID(kImapUrlCID, NS_IMAPURL_CID);
 static NS_DEFINE_CID(kCMailboxUrl, NS_MAILBOXURL_CID);
 static NS_DEFINE_CID(kCNntpUrlCID, NS_NNTPURL_CID);
 
 #define ILLEGAL_FOLDER_CHARS ";#"
 #define ILLEGAL_FOLDER_CHARS_AS_FIRST_LETTER "."
 #define ILLEGAL_FOLDER_CHARS_AS_LAST_LETTER  ".~ "
@@ -1399,76 +1400,48 @@ PRBool MsgHostDomainIsTrusted(nsCString 
 
     domain = end + 1;
   } while (*end);
   return domainIsTrusted;
 }
 
 nsresult FolderUriFromDirInProfile(nsILocalFile *aLocalPath, nsACString &mailboxUri)
 {
-
   nsresult rv;
 
   nsCOMPtr<nsIMsgAccountManager> accountManager =
     do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);
-
   NS_ENSURE_SUCCESS(rv, rv);
-  nsCOMPtr<nsISupportsArray> serverArray;
-  accountManager->GetAllServers(getter_AddRefs(serverArray));
-
-  PRUint32 cnt;
-  rv = serverArray->Count(&cnt);
-  NS_ENSURE_SUCCESS(rv, rv);
-  PRInt32 count = cnt;
-  PRInt32 i;
 
-  for (i = 0; i < count; i++)
-  {
-    nsCOMPtr<nsIMsgIncomingServer> server = do_QueryElementAt(serverArray, i);
-    if (!server) continue;
+  nsCOMPtr<nsIArray> folderArray;
+  rv = accountManager->GetAllFolders(getter_AddRefs(folderArray));
+  NS_ENSURE_SUCCESS(rv, rv);
 
-    // get the base directory
-    nsCOMPtr<nsILocalFile> serverPath;
-    rv = server->GetLocalPath(getter_AddRefs(serverPath));
-    if (NS_FAILED(rv)) continue;
-
-    // compare, getting the relative descriptor
-    nsCAutoString pathStr;
-    aLocalPath->GetRelativeDescriptor(serverPath, pathStr);
+  PRUint32 count;
+  rv = folderArray->GetLength(&count);
+  NS_ENSURE_SUCCESS(rv, rv);
 
-    // if the argument directory is inside the main server directory
-    if (!StringBeginsWith(pathStr, NS_LITERAL_CSTRING("../")))
-    {
-      nsCString serverURI;
-      rv = server->GetServerURI(serverURI);
-      if (NS_FAILED(rv)) continue;
+  for (PRUint32 i = 0; i < count; i++)
+  {
+    nsCOMPtr<nsIMsgFolder> folder(do_QueryElementAt(folderArray, i, &rv));
+    NS_ENSURE_SUCCESS(rv, rv);
 
-      PRInt32 sbdIndex;
-      while((sbdIndex = pathStr.Find(".sbd", PR_TRUE)) != -1)
-        pathStr.Cut(sbdIndex, 4);
-
-      mailboxUri = serverURI;
-      mailboxUri.Append('/');
+    nsCOMPtr<nsILocalFile> folderPath;
+    rv = folder->GetFilePath(getter_AddRefs(folderPath));
+    NS_ENSURE_SUCCESS(rv, rv);
 
-      // If it's a local folder, escape the folder name
-      nsCAutoString localStoreType;
-      server->GetLocalStoreType(localStoreType);
-      if (localStoreType.Equals(NS_LITERAL_CSTRING("mailbox")))
-      {
-        nsCAutoString escapedPathStr;
-        MsgEscapeURL(pathStr, nsINetUtil::ESCAPE_URL_MINIMAL, escapedPathStr);
-        mailboxUri.Append(escapedPathStr);
-      }
-      else
-        mailboxUri.Append(pathStr);
+    // Check if we're equal
+    PRBool isEqual;
+    rv = folderPath->Equals(aLocalPath, &isEqual);
+    NS_ENSURE_SUCCESS(rv, rv);
 
-      break;
-    }
+    if (isEqual)
+      return folder->GetURI(mailboxUri);
   }
-  return mailboxUri.IsEmpty() ? NS_ERROR_FAILURE : NS_OK;
+  return NS_ERROR_FAILURE;
 }
 
 nsresult MsgGetLocalFileFromURI(const nsACString &aUTF8Path, nsILocalFile **aFile)
 {
   nsresult rv;
   nsCOMPtr<nsIURI> argURI;
   rv = NS_NewURI(getter_AddRefs(argURI), aUTF8Path);
   NS_ENSURE_SUCCESS(rv, rv);