Verified Commit 586dd9b8 authored by Daniel Vrátil's avatar Daniel Vrátil 🤖
Browse files

SendLaterAgent: merge libKF5SendLater library into the agent

Summary:
The library has been split into two parts: the client-side code now
lives in MessageComposer where it's used exclusively, and all the code
that is used by the Agent has been moved in here.

Instead of the client writing into a shared config file, a new DBus
method has been added to the agent that the client can call in order to
schedule a new message for sending later.

Tests for classes that now live in MessageComposer have been moved to MessageComposer.

Reviewers: mlaurent

Reviewed By: mlaurent

Subscribers: kde-doc-english, kde-pim

Tags: #kde_pim, #documentation

Differential Revision: https://phabricator.kde.org/D29724
parent a866656c
......@@ -18,24 +18,26 @@ set(sendlateragentconfig_SRCS
sendlaterconfigurewidget.cpp
)
set(sendlateragent_SRCS
set(libsendlateragent_SRCS
${sendlateragentconfig_SRCS}
${sendlater_common_SRCS}
sendlateragent.cpp
sendlatermanager.cpp
sendlaterjob.cpp
sendlaterremovemessagejob.cpp
sendlaterutil.cpp
)
set(sendlateragent_SRCS
sendlateragent.cpp
)
qt5_add_dbus_adaptor(sendlateragent_SRCS org.freedesktop.Akonadi.SendLaterAgent.xml sendlateragent.h SendLaterAgent)
ki18n_wrap_ui(sendlateragent_SRCS ui/sendlaterconfigurewidget.ui)
add_executable(akonadi_sendlater_agent ${sendlateragent_SRCS})
target_link_libraries(akonadi_sendlater_agent
KF5::SendLater
ki18n_wrap_ui(libsendlateragent_SRCS ui/sendlaterconfigurewidget.ui)
kconfig_add_kcfg_files(libsendlateragent_SRCS sendlateragentsettings.kcfgc)
add_library(sendlateragent STATIC ${libsendlateragent_SRCS})
target_link_libraries(sendlateragent
KF5::AkonadiCore
KF5::AkonadiMime
KF5::MailTransportAkonadi
......@@ -43,7 +45,6 @@ target_link_libraries(akonadi_sendlater_agent
KF5::MessageComposer
KF5::MessageCore
KF5::PimCommon
KF5::AkonadiAgentBase
KF5::DBusAddons
KF5::WindowSystem
KF5::XmlGui
......@@ -51,9 +52,15 @@ target_link_libraries(akonadi_sendlater_agent
KF5::I18n
)
add_executable(akonadi_sendlater_agent ${sendlateragent_SRCS})
target_link_libraries(akonadi_sendlater_agent
sendlateragent
KF5::AkonadiAgentBase
)
if( APPLE )
set_target_properties(akonadi_sendlater_agent PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${kmail_SOURCE_DIR}/agents/Info.plist.template)
set_target_properties(akonadi_sendlater_agent PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER "org.kde.Akonadi.KF5::SendLater")
set_target_properties(akonadi_sendlater_agent PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "KDE Akonadi Send Later")
endif ()
......
# Convenience macro to add unit tests.
macro(add_sendlater_agent_test _source )
set(_test ${_source} ../sendlaterconfiguredialog.cpp ../sendlaterconfigurewidget.cpp)
ki18n_wrap_ui(_test ../ui/sendlaterconfigurewidget.ui)
get_filename_component(_name ${_source} NAME_WE)
ecm_add_test(${_test}
ecm_add_test(${_source}
TEST_NAME ${_name}
NAME_PREFIX "sendlateragent-"
LINK_LIBRARIES Qt5::Test KF5::XmlGui KF5::AkonadiCore KF5::SendLater KF5::PimCommon KF5::I18n
LINK_LIBRARIES sendlateragent Qt5::Test
)
endmacro()
add_sendlater_agent_test(sendlaterinfotest.cpp)
add_sendlater_agent_test(sendlaterutiltest.cpp)
add_sendlater_agent_test(sendlaterconfiguredialogtest.cpp)
add_sendlater_agent_test(sendlaterconfigtest.cpp)
add_sendlater_agent_test(sendlaterdialogtest.cpp)
......@@ -18,8 +18,10 @@
*/
#include "sendlaterconfigtest.h"
#include "sendlaterinfo.h"
#include "sendlaterutil.h"
#include <MessageComposer/SendLaterInfo>
#include <QTest>
SendLaterConfigTest::SendLaterConfigTest(QObject *parent)
......@@ -27,9 +29,7 @@ SendLaterConfigTest::SendLaterConfigTest(QObject *parent)
{
}
SendLaterConfigTest::~SendLaterConfigTest()
{
}
SendLaterConfigTest::~SendLaterConfigTest() = default;
void SendLaterConfigTest::init()
{
......@@ -62,14 +62,14 @@ void SendLaterConfigTest::shouldConfigBeEmpty()
void SendLaterConfigTest::shouldAddAnItem()
{
SendLater::SendLaterInfo info;
MessageComposer::SendLaterInfo info;
const QString to = QStringLiteral("kde.org");
info.setTo(to);
info.setItemId(Akonadi::Item::Id(42));
info.setSubject(QStringLiteral("Subject"));
info.setRecurrence(true);
info.setRecurrenceEachValue(5);
info.setRecurrenceUnit(SendLater::SendLaterInfo::Years);
info.setRecurrenceUnit(MessageComposer::SendLaterInfo::Years);
const QDate date(2014, 1, 1);
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
info.setDateTime(QDateTime(date));
......@@ -79,7 +79,7 @@ void SendLaterConfigTest::shouldAddAnItem()
info.setLastDateTimeSend(QDateTime(date.startOfDay()));
#endif
SendLater::SendLaterUtil::writeSendLaterInfo(mConfig, &info, false);
SendLaterUtil::writeSendLaterInfo(mConfig, &info);
const QStringList itemList = mConfig->groupList().filter(mSendlaterRegExpFilter);
QCOMPARE(itemList.isEmpty(), false);
......@@ -88,8 +88,8 @@ void SendLaterConfigTest::shouldAddAnItem()
void SendLaterConfigTest::shouldNotAddInvalidItem()
{
SendLater::SendLaterInfo info;
SendLater::SendLaterUtil::writeSendLaterInfo(mConfig, &info, false);
MessageComposer::SendLaterInfo info;
SendLaterUtil::writeSendLaterInfo(mConfig, &info);
const QStringList itemList = mConfig->groupList().filter(mSendlaterRegExpFilter);
QCOMPARE(itemList.isEmpty(), true);
......
/*
Copyright (C) 2014-2020 Laurent Montel <montel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "sendlaterinfotest.h"
#include "sendlaterinfo.h"
#include <QTest>
#include <QDateTime>
#include <KConfigGroup>
#include <KSharedConfig>
#include <QStandardPaths>
SendLaterInfoTest::SendLaterInfoTest(QObject *parent)
: QObject(parent)
{
QStandardPaths::setTestModeEnabled(true);
}
void SendLaterInfoTest::shouldHaveDefaultValue()
{
SendLater::SendLaterInfo info;
QCOMPARE(info.itemId(), Akonadi::Item::Id(-1));
QCOMPARE(info.isRecurrence(), false);
QVERIFY(!info.dateTime().isValid());
QVERIFY(!info.lastDateTimeSend().isValid());
QCOMPARE(info.to(), QString());
QCOMPARE(info.subject(), QString());
QVERIFY(!info.isValid());
QCOMPARE(info.recurrenceUnit(), SendLater::SendLaterInfo::Days);
QCOMPARE(info.recurrenceEachValue(), 1);
}
void SendLaterInfoTest::shouldRestoreFromSettings()
{
SendLater::SendLaterInfo info;
const QString to = QStringLiteral("kde.org");
info.setTo(to);
info.setItemId(Akonadi::Item::Id(42));
info.setSubject(QStringLiteral("Subject"));
info.setRecurrence(true);
info.setRecurrenceEachValue(5);
info.setRecurrenceUnit(SendLater::SendLaterInfo::Years);
const QDate date(2014, 1, 1);
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
info.setDateTime(QDateTime(date));
info.setLastDateTimeSend(QDateTime(date));
#else
info.setDateTime(QDateTime(date.startOfDay()));
info.setLastDateTimeSend(QDateTime(date.startOfDay()));
#endif
KConfigGroup grp(KSharedConfig::openConfig(), "testsettings");
info.writeConfig(grp);
SendLater::SendLaterInfo restoreInfo(grp);
QCOMPARE(info, restoreInfo);
}
void SendLaterInfoTest::shouldNotValidIfIdIsNotValid()
{
SendLater::SendLaterInfo info;
const QString to = QStringLiteral("kde.org");
info.setTo(to);
info.setSubject(QStringLiteral("Subject"));
info.setRecurrence(true);
info.setRecurrenceEachValue(5);
info.setRecurrenceUnit(SendLater::SendLaterInfo::Years);
const QDate date(2014, 1, 1);
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
info.setDateTime(QDateTime(date));
info.setLastDateTimeSend(QDateTime(date));
#else
info.setDateTime(QDateTime(date.startOfDay()));
info.setLastDateTimeSend(QDateTime(date.startOfDay()));
#endif
QVERIFY(!info.isValid());
}
void SendLaterInfoTest::shouldNotValidIfDateIsNotValid()
{
SendLater::SendLaterInfo info;
const QString to = QStringLiteral("kde.org");
info.setTo(to);
info.setItemId(Akonadi::Item::Id(42));
info.setSubject(QStringLiteral("Subject"));
info.setRecurrence(true);
info.setRecurrenceEachValue(5);
info.setRecurrenceUnit(SendLater::SendLaterInfo::Years);
const QDate date(2014, 1, 1);
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
info.setLastDateTimeSend(QDateTime(date));
#else
info.setLastDateTimeSend(QDateTime(date.startOfDay()));
#endif
QVERIFY(!info.isValid());
}
void SendLaterInfoTest::shouldCopyInfo()
{
SendLater::SendLaterInfo info;
const QString to = QStringLiteral("kde.org");
info.setTo(to);
info.setItemId(Akonadi::Item::Id(42));
info.setSubject(QStringLiteral("Subject"));
info.setRecurrence(true);
info.setRecurrenceEachValue(5);
info.setRecurrenceUnit(SendLater::SendLaterInfo::Years);
const QDate date(2014, 1, 1);
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
info.setDateTime(QDateTime(date));
info.setLastDateTimeSend(QDateTime(date));
#else
info.setDateTime(QDateTime(date.startOfDay()));
info.setLastDateTimeSend(QDateTime(date.startOfDay()));
#endif
SendLater::SendLaterInfo copyInfo(info);
QCOMPARE(info, copyInfo);
}
QTEST_MAIN(SendLaterInfoTest)
/*
Copyright (C) 2015-2020 Laurent Montel <montel@kde.org>
Copyright (C) 2014-2020 Laurent Montel <montel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
......@@ -17,42 +17,46 @@
Boston, MA 02110-1301, USA.
*/
#include "sendlaterdialogtest.h"
#include "sendlaterdialog.h"
#include <KTimeComboBox>
#include <KDateComboBox>
#include <QLineEdit>
#include <QPushButton>
#include "sendlaterutiltest.h"
#include "sendlaterutil.h"
#include <MessageComposer/SendLaterInfo>
#include <QTest>
#include <QDateTime>
#include <KConfigGroup>
#include <KSharedConfig>
#include <QStandardPaths>
SendLaterDialogTest::SendLaterDialogTest(QObject *parent)
: QObject(parent)
{
}
#include <sendlaterinfo.h>
SendLaterDialogTest::~SendLaterDialogTest()
{
}
void SendLaterDialogTest::initTestCase()
SendLaterUtilTest::SendLaterUtilTest(QObject *parent)
: QObject(parent)
{
QStandardPaths::setTestModeEnabled(true);
}
void SendLaterDialogTest::shouldHaveDefaultValue()
void SendLaterUtilTest::shouldRestoreFromSettings()
{
SendLater::SendLaterDialog dlg(nullptr);
KTimeComboBox *timeCombo = dlg.findChild<KTimeComboBox *>(QStringLiteral("time_sendlater"));
QVERIFY(timeCombo);
KDateComboBox *dateCombo = dlg.findChild<KDateComboBox *>(QStringLiteral("date_sendlater"));
QVERIFY(dateCombo);
QVERIFY(dateCombo->date().isValid());
QPushButton *okButton = dlg.findChild<QPushButton *>(QStringLiteral("okbutton"));
QVERIFY(okButton);
QVERIFY(okButton->isEnabled());
dateCombo->lineEdit()->clear();
QVERIFY(!dateCombo->date().isValid());
QVERIFY(!okButton->isEnabled());
MessageComposer::SendLaterInfo info;
const QString to = QStringLiteral("kde.org");
info.setTo(to);
info.setItemId(Akonadi::Item::Id(42));
info.setSubject(QStringLiteral("Subject"));
info.setRecurrence(true);
info.setRecurrenceEachValue(5);
info.setRecurrenceUnit(MessageComposer::SendLaterInfo::Years);
const QDate date(2014, 1, 1);
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
info.setDateTime(QDateTime(date));
info.setLastDateTimeSend(QDateTime(date));
#else
info.setDateTime(QDateTime(date.startOfDay()));
info.setLastDateTimeSend(QDateTime(date.startOfDay()));
#endif
SendLaterUtil::writeSendLaterInfo(KSharedConfig::openConfig(), &info);
KConfigGroup grp(KSharedConfig::openConfig(), SendLaterUtil::sendLaterPattern().arg(42));
std::unique_ptr<MessageComposer::SendLaterInfo> restoreInfo{SendLaterUtil::readSendLaterInfo(grp)};
QCOMPARE(info, *restoreInfo);
}
QTEST_MAIN(SendLaterDialogTest)
QTEST_MAIN(SendLaterUtilTest)
......@@ -17,23 +17,19 @@
Boston, MA 02110-1301, USA.
*/
#ifndef SENDLATERINFOTEST_H
#define SENDLATERINFOTEST_H
#ifndef SENDLATERUTILTEST_H
#define SENDLATERUTILTEST_H
#include <QObject>
class SendLaterInfoTest : public QObject
class SendLaterUtilTest : public QObject
{
Q_OBJECT
public:
explicit SendLaterInfoTest(QObject *parent = nullptr);
explicit SendLaterUtilTest(QObject *parent = nullptr);
private Q_SLOTS:
void shouldHaveDefaultValue();
void shouldRestoreFromSettings();
void shouldNotValidIfIdIsNotValid();
void shouldNotValidIfDateIsNotValid();
void shouldCopyInfo();
};
#endif // SENDLATERINFOTEST_H
#endif // SENDLATERUTILTEST_H
......@@ -14,5 +14,15 @@
<method name="removeItem" >
<arg name="item" type="x" direction="in"/>
</method>
<method name="addItem">
<arg name="datetime" type="x" direction="in" />
<arg name="recurrence" type="b" direction="in" />
<arg name="recurrenceValue" type="i" direction="in" />
<arg name="recurrenceUnit" type="i" direction="in" />
<arg name="itemId" type="x" direction="in" />
<arg name="subject" type="s" direction="in" />
<arg name="to" type="s" direction="in" />
</method>
</interface>
</node>
......@@ -42,6 +42,7 @@
#include <Kdelibs4ConfigMigrator>
#include <QPointer>
#include <sendlaterinfo.h>
//#define DEBUG_SENDLATERAGENT 1
......@@ -162,6 +163,22 @@ void SendLaterAgent::removeItem(qint64 item)
}
}
void SendLaterAgent::addItem(qint64 timestamp, bool recurrence, int recurrenceValue, int recurrenceUnit,
Akonadi::Item::Id id, const QString &subject, const QString &to)
{
auto info = new MessageComposer::SendLaterInfo;
info->setDateTime(QDateTime::fromSecsSinceEpoch(timestamp));
info->setRecurrence(recurrence);
info->setRecurrenceEachValue(recurrenceValue);
info->setRecurrenceUnit(static_cast<MessageComposer::SendLaterInfo::RecurrenceUnit>(recurrenceUnit));
info->setItemId(id);
info->setSubject(subject);
info->setTo(to);
SendLaterUtil::writeSendLaterInfo(SendLaterUtil::defaultConfig(), info);
reload();
}
void SendLaterAgent::slotSendNow(Akonadi::Item::Id id)
{
mManager->sendNow(id);
......
......@@ -21,7 +21,10 @@
#define SENDLATERAGENT_H
#include <agentbase.h>
#include "sendlaterdialog.h"
#include <MessageComposer/SendLaterInfo>
#include <sendlaterinfo.h>
class SendLaterManager;
......@@ -44,6 +47,8 @@ public Q_SLOTS:
void reload();
void configure(WId windowId) override;
void removeItem(qint64 item);
void addItem(qint64 timestamp, bool recurrence, int recurrenceValue, int recurrenceUnit,
Akonadi::Item::Id itemId, const QString &subject, const QString &to);
protected:
void itemsRemoved(const Akonadi::Item::List &item) override;
......
<?xml version="1.0" encoding="UTF-8"?>
<kcfg xmlns="http://www.kde.org/standards/kcfg/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0
http://www.kde.org/standards/kcfg/1.0/kcfg.xsd" >
<kcfgfile name="akonadi_sendlater_agentrc" />
<group name="General">
<entry name="enabled" key="enabled" type="Bool">
<default>true</default>
</entry>
</group>
</kcfg>
# Code generation options for kconfig_compiler
File=sendlateragentsettings.kcfg
ClassName=SendLaterAgentSettings
Singleton=true
Mutators=true
......@@ -18,10 +18,12 @@
*/
#include "sendlaterconfigurewidget.h"
#include "sendlaterinfo.h"
#include "sendlaterutil.h"
#include "sendlaterdialog.h"
#include <MessageComposer/SendLaterInfo>
#include <MessageComposer/SendLaterDialog>
#include <KConfigGroup>
#include <KLocalizedString>
#include <QMenu>
......@@ -48,12 +50,12 @@ SendLaterItem::~SendLaterItem()
delete mInfo;
}
void SendLaterItem::setInfo(SendLater::SendLaterInfo *info)
void SendLaterItem::setInfo(MessageComposer::SendLaterInfo *info)
{
mInfo = info;
}
SendLater::SendLaterInfo *SendLaterItem::info() const
MessageComposer::SendLaterInfo *SendLaterItem::info() const
{
return mInfo;
}
......@@ -147,12 +149,12 @@ void SendLaterWidget::updateButtons()
void SendLaterWidget::load()
{
KSharedConfig::Ptr config = KSharedConfig::openConfig();
KSharedConfig::Ptr config = SendLaterUtil::defaultConfig();
const QStringList filterGroups = config->groupList().filter(QRegularExpression(sendLaterItemPattern()));
const int numberOfItem = filterGroups.count();
for (int i = 0; i < numberOfItem; ++i) {
KConfigGroup group = config->group(filterGroups.at(i));
SendLater::SendLaterInfo *info = new SendLater::SendLaterInfo(group);
auto info = SendLaterUtil::readSendLaterInfo(group);
if (info->isValid()) {
createOrUpdateItem(info);
} else {
......@@ -162,7 +164,7 @@ void SendLaterWidget::load()
mWidget->treeWidget->setShowDefaultText(numberOfItem == 0);
}
void SendLaterWidget::createOrUpdateItem(SendLater::SendLaterInfo *info, SendLaterItem *item)
void SendLaterWidget::createOrUpdateItem(MessageComposer::SendLaterInfo *info, SendLaterItem *item)
{
if (!item) {
item = new SendLaterItem(mWidget->treeWidget);
......@@ -183,7 +185,7 @@ void SendLaterWidget::save()
if (!mChanged) {
return;
}
KSharedConfig::Ptr config = KSharedConfig::openConfig();
KSharedConfig::Ptr config = SendLaterUtil::defaultConfig();
// first, delete all filter groups:
const QStringList filterGroups = config->groupList().filter(QRegularExpression(sendLaterItemPattern()));
......@@ -196,8 +198,7 @@ void SendLaterWidget::save()
for (int i = 0; i < numberOfItem; ++i) {
SendLaterItem *mailItem = static_cast<SendLaterItem *>(mWidget->treeWidget->topLevelItem(i));
if (mailItem->info()) {
KConfigGroup group = config->group(SendLater::SendLaterUtil::sendLaterPattern().arg(mailItem->info()->itemId()));
mailItem->info()->writeConfig(group);
SendLaterUtil::writeSendLaterInfo(config, mailItem->info());
}
}
config->sync();
......@@ -248,9 +249,9 @@ void SendLaterWidget::slotModifyItem()
}
SendLaterItem *mailItem = static_cast<SendLaterItem *>(item);
QPointer<SendLater::SendLaterDialog> dialog = new SendLater::SendLaterDialog(mailItem->info(), this);
QPointer<MessageComposer::SendLaterDialog> dialog = new MessageComposer::SendLaterDialog(mailItem->info(), this);
if (dialog->exec()) {
SendLater::SendLaterInfo *info = dialog->info();
auto info = dialog->info();
createOrUpdateItem(info, mailItem);
mChanged = true;
}
......@@ -261,7 +262,7 @@ void SendLaterWidget::slotModifyItem()
void SendLaterWidget::needToReload()
{
mWidget->treeWidget->clear();
KSharedConfig::Ptr config = KSharedConfig::openConfig();
KSharedConfig::Ptr config = SendLaterUtil::defaultConfig();
config->reparseConfiguration();
load();
}
......
......@@ -26,7 +26,7 @@
#include <QTreeWidgetItem>
#include <KConfigGroup>
namespace SendLater {
namespace MessageComposer {
class SendLaterInfo;
}
......@@ -36,11 +36,11 @@ public:
explicit SendLaterItem(QTreeWidget *parent = nullptr);
~SendLaterItem();
void setInfo(SendLater::SendLaterInfo *info);
SendLater::SendLaterInfo *info() const;
void setInfo(MessageComposer::SendLaterInfo *info);
MessageComposer::SendLaterInfo *info() const;
private:
SendLater::SendLaterInfo *mInfo = nullptr;
MessageComposer::SendLaterInfo *mInfo = nullptr;
};
class SendLaterWidget : public QWidget
......@@ -73,7 +73,7 @@ private:
void updateButtons();
void slotCustomContextMenuRequested(const QPoint &);
void slotSendNow();
void createOrUpdateItem(SendLater::SendLaterInfo *info, SendLaterItem *item = nullptr);
void createOrUpdateItem(MessageComposer::SendLaterInfo *info, SendLaterItem *item = nullptr);
void load();