Bug 1490626 - Add new notifications we need for WebExt address book API; r=mkmelin
authorGeoff Lankow <geoff@darktrojan.net>
Thu, 27 Sep 2018 12:01:04 +1200
changeset 33240 60d7adb0d3bd2503f9de5ffe6627c713d27b54e6
parent 33239 2bab51e3b5c282ca391161b0bd6ef50d9dcfd735
child 33241 4dbfa038c9362bbda2010c883f15688f50a9626e
push id387
push userclokep@gmail.com
push dateMon, 10 Dec 2018 21:30:47 +0000
reviewersmkmelin
bugs1490626
Bug 1490626 - Add new notifications we need for WebExt address book API; r=mkmelin
mailnews/addrbook/src/nsAbMDBDirectory.cpp
mailnews/addrbook/src/nsAddrDatabase.cpp
--- a/mailnews/addrbook/src/nsAbMDBDirectory.cpp
+++ b/mailnews/addrbook/src/nsAbMDBDirectory.cpp
@@ -22,16 +22,18 @@
 #include "nsAppDirectoryServiceDefs.h"
 #include "nsDirectoryServiceUtils.h"
 #include "nsIFile.h"
 #include "nsComponentManagerUtils.h"
 #include "nsMemory.h"
 #include "nsArrayUtils.h"
 #include "nsUnicharUtils.h"
 #include "mozilla/DebugOnly.h"
+#include "nsIObserverService.h"
+#include "mozilla/Services.h"
 
 nsAbMDBDirectory::nsAbMDBDirectory(void):
      nsAbMDBDirProperty(),
      mPerformingQuery(false)
 {
 }
 
 nsAbMDBDirectory::~nsAbMDBDirectory(void)
@@ -657,20 +659,27 @@ NS_IMETHODIMP nsAbMDBDirectory::AddCard(
 
   nsresult rv = NS_OK;
   if (!mDatabase)
     rv = GetAbDatabase();
 
   if (NS_FAILED(rv) || !mDatabase)
     return NS_ERROR_FAILURE;
 
-  if (m_IsMailList)
+  if (m_IsMailList) {
     rv = mDatabase->CreateNewListCardAndAddToDB(this, m_dbRowID, card, true /* notify */);
-  else
+  } else {
     rv = mDatabase->CreateNewCardAndAddToDB(card, true, this);
+    nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
+    if (observerService) {
+      nsAutoCString thisUID;
+      this->GetUID(thisUID);
+      observerService->NotifyObservers(card, "addrbook-contact-created", NS_ConvertUTF8toUTF16(thisUID).get());
+    }
+  }
   NS_ENSURE_SUCCESS(rv, rv);
 
   mDatabase->Commit(nsAddrDBCommitType::kLargeCommit);
 
   NS_IF_ADDREF(*addedCard = card);
   return NS_OK;
 }
 
@@ -682,16 +691,24 @@ NS_IMETHODIMP nsAbMDBDirectory::ModifyCa
   if (!mDatabase)
   {
     rv = GetAbDatabase();
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   rv = mDatabase->EditCard(aModifiedCard, true, this);
   NS_ENSURE_SUCCESS(rv, rv);
+
+  nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
+  if (observerService) {
+    nsAutoCString thisUID;
+    this->GetUID(thisUID);
+    observerService->NotifyObservers(aModifiedCard, "addrbook-contact-updated", NS_ConvertUTF8toUTF16(thisUID).get());
+  }
+
   return mDatabase->Commit(nsAddrDBCommitType::kLargeCommit);
 }
 
 NS_IMETHODIMP nsAbMDBDirectory::DropCard(nsIAbCard* aCard, bool needToCopyCard)
 {
   NS_ENSURE_ARG_POINTER(aCard);
 
   if (mIsQueryURI)
@@ -726,16 +743,22 @@ NS_IMETHODIMP nsAbMDBDirectory::DropCard
       mDatabase->FindRowByCard(newCard, getter_AddRefs(cardRow));
       if (!cardRow)
         mDatabase->CreateNewCardAndAddToDB(newCard, true /* notify */, this);
       else
         mDatabase->InitCardFromRow(newCard, cardRow);
     }
     // since we didn't copy the card, we don't have to notify that it was inserted
     mDatabase->CreateNewListCardAndAddToDB(this, m_dbRowID, newCard, false /* notify */);
+    nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
+    if (observerService) {
+      nsAutoCString thisUID;
+      this->GetUID(thisUID);
+      observerService->NotifyObservers(newCard, "addrbook-list-member-added", NS_ConvertUTF8toUTF16(thisUID).get());
+    }
   }
   else {
     mDatabase->CreateNewCardAndAddToDB(newCard, true /* notify */, this);
   }
   mDatabase->Commit(nsAddrDBCommitType::kLargeCommit);
   return NS_OK;
 }
 
--- a/mailnews/addrbook/src/nsAddrDatabase.cpp
+++ b/mailnews/addrbook/src/nsAddrDatabase.cpp
@@ -27,16 +27,17 @@
 #include "nsIVariant.h"
 #include "nsCOMArray.h"
 #include "nsArrayEnumerator.h"
 #include "nsSimpleEnumerator.h"
 #include "nsIPrefService.h"
 #include "nsIPrefBranch.h"
 #include "nsIAbManager.h"
 #include "mozilla/Services.h"
+#include "nsIObserverService.h"
 
 #define ID_PAB_TABLE            1
 #define ID_DELETEDCARDS_TABLE           2
 
 // There's two books by default, although Mac may have one more, so set this
 // to three. Its not going to affect much, but will save us a few reallocations
 // when the cache is allocated.
 const uint32_t kInitialAddrDBCacheSize = 3;
@@ -1843,16 +1844,21 @@ NS_IMETHODIMP nsAddrDatabase::EditMailLi
   if (notify)
   {
     NotifyListEntryChange(AB_NotifyPropertyChanged, mailList);
 
     if (listCard)
     {
       NotifyCardEntryChange(AB_NotifyPropertyChanged, listCard, mailList);
     }
+
+    nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
+    if (observerService) {
+      observerService->NotifyObservers(mailList, "addrbook-list-updated", nullptr);
+    }
   }
 
   NS_RELEASE(pListRow);
   return NS_OK;
 }
 
 NS_IMETHODIMP nsAddrDatabase::ContainsMailList(nsIAbDirectory *mailList, bool *hasList)
 {