diff --git a/kmail/autotests/CMakeLists.txt b/kmail/autotests/CMakeLists.txt --- a/kmail/autotests/CMakeLists.txt +++ b/kmail/autotests/CMakeLists.txt @@ -35,6 +35,32 @@ ecm_mark_as_test(cryptostateindicatorwidgettest) target_link_libraries( cryptostateindicatorwidgettest Qt5::Test KF5::MessageCore Qt5::Widgets KF5::ConfigWidgets KF5::I18n) +set(kmail_kmcommandstest_source kmcommandstest.cpp ../kmcommands.cpp ../util.cpp ../secondarywindow.cpp ../undostack.cpp ../kmail_debug.cpp) +add_executable(kmcommandstest ${kmail_kmcommandstest_source}) +add_test(kmcommandstest kmcommandstest) +ecm_mark_as_test(kmcommandstest) +target_link_libraries(kmcommandstest +Qt5::Test +Qt5::Widgets +KF5::AkonadiCore +KF5::Bookmarks +KF5::ConfigWidgets +KF5::Contacts +KF5::I18n +KF5::IconThemes +KF5::IdentityManagement +KF5::KIOCore +KF5::KIOFileWidgets +KF5::MessageCore +KF5::MessageComposer +KF5::MessageList +KF5::MessageViewer +KF5::MailCommon +KF5::MailTransport +KF5::Libkdepim +KF5::TemplateParser +kmailprivate +) set( kmail_kactionmenutransporttest_source kactionmenutransporttest.cpp ../widgets/kactionmenutransport.cpp) add_executable( kactionmenutransporttest ${kmail_kactionmenutransporttest_source}) diff --git a/kmail/autotests/kmcommandstest.h b/kmail/autotests/kmcommandstest.h new file mode 100644 --- /dev/null +++ b/kmail/autotests/kmcommandstest.h @@ -0,0 +1,45 @@ +/* + Copyright (c) 2016 Sandro Knauß + + 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; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef KMCOMMANDSTEST_H +#define KMCOMMANDSTEST_H + +#include + +class KMKernel; + +class KMCommandsTest : public QObject +{ + Q_OBJECT +public: + explicit KMCommandsTest(QObject *parent = Q_NULLPTR); + ~KMCommandsTest(); +private Q_SLOTS: + void testMailtoReply(); + void testReply(); + void testReplyWithoutDefaultGPGSign(); + void initTestCase(); +private: + void resetIdentities(); + void verifySignature(bool sign); + void verifyEncryption(bool encrypt); + void waitForMainWindowToClose(); + KMKernel *mKernel; +}; + +#endif // KMCOMMANDSTEST_H \ No newline at end of file diff --git a/kmail/autotests/kmcommandstest.cpp b/kmail/autotests/kmcommandstest.cpp new file mode 100644 --- /dev/null +++ b/kmail/autotests/kmcommandstest.cpp @@ -0,0 +1,244 @@ +/* + Copyright (c) 2016 Sandro Knauß + + 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; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "kmcommandstest.h" +#include "kmcommands.h" +#include "kmkernel.h" +#include +#include + +#include +#include + +#include + +#include +#include +#include +#include +#include + +Akonadi::Item createItem(const KIdentityManagement::Identity &ident) +{ + QByteArray data = + "From: Konqui \n" + "To: Friends \n" + "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\n" + "Subject: Sample message\n" + "MIME-Version: 1.0\n" + "X-KMail-Identity: "+ QByteArray::number(ident.uoid()) +"\n" + "Content-type: text/plain; charset=us-ascii\n" + "\n" + "\n" + "This is explicitly typed plain US-ASCII text.\n" + "It DOES end with a linebreak.\n" + "\n"; + + KMime::Message::Ptr msgPtr = KMime::Message::Ptr(new KMime::Message()); + Akonadi::Item item; + Akonadi::Collection col(0); + msgPtr->setContent(data); + msgPtr->parse(); + item.setPayload(msgPtr); + item.setParentCollection(col); + + return item; +} + +KMCommandsTest::KMCommandsTest(QObject *parent) + : QObject(parent) + , mKernel(new KMKernel(parent)) +{ + +} + +KMCommandsTest::~KMCommandsTest() +{ + delete mKernel; +} + +void KMCommandsTest::initTestCase() +{ + auto &def = mKernel->identityManager()->defaultIdentity(); + auto &i1 = mKernel->identityManager()->modifyIdentityForUoid(def.uoid()); + i1.setIdentityName(QStringLiteral("default")); + mKernel->identityManager()->newFromScratch(QStringLiteral("test2")); + mKernel->identityManager()->newFromScratch(QStringLiteral("test3")); + mKernel->identityManager()->commit(); +} + +void KMCommandsTest::resetIdentities() +{ + auto &i1 = mKernel->identityManager()->modifyIdentityForName(QStringLiteral("default")); + i1.setFullName(QStringLiteral("default")); + i1.setPrimaryEmailAddress(QStringLiteral("firstname.lastname@example.com")); + i1.setPGPSigningKey("0x123456789"); + i1.setPgpAutoSign(true); + auto &i2 = mKernel->identityManager()->modifyIdentityForName(QStringLiteral("test2")); + i2.setFullName(QStringLiteral("second")); + i2.setPrimaryEmailAddress(QStringLiteral("secundus@example.com")); + i2.setPGPSigningKey("0x234567890"); + i2.setPgpAutoSign(false); + auto &i3 = mKernel->identityManager()->modifyIdentityForName(QStringLiteral("test3")); + i3.setFullName(QStringLiteral("third")); + i3.setPrimaryEmailAddress(QStringLiteral("drei@example.com")); + i3.setPGPSigningKey("0x345678901"); + i3.setPgpAutoSign(true); + mKernel->identityManager()->commit(); +} + +void KMCommandsTest::verifyEncryption(bool encrypt) +{ + const auto w = mKernel->mainWin(); + QLabel *encryption = w->findChild(QStringLiteral("encryptionindicator")); + QVERIFY(encryption); + QCOMPARE(encryption->isVisible(), encrypt); +} + +void KMCommandsTest::verifySignature(bool sign) +{ + const auto w = mKernel->mainWin(); + QLabel *signature = w->findChild(QStringLiteral("signatureindicator")); + QVERIFY(signature); + QCOMPARE(signature->isVisible(), sign); +} + +void KMCommandsTest::testMailtoReply() +{ + resetIdentities(); + { // default has auto sign set -> verifySignature = true + auto &ident = mKernel->identityManager()->defaultIdentity(); + Akonadi::Item item(createItem(ident)); + + KMMailtoReplyCommand *cmd(new KMMailtoReplyCommand(Q_NULLPTR, QStringLiteral("mailto:test@example.com") , item, QString())); + cmd->start(); + verifySignature(true); + waitForMainWindowToClose(); + } + { // secundus has no auto sign set -> verifySignature = false + auto &ident = mKernel->identityManager()->identityForAddress(QStringLiteral("secundus@example.com")); + Akonadi::Item item(createItem(ident)); + + KMMailtoReplyCommand *cmd(new KMMailtoReplyCommand(Q_NULLPTR, QStringLiteral("mailto:test@example.com") , item, QString())); + cmd->start(); + verifySignature(false); + waitForMainWindowToClose(); + } + { // drei has auto sign set -> verifySignature = true + auto &ident = mKernel->identityManager()->identityForAddress(QStringLiteral("drei@example.com")); + Akonadi::Item item(createItem(ident)); + + KMMailtoReplyCommand *cmd(new KMMailtoReplyCommand(Q_NULLPTR, QStringLiteral("mailto:test@example.com") , item, QString())); + cmd->start(); + verifySignature(true); + waitForMainWindowToClose(); + } +} + +void KMCommandsTest::testReply() +{ + resetIdentities(); + { // default has auto sign set -> verifySignature = true + auto &ident = mKernel->identityManager()->defaultIdentity(); + Akonadi::Item item(createItem(ident)); + + KMReplyCommand *cmd(new KMReplyCommand(Q_NULLPTR, item, MessageComposer::ReplyAll)); + cmd->start(); + verifySignature(true); + waitForMainWindowToClose(); + } + { // secundus has no auto sign set -> verifySignature = false + auto &ident = mKernel->identityManager()->identityForAddress(QStringLiteral("secundus@example.com")); + Akonadi::Item item(createItem(ident)); + + KMReplyCommand *cmd(new KMReplyCommand(Q_NULLPTR, item, MessageComposer::ReplyAll)); + cmd->start(); + verifySignature(false); + waitForMainWindowToClose(); + } + { // drei has auto sign set -> verifySignature = true + auto &ident = mKernel->identityManager()->identityForAddress(QStringLiteral("drei@example.com")); + Akonadi::Item item(createItem(ident)); + + KMReplyCommand *cmd(new KMReplyCommand(Q_NULLPTR, item, MessageComposer::ReplyAll)); + cmd->start(); + verifySignature(true); + waitForMainWindowToClose(); + } +} + +void KMCommandsTest::testReplyWithoutDefaultGPGSign() +{ + resetIdentities(); + auto &i1 = mKernel->identityManager()->modifyIdentityForName(QStringLiteral("default")); + i1.setPgpAutoSign(false); + mKernel->identityManager()->commit(); + + { // default has auto sign set -> verifySignature = true + auto &ident = mKernel->identityManager()->defaultIdentity(); + Akonadi::Item item(createItem(ident)); + + KMReplyCommand *cmd(new KMReplyCommand(Q_NULLPTR, item, MessageComposer::ReplyAll)); + cmd->start(); + verifySignature(false); + waitForMainWindowToClose(); + } + { // secundus has no auto sign set -> verifySignature = false + auto &ident = mKernel->identityManager()->identityForAddress(QStringLiteral("secundus@example.com")); + Akonadi::Item item(createItem(ident)); + + KMReplyCommand *cmd(new KMReplyCommand(Q_NULLPTR, item, MessageComposer::ReplyAll)); + cmd->start(); + verifySignature(false); + waitForMainWindowToClose(); + } + { // drei has auto sign set -> verifySignature = true + auto &ident = mKernel->identityManager()->identityForAddress(QStringLiteral("drei@example.com")); + Akonadi::Item item(createItem(ident)); + + KMReplyCommand *cmd(new KMReplyCommand(Q_NULLPTR, item, MessageComposer::ReplyAll)); + cmd->start(); + verifySignature(true); + waitForMainWindowToClose(); + } +} + +void KMCommandsTest::waitForMainWindowToClose() +{ + auto w = mKernel->mainWin(); + QEventLoop loop; + loop.connect(w, &QMainWindow::destroyed, &loop, &QEventLoop::quit); + w->close(); + loop.exec(); +} + + +int main(int argc, char *argv[]) +{ + /*QTemporaryDir config; + QTemporaryDir kdehome; + + setenv("LC_ALL", "C", 1); + setenv("XDG_CONFIG_HOME", config.path().toUtf8(), 1); + setenv("KDEHOME", kdehome.path().toUtf8(), 1);*/ + QApplication app(argc, argv); + app.setAttribute(Qt::AA_Use96Dpi, true); + QTEST_DISABLE_KEYPAD_NAVIGATION + KMCommandsTest tc; + return QTest::qExec(&tc, argc, argv); +} \ No newline at end of file diff --git a/kmail/editor/composer.h b/kmail/editor/composer.h --- a/kmail/editor/composer.h +++ b/kmail/editor/composer.h @@ -20,6 +20,7 @@ #ifndef __KMAIL_COMPOSER_H__ #define __KMAIL_COMPOSER_H__ +#include "kmail_export.h" #include "secondarywindow.h" #include @@ -34,7 +35,7 @@ namespace KMail { -class Composer : public KMail::SecondaryWindow +class KMAIL_EXPORT Composer : public KMail::SecondaryWindow { Q_OBJECT protected: @@ -150,7 +151,7 @@ virtual void addAttach(KMime::Content *msgPart) = 0; }; -Composer *makeComposer(const KMime::Message::Ptr &msg = KMime::Message::Ptr(), bool lastSignState = false, bool lastEncryptState = false, +KMAIL_EXPORT Composer *makeComposer(const KMime::Message::Ptr &msg = KMime::Message::Ptr(), bool lastSignState = false, bool lastEncryptState = false, Composer::TemplateContext context = Composer::NoTemplate, uint identity = 0, const QString &textSelection = QString(), const QString &customTemplate = QString()); diff --git a/kmail/editor/kmcomposewin.cpp b/kmail/editor/kmcomposewin.cpp --- a/kmail/editor/kmcomposewin.cpp +++ b/kmail/editor/kmcomposewin.cpp @@ -299,6 +299,7 @@ KIdentityManagement::IdentityCombo *identity = new KIdentityManagement::IdentityCombo(kmkernel->identityManager(), mHeadersArea); identity->setToolTip(i18n("Select an identity for this message")); + identity->setCurrentIdentity(mId); mComposerBase->setIdentityCombo(identity); sigController->setIdentityCombo(identity); @@ -1408,6 +1409,7 @@ { const KIdentityManagement::Identity &ident = KMKernel::self()->identityManager()->identityForUoidOrDefault(mComposerBase->identityCombo()->currentIdentity()); + if (!Kleo::CryptoBackendFactory::instance()->openpgp() && !Kleo::CryptoBackendFactory::instance()->smime()) { // no crypto whatsoever mEncryptAction->setEnabled(false); @@ -1584,6 +1586,8 @@ // manually load the identity's value into the fields slotIdentityChanged(newId, true /*initalChange*/); + // Fixing the identitis with auto signing activated + mLastSignActionState = mSignAction->isChecked(); } const KIdentityManagement::Identity &ident = im->identityForUoid(mComposerBase->identityCombo()->currentIdentity()); diff --git a/kmail/kmmainwin.h b/kmail/kmmainwin.h --- a/kmail/kmmainwin.h +++ b/kmail/kmmainwin.h @@ -20,6 +20,8 @@ #ifndef __KMMAINWIN #define __KMMAINWIN +#include "kmail_export.h" + #include class KMMainWidget; @@ -29,7 +31,7 @@ { class ProgressStatusBarWidget; } -class KMMainWin : public KXmlGuiWindow +class KMAIL_EXPORT KMMainWin : public KXmlGuiWindow { Q_OBJECT diff --git a/kmail/kmreadermainwin.h b/kmail/kmreadermainwin.h --- a/kmail/kmreadermainwin.h +++ b/kmail/kmreadermainwin.h @@ -3,6 +3,8 @@ #ifndef KMReaderMainWin_h #define KMReaderMainWin_h +#include "kmail_export.h" + #include "secondarywindow.h" #include @@ -30,7 +32,7 @@ } Q_DECLARE_METATYPE(QModelIndex) -class KMReaderMainWin : public KMail::SecondaryWindow +class KMAIL_EXPORT KMReaderMainWin : public KMail::SecondaryWindow { Q_OBJECT diff --git a/kmail/kmreaderwin.h b/kmail/kmreaderwin.h --- a/kmail/kmreaderwin.h +++ b/kmail/kmreaderwin.h @@ -21,6 +21,8 @@ #ifndef KMREADERWIN_H #define KMREADERWIN_H +#include "kmail_export.h" + #include #include #include @@ -45,7 +47,7 @@ used for reading or viewing messages. */ -class KMReaderWin: public QWidget +class KMAIL_EXPORT KMReaderWin: public QWidget { Q_OBJECT