Added nsIFileChooser to break dependency on nsFileSpec internals.

This commit is contained in:
warren%netscape.com 1999-06-03 21:51:02 +00:00
Родитель c777f81791
Коммит 2f48fd7da7
9 изменённых файлов: 355 добавлений и 18 удалений

Просмотреть файл

@ -47,3 +47,4 @@ nsIContentConnector.h
nsIKeyBindMgr.h
nsStringUtil.h
nsIContextMenu.h
nsIFileChooser.h

Просмотреть файл

@ -24,14 +24,11 @@ include $(DEPTH)/config/autoconf.mk
DEFINES += -D_IMPL_NS_UI
XPIDLSRCS = \
nsIFileSpecWithUI.idl \
$(NULL)
EXPORTS = \
nsIFontSizeIterator.h \
nsIFontNameIterator.h \
nsIFontRetrieverService.h \
nsIFileChooser.h \
nsIFileListTransferable.h \
nsIMenuBar.h \
nsIMenu.h \

Просмотреть файл

@ -22,14 +22,11 @@ DEFINES=-D_IMPL_NS_UI
MODULE=raptor
XPIDL_MODULE=widget
XPIDLSRCS = \
.\nsIFileSpecWithUI.idl \
$(NULL)
EXPORTS=nsui.h \
nsIFontSizeIterator.h \
nsIFontNameIterator.h \
nsIFontRetrieverService.h \
nsIFileChooser.h \
nsIFileListTransferable.h \
nsIFormatConverter.h \
nsIClipboardOwner.h \

Просмотреть файл

@ -0,0 +1,87 @@
/* -*- 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.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsIFileChooser_h__
#define nsIFileChooser_h__
#include "nsISupports.h"
#include "nscore.h" // for NS_WIDGET
#define NS_IFILECHOOSER_IID \
{ /* b1ec7fc0-17bf-11d3-9337-00104ba0fd40 */ \
0xb1ec7fc0, \
0x17bf, \
0x11d3, \
{0x93, 0x37, 0x00, 0x10, 0x4b, 0xa0, 0xfd, 0x40} \
}
#define NS_FILECHOOSER_CID \
{ /* b7a15700-17bf-11d3-9337-00104ba0fd40 */ \
0xb7a15700, \
0x17bf, \
0x11d3, \
{0x93, 0x37, 0x00, 0x10, 0x4b, 0xa0, 0xfd, 0x40} \
}
class nsIFileChooser : public nsISupports {
public:
NS_DEFINE_STATIC_IID_ACCESSOR(NS_IFILECHOOSER_IID);
NS_IMETHOD Init() = 0;
// The "choose" functions present the file picker UI in order to set the
// value of the file spec.
NS_IMETHOD ChooseOutputFile(const char* windowTitle,
const char* suggestedLeafName) = 0;
// The mask for standard filters is given as follows:
enum StandardFilterMask {
eAllReadable = (1<<0),
eHTMLFiles = (1<<1),
eXMLFiles = (1<<2),
eImageFiles = (1<<3),
eAllFiles = (1<<4),
// Mask containing all the above default filters
eAllStandardFilters = (eAllReadable
| eHTMLFiles
| eXMLFiles
| eImageFiles
| eAllFiles),
// The "extra filter" bit should be set if the "extra filter"
// is passed in to chooseInputFile.
eExtraFilter = (1<<31)
};
enum { kNumStandardFilters = 5 };
NS_IMETHOD ChooseInputFile(const char* title,
StandardFilterMask standardFilterMask,
const char* extraFilterTitle,
const char* extraFilter) = 0;
NS_IMETHOD ChooseDirectory(const char* title) = 0;
NS_IMETHOD GetURLString(char* *result) = 0;
};
extern NS_WIDGET
nsresult NS_NewFileChooser(nsIFileChooser* *result);
#endif // nsIFileChooser_h__

Просмотреть файл

@ -37,7 +37,7 @@ CPPSRCS= \
nsBaseDragService.cpp \
nsBaseClipboard.cpp \
nsFileListTransferable.cpp \
nsFileSpecWithUIImpl.cpp \
nsFileChooser.cpp \
$(NULL)
LOCAL_INCLUDES := \

Просмотреть файл

@ -28,7 +28,7 @@ CPPSRCS = \
nsBaseWidget.cpp \
nsMenuButton.cpp \
nsImageButton.cpp \
nsFileSpecWithUIImpl.cpp \
nsFileChooser.cpp \
nsBaseClipboard.cpp \
nsTransferable.cpp \
nsXIFFormatConverter.cpp \
@ -42,7 +42,7 @@ OBJS= \
.\$(OBJDIR)\nsBaseWidget.obj \
.\$(OBJDIR)\nsMenuButton.obj \
.\$(OBJDIR)\nsImageButton.obj \
.\$(OBJDIR)\nsFileSpecWithUIImpl.obj \
.\$(OBJDIR)\nsFileChooser.obj \
.\$(OBJDIR)\nsBaseClipboard.obj \
.\$(OBJDIR)\nsTransferable.obj \
.\$(OBJDIR)\nsXIFFormatConverter.obj \

Просмотреть файл

@ -0,0 +1,200 @@
/* -*- 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.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "nsFileChooser.h"
#include "nsIFileWidget.h"
#include "nsWidgetsCID.h"
#include "nsCOMPtr.h"
#include "nsIComponentManager.h"
#include "nsCRT.h"
static NS_DEFINE_IID(kCFileWidgetCID, NS_FILEWIDGET_CID);
#undef NS_FILE_FAILURE
#define NS_FILE_FAILURE NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_FILES,(0xFFFF))
nsFileChooser::nsFileChooser()
: mFileSpec()
{
NS_INIT_REFCNT();
// Add any other initialization in Init, not here.
}
NS_IMETHODIMP
nsFileChooser::Init()
{
// Add any other initialization here.
return NS_OK;
}
nsFileChooser::~nsFileChooser()
{
}
NS_METHOD
nsFileChooser::Create(nsISupports* aOuter, const nsIID& aIID, void* *aResult)
{
if (aOuter)
return NS_ERROR_NO_AGGREGATION;
nsFileChooser* FileChooser = new nsFileChooser();
if (FileChooser == NULL)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(FileChooser);
nsresult rv = FileChooser->QueryInterface(aIID, aResult);
NS_RELEASE(FileChooser);
return rv;
}
NS_IMPL_ISUPPORTS(nsFileChooser, nsIFileChooser::GetIID());
////////////////////////////////////////////////////////////////////////////////
NS_IMETHODIMP
nsFileChooser::ChooseOutputFile(const char* windowTitle,
const char* suggestedLeafName)
{
nsresult rv;
nsCOMPtr<nsIFileWidget> fileWidget;
rv = nsComponentManager::CreateInstance(kCFileWidgetCID,
nsnull,
nsIFileWidget::GetIID(),
(void**)getter_AddRefs(fileWidget));
if (NS_FAILED(rv))
return rv;
fileWidget->SetDefaultString(suggestedLeafName);
nsFileDlgResults result = fileWidget->PutFile(nsnull, windowTitle, mFileSpec);
if ( result != nsFileDlgResults_OK)
return NS_FILE_FAILURE;
if (mFileSpec.Exists() && result != nsFileDlgResults_Replace)
return NS_FILE_FAILURE;
return NS_OK;
}
NS_IMETHODIMP
nsFileChooser::ChooseInputFile(const char* inTitle,
StandardFilterMask inMask,
const char* inExtraFilterTitle,
const char* inExtraFilter)
{
nsresult rv = NS_OK;
nsString* nextTitle;
nsString* nextFilter;
nsCOMPtr<nsIFileWidget> fileWidget;
rv = nsComponentManager::CreateInstance(kCFileWidgetCID,
nsnull,
nsIFileWidget::GetIID(),
(void**)getter_AddRefs(fileWidget));
if (NS_FAILED(rv))
return rv;
nsString* titles = nsnull;
nsString* filters = nsnull;
titles = new nsString[1 + kNumStandardFilters];
if (!titles) {
rv = NS_ERROR_OUT_OF_MEMORY;
goto Clean;
}
filters = new nsString[1 + kNumStandardFilters];
if (!filters) {
rv = NS_ERROR_OUT_OF_MEMORY;
goto Clean;
}
nextTitle = titles;
nextFilter = filters;
if (inMask & eAllReadable) {
*nextTitle++ = "All Readable Files";
*nextFilter++ = "*.htm; *.html; *.xml; *.gif; *.jpg; *.jpeg; *.png";
}
if (inMask & eHTMLFiles) {
*nextTitle++ = "HTML Files";
*nextFilter++ = "*.htm; *.html";
}
if (inMask & eXMLFiles) {
*nextTitle++ = "XML Files";
*nextFilter++ = "*.xml";
}
if (inMask & eImageFiles) {
*nextTitle++ = "Image Files";
*nextFilter++ = "*.gif; *.jpg; *.jpeg; *.png";
}
if (inMask & eExtraFilter) {
*nextTitle++ = inExtraFilterTitle;
*nextFilter++ = inExtraFilter;
}
if (inMask & eAllFiles) {
*nextTitle++ = "All Files";
*nextFilter++ = "*.*";
}
fileWidget->SetFilterList(nextFilter - filters, titles, filters);
if (fileWidget->GetFile(nsnull, inTitle, mFileSpec) != nsFileDlgResults_OK)
rv = NS_FILE_FAILURE;
Clean:
delete [] titles;
delete [] filters;
return rv;
}
NS_IMETHODIMP
nsFileChooser::ChooseDirectory(const char* title)
{
nsresult rv;
nsCOMPtr<nsIFileWidget> fileWidget;
rv = nsComponentManager::CreateInstance(kCFileWidgetCID,
nsnull,
nsIFileWidget::GetIID(),
(void**)getter_AddRefs(fileWidget));
if (NS_FAILED(rv))
return rv;
if (fileWidget->GetFolder(nsnull, title, mFileSpec) != nsFileDlgResults_OK)
rv = NS_FILE_FAILURE;
return NS_OK;
}
NS_IMETHODIMP
nsFileChooser::GetURLString(char* *result)
{
nsFileURL fileURL(mFileSpec);
char* urlStr = nsCRT::strdup(fileURL.GetURLString());
if (urlStr == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
*result = urlStr;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
PR_IMPLEMENT(nsresult)
NS_NewFileChooser(nsIFileChooser* *result)
{
nsresult rv = nsFileChooser::Create(NULL, nsIFileChooser::GetIID(),
(void**)result);
if (NS_SUCCEEDED(rv)) {
rv = (*result)->Init();
if (NS_FAILED(rv))
NS_RELEASE(*result);
}
return rv;
}
////////////////////////////////////////////////////////////////////////////////

Просмотреть файл

@ -0,0 +1,55 @@
/* -*- 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.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsFileChooser_h__
#define nsFileChooser_h__
#include "nsIFileChooser.h"
#include "nsFileSpec.h"
class nsFileChooser : public nsIFileChooser {
public:
nsFileChooser();
virtual ~nsFileChooser();
static NS_METHOD
Create(nsISupports* aOuter, const nsIID& aIID, void* *aResult);
// from nsIFileChooser:
NS_IMETHOD Init();
NS_IMETHOD ChooseOutputFile(const char* windowTitle,
const char* suggestedLeafName);
NS_IMETHOD ChooseInputFile(const char* title,
StandardFilterMask standardFilterMask,
const char* extraFilterTitle,
const char* extraFilter);
NS_IMETHOD ChooseDirectory(const char* title);
NS_IMETHOD GetURLString(char* *result);
// from nsISupports:
NS_DECL_ISUPPORTS
protected:
nsFileSpec mFileSpec;
};
#endif // nsFileChooser_h__

Просмотреть файл

@ -28,7 +28,7 @@
#include "nsAppCoresCIDs.h"
#include "nsIDOMAppCoresManager.h"
#include "nsIFileSpecWithUI.h"
#include "nsIFileChooser.h"
#include "nsIScriptContext.h"
#include "nsIScriptContextOwner.h"
@ -1143,19 +1143,19 @@ static void BuildFileURL(const char * aFileName, nsString & aFileURL)
NS_IMETHODIMP nsBrowserAppCore::OpenWindow()
//----------------------------------------------------------------------------------------
{
nsCOMPtr<nsIFileSpecWithUI> fileSpec;
nsresult rv = NS_NewFileSpecWithUI(getter_AddRefs(fileSpec));
nsCOMPtr<nsIFileChooser> fileChooser;
nsresult rv = NS_NewFileChooser(getter_AddRefs(fileChooser));
if (NS_FAILED(rv))
return rv;
rv = fileSpec->chooseInputFile(
"Open File", nsIFileSpecWithUI::eAllStandardFilters, nsnull, nsnull);
rv = fileChooser->ChooseInputFile(
"Open File", nsIFileChooser::eAllStandardFilters, nsnull, nsnull);
if (NS_FAILED(rv))
return rv;
char buffer[1024];
char* urlString;
rv = fileSpec->GetURLString(&urlString);
rv = fileChooser->GetURLString(&urlString);
if (NS_FAILED(rv))
return rv;
PR_snprintf( buffer, sizeof buffer, "OpenFile(\"%s\")", urlString);