From a5c16ce813dc061e3e6709e5332e5d076ab3285e Mon Sep 17 00:00:00 2001 From: "sspitzer%netscape.com" Date: Fri, 16 Nov 2001 07:53:43 +0000 Subject: [PATCH] more work on #73868. converting ab from tree to outliner. restore selection after sorting. NOT PART OF THE BUILD. --- mailnews/addrbook/src/nsAbView.cpp | 139 ++++++++++++++++++++++------- mailnews/addrbook/src/nsAbView.h | 2 + 2 files changed, 109 insertions(+), 32 deletions(-) diff --git a/mailnews/addrbook/src/nsAbView.cpp b/mailnews/addrbook/src/nsAbView.cpp index af87b5a2b01f..bc6c6bb65af8 100644 --- a/mailnews/addrbook/src/nsAbView.cpp +++ b/mailnews/addrbook/src/nsAbView.cpp @@ -609,7 +609,29 @@ NS_IMETHODIMP nsAbView::SortBy(const PRUnichar *colID, const PRUnichar *sortDir) SortClosure closure; SetSortClosure(sortColumn.get(), sortDirection.get(), this, &closure); + nsCOMPtr selectedCards; + rv = GetSelectedCards(getter_AddRefs(selectedCards)); + NS_ENSURE_SUCCESS(rv,rv); + + nsCOMPtr indexCard; + + if (mOutlinerSelection) { + PRInt32 currentIndex = -1; + + rv = mOutlinerSelection->GetCurrentIndex(¤tIndex); + NS_ENSURE_SUCCESS(rv,rv); + + if (currentIndex != -1) { + rv = GetCardFromRow(currentIndex, getter_AddRefs(indexCard)); + NS_ENSURE_SUCCESS(rv,rv); + } + } + mCards.Sort(inplaceSortCallback, (void *)(&closure)); + + rv = ReselectCards(selectedCards, indexCard); + NS_ENSURE_SUCCESS(rv,rv); + mSortColumn = sortColumn.get(); mSortDirection = sortDirection.get(); } @@ -966,45 +988,98 @@ NS_IMETHODIMP nsAbView::GetSortColumn(PRUnichar **column) return NS_OK; } -NS_IMETHODIMP nsAbView::DeleteSelectedCards() +nsresult nsAbView::ReselectCards(nsISupportsArray *cards, nsIAbCard *indexCard) { - if (mOutlinerSelection) - { - PRInt32 selectionCount; - nsresult rv = mOutlinerSelection->GetRangeCount(&selectionCount); - NS_ENSURE_SUCCESS(rv,rv); + PRUint32 count; + PRUint32 i; - if (!selectionCount) - return NS_OK; + if (!mOutlinerSelection) + return NS_OK; - nsCOMPtr cardsToDelete; - NS_NewISupportsArray(getter_AddRefs(cardsToDelete)); + nsresult rv = mOutlinerSelection->ClearSelection(); + NS_ENSURE_SUCCESS(rv,rv); - for (PRInt32 i = 0; i < selectionCount; i++) - { - PRInt32 startRange; - PRInt32 endRange; - rv = mOutlinerSelection->GetRangeAt(i, &startRange, &endRange); - NS_ENSURE_SUCCESS(rv, NS_OK); - PRInt32 totalCards = mCards.Count(); - if (startRange >= 0 && startRange < totalCards) - { - for (PRInt32 rangeIndex = startRange; rangeIndex <= endRange && rangeIndex < totalCards; rangeIndex++) { - nsCOMPtr abCard; - rv = GetCardFromRow(rangeIndex, getter_AddRefs(abCard)); - NS_ENSURE_SUCCESS(rv,rv); + rv = cards->Count(&count); + NS_ENSURE_SUCCESS(rv, rv); - nsCOMPtr supports = do_QueryInterface(abCard, &rv); - NS_ENSURE_SUCCESS(rv,rv); - - rv = cardsToDelete->AppendElement(supports); - NS_ENSURE_SUCCESS(rv,rv); - } + for (i = 0; i < count; i++) { + nsCOMPtr cardSupports; + cardSupports = getter_AddRefs(cards->ElementAt(i)); + nsCOMPtr card = do_QueryInterface(cardSupports); + if (card) { + PRInt32 index = FindIndexForCard(card); + if (index != CARD_NOT_FOUND) { + mOutlinerSelection->RangedSelect(index, index, PR_TRUE /* augment */); } } - - rv = mDirectory->DeleteCards(cardsToDelete); - NS_ENSURE_SUCCESS(rv,rv); } + + // reset the index card, and ensure it is visible + if (indexCard) { + PRInt32 currentIndex = FindIndexForCard(indexCard); + rv = mOutlinerSelection->SetCurrentIndex(currentIndex); + NS_ENSURE_SUCCESS(rv, rv); + + if (mOutliner) { + rv = mOutliner->EnsureRowIsVisible(currentIndex); + NS_ENSURE_SUCCESS(rv, rv); + } + } + return NS_OK; } + +NS_IMETHODIMP nsAbView::DeleteSelectedCards() +{ + nsCOMPtr cardsToDelete; + + nsresult rv = GetSelectedCards(getter_AddRefs(cardsToDelete)); + NS_ENSURE_SUCCESS(rv,rv); + + rv = mDirectory->DeleteCards(cardsToDelete); + NS_ENSURE_SUCCESS(rv,rv); + return NS_OK; +} + +nsresult nsAbView::GetSelectedCards(nsISupportsArray **selectedCards) +{ + *selectedCards = nsnull; + if (!mOutlinerSelection) + return NS_OK; + + nsresult rv = NS_NewISupportsArray(selectedCards); + NS_ENSURE_SUCCESS(rv,rv); + + PRInt32 selectionCount; + rv = mOutlinerSelection->GetRangeCount(&selectionCount); + NS_ENSURE_SUCCESS(rv,rv); + + if (!selectionCount) + return NS_OK; + + NS_NewISupportsArray(selectedCards); + + for (PRInt32 i = 0; i < selectionCount; i++) + { + PRInt32 startRange; + PRInt32 endRange; + rv = mOutlinerSelection->GetRangeAt(i, &startRange, &endRange); + NS_ENSURE_SUCCESS(rv, NS_OK); + PRInt32 totalCards = mCards.Count(); + if (startRange >= 0 && startRange < totalCards) + { + for (PRInt32 rangeIndex = startRange; rangeIndex <= endRange && rangeIndex < totalCards; rangeIndex++) { + nsCOMPtr abCard; + rv = GetCardFromRow(rangeIndex, getter_AddRefs(abCard)); + NS_ENSURE_SUCCESS(rv,rv); + + nsCOMPtr supports = do_QueryInterface(abCard, &rv); + NS_ENSURE_SUCCESS(rv,rv); + + rv = (*selectedCards)->AppendElement(supports); + NS_ENSURE_SUCCESS(rv,rv); + } + } + } + return NS_OK; +} \ No newline at end of file diff --git a/mailnews/addrbook/src/nsAbView.h b/mailnews/addrbook/src/nsAbView.h index d9e150ff668f..afd67d7ecbf5 100644 --- a/mailnews/addrbook/src/nsAbView.h +++ b/mailnews/addrbook/src/nsAbView.h @@ -91,6 +91,8 @@ private: nsresult AddPrefObservers(); nsresult RemovePrefObservers(); nsresult SetGeneratedNameFormatFromPrefs(); + nsresult GetSelectedCards(nsISupportsArray **cards); + nsresult ReselectCards(nsISupportsArray *cards, nsIAbCard *indexCard); nsCString mURI; nsCOMPtr mDirectory;