summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@theqtcompany.com>2015-03-23 15:40:38 +0100
committerRichard Moe Gustavsen <richard.gustavsen@theqtcompany.com>2015-03-31 21:57:10 +0000
commitfdbd3cab382c949ed22415eeeb13eb3f5a4f017a (patch)
treebff8ec64a71b2cdf3f23955fbc7fc76ef28570f5
parentee49311d282ef089d0b34c666c06bf094a4495e7 (diff)
downloadqtquickcontrols-fdbd3cab382c949ed22415eeeb13eb3f5a4f017a.tar.gz
Dialogs: add shortcuts even when they point to non-existing dirs
Currently, shortcuts will only be added if the directories they point to exists. Since QStandardPaths::writableLocation() is documented to return paths that might not yet exist, bindings like the following will cause warnings on e.g iOS: FileDialog { folder: shortcuts.pictures; selectExisting: false } This patch will change this, so that we always add all shortcuts, to not cause trouble for bindings. That way the app can also take the necessary steps to check and create a shortcut up front before showing the dialog. Change-Id: Iba85710c87ae95e82728695cf958e1bf4394bc7c Reviewed-by: Shawn Rutledge <shawn.rutledge@digia.com>
-rw-r--r--src/dialogs/qquickfiledialog.cpp57
-rw-r--r--src/dialogs/qquickfiledialog_p.h2
2 files changed, 29 insertions, 30 deletions
diff --git a/src/dialogs/qquickfiledialog.cpp b/src/dialogs/qquickfiledialog.cpp
index dd6795e2..6fb15314 100644
--- a/src/dialogs/qquickfiledialog.cpp
+++ b/src/dialogs/qquickfiledialog.cpp
@@ -120,36 +120,34 @@ void QQuickFileDialog::addShortcut(const QString &name, const QString &visibleNa
{
QJSEngine *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());
- m_shortcuts.setProperty(name, url.toString());
+
int length = m_shortcutDetails.property(QLatin1String("length")).toInt();
m_shortcutDetails.setProperty(length, o);
}
-void QQuickFileDialog::maybeAdd(const QString &name, const QString &visibleName, QStandardPaths::StandardLocation loc)
+void QQuickFileDialog::addShortcutFromStandardLocation(const QString &name, QStandardPaths::StandardLocation loc, bool local)
{
- if (name.isEmpty() || visibleName.isEmpty())
- return;
- QString path;
- bool usable = false;
if (m_selectExisting) {
QStringList readPaths = QStandardPaths::standardLocations(loc);
- if (!readPaths.isEmpty()) {
- path = readPaths.first();
- usable = QDir(path).isReadable();
- }
+ QString path = readPaths.isEmpty() ? QString() : local ? readPaths.first() : readPaths.last();
+ addShortcut(name, QStandardPaths::displayName(loc), path);
} else {
- path = QStandardPaths::writableLocation(loc);
- // There is no QDir::isWritable(), but we can assume that if you don't have
- // permission to read, you can't write either. When you actually try to write,
- // other things can go wrong anyway, and we cannot know until we try.
- usable = QDir(path).isReadable();
+ QString path = QStandardPaths::writableLocation(loc);
+ addShortcut(name, QStandardPaths::displayName(loc), path);
}
- if (usable)
- addShortcut(name, visibleName, path);
}
void QQuickFileDialog::populateShortcuts()
@@ -158,19 +156,20 @@ void QQuickFileDialog::populateShortcuts()
m_shortcutDetails = engine->newArray();
m_shortcuts = engine->newObject();
- maybeAdd(QLatin1String("desktop"), QStandardPaths::displayName(QStandardPaths::DesktopLocation), QStandardPaths::DesktopLocation);
- maybeAdd(QLatin1String("documents"), QStandardPaths::displayName(QStandardPaths::DocumentsLocation), QStandardPaths::DocumentsLocation);
- maybeAdd(QLatin1String("music"), QStandardPaths::displayName(QStandardPaths::MusicLocation), QStandardPaths::MusicLocation);
- maybeAdd(QLatin1String("movies"), QStandardPaths::displayName(QStandardPaths::MoviesLocation), QStandardPaths::MoviesLocation);
- maybeAdd(QLatin1String("pictures"), QStandardPaths::displayName(QStandardPaths::PicturesLocation), QStandardPaths::PicturesLocation);
- maybeAdd(QLatin1String("home"), QStandardPaths::displayName(QStandardPaths::HomeLocation), QStandardPaths::HomeLocation);
-
-#ifdef Q_OS_IOS
- // PicturesLocation is a special URL, which we cannot check with QDir::isReadable()
- if (m_selectExisting)
- addShortcut(QLatin1String("pictures"), QStandardPaths::displayName(QStandardPaths::PicturesLocation),
- QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).last());
+ 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)
diff --git a/src/dialogs/qquickfiledialog_p.h b/src/dialogs/qquickfiledialog_p.h
index 0eb4eddf..d7b0804b 100644
--- a/src/dialogs/qquickfiledialog_p.h
+++ b/src/dialogs/qquickfiledialog_p.h
@@ -84,7 +84,7 @@ protected:
Q_INVOKABLE QUrl pathFolder(const QString &path);
void addShortcut(const QString &name, const QString &visibleName, const QString &path);
- void maybeAdd(const QString &name, const QString &visibleName, QStandardPaths::StandardLocation loc);
+ void addShortcutFromStandardLocation(const QString &name, QStandardPaths::StandardLocation loc, bool local = true);
void populateShortcuts();
void updateModes() Q_DECL_OVERRIDE;