2000-03-22 10:43:43 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2000-03-22 10:43:43 +03:00
|
|
|
|
2011-12-15 01:22:28 +04:00
|
|
|
#include "nsFilePicker.h"
|
|
|
|
|
|
|
|
#include <shlobj.h>
|
|
|
|
#include <shlwapi.h>
|
|
|
|
#include <cderr.h>
|
|
|
|
|
2016-11-15 21:36:01 +03:00
|
|
|
#include "mozilla/mscom/EnsureMTA.h"
|
2015-10-30 21:35:28 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2013-11-22 07:35:42 +04:00
|
|
|
#include "mozilla/WindowsVersion.h"
|
2001-09-29 12:28:41 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2001-06-22 02:02:47 +04:00
|
|
|
#include "nsNetUtil.h"
|
2003-02-24 23:15:48 +03:00
|
|
|
#include "nsWindow.h"
|
2012-04-09 23:53:46 +04:00
|
|
|
#include "nsILoadContext.h"
|
2000-03-22 10:43:43 +03:00
|
|
|
#include "nsIServiceManager.h"
|
2000-04-05 03:50:41 +04:00
|
|
|
#include "nsIURL.h"
|
2000-03-23 10:41:17 +03:00
|
|
|
#include "nsIStringBundle.h"
|
2002-09-07 09:23:45 +04:00
|
|
|
#include "nsEnumeratorUtils.h"
|
2002-05-15 22:55:21 +04:00
|
|
|
#include "nsCRT.h"
|
2002-07-03 04:55:47 +04:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsToolkit.h"
|
2012-01-04 14:21:44 +04:00
|
|
|
#include "WinUtils.h"
|
2012-04-14 03:49:11 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
2016-01-15 13:38:03 +03:00
|
|
|
#include "GeckoProfiler.h"
|
2012-01-04 14:21:44 +04:00
|
|
|
|
2016-11-15 21:36:01 +03:00
|
|
|
using mozilla::IsWin8OrLater;
|
2015-10-30 21:35:28 +03:00
|
|
|
using mozilla::MakeUnique;
|
2016-11-15 21:36:01 +03:00
|
|
|
using mozilla::mscom::EnsureMTA;
|
2015-10-30 21:35:28 +03:00
|
|
|
using mozilla::UniquePtr;
|
2012-01-04 14:21:44 +04:00
|
|
|
using namespace mozilla::widget;
|
2002-07-03 04:55:47 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t *nsFilePicker::mLastUsedUnicodeDirectory;
|
2002-02-02 02:58:55 +03:00
|
|
|
char nsFilePicker::mLastUsedDirectory[MAX_PATH+1] = { 0 };
|
|
|
|
|
2011-12-15 01:22:28 +04:00
|
|
|
static const unsigned long kDialogTimerTimeout = 300;
|
|
|
|
|
2002-02-20 10:50:52 +03:00
|
|
|
#define MAX_EXTENSION_LENGTH 10
|
2011-12-15 01:22:46 +04:00
|
|
|
#define FILE_BUFFER_SIZE 4096
|
2002-02-20 10:50:52 +03:00
|
|
|
|
2011-12-19 23:27:11 +04:00
|
|
|
typedef DWORD FILEOPENDIALOGOPTIONS;
|
|
|
|
|
2011-12-15 01:22:16 +04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Helper classes
|
|
|
|
|
2012-10-28 08:24:02 +04:00
|
|
|
// Manages matching SuppressBlurEvents calls on the parent widget.
|
|
|
|
class AutoSuppressEvents
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit AutoSuppressEvents(nsIWidget* aWidget) :
|
|
|
|
mWindow(static_cast<nsWindow *>(aWidget)) {
|
|
|
|
SuppressWidgetEvents(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoSuppressEvents() {
|
|
|
|
SuppressWidgetEvents(false);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
void SuppressWidgetEvents(bool aFlag) {
|
|
|
|
if (mWindow) {
|
|
|
|
mWindow->SuppressBlurEvents(aFlag);
|
|
|
|
}
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsWindow> mWindow;
|
2012-10-28 08:24:02 +04:00
|
|
|
};
|
|
|
|
|
2011-12-15 01:22:16 +04:00
|
|
|
// Manages the current working path.
|
|
|
|
class AutoRestoreWorkingPath
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoRestoreWorkingPath() {
|
2013-10-08 22:48:02 +04:00
|
|
|
DWORD bufferLength = GetCurrentDirectoryW(0, nullptr);
|
2015-10-30 21:35:28 +03:00
|
|
|
mWorkingPath = MakeUnique<wchar_t[]>(bufferLength);
|
|
|
|
if (GetCurrentDirectoryW(bufferLength, mWorkingPath.get()) == 0) {
|
2013-10-08 22:48:02 +04:00
|
|
|
mWorkingPath = nullptr;
|
2011-12-15 01:22:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoRestoreWorkingPath() {
|
|
|
|
if (HasWorkingPath()) {
|
2015-10-30 21:35:28 +03:00
|
|
|
::SetCurrentDirectoryW(mWorkingPath.get());
|
2011-12-15 01:22:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool HasWorkingPath() const {
|
2013-10-08 22:48:02 +04:00
|
|
|
return mWorkingPath != nullptr;
|
2011-12-15 01:22:16 +04:00
|
|
|
}
|
|
|
|
private:
|
2015-10-30 21:35:28 +03:00
|
|
|
UniquePtr<wchar_t[]> mWorkingPath;
|
2011-12-15 01:22:16 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Manages NS_NATIVE_TMP_WINDOW child windows. NS_NATIVE_TMP_WINDOWs are
|
|
|
|
// temporary child windows of mParentWidget created to address RTL issues
|
|
|
|
// in picker dialogs. We are responsible for destroying these.
|
|
|
|
class AutoDestroyTmpWindow
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit AutoDestroyTmpWindow(HWND aTmpWnd) :
|
|
|
|
mWnd(aTmpWnd) {
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoDestroyTmpWindow() {
|
|
|
|
if (mWnd)
|
|
|
|
DestroyWindow(mWnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline HWND get() const { return mWnd; }
|
|
|
|
private:
|
|
|
|
HWND mWnd;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Manages matching PickerOpen/PickerClosed calls on the parent widget.
|
|
|
|
class AutoWidgetPickerState
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit AutoWidgetPickerState(nsIWidget* aWidget) :
|
|
|
|
mWindow(static_cast<nsWindow *>(aWidget)) {
|
|
|
|
PickerState(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoWidgetPickerState() {
|
|
|
|
PickerState(false);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
void PickerState(bool aFlag) {
|
|
|
|
if (mWindow) {
|
|
|
|
if (aFlag)
|
|
|
|
mWindow->PickerOpen();
|
|
|
|
else
|
|
|
|
mWindow->PickerClosed();
|
|
|
|
}
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsWindow> mWindow;
|
2011-12-15 01:22:16 +04:00
|
|
|
};
|
|
|
|
|
2011-12-15 01:22:28 +04:00
|
|
|
// Manages a simple callback timer
|
|
|
|
class AutoTimerCallbackCancel
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoTimerCallbackCancel(nsFilePicker* aTarget,
|
2017-06-29 22:13:25 +03:00
|
|
|
nsTimerCallbackFunc aCallbackFunc,
|
|
|
|
const char* aName)
|
|
|
|
{
|
|
|
|
Init(aTarget, aCallbackFunc, aName);
|
2011-12-15 01:22:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
~AutoTimerCallbackCancel() {
|
|
|
|
if (mPickerCallbackTimer) {
|
|
|
|
mPickerCallbackTimer->Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void Init(nsFilePicker* aTarget,
|
2017-06-29 22:13:25 +03:00
|
|
|
nsTimerCallbackFunc aCallbackFunc,
|
|
|
|
const char* aName)
|
|
|
|
{
|
2017-10-16 09:12:54 +03:00
|
|
|
NS_NewTimerWithFuncCallback(getter_AddRefs(mPickerCallbackTimer),
|
|
|
|
aCallbackFunc,
|
|
|
|
aTarget,
|
|
|
|
kDialogTimerTimeout,
|
|
|
|
nsITimer::TYPE_REPEATING_SLACK,
|
|
|
|
aName);
|
2011-12-15 01:22:28 +04:00
|
|
|
if (!mPickerCallbackTimer) {
|
|
|
|
NS_WARNING("do_CreateInstance for timer failed??");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsITimer> mPickerCallbackTimer;
|
|
|
|
};
|
|
|
|
|
2011-12-15 01:22:16 +04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIFilePicker
|
|
|
|
|
2011-12-15 01:22:28 +04:00
|
|
|
nsFilePicker::nsFilePicker() :
|
|
|
|
mSelectedType(1)
|
2013-10-08 22:48:02 +04:00
|
|
|
, mDlgWnd(nullptr)
|
2011-12-15 01:22:28 +04:00
|
|
|
, mFDECookie(0)
|
2000-03-22 10:43:43 +03:00
|
|
|
{
|
2013-10-08 22:48:02 +04:00
|
|
|
CoInitialize(nullptr);
|
2000-03-22 10:43:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsFilePicker::~nsFilePicker()
|
|
|
|
{
|
2006-05-24 17:48:01 +04:00
|
|
|
if (mLastUsedUnicodeDirectory) {
|
2015-04-01 08:29:55 +03:00
|
|
|
free(mLastUsedUnicodeDirectory);
|
2012-07-30 18:20:58 +04:00
|
|
|
mLastUsedUnicodeDirectory = nullptr;
|
2006-05-24 17:48:01 +04:00
|
|
|
}
|
2011-12-15 01:22:28 +04:00
|
|
|
CoUninitialize();
|
2000-03-22 10:43:43 +03:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsFilePicker, nsIFilePicker)
|
2011-12-15 01:22:28 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
NS_IMETHODIMP nsFilePicker::Init(mozIDOMWindowProxy *aParent, const nsAString& aTitle, int16_t aMode)
|
2012-04-09 23:53:46 +04:00
|
|
|
{
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> window = do_QueryInterface(aParent);
|
2013-10-08 22:48:02 +04:00
|
|
|
nsIDocShell* docShell = window ? window->GetDocShell() : nullptr;
|
2012-04-09 23:53:46 +04:00
|
|
|
mLoadContext = do_QueryInterface(docShell);
|
|
|
|
|
|
|
|
return nsBaseFilePicker::Init(aParent, aTitle, aMode);
|
|
|
|
}
|
2011-12-15 01:22:28 +04:00
|
|
|
|
2011-12-15 01:22:28 +04:00
|
|
|
STDMETHODIMP nsFilePicker::QueryInterface(REFIID refiid, void** ppvResult)
|
|
|
|
{
|
2013-10-08 22:48:02 +04:00
|
|
|
*ppvResult = nullptr;
|
2011-12-15 01:22:28 +04:00
|
|
|
if (IID_IUnknown == refiid ||
|
2011-12-15 01:22:28 +04:00
|
|
|
refiid == IID_IFileDialogEvents) {
|
2011-12-15 01:22:28 +04:00
|
|
|
*ppvResult = this;
|
|
|
|
}
|
|
|
|
|
2013-10-08 22:48:02 +04:00
|
|
|
if (nullptr != *ppvResult) {
|
2011-12-15 01:22:28 +04:00
|
|
|
((LPUNKNOWN)*ppvResult)->AddRef();
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
2011-12-15 01:22:16 +04:00
|
|
|
|
2011-12-15 01:22:28 +04:00
|
|
|
/*
|
|
|
|
* Vista+ callbacks
|
|
|
|
*/
|
|
|
|
|
|
|
|
HRESULT
|
|
|
|
nsFilePicker::OnFileOk(IFileDialog *pfd)
|
|
|
|
{
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT
|
2011-12-15 01:22:28 +04:00
|
|
|
nsFilePicker::OnFolderChanging(IFileDialog *pfd,
|
|
|
|
IShellItem *psiFolder)
|
2011-12-15 01:22:28 +04:00
|
|
|
{
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT
|
|
|
|
nsFilePicker::OnFolderChange(IFileDialog *pfd)
|
|
|
|
{
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT
|
|
|
|
nsFilePicker::OnSelectionChange(IFileDialog *pfd)
|
|
|
|
{
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT
|
2011-12-15 01:22:28 +04:00
|
|
|
nsFilePicker::OnShareViolation(IFileDialog *pfd,
|
|
|
|
IShellItem *psi,
|
|
|
|
FDE_SHAREVIOLATION_RESPONSE *pResponse)
|
2011-12-15 01:22:28 +04:00
|
|
|
{
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT
|
|
|
|
nsFilePicker::OnTypeChange(IFileDialog *pfd)
|
|
|
|
{
|
2011-12-15 01:22:28 +04:00
|
|
|
// Failures here result in errors due to security concerns.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IOleWindow> win;
|
2011-12-15 01:22:28 +04:00
|
|
|
pfd->QueryInterface(IID_IOleWindow, getter_AddRefs(win));
|
|
|
|
if (!win) {
|
|
|
|
NS_ERROR("Could not retrieve the IOleWindow interface for IFileDialog.");
|
|
|
|
return S_OK;
|
|
|
|
}
|
2013-10-08 22:48:02 +04:00
|
|
|
HWND hwnd = nullptr;
|
2011-12-15 01:22:28 +04:00
|
|
|
win->GetWindow(&hwnd);
|
|
|
|
if (!hwnd) {
|
|
|
|
NS_ERROR("Could not retrieve the HWND for IFileDialog.");
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetDialogHandle(hwnd);
|
2011-12-15 01:22:28 +04:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT
|
2011-12-15 01:22:28 +04:00
|
|
|
nsFilePicker::OnOverwrite(IFileDialog *pfd,
|
|
|
|
IShellItem *psi,
|
|
|
|
FDE_OVERWRITE_RESPONSE *pResponse)
|
2011-12-15 01:22:28 +04:00
|
|
|
{
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-15 01:22:28 +04:00
|
|
|
/*
|
|
|
|
* Close on parent close logic
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool
|
2016-12-23 05:13:31 +03:00
|
|
|
nsFilePicker::ClosePickerIfNeeded()
|
2011-12-15 01:22:28 +04:00
|
|
|
{
|
|
|
|
if (!mParentWidget || !mDlgWnd)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
nsWindow *win = static_cast<nsWindow *>(mParentWidget.get());
|
2016-12-23 05:13:31 +03:00
|
|
|
if (IsWindow(mDlgWnd) && IsWindowVisible(mDlgWnd) && win->DestroyCalled()) {
|
2013-12-03 19:12:57 +04:00
|
|
|
wchar_t className[64];
|
2011-12-15 01:22:28 +04:00
|
|
|
// Make sure we have the right window
|
2016-12-23 05:13:31 +03:00
|
|
|
if (GetClassNameW(mDlgWnd, className, mozilla::ArrayLength(className)) &&
|
2011-12-15 01:22:28 +04:00
|
|
|
!wcscmp(className, L"#32770") &&
|
2016-12-23 05:13:31 +03:00
|
|
|
DestroyWindow(mDlgWnd)) {
|
2013-10-08 22:48:02 +04:00
|
|
|
mDlgWnd = nullptr;
|
2011-12-15 01:22:28 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsFilePicker::PickerCallbackTimerFunc(nsITimer *aTimer, void *aCtx)
|
|
|
|
{
|
|
|
|
nsFilePicker* picker = (nsFilePicker*)aCtx;
|
2016-12-23 05:13:31 +03:00
|
|
|
if (picker->ClosePickerIfNeeded()) {
|
2011-12-15 01:22:28 +04:00
|
|
|
aTimer->Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsFilePicker::SetDialogHandle(HWND aWnd)
|
|
|
|
{
|
|
|
|
if (!aWnd || mDlgWnd)
|
|
|
|
return;
|
|
|
|
mDlgWnd = aWnd;
|
|
|
|
}
|
|
|
|
|
2011-12-15 01:22:28 +04:00
|
|
|
/*
|
|
|
|
* Folder picker invocation
|
|
|
|
*/
|
|
|
|
|
2012-07-03 18:38:09 +04:00
|
|
|
/*
|
2016-12-23 05:13:31 +03:00
|
|
|
* Show a folder picker.
|
|
|
|
*
|
2012-07-03 18:38:09 +04:00
|
|
|
* @param aInitialDir The initial directory, the last used directory will be
|
|
|
|
* used if left blank.
|
|
|
|
* @return true if a file was selected successfully.
|
|
|
|
*/
|
2011-12-15 01:22:28 +04:00
|
|
|
bool
|
2016-12-23 05:13:31 +03:00
|
|
|
nsFilePicker::ShowFolderPicker(const nsString& aInitialDir)
|
2011-12-15 01:22:28 +04:00
|
|
|
{
|
2016-11-15 21:36:01 +03:00
|
|
|
if (!IsWin8OrLater()) {
|
|
|
|
// Some Windows 7 users are experiencing a race condition when some dlls
|
|
|
|
// that are loaded by the file picker cause a crash while attempting to shut
|
|
|
|
// down the COM multithreaded apartment. By instantiating EnsureMTA, we hold
|
|
|
|
// an additional reference to the MTA that should prevent this race, since
|
|
|
|
// the MTA will remain alive until shutdown.
|
|
|
|
EnsureMTA ensureMTA;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IFileOpenDialog> dialog;
|
2013-10-08 22:48:02 +04:00
|
|
|
if (FAILED(CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC,
|
2011-12-15 01:22:28 +04:00
|
|
|
IID_IFileOpenDialog,
|
2012-07-03 18:38:09 +04:00
|
|
|
getter_AddRefs(dialog)))) {
|
2011-12-15 01:22:28 +04:00
|
|
|
return false;
|
2012-07-03 18:38:09 +04:00
|
|
|
}
|
2011-12-15 01:22:28 +04:00
|
|
|
|
|
|
|
// hook up event callbacks
|
|
|
|
dialog->Advise(this, &mFDECookie);
|
|
|
|
|
|
|
|
// options
|
2011-12-15 01:22:28 +04:00
|
|
|
FILEOPENDIALOGOPTIONS fos = FOS_PICKFOLDERS;
|
2011-12-15 01:22:28 +04:00
|
|
|
dialog->SetOptions(fos);
|
|
|
|
|
|
|
|
// initial strings
|
|
|
|
dialog->SetTitle(mTitle.get());
|
2016-08-24 02:07:21 +03:00
|
|
|
|
|
|
|
if (!mOkButtonLabel.IsEmpty()) {
|
|
|
|
dialog->SetOkButtonLabel(mOkButtonLabel.get());
|
|
|
|
}
|
|
|
|
|
2012-02-17 04:36:04 +04:00
|
|
|
if (!aInitialDir.IsEmpty()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IShellItem> folder;
|
2012-01-11 06:09:41 +04:00
|
|
|
if (SUCCEEDED(
|
2017-02-10 06:06:23 +03:00
|
|
|
SHCreateItemFromParsingName(aInitialDir.get(), nullptr,
|
|
|
|
IID_IShellItem,
|
|
|
|
getter_AddRefs(folder)))) {
|
2011-12-15 01:22:28 +04:00
|
|
|
dialog->SetFolder(folder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoDestroyTmpWindow adtw((HWND)(mParentWidget.get() ?
|
2013-10-08 22:48:02 +04:00
|
|
|
mParentWidget->GetNativeData(NS_NATIVE_TMP_WINDOW) : nullptr));
|
2011-12-15 01:22:28 +04:00
|
|
|
|
|
|
|
// display
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IShellItem> item;
|
2011-12-15 01:22:28 +04:00
|
|
|
if (FAILED(dialog->Show(adtw.get())) ||
|
|
|
|
FAILED(dialog->GetResult(getter_AddRefs(item))) ||
|
|
|
|
!item) {
|
|
|
|
dialog->Unadvise(mFDECookie);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
dialog->Unadvise(mFDECookie);
|
2011-12-15 01:22:28 +04:00
|
|
|
|
2012-02-17 04:36:04 +04:00
|
|
|
// results
|
|
|
|
|
|
|
|
// If the user chose a Win7 Library, resolve to the library's
|
|
|
|
// default save folder.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IShellItem> folderPath;
|
|
|
|
RefPtr<IShellLibrary> shellLib;
|
2013-10-08 22:48:02 +04:00
|
|
|
CoCreateInstance(CLSID_ShellLibrary, nullptr, CLSCTX_INPROC,
|
|
|
|
IID_IShellLibrary, getter_AddRefs(shellLib));
|
2012-02-17 04:36:04 +04:00
|
|
|
if (shellLib &&
|
|
|
|
SUCCEEDED(shellLib->LoadLibraryFromItem(item, STGM_READ)) &&
|
|
|
|
SUCCEEDED(shellLib->GetDefaultSaveFolder(DSFT_DETECT, IID_IShellItem,
|
|
|
|
getter_AddRefs(folderPath)))) {
|
|
|
|
item.swap(folderPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the folder's file system path
|
|
|
|
return WinUtils::GetShellItemPath(item, mUnicodeFile);
|
|
|
|
}
|
2011-12-15 01:22:28 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* File open and save picker invocation
|
|
|
|
*/
|
|
|
|
|
2012-07-03 18:38:09 +04:00
|
|
|
/*
|
2016-12-23 05:13:31 +03:00
|
|
|
* Show a file picker.
|
|
|
|
*
|
2012-07-03 18:38:09 +04:00
|
|
|
* @param aInitialDir The initial directory, the last used directory will be
|
|
|
|
* used if left blank.
|
|
|
|
* @return true if a file was selected successfully.
|
|
|
|
*/
|
2011-12-15 01:22:28 +04:00
|
|
|
bool
|
2016-12-23 05:13:31 +03:00
|
|
|
nsFilePicker::ShowFilePicker(const nsString& aInitialDir)
|
2011-12-15 01:22:28 +04:00
|
|
|
{
|
Bug 1375392 - Tweak the PROFILER_LABEL* macros. r=mstange.
This patch makes the following changes to the macros.
- Removes PROFILER_LABEL_FUNC. It's only suitable for use in functions outside
classes, due to PROFILER_FUNCTION_NAME not getting class names, and it was
mostly misused.
- Removes PROFILER_FUNCTION_NAME. It's no longer used, and __func__ is
universally available now anyway.
- Combines the first two string literal arguments of PROFILER_LABEL and
PROFILER_LABEL_DYNAMIC into a single argument. There was no good reason for
them to be separate, and it forced a '::' in the label, which isn't always
appropriate. Also, the meaning of the "name_space" argument was interpreted
in an interesting variety of ways.
- Adds an "AUTO_" prefix to PROFILER_LABEL and PROFILER_LABEL_DYNAMIC, to make
it clearer they construct RAII objects rather than just being function calls.
(I myself have screwed up the scoping because of this in the past.)
- Fills in the 'js::ProfileEntry::Category::' qualifier within the macro, so
the caller doesn't need to. This makes a *lot* more of the uses fit onto a
single line.
The patch also makes the following changes to the macro uses (beyond those
required by the changes described above).
- Fixes a bunch of labels that had gotten out of sync with the name of the
class and/or function that encloses them.
- Removes a useless PROFILER_LABEL use within a trivial scope in
EventStateManager::DispatchMouseOrPointerEvent(). It clearly wasn't serving
any useful purpose. It also serves as extra evidence that the AUTO_ prefix is
a good idea.
- Tweaks DecodePool::SyncRunIf{Preferred,Possible} so that the labelling is
done within them, instead of at their callsites, because that's a more
standard way of doing things.
--HG--
extra : rebase_source : 318d1bc6fc1425a94aacbf489dd46e4f83211de4
2017-06-22 10:08:53 +03:00
|
|
|
AUTO_PROFILER_LABEL("nsFilePicker::ShowFilePicker", OTHER);
|
2016-11-15 21:36:01 +03:00
|
|
|
|
|
|
|
if (!IsWin8OrLater()) {
|
|
|
|
// Some Windows 7 users are experiencing a race condition when some dlls
|
|
|
|
// that are loaded by the file picker cause a crash while attempting to shut
|
|
|
|
// down the COM multithreaded apartment. By instantiating EnsureMTA, we hold
|
|
|
|
// an additional reference to the MTA that should prevent this race, since
|
|
|
|
// the MTA will remain alive until shutdown.
|
|
|
|
EnsureMTA ensureMTA;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IFileDialog> dialog;
|
2011-12-15 01:22:28 +04:00
|
|
|
if (mMode != modeSave) {
|
2013-10-08 22:48:02 +04:00
|
|
|
if (FAILED(CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC,
|
2011-12-15 01:22:28 +04:00
|
|
|
IID_IFileOpenDialog,
|
2012-07-03 18:38:09 +04:00
|
|
|
getter_AddRefs(dialog)))) {
|
2011-12-15 01:22:28 +04:00
|
|
|
return false;
|
2012-07-03 18:38:09 +04:00
|
|
|
}
|
2011-12-15 01:22:28 +04:00
|
|
|
} else {
|
2013-10-08 22:48:02 +04:00
|
|
|
if (FAILED(CoCreateInstance(CLSID_FileSaveDialog, nullptr, CLSCTX_INPROC,
|
2011-12-15 01:22:28 +04:00
|
|
|
IID_IFileSaveDialog,
|
2012-07-03 18:38:09 +04:00
|
|
|
getter_AddRefs(dialog)))) {
|
2011-12-15 01:22:28 +04:00
|
|
|
return false;
|
2012-07-03 18:38:09 +04:00
|
|
|
}
|
2011-12-15 01:22:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// hook up event callbacks
|
|
|
|
dialog->Advise(this, &mFDECookie);
|
|
|
|
|
|
|
|
// options
|
|
|
|
|
|
|
|
FILEOPENDIALOGOPTIONS fos = 0;
|
2011-12-21 20:27:23 +04:00
|
|
|
fos |= FOS_SHAREAWARE | FOS_OVERWRITEPROMPT |
|
2012-01-18 17:31:09 +04:00
|
|
|
FOS_FORCEFILESYSTEM;
|
2011-12-15 01:22:28 +04:00
|
|
|
|
|
|
|
// Handle add to recent docs settings
|
|
|
|
if (IsPrivacyModeEnabled() || !mAddToRecentDocs) {
|
|
|
|
fos |= FOS_DONTADDTORECENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Msdn claims FOS_NOCHANGEDIR is not needed. We'll add this
|
|
|
|
// just in case.
|
|
|
|
AutoRestoreWorkingPath arw;
|
|
|
|
|
|
|
|
// mode specific
|
|
|
|
switch(mMode) {
|
|
|
|
case modeOpen:
|
|
|
|
fos |= FOS_FILEMUSTEXIST;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case modeOpenMultiple:
|
|
|
|
fos |= FOS_FILEMUSTEXIST | FOS_ALLOWMULTISELECT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case modeSave:
|
2012-01-18 17:31:09 +04:00
|
|
|
fos |= FOS_NOREADONLYRETURN;
|
2011-12-15 01:22:28 +04:00
|
|
|
// Don't follow shortcuts when saving a shortcut, this can be used
|
|
|
|
// to trick users (bug 271732)
|
|
|
|
if (IsDefaultPathLink())
|
|
|
|
fos |= FOS_NODEREFERENCELINKS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog->SetOptions(fos);
|
|
|
|
|
|
|
|
// initial strings
|
|
|
|
|
|
|
|
// title
|
|
|
|
dialog->SetTitle(mTitle.get());
|
|
|
|
|
|
|
|
// default filename
|
|
|
|
if (!mDefaultFilename.IsEmpty()) {
|
|
|
|
dialog->SetFileName(mDefaultFilename.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
// default extension to append to new files
|
|
|
|
if (!mDefaultExtension.IsEmpty()) {
|
|
|
|
dialog->SetDefaultExtension(mDefaultExtension.get());
|
|
|
|
} else if (IsDefaultPathHtml()) {
|
2017-03-14 05:26:27 +03:00
|
|
|
dialog->SetDefaultExtension(L"html");
|
2011-12-15 01:22:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// initial location
|
2012-02-17 04:36:04 +04:00
|
|
|
if (!aInitialDir.IsEmpty()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IShellItem> folder;
|
2012-01-11 06:09:41 +04:00
|
|
|
if (SUCCEEDED(
|
2017-02-10 06:06:23 +03:00
|
|
|
SHCreateItemFromParsingName(aInitialDir.get(), nullptr,
|
|
|
|
IID_IShellItem,
|
|
|
|
getter_AddRefs(folder)))) {
|
2011-12-15 01:22:28 +04:00
|
|
|
dialog->SetFolder(folder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter types and the default index
|
|
|
|
if (!mComFilterList.IsEmpty()) {
|
|
|
|
dialog->SetFileTypes(mComFilterList.Length(), mComFilterList.get());
|
|
|
|
dialog->SetFileTypeIndex(mSelectedType);
|
|
|
|
}
|
|
|
|
|
|
|
|
// display
|
|
|
|
|
2011-12-15 01:22:28 +04:00
|
|
|
{
|
|
|
|
AutoDestroyTmpWindow adtw((HWND)(mParentWidget.get() ?
|
2013-10-08 22:48:02 +04:00
|
|
|
mParentWidget->GetNativeData(NS_NATIVE_TMP_WINDOW) : nullptr));
|
2017-06-29 22:13:25 +03:00
|
|
|
AutoTimerCallbackCancel atcc(this, PickerCallbackTimerFunc,
|
|
|
|
"nsFilePicker::PickerCallbackTimerFunc");
|
2011-12-15 01:22:28 +04:00
|
|
|
AutoWidgetPickerState awps(mParentWidget);
|
2011-12-15 01:22:28 +04:00
|
|
|
|
2011-12-15 01:22:28 +04:00
|
|
|
if (FAILED(dialog->Show(adtw.get()))) {
|
|
|
|
dialog->Unadvise(mFDECookie);
|
|
|
|
return false;
|
|
|
|
}
|
2011-12-15 01:22:28 +04:00
|
|
|
dialog->Unadvise(mFDECookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
// results
|
|
|
|
|
2012-01-03 18:59:29 +04:00
|
|
|
// Remember what filter type the user selected
|
|
|
|
UINT filterIdxResult;
|
|
|
|
if (SUCCEEDED(dialog->GetFileTypeIndex(&filterIdxResult))) {
|
2012-08-22 19:56:38 +04:00
|
|
|
mSelectedType = (int16_t)filterIdxResult;
|
2012-01-03 18:59:29 +04:00
|
|
|
}
|
|
|
|
|
2011-12-15 01:22:28 +04:00
|
|
|
// single selection
|
|
|
|
if (mMode != modeOpenMultiple) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IShellItem> item;
|
2012-02-17 04:36:04 +04:00
|
|
|
if (FAILED(dialog->GetResult(getter_AddRefs(item))) || !item)
|
2011-12-15 01:22:28 +04:00
|
|
|
return false;
|
2012-02-17 04:36:04 +04:00
|
|
|
return WinUtils::GetShellItemPath(item, mUnicodeFile);
|
2011-12-15 01:22:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// multiple selection
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IFileOpenDialog> openDlg;
|
2011-12-15 01:22:28 +04:00
|
|
|
dialog->QueryInterface(IID_IFileOpenDialog, getter_AddRefs(openDlg));
|
|
|
|
if (!openDlg) {
|
|
|
|
// should not happen
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IShellItemArray> items;
|
2011-12-15 01:22:28 +04:00
|
|
|
if (FAILED(openDlg->GetResults(getter_AddRefs(items))) || !items) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD count = 0;
|
|
|
|
items->GetCount(&count);
|
|
|
|
for (unsigned int idx = 0; idx < count; idx++) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IShellItem> item;
|
2012-02-17 04:36:04 +04:00
|
|
|
nsAutoString str;
|
2011-12-15 01:22:28 +04:00
|
|
|
if (SUCCEEDED(items->GetItemAt(idx, getter_AddRefs(item)))) {
|
2012-02-17 04:36:04 +04:00
|
|
|
if (!WinUtils::GetShellItemPath(item, str))
|
2011-12-15 01:22:28 +04:00
|
|
|
continue;
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> file = do_CreateInstance("@mozilla.org/file/local;1");
|
2012-02-17 04:36:04 +04:00
|
|
|
if (file && NS_SUCCEEDED(file->InitWithPath(str)))
|
2011-12-15 01:22:28 +04:00
|
|
|
mFiles.AppendObject(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2011-12-15 01:22:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIFilePicker impl.
|
|
|
|
|
2017-08-06 07:15:31 +03:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsFilePicker::ShowW(int16_t *aReturnVal)
|
2011-12-15 01:22:16 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aReturnVal);
|
|
|
|
|
|
|
|
*aReturnVal = returnCancel;
|
|
|
|
|
2012-10-28 08:24:02 +04:00
|
|
|
AutoSuppressEvents supress(mParentWidget);
|
|
|
|
|
2011-12-15 01:22:16 +04:00
|
|
|
nsAutoString initialDir;
|
|
|
|
if (mDisplayDirectory)
|
|
|
|
mDisplayDirectory->GetPath(initialDir);
|
|
|
|
|
|
|
|
// If no display directory, re-use the last one.
|
|
|
|
if(initialDir.IsEmpty()) {
|
|
|
|
// Allocate copy of last used dir.
|
|
|
|
initialDir = mLastUsedUnicodeDirectory;
|
2003-02-24 23:15:48 +03:00
|
|
|
}
|
|
|
|
|
2011-12-16 19:10:16 +04:00
|
|
|
// Clear previous file selections
|
2011-12-15 01:22:16 +04:00
|
|
|
mUnicodeFile.Truncate();
|
2011-12-16 19:10:16 +04:00
|
|
|
mFiles.Clear();
|
2011-12-15 01:22:16 +04:00
|
|
|
|
2016-07-02 01:42:32 +03:00
|
|
|
// On Win10, the picker doesn't support per-monitor DPI, so we open it
|
|
|
|
// with our context set temporarily to system-dpi-aware
|
|
|
|
WinUtils::AutoSystemDpiAware dpiAwareness;
|
|
|
|
|
2016-12-23 05:13:31 +03:00
|
|
|
bool result = false;
|
2012-07-03 18:38:09 +04:00
|
|
|
if (mMode == modeGetFolder) {
|
2016-12-23 05:13:31 +03:00
|
|
|
result = ShowFolderPicker(initialDir);
|
2012-07-03 18:38:09 +04:00
|
|
|
} else {
|
2016-12-23 05:13:31 +03:00
|
|
|
result = ShowFilePicker(initialDir);
|
2012-07-03 18:38:09 +04:00
|
|
|
}
|
2011-12-15 01:22:16 +04:00
|
|
|
|
|
|
|
// exit, and return returnCancel in aReturnVal
|
|
|
|
if (!result)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
RememberLastUsedDirectory();
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t retValue = returnOK;
|
2011-12-15 01:22:16 +04:00
|
|
|
if (mMode == modeSave) {
|
|
|
|
// Windows does not return resultReplace, we must check if file
|
|
|
|
// already exists.
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> file(do_CreateInstance("@mozilla.org/file/local;1"));
|
2011-12-15 01:22:16 +04:00
|
|
|
bool flag = false;
|
|
|
|
if (file && NS_SUCCEEDED(file->InitWithPath(mUnicodeFile)) &&
|
|
|
|
NS_SUCCEEDED(file->Exists(&flag)) && flag) {
|
|
|
|
retValue = returnReplace;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aReturnVal = retValue;
|
2002-07-03 04:55:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-08-06 07:15:31 +03:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsFilePicker::Show(int16_t *aReturnVal)
|
2000-03-22 10:43:43 +03:00
|
|
|
{
|
2002-07-03 04:55:47 +04:00
|
|
|
return ShowW(aReturnVal);
|
|
|
|
}
|
2000-03-23 10:41:17 +03:00
|
|
|
|
2011-12-15 01:22:16 +04:00
|
|
|
NS_IMETHODIMP
|
2012-06-06 06:08:30 +04:00
|
|
|
nsFilePicker::GetFile(nsIFile **aFile)
|
2000-03-22 10:43:43 +03:00
|
|
|
{
|
2000-04-11 02:08:19 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aFile);
|
2012-07-30 18:20:58 +04:00
|
|
|
*aFile = nullptr;
|
2000-04-11 02:08:19 +04:00
|
|
|
|
2002-07-03 04:55:47 +04:00
|
|
|
if (mUnicodeFile.IsEmpty())
|
|
|
|
return NS_OK;
|
2000-03-22 10:43:43 +03:00
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> file(do_CreateInstance("@mozilla.org/file/local;1"));
|
2000-03-22 10:43:43 +03:00
|
|
|
|
|
|
|
NS_ENSURE_TRUE(file, NS_ERROR_FAILURE);
|
|
|
|
|
2002-07-03 04:55:47 +04:00
|
|
|
file->InitWithPath(mUnicodeFile);
|
2000-03-22 10:43:43 +03:00
|
|
|
|
2000-03-22 13:19:48 +03:00
|
|
|
NS_ADDREF(*aFile = file);
|
2000-03-22 10:43:43 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-15 01:22:16 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePicker::GetFileURL(nsIURI **aFileURL)
|
2000-04-05 03:50:41 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aFileURL = nullptr;
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> file;
|
2008-02-26 19:12:17 +03:00
|
|
|
nsresult rv = GetFile(getter_AddRefs(file));
|
|
|
|
if (!file)
|
|
|
|
return rv;
|
2000-04-05 03:50:41 +04:00
|
|
|
|
2008-02-26 19:12:17 +03:00
|
|
|
return NS_NewFileURI(aFileURL, file);
|
2000-04-05 03:50:41 +04:00
|
|
|
}
|
|
|
|
|
2011-12-15 01:22:16 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePicker::GetFiles(nsISimpleEnumerator **aFiles)
|
2002-09-07 09:23:45 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aFiles);
|
|
|
|
return NS_NewArrayEnumerator(aFiles, mFiles);
|
|
|
|
}
|
|
|
|
|
2000-03-22 10:43:43 +03:00
|
|
|
// Get the file + path
|
2011-12-15 01:22:16 +04:00
|
|
|
NS_IMETHODIMP
|
2012-09-03 19:25:08 +04:00
|
|
|
nsBaseWinFilePicker::SetDefaultString(const nsAString& aString)
|
2000-03-22 10:43:43 +03:00
|
|
|
{
|
2011-12-15 01:22:28 +04:00
|
|
|
mDefaultFilePath = aString;
|
2002-02-12 06:57:39 +03:00
|
|
|
|
2011-12-15 01:22:28 +04:00
|
|
|
// First, make sure the file name is not too long.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t nameLength;
|
|
|
|
int32_t nameIndex = mDefaultFilePath.RFind("\\");
|
2002-02-12 06:57:39 +03:00
|
|
|
if (nameIndex == kNotFound)
|
|
|
|
nameIndex = 0;
|
|
|
|
else
|
|
|
|
nameIndex ++;
|
2011-12-15 01:22:28 +04:00
|
|
|
nameLength = mDefaultFilePath.Length() - nameIndex;
|
|
|
|
mDefaultFilename.Assign(Substring(mDefaultFilePath, nameIndex));
|
2002-02-12 06:57:39 +03:00
|
|
|
|
2009-01-05 21:17:44 +03:00
|
|
|
if (nameLength > MAX_PATH) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t extIndex = mDefaultFilePath.RFind(".");
|
2002-02-12 06:57:39 +03:00
|
|
|
if (extIndex == kNotFound)
|
2011-12-15 01:22:28 +04:00
|
|
|
extIndex = mDefaultFilePath.Length();
|
2002-02-12 06:57:39 +03:00
|
|
|
|
2011-12-15 01:22:28 +04:00
|
|
|
// Let's try to shave the needed characters from the name part.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t charsToRemove = nameLength - MAX_PATH;
|
2002-02-12 06:57:39 +03:00
|
|
|
if (extIndex - nameIndex >= charsToRemove) {
|
2011-12-15 01:22:28 +04:00
|
|
|
mDefaultFilePath.Cut(extIndex - charsToRemove, charsToRemove);
|
2002-02-12 06:57:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-15 01:22:28 +04:00
|
|
|
// Then, we need to replace illegal characters. At this stage, we cannot
|
|
|
|
// replace the backslash as the string might represent a file path.
|
|
|
|
mDefaultFilePath.ReplaceChar(FILE_ILLEGAL_CHARACTERS, '-');
|
2013-05-13 10:58:10 +04:00
|
|
|
mDefaultFilename.ReplaceChar(FILE_ILLEGAL_CHARACTERS, '-');
|
2002-02-12 06:57:39 +03:00
|
|
|
|
2000-03-22 10:43:43 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-15 01:22:16 +04:00
|
|
|
NS_IMETHODIMP
|
2012-09-03 19:25:08 +04:00
|
|
|
nsBaseWinFilePicker::GetDefaultString(nsAString& aString)
|
2000-03-22 10:43:43 +03:00
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2002-02-20 10:50:52 +03:00
|
|
|
// The default extension to use for files
|
2011-12-15 01:22:16 +04:00
|
|
|
NS_IMETHODIMP
|
2012-09-03 19:25:08 +04:00
|
|
|
nsBaseWinFilePicker::GetDefaultExtension(nsAString& aExtension)
|
2002-02-20 10:50:52 +03:00
|
|
|
{
|
2004-04-22 03:38:22 +04:00
|
|
|
aExtension = mDefaultExtension;
|
2002-02-20 10:50:52 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-15 01:22:16 +04:00
|
|
|
NS_IMETHODIMP
|
2012-09-03 19:25:08 +04:00
|
|
|
nsBaseWinFilePicker::SetDefaultExtension(const nsAString& aExtension)
|
2002-02-20 10:50:52 +03:00
|
|
|
{
|
|
|
|
mDefaultExtension = aExtension;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-12-12 07:17:26 +03:00
|
|
|
// Set the filter index
|
2011-12-15 01:22:16 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsFilePicker::GetFilterIndex(int32_t *aFilterIndex)
|
2001-12-12 07:17:26 +03:00
|
|
|
{
|
|
|
|
// Windows' filter index is 1-based, we use a 0-based system.
|
|
|
|
*aFilterIndex = mSelectedType - 1;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-15 01:22:16 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsFilePicker::SetFilterIndex(int32_t aFilterIndex)
|
2001-12-12 07:17:26 +03:00
|
|
|
{
|
|
|
|
// Windows' filter index is 1-based, we use a 0-based system.
|
|
|
|
mSelectedType = aFilterIndex + 1;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-15 01:22:16 +04:00
|
|
|
void
|
|
|
|
nsFilePicker::InitNative(nsIWidget *aParent,
|
2013-07-22 13:12:32 +04:00
|
|
|
const nsAString& aTitle)
|
2000-03-22 10:43:43 +03:00
|
|
|
{
|
2003-02-24 23:15:48 +03:00
|
|
|
mParentWidget = aParent;
|
2004-02-10 13:15:37 +03:00
|
|
|
mTitle.Assign(aTitle);
|
2000-03-22 13:26:22 +03:00
|
|
|
}
|
2000-08-25 06:17:53 +04:00
|
|
|
|
2011-12-15 01:22:28 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePicker::AppendFilter(const nsAString& aTitle, const nsAString& aFilter)
|
|
|
|
{
|
2016-12-23 05:13:31 +03:00
|
|
|
mComFilterList.Append(aTitle, aFilter);
|
2000-08-25 06:17:53 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-08-19 21:55:03 +04:00
|
|
|
|
2011-12-15 01:22:16 +04:00
|
|
|
void
|
|
|
|
nsFilePicker::RememberLastUsedDirectory()
|
2011-08-19 21:55:03 +04:00
|
|
|
{
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> file(do_CreateInstance("@mozilla.org/file/local;1"));
|
2011-12-15 01:22:16 +04:00
|
|
|
if (!file || NS_FAILED(file->InitWithPath(mUnicodeFile))) {
|
|
|
|
NS_WARNING("RememberLastUsedDirectory failed to init file path.");
|
|
|
|
return;
|
2011-08-19 21:55:03 +04:00
|
|
|
}
|
2011-12-15 01:22:16 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> dir;
|
|
|
|
nsAutoString newDir;
|
|
|
|
if (NS_FAILED(file->GetParent(getter_AddRefs(dir))) ||
|
|
|
|
!(mDisplayDirectory = do_QueryInterface(dir)) ||
|
|
|
|
NS_FAILED(mDisplayDirectory->GetPath(newDir)) ||
|
|
|
|
newDir.IsEmpty()) {
|
|
|
|
NS_WARNING("RememberLastUsedDirectory failed to get parent directory.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mLastUsedUnicodeDirectory) {
|
2015-04-01 08:29:55 +03:00
|
|
|
free(mLastUsedUnicodeDirectory);
|
2012-07-30 18:20:58 +04:00
|
|
|
mLastUsedUnicodeDirectory = nullptr;
|
2011-12-15 01:22:16 +04:00
|
|
|
}
|
|
|
|
mLastUsedUnicodeDirectory = ToNewUnicode(newDir);
|
2011-08-19 21:55:03 +04:00
|
|
|
}
|
|
|
|
|
2011-12-15 01:22:16 +04:00
|
|
|
bool
|
|
|
|
nsFilePicker::IsPrivacyModeEnabled()
|
2011-08-19 21:55:03 +04:00
|
|
|
{
|
2012-04-09 23:53:46 +04:00
|
|
|
return mLoadContext && mLoadContext->UsePrivateBrowsing();
|
2011-12-15 01:22:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsFilePicker::IsDefaultPathLink()
|
|
|
|
{
|
2011-12-15 01:22:28 +04:00
|
|
|
NS_ConvertUTF16toUTF8 ext(mDefaultFilePath);
|
2011-12-15 01:22:16 +04:00
|
|
|
ext.Trim(" .", false, true); // watch out for trailing space and dots
|
|
|
|
ToLowerCase(ext);
|
|
|
|
if (StringEndsWith(ext, NS_LITERAL_CSTRING(".lnk")) ||
|
|
|
|
StringEndsWith(ext, NS_LITERAL_CSTRING(".pif")) ||
|
|
|
|
StringEndsWith(ext, NS_LITERAL_CSTRING(".url")))
|
|
|
|
return true;
|
|
|
|
return false;
|
2011-08-19 21:55:03 +04:00
|
|
|
}
|
|
|
|
|
2011-12-15 01:22:16 +04:00
|
|
|
bool
|
|
|
|
nsFilePicker::IsDefaultPathHtml()
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t extIndex = mDefaultFilePath.RFind(".");
|
2011-12-15 01:22:16 +04:00
|
|
|
if (extIndex >= 0) {
|
|
|
|
nsAutoString ext;
|
2011-12-15 01:22:28 +04:00
|
|
|
mDefaultFilePath.Right(ext, mDefaultFilePath.Length() - extIndex);
|
2011-12-15 01:22:16 +04:00
|
|
|
if (ext.LowerCaseEqualsLiteral(".htm") ||
|
|
|
|
ext.LowerCaseEqualsLiteral(".html") ||
|
|
|
|
ext.LowerCaseEqualsLiteral(".shtml"))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2011-12-15 01:22:28 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
nsFilePicker::ComDlgFilterSpec::Append(const nsAString& aTitle, const nsAString& aFilter)
|
|
|
|
{
|
2012-01-05 00:30:57 +04:00
|
|
|
COMDLG_FILTERSPEC* pSpecForward = mSpecList.AppendElement();
|
|
|
|
if (!pSpecForward) {
|
2011-12-15 01:22:28 +04:00
|
|
|
NS_WARNING("mSpecList realloc failed.");
|
|
|
|
return;
|
|
|
|
}
|
2012-01-05 00:30:57 +04:00
|
|
|
memset(pSpecForward, 0, sizeof(*pSpecForward));
|
2011-12-15 01:22:28 +04:00
|
|
|
nsString* pStr = mStrings.AppendElement(aTitle);
|
|
|
|
if (!pStr) {
|
|
|
|
NS_WARNING("mStrings.AppendElement failed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pSpecForward->pszName = pStr->get();
|
|
|
|
pStr = mStrings.AppendElement(aFilter);
|
|
|
|
if (!pStr) {
|
|
|
|
NS_WARNING("mStrings.AppendElement failed.");
|
|
|
|
return;
|
|
|
|
}
|
2012-01-05 00:30:57 +04:00
|
|
|
if (aFilter.EqualsLiteral("..apps"))
|
|
|
|
pStr->AssignLiteral("*.exe;*.com");
|
|
|
|
else {
|
|
|
|
pStr->StripWhitespace();
|
|
|
|
if (pStr->EqualsLiteral("*"))
|
|
|
|
pStr->AppendLiteral(".*");
|
|
|
|
}
|
2011-12-15 01:22:28 +04:00
|
|
|
pSpecForward->pszSpec = pStr->get();
|
|
|
|
}
|