Commit 74383e26 authored by Jonah Brüchert's avatar Jonah Brüchert 🌳
Browse files

mobile filechooser: Fix handling of title and initial folder

parent 8970eaf2
......@@ -32,7 +32,7 @@
#include <QVBoxLayout>
#include <QUrl>
#include <QQmlApplicationEngine>
#include <QStandardPaths>
#include <KLocalizedString>
#include <KFileFilterCombo>
......@@ -415,7 +415,8 @@ uint FileChooserPortal::SaveFile(const QDBusObjectPath &handle,
m_mobileFileDialog->setSelectExisting(false);
if (!currentFolder.isEmpty()) {
m_mobileFileDialog->setFolder(currentFolder);
// Set correct protocol
m_mobileFileDialog->setFolder(QUrl::fromLocalFile(currentFolder));
}
if (!currentFile.isEmpty()) {
......
......@@ -12,6 +12,7 @@
#include <QLoggingCategory>
#include <KLocalizedContext>
#include <KLocalizedString>
#include "dirmodel.h"
#include "dirmodelutils.h"
......@@ -27,6 +28,7 @@ MobileFileDialog::MobileFileDialog(QObject *parent)
: QObject(parent)
, m_engine(new QQmlApplicationEngine(this))
, m_window(nullptr)
, m_customTitleSet(false)
{
qmlRegisterType<DirModel>(URI, 0, 1, "DirModel");
qmlRegisterSingletonType<DirModelUtils>(URI, 0, 1, "DirModelUtils", [=](QQmlEngine *, QJSEngine *) {
......@@ -55,6 +57,9 @@ MobileFileDialog::MobileFileDialog(QObject *parent)
connect(m_callback, &FileChooserQmlCallback::currentFileChanged, this, &MobileFileDialog::currentFileChanged);
connect(m_callback, &FileChooserQmlCallback::acceptLabelChanged, this, &MobileFileDialog::acceptLabelChanged);
connect(m_callback, &FileChooserQmlCallback::selectFolderChanged, this, &MobileFileDialog::selectFolderChanged);
// Set default path for file dialog
setFolder(QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
}
// FileDialog methods pass through to the callback to provide a nice c++ api
......@@ -65,6 +70,7 @@ QString MobileFileDialog::title() const
void MobileFileDialog::setTitle(const QString &title)
{
m_customTitleSet = true;
m_callback->setTitle(title);
}
......@@ -86,6 +92,19 @@ bool MobileFileDialog::selectExisting() const
void MobileFileDialog::setSelectExisting(bool selectExisting)
{
m_callback->setSelectExisting(selectExisting);
// Detect that no custom title is set
if (!m_customTitleSet) {
if (selectFolder()) {
setTitle(i18n("Select Folder"));
} else {
if (selectExisting) {
setTitle(i18n("Open File"));
} else {
setTitle(i18n("Save File"));
}
}
}
}
QStringList MobileFileDialog::nameFilters() const
......@@ -108,12 +127,12 @@ void MobileFileDialog::setMimeTypeFilters(const QStringList &mimeTypeFilters)
m_callback->setMimeTypeFilters(mimeTypeFilters);
}
QString MobileFileDialog::folder() const
QUrl MobileFileDialog::folder() const
{
return m_callback->folder();
}
void MobileFileDialog::setFolder(const QString &folder)
void MobileFileDialog::setFolder(const QUrl &folder)
{
m_callback->setFolder(folder);
}
......
......@@ -34,8 +34,8 @@ public:
QStringList mimeTypeFilters() const;
void setMimeTypeFilters(const QStringList &mimeTypeFilters);
QString folder() const;
void setFolder(const QString &folder);
QUrl folder() const;
void setFolder(const QUrl &folder);
QString currentFile() const;
void setCurrentFile(const QString &currentFile);
......@@ -67,4 +67,6 @@ private:
FileChooserQmlCallback *m_callback;
QStringList m_results;
QQuickWindow *m_window;
bool m_customTitleSet;
};
......@@ -49,25 +49,6 @@ Kirigami.ScrollablePage {
}
}
Component.onCompleted: {
// Reset to home path if the url is empty
if (!dirModel.folder.toString()) {
dirModel.folder = DirModelUtils.homePath
}
}
title: {
if (root.selectFolder) {
return i18n("Open Folder")
} else {
if (root.selectExisting) {
return i18n("Open File")
} else {
return i18n("Save File")
}
}
}
// result
property var fileUrls: []
......@@ -148,7 +129,6 @@ Kirigami.ScrollablePage {
DirModel {
id: dirModel
folder: root.folder
showDotFiles: false
mimeFilters: root.mimeTypeFilters
onLastErrorChanged: errorMessage.visible = true
......
......@@ -68,22 +68,7 @@ Kirigami.ApplicationWindow {
currentFile: callback.currentFile
acceptLabel: callback.acceptLabel
selectFolder: callback.selectFolder
Component.onCompleted: {
// set conditional properties
if (callback.folder) {
console.log("Initial folder set")
filePicker.folder = callback.folder
} else {
console.log("Initial folder not set")
}
if (callback.title) {
console.log("Custom title set")
filePicker.title = callback.title
} else {
console.log("Custom title not set")
}
}
folder: callback.folder
title: callback.title
}
}
......@@ -22,7 +22,7 @@ class DirModel : public KDirSortFilterProxyModel
Q_PROPERTY(QString nameFilter READ nameFilter WRITE setNameFilter NOTIFY nameFilterChanged)
Q_PROPERTY(QStringList mimeFilters READ mimeFilters WRITE setMimeFilters NOTIFY mimeFiltersChanged)
Q_PROPERTY(QString lastError READ lastError NOTIFY lastErrorChanged);
Q_PROPERTY(QString lastError READ lastError NOTIFY lastErrorChanged)
public:
enum Roles {
......
......@@ -35,14 +35,6 @@ QUrl DirModelUtils::partialUrlForIndex(QUrl url, int index) const
return url;
}
QString DirModelUtils::homePath() const
{
QUrl url(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
url.setScheme(QStringLiteral("file"));
return url.toString();
}
void DirModelUtils::mkdir(const QUrl path) const
{
KIO::mkdir(path);
......
......@@ -10,14 +10,11 @@ class DirModelUtils : public QObject
{
Q_OBJECT
Q_PROPERTY(QString homePath READ homePath NOTIFY homePathChanged)
public:
explicit DirModelUtils(QObject *parent = nullptr);
Q_INVOKABLE QStringList getUrlParts(const QUrl &url) const;
Q_INVOKABLE QUrl partialUrlForIndex(QUrl url, int index) const;
Q_INVOKABLE QString homePath() const;
Q_INVOKABLE void mkdir(const QUrl path) const;
......
......@@ -69,12 +69,12 @@ void FileChooserQmlCallback::setMimeTypeFilters(const QStringList &mimeTypeFilte
emit mimeTypeFiltersChanged();
}
QString FileChooserQmlCallback::folder() const
QUrl FileChooserQmlCallback::folder() const
{
return m_folder;
}
void FileChooserQmlCallback::setFolder(const QString &folder)
void FileChooserQmlCallback::setFolder(const QUrl &folder)
{
m_folder = folder;
emit folderChanged();
......
......@@ -5,6 +5,7 @@
#pragma once
#include <QObject>
#include <QUrl>
class FileChooserQmlCallback : public QObject
{
......@@ -15,7 +16,7 @@ class FileChooserQmlCallback : public QObject
Q_PROPERTY(bool selectExisting READ selectExisting WRITE setSelectExisting NOTIFY selectExistingChanged)
Q_PROPERTY(QStringList nameFilters READ nameFilters WRITE setNameFilters NOTIFY nameFiltersChanged)
Q_PROPERTY(QStringList mimeTypeFilters READ mimeTypeFilters WRITE setMimeTypeFilters NOTIFY mimeTypeFiltersChanged)
Q_PROPERTY(QString folder READ folder WRITE setFolder NOTIFY folderChanged)
Q_PROPERTY(QUrl folder READ folder WRITE setFolder NOTIFY folderChanged)
Q_PROPERTY(QString currentFile READ currentFile WRITE setCurrentFile NOTIFY currentFileChanged)
Q_PROPERTY(QString acceptLabel READ acceptLabel WRITE setAcceptLabel NOTIFY acceptLabelChanged)
Q_PROPERTY(bool selectFolder READ selectFolder WRITE setSelectFolder NOTIFY selectFolderChanged)
......@@ -38,8 +39,8 @@ public:
QStringList mimeTypeFilters() const;
void setMimeTypeFilters(const QStringList &mimeTypeFilters);
QString folder() const;
void setFolder(const QString &folder);
QUrl folder() const;
void setFolder(const QUrl &folder);
QString currentFile() const;
void setCurrentFile(const QString &currentFile);
......@@ -70,7 +71,7 @@ private:
bool m_selectExisting;
QStringList m_nameFilters;
QStringList m_mimeTypeFilters;
QString m_folder;
QUrl m_folder;
QString m_currentFile;
QString m_acceptLabel;
bool m_selectFolder;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment