1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/xul/templates/src/nsXULTreeBuilder.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1898 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nscore.h" 1.10 +#include "nsError.h" 1.11 +#include "nsIContent.h" 1.12 +#include "nsINodeInfo.h" 1.13 +#include "nsIDOMElement.h" 1.14 +#include "nsILocalStore.h" 1.15 +#include "nsIBoxObject.h" 1.16 +#include "nsITreeBoxObject.h" 1.17 +#include "nsITreeSelection.h" 1.18 +#include "nsITreeColumns.h" 1.19 +#include "nsITreeView.h" 1.20 +#include "nsTreeUtils.h" 1.21 +#include "nsIServiceManager.h" 1.22 +#include "nsReadableUtils.h" 1.23 +#include "nsQuickSort.h" 1.24 +#include "nsTreeRows.h" 1.25 +#include "nsTemplateRule.h" 1.26 +#include "nsTemplateMatch.h" 1.27 +#include "nsGkAtoms.h" 1.28 +#include "nsXULContentUtils.h" 1.29 +#include "nsXULTemplateBuilder.h" 1.30 +#include "nsIXULSortService.h" 1.31 +#include "nsTArray.h" 1.32 +#include "nsUnicharUtils.h" 1.33 +#include "nsNameSpaceManager.h" 1.34 +#include "nsDOMClassInfoID.h" 1.35 +#include "nsWhitespaceTokenizer.h" 1.36 +#include "nsTreeContentView.h" 1.37 + 1.38 +// For security check 1.39 +#include "nsIDocument.h" 1.40 + 1.41 +/** 1.42 + * A XUL template builder that serves as an tree view, allowing 1.43 + * (pretty much) arbitrary RDF to be presented in an tree. 1.44 + */ 1.45 +class nsXULTreeBuilder : public nsXULTemplateBuilder, 1.46 + public nsIXULTreeBuilder, 1.47 + public nsINativeTreeView 1.48 +{ 1.49 +public: 1.50 + // nsISupports 1.51 + NS_DECL_ISUPPORTS_INHERITED 1.52 + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsXULTreeBuilder, nsXULTemplateBuilder) 1.53 + 1.54 + // nsIXULTreeBuilder 1.55 + NS_DECL_NSIXULTREEBUILDER 1.56 + 1.57 + // nsITreeView 1.58 + NS_DECL_NSITREEVIEW 1.59 + // nsINativeTreeView: Untrusted code can use us 1.60 + NS_IMETHOD EnsureNative() { return NS_OK; } 1.61 + 1.62 + // nsIMutationObserver 1.63 + NS_DECL_NSIMUTATIONOBSERVER_NODEWILLBEDESTROYED 1.64 + 1.65 +protected: 1.66 + friend nsresult 1.67 + NS_NewXULTreeBuilder(nsISupports* aOuter, REFNSIID aIID, void** aResult); 1.68 + 1.69 + nsXULTreeBuilder(); 1.70 + 1.71 + /** 1.72 + * Uninitialize the template builder 1.73 + */ 1.74 + virtual void Uninit(bool aIsFinal); 1.75 + 1.76 + /** 1.77 + * Get sort variables from the active <treecol> 1.78 + */ 1.79 + nsresult 1.80 + EnsureSortVariables(); 1.81 + 1.82 + virtual nsresult 1.83 + RebuildAll(); 1.84 + 1.85 + /** 1.86 + * Given a row, use the row's match to figure out the appropriate 1.87 + * <treerow> in the rule's <action>. 1.88 + */ 1.89 + nsresult 1.90 + GetTemplateActionRowFor(int32_t aRow, nsIContent** aResult); 1.91 + 1.92 + /** 1.93 + * Given a row and a column ID, use the row's match to figure out 1.94 + * the appropriate <treecell> in the rule's <action>. 1.95 + */ 1.96 + nsresult 1.97 + GetTemplateActionCellFor(int32_t aRow, nsITreeColumn* aCol, nsIContent** aResult); 1.98 + 1.99 + /** 1.100 + * Return the resource corresponding to a row in the tree. 1.101 + */ 1.102 + nsresult 1.103 + GetResourceFor(int32_t aRow, nsIRDFResource** aResource); 1.104 + 1.105 + /** 1.106 + * Open a container row, inserting the container's children into 1.107 + * the view. 1.108 + */ 1.109 + nsresult 1.110 + OpenContainer(int32_t aIndex, nsIXULTemplateResult* aResult); 1.111 + 1.112 + /** 1.113 + * Helper for OpenContainer, recursively open subtrees, remembering 1.114 + * persisted ``open'' state 1.115 + */ 1.116 + nsresult 1.117 + OpenSubtreeOf(nsTreeRows::Subtree* aSubtree, 1.118 + int32_t aIndex, 1.119 + nsIXULTemplateResult *aResult, 1.120 + int32_t* aDelta); 1.121 + 1.122 + nsresult 1.123 + OpenSubtreeForQuerySet(nsTreeRows::Subtree* aSubtree, 1.124 + int32_t aIndex, 1.125 + nsIXULTemplateResult *aResult, 1.126 + nsTemplateQuerySet* aQuerySet, 1.127 + int32_t* aDelta, 1.128 + nsTArray<int32_t>& open); 1.129 + 1.130 + /** 1.131 + * Close a container row, removing the container's childrem from 1.132 + * the view. 1.133 + */ 1.134 + nsresult 1.135 + CloseContainer(int32_t aIndex); 1.136 + 1.137 + /** 1.138 + * Remove the matches for the rows in a subtree 1.139 + */ 1.140 + nsresult 1.141 + RemoveMatchesFor(nsTreeRows::Subtree& subtree); 1.142 + 1.143 + /** 1.144 + * Helper methods that determine if the specified container is open. 1.145 + */ 1.146 + nsresult 1.147 + IsContainerOpen(nsIXULTemplateResult *aResult, bool* aOpen); 1.148 + 1.149 + nsresult 1.150 + IsContainerOpen(nsIRDFResource* aResource, bool* aOpen); 1.151 + 1.152 + /** 1.153 + * A sorting callback for NS_QuickSort(). 1.154 + */ 1.155 + static int 1.156 + Compare(const void* aLeft, const void* aRight, void* aClosure); 1.157 + 1.158 + /** 1.159 + * The real sort routine 1.160 + */ 1.161 + int32_t 1.162 + CompareResults(nsIXULTemplateResult* aLeft, nsIXULTemplateResult* aRight); 1.163 + 1.164 + /** 1.165 + * Sort the specified subtree, and recursively sort any subtrees 1.166 + * beneath it. 1.167 + */ 1.168 + nsresult 1.169 + SortSubtree(nsTreeRows::Subtree* aSubtree); 1.170 + 1.171 + NS_IMETHOD 1.172 + HasGeneratedContent(nsIRDFResource* aResource, 1.173 + nsIAtom* aTag, 1.174 + bool* aGenerated); 1.175 + 1.176 + // GetInsertionLocations, ReplaceMatch and SynchronizeResult are inherited 1.177 + // from nsXULTemplateBuilder 1.178 + 1.179 + /** 1.180 + * Return true if the result can be inserted into the template as a new 1.181 + * row. 1.182 + */ 1.183 + bool 1.184 + GetInsertionLocations(nsIXULTemplateResult* aResult, 1.185 + nsCOMArray<nsIContent>** aLocations); 1.186 + 1.187 + /** 1.188 + * Implement result replacement 1.189 + */ 1.190 + virtual nsresult 1.191 + ReplaceMatch(nsIXULTemplateResult* aOldResult, 1.192 + nsTemplateMatch* aNewMatch, 1.193 + nsTemplateRule* aNewMatchRule, 1.194 + void *aContext); 1.195 + 1.196 + /** 1.197 + * Implement match synchronization 1.198 + */ 1.199 + virtual nsresult 1.200 + SynchronizeResult(nsIXULTemplateResult* aResult); 1.201 + 1.202 + /** 1.203 + * The tree's box object, used to communicate with the front-end. 1.204 + */ 1.205 + nsCOMPtr<nsITreeBoxObject> mBoxObject; 1.206 + 1.207 + /** 1.208 + * The tree's selection object. 1.209 + */ 1.210 + nsCOMPtr<nsITreeSelection> mSelection; 1.211 + 1.212 + /** 1.213 + * The datasource that's used to persist open folder information 1.214 + */ 1.215 + nsCOMPtr<nsIRDFDataSource> mPersistStateStore; 1.216 + 1.217 + /** 1.218 + * The rows in the view 1.219 + */ 1.220 + nsTreeRows mRows; 1.221 + 1.222 + /** 1.223 + * The currently active sort variable 1.224 + */ 1.225 + nsCOMPtr<nsIAtom> mSortVariable; 1.226 + 1.227 + enum Direction { 1.228 + eDirection_Descending = -1, 1.229 + eDirection_Natural = 0, 1.230 + eDirection_Ascending = +1 1.231 + }; 1.232 + 1.233 + /** 1.234 + * The currently active sort order 1.235 + */ 1.236 + Direction mSortDirection; 1.237 + 1.238 + /* 1.239 + * Sort hints (compare case, etc) 1.240 + */ 1.241 + uint32_t mSortHints; 1.242 + 1.243 + /** 1.244 + * The builder observers. 1.245 + */ 1.246 + nsCOMArray<nsIXULTreeBuilderObserver> mObservers; 1.247 +}; 1.248 + 1.249 +//---------------------------------------------------------------------- 1.250 + 1.251 +nsresult 1.252 +NS_NewXULTreeBuilder(nsISupports* aOuter, REFNSIID aIID, void** aResult) 1.253 +{ 1.254 + *aResult = nullptr; 1.255 + 1.256 + NS_PRECONDITION(aOuter == nullptr, "no aggregation"); 1.257 + if (aOuter) 1.258 + return NS_ERROR_NO_AGGREGATION; 1.259 + 1.260 + nsresult rv; 1.261 + nsXULTreeBuilder* result = new nsXULTreeBuilder(); 1.262 + if (! result) 1.263 + return NS_ERROR_OUT_OF_MEMORY; 1.264 + 1.265 + NS_ADDREF(result); // stabilize 1.266 + 1.267 + rv = result->InitGlobals(); 1.268 + 1.269 + if (NS_SUCCEEDED(rv)) 1.270 + rv = result->QueryInterface(aIID, aResult); 1.271 + 1.272 + NS_RELEASE(result); 1.273 + return rv; 1.274 +} 1.275 + 1.276 +NS_IMPL_ADDREF_INHERITED(nsXULTreeBuilder, nsXULTemplateBuilder) 1.277 +NS_IMPL_RELEASE_INHERITED(nsXULTreeBuilder, nsXULTemplateBuilder) 1.278 + 1.279 +NS_IMPL_CYCLE_COLLECTION_INHERITED(nsXULTreeBuilder, nsXULTemplateBuilder, 1.280 + mBoxObject, 1.281 + mSelection, 1.282 + mPersistStateStore, 1.283 + mObservers) 1.284 + 1.285 +DOMCI_DATA(XULTreeBuilder, nsXULTreeBuilder) 1.286 + 1.287 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsXULTreeBuilder) 1.288 + NS_INTERFACE_MAP_ENTRY(nsIXULTreeBuilder) 1.289 + NS_INTERFACE_MAP_ENTRY(nsITreeView) 1.290 + NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(XULTreeBuilder) 1.291 +NS_INTERFACE_MAP_END_INHERITING(nsXULTemplateBuilder) 1.292 + 1.293 + 1.294 +nsXULTreeBuilder::nsXULTreeBuilder() 1.295 + : mSortDirection(eDirection_Natural), mSortHints(0) 1.296 +{ 1.297 +} 1.298 + 1.299 +void 1.300 +nsXULTreeBuilder::Uninit(bool aIsFinal) 1.301 +{ 1.302 + int32_t count = mRows.Count(); 1.303 + mRows.Clear(); 1.304 + 1.305 + if (mBoxObject) { 1.306 + mBoxObject->BeginUpdateBatch(); 1.307 + mBoxObject->RowCountChanged(0, -count); 1.308 + if (mBoxObject) { 1.309 + mBoxObject->EndUpdateBatch(); 1.310 + } 1.311 + } 1.312 + 1.313 + nsXULTemplateBuilder::Uninit(aIsFinal); 1.314 +} 1.315 + 1.316 + 1.317 +//---------------------------------------------------------------------- 1.318 +// 1.319 +// nsIXULTreeBuilder methods 1.320 +// 1.321 + 1.322 +NS_IMETHODIMP 1.323 +nsXULTreeBuilder::GetResourceAtIndex(int32_t aRowIndex, nsIRDFResource** aResult) 1.324 +{ 1.325 + if (aRowIndex < 0 || aRowIndex >= mRows.Count()) 1.326 + return NS_ERROR_INVALID_ARG; 1.327 + 1.328 + return GetResourceFor(aRowIndex, aResult); 1.329 +} 1.330 + 1.331 +NS_IMETHODIMP 1.332 +nsXULTreeBuilder::GetIndexOfResource(nsIRDFResource* aResource, int32_t* aResult) 1.333 +{ 1.334 + NS_ENSURE_ARG_POINTER(aResource); 1.335 + nsTreeRows::iterator iter = mRows.FindByResource(aResource); 1.336 + if (iter == mRows.Last()) 1.337 + *aResult = -1; 1.338 + else 1.339 + *aResult = iter.GetRowIndex(); 1.340 + return NS_OK; 1.341 +} 1.342 + 1.343 +NS_IMETHODIMP 1.344 +nsXULTreeBuilder::AddObserver(nsIXULTreeBuilderObserver* aObserver) 1.345 +{ 1.346 + return mObservers.AppendObject(aObserver) ? NS_OK : NS_ERROR_FAILURE; 1.347 +} 1.348 + 1.349 +NS_IMETHODIMP 1.350 +nsXULTreeBuilder::RemoveObserver(nsIXULTreeBuilderObserver* aObserver) 1.351 +{ 1.352 + return mObservers.RemoveObject(aObserver) ? NS_OK : NS_ERROR_FAILURE; 1.353 +} 1.354 + 1.355 +NS_IMETHODIMP 1.356 +nsXULTreeBuilder::Sort(nsIDOMElement* aElement) 1.357 +{ 1.358 + nsCOMPtr<nsIContent> header = do_QueryInterface(aElement); 1.359 + if (! header) 1.360 + return NS_ERROR_FAILURE; 1.361 + 1.362 + if (header->AttrValueIs(kNameSpaceID_None, nsGkAtoms::sortLocked, 1.363 + nsGkAtoms::_true, eCaseMatters)) 1.364 + return NS_OK; 1.365 + 1.366 + nsAutoString sort; 1.367 + header->GetAttr(kNameSpaceID_None, nsGkAtoms::sort, sort); 1.368 + 1.369 + if (sort.IsEmpty()) 1.370 + return NS_OK; 1.371 + 1.372 + // Grab the new sort variable 1.373 + mSortVariable = do_GetAtom(sort); 1.374 + 1.375 + nsAutoString hints; 1.376 + header->GetAttr(kNameSpaceID_None, nsGkAtoms::sorthints, hints); 1.377 + 1.378 + bool hasNaturalState = true; 1.379 + nsWhitespaceTokenizer tokenizer(hints); 1.380 + while (tokenizer.hasMoreTokens()) { 1.381 + const nsDependentSubstring& token(tokenizer.nextToken()); 1.382 + if (token.EqualsLiteral("comparecase")) 1.383 + mSortHints |= nsIXULSortService::SORT_COMPARECASE; 1.384 + else if (token.EqualsLiteral("integer")) 1.385 + mSortHints |= nsIXULSortService::SORT_INTEGER; 1.386 + else if (token.EqualsLiteral("twostate")) 1.387 + hasNaturalState = false; 1.388 + } 1.389 + 1.390 + // Cycle the sort direction 1.391 + nsAutoString dir; 1.392 + header->GetAttr(kNameSpaceID_None, nsGkAtoms::sortDirection, dir); 1.393 + 1.394 + if (dir.EqualsLiteral("ascending")) { 1.395 + dir.AssignLiteral("descending"); 1.396 + mSortDirection = eDirection_Descending; 1.397 + } 1.398 + else if (hasNaturalState && dir.EqualsLiteral("descending")) { 1.399 + dir.AssignLiteral("natural"); 1.400 + mSortDirection = eDirection_Natural; 1.401 + } 1.402 + else { 1.403 + dir.AssignLiteral("ascending"); 1.404 + mSortDirection = eDirection_Ascending; 1.405 + } 1.406 + 1.407 + // Sort it. 1.408 + SortSubtree(mRows.GetRoot()); 1.409 + mRows.InvalidateCachedRow(); 1.410 + if (mBoxObject) 1.411 + mBoxObject->Invalidate(); 1.412 + 1.413 + nsTreeUtils::UpdateSortIndicators(header, dir); 1.414 + 1.415 + return NS_OK; 1.416 +} 1.417 + 1.418 +//---------------------------------------------------------------------- 1.419 +// 1.420 +// nsITreeView methods 1.421 +// 1.422 + 1.423 +NS_IMETHODIMP 1.424 +nsXULTreeBuilder::GetRowCount(int32_t* aRowCount) 1.425 +{ 1.426 + *aRowCount = mRows.Count(); 1.427 + return NS_OK; 1.428 +} 1.429 + 1.430 +NS_IMETHODIMP 1.431 +nsXULTreeBuilder::GetSelection(nsITreeSelection** aSelection) 1.432 +{ 1.433 + NS_IF_ADDREF(*aSelection = mSelection.get()); 1.434 + return NS_OK; 1.435 +} 1.436 + 1.437 +NS_IMETHODIMP 1.438 +nsXULTreeBuilder::SetSelection(nsITreeSelection* aSelection) 1.439 +{ 1.440 + NS_ENSURE_TRUE(!aSelection || 1.441 + nsTreeContentView::CanTrustTreeSelection(aSelection), 1.442 + NS_ERROR_DOM_SECURITY_ERR); 1.443 + mSelection = aSelection; 1.444 + return NS_OK; 1.445 +} 1.446 + 1.447 +NS_IMETHODIMP 1.448 +nsXULTreeBuilder::GetRowProperties(int32_t aIndex, nsAString& aProps) 1.449 +{ 1.450 + NS_PRECONDITION(aIndex >= 0 && aIndex < mRows.Count(), "bad row"); 1.451 + if (aIndex < 0 || aIndex >= mRows.Count()) 1.452 + return NS_ERROR_INVALID_ARG; 1.453 + 1.454 + nsCOMPtr<nsIContent> row; 1.455 + GetTemplateActionRowFor(aIndex, getter_AddRefs(row)); 1.456 + if (row) { 1.457 + nsAutoString raw; 1.458 + row->GetAttr(kNameSpaceID_None, nsGkAtoms::properties, raw); 1.459 + 1.460 + if (!raw.IsEmpty()) { 1.461 + SubstituteText(mRows[aIndex]->mMatch->mResult, raw, aProps); 1.462 + } 1.463 + } 1.464 + 1.465 + return NS_OK; 1.466 +} 1.467 + 1.468 +NS_IMETHODIMP 1.469 +nsXULTreeBuilder::GetCellProperties(int32_t aRow, nsITreeColumn* aCol, 1.470 + nsAString& aProps) 1.471 +{ 1.472 + NS_ENSURE_ARG_POINTER(aCol); 1.473 + NS_PRECONDITION(aRow >= 0 && aRow < mRows.Count(), "bad row"); 1.474 + if (aRow < 0 || aRow >= mRows.Count()) 1.475 + return NS_ERROR_INVALID_ARG; 1.476 + 1.477 + nsCOMPtr<nsIContent> cell; 1.478 + GetTemplateActionCellFor(aRow, aCol, getter_AddRefs(cell)); 1.479 + if (cell) { 1.480 + nsAutoString raw; 1.481 + cell->GetAttr(kNameSpaceID_None, nsGkAtoms::properties, raw); 1.482 + 1.483 + if (!raw.IsEmpty()) { 1.484 + SubstituteText(mRows[aRow]->mMatch->mResult, raw, aProps); 1.485 + } 1.486 + } 1.487 + 1.488 + return NS_OK; 1.489 +} 1.490 + 1.491 +NS_IMETHODIMP 1.492 +nsXULTreeBuilder::GetColumnProperties(nsITreeColumn* aCol, nsAString& aProps) 1.493 +{ 1.494 + NS_ENSURE_ARG_POINTER(aCol); 1.495 + // XXX sortactive fu 1.496 + return NS_OK; 1.497 +} 1.498 + 1.499 +NS_IMETHODIMP 1.500 +nsXULTreeBuilder::IsContainer(int32_t aIndex, bool* aResult) 1.501 +{ 1.502 + NS_PRECONDITION(aIndex >= 0 && aIndex < mRows.Count(), "bad row"); 1.503 + if (aIndex < 0 || aIndex >= mRows.Count()) 1.504 + return NS_ERROR_INVALID_ARG; 1.505 + 1.506 + nsTreeRows::iterator iter = mRows[aIndex]; 1.507 + 1.508 + bool isContainer; 1.509 + iter->mMatch->mResult->GetIsContainer(&isContainer); 1.510 + 1.511 + iter->mContainerType = isContainer 1.512 + ? nsTreeRows::eContainerType_Container 1.513 + : nsTreeRows::eContainerType_Noncontainer; 1.514 + 1.515 + *aResult = (iter->mContainerType == nsTreeRows::eContainerType_Container); 1.516 + return NS_OK; 1.517 +} 1.518 + 1.519 +NS_IMETHODIMP 1.520 +nsXULTreeBuilder::IsContainerOpen(int32_t aIndex, bool* aOpen) 1.521 +{ 1.522 + NS_PRECONDITION(aIndex >= 0 && aIndex < mRows.Count(), "bad row"); 1.523 + if (aIndex < 0 || aIndex >= mRows.Count()) 1.524 + return NS_ERROR_INVALID_ARG; 1.525 + 1.526 + nsTreeRows::iterator iter = mRows[aIndex]; 1.527 + 1.528 + if (iter->mContainerState == nsTreeRows::eContainerState_Unknown) { 1.529 + bool isOpen; 1.530 + IsContainerOpen(iter->mMatch->mResult, &isOpen); 1.531 + 1.532 + iter->mContainerState = isOpen 1.533 + ? nsTreeRows::eContainerState_Open 1.534 + : nsTreeRows::eContainerState_Closed; 1.535 + } 1.536 + 1.537 + *aOpen = (iter->mContainerState == nsTreeRows::eContainerState_Open); 1.538 + return NS_OK; 1.539 +} 1.540 + 1.541 +NS_IMETHODIMP 1.542 +nsXULTreeBuilder::IsContainerEmpty(int32_t aIndex, bool* aResult) 1.543 +{ 1.544 + NS_PRECONDITION(aIndex >= 0 && aIndex < mRows.Count(), "bad row"); 1.545 + if (aIndex < 0 || aIndex >= mRows.Count()) 1.546 + return NS_ERROR_INVALID_ARG; 1.547 + 1.548 + nsTreeRows::iterator iter = mRows[aIndex]; 1.549 + NS_ASSERTION(iter->mContainerType == nsTreeRows::eContainerType_Container, 1.550 + "asking for empty state on non-container"); 1.551 + 1.552 + // if recursion is disabled, pretend that the container is empty. This 1.553 + // ensures that folders are still displayed as such, yet won't display 1.554 + // their children 1.555 + if ((mFlags & eDontRecurse) && (iter->mMatch->mResult != mRootResult)) { 1.556 + *aResult = true; 1.557 + return NS_OK; 1.558 + } 1.559 + 1.560 + if (iter->mContainerFill == nsTreeRows::eContainerFill_Unknown) { 1.561 + bool isEmpty; 1.562 + iter->mMatch->mResult->GetIsEmpty(&isEmpty); 1.563 + 1.564 + iter->mContainerFill = isEmpty 1.565 + ? nsTreeRows::eContainerFill_Empty 1.566 + : nsTreeRows::eContainerFill_Nonempty; 1.567 + } 1.568 + 1.569 + *aResult = (iter->mContainerFill == nsTreeRows::eContainerFill_Empty); 1.570 + return NS_OK; 1.571 +} 1.572 + 1.573 +NS_IMETHODIMP 1.574 +nsXULTreeBuilder::IsSeparator(int32_t aIndex, bool* aResult) 1.575 +{ 1.576 + NS_PRECONDITION(aIndex >= 0 && aIndex < mRows.Count(), "bad row"); 1.577 + if (aIndex < 0 || aIndex >= mRows.Count()) 1.578 + return NS_ERROR_INVALID_ARG; 1.579 + 1.580 + nsAutoString type; 1.581 + nsTreeRows::Row& row = *(mRows[aIndex]); 1.582 + row.mMatch->mResult->GetType(type); 1.583 + 1.584 + *aResult = type.EqualsLiteral("separator"); 1.585 + 1.586 + return NS_OK; 1.587 +} 1.588 + 1.589 +NS_IMETHODIMP 1.590 +nsXULTreeBuilder::GetParentIndex(int32_t aRowIndex, int32_t* aResult) 1.591 +{ 1.592 + NS_PRECONDITION(aRowIndex >= 0 && aRowIndex < mRows.Count(), "bad row"); 1.593 + if (aRowIndex < 0 || aRowIndex >= mRows.Count()) 1.594 + return NS_ERROR_INVALID_ARG; 1.595 + 1.596 + // Construct a path to the row 1.597 + nsTreeRows::iterator iter = mRows[aRowIndex]; 1.598 + 1.599 + // The parent of the row will be at the top of the path 1.600 + nsTreeRows::Subtree* parent = iter.GetParent(); 1.601 + 1.602 + // Now walk through our previous siblings, subtracting off each 1.603 + // one's subtree size 1.604 + int32_t index = iter.GetChildIndex(); 1.605 + while (--index >= 0) 1.606 + aRowIndex -= mRows.GetSubtreeSizeFor(parent, index) + 1; 1.607 + 1.608 + // Now the parent's index will be the first row's index, less one. 1.609 + *aResult = aRowIndex - 1; 1.610 + return NS_OK; 1.611 +} 1.612 + 1.613 +NS_IMETHODIMP 1.614 +nsXULTreeBuilder::HasNextSibling(int32_t aRowIndex, int32_t aAfterIndex, bool* aResult) 1.615 +{ 1.616 + NS_PRECONDITION(aRowIndex >= 0 && aRowIndex < mRows.Count(), "bad row"); 1.617 + if (aRowIndex < 0 || aRowIndex >= mRows.Count()) 1.618 + return NS_ERROR_INVALID_ARG; 1.619 + 1.620 + // Construct a path to the row 1.621 + nsTreeRows::iterator iter = mRows[aRowIndex]; 1.622 + 1.623 + // The parent of the row will be at the top of the path 1.624 + nsTreeRows::Subtree* parent = iter.GetParent(); 1.625 + 1.626 + // We have a next sibling if the child is not the last in the 1.627 + // subtree. 1.628 + *aResult = iter.GetChildIndex() != parent->Count() - 1; 1.629 + return NS_OK; 1.630 +} 1.631 + 1.632 +NS_IMETHODIMP 1.633 +nsXULTreeBuilder::GetLevel(int32_t aRowIndex, int32_t* aResult) 1.634 +{ 1.635 + NS_PRECONDITION(aRowIndex >= 0 && aRowIndex < mRows.Count(), "bad row"); 1.636 + if (aRowIndex < 0 || aRowIndex >= mRows.Count()) 1.637 + return NS_ERROR_INVALID_ARG; 1.638 + 1.639 + // Construct a path to the row; the ``level'' is the path length 1.640 + // less one. 1.641 + nsTreeRows::iterator iter = mRows[aRowIndex]; 1.642 + *aResult = iter.GetDepth() - 1; 1.643 + return NS_OK; 1.644 +} 1.645 + 1.646 +NS_IMETHODIMP 1.647 +nsXULTreeBuilder::GetImageSrc(int32_t aRow, nsITreeColumn* aCol, nsAString& aResult) 1.648 +{ 1.649 + NS_ENSURE_ARG_POINTER(aCol); 1.650 + NS_PRECONDITION(aRow >= 0 && aRow < mRows.Count(), "bad index"); 1.651 + if (aRow < 0 || aRow >= mRows.Count()) 1.652 + return NS_ERROR_INVALID_ARG; 1.653 + 1.654 + // Find the <cell> that corresponds to the column we want. 1.655 + nsCOMPtr<nsIContent> cell; 1.656 + GetTemplateActionCellFor(aRow, aCol, getter_AddRefs(cell)); 1.657 + if (cell) { 1.658 + nsAutoString raw; 1.659 + cell->GetAttr(kNameSpaceID_None, nsGkAtoms::src, raw); 1.660 + 1.661 + SubstituteText(mRows[aRow]->mMatch->mResult, raw, aResult); 1.662 + } 1.663 + else 1.664 + aResult.Truncate(); 1.665 + 1.666 + return NS_OK; 1.667 +} 1.668 + 1.669 + 1.670 +NS_IMETHODIMP 1.671 +nsXULTreeBuilder::GetProgressMode(int32_t aRow, nsITreeColumn* aCol, int32_t* aResult) 1.672 +{ 1.673 + NS_ENSURE_ARG_POINTER(aCol); 1.674 + NS_PRECONDITION(aRow >= 0 && aRow < mRows.Count(), "bad index"); 1.675 + if (aRow < 0 || aRow >= mRows.Count()) 1.676 + return NS_ERROR_INVALID_ARG; 1.677 + 1.678 + *aResult = nsITreeView::PROGRESS_NONE; 1.679 + 1.680 + // Find the <cell> that corresponds to the column we want. 1.681 + nsCOMPtr<nsIContent> cell; 1.682 + GetTemplateActionCellFor(aRow, aCol, getter_AddRefs(cell)); 1.683 + if (cell) { 1.684 + nsAutoString raw; 1.685 + cell->GetAttr(kNameSpaceID_None, nsGkAtoms::mode, raw); 1.686 + 1.687 + nsAutoString mode; 1.688 + SubstituteText(mRows[aRow]->mMatch->mResult, raw, mode); 1.689 + 1.690 + if (mode.EqualsLiteral("normal")) 1.691 + *aResult = nsITreeView::PROGRESS_NORMAL; 1.692 + else if (mode.EqualsLiteral("undetermined")) 1.693 + *aResult = nsITreeView::PROGRESS_UNDETERMINED; 1.694 + } 1.695 + 1.696 + return NS_OK; 1.697 +} 1.698 + 1.699 +NS_IMETHODIMP 1.700 +nsXULTreeBuilder::GetCellValue(int32_t aRow, nsITreeColumn* aCol, nsAString& aResult) 1.701 +{ 1.702 + NS_ENSURE_ARG_POINTER(aCol); 1.703 + NS_PRECONDITION(aRow >= 0 && aRow < mRows.Count(), "bad index"); 1.704 + if (aRow < 0 || aRow >= mRows.Count()) 1.705 + return NS_ERROR_INVALID_ARG; 1.706 + 1.707 + // Find the <cell> that corresponds to the column we want. 1.708 + nsCOMPtr<nsIContent> cell; 1.709 + GetTemplateActionCellFor(aRow, aCol, getter_AddRefs(cell)); 1.710 + if (cell) { 1.711 + nsAutoString raw; 1.712 + cell->GetAttr(kNameSpaceID_None, nsGkAtoms::value, raw); 1.713 + 1.714 + SubstituteText(mRows[aRow]->mMatch->mResult, raw, aResult); 1.715 + } 1.716 + else 1.717 + aResult.Truncate(); 1.718 + 1.719 + return NS_OK; 1.720 +} 1.721 + 1.722 +NS_IMETHODIMP 1.723 +nsXULTreeBuilder::GetCellText(int32_t aRow, nsITreeColumn* aCol, nsAString& aResult) 1.724 +{ 1.725 + NS_ENSURE_ARG_POINTER(aCol); 1.726 + NS_PRECONDITION(aRow >= 0 && aRow < mRows.Count(), "bad index"); 1.727 + if (aRow < 0 || aRow >= mRows.Count()) 1.728 + return NS_ERROR_INVALID_ARG; 1.729 + 1.730 + // Find the <cell> that corresponds to the column we want. 1.731 + nsCOMPtr<nsIContent> cell; 1.732 + GetTemplateActionCellFor(aRow, aCol, getter_AddRefs(cell)); 1.733 + if (cell) { 1.734 + nsAutoString raw; 1.735 + cell->GetAttr(kNameSpaceID_None, nsGkAtoms::label, raw); 1.736 + 1.737 + SubstituteText(mRows[aRow]->mMatch->mResult, raw, aResult); 1.738 + 1.739 + } 1.740 + else 1.741 + aResult.Truncate(); 1.742 + 1.743 + return NS_OK; 1.744 +} 1.745 + 1.746 +NS_IMETHODIMP 1.747 +nsXULTreeBuilder::SetTree(nsITreeBoxObject* aTree) 1.748 +{ 1.749 + mBoxObject = aTree; 1.750 + 1.751 + // If this is teardown time, then we're done. 1.752 + if (!mBoxObject) { 1.753 + Uninit(false); 1.754 + return NS_OK; 1.755 + } 1.756 + NS_ENSURE_TRUE(mRoot, NS_ERROR_NOT_INITIALIZED); 1.757 + 1.758 + // Is our root's principal trusted? 1.759 + bool isTrusted = false; 1.760 + nsresult rv = IsSystemPrincipal(mRoot->NodePrincipal(), &isTrusted); 1.761 + if (NS_SUCCEEDED(rv) && isTrusted) { 1.762 + // Get the datasource we intend to use to remember open state. 1.763 + nsAutoString datasourceStr; 1.764 + mRoot->GetAttr(kNameSpaceID_None, nsGkAtoms::statedatasource, datasourceStr); 1.765 + 1.766 + // since we are trusted, use the user specified datasource 1.767 + // if non specified, use localstore, which gives us 1.768 + // persistence across sessions 1.769 + if (! datasourceStr.IsEmpty()) { 1.770 + gRDFService->GetDataSource(NS_ConvertUTF16toUTF8(datasourceStr).get(), 1.771 + getter_AddRefs(mPersistStateStore)); 1.772 + } 1.773 + else { 1.774 + gRDFService->GetDataSource("rdf:local-store", 1.775 + getter_AddRefs(mPersistStateStore)); 1.776 + } 1.777 + } 1.778 + 1.779 + // Either no specific datasource was specified, or we failed 1.780 + // to get one because we are not trusted. 1.781 + // 1.782 + // XXX if it were possible to ``write an arbitrary datasource 1.783 + // back'', then we could also allow an untrusted document to 1.784 + // use a statedatasource from the same codebase. 1.785 + if (! mPersistStateStore) { 1.786 + mPersistStateStore = 1.787 + do_CreateInstance("@mozilla.org/rdf/datasource;1?name=in-memory-datasource"); 1.788 + } 1.789 + 1.790 + NS_ASSERTION(mPersistStateStore, "failed to get a persistent state store"); 1.791 + if (! mPersistStateStore) 1.792 + return NS_ERROR_FAILURE; 1.793 + 1.794 + Rebuild(); 1.795 + 1.796 + EnsureSortVariables(); 1.797 + if (mSortVariable) 1.798 + SortSubtree(mRows.GetRoot()); 1.799 + 1.800 + return NS_OK; 1.801 +} 1.802 + 1.803 +NS_IMETHODIMP 1.804 +nsXULTreeBuilder::ToggleOpenState(int32_t aIndex) 1.805 +{ 1.806 + if (aIndex < 0 || aIndex >= mRows.Count()) 1.807 + return NS_ERROR_INVALID_ARG; 1.808 + 1.809 + nsIXULTemplateResult* result = mRows[aIndex]->mMatch->mResult; 1.810 + if (! result) 1.811 + return NS_ERROR_FAILURE; 1.812 + 1.813 + if (mFlags & eDontRecurse) 1.814 + return NS_OK; 1.815 + 1.816 + if (result && result != mRootResult) { 1.817 + // don't open containers if child processing isn't allowed 1.818 + bool mayProcessChildren; 1.819 + nsresult rv = result->GetMayProcessChildren(&mayProcessChildren); 1.820 + if (NS_FAILED(rv) || !mayProcessChildren) 1.821 + return rv; 1.822 + } 1.823 + 1.824 + uint32_t count = mObservers.Count(); 1.825 + for (uint32_t i = 0; i < count; ++i) { 1.826 + nsCOMPtr<nsIXULTreeBuilderObserver> observer = mObservers.SafeObjectAt(i); 1.827 + if (observer) 1.828 + observer->OnToggleOpenState(aIndex); 1.829 + } 1.830 + 1.831 + if (mPersistStateStore) { 1.832 + bool isOpen; 1.833 + IsContainerOpen(aIndex, &isOpen); 1.834 + 1.835 + nsCOMPtr<nsIRDFResource> container; 1.836 + GetResourceFor(aIndex, getter_AddRefs(container)); 1.837 + if (! container) 1.838 + return NS_ERROR_FAILURE; 1.839 + 1.840 + bool hasProperty; 1.841 + IsContainerOpen(container, &hasProperty); 1.842 + 1.843 + if (isOpen) { 1.844 + if (hasProperty) { 1.845 + mPersistStateStore->Unassert(container, 1.846 + nsXULContentUtils::NC_open, 1.847 + nsXULContentUtils::true_); 1.848 + } 1.849 + 1.850 + CloseContainer(aIndex); 1.851 + } 1.852 + else { 1.853 + if (! hasProperty) { 1.854 + mPersistStateStore->Assert(container, 1.855 + nsXULContentUtils::NC_open, 1.856 + nsXULContentUtils::true_, 1.857 + true); 1.858 + } 1.859 + 1.860 + OpenContainer(aIndex, result); 1.861 + } 1.862 + } 1.863 + 1.864 + return NS_OK; 1.865 +} 1.866 + 1.867 +NS_IMETHODIMP 1.868 +nsXULTreeBuilder::CycleHeader(nsITreeColumn* aCol) 1.869 +{ 1.870 + NS_ENSURE_ARG_POINTER(aCol); 1.871 + nsCOMPtr<nsIDOMElement> element; 1.872 + aCol->GetElement(getter_AddRefs(element)); 1.873 + 1.874 + nsAutoString id; 1.875 + aCol->GetId(id); 1.876 + 1.877 + uint32_t count = mObservers.Count(); 1.878 + for (uint32_t i = 0; i < count; ++i) { 1.879 + nsCOMPtr<nsIXULTreeBuilderObserver> observer = mObservers.SafeObjectAt(i); 1.880 + if (observer) 1.881 + observer->OnCycleHeader(id.get(), element); 1.882 + } 1.883 + 1.884 + return Sort(element); 1.885 +} 1.886 + 1.887 +NS_IMETHODIMP 1.888 +nsXULTreeBuilder::SelectionChanged() 1.889 +{ 1.890 + uint32_t count = mObservers.Count(); 1.891 + for (uint32_t i = 0; i < count; ++i) { 1.892 + nsCOMPtr<nsIXULTreeBuilderObserver> observer = mObservers.SafeObjectAt(i); 1.893 + if (observer) 1.894 + observer->OnSelectionChanged(); 1.895 + } 1.896 + 1.897 + return NS_OK; 1.898 +} 1.899 + 1.900 +NS_IMETHODIMP 1.901 +nsXULTreeBuilder::CycleCell(int32_t aRow, nsITreeColumn* aCol) 1.902 +{ 1.903 + NS_ENSURE_ARG_POINTER(aCol); 1.904 + 1.905 + nsAutoString id; 1.906 + aCol->GetId(id); 1.907 + 1.908 + uint32_t count = mObservers.Count(); 1.909 + for (uint32_t i = 0; i < count; ++i) { 1.910 + nsCOMPtr<nsIXULTreeBuilderObserver> observer = mObservers.SafeObjectAt(i); 1.911 + if (observer) 1.912 + observer->OnCycleCell(aRow, id.get()); 1.913 + } 1.914 + 1.915 + return NS_OK; 1.916 +} 1.917 + 1.918 +NS_IMETHODIMP 1.919 +nsXULTreeBuilder::IsEditable(int32_t aRow, nsITreeColumn* aCol, bool* _retval) 1.920 +{ 1.921 + *_retval = true; 1.922 + NS_ENSURE_ARG_POINTER(aCol); 1.923 + NS_PRECONDITION(aRow >= 0 && aRow < mRows.Count(), "bad index"); 1.924 + if (aRow < 0 || aRow >= mRows.Count()) 1.925 + return NS_ERROR_INVALID_ARG; 1.926 + 1.927 + // Find the <cell> that corresponds to the column we want. 1.928 + nsCOMPtr<nsIContent> cell; 1.929 + GetTemplateActionCellFor(aRow, aCol, getter_AddRefs(cell)); 1.930 + if (cell) { 1.931 + nsAutoString raw; 1.932 + cell->GetAttr(kNameSpaceID_None, nsGkAtoms::editable, raw); 1.933 + 1.934 + nsAutoString editable; 1.935 + SubstituteText(mRows[aRow]->mMatch->mResult, raw, editable); 1.936 + 1.937 + if (editable.EqualsLiteral("false")) 1.938 + *_retval = false; 1.939 + } 1.940 + 1.941 + return NS_OK; 1.942 +} 1.943 + 1.944 +NS_IMETHODIMP 1.945 +nsXULTreeBuilder::IsSelectable(int32_t aRow, nsITreeColumn* aCol, bool* _retval) 1.946 +{ 1.947 + NS_PRECONDITION(aRow >= 0 && aRow < mRows.Count(), "bad index"); 1.948 + if (aRow < 0 || aRow >= mRows.Count()) 1.949 + return NS_ERROR_INVALID_ARG; 1.950 + 1.951 + *_retval = true; 1.952 + 1.953 + // Find the <cell> that corresponds to the column we want. 1.954 + nsCOMPtr<nsIContent> cell; 1.955 + GetTemplateActionCellFor(aRow, aCol, getter_AddRefs(cell)); 1.956 + if (cell) { 1.957 + nsAutoString raw; 1.958 + cell->GetAttr(kNameSpaceID_None, nsGkAtoms::selectable, raw); 1.959 + 1.960 + nsAutoString selectable; 1.961 + SubstituteText(mRows[aRow]->mMatch->mResult, raw, selectable); 1.962 + 1.963 + if (selectable.EqualsLiteral("false")) 1.964 + *_retval = false; 1.965 + } 1.966 + 1.967 + return NS_OK; 1.968 +} 1.969 + 1.970 +NS_IMETHODIMP 1.971 +nsXULTreeBuilder::SetCellValue(int32_t aRow, nsITreeColumn* aCol, const nsAString& aValue) 1.972 +{ 1.973 + NS_ENSURE_ARG_POINTER(aCol); 1.974 + return NS_OK; 1.975 +} 1.976 + 1.977 +NS_IMETHODIMP 1.978 +nsXULTreeBuilder::SetCellText(int32_t aRow, nsITreeColumn* aCol, const nsAString& aValue) 1.979 +{ 1.980 + NS_ENSURE_ARG_POINTER(aCol); 1.981 + return NS_OK; 1.982 +} 1.983 + 1.984 +NS_IMETHODIMP 1.985 +nsXULTreeBuilder::PerformAction(const char16_t* aAction) 1.986 +{ 1.987 + uint32_t count = mObservers.Count(); 1.988 + for (uint32_t i = 0; i < count; ++i) { 1.989 + nsCOMPtr<nsIXULTreeBuilderObserver> observer = mObservers.SafeObjectAt(i); 1.990 + if (observer) 1.991 + observer->OnPerformAction(aAction); 1.992 + } 1.993 + 1.994 + return NS_OK; 1.995 +} 1.996 + 1.997 +NS_IMETHODIMP 1.998 +nsXULTreeBuilder::PerformActionOnRow(const char16_t* aAction, int32_t aRow) 1.999 +{ 1.1000 + uint32_t count = mObservers.Count(); 1.1001 + for (uint32_t i = 0; i < count; ++i) { 1.1002 + nsCOMPtr<nsIXULTreeBuilderObserver> observer = mObservers.SafeObjectAt(i); 1.1003 + if (observer) 1.1004 + observer->OnPerformActionOnRow(aAction, aRow); 1.1005 + } 1.1006 + 1.1007 + return NS_OK; 1.1008 +} 1.1009 + 1.1010 +NS_IMETHODIMP 1.1011 +nsXULTreeBuilder::PerformActionOnCell(const char16_t* aAction, int32_t aRow, nsITreeColumn* aCol) 1.1012 +{ 1.1013 + NS_ENSURE_ARG_POINTER(aCol); 1.1014 + nsAutoString id; 1.1015 + aCol->GetId(id); 1.1016 + 1.1017 + uint32_t count = mObservers.Count(); 1.1018 + for (uint32_t i = 0; i < count; ++i) { 1.1019 + nsCOMPtr<nsIXULTreeBuilderObserver> observer = mObservers.SafeObjectAt(i); 1.1020 + if (observer) 1.1021 + observer->OnPerformActionOnCell(aAction, aRow, id.get()); 1.1022 + } 1.1023 + 1.1024 + return NS_OK; 1.1025 +} 1.1026 + 1.1027 + 1.1028 +void 1.1029 +nsXULTreeBuilder::NodeWillBeDestroyed(const nsINode* aNode) 1.1030 +{ 1.1031 + nsCOMPtr<nsIMutationObserver> kungFuDeathGrip(this); 1.1032 + mObservers.Clear(); 1.1033 + 1.1034 + nsXULTemplateBuilder::NodeWillBeDestroyed(aNode); 1.1035 +} 1.1036 + 1.1037 +NS_IMETHODIMP 1.1038 +nsXULTreeBuilder::HasGeneratedContent(nsIRDFResource* aResource, 1.1039 + nsIAtom* aTag, 1.1040 + bool* aGenerated) 1.1041 +{ 1.1042 + *aGenerated = false; 1.1043 + NS_ENSURE_ARG_POINTER(aResource); 1.1044 + 1.1045 + if (!mRootResult) 1.1046 + return NS_OK; 1.1047 + 1.1048 + nsCOMPtr<nsIRDFResource> rootresource; 1.1049 + nsresult rv = mRootResult->GetResource(getter_AddRefs(rootresource)); 1.1050 + if (NS_FAILED(rv)) 1.1051 + return rv; 1.1052 + 1.1053 + if (aResource == rootresource || 1.1054 + mRows.FindByResource(aResource) != mRows.Last()) 1.1055 + *aGenerated = true; 1.1056 + 1.1057 + return NS_OK; 1.1058 +} 1.1059 + 1.1060 +bool 1.1061 +nsXULTreeBuilder::GetInsertionLocations(nsIXULTemplateResult* aResult, 1.1062 + nsCOMArray<nsIContent>** aLocations) 1.1063 +{ 1.1064 + *aLocations = nullptr; 1.1065 + 1.1066 + // Get the reference point and check if it is an open container. Rows 1.1067 + // should not be generated otherwise. 1.1068 + 1.1069 + nsAutoString ref; 1.1070 + nsresult rv = aResult->GetBindingFor(mRefVariable, ref); 1.1071 + if (NS_FAILED(rv) || ref.IsEmpty()) 1.1072 + return false; 1.1073 + 1.1074 + nsCOMPtr<nsIRDFResource> container; 1.1075 + rv = gRDFService->GetUnicodeResource(ref, getter_AddRefs(container)); 1.1076 + if (NS_FAILED(rv)) 1.1077 + return false; 1.1078 + 1.1079 + // Can always insert into the root resource 1.1080 + if (container == mRows.GetRootResource()) 1.1081 + return true; 1.1082 + 1.1083 + nsTreeRows::iterator iter = mRows.FindByResource(container); 1.1084 + if (iter == mRows.Last()) 1.1085 + return false; 1.1086 + 1.1087 + return (iter->mContainerState == nsTreeRows::eContainerState_Open); 1.1088 +} 1.1089 + 1.1090 +nsresult 1.1091 +nsXULTreeBuilder::ReplaceMatch(nsIXULTemplateResult* aOldResult, 1.1092 + nsTemplateMatch* aNewMatch, 1.1093 + nsTemplateRule* aNewMatchRule, 1.1094 + void *aLocation) 1.1095 +{ 1.1096 + if (! mBoxObject) 1.1097 + return NS_OK; 1.1098 + 1.1099 + if (aOldResult) { 1.1100 + // Grovel through the rows looking for oldresult. 1.1101 + nsTreeRows::iterator iter = mRows.Find(aOldResult); 1.1102 + 1.1103 + NS_ASSERTION(iter != mRows.Last(), "couldn't find row"); 1.1104 + if (iter == mRows.Last()) 1.1105 + return NS_ERROR_FAILURE; 1.1106 + 1.1107 + // Remove the rows from the view 1.1108 + int32_t row = iter.GetRowIndex(); 1.1109 + 1.1110 + // If the row contains children, remove the matches from the 1.1111 + // children so that they can be regenerated again if the element 1.1112 + // gets added back. 1.1113 + int32_t delta = mRows.GetSubtreeSizeFor(iter); 1.1114 + if (delta) 1.1115 + RemoveMatchesFor(*(iter->mSubtree)); 1.1116 + 1.1117 + if (mRows.RemoveRowAt(iter) == 0 && iter.GetRowIndex() >= 0) { 1.1118 + 1.1119 + // In this case iter now points to its parent 1.1120 + // Invalidate the row's cached fill state 1.1121 + iter->mContainerFill = nsTreeRows::eContainerFill_Unknown; 1.1122 + 1.1123 + nsCOMPtr<nsITreeColumns> cols; 1.1124 + mBoxObject->GetColumns(getter_AddRefs(cols)); 1.1125 + if (cols) { 1.1126 + nsCOMPtr<nsITreeColumn> primaryCol; 1.1127 + cols->GetPrimaryColumn(getter_AddRefs(primaryCol)); 1.1128 + if (primaryCol) 1.1129 + mBoxObject->InvalidateCell(iter.GetRowIndex(), primaryCol); 1.1130 + } 1.1131 + } 1.1132 + 1.1133 + // Notify the box object 1.1134 + mBoxObject->RowCountChanged(row, -delta - 1); 1.1135 + } 1.1136 + 1.1137 + if (aNewMatch && aNewMatch->mResult) { 1.1138 + // Insertion. 1.1139 + int32_t row = -1; 1.1140 + nsTreeRows::Subtree* parent = nullptr; 1.1141 + nsIXULTemplateResult* result = aNewMatch->mResult; 1.1142 + 1.1143 + nsAutoString ref; 1.1144 + nsresult rv = result->GetBindingFor(mRefVariable, ref); 1.1145 + if (NS_FAILED(rv) || ref.IsEmpty()) 1.1146 + return rv; 1.1147 + 1.1148 + nsCOMPtr<nsIRDFResource> container; 1.1149 + rv = gRDFService->GetUnicodeResource(ref, getter_AddRefs(container)); 1.1150 + if (NS_FAILED(rv)) 1.1151 + return rv; 1.1152 + 1.1153 + if (container != mRows.GetRootResource()) { 1.1154 + nsTreeRows::iterator iter = mRows.FindByResource(container); 1.1155 + row = iter.GetRowIndex(); 1.1156 + 1.1157 + NS_ASSERTION(iter != mRows.Last(), "couldn't find container row"); 1.1158 + if (iter == mRows.Last()) 1.1159 + return NS_ERROR_FAILURE; 1.1160 + 1.1161 + // Use the persist store to remember if the container 1.1162 + // is open or closed. 1.1163 + bool open = false; 1.1164 + IsContainerOpen(row, &open); 1.1165 + 1.1166 + // If it's open, make sure that we've got a subtree structure ready. 1.1167 + if (open) 1.1168 + parent = mRows.EnsureSubtreeFor(iter); 1.1169 + 1.1170 + // We know something has just been inserted into the 1.1171 + // container, so whether its open or closed, make sure 1.1172 + // that we've got our tree row's state correct. 1.1173 + if ((iter->mContainerType != nsTreeRows::eContainerType_Container) || 1.1174 + (iter->mContainerFill != nsTreeRows::eContainerFill_Nonempty)) { 1.1175 + iter->mContainerType = nsTreeRows::eContainerType_Container; 1.1176 + iter->mContainerFill = nsTreeRows::eContainerFill_Nonempty; 1.1177 + mBoxObject->InvalidateRow(iter.GetRowIndex()); 1.1178 + } 1.1179 + } 1.1180 + else { 1.1181 + parent = mRows.GetRoot(); 1.1182 + } 1.1183 + 1.1184 + if (parent) { 1.1185 + // If we get here, then we're inserting into an open 1.1186 + // container. By default, place the new element at the 1.1187 + // end of the container 1.1188 + int32_t index = parent->Count(); 1.1189 + 1.1190 + if (mSortVariable) { 1.1191 + // Figure out where to put the new element by doing an 1.1192 + // insertion sort. 1.1193 + int32_t left = 0; 1.1194 + int32_t right = index; 1.1195 + 1.1196 + while (left < right) { 1.1197 + index = (left + right) / 2; 1.1198 + int32_t cmp = CompareResults((*parent)[index].mMatch->mResult, result); 1.1199 + if (cmp < 0) 1.1200 + left = ++index; 1.1201 + else if (cmp > 0) 1.1202 + right = index; 1.1203 + else 1.1204 + break; 1.1205 + } 1.1206 + } 1.1207 + 1.1208 + nsTreeRows::iterator iter = 1.1209 + mRows.InsertRowAt(aNewMatch, parent, index); 1.1210 + 1.1211 + mBoxObject->RowCountChanged(iter.GetRowIndex(), +1); 1.1212 + 1.1213 + // See if this newly added row is open; in which case, 1.1214 + // recursively add its children to the tree, too. 1.1215 + 1.1216 + if (mFlags & eDontRecurse) 1.1217 + return NS_OK; 1.1218 + 1.1219 + if (result != mRootResult) { 1.1220 + // don't open containers if child processing isn't allowed 1.1221 + bool mayProcessChildren; 1.1222 + nsresult rv = result->GetMayProcessChildren(&mayProcessChildren); 1.1223 + if (NS_FAILED(rv) || ! mayProcessChildren) return NS_OK; 1.1224 + } 1.1225 + 1.1226 + bool open; 1.1227 + IsContainerOpen(result, &open); 1.1228 + if (open) 1.1229 + OpenContainer(iter.GetRowIndex(), result); 1.1230 + } 1.1231 + } 1.1232 + 1.1233 + return NS_OK; 1.1234 +} 1.1235 + 1.1236 +nsresult 1.1237 +nsXULTreeBuilder::SynchronizeResult(nsIXULTemplateResult* aResult) 1.1238 +{ 1.1239 + if (mBoxObject) { 1.1240 + // XXX we could be more conservative and just invalidate the cells 1.1241 + // that got whacked... 1.1242 + 1.1243 + nsTreeRows::iterator iter = mRows.Find(aResult); 1.1244 + 1.1245 + NS_ASSERTION(iter != mRows.Last(), "couldn't find row"); 1.1246 + if (iter == mRows.Last()) 1.1247 + return NS_ERROR_FAILURE; 1.1248 + 1.1249 + int32_t row = iter.GetRowIndex(); 1.1250 + if (row >= 0) 1.1251 + mBoxObject->InvalidateRow(row); 1.1252 + 1.1253 + PR_LOG(gXULTemplateLog, PR_LOG_DEBUG, 1.1254 + ("xultemplate[%p] => row %d", this, row)); 1.1255 + } 1.1256 + 1.1257 + return NS_OK; 1.1258 +} 1.1259 + 1.1260 +//---------------------------------------------------------------------- 1.1261 + 1.1262 +nsresult 1.1263 +nsXULTreeBuilder::EnsureSortVariables() 1.1264 +{ 1.1265 + // Grovel through <treecols> kids to find the <treecol> 1.1266 + // with the sort attributes. 1.1267 + nsCOMPtr<nsIContent> treecols; 1.1268 + 1.1269 + nsXULContentUtils::FindChildByTag(mRoot, kNameSpaceID_XUL, 1.1270 + nsGkAtoms::treecols, 1.1271 + getter_AddRefs(treecols)); 1.1272 + 1.1273 + if (!treecols) 1.1274 + return NS_OK; 1.1275 + 1.1276 + for (nsIContent* child = treecols->GetFirstChild(); 1.1277 + child; 1.1278 + child = child->GetNextSibling()) { 1.1279 + 1.1280 + if (child->NodeInfo()->Equals(nsGkAtoms::treecol, 1.1281 + kNameSpaceID_XUL)) { 1.1282 + if (child->AttrValueIs(kNameSpaceID_None, nsGkAtoms::sortActive, 1.1283 + nsGkAtoms::_true, eCaseMatters)) { 1.1284 + nsAutoString sort; 1.1285 + child->GetAttr(kNameSpaceID_None, nsGkAtoms::sort, sort); 1.1286 + if (! sort.IsEmpty()) { 1.1287 + mSortVariable = do_GetAtom(sort); 1.1288 + 1.1289 + static nsIContent::AttrValuesArray strings[] = 1.1290 + {&nsGkAtoms::ascending, &nsGkAtoms::descending, nullptr}; 1.1291 + switch (child->FindAttrValueIn(kNameSpaceID_None, 1.1292 + nsGkAtoms::sortDirection, 1.1293 + strings, eCaseMatters)) { 1.1294 + case 0: mSortDirection = eDirection_Ascending; break; 1.1295 + case 1: mSortDirection = eDirection_Descending; break; 1.1296 + default: mSortDirection = eDirection_Natural; break; 1.1297 + } 1.1298 + } 1.1299 + break; 1.1300 + } 1.1301 + } 1.1302 + } 1.1303 + 1.1304 + return NS_OK; 1.1305 +} 1.1306 + 1.1307 +nsresult 1.1308 +nsXULTreeBuilder::RebuildAll() 1.1309 +{ 1.1310 + NS_ENSURE_TRUE(mRoot, NS_ERROR_NOT_INITIALIZED); 1.1311 + 1.1312 + nsCOMPtr<nsIDocument> doc = mRoot->GetDocument(); 1.1313 + 1.1314 + // Bail out early if we are being torn down. 1.1315 + if (!doc) 1.1316 + return NS_OK; 1.1317 + 1.1318 + if (! mQueryProcessor) 1.1319 + return NS_OK; 1.1320 + 1.1321 + if (mBoxObject) { 1.1322 + mBoxObject->BeginUpdateBatch(); 1.1323 + } 1.1324 + 1.1325 + if (mQueriesCompiled) { 1.1326 + Uninit(false); 1.1327 + } 1.1328 + else if (mBoxObject) { 1.1329 + int32_t count = mRows.Count(); 1.1330 + mRows.Clear(); 1.1331 + mBoxObject->RowCountChanged(0, -count); 1.1332 + } 1.1333 + 1.1334 + nsresult rv = CompileQueries(); 1.1335 + if (NS_SUCCEEDED(rv) && mQuerySets.Length() > 0) { 1.1336 + // Seed the rule network with assignments for the tree row variable 1.1337 + nsAutoString ref; 1.1338 + mRoot->GetAttr(kNameSpaceID_None, nsGkAtoms::ref, ref); 1.1339 + if (!ref.IsEmpty()) { 1.1340 + rv = mQueryProcessor->TranslateRef(mDataSource, ref, 1.1341 + getter_AddRefs(mRootResult)); 1.1342 + if (NS_SUCCEEDED(rv) && mRootResult) { 1.1343 + OpenContainer(-1, mRootResult); 1.1344 + 1.1345 + nsCOMPtr<nsIRDFResource> rootResource; 1.1346 + GetResultResource(mRootResult, getter_AddRefs(rootResource)); 1.1347 + 1.1348 + mRows.SetRootResource(rootResource); 1.1349 + } 1.1350 + } 1.1351 + } 1.1352 + 1.1353 + if (mBoxObject) { 1.1354 + mBoxObject->EndUpdateBatch(); 1.1355 + } 1.1356 + 1.1357 + return rv; 1.1358 +} 1.1359 + 1.1360 +nsresult 1.1361 +nsXULTreeBuilder::GetTemplateActionRowFor(int32_t aRow, nsIContent** aResult) 1.1362 +{ 1.1363 + // Get the template in the DOM from which we're supposed to 1.1364 + // generate text 1.1365 + nsTreeRows::Row& row = *(mRows[aRow]); 1.1366 + 1.1367 + // The match stores the indices of the rule and query to use. Use these 1.1368 + // to look up the right nsTemplateRule and use that rule's action to get 1.1369 + // the treerow in the template. 1.1370 + int16_t ruleindex = row.mMatch->RuleIndex(); 1.1371 + if (ruleindex >= 0) { 1.1372 + nsTemplateQuerySet* qs = mQuerySets[row.mMatch->QuerySetPriority()]; 1.1373 + nsTemplateRule* rule = qs->GetRuleAt(ruleindex); 1.1374 + if (rule) { 1.1375 + nsCOMPtr<nsIContent> children; 1.1376 + nsXULContentUtils::FindChildByTag(rule->GetAction(), kNameSpaceID_XUL, 1.1377 + nsGkAtoms::treechildren, 1.1378 + getter_AddRefs(children)); 1.1379 + if (children) { 1.1380 + nsCOMPtr<nsIContent> item; 1.1381 + nsXULContentUtils::FindChildByTag(children, kNameSpaceID_XUL, 1.1382 + nsGkAtoms::treeitem, 1.1383 + getter_AddRefs(item)); 1.1384 + if (item) 1.1385 + return nsXULContentUtils::FindChildByTag(item, 1.1386 + kNameSpaceID_XUL, 1.1387 + nsGkAtoms::treerow, 1.1388 + aResult); 1.1389 + } 1.1390 + } 1.1391 + } 1.1392 + 1.1393 + *aResult = nullptr; 1.1394 + return NS_OK; 1.1395 +} 1.1396 + 1.1397 +nsresult 1.1398 +nsXULTreeBuilder::GetTemplateActionCellFor(int32_t aRow, 1.1399 + nsITreeColumn* aCol, 1.1400 + nsIContent** aResult) 1.1401 +{ 1.1402 + *aResult = nullptr; 1.1403 + 1.1404 + if (!aCol) return NS_ERROR_INVALID_ARG; 1.1405 + 1.1406 + nsCOMPtr<nsIContent> row; 1.1407 + GetTemplateActionRowFor(aRow, getter_AddRefs(row)); 1.1408 + if (row) { 1.1409 + nsCOMPtr<nsIAtom> colAtom; 1.1410 + int32_t colIndex; 1.1411 + aCol->GetAtom(getter_AddRefs(colAtom)); 1.1412 + aCol->GetIndex(&colIndex); 1.1413 + 1.1414 + uint32_t j = 0; 1.1415 + for (nsIContent* child = row->GetFirstChild(); 1.1416 + child; 1.1417 + child = child->GetNextSibling()) { 1.1418 + 1.1419 + if (child->NodeInfo()->Equals(nsGkAtoms::treecell, 1.1420 + kNameSpaceID_XUL)) { 1.1421 + if (colAtom && 1.1422 + child->AttrValueIs(kNameSpaceID_None, nsGkAtoms::ref, 1.1423 + colAtom, eCaseMatters)) { 1.1424 + *aResult = child; 1.1425 + break; 1.1426 + } 1.1427 + else if (j == (uint32_t)colIndex) 1.1428 + *aResult = child; 1.1429 + j++; 1.1430 + } 1.1431 + } 1.1432 + } 1.1433 + NS_IF_ADDREF(*aResult); 1.1434 + 1.1435 + return NS_OK; 1.1436 +} 1.1437 + 1.1438 +nsresult 1.1439 +nsXULTreeBuilder::GetResourceFor(int32_t aRow, nsIRDFResource** aResource) 1.1440 +{ 1.1441 + nsTreeRows::Row& row = *(mRows[aRow]); 1.1442 + return GetResultResource(row.mMatch->mResult, aResource); 1.1443 +} 1.1444 + 1.1445 +nsresult 1.1446 +nsXULTreeBuilder::OpenContainer(int32_t aIndex, nsIXULTemplateResult* aResult) 1.1447 +{ 1.1448 + // A row index of -1 in this case means ``open tree body'' 1.1449 + NS_ASSERTION(aIndex >= -1 && aIndex < mRows.Count(), "bad row"); 1.1450 + if (aIndex < -1 || aIndex >= mRows.Count()) 1.1451 + return NS_ERROR_INVALID_ARG; 1.1452 + 1.1453 + nsTreeRows::Subtree* container; 1.1454 + 1.1455 + if (aIndex >= 0) { 1.1456 + nsTreeRows::iterator iter = mRows[aIndex]; 1.1457 + container = mRows.EnsureSubtreeFor(iter.GetParent(), 1.1458 + iter.GetChildIndex()); 1.1459 + 1.1460 + iter->mContainerState = nsTreeRows::eContainerState_Open; 1.1461 + } 1.1462 + else 1.1463 + container = mRows.GetRoot(); 1.1464 + 1.1465 + if (! container) 1.1466 + return NS_ERROR_OUT_OF_MEMORY; 1.1467 + 1.1468 + int32_t count; 1.1469 + OpenSubtreeOf(container, aIndex, aResult, &count); 1.1470 + 1.1471 + // Notify the box object 1.1472 + if (mBoxObject) { 1.1473 + if (aIndex >= 0) 1.1474 + mBoxObject->InvalidateRow(aIndex); 1.1475 + 1.1476 + if (count) 1.1477 + mBoxObject->RowCountChanged(aIndex + 1, count); 1.1478 + } 1.1479 + 1.1480 + return NS_OK; 1.1481 +} 1.1482 + 1.1483 +nsresult 1.1484 +nsXULTreeBuilder::OpenSubtreeOf(nsTreeRows::Subtree* aSubtree, 1.1485 + int32_t aIndex, 1.1486 + nsIXULTemplateResult *aResult, 1.1487 + int32_t* aDelta) 1.1488 +{ 1.1489 + nsAutoTArray<int32_t, 8> open; 1.1490 + int32_t count = 0; 1.1491 + 1.1492 + int32_t rulecount = mQuerySets.Length(); 1.1493 + 1.1494 + for (int32_t r = 0; r < rulecount; r++) { 1.1495 + nsTemplateQuerySet* queryset = mQuerySets[r]; 1.1496 + OpenSubtreeForQuerySet(aSubtree, aIndex, aResult, queryset, &count, open); 1.1497 + } 1.1498 + 1.1499 + // Now recursively deal with any open sub-containers that just got 1.1500 + // inserted. We need to do this back-to-front to avoid skewing offsets. 1.1501 + for (int32_t i = open.Length() - 1; i >= 0; --i) { 1.1502 + int32_t index = open[i]; 1.1503 + 1.1504 + nsTreeRows::Subtree* child = 1.1505 + mRows.EnsureSubtreeFor(aSubtree, index); 1.1506 + 1.1507 + nsIXULTemplateResult* result = (*aSubtree)[index].mMatch->mResult; 1.1508 + 1.1509 + int32_t delta; 1.1510 + OpenSubtreeOf(child, aIndex + index, result, &delta); 1.1511 + count += delta; 1.1512 + } 1.1513 + 1.1514 + // Sort the container. 1.1515 + if (mSortVariable) { 1.1516 + NS_QuickSort(mRows.GetRowsFor(aSubtree), 1.1517 + aSubtree->Count(), 1.1518 + sizeof(nsTreeRows::Row), 1.1519 + Compare, 1.1520 + this); 1.1521 + } 1.1522 + 1.1523 + *aDelta = count; 1.1524 + return NS_OK; 1.1525 +} 1.1526 + 1.1527 +nsresult 1.1528 +nsXULTreeBuilder::OpenSubtreeForQuerySet(nsTreeRows::Subtree* aSubtree, 1.1529 + int32_t aIndex, 1.1530 + nsIXULTemplateResult* aResult, 1.1531 + nsTemplateQuerySet* aQuerySet, 1.1532 + int32_t* aDelta, 1.1533 + nsTArray<int32_t>& open) 1.1534 +{ 1.1535 + int32_t count = *aDelta; 1.1536 + 1.1537 + nsCOMPtr<nsISimpleEnumerator> results; 1.1538 + nsresult rv = mQueryProcessor->GenerateResults(mDataSource, aResult, 1.1539 + aQuerySet->mCompiledQuery, 1.1540 + getter_AddRefs(results)); 1.1541 + if (NS_FAILED(rv)) 1.1542 + return rv; 1.1543 + 1.1544 + bool hasMoreResults; 1.1545 + rv = results->HasMoreElements(&hasMoreResults); 1.1546 + 1.1547 + for (; NS_SUCCEEDED(rv) && hasMoreResults; 1.1548 + rv = results->HasMoreElements(&hasMoreResults)) { 1.1549 + nsCOMPtr<nsISupports> nr; 1.1550 + rv = results->GetNext(getter_AddRefs(nr)); 1.1551 + if (NS_FAILED(rv)) 1.1552 + return rv; 1.1553 + 1.1554 + nsCOMPtr<nsIXULTemplateResult> nextresult = do_QueryInterface(nr); 1.1555 + if (!nextresult) 1.1556 + return NS_ERROR_UNEXPECTED; 1.1557 + 1.1558 + nsCOMPtr<nsIRDFResource> resultid; 1.1559 + rv = GetResultResource(nextresult, getter_AddRefs(resultid)); 1.1560 + if (NS_FAILED(rv)) 1.1561 + return rv; 1.1562 + 1.1563 + if (! resultid) 1.1564 + continue; 1.1565 + 1.1566 + // check if there is already an existing match. If so, a previous 1.1567 + // query already generated content so the match is just added to the 1.1568 + // end of the set of matches. 1.1569 + 1.1570 + bool generateContent = true; 1.1571 + 1.1572 + nsTemplateMatch* prevmatch = nullptr; 1.1573 + nsTemplateMatch* existingmatch = nullptr; 1.1574 + if (mMatchMap.Get(resultid, &existingmatch)){ 1.1575 + // check if there is an existing match that matched a rule 1.1576 + while (existingmatch) { 1.1577 + if (existingmatch->IsActive()) 1.1578 + generateContent = false; 1.1579 + prevmatch = existingmatch; 1.1580 + existingmatch = existingmatch->mNext; 1.1581 + } 1.1582 + } 1.1583 + 1.1584 + nsTemplateMatch *newmatch = 1.1585 + nsTemplateMatch::Create(aQuerySet->Priority(), nextresult, nullptr); 1.1586 + if (!newmatch) 1.1587 + return NS_ERROR_OUT_OF_MEMORY; 1.1588 + 1.1589 + if (generateContent) { 1.1590 + // Don't allow cyclic graphs to get our knickers in a knot. 1.1591 + bool cyclic = false; 1.1592 + 1.1593 + if (aIndex >= 0) { 1.1594 + for (nsTreeRows::iterator iter = mRows[aIndex]; iter.GetDepth() > 0; iter.Pop()) { 1.1595 + nsCOMPtr<nsIRDFResource> parentid; 1.1596 + rv = GetResultResource(iter->mMatch->mResult, getter_AddRefs(parentid)); 1.1597 + if (NS_FAILED(rv)) { 1.1598 + nsTemplateMatch::Destroy(newmatch, false); 1.1599 + return rv; 1.1600 + } 1.1601 + 1.1602 + if (resultid == parentid) { 1.1603 + cyclic = true; 1.1604 + break; 1.1605 + } 1.1606 + } 1.1607 + } 1.1608 + 1.1609 + if (cyclic) { 1.1610 + NS_WARNING("tree cannot handle cyclic graphs"); 1.1611 + nsTemplateMatch::Destroy(newmatch, false); 1.1612 + continue; 1.1613 + } 1.1614 + 1.1615 + int16_t ruleindex; 1.1616 + nsTemplateRule* matchedrule = nullptr; 1.1617 + rv = DetermineMatchedRule(nullptr, nextresult, aQuerySet, 1.1618 + &matchedrule, &ruleindex); 1.1619 + if (NS_FAILED(rv)) { 1.1620 + nsTemplateMatch::Destroy(newmatch, false); 1.1621 + return rv; 1.1622 + } 1.1623 + 1.1624 + if (matchedrule) { 1.1625 + rv = newmatch->RuleMatched(aQuerySet, matchedrule, ruleindex, 1.1626 + nextresult); 1.1627 + if (NS_FAILED(rv)) { 1.1628 + nsTemplateMatch::Destroy(newmatch, false); 1.1629 + return rv; 1.1630 + } 1.1631 + 1.1632 + // Remember that this match applied to this row 1.1633 + mRows.InsertRowAt(newmatch, aSubtree, count); 1.1634 + 1.1635 + // If this is open, then remember it so we can recursively add 1.1636 + // *its* rows to the tree. 1.1637 + bool isOpen = false; 1.1638 + IsContainerOpen(nextresult, &isOpen); 1.1639 + if (isOpen) { 1.1640 + if (open.AppendElement(count) == nullptr) 1.1641 + return NS_ERROR_OUT_OF_MEMORY; 1.1642 + } 1.1643 + 1.1644 + ++count; 1.1645 + } 1.1646 + 1.1647 + if (mFlags & eLoggingEnabled) 1.1648 + OutputMatchToLog(resultid, newmatch, true); 1.1649 + 1.1650 + } 1.1651 + 1.1652 + if (prevmatch) { 1.1653 + prevmatch->mNext = newmatch; 1.1654 + } 1.1655 + else { 1.1656 + mMatchMap.Put(resultid, newmatch); 1.1657 + } 1.1658 + } 1.1659 + 1.1660 + *aDelta = count; 1.1661 + return rv; 1.1662 +} 1.1663 + 1.1664 +nsresult 1.1665 +nsXULTreeBuilder::CloseContainer(int32_t aIndex) 1.1666 +{ 1.1667 + NS_ASSERTION(aIndex >= 0 && aIndex < mRows.Count(), "bad row"); 1.1668 + if (aIndex < 0 || aIndex >= mRows.Count()) 1.1669 + return NS_ERROR_INVALID_ARG; 1.1670 + 1.1671 + nsTreeRows::iterator iter = mRows[aIndex]; 1.1672 + 1.1673 + if (iter->mSubtree) 1.1674 + RemoveMatchesFor(*iter->mSubtree); 1.1675 + 1.1676 + 1.1677 + int32_t count = mRows.GetSubtreeSizeFor(iter); 1.1678 + mRows.RemoveSubtreeFor(iter); 1.1679 + 1.1680 + iter->mContainerState = nsTreeRows::eContainerState_Closed; 1.1681 + 1.1682 + if (mBoxObject) { 1.1683 + mBoxObject->InvalidateRow(aIndex); 1.1684 + 1.1685 + if (count) 1.1686 + mBoxObject->RowCountChanged(aIndex + 1, -count); 1.1687 + } 1.1688 + 1.1689 + return NS_OK; 1.1690 +} 1.1691 + 1.1692 +nsresult 1.1693 +nsXULTreeBuilder::RemoveMatchesFor(nsTreeRows::Subtree& subtree) 1.1694 +{ 1.1695 + for (int32_t i = subtree.Count() - 1; i >= 0; --i) { 1.1696 + nsTreeRows::Row& row = subtree[i]; 1.1697 + 1.1698 + nsTemplateMatch* match = row.mMatch; 1.1699 + 1.1700 + nsCOMPtr<nsIRDFResource> id; 1.1701 + nsresult rv = GetResultResource(match->mResult, getter_AddRefs(id)); 1.1702 + if (NS_FAILED(rv)) 1.1703 + return rv; 1.1704 + 1.1705 + nsTemplateMatch* existingmatch; 1.1706 + if (mMatchMap.Get(id, &existingmatch)) { 1.1707 + while (existingmatch) { 1.1708 + nsTemplateMatch* nextmatch = existingmatch->mNext; 1.1709 + nsTemplateMatch::Destroy(existingmatch, true); 1.1710 + existingmatch = nextmatch; 1.1711 + } 1.1712 + 1.1713 + mMatchMap.Remove(id); 1.1714 + } 1.1715 + 1.1716 + if ((row.mContainerState == nsTreeRows::eContainerState_Open) && row.mSubtree) 1.1717 + RemoveMatchesFor(*(row.mSubtree)); 1.1718 + } 1.1719 + 1.1720 + return NS_OK; 1.1721 +} 1.1722 + 1.1723 +nsresult 1.1724 +nsXULTreeBuilder::IsContainerOpen(nsIXULTemplateResult *aResult, bool* aOpen) 1.1725 +{ 1.1726 + // items are never open if recursion is disabled 1.1727 + if ((mFlags & eDontRecurse) && aResult != mRootResult) { 1.1728 + *aOpen = false; 1.1729 + return NS_OK; 1.1730 + } 1.1731 + 1.1732 + nsCOMPtr<nsIRDFResource> id; 1.1733 + nsresult rv = GetResultResource(aResult, getter_AddRefs(id)); 1.1734 + if (NS_FAILED(rv)) 1.1735 + return rv; 1.1736 + 1.1737 + return IsContainerOpen(id, aOpen); 1.1738 +} 1.1739 + 1.1740 +nsresult 1.1741 +nsXULTreeBuilder::IsContainerOpen(nsIRDFResource* aResource, bool* aOpen) 1.1742 +{ 1.1743 + if (mPersistStateStore) 1.1744 + mPersistStateStore->HasAssertion(aResource, 1.1745 + nsXULContentUtils::NC_open, 1.1746 + nsXULContentUtils::true_, 1.1747 + true, 1.1748 + aOpen); 1.1749 + else 1.1750 + *aOpen = false; 1.1751 + 1.1752 + return NS_OK; 1.1753 +} 1.1754 + 1.1755 +int 1.1756 +nsXULTreeBuilder::Compare(const void* aLeft, const void* aRight, void* aClosure) 1.1757 +{ 1.1758 + nsXULTreeBuilder* self = static_cast<nsXULTreeBuilder*>(aClosure); 1.1759 + 1.1760 + nsTreeRows::Row* left = static_cast<nsTreeRows::Row*> 1.1761 + (const_cast<void*>(aLeft)); 1.1762 + 1.1763 + nsTreeRows::Row* right = static_cast<nsTreeRows::Row*> 1.1764 + (const_cast<void*>(aRight)); 1.1765 + 1.1766 + return self->CompareResults(left->mMatch->mResult, right->mMatch->mResult); 1.1767 +} 1.1768 + 1.1769 +int32_t 1.1770 +nsXULTreeBuilder::CompareResults(nsIXULTemplateResult* aLeft, nsIXULTemplateResult* aRight) 1.1771 +{ 1.1772 + // this is an extra check done for RDF queries such that results appear in 1.1773 + // the order they appear in their containing Seq 1.1774 + if (mSortDirection == eDirection_Natural && mDB) { 1.1775 + // If the sort order is ``natural'', then see if the container 1.1776 + // is an RDF sequence. If so, we'll try to use the ordinal 1.1777 + // properties to determine order. 1.1778 + // 1.1779 + // XXX the problem with this is, it doesn't always get the 1.1780 + // *real* container; e.g., 1.1781 + // 1.1782 + // <treerow uri="?uri" /> 1.1783 + // 1.1784 + // <triple subject="?uri" 1.1785 + // predicate="http://home.netscape.com/NC-rdf#subheadings" 1.1786 + // object="?subheadings" /> 1.1787 + // 1.1788 + // <member container="?subheadings" child="?subheading" /> 1.1789 + // 1.1790 + // In this case mRefVariable is bound to ?uri, not 1.1791 + // ?subheadings. (The ``container'' in the template sense != 1.1792 + // container in the RDF sense.) 1.1793 + 1.1794 + nsCOMPtr<nsISupports> ref; 1.1795 + nsresult rv = aLeft->GetBindingObjectFor(mRefVariable, getter_AddRefs(ref)); 1.1796 + if (NS_FAILED(rv)) 1.1797 + return 0; 1.1798 + 1.1799 + nsCOMPtr<nsIRDFResource> container = do_QueryInterface(ref); 1.1800 + if (container) { 1.1801 + bool isSequence = false; 1.1802 + gRDFContainerUtils->IsSeq(mDB, container, &isSequence); 1.1803 + if (isSequence) { 1.1804 + // Determine the indices of the left and right elements 1.1805 + // in the container. 1.1806 + int32_t lindex = 0, rindex = 0; 1.1807 + 1.1808 + nsCOMPtr<nsIRDFResource> leftitem; 1.1809 + aLeft->GetResource(getter_AddRefs(leftitem)); 1.1810 + if (leftitem) { 1.1811 + gRDFContainerUtils->IndexOf(mDB, container, leftitem, &lindex); 1.1812 + if (lindex < 0) 1.1813 + return 0; 1.1814 + } 1.1815 + 1.1816 + nsCOMPtr<nsIRDFResource> rightitem; 1.1817 + aRight->GetResource(getter_AddRefs(rightitem)); 1.1818 + if (rightitem) { 1.1819 + gRDFContainerUtils->IndexOf(mDB, container, rightitem, &rindex); 1.1820 + if (rindex < 0) 1.1821 + return 0; 1.1822 + } 1.1823 + 1.1824 + return lindex - rindex; 1.1825 + } 1.1826 + } 1.1827 + } 1.1828 + 1.1829 + int32_t sortorder; 1.1830 + if (!mQueryProcessor) 1.1831 + return 0; 1.1832 + 1.1833 + mQueryProcessor->CompareResults(aLeft, aRight, mSortVariable, mSortHints, &sortorder); 1.1834 + 1.1835 + if (sortorder) 1.1836 + sortorder = sortorder * mSortDirection; 1.1837 + return sortorder; 1.1838 +} 1.1839 + 1.1840 +nsresult 1.1841 +nsXULTreeBuilder::SortSubtree(nsTreeRows::Subtree* aSubtree) 1.1842 +{ 1.1843 + NS_QuickSort(mRows.GetRowsFor(aSubtree), 1.1844 + aSubtree->Count(), 1.1845 + sizeof(nsTreeRows::Row), 1.1846 + Compare, 1.1847 + this); 1.1848 + 1.1849 + for (int32_t i = aSubtree->Count() - 1; i >= 0; --i) { 1.1850 + nsTreeRows::Subtree* child = (*aSubtree)[i].mSubtree; 1.1851 + if (child) 1.1852 + SortSubtree(child); 1.1853 + } 1.1854 + 1.1855 + return NS_OK; 1.1856 +} 1.1857 + 1.1858 + 1.1859 +/* boolean canDrop (in long index, in long orientation); */ 1.1860 +NS_IMETHODIMP 1.1861 +nsXULTreeBuilder::CanDrop(int32_t index, int32_t orientation, 1.1862 + nsIDOMDataTransfer* dataTransfer, bool *_retval) 1.1863 +{ 1.1864 + *_retval = false; 1.1865 + uint32_t count = mObservers.Count(); 1.1866 + for (uint32_t i = 0; i < count; ++i) { 1.1867 + nsCOMPtr<nsIXULTreeBuilderObserver> observer = mObservers.SafeObjectAt(i); 1.1868 + if (observer) { 1.1869 + observer->CanDrop(index, orientation, dataTransfer, _retval); 1.1870 + if (*_retval) 1.1871 + break; 1.1872 + } 1.1873 + } 1.1874 + 1.1875 + return NS_OK; 1.1876 +} 1.1877 + 1.1878 +NS_IMETHODIMP 1.1879 +nsXULTreeBuilder::Drop(int32_t row, int32_t orient, nsIDOMDataTransfer* dataTransfer) 1.1880 +{ 1.1881 + uint32_t count = mObservers.Count(); 1.1882 + for (uint32_t i = 0; i < count; ++i) { 1.1883 + nsCOMPtr<nsIXULTreeBuilderObserver> observer = mObservers.SafeObjectAt(i); 1.1884 + if (observer) { 1.1885 + bool canDrop = false; 1.1886 + observer->CanDrop(row, orient, dataTransfer, &canDrop); 1.1887 + if (canDrop) 1.1888 + observer->OnDrop(row, orient, dataTransfer); 1.1889 + } 1.1890 + } 1.1891 + 1.1892 + return NS_OK; 1.1893 +} 1.1894 + 1.1895 +NS_IMETHODIMP 1.1896 +nsXULTreeBuilder::IsSorted(bool *_retval) 1.1897 +{ 1.1898 + *_retval = (mSortVariable != nullptr); 1.1899 + return NS_OK; 1.1900 +} 1.1901 +