Bug 453908 - Add FindFolder() and GetOrCreateFolder() to nsMsgUtils. r=aceman
authorBen Campbell <benc@thunderbird.net>
Mon, 21 Jan 2019 14:51:42 +1300
changeset 34288 0b1d7c60056bd4b138c1e12c2b50d8ebfbfcdc98
parent 34287 5d2d5584e944f5432e142faaf68fa88d77549429
child 34289 a895eda2b2574153242e3b9e13b6d2fe15b2aae0
push id389
push userclokep@gmail.com
push dateMon, 18 Mar 2019 19:01:53 +0000
reviewersaceman
bugs453908
Bug 453908 - Add FindFolder() and GetOrCreateFolder() to nsMsgUtils. r=aceman
mailnews/base/util/nsMsgUtils.cpp
mailnews/base/util/nsMsgUtils.h
--- a/mailnews/base/util/nsMsgUtils.cpp
+++ b/mailnews/base/util/nsMsgUtils.cpp
@@ -743,29 +743,60 @@ bool WeAreOffline()
   nsCOMPtr <nsIIOService> ioService =
     mozilla::services::GetIOService();
   if (ioService)
     ioService->GetOffline(&offline);
 
   return offline;
 }
 
-nsresult GetExistingFolder(const nsCString& aFolderURI, nsIMsgFolder **aFolder)
+// Find a folder by URL. If it doesn't exist, null will be returned
+// via aFolder.
+nsresult FindFolder(const nsACString& aFolderURI, nsIMsgFolder **aFolder)
 {
   NS_ENSURE_ARG_POINTER(aFolder);
 
   *aFolder = nullptr;
 
   nsresult rv;
   nsCOMPtr<nsIFolderLookupService> fls(do_GetService(NSIFLS_CONTRACTID, &rv));
   NS_ENSURE_SUCCESS(rv, rv);
 
+  // GetFolderForURL returns NS_OK and null for non-existent folders
   rv = fls->GetFolderForURL(aFolderURI, aFolder);
   NS_ENSURE_SUCCESS(rv, rv);
 
+  return NS_OK;
+}
+
+// Fetch an existing folder by URL
+// The returned aFolder will be non-null if and only if result is NS_OK.
+// NS_OK - folder was found
+// NS_MSG_FOLDER_MISSING - if aFolderURI not found
+nsresult GetExistingFolder(const nsACString& aFolderURI, nsIMsgFolder **aFolder)
+{
+  nsresult rv = FindFolder(aFolderURI, aFolder);
+  NS_ENSURE_SUCCESS(rv, rv);
+  return *aFolder ? NS_OK : NS_MSG_ERROR_FOLDER_MISSING;
+}
+
+
+nsresult GetOrCreateFolder(const nsACString& aFolderURI, nsIMsgFolder **aFolder)
+{
+  NS_ENSURE_ARG_POINTER(aFolder);
+
+  *aFolder = nullptr;
+
+  nsresult rv;
+  nsCOMPtr<nsIFolderLookupService> fls(do_GetService(NSIFLS_CONTRACTID, &rv));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = fls->GetOrCreateFolderForURL(aFolderURI, aFolder);
+  NS_ENSURE_SUCCESS(rv, rv);
+
   return *aFolder ? NS_OK : NS_ERROR_FAILURE;
 }
 
 bool IsAFromSpaceLine(char *start, const char *end)
 {
   bool rv = false;
   while ((start < end) && (*start == '>'))
     start++;
--- a/mailnews/base/util/nsMsgUtils.h
+++ b/mailnews/base/util/nsMsgUtils.h
@@ -100,18 +100,30 @@ NS_MSG_BASE char * NS_MsgSACat(char **de
 NS_MSG_BASE nsresult NS_MsgEscapeEncodeURLPath(const nsAString& aStr,
                                                nsCString& aResult);
 
 NS_MSG_BASE nsresult NS_MsgDecodeUnescapeURLPath(const nsACString& aPath,
                                                  nsAString& aResult);
 
 NS_MSG_BASE bool WeAreOffline();
 
-// Check if a folder with aFolderUri exists
-NS_MSG_BASE nsresult GetExistingFolder(const nsCString& aFolderURI, nsIMsgFolder **aFolder);
+// Get a folder by Uri, returning null if it doesn't exist (or if some
+// error occurs). A missing folder is not considered an error.
+NS_MSG_BASE nsresult FindFolder(const nsACString& aFolderURI, nsIMsgFolder **aFolder);
+
+// Get a folder by Uri.
+// A missing folder is considered to be an error.
+// Returns a non-null folder if and only if result is NS_OK.
+NS_MSG_BASE nsresult GetExistingFolder(const nsACString& aFolderURI, nsIMsgFolder **aFolder);
+
+// Get a folder by Uri, creating it if it doesn't already exist.
+// An error is returned if a folder cannot be found or created.
+// Created folders will be 'dangling' folders (ie not connected to a
+// parent).
+NS_MSG_BASE nsresult GetOrCreateFolder(const nsACString& aFolderURI, nsIMsgFolder **aFolder);
 
 // Escape lines starting with "From ", ">From ", etc. in a buffer.
 NS_MSG_BASE nsresult EscapeFromSpaceLine(nsIOutputStream *ouputStream, char *start, const char *end);
 NS_MSG_BASE bool IsAFromSpaceLine(char *start, const char *end);
 
 NS_MSG_BASE nsresult NS_GetPersistentFile(const char *relPrefName,
                                           const char *absPrefName,
                                           const char *dirServiceProp, // Can be NULL