From 330980d79043f05be7f26301031bd4493a31fd14 Mon Sep 17 00:00:00 2001 From: "sspitzer%netscape.com" Date: Fri, 12 May 2000 03:59:02 +0000 Subject: [PATCH] re-add these, in the right place. --- mailnews/base/src/nsSubscribableServer.cpp | 328 +++++++++++++++++++++ mailnews/base/src/nsSubscribableServer.h | 55 ++++ 2 files changed, 383 insertions(+) create mode 100644 mailnews/base/src/nsSubscribableServer.cpp create mode 100644 mailnews/base/src/nsSubscribableServer.h diff --git a/mailnews/base/src/nsSubscribableServer.cpp b/mailnews/base/src/nsSubscribableServer.cpp new file mode 100644 index 000000000000..79506e941078 --- /dev/null +++ b/mailnews/base/src/nsSubscribableServer.cpp @@ -0,0 +1,328 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * 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/ + * + * 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 Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + */ + +#include "nsSubscribableServer.h" + +#include "nsCOMPtr.h" + +#include "nsIRDFService.h" +#include "nsRDFCID.h" + +#include "nsEnumeratorUtils.h" +#include "nsXPIDLString.h" +#include "nsIFolder.h" + +static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID); + +MOZ_DECL_CTOR_COUNTER(nsSubscribableServer); + +nsSubscribableServer::nsSubscribableServer(void) +{ + NS_INIT_REFCNT(); + mDelimiter = '.'; +} + +nsSubscribableServer::~nsSubscribableServer(void) +{ +} + +NS_IMPL_THREADSAFE_ISUPPORTS(nsSubscribableServer, NS_GET_IID(nsISubscribableServer)); + +NS_IMETHODIMP +nsSubscribableServer::SetIncomingServer(nsIMsgIncomingServer *aServer) +{ + mIncomingServer = aServer; + return NS_OK; +} + +NS_IMETHODIMP +nsSubscribableServer::SetDelimiter(char aDelimiter) +{ + mDelimiter = aDelimiter; + return NS_OK; +} + +NS_IMETHODIMP +nsSubscribableServer::SetAsSubscribedInSubscribeDS(const char *aName) +{ + nsresult rv; + + NS_ASSERTION(aName,"no name"); + if (!aName) return NS_ERROR_FAILURE; + + nsXPIDLCString serverUri; + + rv = mIncomingServer->GetServerURI(getter_Copies(serverUri)); + if (NS_FAILED(rv)) return rv; + + nsCAutoString uri; + uri = (const char *)serverUri; + uri += "/"; + uri += aName; + + nsCOMPtr resource; + rv = mRDFService->GetResource((const char *) uri, getter_AddRefs(resource)); + + nsCOMPtr ds; + rv = mRDFService->GetDataSource("rdf:subscribe",getter_AddRefs(mSubscribeDatasource)); + if(NS_FAILED(rv)) return rv; + if (!mSubscribeDatasource) return NS_ERROR_FAILURE; + + nsCOMPtr oldLiteral; + rv = mSubscribeDatasource->GetTarget(resource, kNC_Subscribed, PR_TRUE, getter_AddRefs(oldLiteral)); + if(NS_FAILED(rv)) return rv; + + rv = mSubscribeDatasource->Change(resource, kNC_Subscribed, oldLiteral, kTrueLiteral); + if(NS_FAILED(rv)) return rv; + + return NS_OK; +} + +NS_IMETHODIMP +nsSubscribableServer::UpdateSubscribedInSubscribeDS() +{ + nsresult rv; + nsCOMPtr subFolders; + nsCOMPtr rootFolder; + nsCOMPtr currFolder; + + rv = mIncomingServer->GetRootFolder(getter_AddRefs(rootFolder)); + if (NS_FAILED(rv)) return rv; + + rv = rootFolder->GetSubFolders(getter_AddRefs(subFolders)); + if (NS_FAILED(rv)) return rv; + + nsAdapterEnumerator *simpleEnumerator = new nsAdapterEnumerator(subFolders); + if (simpleEnumerator == nsnull) return NS_ERROR_OUT_OF_MEMORY; + + PRBool moreFolders; + + while (NS_SUCCEEDED(simpleEnumerator->HasMoreElements(&moreFolders)) && moreFolders) { + nsCOMPtr child; + rv = simpleEnumerator->GetNext(getter_AddRefs(child)); + if (NS_SUCCEEDED(rv) && child) { + currFolder = do_QueryInterface(child, &rv); + if (NS_SUCCEEDED(rv) && currFolder) { + nsXPIDLString name; + rv = currFolder->GetName(getter_Copies(name)); + if (NS_SUCCEEDED(rv) && name) { + nsCAutoString asciiName; asciiName.AssignWithConversion(name); + rv = SetAsSubscribedInSubscribeDS((const char *)asciiName); + } + } + } + } + + delete simpleEnumerator; + return NS_OK; +} + +NS_IMETHODIMP +nsSubscribableServer::AddToSubscribeDS(const char *aName) +{ + nsresult rv; + + NS_ASSERTION(aName,"attempting to add something with no name"); + if (!aName) return NS_ERROR_FAILURE; + +#ifdef DEBUG_NEWS + printf("AddToSubscribeDS(%s)\n",aName); +#endif + nsXPIDLCString serverUri; + + rv = mIncomingServer->GetServerURI(getter_Copies(serverUri)); + if (NS_FAILED(rv)) return rv; + + nsCAutoString uri; + uri = (const char *)serverUri; + uri += "/"; + uri += aName; + + nsCOMPtr resource; + rv = mRDFService->GetResource((const char *) uri, getter_AddRefs(resource)); + if(NS_FAILED(rv)) return rv; + + rv = SetPropertiesInSubscribeDS((const char *) uri, aName, resource); + if (NS_FAILED(rv)) return rv; + + rv = FindAndAddParentToSubscribeDS((const char *) uri, (const char *)serverUri, aName, resource); + if(NS_FAILED(rv)) return rv; + + return NS_OK; +} + +NS_IMETHODIMP +nsSubscribableServer::SetPropertiesInSubscribeDS(const char *uri, const char *aName, nsIRDFResource *aResource) +{ + nsresult rv; + +#ifdef DEBUG_sspitzer_ + printf("SetPropertiesInSubscribeDS(%s,%s,??)\n",uri,aName); +#endif + + nsCOMPtr nameLiteral; + nsAutoString nameString; + nameString.AssignWithConversion(aName); + rv = mRDFService->GetLiteral(nameString.GetUnicode(), getter_AddRefs(nameLiteral)); + if(NS_FAILED(rv)) return rv; + + rv = mSubscribeDatasource->Assert(aResource, kNC_Name, nameLiteral, PR_TRUE); + if(NS_FAILED(rv)) return rv; + + rv = mSubscribeDatasource->Assert(aResource, kNC_Subscribed, kFalseLiteral, PR_TRUE); + if(NS_FAILED(rv)) return rv; + + return rv; +} + +NS_IMETHODIMP +nsSubscribableServer::FindAndAddParentToSubscribeDS(const char *uri, const char *serverUri, const char *aName, nsIRDFResource *aChildResource) +{ + nsresult rv; + printf("FindAndAddParentToSubscribeDS(%s,%s,%s,??)\n",uri,serverUri,aName); + + nsCOMPtr parentResource; + + nsCAutoString uriCStr(uri); + + PRInt32 startpos = nsCRT::strlen(serverUri) + 1; + PRInt32 delimpos = uriCStr.RFindChar(mDelimiter,PR_TRUE); + + if (delimpos > startpos) { + uriCStr.Truncate(delimpos); + + nsCAutoString nameCStr(aName); + PRInt32 namedelimpos = nameCStr.RFindChar(mDelimiter,PR_TRUE); + nameCStr.Truncate(namedelimpos); + + rv = mRDFService->GetResource((const char *) uriCStr, getter_AddRefs(parentResource)); + if(NS_FAILED(rv)) return rv; + + PRBool prune = PR_FALSE; + printf("this this has assertion code, you rat bastard\n"); +#if 0 + rv = mSubscribeDatasource->HasAssertion(parentResource, kNC_Subscribed, kFalseLiteral, PR_TRUE, &prune); + if(NS_FAILED(rv)) return rv; +#endif + + if (!prune) { + rv = SetPropertiesInSubscribeDS((const char *)uriCStr, (const char *)nameCStr, parentResource); + if(NS_FAILED(rv)) return rv; + } + + // assert the group as a child of the group above + rv = mSubscribeDatasource->Assert(parentResource, kNC_Child, aChildResource, PR_TRUE); + if(NS_FAILED(rv)) return rv; + + // recurse + if (!prune) { + rv = FindAndAddParentToSubscribeDS((const char *)uriCStr, serverUri, (const char *)nameCStr, parentResource); + if(NS_FAILED(rv)) return rv; + } + } + else { + rv = mRDFService->GetResource(serverUri, getter_AddRefs(parentResource)); + if(NS_FAILED(rv)) return rv; + + // assert the group as a child of the server + rv = mSubscribeDatasource->Assert(parentResource, kNC_Child, aChildResource, PR_TRUE); + if(NS_FAILED(rv)) return rv; + } + + return rv; +} + + +NS_IMETHODIMP +nsSubscribableServer::StopPopulatingSubscribeDS() +{ + mRDFService = nsnull; + mSubscribeDatasource = nsnull; + kNC_Name = nsnull; + kNC_Child = nsnull; + kNC_Subscribed = nsnull; + + return NS_OK; +} + +NS_IMETHODIMP +nsSubscribableServer::StartPopulatingSubscribeDS() +{ + nsresult rv; + mRDFService = do_GetService(kRDFServiceCID, &rv); + NS_ASSERTION(NS_SUCCEEDED(rv) && mRDFService,"no rdf server"); + if (NS_FAILED(rv)) return rv; + + rv = mRDFService->GetDataSource("rdf:subscribe",getter_AddRefs(mSubscribeDatasource)); + NS_ASSERTION(NS_SUCCEEDED(rv) && mSubscribeDatasource,"no subscribe datasource"); + if (NS_FAILED(rv)) return rv; + + rv = mRDFService->GetResource("http://home.netscape.com/NC-rdf#Name", getter_AddRefs(kNC_Name)); + NS_ASSERTION(NS_SUCCEEDED(rv) && kNC_Name,"no name resource"); + if (NS_FAILED(rv)) return rv; + + rv = mRDFService->GetResource("http://home.netscape.com/NC-rdf#child", getter_AddRefs(kNC_Child)); + NS_ASSERTION(NS_SUCCEEDED(rv) && kNC_Child,"no child resource"); + if (NS_FAILED(rv)) return rv; + + rv = mRDFService->GetResource("http://home.netscape.com/NC-rdf#Subscribed", getter_AddRefs(kNC_Subscribed)); + NS_ASSERTION(NS_SUCCEEDED(rv) && kNC_Subscribed, "no subscribed resource"); + if (NS_FAILED(rv)) return rv; + + nsAutoString trueString; + trueString.AssignWithConversion("true"); + rv = mRDFService->GetLiteral(trueString.GetUnicode(), getter_AddRefs(kTrueLiteral)); + if(NS_FAILED(rv)) return rv; + + nsAutoString falseString; + falseString.AssignWithConversion("false"); + rv = mRDFService->GetLiteral(falseString.GetUnicode(), getter_AddRefs(kFalseLiteral)); + if(NS_FAILED(rv)) return rv; + + return NS_OK; +} + +NS_IMETHODIMP +nsSubscribableServer::SetSubscribeListener(nsISubscribeListener *aListener) +{ + if (!aListener) return NS_ERROR_NULL_POINTER; + mSubscribeListener = aListener; + return NS_OK; +} + +NS_IMETHODIMP +nsSubscribableServer::GetSubscribeListener(nsISubscribeListener **aListener) +{ + if (!aListener) return NS_ERROR_NULL_POINTER; + if (mSubscribeListener) { + *aListener = mSubscribeListener; + NS_ADDREF(*aListener); + } + return NS_OK; +} + +NS_IMETHODIMP +nsSubscribableServer::PopulateSubscribeDatasource(nsIMsgWindow *aMsgWindow) +{ + NS_ASSERTION(PR_FALSE,"override this."); + return NS_ERROR_FAILURE; +} diff --git a/mailnews/base/src/nsSubscribableServer.h b/mailnews/base/src/nsSubscribableServer.h new file mode 100644 index 000000000000..f5481edf3c99 --- /dev/null +++ b/mailnews/base/src/nsSubscribableServer.h @@ -0,0 +1,55 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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/ + * + * 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 Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * Seth Spitzer + */ + +#ifndef nsSubscribableServer_h__ +#define nsSubscribableServer_h__ + +#include "nsISubscribableServer.h" +#include "nsCOMPtr.h" +#include "nsIRDFResource.h" +#include "nsIRDFService.h" +#include "nsIMsgIncomingServer.h" + +class nsSubscribableServer : public nsISubscribableServer +{ + public: + nsSubscribableServer(); + virtual ~nsSubscribableServer(); + + NS_DECL_ISUPPORTS + NS_DECL_NSISUBSCRIBABLESERVER + +private: + nsCOMPtr mSubscribeListener; + nsCOMPtr mSubscribeDatasource; + nsCOMPtr mRDFService; + nsCOMPtr kNC_Name; + nsCOMPtr kNC_Child; + nsCOMPtr kNC_Subscribed; + nsCOMPtr kTrueLiteral; + nsCOMPtr kFalseLiteral; + nsCOMPtr mIncomingServer; + char mDelimiter; +}; + +#endif // nsSubscribableServer_h__