File: fileinfo.cpp

package info (click to toggle)
plasma-workspace 4%3A5.27.5-2%2Bdeb12u2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 102,040 kB
  • sloc: cpp: 121,800; xml: 3,238; python: 645; perl: 586; sh: 254; javascript: 113; ruby: 62; makefile: 15; ansic: 13
file content (189 lines) | stat: -rw-r--r-- 4,600 bytes parent folder | download | duplicates (3)
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
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
/*
    SPDX-FileCopyrightText: 2021 Kai Uwe Broulik <kde@broulik.de>

    SPDX-License-Identifier: LGPL-2.1-or-later
*/

#include "fileinfo.h"
#include "notifications_debug.h"

#include <QAction>
#include <QMimeDatabase>

#include <KApplicationTrader>
#include <KAuthorized>
#include <KIO/ApplicationLauncherJob>
#include <KIO/JobUiDelegateFactory>
#include <KIO/MimeTypeFinderJob>
#include <KIO/OpenUrlJob>
#include <KLocalizedString>
#include <KNotificationJobUiDelegate>

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

FileInfo::~FileInfo() = default;

QUrl FileInfo::url() const
{
    return m_url;
}

void FileInfo::setUrl(const QUrl &url)
{
    if (m_url != url) {
        m_url = url;
        reload();
        Q_EMIT urlChanged(url);
    }
}

bool FileInfo::busy() const
{
    return m_busy;
}

void FileInfo::setBusy(bool busy)
{
    if (m_busy != busy) {
        m_busy = busy;
        Q_EMIT busyChanged(busy);
    }
}

int FileInfo::error() const
{
    return m_error;
}

void FileInfo::setError(int error)
{
    if (m_error != error) {
        m_error = error;
        Q_EMIT errorChanged(error);
    }
}

QString FileInfo::mimeType() const
{
    return m_mimeType;
}

QString FileInfo::iconName() const
{
    return m_iconName;
}

QAction *FileInfo::openAction() const
{
    return m_openAction;
}

QString FileInfo::openActionIconName() const
{
    return m_openAction ? m_openAction->icon().name() : QString();
}

void FileInfo::reload()
{
    if (!m_url.isValid()) {
        return;
    }

    if (m_job) {
        m_job->kill();
    }

    setError(0);

    // Do a quick guess by file name while we wait for the job to find the mime type
    QString guessedMimeType;

    // NOTE using QUrl::path() for API that accepts local files is usually wrong
    // but here we really only care about the file name and its extension.
    const auto type = QMimeDatabase().mimeTypeForFile(m_url.path(), QMimeDatabase::MatchExtension);
    if (!type.isDefault()) {
        guessedMimeType = type.name();
    }

    mimeTypeFound(guessedMimeType);

    m_job = new KIO::MimeTypeFinderJob(m_url);
    m_job->setAuthenticationPromptEnabled(false);

    const QUrl url = m_url;
    connect(m_job, &KIO::MimeTypeFinderJob::result, this, [this, url] {
        setError(m_job->error());
        if (m_job->error()) {
            qCWarning(PLASMA_APPLET_NOTIFICATIONS_DEBUG) << "Failed to determine mime type for" << url << m_job->errorString();
        } else {
            mimeTypeFound(m_job->mimeType());
        }
        setBusy(false);
    });

    setBusy(true);
    m_job->start();
}

void FileInfo::mimeTypeFound(const QString &mimeType)
{
    if (m_mimeType == mimeType) {
        return;
    }

    const QString oldOpenActionIconName = openActionIconName();

    bool emitOpenActionChanged = false;
    if (!m_openAction) {
        m_openAction = new QAction(this);
        connect(m_openAction, &QAction::triggered, this, [this] {
            auto *job = new KIO::ApplicationLauncherJob(m_preferredApplication);
            if (m_preferredApplication) {
                job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoErrorHandlingEnabled));
            } else {
                // needs KIO::JobUiDelegate for open with handler
                job->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoErrorHandlingEnabled, nullptr /*widget*/));
            }
            job->setUrls({m_url});
            job->start();
        });
        emitOpenActionChanged = true;
    }

    m_mimeType = mimeType;

    m_preferredApplication.reset();

    if (!mimeType.isEmpty()) {
        const auto type = QMimeDatabase().mimeTypeForName(mimeType);
        m_iconName = type.iconName();

        m_preferredApplication = KApplicationTrader::preferredService(mimeType);
    } else {
        m_iconName.clear();
    }

    if (m_preferredApplication) {
        m_openAction->setText(i18n("Open with %1", m_preferredApplication->name()));
        m_openAction->setIcon(QIcon::fromTheme(m_preferredApplication->icon()));
        m_openAction->setEnabled(true);
    } else {
        m_openAction->setText(i18n("Open with…"));
        m_openAction->setIcon(QIcon::fromTheme(QStringLiteral("system-run")));
        m_openAction->setEnabled(KAuthorized::authorizeAction(KAuthorized::OPEN_WITH));
    }

    Q_EMIT mimeTypeChanged();

    if (emitOpenActionChanged) {
        Q_EMIT openActionChanged();
    }
    if (oldOpenActionIconName != openActionIconName()) {
        Q_EMIT openActionIconNameChanged();
    }
}

#include "moc_fileinfo.cpp"