From c38162e8508dd51ec47ebf28945920ac9ace6d0c Mon Sep 17 00:00:00 2001 From: "rjc%netscape.com" Date: Sat, 26 Jun 1999 08:31:16 +0000 Subject: [PATCH] Implement some commands for bookmarks. --- .../bookmarks/src/nsBookmarksService.cpp | 292 ++++++++++++++---- 1 file changed, 237 insertions(+), 55 deletions(-) diff --git a/xpfe/components/bookmarks/src/nsBookmarksService.cpp b/xpfe/components/bookmarks/src/nsBookmarksService.cpp index dd3eb17e3943..a4998cee8a6b 100644 --- a/xpfe/components/bookmarks/src/nsBookmarksService.cpp +++ b/xpfe/components/bookmarks/src/nsBookmarksService.cpp @@ -45,6 +45,8 @@ #include "rdf.h" #include "xp_core.h" +#include "nsEnumeratorUtils.h" + //////////////////////////////////////////////////////////////////////// static NS_DEFINE_CID(kBookmarksServiceCID, NS_BOOKMARKS_SERVICE_CID); @@ -62,27 +64,37 @@ static const char kURINC_PersonalToolbarFolder[] = "NC:PersonalToolbarFolder"; / static const char kPersonalToolbarFolder[] = "Personal Toolbar Folder"; +static const char kBookmarkCommand[] = "http://home.netscape.com/NC-rdf#bookmarkcommand?"; //////////////////////////////////////////////////////////////////////// PRInt32 gRefCnt; -nsIRDFService* gRDF; -nsIRDFContainerUtils* gRDFC; -nsIRDFResource* kNC_Bookmark; -nsIRDFResource* kNC_BookmarkSeparator; -nsIRDFResource* kNC_BookmarkAddDate; -nsIRDFResource* kNC_BookmarksRoot; -nsIRDFResource* kNC_Description; -nsIRDFResource* kNC_Folder; -nsIRDFResource* kNC_IEFavorite; -nsIRDFResource* kNC_IEFavoritesRoot; -nsIRDFResource* kNC_Name; -nsIRDFResource* kNC_PersonalToolbarFolder; -nsIRDFResource* kNC_ShortcutURL; -nsIRDFResource* kNC_URL; -nsIRDFResource* kRDF_type; -nsIRDFResource* kWEB_LastModifiedDate; -nsIRDFResource* kWEB_LastVisitDate; +nsIRDFService *gRDF; +nsIRDFContainerUtils *gRDFC; + +nsIRDFResource *kNC_Bookmark; +nsIRDFResource *kNC_BookmarkSeparator; +nsIRDFResource *kNC_BookmarkAddDate; +nsIRDFResource *kNC_BookmarksRoot; +nsIRDFResource *kNC_Description; +nsIRDFResource *kNC_Folder; +nsIRDFResource *kNC_IEFavorite; +nsIRDFResource *kNC_IEFavoritesRoot; +nsIRDFResource *kNC_Name; +nsIRDFResource *kNC_PersonalToolbarFolder; +nsIRDFResource *kNC_ShortcutURL; +nsIRDFResource *kNC_URL; +nsIRDFResource *kRDF_type; +nsIRDFResource *kWEB_LastModifiedDate; +nsIRDFResource *kWEB_LastVisitDate; + +nsIRDFResource *kNC_BookmarkCommand_NewFolder; +nsIRDFResource *kNC_BookmarkCommand_NewSeparator; +nsIRDFResource *kNC_BookmarkCommand_DeleteBookmark; +nsIRDFResource *kNC_BookmarkCommand_DeleteBookmarkFolder; +nsIRDFResource *kNC_BookmarkCommand_DeleteBookmarkSeparator; + + static nsresult bm_AddRefGlobals() @@ -120,11 +132,18 @@ bm_AddRefGlobals() gRDF->GetResource(RDF_NAMESPACE_URI "type", &kRDF_type); gRDF->GetResource(WEB_NAMESPACE_URI "LastModifiedDate", &kWEB_LastModifiedDate); gRDF->GetResource(WEB_NAMESPACE_URI "LastVisitDate", &kWEB_LastVisitDate); + + gRDF->GetResource(NC_NAMESPACE_URI "bookmarkcommand?newfolder", &kNC_BookmarkCommand_NewFolder); + gRDF->GetResource(NC_NAMESPACE_URI "bookmarkcommand?newseparator", &kNC_BookmarkCommand_NewSeparator); + gRDF->GetResource(NC_NAMESPACE_URI "bookmarkcommand?deletebookmark", &kNC_BookmarkCommand_DeleteBookmark); + gRDF->GetResource(NC_NAMESPACE_URI "bookmarkcommand?deletebookmarkfolder", &kNC_BookmarkCommand_DeleteBookmarkFolder); + gRDF->GetResource(NC_NAMESPACE_URI "bookmarkcommand?deletebookmarkseparator", &kNC_BookmarkCommand_DeleteBookmarkSeparator); } return NS_OK; } + static void bm_ReleaseGlobals() { @@ -151,10 +170,17 @@ bm_ReleaseGlobals() NS_IF_RELEASE(kRDF_type); NS_IF_RELEASE(kWEB_LastModifiedDate); NS_IF_RELEASE(kWEB_LastVisitDate); + + NS_IF_RELEASE(kNC_BookmarkCommand_NewFolder); + NS_IF_RELEASE(kNC_BookmarkCommand_NewSeparator); + NS_IF_RELEASE(kNC_BookmarkCommand_DeleteBookmark); + NS_IF_RELEASE(kNC_BookmarkCommand_DeleteBookmarkFolder); + NS_IF_RELEASE(kNC_BookmarkCommand_DeleteBookmarkSeparator); } } + //////////////////////////////////////////////////////////////////////// /** @@ -225,11 +251,14 @@ public: }; + BookmarkParser::BookmarkParser() { bm_AddRefGlobals(); } + + nsresult BookmarkParser::Init(nsInputFileStream *aStream, nsIRDFDataSource *aDataSource) { @@ -240,11 +269,15 @@ BookmarkParser::Init(nsInputFileStream *aStream, nsIRDFDataSource *aDataSource) return(NS_OK); } + + BookmarkParser::~BookmarkParser() { bm_ReleaseGlobals(); } + + static const char kHREFEquals[] = "HREF=\""; static const char kCloseAnchor[] = ""; @@ -270,6 +303,7 @@ static const char kShortcutURLEquals[] = "SHORTCUTURL=\""; static const char kIDEquals[] = "ID=\""; + nsresult BookmarkParser::Parse(nsIRDFResource* aContainer, nsIRDFResource *nodeType) { @@ -344,6 +378,7 @@ BookmarkParser::Parse(nsIRDFResource* aContainer, nsIRDFResource *nodeType) } + nsresult BookmarkParser::CreateAnonymousResource(nsCOMPtr* aResult) { @@ -358,6 +393,8 @@ BookmarkParser::CreateAnonymousResource(nsCOMPtr* aResult) return gRDF->GetUnicodeResource(uri.GetUnicode(), getter_AddRefs(*aResult)); } + + nsresult BookmarkParser::ParseBookmark(const nsString& aLine, nsCOMPtr& aContainer, @@ -695,6 +732,7 @@ BookmarkParser::ParseBookmarkHeader(const nsString& aLine, } + nsresult BookmarkParser::ParseBookmarkSeparator(const nsString& aLine, nsCOMPtr& aContainer) @@ -739,6 +777,7 @@ BookmarkParser::ParseHeaderEnd(const nsString& aLine) } + nsresult BookmarkParser::ParseAttribute(const nsString& aLine, const char* aAttributeName, @@ -758,6 +797,8 @@ BookmarkParser::ParseAttribute(const nsString& aLine, return NS_OK; } + + nsresult BookmarkParser::AssertTime(nsIRDFResource* aSource, nsIRDFResource* aLabel, @@ -782,9 +823,13 @@ BookmarkParser::AssertTime(nsIRDFResource* aSource, return rv; } + + //////////////////////////////////////////////////////////////////////// // BookmarkDataSourceImpl + + class nsBookmarksService : public nsIBookmarksService, public nsIRDFDataSource, public nsIRDFRemoteDataSource @@ -909,17 +954,22 @@ public: // nsIRDFRemoteDataSource NS_IMETHOD Init(const char* aURI); NS_IMETHOD Refresh(PRBool aBlocking); - NS_IMETHOD Flush(); + NS_IMETHOD Flush(); }; + //////////////////////////////////////////////////////////////////////// + + nsBookmarksService::nsBookmarksService() { NS_INIT_REFCNT(); } + + nsBookmarksService::~nsBookmarksService() { Flush(); @@ -928,6 +978,7 @@ nsBookmarksService::~nsBookmarksService() } + nsresult nsBookmarksService::Init() { @@ -952,6 +1003,7 @@ nsBookmarksService::Init() } + NS_IMETHODIMP NS_NewBookmarksService(nsISupports* aOuter, REFNSIID aIID, void** aResult) { @@ -983,11 +1035,16 @@ NS_NewBookmarksService(nsISupports* aOuter, REFNSIID aIID, void** aResult) } + //////////////////////////////////////////////////////////////////////// + + NS_IMPL_ADDREF(nsBookmarksService); NS_IMPL_RELEASE(nsBookmarksService); + + NS_IMETHODIMP nsBookmarksService::QueryInterface(REFNSIID aIID, void **aResult) { @@ -1015,10 +1072,13 @@ nsBookmarksService::QueryInterface(REFNSIID aIID, void **aResult) return NS_OK; } + + //////////////////////////////////////////////////////////////////////// // nsIBookmarksService + NS_IMETHODIMP nsBookmarksService::AddBookmark(const char *aURI, const PRUnichar *aOptionalTitle) { @@ -1052,6 +1112,8 @@ nsBookmarksService::AddBookmark(const char *aURI, const PRUnichar *aOptionalTitl return NS_OK; } + + NS_IMETHODIMP nsBookmarksService::FindShortcut(const PRUnichar *aUserInput, char **aShortcutURL) { @@ -1095,6 +1157,8 @@ nsBookmarksService::FindShortcut(const PRUnichar *aUserInput, char **aShortcutUR //////////////////////////////////////////////////////////////////////// // nsIRDFDataSource + + NS_IMETHODIMP nsBookmarksService::GetURI(char* *aURI) { @@ -1105,48 +1169,105 @@ nsBookmarksService::GetURI(char* *aURI) return NS_OK; } + + +static PRBool +isBookmarkCommand(nsIRDFResource *r) +{ + PRBool isBookmarkCommandFlag = PR_FALSE; + nsXPIDLCString uri; + + r->GetValue( getter_Copies(uri) ); + if (!strncmp(uri, kBookmarkCommand, sizeof(kBookmarkCommand) - 1)) + { + isBookmarkCommandFlag = PR_TRUE; + } + return(isBookmarkCommandFlag); +} + + + NS_IMETHODIMP nsBookmarksService::GetTarget(nsIRDFResource* aSource, nsIRDFResource* aProperty, PRBool aTruthValue, nsIRDFNode** aTarget) { - nsresult rv; + nsresult rv; - // If they want the URL... - if (aTruthValue && aProperty == kNC_URL) { - // ...and it is in fact a bookmark... - PRBool hasAssertion; - if (NS_SUCCEEDED(mInner->HasAssertion(aSource, kRDF_type, kNC_Bookmark, PR_TRUE, &hasAssertion)) - && hasAssertion) { - - nsXPIDLCString uri; - if (NS_FAILED(rv = aSource->GetValue( getter_Copies(uri) ))) { - NS_ERROR("unable to get source's URI"); - return rv; - } - - nsAutoString ncURI(uri); - if (ncURI.Find("NC:") == 0) + // If they want the URL... + if (aTruthValue && aProperty == kNC_URL) + { + // ...and it is in fact a bookmark... + PRBool hasAssertion; + if (NS_SUCCEEDED(mInner->HasAssertion(aSource, kRDF_type, kNC_Bookmark, PR_TRUE, &hasAssertion)) + && hasAssertion) { - return(NS_RDF_NO_VALUE); + nsXPIDLCString uri; + if (NS_FAILED(rv = aSource->GetValue( getter_Copies(uri) ))) + { + NS_ERROR("unable to get source's URI"); + return rv; + } + + nsAutoString ncURI(uri); + if (ncURI.Find("NC:") == 0) + { + return(NS_RDF_NO_VALUE); + } + + nsIRDFLiteral* literal; + if (NS_FAILED(rv = gRDF->GetLiteral(nsAutoString(uri).GetUnicode(), &literal))) + { + NS_ERROR("unable to construct literal for URL"); + return rv; + } + + *aTarget = (nsIRDFNode*)literal; + return NS_OK; + } + } + else if (aTruthValue && isBookmarkCommand(aSource) && (aProperty == kNC_Name)) + { + nsAutoString name; + if (aSource == kNC_BookmarkCommand_NewFolder) + { + name = "New Folder..."; // XXX localization + } + else if (aSource == kNC_BookmarkCommand_NewSeparator) + { + name = "New Separator..."; // XXX localization + } + else if (aSource == kNC_BookmarkCommand_DeleteBookmark) + { + name = "Delete Bookmark..."; // XXX localization + } + else if (aSource == kNC_BookmarkCommand_DeleteBookmarkFolder) + { + name = "Delete Folder..."; // XXX localization + } + else if (aSource == kNC_BookmarkCommand_DeleteBookmarkSeparator) + { + name = "Delete Separator..."; // XXX localization } - nsIRDFLiteral* literal; - if (NS_FAILED(rv = gRDF->GetLiteral(nsAutoString(uri).GetUnicode(), &literal))) { - NS_ERROR("unable to construct literal for URL"); - return rv; - } + if (name.Length() > 0) + { + nsIRDFLiteral *literal; + rv = gRDF->GetLiteral(name.GetUnicode(), &literal); - *aTarget = (nsIRDFNode*)literal; - return NS_OK; - } - } + rv = literal->QueryInterface(nsIRDFNode::GetIID(), (void**) aTarget); + NS_RELEASE(literal); - return mInner->GetTarget(aSource, aProperty, aTruthValue, aTarget); + return rv; + } + } + + return mInner->GetTarget(aSource, aProperty, aTruthValue, aTarget); } + NS_IMETHODIMP nsBookmarksService::Assert(nsIRDFResource* aSource, nsIRDFResource* aProperty, @@ -1161,6 +1282,8 @@ nsBookmarksService::Assert(nsIRDFResource* aSource, } } + + NS_IMETHODIMP nsBookmarksService::Unassert(nsIRDFResource* aSource, nsIRDFResource* aProperty, @@ -1175,6 +1298,7 @@ nsBookmarksService::Unassert(nsIRDFResource* aSource, } + NS_IMETHODIMP nsBookmarksService::Change(nsIRDFResource* aSource, nsIRDFResource* aProperty, @@ -1190,6 +1314,7 @@ nsBookmarksService::Change(nsIRDFResource* aSource, } + NS_IMETHODIMP nsBookmarksService::Move(nsIRDFResource* aOldSource, nsIRDFResource* aNewSource, @@ -1204,43 +1329,89 @@ nsBookmarksService::Move(nsIRDFResource* aOldSource, } } + + NS_IMETHODIMP nsBookmarksService::GetAllCommands(nsIRDFResource* source, nsIEnumerator/**/** commands) { - NS_NOTYETIMPLEMENTED("write me!"); - return NS_ERROR_NOT_IMPLEMENTED; + NS_NOTYETIMPLEMENTED("write me!"); + return NS_ERROR_NOT_IMPLEMENTED; } + + NS_IMETHODIMP nsBookmarksService::GetAllCmds(nsIRDFResource* source, nsISimpleEnumerator/**/** commands) { - return(NS_NewEmptyEnumerator(commands)); + nsCOMPtr cmdArray; + nsresult rv; + rv = NS_NewISupportsArray(getter_AddRefs(cmdArray)); + if (NS_FAILED(rv)) return(rv); + + // determine type + PRBool isBookmark = PR_FALSE, isBookmarkFolder = PR_FALSE, isBookmarkSeparator = PR_FALSE; + mInner->HasAssertion(source, kRDF_type, kNC_Bookmark, PR_TRUE, &isBookmark); + mInner->HasAssertion(source, kRDF_type, kNC_Folder, PR_TRUE, &isBookmarkFolder); + mInner->HasAssertion(source, kRDF_type, kNC_BookmarkSeparator, PR_TRUE, &isBookmarkSeparator); + + if (isBookmark || isBookmarkFolder || isBookmarkSeparator) + { + cmdArray->AppendElement(kNC_BookmarkCommand_NewFolder); + cmdArray->AppendElement(kNC_BookmarkCommand_NewSeparator); + } + if (isBookmark) + { + cmdArray->AppendElement(kNC_BookmarkCommand_DeleteBookmark); + } + if (isBookmarkFolder) + { + cmdArray->AppendElement(kNC_BookmarkCommand_DeleteBookmarkFolder); + } + if (isBookmarkSeparator) + { + cmdArray->AppendElement(kNC_BookmarkCommand_DeleteBookmarkSeparator); + } + + nsISimpleEnumerator *result = new nsArrayEnumerator(cmdArray); + if (!result) + return(NS_ERROR_OUT_OF_MEMORY); + NS_ADDREF(result); + *commands = result; + return(NS_OK); } + + NS_IMETHODIMP nsBookmarksService::IsCommandEnabled(nsISupportsArray/**/* aSources, nsIRDFResource* aCommand, nsISupportsArray/**/* aArguments, PRBool* aResult) { - NS_NOTYETIMPLEMENTED("write me!"); - return NS_ERROR_NOT_IMPLEMENTED; + NS_NOTYETIMPLEMENTED("write me!"); + return NS_ERROR_NOT_IMPLEMENTED; } + + NS_IMETHODIMP nsBookmarksService::DoCommand(nsISupportsArray* aSources, nsIRDFResource* aCommand, nsISupportsArray* aArguments) { - NS_NOTYETIMPLEMENTED("write me!"); - return NS_ERROR_NOT_IMPLEMENTED; + NS_NOTYETIMPLEMENTED("write me!"); + return NS_ERROR_NOT_IMPLEMENTED; } + + //////////////////////////////////////////////////////////////////////// // nsIRDFRemoteDataSource + + NS_IMETHODIMP nsBookmarksService::Init(const char* aURI) { @@ -1248,6 +1419,7 @@ nsBookmarksService::Init(const char* aURI) } + NS_IMETHODIMP nsBookmarksService::Refresh(PRBool aBlocking) { @@ -1256,16 +1428,20 @@ nsBookmarksService::Refresh(PRBool aBlocking) } + NS_IMETHODIMP nsBookmarksService::Flush() { - return WriteBookmarks(mInner, kNC_BookmarksRoot); + return WriteBookmarks(mInner, kNC_BookmarksRoot); } + + //////////////////////////////////////////////////////////////////////// // Implementation methods + nsresult nsBookmarksService::ReadBookmarks() { @@ -1399,6 +1575,7 @@ nsBookmarksService::ReadBookmarks() } + nsresult nsBookmarksService::WriteBookmarks(nsIRDFDataSource *ds, nsIRDFResource *root) { @@ -1426,6 +1603,7 @@ nsBookmarksService::WriteBookmarks(nsIRDFDataSource *ds, nsIRDFResource *root) } + nsresult nsBookmarksService::WriteBookmarksContainer(nsIRDFDataSource *ds, nsOutputFileStream strm, nsIRDFResource *parent, PRInt32 level) { @@ -1574,6 +1752,7 @@ nsBookmarksService::WriteBookmarksContainer(nsIRDFDataSource *ds, nsOutputFileSt } + nsresult nsBookmarksService::WriteBookmarkProperties(nsIRDFDataSource *ds, nsOutputFileStream strm, nsIRDFResource *child, nsIRDFResource *property, const char *htmlAttrib, PRBool isFirst) @@ -1609,6 +1788,7 @@ nsBookmarksService::WriteBookmarkProperties(nsIRDFDataSource *ds, nsOutputFileSt } + PRBool nsBookmarksService::CanAccept(nsIRDFResource* aSource, nsIRDFResource* aProperty, @@ -1639,9 +1819,13 @@ nsBookmarksService::CanAccept(nsIRDFResource* aSource, } } + + //////////////////////////////////////////////////////////////////////// // Component Exports + + extern "C" PR_IMPLEMENT(nsresult) NSGetFactory(nsISupports* aServiceMgr, const nsCID &aClass, @@ -1724,5 +1908,3 @@ NSUnregisterSelf(nsISupports* aServMgr, const char* aPath) return NS_OK; } - -