KDE PIM support for the p≡p engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

170 lines
4.5 KiB

/*
SPDX-FileCopyrightText: 2015-2020 Laurent Montel <montel@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "viewerpluginpepinterface.h"
#include "pepjob.h"
#include "pepplugin_debug.h"
#include <KLocalizedString>
#include <KActionCollection>
#include <QLayout>
#include <QLabel>
#include <QIcon>
#include <QAction>
#include <QPushButton>
#include <QPainter>
#include <QColor>
#include <QStringLiteral>
#include <QByteArray>
#include <QDBusInterface>
#include <QDBusReply>
#include <ItemFetchJob>
#include <ItemFetchScope>
#include <MessageViewer/CSSHelper>
#include <KontactInterface/Core>
#include <KontactInterface/PimUniqueApplication>
#include <AkonadiCore/ServerManager>
using namespace MessageViewer;
pEpViewerInterface::pEpViewerInterface(KActionCollection *ac, QWidget *parent)
: ViewerPluginInterface(parent)
{
QDBusInterface *pEpAgent = nullptr;
mStatusWidget = new QWidget(parent);
mStatusWidget->setFixedHeight(20);
parent->layout()->addWidget(mStatusWidget);
const QString service = Akonadi::ServerManager::self()->agentServiceName(Akonadi::ServerManager::Agent, QStringLiteral("pEpAgent"));
qDebug() << service;
m_pEpAgent = new QDBusInterface(
service,
QStringLiteral("/pEpAgent"),
QStringLiteral("org.freedesktop.Akonadi.Agent.pEpAgent"),
QDBusConnection::sessionBus()
);
}
pEpViewerInterface::~pEpViewerInterface()
{
qDebug() << __FUNCTION__;
}
QList<QAction *> pEpViewerInterface::actions() const
{
qDebug() << __FUNCTION__;
return mAction;
}
bool pEpViewerInterface::isApEpMessage(KMime::Content *content)
{
if(!content)
return false;
if(content->attachments().size()) {
qDebug() << __FUNCTION__ << "Has attachments";
for(KMime::Content *c : content->attachments()) {
//if(isApEpMessage(c)) return true;
isApEpMessage(c);
}
} else {
//qDebug() << content->decodedContent();
}
qDebug() << __FUNCTION__ << "Not a pEp message";
//qDebug() << content->contentType()->name();
return false;
}
void pEpViewerInterface::decryptMessage()
{
QByteArray type;
QByteArray pgpText;
if(!m_pEpAgent->isValid()) {
// We're not connected to the agent
qDebug() << "DBus not connected";
return;
}
foreach ( KMime::Content *attachment, mMessage->contents() ) {
type = attachment->contentType()->mimeType();
if(type==QByteArray("application/octet-stream")) {
//attachment->setContent(QByteArray("pEp"));
//attachment->contentType()->setMimeType(QByteArray("text/plain"));
//mMessage->contents().removeOne(attachment);
pgpText=attachment->decodedContent();
} else if(type==QByteArray("application/pgp-encrypted")) {
//attachment->setContent(QByteArray("pEp"));
//attachment->contentType()->setMimeType(QByteArray("text/plain"));
} else {
qDebug() << type;
}
}
//mMessage->contentType()->setMimeType(QByteArray("text/plain"));
qDebug() << m_pEpAgent->call(QStringLiteral("decryptMessage"), pgpText);
}
void pEpViewerInterface::setMessage(const KMime::Message::Ptr &value)
{
QByteArray type;
QDBusReply<QVariant> reply;
QVariant testload = QVariant(QStringLiteral("test"));
if(value) {
mMessage = value;
type = mMessage->contentType()->mimeType();
if(type==QByteArray("multipart/mixed")) {
qDebug() << "Multipart detected";
} else if(type==QByteArray("multipart/encrypted")) {
qDebug() << "Encrypted message detected";
decryptMessage();
} else {
qDebug() << type;
}
//value->mainBodyPart()->assemble();
//if(isApEpMessage(value->mainBodyPart())) qDebug() << "pEp message detected";
//else qDebug() << "Not a pEp message";
}
}
void pEpViewerInterface::closePlugin()
{
qDebug() << __FUNCTION__;
}
void pEpViewerInterface::showWidget()
{
qDebug() << __FUNCTION__;
}
void pEpViewerInterface::setMessageItem(const Akonadi::Item &item)
{
mMessageItem = item;
if(item.hasPayload()) {
setMessage(item.payload<KMime::Message::Ptr>());
}
}
ViewerPluginInterface::SpecificFeatureTypes pEpViewerInterface::featureTypes() const
{
qDebug() << __FUNCTION__;
return ViewerPluginInterface::NeedMessage;
}
void pEpViewerInterface::updateAction(const Akonadi::Item &item)
{
qDebug() << __FUNCTION__;
mMessageItem = item;
if(item.hasPayload()) {
setMessage(item.payload<KMime::Message::Ptr>());
}
}