| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Quick Dialogs module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qquickfiledialog_p.h" |
| 41 | #include <QQuickItem> |
| 42 | #include <QQmlEngine> |
| 43 | #include <QJSValueIterator> |
| 44 | #include <private/qguiapplication_p.h> |
| 45 | |
| 46 | QT_BEGIN_NAMESPACE |
| 47 | |
| 48 | using namespace QV4; |
| 49 | |
| 50 | // Note: documentation comments here are not currently used to generate |
| 51 | // user documentation, because AbstractFileDialog is not a user-facing type. |
| 52 | // FileDialog docs go into qquickplatformfiledialog.cpp |
| 53 | |
| 54 | /*! |
| 55 | \qmltype AbstractFileDialog |
| 56 | \instantiates QQuickFileDialog |
| 57 | \inqmlmodule QtQuick.Dialogs |
| 58 | \ingroup qtquick-visual |
| 59 | \brief API wrapper for QML file dialog implementations |
| 60 | \since 5.1 |
| 61 | \internal |
| 62 | |
| 63 | AbstractFileDialog provides only the API for implementing a file dialog. |
| 64 | The implementation (e.g. a Window or preferably an Item, in case it is |
| 65 | shown on a device that doesn't support multiple windows) can be provided as |
| 66 | \l implementation, which is the default property (the only allowed child |
| 67 | element). |
| 68 | */ |
| 69 | |
| 70 | /*! |
| 71 | \qmlsignal QtQuick::Dialogs::AbstractFileDialog::accepted |
| 72 | |
| 73 | This signal is emitted by \l accept(). |
| 74 | |
| 75 | The corresponding handler is \c onAccepted. |
| 76 | */ |
| 77 | |
| 78 | /*! |
| 79 | \qmlsignal QtQuick::Dialogs::AbstractFileDialog::rejected |
| 80 | |
| 81 | This signal is emitted by \l reject(). |
| 82 | |
| 83 | The corresponding handler is \c onRejected. |
| 84 | */ |
| 85 | |
| 86 | /*! |
| 87 | \class QQuickFileDialog |
| 88 | \inmodule QtQuick.Dialogs |
| 89 | \internal |
| 90 | |
| 91 | The QQuickFileDialog class is a concrete subclass of |
| 92 | \l QQuickAbstractFileDialog, but it is abstract from the QML perspective |
| 93 | because it needs to enclose a graphical implementation. It exists in order |
| 94 | to provide accessors and helper functions which the QML implementation will |
| 95 | need. |
| 96 | |
| 97 | \since 5.1 |
| 98 | */ |
| 99 | |
| 100 | /*! |
| 101 | Constructs a file dialog wrapper with parent window \a parent. |
| 102 | */ |
| 103 | QQuickFileDialog::QQuickFileDialog(QObject *parent) |
| 104 | : QQuickAbstractFileDialog(parent) |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /*! |
| 110 | Destroys the file dialog wrapper. |
| 111 | */ |
| 112 | QQuickFileDialog::~QQuickFileDialog() |
| 113 | { |
| 114 | } |
| 115 | |
| 116 | QList<QUrl> QQuickFileDialog::fileUrls() const |
| 117 | { |
| 118 | return m_selections; |
| 119 | } |
| 120 | |
| 121 | /*! |
| 122 | \qmlproperty bool AbstractFileDialog::visible |
| 123 | |
| 124 | This property holds whether the dialog is visible. By default this is false. |
| 125 | */ |
| 126 | |
| 127 | /*! |
| 128 | \qmlproperty bool AbstractFileDialog::fileUrls |
| 129 | |
| 130 | A list of files to be populated as the user chooses. |
| 131 | */ |
| 132 | |
| 133 | /*! |
| 134 | \brief Clears \l fileUrls |
| 135 | */ |
| 136 | void QQuickFileDialog::clearSelection() |
| 137 | { |
| 138 | m_selections.clear(); |
| 139 | } |
| 140 | |
| 141 | /*! |
| 142 | \brief Adds one file to \l fileUrls |
| 143 | |
| 144 | \l path should be given as an absolute file:// path URL. |
| 145 | Returns true on success, false if the given path is |
| 146 | not valid given the current property settings. |
| 147 | */ |
| 148 | bool QQuickFileDialog::addSelection(const QUrl &path) |
| 149 | { |
| 150 | QFileInfo info(path.toLocalFile()); |
| 151 | if (selectExisting() && !info.exists()) |
| 152 | return false; |
| 153 | if (selectFolder() != info.isDir()) |
| 154 | return false; |
| 155 | if (selectFolder()) |
| 156 | m_selections.append(t: pathFolder(path: path.toLocalFile())); |
| 157 | else |
| 158 | m_selections.append(t: path); |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | /*! |
| 163 | \brief get a file's directory as a URL |
| 164 | |
| 165 | If \a path points to a directory, just convert it to a URL. |
| 166 | If \a path points to a file, convert the file's directory to a URL. |
| 167 | */ |
| 168 | QUrl QQuickFileDialog::pathFolder(const QString &path) |
| 169 | { |
| 170 | QFileInfo info(path); |
| 171 | if (info.exists() && info.isDir()) |
| 172 | return QUrl::fromLocalFile(localfile: path); |
| 173 | return QUrl::fromLocalFile(localfile: QFileInfo(path).absolutePath()); |
| 174 | } |
| 175 | |
| 176 | /*! |
| 177 | \qmlproperty QObject AbstractFileDialog::implementation |
| 178 | |
| 179 | The QML object which implements the actual file dialog. Should be either a |
| 180 | \l Window or an \l Item. |
| 181 | */ |
| 182 | |
| 183 | QT_END_NAMESPACE |
| 184 | |