File indexing completed on 2024-12-22 05:01:11

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "dndfromarkjob.h"
0008 #include "editor/kmcomposerwin.h"
0009 #include "kmail_debug.h"
0010 
0011 #include <QDBusConnection>
0012 #include <QDBusMessage>
0013 #include <QDir>
0014 #include <QMimeData>
0015 #include <QTemporaryDir>
0016 #include <QUrl>
0017 
0018 #include "editor/composer.h"
0019 
0020 DndFromArkJob::DndFromArkJob(QObject *parent)
0021     : QObject(parent)
0022 {
0023 }
0024 
0025 bool DndFromArkJob::dndFromArk(const QMimeData *source)
0026 {
0027     if (source->hasFormat(QStringLiteral("application/x-kde-ark-dndextract-service"))
0028         && source->hasFormat(QStringLiteral("application/x-kde-ark-dndextract-path"))) {
0029         return true;
0030     }
0031     return false;
0032 }
0033 
0034 bool DndFromArkJob::extract(const QMimeData *source)
0035 {
0036     bool result = false;
0037     if (dndFromArk(source)) {
0038         if (!mComposerWin) {
0039             qCWarning(KMAIL_LOG) << "mComposerWin is null, it's a bug";
0040             deleteLater();
0041             return result;
0042         }
0043         const QString remoteDBusClient = QString::fromLatin1(source->data(QStringLiteral("application/x-kde-ark-dndextract-service")));
0044         const QString remoteDBusPath = QString::fromLatin1(source->data(QStringLiteral("application/x-kde-ark-dndextract-path")));
0045 
0046         const QString tmpPath = QDir::tempPath() + QLatin1StringView("/attachments_ark");
0047         QDir().mkpath(tmpPath);
0048 
0049         auto linkDir = new QTemporaryDir(tmpPath);
0050         const QString arkPath = linkDir->path();
0051         QDBusMessage message = QDBusMessage::createMethodCall(remoteDBusClient,
0052                                                               remoteDBusPath,
0053                                                               QStringLiteral("org.kde.ark.DndExtract"),
0054                                                               QStringLiteral("extractSelectedFilesTo"));
0055         message.setArguments({arkPath});
0056         QDBusConnection::sessionBus().call(message);
0057         QDir dir(arkPath);
0058         const QStringList list = dir.entryList(QDir::NoDotAndDotDot | QDir::Files);
0059         QList<KMail::Composer::AttachmentInfo> infoList;
0060         infoList.reserve(list.size());
0061         for (int i = 0; i < list.size(); ++i) {
0062             KMail::Composer::AttachmentInfo info;
0063             info.url = QUrl::fromLocalFile(list.at(i));
0064             infoList.append(info);
0065         }
0066         mComposerWin->addAttachment(infoList, false);
0067         delete linkDir;
0068         result = true;
0069     }
0070     deleteLater();
0071     return result;
0072 }
0073 
0074 void DndFromArkJob::setComposerWin(KMComposerWin *composerWin)
0075 {
0076     mComposerWin = composerWin;
0077 }
0078 
0079 #include "moc_dndfromarkjob.cpp"