File indexing completed on 2024-05-12 05:48:10

0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0002 // SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
0003 
0004 #include "file.h"
0005 
0006 #include <KIO/FileJob>
0007 
0008 File::File(const QUrl &url, QIODevice::OpenMode openMode, const QString &remoteService, const QDBusObjectPath &objectPath, QObject *parent)
0009     : BusObject(remoteService, objectPath, parent)
0010     , m_url(url)
0011     , m_openMode(openMode)
0012 {
0013 }
0014 
0015 void File::open()
0016 {
0017     if (!isAuthorized()) {
0018         sendErrorReply(QDBusError::AccessDenied);
0019         return;
0020     }
0021 
0022     m_job = KIO::open(m_url, m_openMode);
0023     setParent(m_job);
0024     connect(m_job, &KIO::FileJob::open, this, [this] {
0025         sendSignal(&File::opened);
0026     });
0027     connect(m_job, &KIO::FileJob::fileClosed, this, [this] {
0028         sendSignal(&File::closed);
0029     });
0030     connect(m_job, &KIO::FileJob::data, this, [this](KIO::Job *, const QByteArray &blob) {
0031         sendSignal(&File::data, blob);
0032     });
0033     connect(m_job, &KIO::FileJob::truncated, this, [this](KIO::Job *, qulonglong length) {
0034         sendSignal(&File::truncated, length);
0035     });
0036     connect(m_job, &KIO::FileJob::written, this, [this](KIO::Job *, qulonglong length) {
0037         sendSignal(&File::written, length);
0038     });
0039     connect(m_job, &KIO::FileJob::position, this, [this](KIO::Job *, qulonglong offset) {
0040         sendSignal(&File::positionChanged, offset);
0041     });
0042     connect(m_job, &KIO::FileJob::mimeTypeFound, this, [this](KIO::Job *, const QString &mimeType) {
0043         sendSignal(&File::mimeTypeFound, mimeType);
0044     });
0045     connect(m_job, &KIO::Job::result, this, [this](KJob *job) {
0046         sendSignal(&File::result, job->error(), job->errorString());
0047     });
0048 }
0049 
0050 void File::read(qulonglong size)
0051 {
0052     if (!isAuthorized()) {
0053         sendErrorReply(QDBusError::AccessDenied);
0054         return;
0055     }
0056     m_job->read(size);
0057 }
0058 
0059 void File::write(const QByteArray &data)
0060 {
0061     if (!isAuthorized()) {
0062         sendErrorReply(QDBusError::AccessDenied);
0063         return;
0064     }
0065     m_job->write(data);
0066 }
0067 
0068 void File::close()
0069 {
0070     if (!isAuthorized()) {
0071         sendErrorReply(QDBusError::AccessDenied);
0072         return;
0073     }
0074     m_job->close();
0075 }
0076 
0077 void File::seek(qulonglong offset)
0078 {
0079     if (!isAuthorized()) {
0080         sendErrorReply(QDBusError::AccessDenied);
0081         return;
0082     }
0083     m_job->seek(offset);
0084 }
0085 
0086 void File::truncate(qulonglong length)
0087 {
0088     if (!isAuthorized()) {
0089         sendErrorReply(QDBusError::AccessDenied);
0090         return;
0091     }
0092     m_job->truncate(length);
0093 }
0094 
0095 qulonglong File::size()
0096 {
0097     if (!isAuthorized()) {
0098         sendErrorReply(QDBusError::AccessDenied);
0099         return 0;
0100     }
0101     return m_job->size();
0102 }