diff --git a/mozilla/xpcom/io/nsDirectoryService.cpp b/mozilla/xpcom/io/nsDirectoryService.cpp new file mode 100644 index 00000000000..5c8bd2dcab3 --- /dev/null +++ b/mozilla/xpcom/io/nsDirectoryService.cpp @@ -0,0 +1,149 @@ +/* -*- Mode: C++; tab-width: 2; 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): + */ + + #include "nsDirectoryService.h" + + +nsDirectoryService::nsDirectoryService(nsISupports* outer) +{ + NS_INIT_AGGREGATED(outer); +} + +NS_METHOD +nsDirectoryService::Create(nsISupports *outer, REFNSIID aIID, void **aResult) +{ + NS_ENSURE_ARG_POINTER(aResult); + NS_ENSURE_PROPER_AGGREGATION(outer, aIID); + + nsDirectoryService* props = new nsDirectoryService(outer); + if (props == NULL) + return NS_ERROR_OUT_OF_MEMORY; + + nsresult rv = props->AggregatedQueryInterface(aIID, aResult); + if (NS_FAILED(rv)) + delete props; + return rv; +} + +PRBool +nsDirectoryService::ReleaseValues(nsHashKey* key, void* data, void* closure) +{ + nsISupports* value = (nsISupports*)data; + NS_IF_RELEASE(value); + return PR_TRUE; +} + +nsDirectoryService::~nsDirectoryService() +{ + Enumerate(ReleaseValues); +} + +NS_IMPL_AGGREGATED(nsDirectoryService); + +NS_METHOD +nsDirectoryService::AggregatedQueryInterface(const nsIID& aIID, void** aInstancePtr) +{ + NS_ENSURE_ARG_POINTER(aInstancePtr); + + if (aIID.Equals(NS_GET_IID(nsISupports))) + *aInstancePtr = GetInner(); + else if (aIID.Equals(NS_GET_IID(nsIProperties))) + *aInstancePtr = NS_STATIC_CAST(nsIProperties*, this); + else { + *aInstancePtr = nsnull; + return NS_NOINTERFACE; + } + + NS_ADDREF((nsISupports*)*aInstancePtr); + return NS_OK; +} + +NS_IMETHODIMP +nsDirectoryService::DefineProperty(const char* prop, nsISupports* initialValue) +{ + nsStringKey key(prop); + if (Exists(&key)) + return NS_ERROR_FAILURE; + + nsISupports* prevValue = (nsISupports*)Put(&key, initialValue); + NS_ASSERTION(prevValue == NULL, "hashtable error"); + NS_IF_ADDREF(initialValue); + return NS_OK; +} + +NS_IMETHODIMP +nsDirectoryService::UndefineProperty(const char* prop) +{ + nsStringKey key(prop); + if (!Exists(&key)) + return NS_ERROR_FAILURE; + + nsISupports* prevValue = (nsISupports*)Remove(&key); + NS_IF_RELEASE(prevValue); + return NS_OK; +} + +NS_IMETHODIMP +nsDirectoryService::GetProperty(const char* prop, nsISupports* *result) +{ + nsStringKey key(prop); + if (!Exists(&key)) + { + // check to see if it is one of our defaults + // + // code here.. + // + // + // else + // + return NS_ERROR_FAILURE; + } + + nsISupports* value = (nsISupports*)Get(&key); + NS_IF_ADDREF(value); + *result = value; + return NS_OK; +} + +NS_IMETHODIMP +nsDirectoryService::SetProperty(const char* prop, nsISupports* value) +{ + nsStringKey key(prop); + if (!Exists(&key)) + return NS_ERROR_FAILURE; + + nsISupports* prevValue = (nsISupports*)Put(&key, value); + NS_IF_RELEASE(prevValue); + NS_IF_ADDREF(value); + return NS_OK; +} + +NS_IMETHODIMP +nsDirectoryService::HasProperty(const char* prop, nsISupports* expectedValue) +{ + nsISupports* value; + nsresult rv = GetProperty(prop, &value); + if (NS_FAILED(rv)) return rv; + rv = (value == expectedValue) ? NS_OK : NS_ERROR_FAILURE; + NS_IF_RELEASE(value); + return rv; +} diff --git a/mozilla/xpcom/io/nsDirectoryService.h b/mozilla/xpcom/io/nsDirectoryService.h new file mode 100644 index 00000000000..03ad3c175a2 --- /dev/null +++ b/mozilla/xpcom/io/nsDirectoryService.h @@ -0,0 +1,69 @@ +/* -*- Mode: C++; tab-width: 2; 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): + */ + +#ifndef nsDirectoryService_h___ +#define nsDirectoryService_h___ + +#include "nsIProperties.h" +#include "nsHashtable.h" +#include "nsAgg.h" + + +#define NS_DIRECTORY_SERVICE_CID \ +{ /* f00152d0-b40b-11d3-8c9c-000064657374 */ \ + 0xf00152d0, \ + 0xb40b, \ + 0x11d3, \ + {0x8c, 0x9c, 0x00, 0x00, 0x64, 0x65, 0x73, 0x74} \ +} + +#define NS_DIRECTORY_SERVICE_PROGID "component://netscape/file/directory_service" +#define NS_DIRECTORY_SERVICE_CLASSNAME "nsIFile Directory Service" + + +class nsDirectoryService : public nsIProperties, public nsHashtable { +public: + + NS_DEFINE_STATIC_CID_ACCESSOR(NS_DIRECTORY_SERVICE_CID) + + NS_DECL_AGGREGATED + + // nsIProperties methods: + NS_IMETHOD DefineProperty(const char* prop, nsISupports* initialValue); + NS_IMETHOD UndefineProperty(const char* prop); + NS_IMETHOD GetProperty(const char* prop, nsISupports* *result); + NS_IMETHOD SetProperty(const char* prop, nsISupports* value); + NS_IMETHOD HasProperty(const char* prop, nsISupports* value); + + // nsProperties methods: + nsDirectoryService(nsISupports* outer); + virtual ~nsDirectoryService(); + + static NS_METHOD + Create(nsISupports *aOuter, REFNSIID aIID, void **aResult); + + static PRBool ReleaseValues(nsHashKey* key, void* data, void* closure); + +}; + + +#endif \ No newline at end of file diff --git a/mozilla/xpcom/io/nsIFile.idl b/mozilla/xpcom/io/nsIFile.idl index 3b6253dc30b..6f7f97062aa 100644 --- a/mozilla/xpcom/io/nsIFile.idl +++ b/mozilla/xpcom/io/nsIFile.idl @@ -220,6 +220,22 @@ interface nsIFile : nsISupports */ boolean isSpecial(); + + + /** + * clone() + * + * This function will allocate and initialize a nsIFile object to the + * exact location of the |this| nsIFile. + * + * @param file + * A nsIFile which this object will be initialize + * with. + * + */ + + nsIFile clone(); + /** * Will determine if the inFile equals this. */ diff --git a/mozilla/xpcom/io/nsILocalFile.idl b/mozilla/xpcom/io/nsILocalFile.idl index c9794d70690..270f411e72f 100644 --- a/mozilla/xpcom/io/nsILocalFile.idl +++ b/mozilla/xpcom/io/nsILocalFile.idl @@ -25,42 +25,15 @@ #include "nsIFile.idl" +%{C++ +#include "prio.h" +%} + +[ptr] native PRFileDescStar(PRFileDesc); + [scriptable, uuid(aa610f20-a889-11d3-8c81-000064657374)] interface nsILocalFile : nsIFile { - - /** - * initWithKey - * - * This function will initialize the nsIFile object to the - * location of a well known place. The location is specified - * by a string which will be looked up in the registry. If - * the string is not found, NS_ERROR_FILE_UNRECONGNIZED_PATH - * will be returned - * - * @param fileKey - * A string which represents a special system directory - * or a directory which has been registered. - * - */ - - void initWithKey( [const] in string fileKey); - - - /** - * initWithFile - * - * This function will initialize the nsIFile object to the - * exact location of the passed nsIFile. // what errors can this return? - * - * @param file - * A nsIFile which this object will be initialize - * with. - * - */ - - void initWithFile(in nsIFile file); - /** * initWithPath * @@ -83,7 +56,8 @@ interface nsILocalFile : nsIFile */ void initWithPath([const] in string filePath ); - + + [noscript] PRFileDescStar open(in long flags, in long mode); readonly attribute PRInt64 diskSpaceAvailable; // maybe we should put this somewhere else. diff --git a/mozilla/xpcom/io/nsLocalFileMac.cpp b/mozilla/xpcom/io/nsLocalFileMac.cpp index e2368a9b122..d0a3c21610c 100644 --- a/mozilla/xpcom/io/nsLocalFileMac.cpp +++ b/mozilla/xpcom/io/nsLocalFileMac.cpp @@ -145,24 +145,14 @@ class nsDirEnumerator : public nsISimpleEnumerator return NS_OK; } - nsCOMPtr file; - - rv = nsComponentManager::CreateInstance(NS_LOCAL_FILE_PROGID, - nsnull, - nsCOMTypeInfo::GetIID(), - getter_AddRefs(file)); - if (NS_FAILED(rv)) - return rv; - - rv = file->InitWithFile(mParent); - if (NS_FAILED(rv)) - return rv; + nsCOMPtr file; + mParent->Clone(getter_AddRefs(file)); rv = file->AppendPath(entry->name); if (NS_FAILED(rv)) return rv; - mNext = file; + mNext = do_QueryInterface(file); } *result = mNext != nsnull; return NS_OK; @@ -245,32 +235,33 @@ nsLocalFile::MakeDirty() mStatDirty = PR_TRUE; } - NS_IMETHODIMP -nsLocalFile::InitWithKey(const char *fileKey) +nsLocalFile::Clone(nsIFile **file) { - MakeDirty(); - return NS_ERROR_NOT_IMPLEMENTED; -} - -NS_IMETHODIMP -nsLocalFile::InitWithFile(nsIFile *file) -{ - MakeDirty(); NS_ENSURE_ARG(file); + *file = nsnull; - // Until we have a version that extracts the FSSpec from the donor nsIFile just - // grab the + nsCOMPtr localFile; + nsresult rv = nsComponentManager::CreateInstance(NS_LOCAL_FILE_PROGID, + nsnull, + nsCOMTypeInfo::GetIID(), + getter_AddRefs(localFile)); + if (NS_FAILED(rv)) + return rv; + char* aFilePath; - file->GetPath(&aFilePath); + GetPath(&aFilePath); - // Be paranoid and see if we got a null string for the path - if (aFilePath == nsnull) - return NS_ERROR_FILE_UNRECOGNIZED_PATH; - - mWorkingPath.SetString(aFilePath); + rv = localFile->InitWithPath(aFilePath); + nsAllocator::Free(aFilePath); + if (NS_FAILED(rv)) + return rv; + + *file = localFile; + NS_ADDREF(*file); + return NS_OK; } @@ -292,6 +283,11 @@ nsLocalFile::InitWithPath(const char *filePath) return NS_OK; } +NS_IMETHODIMP +nsLocalFile::Open(PRInt32 flags, PRInt32 mode, PRFileDesc **_retval) +{ + return NS_ERROR_FAILURE; +} NS_IMETHODIMP nsLocalFile::Create(PRUint32 type, PRUint32 attributes) @@ -612,18 +608,8 @@ nsLocalFile::CopyMove(nsIFile *newParentDir, const char *newName, PRBool followS { // create a new target destination in the new parentDir; nsCOMPtr target; + newParentDir->Clone(getter_AddRefs(target)); - rv = nsComponentManager::CreateInstance(NS_LOCAL_FILE_PROGID, - nsnull, - nsCOMTypeInfo::GetIID(), - getter_AddRefs(target)); - if (NS_FAILED(rv)) - return rv; - - rv = target->InitWithFile(newParentDir); - if (NS_FAILED(rv)) - return rv; - char *allocatedNewName; if (!newName) { diff --git a/mozilla/xpcom/io/nsLocalFileUnix.cpp b/mozilla/xpcom/io/nsLocalFileUnix.cpp index c27e9f3bcbc..1186d4c4d97 100644 --- a/mozilla/xpcom/io/nsLocalFileUnix.cpp +++ b/mozilla/xpcom/io/nsLocalFileUnix.cpp @@ -86,24 +86,31 @@ nsLocalFile::Create(nsISupports *outer, const nsIID &aIID, void **aInstancePtr) return inst->QueryInterface(aIID, aInstancePtr); } -NS_IMETHODIMP -nsLocalFile::InitWithKey(const char *fileKey) -{ - return NS_ERROR_NOT_IMPLEMENTED; -} NS_IMETHODIMP -nsLocalFile::InitWithFile(nsIFile *file) +nsLocalFile::Clone(nsIFile **file) { NS_ENSURE_ARG(file); - invalidateCache(); - if (NS_FAILED(file->GetPath(nsIFile::NATIVE_PATH, getter_Copies(mPath)))) - return NS_ERROR_FAILURE; - - if ((const char *)mPath == 0) - return NS_ERROR_FILE_UNRECOGNIZED_PATH; + *file = nsnull; + nsCOMPtr localFile; + nsresult rv = nsComponentManager::CreateInstance(NS_LOCAL_FILE_PROGID, + nsnull, + nsCOMTypeInfo::GetIID(), + getter_AddRefs(localFile)); + + if (NS_FAILED(rv)) + return rv; + + rv = localfile->InitWithPath(mPath); + + if (NS_FAILED(rv)) + return rv; + + *file = localFile; + NS_ADDREF(*file); + return NS_OK; } @@ -127,6 +134,22 @@ nsLocalFile::createAllParentDirectories(PRUint32 permissions) return NS_ERROR_NOT_IMPLEMENTED; } + +NS_IMETHODIMP +nsLocalFile::Open(PRInt32 flags, PRInt32 mode, PRFileDesc **_retval) +{ + CHECK_mPath(); + + *_retval = PR_Open(mPath, flags, mode); + + if (*_retval) + return NS_OK; + + return NS_ERROR_FAILURE; +} + + + NS_IMETHODIMP nsLocalFile::Create(PRUint32 type, PRUint32 permissions) { diff --git a/mozilla/xpcom/io/nsLocalFileWin.cpp b/mozilla/xpcom/io/nsLocalFileWin.cpp index 8008fcf701e..783feaaf4e2 100644 --- a/mozilla/xpcom/io/nsLocalFileWin.cpp +++ b/mozilla/xpcom/io/nsLocalFileWin.cpp @@ -168,24 +168,14 @@ class nsDirEnumerator : public nsISimpleEnumerator return NS_OK; } - nsCOMPtr file; + nsCOMPtr file; + mParent->Clone(getter_AddRefs(file)); - rv = nsComponentManager::CreateInstance(NS_LOCAL_FILE_PROGID, - nsnull, - nsCOMTypeInfo::GetIID(), - getter_AddRefs(file)); - if (NS_FAILED(rv)) - return rv; - - rv = file->InitWithFile(mParent); - if (NS_FAILED(rv)) - return rv; - rv = file->AppendPath(entry->name); if (NS_FAILED(rv)) return rv; - mNext = file; + mNext = do_QueryInterface(file); } *result = mNext != nsnull; return NS_OK; @@ -514,27 +504,32 @@ nsLocalFile::ResolveAndStat(PRBool resolveTerminal) } NS_IMETHODIMP -nsLocalFile::InitWithKey(const char *fileKey) +nsLocalFile::Clone(nsIFile **file) { - MakeDirty(); - return NS_ERROR_NOT_IMPLEMENTED; -} - -NS_IMETHODIMP -nsLocalFile::InitWithFile(nsIFile *file) -{ - MakeDirty(); NS_ENSURE_ARG(file); + *file = nsnull; + nsCOMPtr localFile; + nsresult rv = nsComponentManager::CreateInstance(NS_LOCAL_FILE_PROGID, + nsnull, + nsCOMTypeInfo::GetIID(), + getter_AddRefs(localFile)); + if (NS_FAILED(rv)) + return rv; + char* aFilePath; - file->GetPath(&aFilePath); - - if (aFilePath == nsnull) - return NS_ERROR_FILE_UNRECOGNIZED_PATH; - - mWorkingPath.SetString(aFilePath); + GetPath(&aFilePath); + rv = localFile->InitWithPath(aFilePath); + nsAllocator::Free(aFilePath); + + if (NS_FAILED(rv)) + return rv; + + *file = localFile; + NS_ADDREF(*file); + return NS_OK; } @@ -570,6 +565,22 @@ nsLocalFile::InitWithPath(const char *filePath) return NS_OK; } +NS_IMETHODIMP +nsLocalFile::Open(PRInt32 flags, PRInt32 mode, PRFileDesc **_retval) +{ + nsresult rv = ResolveAndStat(PR_TRUE); + if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND) + return rv; + + *_retval = PR_Open(mResolvedPath, flags, mode); + + if (*_retval) + return NS_OK; + + return NS_ERROR_FAILURE; +} + + NS_IMETHODIMP nsLocalFile::Create(PRUint32 type, PRUint32 attributes) @@ -828,16 +839,9 @@ nsLocalFile::CopyMove(nsIFile *newParentDir, const char *newName, PRBool followS else { // create a new target destination in the new parentDir; - nsCOMPtr target; - - rv = nsComponentManager::CreateInstance(NS_LOCAL_FILE_PROGID, - nsnull, - nsCOMTypeInfo::GetIID(), - getter_AddRefs(target)); - if (NS_FAILED(rv)) - return rv; - - rv = target->InitWithFile(newParentDir); + nsCOMPtr target; + rv = newParentDir->Clone(getter_AddRefs(target)); + if (NS_FAILED(rv)) return rv; @@ -919,20 +923,30 @@ nsLocalFile::CopyMove(nsIFile *newParentDir, const char *newName, PRBool followS // If we moved, we want to adjust this. if (move) { + MakeDirty(); + + char* newParentPath; + newParentDir->GetPath(&newParentPath); + + if (newParentPath == nsnull) + return NS_ERROR_FAILURE; + + InitWithPath(newParentPath); + if (newName == nsnull) { char *aFileName; GetLeafName(&aFileName); - InitWithFile(newParentDir); + AppendPath(aFileName); nsAllocator::Free(aFileName); } else { - InitWithFile(newParentDir); AppendPath(newName); } - MakeDirty(); + + nsAllocator::Free(newParentPath); } return NS_OK;