From 144bc5cedd9af5e62fe7210bac7df2c19d20c41b Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Tue, 26 Jan 2016 14:32:59 +0100 Subject: FileDialog: move shortcuts properties up to QQuickAbstractFileDialog QQuickQFileDialog does not inherit QQuickFileDialog, so the documented shortcuts property was not defined if your platform happens to use the QFileDialog implementation. As long as we are moving the public property, it's easiest to move everything related to shortcuts. Task-number: QTBUG-50673 Change-Id: Ic2baf4a3c83ca12edf11d7d011aaa78a389e0fde Reviewed-by: Richard Moe Gustavsen --- src/dialogs/qquickabstractfiledialog.cpp | 82 +++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) (limited to 'src/dialogs/qquickabstractfiledialog.cpp') diff --git a/src/dialogs/qquickabstractfiledialog.cpp b/src/dialogs/qquickabstractfiledialog.cpp index 233a2db77..26c7218b0 100644 --- a/src/dialogs/qquickabstractfiledialog.cpp +++ b/src/dialogs/qquickabstractfiledialog.cpp @@ -38,9 +38,10 @@ #include "qquickitem.h" #include +#include +#include #include #include -#include QT_BEGIN_NAMESPACE @@ -221,4 +222,83 @@ void QQuickAbstractFileDialog::updateModes() emit fileModeChanged(); } +void QQuickAbstractFileDialog::addShortcut(const QString &name, const QString &visibleName, const QString &path) +{ + QQmlEngine *engine = qmlEngine(this); + QUrl url = QUrl::fromLocalFile(path); + + // Since the app can have bindings to the shortcut, we always add it + // to the public API, even if the directory doesn't (yet) exist. + m_shortcuts.setProperty(name, url.toString()); + + // ...but we are more strict about showing it as a clickable link inside the dialog + if (visibleName.isEmpty() || !QDir(path).exists()) + return; + + QJSValue o = engine->newObject(); + o.setProperty("name", visibleName); + // TODO maybe some day QJSValue could directly store a QUrl + o.setProperty("url", url.toString()); + + int length = m_shortcutDetails.property(QLatin1String("length")).toInt(); + m_shortcutDetails.setProperty(length, o); +} + +void QQuickAbstractFileDialog::addShortcutFromStandardLocation(const QString &name, QStandardPaths::StandardLocation loc, bool local) +{ + if (m_selectExisting) { + QStringList readPaths = QStandardPaths::standardLocations(loc); + QString path = readPaths.isEmpty() ? QString() : local ? readPaths.first() : readPaths.last(); + addShortcut(name, QStandardPaths::displayName(loc), path); + } else { + QString path = QStandardPaths::writableLocation(loc); + addShortcut(name, QStandardPaths::displayName(loc), path); + } +} + +void QQuickAbstractFileDialog::populateShortcuts() +{ + QJSEngine *engine = qmlEngine(this); + m_shortcutDetails = engine->newArray(); + m_shortcuts = engine->newObject(); + + addShortcutFromStandardLocation(QLatin1String("desktop"), QStandardPaths::DesktopLocation); + addShortcutFromStandardLocation(QLatin1String("documents"), QStandardPaths::DocumentsLocation); + addShortcutFromStandardLocation(QLatin1String("music"), QStandardPaths::MusicLocation); + addShortcutFromStandardLocation(QLatin1String("movies"), QStandardPaths::MoviesLocation); + addShortcutFromStandardLocation(QLatin1String("home"), QStandardPaths::HomeLocation); + +#ifndef Q_OS_IOS + addShortcutFromStandardLocation(QLatin1String("pictures"), QStandardPaths::PicturesLocation); +#else + // On iOS we point pictures to the system picture folder when loading + addShortcutFromStandardLocation(QLatin1String("pictures"), QStandardPaths::PicturesLocation, !m_selectExisting); +#endif + +#ifndef Q_OS_IOS + // on iOS, this returns only "/", which is never a useful path to read or write anything + QFileInfoList drives = QDir::drives(); + foreach (QFileInfo fi, drives) + addShortcut(fi.absoluteFilePath(), fi.absoluteFilePath(), fi.absoluteFilePath()); +#endif + + emit shortcutsChanged(); +} + +QJSValue QQuickAbstractFileDialog::shortcuts() +{ + if (m_shortcuts.isUndefined()) + populateShortcuts(); + + return m_shortcuts; +} + +QJSValue QQuickAbstractFileDialog::__shortcuts() +{ + if (m_shortcutDetails.isUndefined()) + populateShortcuts(); + + return m_shortcutDetails; +} + QT_END_NAMESPACE -- cgit v1.2.3