File: openfilejob.cpp

package info (click to toggle)
ktextaddons 1.9.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,856 kB
  • sloc: cpp: 62,045; ansic: 6,520; xml: 2,630; sh: 11; makefile: 7
file content (117 lines) | stat: -rw-r--r-- 3,495 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
   SPDX-FileCopyrightText: 2025-2026 Laurent Montel <montel@kde.org>

   SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "openfilejob.h"
#include "savefileutils.h"
#include "textaddonswidgets_debug.h"
#include <KApplicationTrader>
#include <KLocalizedString>
#include <KService>
#include <QMessageBox>
#include <QMimeDatabase>
#include <QPushButton>
#include <QTemporaryDir>
#include <QUrl>

using namespace TextAddonsWidgets;
OpenFileJob::OpenFileJob(QObject *parent)
    : QObject{parent}
{
}

OpenFileJob::~OpenFileJob() = default;

QWidget *OpenFileJob::parentWidget() const
{
    return mParentWidget;
}

void OpenFileJob::setParentWidget(QWidget *newParentWidget)
{
    mParentWidget = newParentWidget;
}

QString OpenFileJob::link() const
{
    return mLink;
}

void OpenFileJob::setLink(const QString &newLink)
{
    mLink = newLink;
}

bool OpenFileJob::canStart() const
{
    return !mLink.isEmpty();
}

enum class UserChoice : uint8_t {
    Save,
    Open,
    OpenWith,
    Cancel,
};
Q_DECLARE_METATYPE(UserChoice)

static UserChoice askUser(const QUrl &url, const KService::Ptr &offer, QWidget *widget)
{
    const QString title = i18nc("@title:window", "Open Attachment?");
    const QString text = xi18nc("@info", "Open attachment <filename>%1</filename>?<nl/>", url.fileName());
    QMessageBox msgBox(QMessageBox::Question, title, text, QMessageBox::NoButton, widget);
    const char *prop = "_enumValue";
#if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
    msgBox.addButton(i18nc("@action:button", "Open"), QMessageBox::YesRole)->setProperty(prop, QVariant::fromValue(UserChoice::Open));
#else
    if (offer) {
        auto *b = msgBox.addButton(i18nc("@action:button", "&Open With '%1'", offer->name()), QMessageBox::YesRole);
        b->setProperty(prop, QVariant::fromValue(UserChoice::Open));
    }
    msgBox.addButton(i18nc("@action:button", "Open &With…"), QMessageBox::YesRole)->setProperty(prop, QVariant::fromValue(UserChoice::OpenWith));
#endif
    msgBox.addButton(i18nc("@action:button", "Save &As…"), QMessageBox::ActionRole)->setProperty(prop, QVariant::fromValue(UserChoice::Save));
    msgBox.addButton(QMessageBox::Cancel)->setProperty(prop, QVariant::fromValue(UserChoice::Cancel));
    msgBox.exec();
    return msgBox.clickedButton()->property(prop).value<UserChoice>();
}

void OpenFileJob::start()
{
    if (!canStart()) {
        qCWarning(TEXTADDONSWIDGETS_LOG) << "OpenFileJob::start: Link is empty";
        deleteLater();
        return;
    }
    const QUrl url(mLink);
    const QMimeDatabase db;
    const QMimeType mimeType = db.mimeTypeForUrl(url);
    const bool valid = mimeType.isValid() && !mimeType.isDefault();
    const KService::Ptr offer = valid ? KApplicationTrader::preferredService(mimeType.name()) : KService::Ptr{};
    const UserChoice choice = askUser(url, offer, mParentWidget);
    switch (choice) {
    case UserChoice::Save: {
        const QString file = SaveFileUtils::querySaveFileName(mParentWidget, i18nc("@title:window", "Save File"), url);
        if (!file.isEmpty()) {
            const QUrl fileUrl = QUrl::fromLocalFile(file);
            downloadFile(fileUrl);
        }
        break;
    }
    case UserChoice::Open:
#if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
        openUrl();
#else
        runApplication(offer);
#endif
        break;
    case UserChoice::OpenWith:
        runApplication({});
        break;
    case UserChoice::Cancel:
        break;
    }
}

#include "moc_openfilejob.cpp"