2008-04-19 15:44:21 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 11:12:37 +00: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/. */
|
2010-03-04 21:51:42 +00:00
|
|
|
|
2011-09-14 13:16:28 +00:00
|
|
|
#include <QStringList>
|
|
|
|
#include <QGraphicsWidget>
|
|
|
|
#include <QGraphicsScene>
|
|
|
|
#include <QGraphicsView>
|
|
|
|
#include <QTemporaryFile>
|
|
|
|
|
|
|
|
#ifdef MOZ_ENABLE_CONTENTMANAGER
|
|
|
|
#include "nsMFilePicker.h"
|
|
|
|
#define MozFileDialog MeegoFileDialog
|
|
|
|
#else
|
|
|
|
#include <QFileDialog>
|
|
|
|
#define MozFileDialog QFileDialog
|
|
|
|
#endif
|
2008-04-19 15:44:21 +00:00
|
|
|
|
2011-09-14 13:16:28 +00:00
|
|
|
|
|
|
|
#include "nsFilePicker.h"
|
2008-04-19 15:44:21 +00:00
|
|
|
#include "nsNetUtil.h"
|
2009-04-01 21:59:02 +00:00
|
|
|
#include "nsIWidget.h"
|
2010-08-26 15:18:06 +00:00
|
|
|
#include "prlog.h"
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2012-07-30 14:20:58 +00:00
|
|
|
static PRLogModuleInfo* sFilePickerLog = nullptr;
|
2010-08-26 15:18:06 +00:00
|
|
|
#endif
|
2008-04-19 15:44:21 +00:00
|
|
|
|
2011-09-14 13:16:28 +00:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
|
|
|
|
//-----------------------------
|
|
|
|
|
2008-04-19 15:44:21 +00:00
|
|
|
/* Implementation file */
|
|
|
|
NS_IMPL_ISUPPORTS1(nsFilePicker, nsIFilePicker)
|
|
|
|
|
|
|
|
nsFilePicker::nsFilePicker()
|
|
|
|
{
|
2010-08-26 15:18:06 +00:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!sFilePickerLog)
|
|
|
|
sFilePickerLog = PR_NewLogModule("nsQtFilePicker");
|
|
|
|
#endif
|
2008-04-19 15:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsFilePicker::~nsFilePicker()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsFilePicker::Init(nsIDOMWindow* parent, const nsAString& title, int16_t mode)
|
2008-04-19 15:44:21 +00:00
|
|
|
{
|
|
|
|
return nsBaseFilePicker::Init(parent, title, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void appendFilters (in long filterMask); */
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsFilePicker::AppendFilters(int32_t filterMask)
|
2008-04-19 15:44:21 +00:00
|
|
|
{
|
|
|
|
return nsBaseFilePicker::AppendFilters(filterMask);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void appendFilter (in AString title, in AString filter); */
|
|
|
|
NS_IMETHODIMP
|
2011-09-14 13:16:28 +00:00
|
|
|
nsFilePicker::AppendFilter(const nsAString& aTitle, const nsAString& aFilter)
|
2008-04-19 15:44:21 +00:00
|
|
|
{
|
|
|
|
if (aFilter.Equals(NS_LITERAL_STRING("..apps"))) {
|
|
|
|
// No platform specific thing we can do here, really....
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString filter, name;
|
2008-04-19 15:44:21 +00:00
|
|
|
CopyUTF16toUTF8(aFilter, filter);
|
|
|
|
CopyUTF16toUTF8(aTitle, name);
|
|
|
|
|
2009-01-22 04:15:34 +00:00
|
|
|
mFilters.AppendElement(filter);
|
|
|
|
mFilterNames.AppendElement(name);
|
2008-04-19 15:44:21 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* attribute AString defaultString; */
|
|
|
|
NS_IMETHODIMP
|
2011-09-14 13:16:28 +00:00
|
|
|
nsFilePicker::GetDefaultString(nsAString& aDefaultString)
|
2008-04-19 15:44:21 +00:00
|
|
|
{
|
2010-03-17 06:27:40 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2008-04-19 15:44:21 +00:00
|
|
|
}
|
2010-03-17 06:27:40 +00:00
|
|
|
|
2008-04-19 15:44:21 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-14 13:16:28 +00:00
|
|
|
nsFilePicker::SetDefaultString(const nsAString& aDefaultString)
|
2008-04-19 15:44:21 +00:00
|
|
|
{
|
2010-03-17 06:27:40 +00:00
|
|
|
mDefault = aDefaultString;
|
|
|
|
|
|
|
|
return NS_OK;
|
2008-04-19 15:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* attribute AString defaultExtension; */
|
|
|
|
NS_IMETHODIMP
|
2011-09-14 13:16:28 +00:00
|
|
|
nsFilePicker::GetDefaultExtension(nsAString& aDefaultExtension)
|
2008-04-19 15:44:21 +00:00
|
|
|
{
|
|
|
|
aDefaultExtension = mDefaultExtension;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-09-14 13:16:28 +00:00
|
|
|
|
2008-04-19 15:44:21 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-14 13:16:28 +00:00
|
|
|
nsFilePicker::SetDefaultExtension(const nsAString& aDefaultExtension)
|
2008-04-19 15:44:21 +00:00
|
|
|
{
|
|
|
|
mDefaultExtension = aDefaultExtension;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* attribute long filterIndex; */
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsFilePicker::GetFilterIndex(int32_t* aFilterIndex)
|
2008-04-19 15:44:21 +00:00
|
|
|
{
|
|
|
|
*aFilterIndex = mSelectedType;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-09-14 13:16:28 +00:00
|
|
|
|
2008-04-19 15:44:21 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsFilePicker::SetFilterIndex(int32_t aFilterIndex)
|
2008-04-19 15:44:21 +00:00
|
|
|
{
|
|
|
|
mSelectedType = aFilterIndex;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-06 02:08:30 +00:00
|
|
|
/* readonly attribute nsIFile file; */
|
2008-04-19 15:44:21 +00:00
|
|
|
NS_IMETHODIMP
|
2012-06-06 02:08:30 +00:00
|
|
|
nsFilePicker::GetFile(nsIFile* *aFile)
|
2008-04-19 15:44:21 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aFile);
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
*aFile = nullptr;
|
2008-04-19 15:44:21 +00:00
|
|
|
if (mFile.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-06 02:08:30 +00:00
|
|
|
nsCOMPtr<nsIFile> file(do_CreateInstance("@mozilla.org/file/local;1"));
|
2008-04-19 15:44:21 +00:00
|
|
|
NS_ENSURE_TRUE(file, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
file->InitWithNativePath(mFile);
|
|
|
|
|
|
|
|
NS_ADDREF(*aFile = file);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute nsIFileURL fileURL; */
|
|
|
|
NS_IMETHODIMP
|
2011-09-14 13:16:28 +00:00
|
|
|
nsFilePicker::GetFileURL(nsIURI* *aFileURL)
|
2008-04-19 15:44:21 +00:00
|
|
|
{
|
2012-06-06 02:08:30 +00:00
|
|
|
nsCOMPtr<nsIFile> file;
|
2008-04-19 15:44:21 +00:00
|
|
|
GetFile(getter_AddRefs(file));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
NS_NewFileURI(getter_AddRefs(uri), file);
|
|
|
|
NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return CallQueryInterface(uri, aFileURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute nsISimpleEnumerator files; */
|
|
|
|
NS_IMETHODIMP
|
2011-09-14 13:16:28 +00:00
|
|
|
nsFilePicker::GetFiles(nsISimpleEnumerator* *aFiles)
|
2008-04-19 15:44:21 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aFiles);
|
|
|
|
|
|
|
|
if (mMode == nsIFilePicker::modeOpenMultiple) {
|
|
|
|
return NS_NewArrayEnumerator(aFiles, mFiles);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* short show (); */
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsFilePicker::Show(int16_t* aReturn)
|
2008-04-19 15:44:21 +00:00
|
|
|
{
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString directory;
|
2008-04-19 15:44:21 +00:00
|
|
|
if (mDisplayDirectory) {
|
|
|
|
mDisplayDirectory->GetNativePath(directory);
|
|
|
|
}
|
|
|
|
|
2011-09-14 13:16:28 +00:00
|
|
|
QStringList filters;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count = mFilters.Length();
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2011-09-14 13:16:28 +00:00
|
|
|
filters.append(mFilters[i].get());
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsWidget* parentWidget = 0;
|
|
|
|
if (mParent) {
|
|
|
|
parentWidget = static_cast<QGraphicsWidget*>
|
|
|
|
(mParent->GetNativeData(NS_NATIVE_WIDGET));
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget* parentQWidget = 0;
|
|
|
|
if (parentWidget && parentWidget->scene()) {
|
|
|
|
if (parentWidget->scene()->views().size()>0) {
|
|
|
|
parentQWidget = parentWidget->scene()->views()[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList files;
|
|
|
|
QString selected;
|
|
|
|
|
2008-04-19 15:44:21 +00:00
|
|
|
switch (mMode) {
|
|
|
|
case nsIFilePicker::modeOpen:
|
2011-09-14 13:16:28 +00:00
|
|
|
selected = MozFileDialog::getOpenFileName(parentQWidget, mCaption,
|
|
|
|
directory.get(), filters.join(";"));
|
|
|
|
if (selected.isNull()) {
|
|
|
|
*aReturn = nsIFilePicker::returnCancel;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-04-19 15:44:21 +00:00
|
|
|
break;
|
|
|
|
case nsIFilePicker::modeOpenMultiple:
|
2011-09-14 13:16:28 +00:00
|
|
|
files = MozFileDialog::getOpenFileNames(parentQWidget, mCaption,
|
|
|
|
directory.get(), filters.join(";"));
|
|
|
|
if (files.empty()) {
|
|
|
|
*aReturn = nsIFilePicker::returnCancel;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
selected = files[0];
|
2008-04-19 15:44:21 +00:00
|
|
|
break;
|
|
|
|
case nsIFilePicker::modeSave:
|
2011-09-14 13:16:28 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFile> targetFile;
|
|
|
|
NS_GetSpecialDirectory(NS_UNIX_XDG_DOCUMENTS_DIR,
|
|
|
|
getter_AddRefs(targetFile));
|
|
|
|
if (!targetFile) {
|
|
|
|
// failed to get the XDG directory, using $HOME for now
|
|
|
|
NS_GetSpecialDirectory(NS_UNIX_HOME_DIR,
|
|
|
|
getter_AddRefs(targetFile));
|
|
|
|
}
|
2008-04-19 15:44:21 +00:00
|
|
|
|
2011-09-14 13:16:28 +00:00
|
|
|
if (targetFile) {
|
|
|
|
targetFile->Append(mDefault);
|
|
|
|
nsString targetPath;
|
|
|
|
targetFile->GetPath(targetPath);
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool exists = false;
|
2011-09-14 13:16:28 +00:00
|
|
|
targetFile->Exists(&exists);
|
|
|
|
if (exists) {
|
|
|
|
// file exists already create temporary filename
|
|
|
|
QTemporaryFile temp(QString::fromUtf16(targetPath.get()));
|
|
|
|
temp.open();
|
|
|
|
selected = temp.fileName();
|
|
|
|
temp.close();
|
|
|
|
} else {
|
|
|
|
selected = QString::fromUtf16(targetPath.get());
|
|
|
|
}
|
|
|
|
}
|
2008-04-19 15:44:21 +00:00
|
|
|
}
|
2011-09-14 13:16:28 +00:00
|
|
|
break;
|
|
|
|
case nsIFilePicker::modeGetFolder:
|
|
|
|
selected = MozFileDialog::getExistingDirectory(parentQWidget, mCaption,
|
|
|
|
directory.get());
|
|
|
|
if (selected.isNull()) {
|
|
|
|
*aReturn = nsIFilePicker::returnCancel;
|
|
|
|
return NS_OK;
|
2008-04-19 15:44:21 +00:00
|
|
|
}
|
2011-09-14 13:16:28 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2008-04-19 15:44:21 +00:00
|
|
|
|
2011-09-14 13:16:28 +00:00
|
|
|
if (!selected.isEmpty()) {
|
2008-04-19 15:53:41 +00:00
|
|
|
QString path = QFile::encodeName(selected);
|
|
|
|
mFile.Assign(path.toUtf8().data());
|
2011-09-14 13:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*aReturn = nsIFilePicker::returnOK;
|
|
|
|
if (mMode == modeSave) {
|
2012-06-06 02:08:30 +00:00
|
|
|
nsCOMPtr<nsIFile> file;
|
2011-09-14 13:16:28 +00:00
|
|
|
GetFile(getter_AddRefs(file));
|
|
|
|
if (file) {
|
2011-09-29 06:19:26 +00:00
|
|
|
bool exists = false;
|
2011-09-14 13:16:28 +00:00
|
|
|
file->Exists(&exists);
|
|
|
|
if (exists) {
|
|
|
|
*aReturn = nsIFilePicker::returnReplace;
|
2008-04-19 15:44:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-22 09:12:32 +00:00
|
|
|
void nsFilePicker::InitNative(nsIWidget *aParent, const nsAString &aTitle)
|
2008-04-19 15:44:21 +00:00
|
|
|
{
|
2010-08-26 15:18:06 +00:00
|
|
|
PR_LOG(sFilePickerLog, PR_LOG_DEBUG, ("nsFilePicker::InitNative"));
|
2011-09-14 13:16:28 +00:00
|
|
|
nsAutoString str(aTitle);
|
|
|
|
mCaption = QString::fromUtf16(str.get());
|
|
|
|
mParent = aParent;
|
2008-04-19 15:44:21 +00:00
|
|
|
}
|