appchooserdialog.cpp 9.69 KB
Newer Older
Jan Grulich's avatar
Jan Grulich committed
1
/*
Jan Grulich's avatar
Jan Grulich committed
2
 * Copyright © 2017-2019 Red Hat, Inc
3
 * Copyright © 2020 Harald Sitter <sitter@kde.org>
Jan Grulich's avatar
Jan Grulich committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * 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 "appchooserdialog.h"
Jan Grulich's avatar
Jan Grulich committed
23
24
25
26
27
28
29
30
#include "ui_appchooserdialog.h"

#include <QQmlContext>
#include <QQmlEngine>
#include <QQuickWidget>
#include <QQuickItem>

#include <QDir>
Jan Grulich's avatar
Jan Grulich committed
31
#include <QStandardPaths>
Jan Grulich's avatar
Jan Grulich committed
32
#include <QSettings>
Jan Grulich's avatar
Jan Grulich committed
33

34
#include <KApplicationTrader>
Jan Grulich's avatar
Jan Grulich committed
35
#include <KProcess>
36
#include <KService>
Jan Grulich's avatar
Jan Grulich committed
37
#include <kdeclarative/kdeclarative.h>
Jan Grulich's avatar
Jan Grulich committed
38

39
AppChooserDialog::AppChooserDialog(const QStringList &choices, const QString &defaultApp, const QString &fileName, const QString &mimeName, QDialog *parent, Qt::WindowFlags flags)
Jan Grulich's avatar
Jan Grulich committed
40
    : QDialog(parent, flags)
Jan Grulich's avatar
Jan Grulich committed
41
42
    , m_dialog(new Ui::AppChooserDialog)
    , m_defaultChoices(choices)
43
    , m_defaultApp(defaultApp)
44
    , m_mimeName(mimeName)
Jan Grulich's avatar
Jan Grulich committed
45
{
Jan Grulich's avatar
Jan Grulich committed
46
    m_dialog->setupUi(this);
Jan Grulich's avatar
Jan Grulich committed
47

Jan Grulich's avatar
Jan Grulich committed
48
49
50
51
52
    KDeclarative::KDeclarative kdeclarative;
    kdeclarative.setDeclarativeEngine(m_dialog->quickWidget->engine());
    kdeclarative.setTranslationDomain(QStringLiteral(TRANSLATION_DOMAIN));
    kdeclarative.setupEngine(m_dialog->quickWidget->engine());
    kdeclarative.setupContext();
Jan Grulich's avatar
Jan Grulich committed
53

Jan Grulich's avatar
Jan Grulich committed
54
55
    m_model = new AppModel(this);
    m_model->setPreferredApps(choices);
Jan Grulich's avatar
Jan Grulich committed
56

Jan Grulich's avatar
Jan Grulich committed
57
58
    AppFilterModel *filterModel = new AppFilterModel(this);
    filterModel->setSourceModel(m_model);
Jan Grulich's avatar
Jan Grulich committed
59

60
61
62
63
64
65
    auto *data = new AppChooserData(this);
    data->setFileName(fileName);
    data->setDefaultApp(defaultApp);

    qmlRegisterSingletonInstance<AppFilterModel>("org.kde.xdgdesktopportal", 1, 0, "AppModel", filterModel);
    qmlRegisterSingletonInstance<AppChooserData>("org.kde.xdgdesktopportal", 1, 0, "AppChooserData", data);
Jan Grulich's avatar
Jan Grulich committed
66
67
68
    m_dialog->quickWidget->setClearColor(Qt::transparent);
    m_dialog->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
    m_dialog->quickWidget->setSource(QUrl::fromLocalFile(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("xdg-desktop-portal-kde/qml/AppChooserDialog.qml"))));
69

70
71
    connect(data, &AppChooserData::openDiscover, this, &AppChooserDialog::onOpenDiscover);
    connect(data, &AppChooserData::applicationSelected, this, &AppChooserDialog::onApplicationSelected);
72

Jan Grulich's avatar
Jan Grulich committed
73
74
    setWindowTitle(i18n("Open with..."));
}
Jan Grulich's avatar
Jan Grulich committed
75

Jan Grulich's avatar
Jan Grulich committed
76
77
78
79
AppChooserDialog::~AppChooserDialog()
{
    delete m_dialog;
}
80

Jan Grulich's avatar
Jan Grulich committed
81
82
83
84
QString AppChooserDialog::selectedApplication() const
{
    return m_selectedApplication;
}
85

Jan Grulich's avatar
Jan Grulich committed
86
87
88
89
void AppChooserDialog::onApplicationSelected(const QString& desktopFile)
{
    m_selectedApplication = desktopFile;
    QDialog::accept();
90
91
}

Jan Grulich's avatar
Jan Grulich committed
92
void AppChooserDialog::onOpenDiscover()
93
{
94
95
96
97
98
    QStringList args;
    if (!m_mimeName.isEmpty()) {
        args << QStringLiteral("--mime") << m_mimeName;
    }
    KProcess::startDetached(QStringLiteral("plasma-discover"), args);
99
100
101
102
}

void AppChooserDialog::updateChoices(const QStringList &choices)
{
Jan Grulich's avatar
Jan Grulich committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
    m_model->setPreferredApps(choices);
}

ApplicationItem::ApplicationItem(const QString &name, const QString &icon, const QString &desktopFileName)
    : m_applicationName(name)
    , m_applicationIcon(icon)
    , m_applicationDesktopFile(desktopFileName)
    , m_applicationCategory(AllApplications)
{
}

QString ApplicationItem::applicationName() const
{
    return m_applicationName;
}

QString ApplicationItem::applicationIcon() const
{
    return m_applicationIcon;
}

QString ApplicationItem::applicationDesktopFile() const
{
    return m_applicationDesktopFile;
}

void ApplicationItem::setApplicationCategory(ApplicationItem::ApplicationCategory category)
{
    m_applicationCategory = category;
}

ApplicationItem::ApplicationCategory ApplicationItem::applicationCategory() const
{
    return m_applicationCategory;
}

bool ApplicationItem::operator==(const ApplicationItem &item) const
{
    return item.applicationDesktopFile() == applicationDesktopFile();
}

AppFilterModel::AppFilterModel(QObject *parent)
    : QSortFilterProxyModel(parent)
{
    setDynamicSortFilter(true);
    setFilterCaseSensitivity(Qt::CaseInsensitive);
    sort(0, Qt::DescendingOrder);
}

AppFilterModel::~AppFilterModel()
{
}

void AppFilterModel::setShowOnlyPrefferedApps(bool show)
{
    m_showOnlyPreferredApps = show;

    invalidate();
}

bool AppFilterModel::showOnlyPreferredApps() const
{
    return m_showOnlyPreferredApps;
}

void AppFilterModel::setFilter(const QString &text)
{
    m_filter = text;

    invalidate();
}

QString AppFilterModel::filter() const
{
    return m_filter;
}

bool AppFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
    const QModelIndex index = sourceModel()->index(source_row, 0, source_parent);

    ApplicationItem::ApplicationCategory category = static_cast<ApplicationItem::ApplicationCategory>(sourceModel()->data(index, AppModel::ApplicationCategoryRole).toInt());
    QString appName = sourceModel()->data(index, AppModel::ApplicationNameRole).toString();

    if (m_showOnlyPreferredApps)
        return category == ApplicationItem::PreferredApplication;

    if (category == ApplicationItem::PreferredApplication)
        return true;

    if (m_filter.isEmpty())
        return true;

    return appName.toLower().contains(m_filter);
}

bool AppFilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
    ApplicationItem::ApplicationCategory leftCategory = static_cast<ApplicationItem::ApplicationCategory>(sourceModel()->data(left, AppModel::ApplicationCategoryRole).toInt());
    ApplicationItem::ApplicationCategory rightCategory = static_cast<ApplicationItem::ApplicationCategory>(sourceModel()->data(right, AppModel::ApplicationCategoryRole).toInt());
    QString leftName = sourceModel()->data(left, AppModel::ApplicationNameRole).toString();
    QString rightName = sourceModel()->data(right, AppModel::ApplicationNameRole).toString();

    if (leftCategory < rightCategory) {
        return false;
    } else if (leftCategory > rightCategory) {
        return true;
    }

    return QString::localeAwareCompare(leftName, rightName) > 0;
}

215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
QString AppChooserData::defaultApp() const
{
    return m_defaultApp;
}

void AppChooserData::setDefaultApp(const QString &defaultApp)
{
    m_defaultApp = defaultApp;
    Q_EMIT defaultAppChanged();
}

AppChooserData::AppChooserData(QObject *parent)
    : QObject(parent)
{
}

QString AppChooserData::fileName() const
{
    return m_fileName;
}

void AppChooserData::setFileName(const QString &fileName)
{
    m_fileName = fileName;
    Q_EMIT fileNameChanged();
}

Jan Grulich's avatar
Jan Grulich committed
242
243
244
245
246
247
248
249
250
AppModel::AppModel(QObject *parent)
    : QAbstractListModel(parent)
{
    loadApplications();
}

AppModel::~AppModel()
{
}
251

Jan Grulich's avatar
Jan Grulich committed
252
253
254
255
256
257
258
259
void AppModel::setPreferredApps(const QStringList &list)
{
    for (ApplicationItem &item : m_list) {
        bool changed = false;

        // First reset to initial type
        if (item.applicationCategory() != ApplicationItem::AllApplications) {
            item.setApplicationCategory(ApplicationItem::AllApplications);
260
261
262
            changed = true;
        }

Jan Grulich's avatar
Jan Grulich committed
263
264
        if (list.contains(item.applicationDesktopFile())) {
            item.setApplicationCategory(ApplicationItem::PreferredApplication);
265
266
267
            changed = true;
        }

Jan Grulich's avatar
Jan Grulich committed
268
269
270
271
272
        if (changed) {
            const int row = m_list.indexOf(item);
            if (row >= 0) {
                QModelIndex index = createIndex(row, 0, AppModel::ApplicationCategoryRole);
                Q_EMIT dataChanged(index, index);
273
274
275
276
277
            }
        }
    }
}

Jan Grulich's avatar
Jan Grulich committed
278
QVariant AppModel::data(const QModelIndex &index, int role) const
279
{
Jan Grulich's avatar
Jan Grulich committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
    const int row = index.row();

    if (row >= 0 && row < m_list.count()) {
        ApplicationItem item = m_list.at(row);

        switch (role) {
            case ApplicationNameRole:
                return item.applicationName();
            case ApplicationIconRole:
                return item.applicationIcon();
            case ApplicationDesktopFileRole:
                return item.applicationDesktopFile();
            case ApplicationCategoryRole:
                return static_cast<int>(item.applicationCategory());
            default:
                break;
        }
297
298
    }

Jan Grulich's avatar
Jan Grulich committed
299
300
301
302
303
304
305
306
307
308
    return QVariant();
}

int AppModel::rowCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : m_list.count();
}

QHash<int, QByteArray> AppModel::roleNames() const
{
Jonah Brüchert's avatar
Jonah Brüchert committed
309
310
311
312
313
314
    return {
        {ApplicationNameRole, QByteArrayLiteral("applicationName")},
        {ApplicationIconRole, QByteArrayLiteral("applicationIcon")},
        {ApplicationDesktopFileRole, QByteArrayLiteral("applicationDesktopFile")},
        {ApplicationCategoryRole, QByteArrayLiteral("applicationCategory")}
    };
315
316
}

Jan Grulich's avatar
Jan Grulich committed
317
void AppModel::loadApplications()
318
{
319
320
321
322
323
324
325
326
327
    const KService::List appServices = KApplicationTrader::query([](const KService::Ptr &service) -> bool {
         return service->isValid() && !service->noDisplay() /* includes platform and desktop considerations */;
    });
    for (const KService::Ptr &service : appServices) {
        const QString fullName = service->property(QStringLiteral("X-GNOME-FullName"), QVariant::String).toString();
        const QString name = fullName.isEmpty() ? service->name() : fullName;
        ApplicationItem appItem(name, service->icon(), service->desktopEntryName());
        if (!m_list.contains(appItem)) {
            m_list.append(appItem);
Jan Grulich's avatar
Jan Grulich committed
328
329
330
        }
    }
}