1999-06-25 10:47:28 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-file-style: "stroustrup" -*-
|
1999-03-06 02:51:41 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-03-06 02:51:41 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-03-06 02:51:41 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-03-06 02:51:41 +03:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:43:54 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-03-06 02:51:41 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
This file provides the implementation for the sort service manager.
|
|
|
|
*/
|
|
|
|
|
1999-03-07 12:44:38 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsINameSpaceManager.h"
|
|
|
|
#include "nsIRDFContentModelBuilder.h"
|
|
|
|
#include "nsIRDFCompositeDataSource.h"
|
|
|
|
#include "nsIRDFNode.h"
|
|
|
|
#include "nsIRDFObserver.h"
|
|
|
|
#include "nsIRDFService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsINameSpaceManager.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsISupportsArray.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsLayoutCID.h"
|
|
|
|
#include "nsRDFCID.h"
|
1999-09-07 06:51:13 +04:00
|
|
|
#include "nsIXULContentUtils.h"
|
1999-03-07 12:44:38 +03:00
|
|
|
#include "nsString.h"
|
1999-03-29 23:52:54 +04:00
|
|
|
#include "nsXPIDLString.h"
|
1999-03-07 12:44:38 +03:00
|
|
|
#include "rdf.h"
|
|
|
|
#include "rdfutil.h"
|
|
|
|
|
|
|
|
#include "nsVoidArray.h"
|
1999-04-14 07:28:49 +04:00
|
|
|
#include "nsQuickSort.h"
|
1999-03-06 02:51:41 +03:00
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "nsIXULSortService.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "plhash.h"
|
|
|
|
#include "plstr.h"
|
1999-07-07 09:32:07 +04:00
|
|
|
#include "prlong.h"
|
1999-03-06 02:51:41 +03:00
|
|
|
#include "prlog.h"
|
|
|
|
|
1999-05-18 02:43:47 +04:00
|
|
|
#include "nsICollation.h"
|
|
|
|
#include "nsCollationCID.h"
|
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsINameSpaceManager.h"
|
|
|
|
#include "nsLayoutCID.h"
|
|
|
|
#include "nsRDFCID.h"
|
|
|
|
|
|
|
|
#include "nsIContent.h"
|
1999-05-06 23:28:22 +04:00
|
|
|
#include "nsIDOMText.h"
|
1999-03-06 02:51:41 +03:00
|
|
|
|
|
|
|
#include "nsVoidArray.h"
|
|
|
|
|
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
|
1999-03-07 12:44:38 +03:00
|
|
|
#include "nsIRDFContentModelBuilder.h"
|
|
|
|
#include "nsIRDFCompositeDataSource.h"
|
|
|
|
#include "nsIRDFNode.h"
|
|
|
|
#include "nsIRDFService.h"
|
1999-03-06 02:51:41 +03:00
|
|
|
#include "rdf.h"
|
|
|
|
|
1999-03-11 15:01:47 +03:00
|
|
|
#include "nsIDOMXULElement.h"
|
|
|
|
|
1999-07-18 01:36:17 +04:00
|
|
|
#include "nsILocale.h"
|
|
|
|
#include "nsLocaleCID.h"
|
|
|
|
#include "nsILocaleFactory.h"
|
|
|
|
|
1999-07-01 05:55:09 +04:00
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static NS_DEFINE_IID(kXULSortServiceCID, NS_XULSORTSERVICE_CID);
|
|
|
|
static NS_DEFINE_IID(kIXULSortServiceIID, NS_IXULSORTSERVICE_IID);
|
|
|
|
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
|
|
|
|
|
|
|
|
static NS_DEFINE_CID(kNameSpaceManagerCID, NS_NAMESPACEMANAGER_CID);
|
|
|
|
static NS_DEFINE_IID(kINameSpaceManagerIID, NS_INAMESPACEMANAGER_IID);
|
|
|
|
|
|
|
|
static NS_DEFINE_IID(kIContentIID, NS_ICONTENT_IID);
|
1999-05-06 23:28:22 +04:00
|
|
|
static NS_DEFINE_IID(kIDOMTextIID, NS_IDOMTEXT_IID);
|
1999-03-06 02:51:41 +03:00
|
|
|
|
|
|
|
static NS_DEFINE_IID(kIDomNodeIID, NS_IDOMNODE_IID);
|
|
|
|
static NS_DEFINE_IID(kIDomElementIID, NS_IDOMELEMENT_IID);
|
|
|
|
|
1999-03-07 12:44:38 +03:00
|
|
|
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
|
|
|
|
static NS_DEFINE_IID(kIRDFServiceIID, NS_IRDFSERVICE_IID);
|
|
|
|
static NS_DEFINE_IID(kIRDFResourceIID, NS_IRDFRESOURCE_IID);
|
|
|
|
static NS_DEFINE_IID(kIRDFLiteralIID, NS_IRDFLITERAL_IID);
|
|
|
|
|
1999-09-07 06:51:13 +04:00
|
|
|
static NS_DEFINE_CID(kXULContentUtilsCID, NS_XULCONTENTUTILS_CID);
|
1999-03-11 15:01:47 +03:00
|
|
|
static NS_DEFINE_IID(kIDomXulElementIID, NS_IDOMXULELEMENT_IID);
|
|
|
|
|
1999-05-18 02:43:47 +04:00
|
|
|
static NS_DEFINE_CID(kCollationFactoryCID, NS_COLLATIONFACTORY_CID);
|
|
|
|
static NS_DEFINE_IID(kICollationFactoryIID, NS_ICOLLATIONFACTORY_IID);
|
|
|
|
|
1999-09-08 07:04:45 +04:00
|
|
|
static NS_DEFINE_CID(kRDFInMemoryDataSourceCID, NS_RDFINMEMORYDATASOURCE_CID);
|
|
|
|
|
1999-07-18 01:36:17 +04:00
|
|
|
static NS_DEFINE_CID(kLocaleFactoryCID, NS_LOCALEFACTORY_CID);
|
|
|
|
static NS_DEFINE_IID(kILocaleFactoryIID, NS_ILOCALEFACTORY_IID);
|
|
|
|
static NS_DEFINE_CID(kLocaleCID, NS_LOCALE_CID);
|
|
|
|
static NS_DEFINE_IID(kILocaleIID, NS_ILOCALE_IID);
|
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
// XXX This is sure to change. Copied from mozilla/layout/xul/content/src/nsXULAtoms.cpp
|
|
|
|
static const char kXULNameSpaceURI[]
|
|
|
|
= "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
|
1999-03-19 04:52:18 +03:00
|
|
|
DEFINE_RDF_VOCAB(NC_NAMESPACE_URI, NC, Name);
|
1999-04-27 06:28:12 +04:00
|
|
|
DEFINE_RDF_VOCAB(NC_NAMESPACE_URI, NC, BookmarkSeparator);
|
1999-03-06 02:51:41 +03:00
|
|
|
|
1999-03-10 10:07:10 +03:00
|
|
|
|
|
|
|
typedef struct _sortStruct {
|
1999-11-11 02:50:34 +03:00
|
|
|
nsCOMPtr<nsIRDFResource> sortProperty;
|
|
|
|
nsCOMPtr<nsIRDFResource> sortProperty2;
|
|
|
|
nsCOMPtr<nsIRDFCompositeDataSource> db;
|
|
|
|
nsCOMPtr<nsIRDFService> rdfService;
|
|
|
|
nsCOMPtr<nsIRDFDataSource> mInner;
|
|
|
|
nsCOMPtr<nsISupportsArray> resCache;
|
|
|
|
nsCOMPtr<nsIAtom> kTreeCellAtom;
|
|
|
|
PRInt32 colIndex;
|
1999-11-20 11:46:22 +03:00
|
|
|
nsCOMPtr<nsIContent> parentContainer;
|
1999-11-11 02:50:34 +03:00
|
|
|
PRInt32 kNameSpaceID_XUL;
|
|
|
|
PRBool descendingSort;
|
|
|
|
PRBool naturalOrderSort;
|
1999-11-23 04:02:55 +03:00
|
|
|
PRBool inbetweenSeparatorSort;
|
1999-03-10 10:07:10 +03:00
|
|
|
} sortStruct, *sortPtr;
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-05-13 12:25:12 +04:00
|
|
|
int inplaceSortCallback(const void *data1, const void *data2, void *privateData);
|
1999-03-11 15:01:47 +03:00
|
|
|
|
1999-03-10 10:07:10 +03:00
|
|
|
|
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// ServiceImpl
|
|
|
|
//
|
|
|
|
// This is the sort service.
|
|
|
|
//
|
|
|
|
class XULSortServiceImpl : public nsIXULSortService
|
|
|
|
{
|
|
|
|
protected:
|
1999-08-30 14:05:40 +04:00
|
|
|
XULSortServiceImpl(void);
|
|
|
|
virtual ~XULSortServiceImpl(void);
|
1999-03-06 02:51:41 +03:00
|
|
|
|
1999-08-30 14:05:40 +04:00
|
|
|
static nsICollation *collationService;
|
|
|
|
|
1999-08-30 14:28:37 +04:00
|
|
|
friend nsresult NS_NewXULSortService(nsIXULSortService** mgr);
|
1999-03-06 02:51:41 +03:00
|
|
|
|
|
|
|
private:
|
1999-08-30 14:05:40 +04:00
|
|
|
static nsrefcnt gRefCnt;
|
|
|
|
static nsIAtom *kTreeAtom;
|
|
|
|
static nsIAtom *kTreeCellAtom;
|
|
|
|
static nsIAtom *kTreeChildrenAtom;
|
|
|
|
static nsIAtom *kTreeColAtom;
|
|
|
|
static nsIAtom *kTreeItemAtom;
|
1999-11-23 07:39:29 +03:00
|
|
|
static nsIAtom *kContainerAtom;
|
1999-08-30 14:05:40 +04:00
|
|
|
static nsIAtom *kResourceAtom;
|
1999-11-11 02:50:34 +03:00
|
|
|
static nsIAtom *kResource2Atom;
|
1999-11-23 04:02:55 +03:00
|
|
|
static nsIAtom *kSortActiveAtom;
|
1999-11-14 14:10:34 +03:00
|
|
|
static nsIAtom *kSortResourceAtom;
|
|
|
|
static nsIAtom *kSortResource2Atom;
|
1999-08-30 14:05:40 +04:00
|
|
|
static nsIAtom *kSortDirectionAtom;
|
1999-11-23 04:02:55 +03:00
|
|
|
static nsIAtom *kSortSeparatorsAtom;
|
1999-08-30 14:05:40 +04:00
|
|
|
static nsIAtom *kIdAtom;
|
|
|
|
static nsIAtom *kRDF_type;
|
1999-04-27 06:28:12 +04:00
|
|
|
|
1999-11-23 04:51:46 +03:00
|
|
|
static nsString trueStr;
|
1999-03-07 12:44:38 +03:00
|
|
|
|
1999-11-23 04:51:46 +03:00
|
|
|
static nsIRDFResource *kNC_Name;
|
|
|
|
static nsIRDFResource *kRDF_instanceOf;
|
|
|
|
static nsIRDFResource *kRDF_Seq;
|
1999-03-06 02:51:41 +03:00
|
|
|
|
1999-11-23 04:51:46 +03:00
|
|
|
static PRInt32 kNameSpaceID_XUL;
|
|
|
|
static PRInt32 kNameSpaceID_RDF;
|
|
|
|
|
|
|
|
static nsIRDFService *gRDFService;
|
|
|
|
static nsIXULContentUtils *gXULUtils;
|
1999-09-07 06:51:13 +04:00
|
|
|
|
1999-11-14 14:10:34 +03:00
|
|
|
PRBool IsTreeElement(nsIContent *element);
|
|
|
|
nsresult FindTreeElement(nsIContent *root, nsIContent* aElement,nsIContent** aTreeElement);
|
1999-06-25 10:47:28 +04:00
|
|
|
nsresult FindTreeChildrenElement(nsIContent *tree, nsIContent **treeBody);
|
1999-11-23 04:51:46 +03:00
|
|
|
nsresult GetSortColumnIndex(nsIContent *tree, const nsString &sortResource, const nsString& sortDirection, nsString &sortResource2, PRBool &inbetweenSeparatorSort, PRInt32 &colIndex, PRBool &found);
|
|
|
|
nsresult SetSortHints(nsIContent *tree, const nsString &sortResource, const nsString &sortDirection, const nsString &sortResource2, PRBool inbetweenSeparatorSort, PRBool found);
|
1999-11-23 04:02:55 +03:00
|
|
|
nsresult NodeHasSortInfo(nsIContent *node, nsString &sortResource, nsString &sortDirection, nsString &sortResource2, PRBool &inbetweenSeparatorSort, PRBool &found);
|
|
|
|
nsresult GetSortColumnInfo(nsIContent *tree, nsString &sortResource, nsString &sortDirection, nsString &sortResource2, PRBool &inbetweenSeparatorSort);
|
1999-03-06 02:51:41 +03:00
|
|
|
nsresult GetTreeCell(nsIContent *node, PRInt32 colIndex, nsIContent **cell);
|
1999-11-23 04:02:55 +03:00
|
|
|
nsresult SortTreeChildren(nsIContent *container, sortPtr sortInfo);
|
1999-08-30 14:05:40 +04:00
|
|
|
nsresult DoSort(nsIDOMNode* node, const nsString& sortResource, const nsString& sortDirection);
|
1999-03-06 02:51:41 +03:00
|
|
|
|
1999-09-08 07:04:45 +04:00
|
|
|
static nsresult GetCachedTarget(sortPtr sortInfo, nsIRDFResource* aSource, nsIRDFResource *aProperty, PRBool aTruthValue, nsIRDFNode **aResult);
|
1999-09-07 11:27:49 +04:00
|
|
|
static nsresult GetCachedResource(sortPtr sortInfo, nsIRDFResource *sortProperty, const char *suffix, nsIRDFResource **res);
|
1999-07-07 09:32:07 +04:00
|
|
|
static nsresult GetResourceValue(nsIRDFResource *res1, nsIRDFResource *sortProperty, sortPtr sortInfo, nsIRDFNode **, PRBool &isCollationKey);
|
|
|
|
static nsresult GetNodeValue(nsIContent *node1, nsIRDFResource *sortProperty, sortPtr sortInfo, nsIRDFNode **, PRBool &isCollationKey);
|
1999-05-13 20:57:59 +04:00
|
|
|
static nsresult GetTreeCell(sortPtr sortInfo, nsIContent *node, PRInt32 cellIndex, nsIContent **cell);
|
1999-11-14 14:10:34 +03:00
|
|
|
static nsresult GetNodeTextValue(sortPtr sortInfo, nsIContent *node, nsString & val);
|
1999-05-13 12:25:12 +04:00
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
public:
|
1999-09-07 11:27:49 +04:00
|
|
|
static nsresult InplaceSort(nsIContent *node1, nsIContent *node2, sortPtr sortInfo, PRInt32 & sortOrder);
|
1999-07-07 09:32:07 +04:00
|
|
|
static nsresult CompareNodes(nsIRDFNode *cellNode1, PRBool isCollationKey1,
|
|
|
|
nsIRDFNode *cellNode2, PRBool isCollationKey2,
|
|
|
|
PRInt32 & sortOrder);
|
1999-05-13 12:25:12 +04:00
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
// nsISupports
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsISortService
|
1999-08-30 14:05:40 +04:00
|
|
|
NS_DECL_NSIXULSORTSERVICE
|
1999-03-06 02:51:41 +03:00
|
|
|
};
|
|
|
|
|
1999-08-30 14:05:40 +04:00
|
|
|
|
|
|
|
|
1999-05-18 02:43:47 +04:00
|
|
|
nsICollation *XULSortServiceImpl::collationService = nsnull;
|
1999-08-30 14:05:40 +04:00
|
|
|
nsIRDFService *XULSortServiceImpl::gRDFService = nsnull;
|
1999-03-06 02:51:41 +03:00
|
|
|
nsrefcnt XULSortServiceImpl::gRefCnt = 0;
|
|
|
|
|
1999-09-07 06:51:13 +04:00
|
|
|
nsIXULContentUtils *XULSortServiceImpl::gXULUtils = nsnull;
|
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
nsIAtom* XULSortServiceImpl::kTreeAtom;
|
|
|
|
nsIAtom* XULSortServiceImpl::kTreeCellAtom;
|
|
|
|
nsIAtom* XULSortServiceImpl::kTreeChildrenAtom;
|
|
|
|
nsIAtom* XULSortServiceImpl::kTreeColAtom;
|
|
|
|
nsIAtom* XULSortServiceImpl::kTreeItemAtom;
|
1999-11-23 07:39:29 +03:00
|
|
|
nsIAtom* XULSortServiceImpl::kContainerAtom;
|
1999-03-06 02:51:41 +03:00
|
|
|
nsIAtom* XULSortServiceImpl::kResourceAtom;
|
1999-11-11 02:50:34 +03:00
|
|
|
nsIAtom* XULSortServiceImpl::kResource2Atom;
|
1999-11-23 04:02:55 +03:00
|
|
|
nsIAtom* XULSortServiceImpl::kSortActiveAtom;
|
1999-11-14 14:10:34 +03:00
|
|
|
nsIAtom* XULSortServiceImpl::kSortResourceAtom;
|
|
|
|
nsIAtom* XULSortServiceImpl::kSortResource2Atom;
|
1999-03-07 12:44:38 +03:00
|
|
|
nsIAtom* XULSortServiceImpl::kSortDirectionAtom;
|
1999-11-23 04:02:55 +03:00
|
|
|
nsIAtom* XULSortServiceImpl::kSortSeparatorsAtom;
|
1999-03-07 12:44:38 +03:00
|
|
|
nsIAtom* XULSortServiceImpl::kIdAtom;
|
1999-04-27 06:28:12 +04:00
|
|
|
nsIAtom* XULSortServiceImpl::kRDF_type;
|
|
|
|
|
1999-11-23 04:51:46 +03:00
|
|
|
nsString XULSortServiceImpl::trueStr;
|
|
|
|
|
1999-04-27 06:28:12 +04:00
|
|
|
nsIRDFResource *XULSortServiceImpl::kNC_Name;
|
1999-07-20 09:20:32 +04:00
|
|
|
nsIRDFResource *XULSortServiceImpl::kRDF_instanceOf;
|
|
|
|
nsIRDFResource *XULSortServiceImpl::kRDF_Seq;
|
1999-03-07 12:44:38 +03:00
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
PRInt32 XULSortServiceImpl::kNameSpaceID_XUL;
|
|
|
|
PRInt32 XULSortServiceImpl::kNameSpaceID_RDF;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-03-11 15:01:47 +03:00
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
XULSortServiceImpl::XULSortServiceImpl(void)
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
if (gRefCnt == 0)
|
|
|
|
{
|
|
|
|
kTreeAtom = NS_NewAtom("tree");
|
|
|
|
kTreeCellAtom = NS_NewAtom("treecell");
|
|
|
|
kTreeChildrenAtom = NS_NewAtom("treechildren");
|
|
|
|
kTreeColAtom = NS_NewAtom("treecol");
|
|
|
|
kTreeItemAtom = NS_NewAtom("treeitem");
|
1999-11-23 07:39:29 +03:00
|
|
|
kContainerAtom = NS_NewAtom("container");
|
1999-03-06 02:51:41 +03:00
|
|
|
kResourceAtom = NS_NewAtom("resource");
|
1999-11-11 02:50:34 +03:00
|
|
|
kResource2Atom = NS_NewAtom("resource2");
|
1999-11-23 04:02:55 +03:00
|
|
|
kSortActiveAtom = NS_NewAtom("sortActive");
|
1999-11-14 14:10:34 +03:00
|
|
|
kSortResourceAtom = NS_NewAtom("sortResource");
|
|
|
|
kSortResource2Atom = NS_NewAtom("sortResource2");
|
1999-03-07 12:44:38 +03:00
|
|
|
kSortDirectionAtom = NS_NewAtom("sortDirection");
|
1999-11-23 04:02:55 +03:00
|
|
|
kSortSeparatorsAtom = NS_NewAtom("sortSeparators");
|
1999-03-07 12:44:38 +03:00
|
|
|
kIdAtom = NS_NewAtom("id");
|
1999-04-27 06:28:12 +04:00
|
|
|
kRDF_type = NS_NewAtom("type");
|
1999-03-06 02:51:41 +03:00
|
|
|
|
1999-11-23 04:51:46 +03:00
|
|
|
trueStr = "true";
|
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
1999-03-07 12:44:38 +03:00
|
|
|
if (NS_FAILED(rv = nsServiceManager::GetService(kRDFServiceCID,
|
|
|
|
kIRDFServiceIID, (nsISupports**) &gRDFService)))
|
|
|
|
{
|
|
|
|
NS_ERROR("couldn't create rdf service");
|
|
|
|
}
|
1999-04-27 06:28:12 +04:00
|
|
|
|
1999-09-07 06:51:13 +04:00
|
|
|
rv = nsServiceManager::GetService(kXULContentUtilsCID,
|
1999-11-20 11:46:22 +03:00
|
|
|
nsCOMTypeInfo<nsIXULContentUtils>::GetIID(),
|
|
|
|
(nsISupports**) &gXULUtils);
|
1999-09-07 06:51:13 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get XUL content utils");
|
|
|
|
|
1999-07-18 01:36:17 +04:00
|
|
|
// get a locale factory
|
1999-10-15 10:27:02 +04:00
|
|
|
nsCOMPtr<nsIFactory> aFactory;
|
|
|
|
nsCOMPtr<nsILocaleFactory> localeFactory;
|
|
|
|
if (NS_SUCCEEDED(rv = nsComponentManager::FindFactory(kLocaleFactoryCID, getter_AddRefs(aFactory)))
|
|
|
|
&& ((localeFactory = do_QueryInterface(aFactory)) != nsnull))
|
1999-05-18 02:43:47 +04:00
|
|
|
{
|
1999-10-15 10:27:02 +04:00
|
|
|
nsCOMPtr<nsILocale> locale;
|
|
|
|
if (NS_SUCCEEDED(rv = localeFactory->GetApplicationLocale(getter_AddRefs(locale))) && (locale))
|
1999-05-18 02:43:47 +04:00
|
|
|
{
|
1999-10-15 10:27:02 +04:00
|
|
|
nsCOMPtr<nsICollationFactory> colFactory;
|
1999-07-18 01:36:17 +04:00
|
|
|
if (NS_SUCCEEDED(rv = nsComponentManager::CreateInstance(kCollationFactoryCID, NULL,
|
1999-10-15 10:27:02 +04:00
|
|
|
kICollationFactoryIID, getter_AddRefs(colFactory))))
|
1999-07-18 01:36:17 +04:00
|
|
|
{
|
|
|
|
if (NS_FAILED(rv = colFactory->CreateCollation(locale, &collationService)))
|
|
|
|
{
|
|
|
|
NS_ERROR("couldn't create collation instance");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NS_ERROR("couldn't create instance of collation factory");
|
|
|
|
}
|
1999-05-18 02:43:47 +04:00
|
|
|
}
|
1999-07-18 01:36:17 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
NS_ERROR("unable to get application locale");
|
|
|
|
}
|
1999-05-18 02:43:47 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-07-18 01:36:17 +04:00
|
|
|
NS_ERROR("couldn't get locale factory");
|
1999-05-18 02:43:47 +04:00
|
|
|
}
|
|
|
|
|
1999-07-20 09:20:32 +04:00
|
|
|
gRDFService->GetResource(kURINC_Name, &kNC_Name);
|
|
|
|
gRDFService->GetResource(RDF_NAMESPACE_URI "instanceOf", &kRDF_instanceOf);
|
|
|
|
gRDFService->GetResource(RDF_NAMESPACE_URI "Seq", &kRDF_Seq);
|
1999-04-27 06:28:12 +04:00
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
// Register the XUL and RDF namespaces: these'll just retrieve
|
|
|
|
// the IDs if they've already been registered by someone else.
|
|
|
|
nsINameSpaceManager* mgr;
|
1999-03-09 12:44:27 +03:00
|
|
|
if (NS_SUCCEEDED(rv = nsComponentManager::CreateInstance(kNameSpaceManagerCID,
|
1999-03-06 02:51:41 +03:00
|
|
|
nsnull,
|
|
|
|
kINameSpaceManagerIID,
|
|
|
|
(void**) &mgr)))
|
|
|
|
{
|
1999-07-18 01:36:17 +04:00
|
|
|
static const char kRDFNameSpaceURI[] = RDF_NAMESPACE_URI;
|
1999-03-06 02:51:41 +03:00
|
|
|
|
|
|
|
rv = mgr->RegisterNameSpace(kXULNameSpaceURI, kNameSpaceID_XUL);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to register XUL namespace");
|
|
|
|
|
|
|
|
rv = mgr->RegisterNameSpace(kRDFNameSpaceURI, kNameSpaceID_RDF);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to register RDF namespace");
|
|
|
|
|
|
|
|
NS_RELEASE(mgr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NS_ERROR("couldn't create namepsace manager");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++gRefCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-03-11 15:01:47 +03:00
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
XULSortServiceImpl::~XULSortServiceImpl(void)
|
|
|
|
{
|
1999-10-07 05:51:30 +04:00
|
|
|
#ifdef DEBUG_REFS
|
|
|
|
--gInstanceCount;
|
|
|
|
fprintf(stdout, "%d - RDF: XULSortServiceImpl\n", gInstanceCount);
|
|
|
|
#endif
|
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
--gRefCnt;
|
|
|
|
if (gRefCnt == 0)
|
|
|
|
{
|
1999-08-30 14:05:40 +04:00
|
|
|
NS_IF_RELEASE(kTreeAtom);
|
|
|
|
NS_IF_RELEASE(kTreeCellAtom);
|
|
|
|
NS_IF_RELEASE(kTreeChildrenAtom);
|
|
|
|
NS_IF_RELEASE(kTreeColAtom);
|
|
|
|
NS_IF_RELEASE(kTreeItemAtom);
|
1999-11-23 07:39:29 +03:00
|
|
|
NS_IF_RELEASE(kContainerAtom);
|
1999-08-30 14:05:40 +04:00
|
|
|
NS_IF_RELEASE(kResourceAtom);
|
1999-11-11 02:50:34 +03:00
|
|
|
NS_IF_RELEASE(kResource2Atom);
|
1999-11-23 04:02:55 +03:00
|
|
|
NS_IF_RELEASE(kSortActiveAtom);
|
1999-11-14 14:10:34 +03:00
|
|
|
NS_IF_RELEASE(kSortResourceAtom);
|
|
|
|
NS_IF_RELEASE(kSortResource2Atom);
|
1999-08-30 14:05:40 +04:00
|
|
|
NS_IF_RELEASE(kSortDirectionAtom);
|
1999-11-23 04:02:55 +03:00
|
|
|
NS_IF_RELEASE(kSortSeparatorsAtom);
|
1999-08-30 14:05:40 +04:00
|
|
|
NS_IF_RELEASE(kIdAtom);
|
|
|
|
NS_IF_RELEASE(kRDF_type);
|
|
|
|
NS_IF_RELEASE(kNC_Name);
|
|
|
|
NS_IF_RELEASE(kRDF_instanceOf);
|
|
|
|
NS_IF_RELEASE(kRDF_Seq);
|
|
|
|
|
|
|
|
NS_IF_RELEASE(collationService);
|
|
|
|
collationService = nsnull;
|
1999-09-07 06:51:13 +04:00
|
|
|
|
1999-11-20 11:46:22 +03:00
|
|
|
if (gRDFService)
|
|
|
|
{
|
|
|
|
nsServiceManager::ReleaseService(kRDFServiceCID, gRDFService);
|
|
|
|
gRDFService = nsnull;
|
1999-09-07 06:51:13 +04:00
|
|
|
}
|
|
|
|
|
1999-11-20 11:46:22 +03:00
|
|
|
if (gXULUtils)
|
|
|
|
{
|
|
|
|
nsServiceManager::ReleaseService(kXULContentUtilsCID, gXULUtils);
|
|
|
|
gXULUtils = nsnull;
|
1999-09-07 06:51:13 +04:00
|
|
|
}
|
1999-03-06 02:51:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-03-11 15:01:47 +03:00
|
|
|
|
1999-05-13 12:25:12 +04:00
|
|
|
NS_IMPL_ISUPPORTS(XULSortServiceImpl, nsIXULSortService::GetIID());
|
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
|
1999-03-11 15:01:47 +03:00
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-03-11 15:01:47 +03:00
|
|
|
|
|
|
|
|
1999-11-14 14:10:34 +03:00
|
|
|
PRBool
|
|
|
|
XULSortServiceImpl::IsTreeElement(nsIContent *element)
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
1999-11-14 14:10:34 +03:00
|
|
|
PRBool isTreeNode = PR_FALSE;
|
|
|
|
PRInt32 nameSpaceID;
|
|
|
|
nsresult rv;
|
1999-03-06 02:51:41 +03:00
|
|
|
|
1999-11-14 14:10:34 +03:00
|
|
|
if (NS_SUCCEEDED(rv = element->GetNameSpaceID(nameSpaceID)))
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
|
|
|
if (nameSpaceID == kNameSpaceID_XUL)
|
|
|
|
{
|
1999-11-14 14:10:34 +03:00
|
|
|
nsCOMPtr<nsIAtom> tag;
|
|
|
|
if (NS_FAILED(rv = element->GetTag(*getter_AddRefs(tag))))
|
|
|
|
return(rv);
|
1999-03-06 02:51:41 +03:00
|
|
|
if (tag.get() == kTreeAtom)
|
1999-11-14 14:10:34 +03:00
|
|
|
{
|
|
|
|
isTreeNode = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(isTreeNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
XULSortServiceImpl::FindTreeElement(nsIContent *root, nsIContent *aElement, nsIContent **aTreeElement)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
*aTreeElement = nsnull;
|
|
|
|
|
|
|
|
if (root)
|
|
|
|
{
|
|
|
|
// we have a root hint, so look under it for the tree tag
|
|
|
|
|
|
|
|
PRInt32 numKids, loop;
|
|
|
|
if (NS_SUCCEEDED(rv = root->ChildCount(numKids)))
|
|
|
|
{
|
|
|
|
for (loop=0; loop<numKids; loop++)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> child;
|
|
|
|
if (NS_FAILED(rv = root->ChildAt(loop, *getter_AddRefs(child))))
|
|
|
|
return(rv);
|
|
|
|
if (IsTreeElement(child) == PR_TRUE)
|
|
|
|
{
|
|
|
|
*aTreeElement = child;
|
|
|
|
NS_ADDREF(*aTreeElement);
|
|
|
|
return(NS_OK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we don't have a root hint, so look from the current
|
|
|
|
// node upwards until we find it (or hit the top)
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> element(do_QueryInterface(aElement));
|
|
|
|
while (element)
|
|
|
|
{
|
|
|
|
if (IsTreeElement(element) == PR_TRUE)
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
|
|
|
*aTreeElement = element;
|
|
|
|
NS_ADDREF(*aTreeElement);
|
1999-11-14 14:10:34 +03:00
|
|
|
return(NS_OK);
|
1999-03-06 02:51:41 +03:00
|
|
|
}
|
1999-11-14 14:10:34 +03:00
|
|
|
nsCOMPtr<nsIContent> parent;
|
|
|
|
element->GetParent(*getter_AddRefs(parent));
|
|
|
|
element = parent;
|
1999-03-06 02:51:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
1999-03-11 15:01:47 +03:00
|
|
|
|
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
nsresult
|
1999-06-25 10:47:28 +04:00
|
|
|
XULSortServiceImpl::FindTreeChildrenElement(nsIContent *tree, nsIContent **treeBody)
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> child;
|
|
|
|
PRInt32 childIndex = 0, numChildren = 0, nameSpaceID;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv = tree->ChildCount(numChildren))) return(rv);
|
|
|
|
for (childIndex=0; childIndex<numChildren; childIndex++)
|
|
|
|
{
|
|
|
|
if (NS_FAILED(rv = tree->ChildAt(childIndex, *getter_AddRefs(child)))) return(rv);
|
|
|
|
if (NS_FAILED(rv = child->GetNameSpaceID(nameSpaceID))) return rv;
|
|
|
|
if (nameSpaceID == kNameSpaceID_XUL)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAtom> tag;
|
|
|
|
if (NS_FAILED(rv = child->GetTag(*getter_AddRefs(tag)))) return rv;
|
1999-06-25 10:47:28 +04:00
|
|
|
if (tag.get() == kTreeChildrenAtom)
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
|
|
|
*treeBody = child;
|
|
|
|
NS_ADDREF(*treeBody);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
1999-03-11 15:01:47 +03:00
|
|
|
|
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
nsresult
|
1999-11-23 04:02:55 +03:00
|
|
|
XULSortServiceImpl::GetSortColumnIndex(nsIContent *tree, const nsString &sortResource, const nsString &sortDirection,
|
1999-11-23 04:51:46 +03:00
|
|
|
nsString &sortResource2, PRBool &inbetweenSeparatorSort, PRInt32 &sortColIndex, PRBool &found)
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
1999-03-07 12:44:38 +03:00
|
|
|
PRInt32 childIndex, colIndex = 0, numChildren, nameSpaceID;
|
1999-03-06 02:51:41 +03:00
|
|
|
nsCOMPtr<nsIContent> child;
|
|
|
|
nsresult rv;
|
|
|
|
|
1999-11-23 04:51:46 +03:00
|
|
|
found = PR_FALSE;
|
1999-11-23 04:02:55 +03:00
|
|
|
inbetweenSeparatorSort = PR_FALSE;
|
|
|
|
|
|
|
|
sortColIndex = 0;
|
1999-03-06 02:51:41 +03:00
|
|
|
if (NS_FAILED(rv = tree->ChildCount(numChildren))) return(rv);
|
|
|
|
for (childIndex=0; childIndex<numChildren; childIndex++)
|
|
|
|
{
|
|
|
|
if (NS_FAILED(rv = tree->ChildAt(childIndex, *getter_AddRefs(child)))) return(rv);
|
|
|
|
if (NS_FAILED(rv = child->GetNameSpaceID(nameSpaceID))) return(rv);
|
|
|
|
if (nameSpaceID == kNameSpaceID_XUL)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAtom> tag;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv = child->GetTag(*getter_AddRefs(tag))))
|
1999-11-23 04:51:46 +03:00
|
|
|
return(rv);
|
1999-03-06 02:51:41 +03:00
|
|
|
if (tag.get() == kTreeColAtom)
|
|
|
|
{
|
1999-11-23 04:02:55 +03:00
|
|
|
nsAutoString value;
|
1999-03-06 02:51:41 +03:00
|
|
|
|
1999-11-23 04:02:55 +03:00
|
|
|
if (NS_SUCCEEDED(rv = child->GetAttribute(kNameSpaceID_RDF, kResourceAtom, value))
|
1999-08-30 14:05:40 +04:00
|
|
|
&& (rv == NS_CONTENT_ATTR_HAS_VALUE))
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
1999-06-25 13:19:29 +04:00
|
|
|
PRBool setFlag = PR_FALSE;
|
1999-11-23 04:02:55 +03:00
|
|
|
if (value == sortResource)
|
1999-06-25 13:19:29 +04:00
|
|
|
{
|
1999-11-23 04:02:55 +03:00
|
|
|
sortColIndex = colIndex;
|
1999-11-23 04:51:46 +03:00
|
|
|
|
1999-06-25 13:19:29 +04:00
|
|
|
if (!sortDirection.EqualsIgnoreCase("natural"))
|
|
|
|
{
|
1999-11-23 05:27:03 +03:00
|
|
|
found = PR_TRUE;
|
1999-06-25 13:19:29 +04:00
|
|
|
setFlag = PR_TRUE;
|
1999-11-23 04:51:46 +03:00
|
|
|
|
|
|
|
// secondary sort info is optional
|
|
|
|
if (NS_FAILED(rv = child->GetAttribute(kNameSpaceID_RDF, kResource2Atom,
|
|
|
|
sortResource2)) || (rv != NS_CONTENT_ATTR_HAS_VALUE))
|
|
|
|
{
|
|
|
|
sortResource2.Truncate();
|
|
|
|
}
|
1999-06-25 13:19:29 +04:00
|
|
|
}
|
|
|
|
}
|
1999-11-23 04:02:55 +03:00
|
|
|
if (NS_SUCCEEDED(rv = child->GetAttribute(kNameSpaceID_None, kSortSeparatorsAtom, value))
|
1999-11-23 04:51:46 +03:00
|
|
|
&& (rv == NS_CONTENT_ATTR_HAS_VALUE) && (value.EqualsIgnoreCase(trueStr)))
|
1999-11-23 04:02:55 +03:00
|
|
|
{
|
|
|
|
inbetweenSeparatorSort = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
1999-06-25 13:19:29 +04:00
|
|
|
if (setFlag == PR_TRUE)
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
1999-11-23 04:02:55 +03:00
|
|
|
child->SetAttribute(kNameSpaceID_None, kSortActiveAtom, trueStr, PR_TRUE);
|
1999-03-07 12:44:38 +03:00
|
|
|
child->SetAttribute(kNameSpaceID_None, kSortDirectionAtom, sortDirection, PR_TRUE);
|
1999-06-25 13:19:29 +04:00
|
|
|
|
|
|
|
// Note: don't break out of loop; want to set/unset attribs on ALL sort columns
|
1999-03-07 12:44:38 +03:00
|
|
|
// break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-11-23 04:02:55 +03:00
|
|
|
child->UnsetAttribute(kNameSpaceID_None, kSortActiveAtom, PR_TRUE);
|
1999-06-14 12:20:35 +04:00
|
|
|
child->UnsetAttribute(kNameSpaceID_None, kSortDirectionAtom, PR_TRUE);
|
1999-03-07 12:44:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
++colIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-11-23 04:51:46 +03:00
|
|
|
SetSortHints(tree, sortResource, sortDirection, sortResource2, inbetweenSeparatorSort, found);
|
|
|
|
return(NS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
XULSortServiceImpl::SetSortHints(nsIContent *tree, const nsString &sortResource, const nsString &sortDirection,
|
|
|
|
const nsString &sortResource2, PRBool inbetweenSeparatorSort, PRBool found)
|
|
|
|
{
|
|
|
|
if (found == PR_TRUE)
|
|
|
|
{
|
|
|
|
// set hints on tree root node
|
|
|
|
tree->SetAttribute(kNameSpaceID_None, kSortActiveAtom, trueStr, PR_FALSE);
|
|
|
|
tree->SetAttribute(kNameSpaceID_None, kSortDirectionAtom, sortDirection, PR_FALSE);
|
|
|
|
tree->SetAttribute(kNameSpaceID_RDF, kResourceAtom, sortResource, PR_FALSE);
|
|
|
|
|
|
|
|
if (sortResource2.Length() > 0)
|
|
|
|
tree->SetAttribute(kNameSpaceID_RDF, kResource2Atom, sortDirection, PR_FALSE);
|
|
|
|
else tree->UnsetAttribute(kNameSpaceID_RDF, kResource2Atom, PR_FALSE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tree->UnsetAttribute(kNameSpaceID_None, kSortActiveAtom, PR_FALSE);
|
|
|
|
tree->UnsetAttribute(kNameSpaceID_None, kSortDirectionAtom, PR_FALSE);
|
|
|
|
tree->UnsetAttribute(kNameSpaceID_RDF, kResourceAtom, PR_FALSE);
|
|
|
|
tree->UnsetAttribute(kNameSpaceID_RDF, kResource2Atom, PR_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// optional hint
|
|
|
|
if (inbetweenSeparatorSort == PR_TRUE)
|
|
|
|
tree->SetAttribute(kNameSpaceID_None, kSortSeparatorsAtom, trueStr, PR_FALSE);
|
|
|
|
else tree->UnsetAttribute(kNameSpaceID_None, kSortSeparatorsAtom, PR_FALSE);
|
|
|
|
|
|
|
|
return(NS_OK);
|
1999-03-07 12:44:38 +03:00
|
|
|
}
|
|
|
|
|
1999-03-11 15:01:47 +03:00
|
|
|
|
|
|
|
|
1999-03-07 12:44:38 +03:00
|
|
|
nsresult
|
1999-11-23 04:02:55 +03:00
|
|
|
XULSortServiceImpl::NodeHasSortInfo(nsIContent *child, nsString &sortResource, nsString &sortDirection,
|
|
|
|
nsString &sortResource2, PRBool &inbetweenSeparatorSort, PRBool &found)
|
1999-03-07 12:44:38 +03:00
|
|
|
{
|
1999-11-23 04:02:55 +03:00
|
|
|
nsresult rv;
|
1999-03-07 12:44:38 +03:00
|
|
|
|
1999-11-23 04:02:55 +03:00
|
|
|
inbetweenSeparatorSort = PR_FALSE;
|
|
|
|
found = PR_FALSE;
|
|
|
|
|
|
|
|
nsAutoString value;
|
|
|
|
if (NS_SUCCEEDED(rv = child->GetAttribute(kNameSpaceID_None, kSortActiveAtom, value))
|
|
|
|
&& (rv == NS_CONTENT_ATTR_HAS_VALUE))
|
1999-03-07 12:44:38 +03:00
|
|
|
{
|
1999-11-23 04:51:46 +03:00
|
|
|
if (value.EqualsIgnoreCase(trueStr))
|
1999-03-07 12:44:38 +03:00
|
|
|
{
|
1999-11-23 04:02:55 +03:00
|
|
|
if (NS_SUCCEEDED(rv = child->GetAttribute(kNameSpaceID_RDF, kResourceAtom,
|
|
|
|
sortResource)) && (rv == NS_CONTENT_ATTR_HAS_VALUE))
|
1999-03-07 12:44:38 +03:00
|
|
|
{
|
1999-11-23 04:02:55 +03:00
|
|
|
if (NS_SUCCEEDED(rv = child->GetAttribute(kNameSpaceID_None, kSortDirectionAtom,
|
|
|
|
sortDirection)) && (rv == NS_CONTENT_ATTR_HAS_VALUE))
|
1999-03-07 12:44:38 +03:00
|
|
|
{
|
1999-11-23 04:02:55 +03:00
|
|
|
found = PR_TRUE;
|
|
|
|
|
|
|
|
// sort separator flag is optional
|
|
|
|
if (NS_SUCCEEDED(rv = child->GetAttribute(kNameSpaceID_None, kSortSeparatorsAtom,
|
|
|
|
value)) && (rv == NS_CONTENT_ATTR_HAS_VALUE))
|
1999-03-07 12:44:38 +03:00
|
|
|
{
|
1999-11-23 04:51:46 +03:00
|
|
|
if (value.EqualsIgnoreCase(trueStr))
|
1999-03-07 12:44:38 +03:00
|
|
|
{
|
1999-11-23 04:02:55 +03:00
|
|
|
inbetweenSeparatorSort = PR_TRUE;
|
1999-03-07 12:44:38 +03:00
|
|
|
}
|
1999-11-23 04:02:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// secondary sort info is optional
|
|
|
|
if (NS_FAILED(rv = child->GetAttribute(kNameSpaceID_RDF, kResource2Atom,
|
|
|
|
sortResource2)) || (rv != NS_CONTENT_ATTR_HAS_VALUE))
|
|
|
|
{
|
|
|
|
sortResource2.Truncate();
|
1999-03-06 02:51:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-11-23 04:02:55 +03:00
|
|
|
return(NS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
XULSortServiceImpl::GetSortColumnInfo(nsIContent *tree, nsString &sortResource,
|
|
|
|
nsString &sortDirection, nsString &sortResource2, PRBool &inbetweenSeparatorSort)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> child;
|
|
|
|
PRBool found = PR_FALSE;
|
1999-11-23 04:51:46 +03:00
|
|
|
PRInt32 childIndex, nameSpaceID, numChildren;
|
1999-11-23 04:02:55 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv = NodeHasSortInfo(tree, sortResource, sortDirection,
|
|
|
|
sortResource2, inbetweenSeparatorSort, found)) && (found == PR_TRUE))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (NS_FAILED(rv = tree->ChildCount(numChildren))) return(rv);
|
|
|
|
for (childIndex=0; childIndex<numChildren; childIndex++)
|
|
|
|
{
|
|
|
|
if (NS_FAILED(rv = tree->ChildAt(childIndex, *getter_AddRefs(child)))) return(rv);
|
1999-11-23 04:51:46 +03:00
|
|
|
if (NS_FAILED(rv = child->GetNameSpaceID(nameSpaceID))) return(rv);
|
|
|
|
if (nameSpaceID != kNameSpaceID_XUL) continue;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAtom> tag;
|
|
|
|
if (NS_FAILED(rv = child->GetTag(*getter_AddRefs(tag)))) return(rv);
|
|
|
|
if (tag.get() != kTreeColAtom) continue;
|
|
|
|
|
1999-11-23 04:02:55 +03:00
|
|
|
if (NS_SUCCEEDED(rv = NodeHasSortInfo(child, sortResource, sortDirection,
|
|
|
|
sortResource2, inbetweenSeparatorSort, found)) && (found == PR_TRUE))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-11-23 04:51:46 +03:00
|
|
|
SetSortHints(tree, sortResource, sortDirection, sortResource2, inbetweenSeparatorSort, found);
|
1999-11-23 04:02:55 +03:00
|
|
|
}
|
1999-03-06 02:51:41 +03:00
|
|
|
return((found == PR_TRUE) ? NS_OK : NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
1999-03-11 15:01:47 +03:00
|
|
|
|
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
nsresult
|
1999-05-13 12:25:12 +04:00
|
|
|
XULSortServiceImpl::GetTreeCell(sortPtr sortInfo, nsIContent *node, PRInt32 cellIndex, nsIContent **cell)
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
|
|
|
PRBool found = PR_FALSE;
|
|
|
|
PRInt32 childIndex = 0, numChildren = 0, nameSpaceID;
|
|
|
|
nsCOMPtr<nsIContent> child;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv = node->ChildCount(numChildren))) return(rv);
|
|
|
|
|
|
|
|
for (childIndex=0; childIndex<numChildren; childIndex++)
|
|
|
|
{
|
1999-03-11 15:01:47 +03:00
|
|
|
if (NS_FAILED(rv = node->ChildAt(childIndex, *getter_AddRefs(child)))) break;
|
1999-03-06 02:51:41 +03:00
|
|
|
if (NS_FAILED(rv = child->GetNameSpaceID(nameSpaceID))) break;
|
1999-03-11 15:01:47 +03:00
|
|
|
if (nameSpaceID == sortInfo->kNameSpaceID_XUL)
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAtom> tag;
|
|
|
|
if (NS_FAILED(rv = child->GetTag(*getter_AddRefs(tag)))) return rv;
|
1999-11-11 02:50:34 +03:00
|
|
|
if (tag.get() == sortInfo->kTreeCellAtom.get())
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
|
|
|
if (cellIndex == 0)
|
|
|
|
{
|
|
|
|
found = PR_TRUE;
|
|
|
|
*cell = child;
|
|
|
|
NS_ADDREF(*cell);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
--cellIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return((found == PR_TRUE) ? NS_OK : NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-03-11 15:01:47 +03:00
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
nsresult
|
1999-11-14 14:10:34 +03:00
|
|
|
XULSortServiceImpl::GetNodeTextValue(sortPtr sortInfo, nsIContent *node, nsString & val)
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
|
|
|
PRBool found = PR_FALSE;
|
|
|
|
PRInt32 childIndex = 0, numChildren = 0, nameSpaceID;
|
|
|
|
nsCOMPtr<nsIContent> child;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv = node->ChildCount(numChildren))) return(rv);
|
|
|
|
|
|
|
|
for (childIndex=0; childIndex<numChildren; childIndex++)
|
|
|
|
{
|
1999-05-06 23:28:22 +04:00
|
|
|
if (NS_FAILED(rv = node->ChildAt(childIndex, *getter_AddRefs(child))))
|
1999-06-25 10:47:28 +04:00
|
|
|
break;
|
1999-05-06 23:28:22 +04:00
|
|
|
if (NS_FAILED(rv = child->GetNameSpaceID(nameSpaceID)))
|
1999-06-25 10:47:28 +04:00
|
|
|
break;
|
1999-03-11 15:01:47 +03:00
|
|
|
if (nameSpaceID != sortInfo->kNameSpaceID_XUL)
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
1999-06-25 10:47:28 +04:00
|
|
|
// Get text using the DOM
|
|
|
|
nsCOMPtr<nsIDOMText> domText;
|
|
|
|
rv = child->QueryInterface(kIDOMTextIID, getter_AddRefs(domText));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
val.Truncate();
|
|
|
|
domText->GetData(val);
|
|
|
|
found = PR_TRUE;
|
|
|
|
break;
|
1999-03-06 02:51:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return((found == PR_TRUE) ? NS_OK : NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
1999-03-11 15:01:47 +03:00
|
|
|
|
|
|
|
|
1999-05-13 12:25:12 +04:00
|
|
|
nsresult
|
1999-07-07 09:32:07 +04:00
|
|
|
XULSortServiceImpl::CompareNodes(nsIRDFNode *cellNode1, PRBool isCollationKey1,
|
|
|
|
nsIRDFNode *cellNode2, PRBool isCollationKey2,
|
|
|
|
PRInt32 & sortOrder)
|
1999-03-07 12:44:38 +03:00
|
|
|
{
|
1999-07-07 09:32:07 +04:00
|
|
|
sortOrder = 0;
|
1999-03-07 12:44:38 +03:00
|
|
|
|
1999-11-17 05:03:46 +03:00
|
|
|
const PRUnichar *uni1 = nsnull, *uni2 = nsnull;
|
1999-07-07 09:32:07 +04:00
|
|
|
nsCOMPtr<nsIRDFLiteral> literal1 = do_QueryInterface(cellNode1);
|
|
|
|
nsCOMPtr<nsIRDFLiteral> literal2 = do_QueryInterface(cellNode2);
|
1999-11-17 05:03:46 +03:00
|
|
|
if (literal1) literal1->GetValueConst(&uni1);
|
|
|
|
if (literal2) literal2->GetValueConst(&uni2);
|
1999-05-13 12:25:12 +04:00
|
|
|
|
1999-07-07 09:32:07 +04:00
|
|
|
if (isCollationKey1 == PR_TRUE && isCollationKey2 == PR_TRUE)
|
|
|
|
{
|
|
|
|
// sort collation keys
|
|
|
|
if (collationService)
|
|
|
|
{
|
1999-11-17 05:03:46 +03:00
|
|
|
collationService->CompareSortKey(uni1, uni2, &sortOrder);
|
1999-07-07 09:32:07 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// without a collation service, unable to collate
|
|
|
|
sortOrder = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((isCollationKey1 == PR_TRUE) && (isCollationKey2 == PR_FALSE))
|
1999-05-18 10:19:04 +04:00
|
|
|
{
|
|
|
|
sortOrder = -1;
|
|
|
|
}
|
|
|
|
else if ((isCollationKey1 == PR_FALSE) && (isCollationKey2 == PR_TRUE))
|
|
|
|
{
|
|
|
|
sortOrder = 1;
|
|
|
|
}
|
1999-11-14 14:10:34 +03:00
|
|
|
else if (literal1 || literal2)
|
1999-03-07 12:44:38 +03:00
|
|
|
{
|
1999-11-14 14:10:34 +03:00
|
|
|
// not a collation key, but one or both are strings
|
|
|
|
if (literal1 && literal2)
|
|
|
|
{
|
1999-11-17 05:03:46 +03:00
|
|
|
sortOrder = nsCRT::strcasecmp(uni1, uni2);
|
1999-11-14 14:10:34 +03:00
|
|
|
}
|
|
|
|
else if (literal1) sortOrder = -1;
|
|
|
|
else sortOrder = 1;
|
1999-07-07 09:32:07 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// not a collation key, and both aren't strings, so try other data types (ints)
|
|
|
|
nsCOMPtr<nsIRDFInt> intLiteral1 = do_QueryInterface(cellNode1);
|
|
|
|
nsCOMPtr<nsIRDFInt> intLiteral2 = do_QueryInterface(cellNode2);
|
|
|
|
if (intLiteral1 && intLiteral2)
|
1999-03-07 12:44:38 +03:00
|
|
|
{
|
1999-07-07 09:32:07 +04:00
|
|
|
PRInt32 intVal1, intVal2;
|
|
|
|
intLiteral1->GetValue(&intVal1);
|
|
|
|
intLiteral2->GetValue(&intVal2);
|
|
|
|
|
|
|
|
sortOrder = 0;
|
|
|
|
if (intVal1 < intVal2) sortOrder = -1;
|
|
|
|
else if (intVal1 > intVal2) sortOrder = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// not a collation key, and both aren't strings, so try other data types (dates)
|
|
|
|
nsCOMPtr<nsIRDFDate> dateLiteral1 = do_QueryInterface(cellNode1);
|
|
|
|
nsCOMPtr<nsIRDFDate> dateLiteral2 = do_QueryInterface(cellNode2);
|
|
|
|
if (dateLiteral1 && dateLiteral2)
|
|
|
|
{
|
|
|
|
PRInt64 dateVal1, dateVal2;
|
|
|
|
dateLiteral1->GetValue(&dateVal1);
|
|
|
|
dateLiteral2->GetValue(&dateVal2);
|
|
|
|
|
|
|
|
sortOrder = 0;
|
|
|
|
if (LL_CMP(dateVal1, <, dateVal2)) sortOrder = -1;
|
1999-08-11 10:24:56 +04:00
|
|
|
else if (LL_CMP(dateVal1, >, dateVal2)) sortOrder = 1;
|
1999-07-07 09:32:07 +04:00
|
|
|
}
|
1999-03-07 12:44:38 +03:00
|
|
|
}
|
|
|
|
}
|
1999-07-07 09:32:07 +04:00
|
|
|
return(NS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-09-08 07:04:45 +04:00
|
|
|
nsresult
|
|
|
|
XULSortServiceImpl::GetCachedTarget(sortPtr sortInfo, nsIRDFResource* aSource,
|
|
|
|
nsIRDFResource *aProperty, PRBool aTruthValue, nsIRDFNode **aResult)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK, rvTemp;
|
|
|
|
|
|
|
|
if (!(sortInfo->mInner))
|
|
|
|
{
|
|
|
|
// if we don't have a mInner, create one
|
|
|
|
rvTemp = nsComponentManager::CreateInstance(kRDFInMemoryDataSourceCID,
|
|
|
|
nsnull, nsIRDFDataSource::GetIID(), (void **)&(sortInfo->mInner));
|
|
|
|
}
|
|
|
|
if (sortInfo->mInner)
|
|
|
|
{
|
|
|
|
// else, if we do have a mInner, look for the resource in it
|
|
|
|
rv = sortInfo->mInner->GetTarget(aSource, aProperty, aTruthValue, aResult);
|
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(rv) && (rv == NS_RDF_NO_VALUE) && (sortInfo->db))
|
|
|
|
{
|
|
|
|
// if we don't have a cached value, look it up in the document's DB
|
|
|
|
if (NS_SUCCEEDED(rv = (sortInfo->db)->GetTarget(aSource, aProperty,
|
|
|
|
aTruthValue, aResult)) && (rv != NS_RDF_NO_VALUE))
|
|
|
|
{
|
|
|
|
// and if we have a value, cache it away in our mInner also
|
|
|
|
rvTemp = sortInfo->mInner->Assert(aSource, aProperty, *aResult, PR_TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-03-19 04:52:18 +03:00
|
|
|
nsresult
|
1999-09-07 11:27:49 +04:00
|
|
|
XULSortServiceImpl::GetCachedResource(sortPtr sortInfo, nsIRDFResource *sortProperty, const char *suffix, nsIRDFResource **res)
|
1999-03-19 04:52:18 +03:00
|
|
|
{
|
1999-09-07 11:27:49 +04:00
|
|
|
nsresult rv;
|
1999-03-19 04:52:18 +03:00
|
|
|
|
1999-09-07 11:27:49 +04:00
|
|
|
*res = nsnull;
|
1999-05-13 12:25:12 +04:00
|
|
|
|
1999-09-07 11:27:49 +04:00
|
|
|
const char *sortPropertyURI = nsnull;
|
|
|
|
rv = sortProperty->GetValueConst(&sortPropertyURI);
|
|
|
|
if (NS_SUCCEEDED(rv) && (sortPropertyURI))
|
1999-03-19 04:52:18 +03:00
|
|
|
{
|
1999-09-07 11:27:49 +04:00
|
|
|
nsAutoString resName(sortPropertyURI);
|
|
|
|
if (suffix) resName += suffix;
|
1999-03-19 04:52:18 +03:00
|
|
|
|
1999-09-07 11:27:49 +04:00
|
|
|
if (!(sortInfo->resCache))
|
|
|
|
{
|
|
|
|
// if we don't have a cache, create one
|
|
|
|
rv = NS_NewISupportsArray(getter_AddRefs(sortInfo->resCache));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// else, if we do have a cache, look for the resource in it
|
|
|
|
PRUint32 numRes;
|
|
|
|
if (NS_SUCCEEDED(rv = sortInfo->resCache->Count(&numRes)))
|
1999-05-13 12:25:12 +04:00
|
|
|
{
|
1999-09-07 11:27:49 +04:00
|
|
|
PRUint32 loop;
|
|
|
|
for (loop=0; loop<numRes; loop++)
|
1999-03-19 04:52:18 +03:00
|
|
|
{
|
1999-09-07 11:27:49 +04:00
|
|
|
nsCOMPtr<nsISupports> iSupports;
|
|
|
|
if (NS_SUCCEEDED(rv = sortInfo->resCache->GetElementAt(loop,
|
|
|
|
getter_AddRefs(iSupports))))
|
1999-03-19 04:52:18 +03:00
|
|
|
{
|
1999-09-07 11:27:49 +04:00
|
|
|
nsCOMPtr<nsIRDFResource> aRes = do_QueryInterface(iSupports);
|
|
|
|
if (aRes)
|
|
|
|
{
|
|
|
|
const char *resURI = nsnull;
|
|
|
|
if (NS_SUCCEEDED(rv = aRes->GetValueConst(&resURI)) && (resURI))
|
|
|
|
{
|
|
|
|
if (resName.Equals(resURI))
|
|
|
|
{
|
|
|
|
// found res in cache, so just return it
|
|
|
|
*res = aRes;
|
|
|
|
NS_ADDREF(*res);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-03-19 04:52:18 +03:00
|
|
|
}
|
|
|
|
}
|
1999-05-13 12:25:12 +04:00
|
|
|
}
|
1999-09-07 11:27:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(*res))
|
|
|
|
{
|
1999-11-11 02:50:34 +03:00
|
|
|
nsCOMPtr<nsIRDFResource> sortRes;
|
1999-11-17 05:03:46 +03:00
|
|
|
if (NS_SUCCEEDED(rv = sortInfo->rdfService->GetUnicodeResource(resName.GetUnicode(),
|
1999-11-11 02:50:34 +03:00
|
|
|
getter_AddRefs(sortRes))) && (sortRes))
|
1999-05-13 12:25:12 +04:00
|
|
|
{
|
1999-11-11 02:50:34 +03:00
|
|
|
*res = sortRes;
|
|
|
|
NS_ADDREF(*res);
|
|
|
|
|
|
|
|
// and add it into the cache array
|
|
|
|
if (sortInfo->resCache)
|
1999-03-19 04:52:18 +03:00
|
|
|
{
|
1999-11-11 02:50:34 +03:00
|
|
|
sortInfo->resCache->AppendElement(sortRes);
|
1999-09-07 11:27:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
XULSortServiceImpl::GetResourceValue(nsIRDFResource *res1, nsIRDFResource *sortProperty, sortPtr sortInfo,
|
|
|
|
nsIRDFNode **target, PRBool &isCollationKey)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
*target = nsnull;
|
|
|
|
isCollationKey = PR_FALSE;
|
|
|
|
|
|
|
|
if ((res1) && (sortInfo->naturalOrderSort == PR_FALSE) && (sortInfo->sortProperty))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRDFResource> modSortRes;
|
|
|
|
|
|
|
|
// for any given property, first ask the graph for its value with "?collation=true" appended
|
|
|
|
// to indicate that if there is a collation key available for this value, we want it
|
|
|
|
if (NS_SUCCEEDED(rv = GetCachedResource(sortInfo, sortInfo->sortProperty, "?collation=true",
|
|
|
|
getter_AddRefs(modSortRes))) && (modSortRes))
|
|
|
|
{
|
1999-09-08 07:04:45 +04:00
|
|
|
if (NS_SUCCEEDED(rv = GetCachedTarget(sortInfo, res1, modSortRes,
|
1999-09-07 11:27:49 +04:00
|
|
|
PR_TRUE, target)) && (rv != NS_RDF_NO_VALUE))
|
|
|
|
{
|
|
|
|
isCollationKey = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(*target))
|
|
|
|
{
|
|
|
|
// if no collation key, ask the graph for its value with "?sort=true" appended
|
|
|
|
// to indicate that if there is any distinction between its display value and sorting
|
|
|
|
// value, we want the sorting value (so that, for example, a mail datasource could strip
|
|
|
|
// off a "Re:" on a mail message subject)
|
|
|
|
if (NS_SUCCEEDED(rv = GetCachedResource(sortInfo, sortInfo->sortProperty, "?sort=true",
|
|
|
|
getter_AddRefs(modSortRes))) && (modSortRes))
|
|
|
|
{
|
1999-09-08 07:04:45 +04:00
|
|
|
if (NS_SUCCEEDED(rv = GetCachedTarget(sortInfo, res1, modSortRes,
|
1999-09-07 11:27:49 +04:00
|
|
|
PR_TRUE, target)) && (rv != NS_RDF_NO_VALUE))
|
|
|
|
{
|
1999-05-13 12:25:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-07-07 09:32:07 +04:00
|
|
|
if (!(*target))
|
1999-05-13 12:25:12 +04:00
|
|
|
{
|
|
|
|
// if no collation key and no special sorting value, just get the property value
|
1999-09-08 07:04:45 +04:00
|
|
|
if (NS_SUCCEEDED(rv = GetCachedTarget(sortInfo, res1, sortProperty,
|
1999-09-07 11:27:49 +04:00
|
|
|
PR_TRUE, target) && (rv != NS_RDF_NO_VALUE)))
|
1999-05-13 12:25:12 +04:00
|
|
|
{
|
1999-03-19 04:52:18 +03:00
|
|
|
}
|
1999-05-18 10:19:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
XULSortServiceImpl::GetNodeValue(nsIContent *node1, nsIRDFResource *sortProperty, sortPtr sortInfo,
|
1999-07-07 09:32:07 +04:00
|
|
|
nsIRDFNode **theNode, PRBool &isCollationKey)
|
1999-05-18 10:19:04 +04:00
|
|
|
{
|
1999-11-14 14:10:34 +03:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIRDFResource> res1;
|
1999-05-18 10:19:04 +04:00
|
|
|
|
|
|
|
isCollationKey = PR_FALSE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMXULElement> dom1 = do_QueryInterface(node1);
|
1999-11-14 14:10:34 +03:00
|
|
|
if (dom1)
|
1999-05-19 09:30:25 +04:00
|
|
|
{
|
1999-11-14 14:10:34 +03:00
|
|
|
if (NS_FAILED(rv = dom1->GetResource(getter_AddRefs(res1))))
|
|
|
|
{
|
|
|
|
res1 = nsnull;
|
|
|
|
}
|
|
|
|
// Note: don't check for res1 QI failure here. It only succeeds for RDF nodes,
|
|
|
|
// but for XUL nodes it will failure; in the failure case, the code below gets
|
|
|
|
// the cell's text value straight from the DOM
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMElement> htmlDom = do_QueryInterface(node1);
|
|
|
|
if (htmlDom)
|
|
|
|
{
|
|
|
|
nsAutoString htmlID;
|
|
|
|
if (NS_SUCCEEDED(rv = node1->GetAttribute(kNameSpaceID_None, kIdAtom, htmlID))
|
|
|
|
&& (rv == NS_CONTENT_ATTR_HAS_VALUE))
|
|
|
|
{
|
1999-11-17 05:03:46 +03:00
|
|
|
if (NS_FAILED(rv = gRDFService->GetUnicodeResource(htmlID.GetUnicode(),
|
1999-11-14 14:10:34 +03:00
|
|
|
getter_AddRefs(res1))))
|
|
|
|
{
|
|
|
|
res1 = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return(NS_ERROR_FAILURE);
|
|
|
|
}
|
1999-05-19 09:30:25 +04:00
|
|
|
}
|
1999-05-18 10:19:04 +04:00
|
|
|
|
|
|
|
if ((sortInfo->naturalOrderSort == PR_FALSE) && (sortInfo->sortProperty))
|
|
|
|
{
|
1999-11-14 14:10:34 +03:00
|
|
|
if (res1)
|
|
|
|
{
|
|
|
|
rv = GetResourceValue(res1, sortProperty, sortInfo, theNode, isCollationKey);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rv = NS_RDF_NO_VALUE;
|
|
|
|
}
|
1999-07-07 09:32:07 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv) || (rv == NS_RDF_NO_VALUE))
|
1999-05-18 10:19:04 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> cell1;
|
1999-11-14 14:10:34 +03:00
|
|
|
if (sortInfo->colIndex >= 0)
|
|
|
|
{
|
|
|
|
rv = GetTreeCell(sortInfo, node1, sortInfo->colIndex, getter_AddRefs(cell1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cell1 = node1;
|
|
|
|
}
|
|
|
|
if (cell1)
|
1999-03-19 04:52:18 +03:00
|
|
|
{
|
1999-07-07 09:32:07 +04:00
|
|
|
nsAutoString cellVal1;
|
1999-11-14 14:10:34 +03:00
|
|
|
if (NS_SUCCEEDED(rv = GetNodeTextValue(sortInfo, cell1, cellVal1)) &&
|
1999-07-07 09:32:07 +04:00
|
|
|
(rv != NS_RDF_NO_VALUE))
|
|
|
|
{
|
1999-11-11 02:50:34 +03:00
|
|
|
nsCOMPtr<nsIRDFLiteral> nodeLiteral;
|
1999-11-17 05:03:46 +03:00
|
|
|
rv = gRDFService->GetLiteral(cellVal1.GetUnicode(), getter_AddRefs(nodeLiteral));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
*theNode = nodeLiteral;
|
|
|
|
NS_IF_ADDREF(*theNode);
|
|
|
|
isCollationKey = PR_FALSE;
|
|
|
|
}
|
1999-07-07 09:32:07 +04:00
|
|
|
}
|
1999-03-19 04:52:18 +03:00
|
|
|
}
|
|
|
|
}
|
1999-05-13 12:25:12 +04:00
|
|
|
}
|
1999-11-20 11:46:22 +03:00
|
|
|
else if ((sortInfo->naturalOrderSort == PR_TRUE) && (sortInfo->parentContainer))
|
1999-05-13 12:25:12 +04:00
|
|
|
{
|
1999-07-07 09:32:07 +04:00
|
|
|
nsAutoString cellPosVal1;
|
1999-07-20 07:13:15 +04:00
|
|
|
|
|
|
|
// check to see if this is a RDF_Seq
|
|
|
|
// Note: this code doesn't handle the aggregated Seq case especially well
|
|
|
|
if ((res1) && (sortInfo->db))
|
|
|
|
{
|
1999-11-20 11:46:22 +03:00
|
|
|
nsCOMPtr<nsIRDFResource> parentResource;
|
|
|
|
nsCOMPtr<nsIDOMXULElement> parentDOMNode = do_QueryInterface(sortInfo->parentContainer);
|
|
|
|
if (parentDOMNode)
|
|
|
|
{
|
|
|
|
if (NS_FAILED(rv = parentDOMNode->GetResource(getter_AddRefs(parentResource))))
|
|
|
|
{
|
|
|
|
parentResource = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-07-20 07:13:15 +04:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> arcs;
|
1999-11-20 11:46:22 +03:00
|
|
|
if ((parentResource) && (NS_SUCCEEDED(rv = sortInfo->db->ArcLabelsIn(res1, getter_AddRefs(arcs)))))
|
1999-07-20 07:13:15 +04:00
|
|
|
{
|
|
|
|
PRBool hasMore = PR_TRUE;
|
|
|
|
while(hasMore)
|
|
|
|
{
|
|
|
|
if (NS_FAILED(rv = arcs->HasMoreElements(&hasMore))) break;
|
|
|
|
if (hasMore == PR_FALSE) break;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> isupports;
|
|
|
|
if (NS_FAILED(rv = arcs->GetNext(getter_AddRefs(isupports)))) break;
|
|
|
|
nsCOMPtr<nsIRDFResource> property = do_QueryInterface(isupports);
|
|
|
|
if (!property) continue;
|
|
|
|
|
|
|
|
// hack: it it looks like a RDF_Seq and smells like a RDF_Seq...
|
|
|
|
static const char kRDFNameSpace_Seq_Prefix[] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#_";
|
|
|
|
const char *uri = nsnull;
|
|
|
|
if (NS_FAILED(rv = property->GetValueConst(&uri))) continue;
|
|
|
|
if (!uri) continue;
|
|
|
|
if (nsCRT::strncasecmp(uri, kRDFNameSpace_Seq_Prefix, sizeof(kRDFNameSpace_Seq_Prefix)-1))
|
|
|
|
continue;
|
|
|
|
|
1999-11-20 11:46:22 +03:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> srcs;
|
|
|
|
if (NS_FAILED(rv = sortInfo->db->GetSources(property, res1, PR_TRUE, getter_AddRefs(srcs))))
|
|
|
|
continue;
|
|
|
|
PRBool hasMoreSrcs = PR_TRUE;
|
|
|
|
while(hasMoreSrcs)
|
|
|
|
{
|
|
|
|
if (NS_FAILED(rv = srcs->HasMoreElements(&hasMoreSrcs))) break;
|
|
|
|
if (hasMoreSrcs == PR_FALSE) break;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> isupports2;
|
|
|
|
if (NS_FAILED(rv = srcs->GetNext(getter_AddRefs(isupports2)))) break;
|
|
|
|
nsCOMPtr<nsIRDFResource> src = do_QueryInterface(isupports2);
|
|
|
|
if (!src) continue;
|
|
|
|
|
|
|
|
if (src == parentResource)
|
|
|
|
{
|
|
|
|
cellPosVal1 = uri;
|
|
|
|
cellPosVal1.Cut(0, sizeof(kRDFNameSpace_Seq_Prefix)-1);
|
|
|
|
|
|
|
|
// hack: assume that its a number, so pad out a bit
|
|
|
|
nsAutoString zero("000000");
|
|
|
|
if (cellPosVal1.Length() < zero.Length())
|
|
|
|
{
|
|
|
|
cellPosVal1.Insert(zero, 0, zero.Length() - cellPosVal1.Length());
|
|
|
|
}
|
|
|
|
hasMore = PR_FALSE;
|
|
|
|
hasMoreSrcs = PR_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-07-20 07:13:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-11-23 04:02:55 +03:00
|
|
|
if (cellPosVal1.Length() > 0)
|
1999-07-07 09:32:07 +04:00
|
|
|
{
|
1999-11-11 02:50:34 +03:00
|
|
|
nsCOMPtr<nsIRDFLiteral> nodePosLiteral;
|
|
|
|
gRDFService->GetLiteral(cellPosVal1.GetUnicode(), getter_AddRefs(nodePosLiteral));
|
1999-07-07 09:32:07 +04:00
|
|
|
*theNode = nodePosLiteral;
|
1999-11-11 02:50:34 +03:00
|
|
|
NS_IF_ADDREF(*theNode);
|
1999-07-07 09:32:07 +04:00
|
|
|
isCollationKey = PR_FALSE;
|
|
|
|
}
|
1999-03-19 04:52:18 +03:00
|
|
|
}
|
|
|
|
return(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-05-13 12:25:12 +04:00
|
|
|
nsresult
|
1999-09-07 11:27:49 +04:00
|
|
|
XULSortServiceImpl::InplaceSort(nsIContent *node1, nsIContent *node2, sortPtr sortInfo, PRInt32 & sortOrder)
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
1999-05-13 12:25:12 +04:00
|
|
|
PRBool isCollationKey1 = PR_FALSE, isCollationKey2 = PR_FALSE;
|
1999-03-11 15:01:47 +03:00
|
|
|
nsresult rv;
|
1999-03-19 04:52:18 +03:00
|
|
|
|
1999-07-07 09:32:07 +04:00
|
|
|
sortOrder = 0;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFNode> cellNode1, cellNode2;
|
|
|
|
GetNodeValue(node1, sortInfo->sortProperty, sortInfo, getter_AddRefs(cellNode1), isCollationKey1);
|
|
|
|
GetNodeValue(node2, sortInfo->sortProperty, sortInfo, getter_AddRefs(cellNode2), isCollationKey2);
|
1999-11-11 02:50:34 +03:00
|
|
|
if ((!cellNode1) && (!cellNode2) && (sortInfo->sortProperty2 == nsnull))
|
1999-03-19 04:52:18 +03:00
|
|
|
{
|
1999-11-11 02:50:34 +03:00
|
|
|
rv = GetNodeValue(node1, kNC_Name, sortInfo, getter_AddRefs(cellNode1), isCollationKey1);
|
|
|
|
rv = GetNodeValue(node2, kNC_Name, sortInfo, getter_AddRefs(cellNode2), isCollationKey2);
|
1999-03-19 04:52:18 +03:00
|
|
|
}
|
1999-05-13 12:25:12 +04:00
|
|
|
|
1999-07-07 09:32:07 +04:00
|
|
|
rv = CompareNodes(cellNode1, isCollationKey1, cellNode2, isCollationKey2, sortOrder);
|
1999-05-13 12:25:12 +04:00
|
|
|
|
1999-11-11 02:50:34 +03:00
|
|
|
if (sortOrder == 0)
|
|
|
|
{
|
|
|
|
// nodes appear to be equivalent, check for secondary sort criteria
|
|
|
|
if (sortInfo->sortProperty2 != nsnull)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRDFResource> temp = sortInfo->sortProperty;
|
|
|
|
sortInfo->sortProperty = sortInfo->sortProperty2;
|
|
|
|
sortInfo->sortProperty2 = nsnull;
|
|
|
|
|
|
|
|
rv = InplaceSort(node1, node2, sortInfo, sortOrder);
|
|
|
|
|
|
|
|
sortInfo->sortProperty2 = sortInfo->sortProperty;
|
|
|
|
sortInfo->sortProperty = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-03-19 04:52:18 +03:00
|
|
|
if (sortInfo->descendingSort == PR_TRUE)
|
|
|
|
{
|
1999-05-13 12:25:12 +04:00
|
|
|
// descending sort is being imposed, so reverse the sort order
|
1999-03-19 04:52:18 +03:00
|
|
|
sortOrder = -sortOrder;
|
|
|
|
}
|
1999-05-13 12:25:12 +04:00
|
|
|
|
|
|
|
return(NS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
inplaceSortCallback(const void *data1, const void *data2, void *privateData)
|
|
|
|
{
|
|
|
|
/// Note: inplaceSortCallback is a small C callback stub for NS_QuickSort
|
|
|
|
|
|
|
|
_sortStruct *sortInfo = (_sortStruct *)privateData;
|
|
|
|
nsIContent *node1 = *(nsIContent **)data1;
|
|
|
|
nsIContent *node2 = *(nsIContent **)data2;
|
|
|
|
PRInt32 sortOrder = 0;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (nsnull != sortInfo)
|
|
|
|
{
|
1999-09-07 11:27:49 +04:00
|
|
|
rv = XULSortServiceImpl::InplaceSort(node1, node2, sortInfo, sortOrder);
|
1999-05-13 12:25:12 +04:00
|
|
|
}
|
1999-03-19 04:52:18 +03:00
|
|
|
return(sortOrder);
|
1999-03-06 02:51:41 +03:00
|
|
|
}
|
|
|
|
|
1999-03-07 12:44:38 +03:00
|
|
|
|
|
|
|
|
1999-03-06 02:51:41 +03:00
|
|
|
nsresult
|
1999-11-23 04:02:55 +03:00
|
|
|
XULSortServiceImpl::SortTreeChildren(nsIContent *container, sortPtr sortInfo)
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
1999-11-23 07:43:01 +03:00
|
|
|
PRInt32 childIndex = 0, loop, numChildren = 0, numElements = 0, currentElement, nameSpaceID;
|
1999-03-06 02:51:41 +03:00
|
|
|
nsCOMPtr<nsIContent> child;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv = container->ChildCount(numChildren))) return(rv);
|
1999-11-23 04:02:55 +03:00
|
|
|
if (numChildren < 1) return(NS_OK);
|
1999-03-06 02:51:41 +03:00
|
|
|
|
1999-11-23 07:39:29 +03:00
|
|
|
// Note: This is a straight allocation (not a COMPtr) so we
|
|
|
|
// can't return out of this routine until/unless we free it!
|
|
|
|
nsIContent ** flatArray = new nsIContent*[numChildren + 1];
|
|
|
|
if (!flatArray) return(NS_ERROR_OUT_OF_MEMORY);
|
1999-03-06 02:51:41 +03:00
|
|
|
|
1999-11-23 07:39:29 +03:00
|
|
|
// Note: walk backwards (and add nodes into the array backwards) because
|
|
|
|
// we also remove the nodes in this loop [via RemoveChildAt()] and if we
|
|
|
|
// were to do this in a forward-looking manner it would be harder
|
|
|
|
// (since we also skip over non XUL:treeitem nodes)
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAtom> tag;
|
|
|
|
currentElement = numChildren;
|
|
|
|
for (childIndex=numChildren-1; childIndex >= 0; childIndex--)
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
1999-11-23 07:39:29 +03:00
|
|
|
if (NS_FAILED(rv = container->ChildAt(childIndex, *getter_AddRefs(child)))) continue;
|
|
|
|
if (NS_FAILED(rv = child->GetNameSpaceID(nameSpaceID))) continue;
|
1999-03-06 02:51:41 +03:00
|
|
|
if (nameSpaceID == kNameSpaceID_XUL)
|
|
|
|
{
|
1999-11-23 07:39:29 +03:00
|
|
|
if (NS_FAILED(rv = child->GetTag(*getter_AddRefs(tag)))) continue;
|
1999-03-06 02:51:41 +03:00
|
|
|
if (tag.get() == kTreeItemAtom)
|
|
|
|
{
|
1999-11-23 07:39:29 +03:00
|
|
|
--currentElement;
|
|
|
|
flatArray[currentElement] = child;
|
|
|
|
NS_ADDREF(flatArray[currentElement]); // Note: addref it here
|
|
|
|
|
|
|
|
// Bug 6665. This is a hack to "addref" the resources
|
|
|
|
// before we remove them from the content model. This
|
|
|
|
// keeps them from getting released, which causes
|
|
|
|
// performance problems for some datasources.
|
|
|
|
nsIRDFResource *resource;
|
|
|
|
gXULUtils->GetElementResource(flatArray[currentElement], &resource);
|
|
|
|
// Note: we don't release; see part deux below...
|
|
|
|
|
|
|
|
++numElements;
|
|
|
|
|
|
|
|
// immediately remove the child node, and ignore any errors
|
|
|
|
container->RemoveChildAt(childIndex, PR_FALSE);
|
1999-03-06 02:51:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-03-11 15:01:47 +03:00
|
|
|
if (numElements > 0)
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
1999-11-23 07:39:29 +03:00
|
|
|
/* smart sorting (sort within separators) on name column */
|
|
|
|
if (sortInfo->inbetweenSeparatorSort == PR_TRUE)
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
1999-11-23 07:39:29 +03:00
|
|
|
PRUint32 startIndex=currentElement;
|
|
|
|
nsAutoString type;
|
|
|
|
for (loop=currentElement; loop< currentElement + numElements; loop++)
|
1999-04-27 06:28:12 +04:00
|
|
|
{
|
1999-11-23 07:39:29 +03:00
|
|
|
if (NS_SUCCEEDED(rv = flatArray[loop]->GetAttribute(kNameSpaceID_None, kRDF_type, type))
|
|
|
|
&& (rv == NS_CONTENT_ATTR_HAS_VALUE))
|
1999-04-27 06:28:12 +04:00
|
|
|
{
|
1999-11-23 07:39:29 +03:00
|
|
|
if (type.Equals(kURINC_BookmarkSeparator))
|
1999-04-27 06:28:12 +04:00
|
|
|
{
|
1999-11-23 07:39:29 +03:00
|
|
|
if (loop > startIndex+1)
|
1999-04-27 06:28:12 +04:00
|
|
|
{
|
1999-11-23 07:39:29 +03:00
|
|
|
NS_QuickSort((void *)&flatArray[startIndex], loop-startIndex,
|
|
|
|
sizeof(nsIContent *), inplaceSortCallback, (void *)sortInfo);
|
|
|
|
startIndex = loop+1;
|
1999-04-27 06:28:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-11-23 07:39:29 +03:00
|
|
|
if (loop > startIndex+1)
|
1999-04-27 06:28:12 +04:00
|
|
|
{
|
1999-11-23 07:39:29 +03:00
|
|
|
NS_QuickSort((void *)&flatArray[startIndex], loop-startIndex, sizeof(nsIContent *),
|
1999-04-27 06:28:12 +04:00
|
|
|
inplaceSortCallback, (void *)sortInfo);
|
1999-11-23 07:39:29 +03:00
|
|
|
startIndex = loop+1;
|
1999-04-27 06:28:12 +04:00
|
|
|
}
|
1999-11-23 07:39:29 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NS_QuickSort((void *)(&flatArray[currentElement]), numElements, sizeof(nsIContent *),
|
|
|
|
inplaceSortCallback, (void *)sortInfo);
|
|
|
|
}
|
1999-03-06 02:51:41 +03:00
|
|
|
|
1999-11-23 07:39:29 +03:00
|
|
|
numChildren = 0;
|
|
|
|
for (loop=currentElement; loop < currentElement + numElements; loop++)
|
|
|
|
{
|
|
|
|
container->InsertChildAt((nsIContent *)flatArray[loop], numChildren++, PR_FALSE);
|
|
|
|
}
|
1999-03-07 12:44:38 +03:00
|
|
|
|
1999-11-23 07:39:29 +03:00
|
|
|
nsCOMPtr<nsIContent> parentNode;
|
|
|
|
nsAutoString value;
|
|
|
|
// recurse on grandchildren
|
|
|
|
for (loop=currentElement; loop < currentElement + numElements; loop++)
|
|
|
|
{
|
1999-06-16 06:07:56 +04:00
|
|
|
// Bug 6665, part deux. The Big Hack.
|
1999-11-23 07:39:29 +03:00
|
|
|
nsIRDFResource *resource;
|
|
|
|
gXULUtils->GetElementResource(flatArray[loop], &resource);
|
|
|
|
nsrefcnt refcnt;
|
|
|
|
NS_RELEASE2(resource, refcnt);
|
|
|
|
NS_RELEASE(resource);
|
|
|
|
|
|
|
|
parentNode = (nsIContent *)flatArray[loop];
|
|
|
|
NS_RELEASE(flatArray[loop]); // Note: release it here
|
|
|
|
|
|
|
|
// if its a container, find its treechildren node, and sort those
|
|
|
|
if (NS_FAILED(rv = parentNode->GetAttribute(kNameSpaceID_None, kContainerAtom, value)) ||
|
|
|
|
(rv != NS_CONTENT_ATTR_HAS_VALUE) || (!value.EqualsIgnoreCase(trueStr)))
|
|
|
|
continue;
|
|
|
|
if (NS_FAILED(rv = parentNode->ChildCount(numChildren))) continue;
|
|
|
|
|
|
|
|
for (childIndex=0; childIndex<numChildren; childIndex++)
|
1999-06-16 06:07:56 +04:00
|
|
|
{
|
1999-11-23 07:39:29 +03:00
|
|
|
if (NS_FAILED(rv = parentNode->ChildAt(childIndex, *getter_AddRefs(child))))
|
|
|
|
continue;
|
|
|
|
if (NS_FAILED(rv = child->GetNameSpaceID(nameSpaceID))) continue;
|
|
|
|
if (nameSpaceID != kNameSpaceID_XUL) continue;
|
1999-06-16 06:07:56 +04:00
|
|
|
|
1999-11-23 07:39:29 +03:00
|
|
|
nsCOMPtr<nsIAtom> tag;
|
|
|
|
if (NS_FAILED(rv = child->GetTag(*getter_AddRefs(tag)))) continue;
|
|
|
|
if (tag.get() != kTreeChildrenAtom) continue;
|
1999-03-11 15:01:47 +03:00
|
|
|
|
1999-11-23 07:39:29 +03:00
|
|
|
sortInfo->parentContainer = parentNode;
|
|
|
|
SortTreeChildren(child, sortInfo);
|
|
|
|
}
|
1999-03-06 02:51:41 +03:00
|
|
|
}
|
|
|
|
}
|
1999-11-23 07:39:29 +03:00
|
|
|
delete [] flatArray;
|
|
|
|
flatArray = nsnull;
|
|
|
|
|
1999-03-07 12:44:38 +03:00
|
|
|
return(NS_OK);
|
1999-03-06 02:51:41 +03:00
|
|
|
}
|
|
|
|
|
1999-03-07 12:44:38 +03:00
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-11-20 11:46:22 +03:00
|
|
|
XULSortServiceImpl::InsertContainerNode(nsIRDFCompositeDataSource *db, nsIContent *root, nsIContent *trueParent,
|
1999-11-14 14:10:34 +03:00
|
|
|
nsIContent *container, nsIContent *node, PRBool aNotify)
|
1999-03-07 12:44:38 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
1999-11-11 02:50:34 +03:00
|
|
|
nsAutoString sortResource, sortDirection, sortResource2;
|
1999-03-07 12:44:38 +03:00
|
|
|
_sortStruct sortInfo;
|
|
|
|
|
1999-11-14 14:10:34 +03:00
|
|
|
// get composite db for tree
|
1999-03-19 04:52:18 +03:00
|
|
|
sortInfo.rdfService = gRDFService;
|
1999-03-07 12:44:38 +03:00
|
|
|
sortInfo.db = db;
|
1999-09-07 11:27:49 +04:00
|
|
|
sortInfo.resCache = nsnull;
|
1999-09-08 07:04:45 +04:00
|
|
|
sortInfo.mInner = nsnull;
|
1999-11-14 14:10:34 +03:00
|
|
|
sortInfo.colIndex = -1;
|
1999-11-20 11:46:22 +03:00
|
|
|
sortInfo.parentContainer = trueParent;
|
1999-03-11 15:01:47 +03:00
|
|
|
sortInfo.kTreeCellAtom = kTreeCellAtom;
|
|
|
|
sortInfo.kNameSpaceID_XUL = kNameSpaceID_XUL;
|
1999-11-14 14:10:34 +03:00
|
|
|
sortInfo.sortProperty = nsnull;
|
|
|
|
sortInfo.sortProperty2 = nsnull;
|
1999-11-23 04:02:55 +03:00
|
|
|
sortInfo.inbetweenSeparatorSort = PR_FALSE;
|
1999-03-11 15:01:47 +03:00
|
|
|
|
1999-11-14 14:10:34 +03:00
|
|
|
PRBool sortInfoAvailable = PR_FALSE;
|
1999-11-11 02:50:34 +03:00
|
|
|
|
1999-11-14 14:10:34 +03:00
|
|
|
if (IsTreeElement(root) == PR_TRUE)
|
1999-03-11 15:01:47 +03:00
|
|
|
{
|
1999-11-14 14:10:34 +03:00
|
|
|
// tree, so look for treecol node(s) which provide sorting info
|
1999-05-13 12:25:12 +04:00
|
|
|
|
1999-11-23 04:02:55 +03:00
|
|
|
if (NS_SUCCEEDED(rv = GetSortColumnInfo(root, sortResource, sortDirection,
|
|
|
|
sortResource2, sortInfo.inbetweenSeparatorSort)))
|
1999-11-14 14:10:34 +03:00
|
|
|
{
|
|
|
|
sortInfoAvailable = PR_TRUE;
|
|
|
|
}
|
1999-11-20 11:46:22 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
sortDirection = "natural";
|
|
|
|
}
|
1999-03-07 12:44:38 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-11-14 14:10:34 +03:00
|
|
|
// not a tree, so look for sorting info on root node
|
1999-04-21 00:26:13 +04:00
|
|
|
|
1999-11-14 14:10:34 +03:00
|
|
|
if (NS_SUCCEEDED(rv = root->GetAttribute(kNameSpaceID_None, kSortResourceAtom,
|
|
|
|
sortResource)) && (rv == NS_CONTENT_ATTR_HAS_VALUE))
|
1999-04-21 00:26:13 +04:00
|
|
|
{
|
1999-11-14 14:10:34 +03:00
|
|
|
if (NS_SUCCEEDED(rv = root->GetAttribute(kNameSpaceID_None, kSortDirectionAtom,
|
|
|
|
sortDirection)) && (rv == NS_CONTENT_ATTR_HAS_VALUE))
|
|
|
|
{
|
|
|
|
sortInfoAvailable = PR_TRUE;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv = root->GetAttribute(kNameSpaceID_None, kSortResource2Atom,
|
|
|
|
sortResource2)) || (rv != NS_CONTENT_ATTR_HAS_VALUE))
|
|
|
|
{
|
|
|
|
sortResource2.Truncate();
|
|
|
|
}
|
|
|
|
}
|
1999-04-21 00:26:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-16 08:34:13 +03:00
|
|
|
if (sortInfoAvailable)
|
1999-04-21 00:26:13 +04:00
|
|
|
{
|
1999-11-17 05:03:46 +03:00
|
|
|
rv = gRDFService->GetUnicodeResource(sortResource.GetUnicode(), getter_AddRefs(sortInfo.sortProperty));
|
1999-09-03 01:34:57 +04:00
|
|
|
if (NS_FAILED(rv)) return(rv);
|
1999-11-16 08:34:13 +03:00
|
|
|
if (sortResource2.Length() > 0)
|
|
|
|
{
|
1999-11-17 05:03:46 +03:00
|
|
|
rv = gRDFService->GetUnicodeResource(sortResource2.GetUnicode(), getter_AddRefs(sortInfo.sortProperty2));
|
1999-11-16 08:34:13 +03:00
|
|
|
if (NS_FAILED(rv)) return(rv);
|
|
|
|
}
|
1999-07-20 07:13:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// set up sort order info
|
|
|
|
sortInfo.naturalOrderSort = PR_FALSE;
|
|
|
|
sortInfo.descendingSort = PR_FALSE;
|
|
|
|
if (sortDirection.EqualsIgnoreCase("descending"))
|
|
|
|
{
|
|
|
|
sortInfo.descendingSort = PR_TRUE;
|
|
|
|
}
|
1999-09-03 01:34:57 +04:00
|
|
|
else if (!sortDirection.EqualsIgnoreCase("ascending"))
|
1999-07-20 07:13:15 +04:00
|
|
|
{
|
|
|
|
sortInfo.naturalOrderSort = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
1999-07-20 09:20:32 +04:00
|
|
|
PRBool isContainerRDFSeq = PR_FALSE;
|
1999-11-20 11:46:22 +03:00
|
|
|
|
1999-07-20 13:04:24 +04:00
|
|
|
if ((sortInfo.db) && (sortInfo.naturalOrderSort == PR_TRUE))
|
1999-07-20 07:13:15 +04:00
|
|
|
{
|
1999-07-20 13:04:24 +04:00
|
|
|
// walk up the content model to find the REAL
|
|
|
|
// parent container to determine if its a RDF_Seq
|
1999-07-20 21:32:43 +04:00
|
|
|
nsCOMPtr<nsIContent> parent = do_QueryInterface(container, &rv);
|
|
|
|
nsCOMPtr<nsIContent> aContent;
|
1999-09-03 01:34:57 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc;
|
|
|
|
if (NS_SUCCEEDED(rv) && parent) {
|
|
|
|
rv = parent->GetDocument(*getter_AddRefs(doc));
|
|
|
|
|
|
|
|
if (! doc)
|
|
|
|
parent = nsnull;
|
|
|
|
}
|
|
|
|
|
1999-07-20 21:32:43 +04:00
|
|
|
while(NS_SUCCEEDED(rv) && parent)
|
1999-04-21 00:26:13 +04:00
|
|
|
{
|
1999-11-14 14:10:34 +03:00
|
|
|
nsAutoString id;
|
1999-09-03 01:34:57 +04:00
|
|
|
if (NS_SUCCEEDED(rv = parent->GetAttribute(kNameSpaceID_None, kIdAtom, id))
|
1999-07-20 13:04:24 +04:00
|
|
|
&& (rv == NS_CONTENT_ATTR_HAS_VALUE))
|
1999-07-20 07:13:15 +04:00
|
|
|
{
|
1999-09-03 01:34:57 +04:00
|
|
|
nsCOMPtr<nsIRDFResource> containerRes;
|
1999-09-07 06:51:13 +04:00
|
|
|
rv = gXULUtils->MakeElementResource(doc, id, getter_AddRefs(containerRes));
|
1999-09-03 01:34:57 +04:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = sortInfo.db->HasAssertion(containerRes,
|
|
|
|
kRDF_instanceOf,
|
|
|
|
kRDF_Seq,
|
|
|
|
PR_TRUE,
|
|
|
|
&isContainerRDFSeq);
|
1999-07-20 13:04:24 +04:00
|
|
|
}
|
|
|
|
break;
|
1999-07-20 07:13:15 +04:00
|
|
|
}
|
1999-07-20 21:32:43 +04:00
|
|
|
aContent = do_QueryInterface(parent, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = aContent->GetParent(*getter_AddRefs(parent));
|
1999-07-20 09:20:32 +04:00
|
|
|
}
|
|
|
|
}
|
1999-04-21 02:14:38 +04:00
|
|
|
|
1999-07-20 09:20:32 +04:00
|
|
|
PRBool childAdded = PR_FALSE;
|
|
|
|
|
|
|
|
if ((sortInfo.naturalOrderSort == PR_FALSE) ||
|
|
|
|
((sortInfo.naturalOrderSort == PR_TRUE) &&
|
|
|
|
(isContainerRDFSeq == PR_TRUE)))
|
|
|
|
{
|
|
|
|
// figure out where to insert the node when a sort order is being imposed
|
|
|
|
// using a smart binary comparison
|
|
|
|
PRInt32 numChildren = 0;
|
|
|
|
if (NS_FAILED(rv = container->ChildCount(numChildren))) return(rv);
|
|
|
|
if (numChildren > 0)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> child;
|
|
|
|
PRInt32 last = -1, current, direction = 0, delta = numChildren;
|
|
|
|
while(PR_TRUE)
|
1999-07-01 05:55:09 +04:00
|
|
|
{
|
1999-07-20 09:20:32 +04:00
|
|
|
delta = delta / 2;
|
|
|
|
|
|
|
|
if (last == -1)
|
|
|
|
{
|
|
|
|
current = delta;
|
|
|
|
}
|
|
|
|
else if (direction > 0)
|
1999-07-01 05:55:09 +04:00
|
|
|
{
|
1999-07-20 09:20:32 +04:00
|
|
|
if (delta == 0) delta = 1;
|
|
|
|
current = last + delta;
|
1999-07-20 07:13:15 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-07-20 09:20:32 +04:00
|
|
|
if (delta == 0) delta = 1;
|
|
|
|
current = last - delta;
|
1999-07-01 05:55:09 +04:00
|
|
|
}
|
1999-07-20 09:20:32 +04:00
|
|
|
|
|
|
|
if (current != last)
|
|
|
|
{
|
|
|
|
container->ChildAt(current, *getter_AddRefs(child));
|
|
|
|
nsIContent *theChild = child.get();
|
|
|
|
direction = inplaceSortCallback(&node, &theChild, &sortInfo);
|
|
|
|
}
|
|
|
|
if ( (direction == 0) ||
|
|
|
|
((current == last + 1) && (direction < 0)) ||
|
|
|
|
((current == last - 1) && (direction > 0)) ||
|
|
|
|
((current == 0) && (direction < 0)) ||
|
|
|
|
((current >= numChildren - 1) && (direction > 0)) )
|
|
|
|
{
|
|
|
|
if (current >= numChildren)
|
|
|
|
{
|
1999-07-28 09:21:22 +04:00
|
|
|
container->AppendChildTo(node, aNotify);
|
1999-07-20 09:20:32 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-11-20 11:46:22 +03:00
|
|
|
PRInt32 thePos = ((direction > 0) ? current + 1: (current >= 0) ? current : 0);
|
|
|
|
container->InsertChildAt(node, thePos, aNotify);
|
1999-07-20 09:20:32 +04:00
|
|
|
}
|
|
|
|
childAdded = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
last = current;
|
1999-07-01 05:55:09 +04:00
|
|
|
}
|
1999-07-20 07:13:15 +04:00
|
|
|
}
|
1999-07-20 09:20:32 +04:00
|
|
|
}
|
1999-07-20 07:13:15 +04:00
|
|
|
|
1999-04-21 02:14:38 +04:00
|
|
|
if (childAdded == PR_FALSE)
|
|
|
|
{
|
1999-07-28 09:21:22 +04:00
|
|
|
container->AppendChildTo(node, aNotify);
|
1999-03-07 12:44:38 +03:00
|
|
|
}
|
|
|
|
return(NS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-08-27 10:54:08 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
XULSortServiceImpl::Sort(nsIDOMNode* node, const char *sortResource, const char *sortDirection)
|
|
|
|
{
|
|
|
|
nsAutoString sortRes(sortResource), sortDir(sortDirection);
|
|
|
|
nsresult rv = DoSort(node, sortResource, sortDirection);
|
|
|
|
return(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-08-30 14:05:40 +04:00
|
|
|
nsresult
|
1999-03-07 12:44:38 +03:00
|
|
|
XULSortServiceImpl::DoSort(nsIDOMNode* node, const nsString& sortResource,
|
|
|
|
const nsString& sortDirection)
|
1999-03-06 02:51:41 +03:00
|
|
|
{
|
1999-11-23 04:02:55 +03:00
|
|
|
PRInt32 treeBodyIndex;
|
1999-03-06 02:51:41 +03:00
|
|
|
nsresult rv;
|
1999-03-10 10:07:10 +03:00
|
|
|
_sortStruct sortInfo;
|
1999-03-06 02:51:41 +03:00
|
|
|
|
1999-03-11 15:01:47 +03:00
|
|
|
// get tree node
|
1999-04-28 11:03:11 +04:00
|
|
|
nsCOMPtr<nsIContent> contentNode = do_QueryInterface(node);
|
|
|
|
if (!contentNode) return(NS_ERROR_FAILURE);
|
|
|
|
nsCOMPtr<nsIContent> treeNode;
|
1999-11-14 14:10:34 +03:00
|
|
|
if (NS_FAILED(rv = FindTreeElement(nsnull, contentNode, getter_AddRefs(treeNode))))
|
|
|
|
return(rv);
|
1999-11-23 04:02:55 +03:00
|
|
|
nsCOMPtr<nsIDOMXULElement> domXulTree = do_QueryInterface(treeNode);
|
|
|
|
if (!domXulTree) return(NS_ERROR_FAILURE);
|
1999-03-10 10:07:10 +03:00
|
|
|
|
1999-03-11 15:01:47 +03:00
|
|
|
// get composite db for tree
|
1999-03-19 04:52:18 +03:00
|
|
|
sortInfo.rdfService = gRDFService;
|
1999-03-10 10:07:10 +03:00
|
|
|
sortInfo.db = nsnull;
|
1999-09-07 11:27:49 +04:00
|
|
|
sortInfo.resCache = nsnull;
|
1999-09-08 07:04:45 +04:00
|
|
|
sortInfo.mInner = nsnull;
|
1999-11-23 04:02:55 +03:00
|
|
|
sortInfo.parentContainer = treeNode;
|
|
|
|
sortInfo.inbetweenSeparatorSort = PR_FALSE;
|
|
|
|
|
|
|
|
// remove any sort hints on tree root node
|
|
|
|
treeNode->UnsetAttribute(kNameSpaceID_None, kSortActiveAtom, PR_FALSE);
|
|
|
|
treeNode->UnsetAttribute(kNameSpaceID_None, kSortDirectionAtom, PR_FALSE);
|
|
|
|
treeNode->UnsetAttribute(kNameSpaceID_None, kSortSeparatorsAtom, PR_FALSE);
|
|
|
|
treeNode->UnsetAttribute(kNameSpaceID_RDF, kResourceAtom, PR_FALSE);
|
|
|
|
treeNode->UnsetAttribute(kNameSpaceID_RDF, kResource2Atom, PR_FALSE);
|
1999-09-07 11:27:49 +04:00
|
|
|
|
1999-11-11 02:50:34 +03:00
|
|
|
nsCOMPtr<nsIRDFCompositeDataSource> cds;
|
1999-04-28 11:03:11 +04:00
|
|
|
if (NS_SUCCEEDED(rv = domXulTree->GetDatabase(getter_AddRefs(cds))))
|
1999-03-11 15:01:47 +03:00
|
|
|
{
|
1999-04-28 11:03:11 +04:00
|
|
|
sortInfo.db = cds;
|
1999-03-11 15:01:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sortInfo.kTreeCellAtom = kTreeCellAtom;
|
|
|
|
sortInfo.kNameSpaceID_XUL = kNameSpaceID_XUL;
|
|
|
|
|
1999-11-17 05:03:46 +03:00
|
|
|
if (NS_FAILED(rv = gRDFService->GetUnicodeResource(sortResource.GetUnicode(),
|
1999-11-14 14:10:34 +03:00
|
|
|
getter_AddRefs(sortInfo.sortProperty))))
|
1999-09-07 11:27:49 +04:00
|
|
|
return(rv);
|
1999-11-23 04:02:55 +03:00
|
|
|
|
1999-03-11 15:01:47 +03:00
|
|
|
// determine new sort resource and direction to use
|
1999-03-10 12:41:07 +03:00
|
|
|
if (sortDirection.EqualsIgnoreCase("natural"))
|
1999-03-10 10:07:10 +03:00
|
|
|
{
|
|
|
|
sortInfo.naturalOrderSort = PR_TRUE;
|
|
|
|
sortInfo.descendingSort = PR_FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-03-11 15:01:47 +03:00
|
|
|
sortInfo.naturalOrderSort = PR_FALSE;
|
1999-03-10 12:41:07 +03:00
|
|
|
if (sortDirection.EqualsIgnoreCase("ascending")) sortInfo.descendingSort = PR_FALSE;
|
|
|
|
else if (sortDirection.EqualsIgnoreCase("descending")) sortInfo.descendingSort = PR_TRUE;
|
1999-03-10 10:07:10 +03:00
|
|
|
}
|
|
|
|
|
1999-03-18 23:46:02 +03:00
|
|
|
// get index of sort column, find tree body, and sort. The sort
|
1999-05-13 12:25:12 +04:00
|
|
|
// _won't_ send any notifications, so we won't trigger any reflows...
|
1999-11-23 04:51:46 +03:00
|
|
|
nsAutoString sortResource2;
|
|
|
|
PRBool found;
|
|
|
|
if (NS_FAILED(rv = GetSortColumnIndex(treeNode, sortResource, sortDirection, sortResource2,
|
|
|
|
sortInfo.inbetweenSeparatorSort, sortInfo.colIndex, found))) return(rv);
|
|
|
|
SetSortHints(treeNode, sortResource, sortDirection, sortResource2, sortInfo.inbetweenSeparatorSort, found);
|
|
|
|
|
1999-04-28 11:03:11 +04:00
|
|
|
nsCOMPtr<nsIContent> treeBody;
|
1999-06-25 10:47:28 +04:00
|
|
|
if (NS_FAILED(rv = FindTreeChildrenElement(treeNode, getter_AddRefs(treeBody)))) return(rv);
|
1999-11-23 04:02:55 +03:00
|
|
|
if (NS_SUCCEEDED(rv = SortTreeChildren(treeBody, &sortInfo)))
|
1999-03-06 03:11:14 +03:00
|
|
|
{
|
|
|
|
}
|
1999-03-06 02:51:41 +03:00
|
|
|
|
1999-05-13 12:25:12 +04:00
|
|
|
// Now remove the treebody and re-insert it to force the frames to be rebuilt.
|
1999-04-28 11:03:11 +04:00
|
|
|
nsCOMPtr<nsIContent> treeParent;
|
|
|
|
if (NS_FAILED(rv = treeBody->GetParent(*getter_AddRefs(treeParent)))) return(rv);
|
1999-03-18 23:46:02 +03:00
|
|
|
if (NS_FAILED(rv = treeParent->IndexOf(treeBody, treeBodyIndex))) return(rv);
|
|
|
|
if (NS_FAILED(rv = treeParent->RemoveChildAt(treeBodyIndex, PR_TRUE))) return(rv);
|
|
|
|
|
1999-03-06 03:11:14 +03:00
|
|
|
if (NS_FAILED(rv = treeParent->AppendChildTo(treeBody, PR_TRUE))) return(rv);
|
1999-03-06 02:51:41 +03:00
|
|
|
return(NS_OK);
|
|
|
|
}
|
|
|
|
|
1999-03-07 12:44:38 +03:00
|
|
|
|
|
|
|
|
1999-08-30 14:28:37 +04:00
|
|
|
nsresult
|
1999-03-06 02:51:41 +03:00
|
|
|
NS_NewXULSortService(nsIXULSortService** mgr)
|
|
|
|
{
|
1999-08-30 14:05:40 +04:00
|
|
|
XULSortServiceImpl *sortService = new XULSortServiceImpl();
|
|
|
|
if (!sortService)
|
|
|
|
return(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
*mgr = sortService;
|
|
|
|
NS_ADDREF(*mgr);
|
|
|
|
return(NS_OK);
|
1999-03-06 02:51:41 +03:00
|
|
|
}
|