diff --git a/mozilla/content/base/src/nsGkAtomList.h b/mozilla/content/base/src/nsGkAtomList.h index ea6fc17e72e..9ee8c1aa806 100755 --- a/mozilla/content/base/src/nsGkAtomList.h +++ b/mozilla/content/base/src/nsGkAtomList.h @@ -281,6 +281,7 @@ GK_ATOM(DOMNodeInsertedIntoDocument, "DOMNodeInsertedInfoDocument") GK_ATOM(DOMNodeRemoved, "DOMNodeRemoved") GK_ATOM(DOMNodeRemovedFromDocument, "DOMNodeRemovedFromDocument") GK_ATOM(DOMSubtreeModified, "DOMSubtreeModified") +GK_ATOM(double_, "double") GK_ATOM(drag, "drag") GK_ATOM(dragdrop, "dragdrop") GK_ATOM(dragend, "dragend") @@ -422,6 +423,9 @@ GK_ATOM(ins, "ins") GK_ATOM(insertafter, "insertafter") GK_ATOM(insertbefore, "insertbefore") GK_ATOM(instanceOf, "instanceOf") +GK_ATOM(int32, "int32") +GK_ATOM(int64, "int64") +GK_ATOM(integer, "integer") GK_ATOM(intersection, "intersection") GK_ATOM(iscontainer, "iscontainer") GK_ATOM(isempty, "isempty") @@ -539,6 +543,7 @@ GK_ATOM(noshade, "noshade") GK_ATOM(_not, "not") GK_ATOM(nowrap, "nowrap") GK_ATOM(number, "number") +GK_ATOM(null, "null") GK_ATOM(object, "object") GK_ATOM(objectType, "object-type") GK_ATOM(observer, "observer") diff --git a/mozilla/content/xul/templates/src/Makefile.in b/mozilla/content/xul/templates/src/Makefile.in index 34b85e3911b..994ae98d44f 100644 --- a/mozilla/content/xul/templates/src/Makefile.in +++ b/mozilla/content/xul/templates/src/Makefile.in @@ -64,6 +64,7 @@ REQUIRES = xpcom \ unicharutil \ xuldoc \ webshell \ + storage \ $(NULL) CPPSRCS = \ @@ -91,6 +92,8 @@ CPPSRCS = \ nsXMLBinding.cpp \ nsXULTemplateQueryProcessorXML.cpp \ nsXULTemplateResultXML.cpp \ + nsXULTemplateQueryProcessorStorage.cpp \ + nsXULTemplateResultStorage.cpp \ $(NULL) # we don't want the shared lib, but we want to force the creation of a static lib. diff --git a/mozilla/content/xul/templates/src/nsXULTemplateBuilder.cpp b/mozilla/content/xul/templates/src/nsXULTemplateBuilder.cpp index 8e877848f75..6072ffbe3e7 100644 --- a/mozilla/content/xul/templates/src/nsXULTemplateBuilder.cpp +++ b/mozilla/content/xul/templates/src/nsXULTemplateBuilder.cpp @@ -104,6 +104,7 @@ #include "nsXULTemplateBuilder.h" #include "nsXULTemplateQueryProcessorRDF.h" #include "nsXULTemplateQueryProcessorXML.h" +#include "nsXULTemplateQueryProcessorStorage.h" //---------------------------------------------------------------------- @@ -1161,6 +1162,10 @@ nsXULTemplateBuilder::LoadDataSources(nsIDocument* aDocument, mQueryProcessor = new nsXULTemplateQueryProcessorXML(); NS_ENSURE_TRUE(mQueryProcessor, NS_ERROR_OUT_OF_MEMORY); } + else if (querytype.EqualsLiteral("storage")) { + mQueryProcessor = new nsXULTemplateQueryProcessorStorage(); + NS_ENSURE_TRUE(mQueryProcessor, NS_ERROR_OUT_OF_MEMORY); + } else { nsCAutoString cid(NS_QUERY_PROCESSOR_CONTRACTID_PREFIX); AppendUTF16toUTF8(querytype, cid); diff --git a/mozilla/content/xul/templates/src/nsXULTemplateQueryProcessorStorage.cpp b/mozilla/content/xul/templates/src/nsXULTemplateQueryProcessorStorage.cpp new file mode 100644 index 00000000000..529ff141c50 --- /dev/null +++ b/mozilla/content/xul/templates/src/nsXULTemplateQueryProcessorStorage.cpp @@ -0,0 +1,501 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is Neil Deakin + * Portions created by the Initial Developer are Copyright (C) 2005 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Laurent Jouanneau + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsIDOMNodeList.h" +#include "nsUnicharUtils.h" + +#include "nsArrayUtils.h" +#include "nsIVariant.h" +#include "nsAppDirectoryServiceDefs.h" + +#include "nsIURI.h" +#include "nsIIOService.h" +#include "nsIFileChannel.h" +#include "nsIFile.h" +#include "nsGkAtoms.h" +#include "nsContentUtils.h" + +#include "nsXULTemplateBuilder.h" +#include "nsXULTemplateResultStorage.h" + +#include "mozIStorageService.h" + +//---------------------------------------------------------------------- +// +// nsXULTemplateResultSetStorage +// + +NS_IMPL_ISUPPORTS1(nsXULTemplateResultSetStorage, nsISimpleEnumerator) + + +nsXULTemplateResultSetStorage::nsXULTemplateResultSetStorage(mozIStorageStatement* aStatement) + : mStatement(aStatement) +{ + PRUint32 count; + nsresult rv = aStatement->GetColumnCount(&count); + if (NS_FAILED(rv)) { + mStatement = nsnull; + return; + } + for (PRUint32 c = 0; c < count; c++) { + nsCAutoString name; + rv = aStatement->GetColumnName(c, name); + if (NS_SUCCEEDED(rv)) { + nsCOMPtr columnName = do_GetAtom(NS_LITERAL_CSTRING("?") + name); + mColumnNames.AppendObject(columnName); + } + } +} + +NS_IMETHODIMP +nsXULTemplateResultSetStorage::HasMoreElements(PRBool *aResult) +{ + if (!mStatement) { + *aResult = PR_FALSE; + return NS_OK; + } + + nsresult rv = mStatement->ExecuteStep(aResult); + NS_ENSURE_SUCCESS(rv, rv); + // Because the nsXULTemplateResultSetStorage is owned by many nsXULTemplateResultStorage objects, + // it could live longer than it needed to get results. + // So we destroy the statement to free resources when all results are fetched + if (*aResult == PR_FALSE) { + mStatement = nsnull; + } + return NS_OK; +} + +NS_IMETHODIMP +nsXULTemplateResultSetStorage::GetNext(nsISupports **aResult) +{ + nsXULTemplateResultStorage* result = + new nsXULTemplateResultStorage(this); + + if (!result) + return NS_ERROR_OUT_OF_MEMORY; + + *aResult = result; + NS_ADDREF(result); + return NS_OK; +} + + +PRInt32 +nsXULTemplateResultSetStorage::GetColumnIndex(nsIAtom* aColumnName) +{ + PRInt32 count = mColumnNames.Count(); + for (PRInt32 c = 0; c < count; c++) { + if (mColumnNames[c] == aColumnName) + return c; + } + + return -1; +} + +void +nsXULTemplateResultSetStorage::FillColumnValues(nsCOMArray& aArray) +{ + if (!mStatement) + return; + + PRInt32 count = mColumnNames.Count(); + + for (PRInt32 c = 0; c < count; c++) { + nsCOMPtr value = do_CreateInstance("@mozilla.org/variant;1"); + + PRInt32 type; + mStatement->GetTypeOfIndex(c, &type); + + if (type == mStatement->VALUE_TYPE_INTEGER) { + PRInt32 val = mStatement->AsInt32(c); + value->SetAsInt32(val); + } + else if (type == mStatement->VALUE_TYPE_FLOAT) { + double val = mStatement->AsDouble(c); + value->SetAsDouble(val); + } + else { + nsAutoString val; + nsresult rv = mStatement->GetString(c, val); + if (NS_FAILED(rv)) + value->SetAsAString(EmptyString()); + else + value->SetAsAString(val); + } + aArray.AppendObject(value); + } +} + + + +//---------------------------------------------------------------------- +// +// nsXULTemplateQueryProcessorStorage +// + +NS_IMPL_ISUPPORTS1(nsXULTemplateQueryProcessorStorage, + nsIXULTemplateQueryProcessor) + + +nsXULTemplateQueryProcessorStorage::nsXULTemplateQueryProcessorStorage() + : mGenerationStarted(PR_FALSE) +{ +} + +NS_IMETHODIMP +nsXULTemplateQueryProcessorStorage::GetDatasource(nsIArray* aDataSources, + nsIDOMNode* aRootNode, + PRBool aIsTrusted, + nsIXULTemplateBuilder* aBuilder, + PRBool* aShouldDelayBuilding, + nsISupports** aReturn) +{ + *aReturn = nsnull; + *aShouldDelayBuilding = PR_FALSE; + + if (!aIsTrusted) { + return NS_OK; + } + + PRUint32 length; + nsresult rv = aDataSources->GetLength(&length); + NS_ENSURE_SUCCESS(rv, rv); + + if (length == 0) { + return NS_OK; + } + + // We get only the first uri. This query processor supports + // only one database at a time. + nsCOMPtr uri; + uri = do_QueryElementAt(aDataSources, 0); + + if (!uri) { + // No uri in the list of datasources + return NS_OK; + } + + nsCOMPtr storage = + do_GetService("@mozilla.org/storage/service;1", &rv); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr databaseFile; + nsCAutoString scheme; + rv = uri->GetScheme(scheme); + NS_ENSURE_SUCCESS(rv, rv); + + if (scheme.EqualsLiteral("profile")) { + + nsCAutoString path; + rv = uri->GetPath(path); + NS_ENSURE_SUCCESS(rv, rv); + + if (path.IsEmpty()) { + return NS_ERROR_FAILURE; + } + + rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, + getter_AddRefs(databaseFile)); + NS_ENSURE_SUCCESS(rv, rv); + + rv = databaseFile->AppendNative(path); + NS_ENSURE_SUCCESS(rv, rv); + } + else { + nsCOMPtr channel; + nsCOMPtr ioservice = + do_GetService("@mozilla.org/network/io-service;1", &rv); + NS_ENSURE_SUCCESS(rv, rv); + + rv = ioservice->NewChannelFromURI(uri, getter_AddRefs(channel)); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr fileChannel = do_QueryInterface(channel, &rv); + NS_ENSURE_SUCCESS(rv, rv); // if it fails, not a file url + + nsCOMPtr file; + rv = fileChannel->GetFile(getter_AddRefs(databaseFile)); + NS_ENSURE_SUCCESS(rv, rv); + } + + // ok now we have an URI of a sqlite file + nsCOMPtr connection; + rv = storage->OpenDatabase(databaseFile, getter_AddRefs(connection)); + NS_ENSURE_SUCCESS(rv, rv); + + NS_ADDREF(*aReturn = connection); + return NS_OK; +} + + + +NS_IMETHODIMP +nsXULTemplateQueryProcessorStorage::InitializeForBuilding(nsISupports* aDatasource, + nsIXULTemplateBuilder* aBuilder, + nsIDOMNode* aRootNode) +{ + NS_ENSURE_STATE(!mGenerationStarted); + + mStorageConnection = do_QueryInterface(aDatasource); + if (!mStorageConnection) + return NS_ERROR_INVALID_ARG; + + return NS_OK; +} + +NS_IMETHODIMP +nsXULTemplateQueryProcessorStorage::Done() +{ + mGenerationStarted = PR_FALSE; + return NS_OK; +} + +NS_IMETHODIMP +nsXULTemplateQueryProcessorStorage::CompileQuery(nsIXULTemplateBuilder* aBuilder, + nsIDOMNode* aQueryNode, + nsIAtom* aRefVariable, + nsIAtom* aMemberVariable, + nsISupports** aReturn) +{ + nsCOMPtr childNodes; + aQueryNode->GetChildNodes(getter_AddRefs(childNodes)); + + PRUint32 length; + childNodes->GetLength(&length); + + nsCOMPtr statement; + nsCOMPtr queryContent = do_QueryInterface(aQueryNode); + nsAutoString sqlQuery; + + // Let's get all text nodes (which should be the query) + nsContentUtils::GetNodeTextContent(queryContent, PR_FALSE, sqlQuery); + + nsresult rv = mStorageConnection->CreateStatement(NS_ConvertUTF16toUTF8(sqlQuery), + getter_AddRefs(statement)); + NS_ENSURE_SUCCESS(rv, rv); + + PRUint32 parameterCount = 0; + PRUint32 count = queryContent->GetChildCount(); + + for (PRUint32 i = 0; i < count; ++i) { + nsIContent *child = queryContent->GetChildAt(i); + + if (child->NodeInfo()->Equals(nsGkAtoms::param, kNameSpaceID_XUL)) { + nsAutoString value; + nsContentUtils::GetNodeTextContent(child, PR_FALSE, value); + + PRUint32 index = parameterCount; + nsAutoString name, indexValue; + + if (child->GetAttr(kNameSpaceID_None, nsGkAtoms::name, name)) { + nsAutoString fullName; + fullName.AssignLiteral(":"); + fullName.Append(name); + rv = statement->GetParameterIndex(NS_ConvertUTF16toUTF8(fullName) , &index); + NS_ENSURE_SUCCESS(rv, rv); + parameterCount++; + } + else if (child->GetAttr(kNameSpaceID_None, nsGkAtoms::index, indexValue)) { + PR_sscanf(NS_ConvertUTF16toUTF8(indexValue).get(),"%d",&index); + if (index > 0) + index--; + } + else { + parameterCount++; + } + + static nsIContent::AttrValuesArray sTypeValues[] = + { &nsGkAtoms::int32, &nsGkAtoms::integer, &nsGkAtoms::int64, + &nsGkAtoms::null, &nsGkAtoms::double_, &nsGkAtoms::string, nsnull }; + + PRInt32 typeError, typeValue = child->FindAttrValueIn(kNameSpaceID_None, nsGkAtoms::type, + sTypeValues, eCaseMatters); + rv = NS_ERROR_ILLEGAL_VALUE; + PRInt32 valInt32 = 0; + PRInt64 valInt64 = 0; + PRFloat64 valFloat = 0; + + switch (typeValue) { + case 0: + case 1: + typeError = PR_sscanf(NS_ConvertUTF16toUTF8(value).get(),"%d",&valInt32); + if (typeError > 0) + rv = statement->BindInt32Parameter(index, valInt32); + break; + case 2: + typeError = PR_sscanf(NS_ConvertUTF16toUTF8(value).get(),"%lld",&valInt64); + if (typeError > 0) + rv = statement->BindInt64Parameter(index, valInt64); + break; + case 3: + rv = statement->BindNullParameter(index); + break; + case 4: + typeError = PR_sscanf(NS_ConvertUTF16toUTF8(value).get(),"%lf",&valFloat); + if (typeError > 0) + rv = statement->BindDoubleParameter(index, valFloat); + break; + case 5: + case nsIContent::ATTR_MISSING: + rv = statement->BindStringParameter(index, value); + break; + } + NS_ENSURE_SUCCESS(rv, rv); + } + } + + *aReturn = statement; + NS_IF_ADDREF(*aReturn); + + return NS_OK; +} + +NS_IMETHODIMP +nsXULTemplateQueryProcessorStorage::GenerateResults(nsISupports* aDatasource, + nsIXULTemplateResult* aRef, + nsISupports* aQuery, + nsISimpleEnumerator** aResults) +{ + mGenerationStarted = PR_TRUE; + + nsCOMPtr statement = do_QueryInterface(aQuery); + if (!statement) + return NS_ERROR_FAILURE; + + nsXULTemplateResultSetStorage* results = + new nsXULTemplateResultSetStorage(statement); + + if (!results) + return NS_ERROR_OUT_OF_MEMORY; + + *aResults = results; + NS_ADDREF(*aResults); + + return NS_OK; +} + +NS_IMETHODIMP +nsXULTemplateQueryProcessorStorage::AddBinding(nsIDOMNode* aRuleNode, + nsIAtom* aVar, + nsIAtom* aRef, + const nsAString& aExpr) +{ + return NS_OK; +} + +NS_IMETHODIMP +nsXULTemplateQueryProcessorStorage::TranslateRef(nsISupports* aDatasource, + const nsAString& aRefString, + nsIXULTemplateResult** aRef) +{ + nsXULTemplateResultStorage* result = + new nsXULTemplateResultStorage(nsnull); + if (!result) + return NS_ERROR_OUT_OF_MEMORY; + + *aRef = result; + NS_ADDREF(*aRef); + return NS_OK; +} + + +NS_IMETHODIMP +nsXULTemplateQueryProcessorStorage::CompareResults(nsIXULTemplateResult* aLeft, + nsIXULTemplateResult* aRight, + nsIAtom* aVar, + PRInt32* aResult) +{ + *aResult = 0; + + // We're going to see if values are integers or float, to perform + // a suitable comparison + nsCOMPtr leftValue, rightValue; + aLeft->GetBindingObjectFor(aVar, getter_AddRefs(leftValue)); + aRight->GetBindingObjectFor(aVar, getter_AddRefs(rightValue)); + + if (leftValue && rightValue) { + nsCOMPtr vLeftValue = do_QueryInterface(leftValue); + nsCOMPtr vRightValue = do_QueryInterface(rightValue); + + if (vLeftValue && vRightValue) { + nsresult rv1, rv2; + PRUint16 vtypeL, vtypeR; + vLeftValue->GetDataType(&vtypeL); + vRightValue->GetDataType(&vtypeR); + + if (vtypeL == vtypeR) { + if (vtypeL == nsIDataType::VTYPE_INT32) { + PRInt32 leftValue, rightValue; + rv1 = vLeftValue->GetAsInt32(&leftValue); + rv2 = vRightValue->GetAsInt32(&rightValue); + if (NS_SUCCEEDED(rv1) && NS_SUCCEEDED(rv2)) { + if (leftValue > rightValue) + *aResult = 1; + else if (leftValue < rightValue) + *aResult = -1; + return NS_OK; + } + } + else if (vtypeL == nsIDataType::VTYPE_DOUBLE) { + double leftValue, rightValue; + rv1 = vLeftValue->GetAsDouble(&leftValue); + rv2 = vRightValue->GetAsDouble(&rightValue); + if (NS_SUCCEEDED(rv1) && NS_SUCCEEDED(rv2)) { + if (leftValue > rightValue) + *aResult = 1; + else if (leftValue < rightValue) + *aResult = -1; + return NS_OK; + } + } + } + } + } + + // Values are not integers or floats, so we just compare them as simple strings + nsAutoString leftVal; + aLeft->GetBindingFor(aVar, leftVal); + + nsAutoString rightVal; + aRight->GetBindingFor(aVar, rightVal); + + *aResult = Compare(nsDependentString(leftVal), + nsDependentString(rightVal), + nsCaseInsensitiveStringComparator()); + return NS_OK; +} diff --git a/mozilla/content/xul/templates/src/nsXULTemplateQueryProcessorStorage.h b/mozilla/content/xul/templates/src/nsXULTemplateQueryProcessorStorage.h new file mode 100644 index 00000000000..09279f2e5ce --- /dev/null +++ b/mozilla/content/xul/templates/src/nsXULTemplateQueryProcessorStorage.h @@ -0,0 +1,96 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is Neil Deakin + * Portions created by the Initial Developer are Copyright (C) 2005 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Laurent Jouanneau + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef nsXULTemplateQueryProcessorStorage_h__ +#define nsXULTemplateQueryProcessorStorage_h__ + +#include "nsIXULTemplateBuilder.h" +#include "nsIXULTemplateQueryProcessor.h" + +#include "nsISimpleEnumerator.h" +#include "nsCOMArray.h" +#include "nsIVariant.h" + +#include "mozIStorageValueArray.h" +#include "mozIStorageStatement.h" +#include "mozIStorageConnection.h" + +class nsXULTemplateQueryProcessorStorage; + +class nsXULTemplateResultSetStorage : public nsISimpleEnumerator +{ +private: + + nsCOMPtr mStatement; + + nsCOMArray mColumnNames; + +public: + + // nsISupports interface + NS_DECL_ISUPPORTS + + // nsISimpleEnumerator interface + NS_DECL_NSISIMPLEENUMERATOR + + nsXULTemplateResultSetStorage(mozIStorageStatement* aStatement); + + PRInt32 GetColumnIndex(nsIAtom* aColumnName); + + void FillColumnValues(nsCOMArray& aArray); + +}; + +class nsXULTemplateQueryProcessorStorage : public nsIXULTemplateQueryProcessor +{ +public: + + nsXULTemplateQueryProcessorStorage(); + + // nsISupports interface + NS_DECL_ISUPPORTS + + // nsIXULTemplateQueryProcessor interface + NS_DECL_NSIXULTEMPLATEQUERYPROCESSOR + +private: + + nsCOMPtr mStorageConnection; + PRBool mGenerationStarted; +}; + +#endif // nsXULTemplateQueryProcessorStorage_h__ diff --git a/mozilla/content/xul/templates/src/nsXULTemplateResultStorage.cpp b/mozilla/content/xul/templates/src/nsXULTemplateResultStorage.cpp new file mode 100644 index 00000000000..9f6f8bcfb57 --- /dev/null +++ b/mozilla/content/xul/templates/src/nsXULTemplateResultStorage.cpp @@ -0,0 +1,151 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is Neil Deakin + * Portions created by the Initial Developer are Copyright (C) 2005 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Laurent Jouanneau + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsIServiceManager.h" +#include "nsRDFCID.h" +#include "nsIRDFService.h" + +#include "nsXULTemplateResultStorage.h" + +static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID); + +NS_IMPL_ISUPPORTS1(nsXULTemplateResultStorage, nsIXULTemplateResult) + +nsXULTemplateResultStorage::nsXULTemplateResultStorage(nsXULTemplateResultSetStorage* aResultSet) +{ + nsCOMPtr rdfService = do_GetService(kRDFServiceCID); + rdfService->GetAnonymousResource(getter_AddRefs(mNode)); + mResultSet = aResultSet; + if (aResultSet) { + mResultSet->FillColumnValues(mValues); + } +} + +NS_IMETHODIMP +nsXULTemplateResultStorage::GetIsContainer(PRBool* aIsContainer) +{ + *aIsContainer = PR_FALSE; + return NS_OK; +} + +NS_IMETHODIMP +nsXULTemplateResultStorage::GetIsEmpty(PRBool* aIsEmpty) +{ + *aIsEmpty = PR_TRUE; + return NS_OK; +} + +NS_IMETHODIMP +nsXULTemplateResultStorage::GetMayProcessChildren(PRBool* aMayProcessChildren) +{ + *aMayProcessChildren = PR_FALSE; + return NS_OK; +} + +NS_IMETHODIMP +nsXULTemplateResultStorage::GetId(nsAString& aId) +{ + const char* uri = nsnull; + mNode->GetValueConst(&uri); + + aId.Assign(NS_ConvertUTF8toUTF16(uri)); + + return NS_OK; +} + +NS_IMETHODIMP +nsXULTemplateResultStorage::GetResource(nsIRDFResource** aResource) +{ + *aResource = mNode; + NS_IF_ADDREF(*aResource); + return NS_OK; +} + +NS_IMETHODIMP +nsXULTemplateResultStorage::GetType(nsAString& aType) +{ + aType.Truncate(); + return NS_OK; +} + + +NS_IMETHODIMP +nsXULTemplateResultStorage::GetBindingFor(nsIAtom* aVar, nsAString& aValue) +{ + aValue.Truncate(); + if (!mResultSet) { + return NS_OK; + } + + PRInt32 idx = mResultSet->GetColumnIndex(aVar); + if (idx < 0) { + return NS_OK; + } + + nsIVariant * value = mValues[idx]; + if (value) { + value->GetAsAString(aValue); + } + return NS_OK; +} + +NS_IMETHODIMP +nsXULTemplateResultStorage::GetBindingObjectFor(nsIAtom* aVar, nsISupports** aValue) +{ + if (mResultSet) { + PRInt32 idx = mResultSet->GetColumnIndex(aVar); + if (idx >= 0) { + *aValue = mValues[idx]; + NS_IF_ADDREF(*aValue); + return NS_OK; + } + } + *aValue = nsnull; + return NS_OK; +} + +NS_IMETHODIMP +nsXULTemplateResultStorage::RuleMatched(nsISupports* aQuery, nsIDOMNode* aRuleNode) +{ + return NS_OK; +} + +NS_IMETHODIMP +nsXULTemplateResultStorage::HasBeenRemoved() +{ + return NS_OK; +} diff --git a/mozilla/content/xul/templates/src/nsXULTemplateResultStorage.h b/mozilla/content/xul/templates/src/nsXULTemplateResultStorage.h new file mode 100644 index 00000000000..6418be968f7 --- /dev/null +++ b/mozilla/content/xul/templates/src/nsXULTemplateResultStorage.h @@ -0,0 +1,67 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla 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/MPL/ + * + * 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. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is Neil Deakin + * Portions created by the Initial Developer are Copyright (C) 2005 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Laurent Jouanneau + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef nsXULTemplateResultStorage_h__ +#define nsXULTemplateResultStorage_h__ + +#include "nsXULTemplateQueryProcessorStorage.h" +#include "nsIRDFResource.h" +#include "nsIXULTemplateResult.h" +#include "nsAutoPtr.h" + +/** + * A single result of a query from mozstorage + */ +class nsXULTemplateResultStorage : public nsIXULTemplateResult +{ +public: + NS_DECL_ISUPPORTS + + NS_DECL_NSIXULTEMPLATERESULT + + nsXULTemplateResultStorage(nsXULTemplateResultSetStorage* aResultSet); + +protected: + + nsRefPtr mResultSet; + + nsCOMArray mValues; + + nsCOMPtr mNode; +}; + +#endif // nsXULTemplateResultStorage_h__