filechooser.cpp 10.1 KB
Newer Older
Jan Grulich's avatar
Jan Grulich committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * Copyright © 2016 Red Hat, Inc
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Jan Grulich <jgrulich@redhat.com>
 */

#include "filechooser.h"

23
#include <QDBusMetaType>
24
#include <QDBusArgument>
Jan Grulich's avatar
Jan Grulich committed
25
26
#include <QLoggingCategory>
#include <QFileDialog>
Jan Grulich's avatar
Jan Grulich committed
27
#include <KLocalizedString>
Jan Grulich's avatar
Jan Grulich committed
28

Jan Grulich's avatar
Jan Grulich committed
29
Q_LOGGING_CATEGORY(XdgDesktopPortalKdeFileChooser, "xdp-kde-file-chooser")
Jan Grulich's avatar
Jan Grulich committed
30

31
// Keep in sync with qflatpakfiledialog from flatpak-platform-plugin
32
33
34
35
Q_DECLARE_METATYPE(FileChooserPortal::Filter)
Q_DECLARE_METATYPE(FileChooserPortal::Filters)
Q_DECLARE_METATYPE(FileChooserPortal::FilterList)
Q_DECLARE_METATYPE(FileChooserPortal::FilterListList)
36

37
QDBusArgument &operator << (QDBusArgument &arg, const FileChooserPortal::Filter &filter)
38
39
40
41
42
43
44
{
    arg.beginStructure();
    arg << filter.type << filter.filterString;
    arg.endStructure();
    return arg;
}

45
const QDBusArgument &operator >> (const QDBusArgument &arg, FileChooserPortal::Filter &filter)
46
47
48
49
50
51
52
53
54
55
56
57
{
    uint type;
    QString filterString;
    arg.beginStructure();
    arg >> type >> filterString;
    filter.type = type;
    filter.filterString = filterString;
    arg.endStructure();

    return arg;
}

58
QDBusArgument &operator << (QDBusArgument &arg, const FileChooserPortal::FilterList &filterList)
59
60
61
62
63
64
65
{
    arg.beginStructure();
    arg << filterList.userVisibleName << filterList.filters;
    arg.endStructure();
    return arg;
}

66
const QDBusArgument &operator >> (const QDBusArgument &arg, FileChooserPortal::FilterList &filterList)
67
68
{
    QString userVisibleName;
69
    FileChooserPortal::Filters filters;
70
71
72
73
74
75
76
77
    arg.beginStructure();
    arg >> userVisibleName >> filters;
    filterList.userVisibleName = userVisibleName;
    filterList.filters = filters;
    arg.endStructure();

    return arg;
}
Jan Grulich's avatar
Jan Grulich committed
78

79
80
FileChooserPortal::FileChooserPortal(QObject *parent)
    : QDBusAbstractAdaptor(parent)
Jan Grulich's avatar
Jan Grulich committed
81
{
82
83
84
85
    qDBusRegisterMetaType<Filter>();
    qDBusRegisterMetaType<Filters>();
    qDBusRegisterMetaType<FilterList>();
    qDBusRegisterMetaType<FilterListList>();
Jan Grulich's avatar
Jan Grulich committed
86
87
}

88
FileChooserPortal::~FileChooserPortal()
Jan Grulich's avatar
Jan Grulich committed
89
90
91
{
}

92
uint FileChooserPortal::OpenFile(const QDBusObjectPath &handle,
Jan Grulich's avatar
Jan Grulich committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
                           const QString &app_id,
                           const QString &parent_window,
                           const QString &title,
                           const QVariantMap &options,
                           QVariantMap &results)
{
    Q_UNUSED(app_id);

    qCDebug(XdgDesktopPortalKdeFileChooser) << "OpenFile called with parameters:";
    qCDebug(XdgDesktopPortalKdeFileChooser) << "    handle: " << handle.path();
    qCDebug(XdgDesktopPortalKdeFileChooser) << "    parent_window: " << parent_window;
    qCDebug(XdgDesktopPortalKdeFileChooser) << "    title: " << title;
    qCDebug(XdgDesktopPortalKdeFileChooser) << "    options: " << options;

    bool modalDialog = true;
    bool multipleFiles = false;
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    QString acceptLabel;
    QStringList nameFilters;
    QStringList mimeTypeFilters;

    /* TODO
     * choices a(ssa(ss)s)
     * List of serialized combo boxes to add to the file chooser.
     *
     * For each element, the first string is an ID that will be returned with the response, te second string is a user-visible label.
     * The a(ss) is the list of choices, each being a is an ID and a user-visible label. The final string is the initial selection,
     * or "", to let the portal decide which choice will be initially selected. None of the strings, except for the initial selection, should be empty.
     *
     * As a special case, passing an empty array for the list of choices indicates a boolean choice that is typically displayed as a check button, using "true" and "false" as the choices.
     * Example: [('encoding', 'Encoding', [('utf8', 'Unicode (UTF-8)'), ('latin15', 'Western')], 'latin15'), ('reencode', 'Reencode', [], 'false')]
     */
Jan Grulich's avatar
Jan Grulich committed
124

125
126
    if (options.contains(QLatin1String("accept_label"))) {
        acceptLabel = options.value(QLatin1String("accept_label")).toString();
Jan Grulich's avatar
Jan Grulich committed
127
128
129
130
131
132
    }

    if (options.contains(QLatin1String("modal"))) {
        modalDialog = options.value(QLatin1String("modal")).toBool();
    }

133
134
135
136
137
138
    if (options.contains(QLatin1String("multiple"))) {
        multipleFiles = options.value(QLatin1String("multiple")).toBool();
    }

    if (options.contains(QLatin1String("filters"))) {
        FilterListList filterListList = qdbus_cast<FilterListList>(options.value(QLatin1String("filters")));
Jan Grulich's avatar
Jan Grulich committed
139
        for (const FilterList &filterList : filterListList) {
140
            QStringList filterStrings;
Jan Grulich's avatar
Jan Grulich committed
141
            for (const Filter &filterStruct : filterList.filters) {
142
143
144
145
146
147
148
149
                if (filterStruct.type == 0) {
                    filterStrings << filterStruct.filterString;
                } else {
                    mimeTypeFilters << filterStruct.filterString;
                }
            }

            if (!filterStrings.isEmpty()) {
150
                nameFilters << QStringLiteral("%1 (%2)").arg(filterList.userVisibleName).arg(filterStrings.join(QLatin1String(" ")));
151
152
            }
        }
Jan Grulich's avatar
Jan Grulich committed
153
154
155
156
157
158
    }

    QFileDialog *fileDialog = new QFileDialog();
    fileDialog->setWindowTitle(title);
    fileDialog->setModal(modalDialog);
    fileDialog->setFileMode(multipleFiles ? QFileDialog::ExistingFiles : QFileDialog::ExistingFile);
Jan Grulich's avatar
Jan Grulich committed
159
    fileDialog->setLabelText(QFileDialog::Accept, !acceptLabel.isEmpty() ? acceptLabel : i18n("Open"));
Jan Grulich's avatar
Jan Grulich committed
160

161
162
163
164
165
166
167
168
    if (!nameFilters.isEmpty()) {
        fileDialog->setNameFilters(nameFilters);
    }

    if (!mimeTypeFilters.isEmpty()) {
        fileDialog->setMimeTypeFilters(mimeTypeFilters);
    }

Jan Grulich's avatar
Jan Grulich committed
169
170
    if (fileDialog->exec() == QDialog::Accepted) {
        QStringList files;
Jan Grulich's avatar
Jan Grulich committed
171
        for (const QString &filename : fileDialog->selectedFiles()) {
Jan Grulich's avatar
Jan Grulich committed
172
173
174
175
           QUrl url = QUrl::fromLocalFile(filename);
           files << url.toDisplayString();
        }
        results.insert(QLatin1String("uris"), files);
176
        fileDialog->deleteLater();
Jan Grulich's avatar
Jan Grulich committed
177
178
179
        return 0;
    }

180
    fileDialog->deleteLater();
Jan Grulich's avatar
Jan Grulich committed
181
182
183
    return 1;
}

184
uint FileChooserPortal::SaveFile(const QDBusObjectPath &handle,
Jan Grulich's avatar
Jan Grulich committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
                           const QString &app_id,
                           const QString &parent_window,
                           const QString &title,
                           const QVariantMap &options,
                           QVariantMap &results)
{
    Q_UNUSED(app_id);

    qCDebug(XdgDesktopPortalKdeFileChooser) << "SaveFile called with parameters:";
    qCDebug(XdgDesktopPortalKdeFileChooser) << "    handle: " << handle.path();
    qCDebug(XdgDesktopPortalKdeFileChooser) << "    parent_window: " << parent_window;
    qCDebug(XdgDesktopPortalKdeFileChooser) << "    title: " << title;
    qCDebug(XdgDesktopPortalKdeFileChooser) << "    options: " << options;

199
    bool modalDialog = true;
Jan Grulich's avatar
Jan Grulich committed
200
201
202
203
    QString acceptLabel;
    QString currentName;
    QString currentFolder;
    QString currentFile;
204
205
    QStringList nameFilters;
    QStringList mimeTypeFilters;
Jan Grulich's avatar
Jan Grulich committed
206

207
    // TODO parse options - choices
Jan Grulich's avatar
Jan Grulich committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221

    if (options.contains(QLatin1String("modal"))) {
        modalDialog = options.value(QLatin1String("modal")).toBool();
    }

    if (options.contains(QLatin1String("accept_label"))) {
        acceptLabel = options.value(QLatin1String("accept_label")).toString();
    }

    if (options.contains(QLatin1String("current_name"))) {
        currentName = options.value(QLatin1String("current_name")).toString();
    }

    if (options.contains(QLatin1String("current_folder"))) {
222
        currentFolder = QString::fromUtf8(options.value(QLatin1String("current_folder")).toByteArray());
Jan Grulich's avatar
Jan Grulich committed
223
224
225
    }

    if (options.contains(QLatin1String("current_file"))) {
226
        currentFile = QString::fromUtf8(options.value(QLatin1String("current_file")).toByteArray());
Jan Grulich's avatar
Jan Grulich committed
227
228
    }

229
230
    if (options.contains(QLatin1String("filters"))) {
        FilterListList filterListList = qdbus_cast<FilterListList>(options.value(QLatin1String("filters")));
Jan Grulich's avatar
Jan Grulich committed
231
        for (const FilterList &filterList : filterListList) {
232
            QStringList filterStrings;
Jan Grulich's avatar
Jan Grulich committed
233
            for (const Filter &filterStruct : filterList.filters) {
234
235
236
237
238
239
240
241
                if (filterStruct.type == 0) {
                    filterStrings << filterStruct.filterString;
                } else {
                    mimeTypeFilters << filterStruct.filterString;
                }
            }

            if (!filterStrings.isEmpty()) {
242
                nameFilters << QStringLiteral("%1 (%2)").arg(filterList.userVisibleName).arg(filterStrings.join(QLatin1String(" ")));
243
244
245
246
            }
        }
    }

Jan Grulich's avatar
Jan Grulich committed
247
248
249
250
251
    QFileDialog *fileDialog = new QFileDialog();
    fileDialog->setWindowTitle(title);
    fileDialog->setModal(modalDialog);
    fileDialog->setAcceptMode(QFileDialog::AcceptSave);

252
253
254
255
    // TODO: Looks Qt doesn't have API for this
    // if (!currentName.isEmpty()) {
    //    fileDialog->selectFile(currentName);
    // }
Jan Grulich's avatar
Jan Grulich committed
256
257

    if (!currentFolder.isEmpty()) {
258
        fileDialog->setDirectoryUrl(QUrl(currentFolder));
Jan Grulich's avatar
Jan Grulich committed
259
260
261
    }

    if (!currentFile.isEmpty()) {
262
        fileDialog->selectFile(currentFile);
Jan Grulich's avatar
Jan Grulich committed
263
264
265
266
267
268
    }

    if (!acceptLabel.isEmpty()) {
        fileDialog->setLabelText(QFileDialog::Accept, acceptLabel);
    }

269
270
271
272
273
274
275
276
    if (!nameFilters.isEmpty()) {
        fileDialog->setNameFilters(nameFilters);
    }

    if (!mimeTypeFilters.isEmpty()) {
        fileDialog->setMimeTypeFilters(mimeTypeFilters);
    }

Jan Grulich's avatar
Jan Grulich committed
277
278
    if (fileDialog->exec() == QDialog::Accepted) {
        QStringList files;
Jan Grulich's avatar
Jan Grulich committed
279
        for (const QString &filename : fileDialog->selectedFiles()) {
Jan Grulich's avatar
Jan Grulich committed
280
281
282
283
           QUrl url = QUrl::fromLocalFile(filename);
           files << url.toDisplayString();
        }
        results.insert(QLatin1String("uris"), files);
284
        fileDialog->deleteLater();
Jan Grulich's avatar
Jan Grulich committed
285
286
287
        return 0;
    }

288
    fileDialog->deleteLater();
Jan Grulich's avatar
Jan Grulich committed
289
290
291
    return 1;
}