[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [KSecretService] 8bcff0e: Add the architecture for UI frameworks for
From: Michael Leupold <lemma () confuego ! org>
Date: 2010-11-09 19:14:27
Message-ID: 20101109191427.1E44DA60E0 () git ! kde ! org
[Download RAW message or body]
A backend/tests/dialoguifactorytest.h [License: GPL(v2)]
A backend/tests/dialoguifactorytest.cpp [License: GPL(v2)]
A backend/dialoguifactory.h [License: GPL(v2)]
A backend/dialoguifactory.cpp [License: GPL(v2)]
A backend/abstractuimanager.h [License: GPL(v2)]
A backend/abstractuijobs.h [License: GPL(v2)]
A backend/abstractuijobs.cpp [License: GPL(v2)]
commit 8bcff0e9bbe91c76b6715c9672ce72594ea4c732
Author: Michael Leupold <lemma@confuego.org>
Date: Sat Aug 28 21:46:02 2010 +0000
Add the architecture for UI frameworks for the daemon using QueuedJob as its base.
svn path=/trunk/playground/base/ksecretservice/; revision=1169263
diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt
index f5e127b..a081412 100644
--- a/backend/CMakeLists.txt
+++ b/backend/CMakeLists.txt
@@ -18,6 +18,9 @@ SET (ksecretservice_backend_SRCS
ksecret/ksecretcollection.cpp
ksecret/ksecretitem.cpp
ksecret/ksecretjobs.cpp
+ # user interface
+ abstractuijobs.cpp
+ dialoguifactory.cpp
)
KDE4_ADD_LIBRARY (ksecretservicebackend STATIC ${ksecretservice_backend_SRCS})
diff --git a/backend/abstractuijobs.cpp b/backend/abstractuijobs.cpp
new file mode 100644
index 0000000..b8304b0
--- /dev/null
+++ b/backend/abstractuijobs.cpp
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2010, Michael Leupold <lemma@confuego.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) version 3 or any later version
+ * accepted by the membership of KDE e.V. (or its successor approved
+ * by the membership of KDE e.V.), which shall act as a proxy
+ * defined in Section 14 of version 3 of the license.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "abstractuijobs.h"
+#include "abstractuimanager.h"
+
+#include <QtCore/QEventLoop>
+#include <QtCore/QTimer>
+
+AbstractUiJob::AbstractUiJob(AbstractUiManager *manager) : QueuedJob(manager)
+{
+}
+
+AbstractUiJob::~AbstractUiJob()
+{
+}
+
+void AbstractUiJob::exec()
+{
+ Q_ASSERT(false);
+}
+
+AbstractAskPasswordJob::AbstractAskPasswordJob(AbstractUiManager *manager,
+ const QString &collection,
+ bool secondTry)
+ : AbstractUiJob(manager), m_collection(collection), m_secondTry(secondTry)
+{
+}
+
+AbstractAskPasswordJob::~AbstractAskPasswordJob()
+{
+}
+
+const QString &AbstractAskPasswordJob::collection() const
+{
+ return m_collection;
+}
+
+bool AbstractAskPasswordJob::isSecondTry() const
+{
+ return m_secondTry;
+}
+
+bool AbstractAskPasswordJob::result() const
+{
+ return m_result;
+}
+
+const QCA::SecureArray &AbstractAskPasswordJob::password() const
+{
+ return m_password;
+}
+
+void AbstractAskPasswordJob::setResult(bool result)
+{
+ m_result = result;
+}
+
+void AbstractAskPasswordJob::setPassword(const QCA::SecureArray &password)
+{
+ m_password = password;
+}
+
+#include "abstractuijobs.moc"
diff --git a/backend/abstractuijobs.h b/backend/abstractuijobs.h
new file mode 100644
index 0000000..c9ad9aa
--- /dev/null
+++ b/backend/abstractuijobs.h
@@ -0,0 +1,137 @@
+/*
+ * Copyright 2010, Michael Leupold <lemma@confuego.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) version 3 or any later version
+ * accepted by the membership of KDE e.V. (or its successor approved
+ * by the membership of KDE e.V.), which shall act as a proxy
+ * defined in Section 14 of version 3 of the license.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef ABSTRACTUIJOBS_H
+#define ABSTRACTUIJOBS_H
+
+#include <queuedjob.h>
+#include <jobqueue.h>
+
+#include <QtCore/QQueue>
+#include <QtCrypto/QtCrypto>
+#include <kglobal.h>
+#include <kjob.h>
+
+class AbstractUiManager;
+
+/*
+ * Abstract master-class of user interface jobs.
+ */
+class AbstractUiJob : public QueuedJob
+{
+ Q_OBJECT
+
+public:
+ /**
+ * Constructor.
+ *
+ * @param manager user interface job manager and parent object
+ */
+ explicit AbstractUiJob(AbstractUiManager *manager);
+
+ /**
+ * Destructor.
+ */
+ virtual ~AbstractUiJob();
+
+ /**
+ * Default implementation for user interface jobs. As a ui job can rarely be
+ * run synchronously, exec() crashes.
+ */
+ virtual void exec();
+
+private:
+ friend class UiJobManager;
+};
+
+/**
+ * Job that asks a user for a password to unlock a collection.
+ */
+class AbstractAskPasswordJob : public AbstractUiJob
+{
+ Q_OBJECT
+
+public:
+ /**
+ * Constructor.
+ *
+ * @param manager ui job manager and parent object
+ * @param collection label of the collection that should be opened
+ * @param secondTry set to true if this is not the user's first try to enter
+ * the password correctly
+ */
+ AbstractAskPasswordJob(AbstractUiManager *manager, const QString &collection,
+ bool secondTry);
+
+ /**
+ * Destructor.
+ */
+ virtual ~AbstractAskPasswordJob();
+
+ /**
+ * Get the label of the collection that's about to be unlocked.
+ */
+ const QString &collection() const;
+
+ /**
+ * Check whether this is not the user's first try to enter the password.
+ */
+ bool isSecondTry() const;
+
+ /**
+ * Get the result of the operation.
+ *
+ * @return true if the user entered a password, false if not
+ */
+ bool result() const;
+
+ /**
+ * Get the password the user entered.
+ *
+ * @return the password entered by the user or an empty array if the user
+ * didn't enter a password
+ */
+ const QCA::SecureArray &password() const;
+
+protected:
+ /**
+ * Set the result of the operation.
+ *
+ * @remarks this is used by derived classes
+ * @param result result of the operation
+ */
+ void setResult(bool result);
+
+ /**
+ * Set the password entered by the user.
+ *
+ * @remarks This is used by derived classes
+ * @param password password entered by the user
+ */
+ void setPassword(const QCA::SecureArray &password);
+
+private:
+ QString m_collection; // name of the collection to be unlocked
+ bool m_secondTry; // true if this is not the first try
+ bool m_result; // the result of the operation
+ QCA::SecureArray m_password; // the password entered by the user
+};
+
+#endif
diff --git a/backend/abstractuimanager.h b/backend/abstractuimanager.h
new file mode 100644
index 0000000..24e0d06
--- /dev/null
+++ b/backend/abstractuimanager.h
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2010, Michael Leupold <lemma@confuego.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) version 3 or any later version
+ * accepted by the membership of KDE e.V. (or its successor approved
+ * by the membership of KDE e.V.), which shall act as a proxy
+ * defined in Section 14 of version 3 of the license.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef ABSTRACTUIMANAGER_H
+#define ABSTRACTUIMANAGER_H
+
+#include "abstractuijobs.h"
+
+/**
+ * Abstract factory that provides means to create a user interface for the various
+ * backend classes.
+ *
+ * This class can be reimplemented to provide a standard UI, a stripped-down UI
+ * (probably for mobile devices) or even a bogus UI which can be used inside
+ * unit-tests.
+ */
+class AbstractUiManager : public JobQueue
+{
+public:
+ virtual ~AbstractUiManager()
+ {
+ // TODO: clean-up
+ };
+
+ /**
+ * Create a job for asking for user's password for unlocking a collection.
+ *
+ * @param collection label of the collection to be unlocked
+ * @param secondTry set to true if the user already entered a wrong password before
+ * @return a job which can be enqueued to ask the user for the unlock password
+ */
+ virtual AbstractAskPasswordJob *createAskPasswordJob(const QString &collection,
+ bool secondTry) = 0;
+};
+
+#endif
diff --git a/backend/dialoguifactory.cpp b/backend/dialoguifactory.cpp
new file mode 100644
index 0000000..ae12429
--- /dev/null
+++ b/backend/dialoguifactory.cpp
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2010, Michael Leupold <lemma@confuego.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) version 3 or any later version
+ * accepted by the membership of KDE e.V. (or its successor approved
+ * by the membership of KDE e.V.), which shall act as a proxy
+ * defined in Section 14 of version 3 of the license.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "dialoguifactory.h"
+
+#include <QtCore/QTimer>
+#include <kpassworddialog.h>
+
+AbstractAskPasswordJob* DialogUiFactory::createAskPasswordJob(const QString& collection,
+ bool secondTry)
+{
+ return new DialogAskPasswordJob(this, collection, secondTry);
+}
+
+DialogAskPasswordJob::DialogAskPasswordJob(AbstractUiManager *manager,
+ const QString& collection,
+ bool secondTry)
+ : AbstractAskPasswordJob(manager, collection, secondTry), m_dialog(0)
+{
+}
+
+DialogAskPasswordJob::~DialogAskPasswordJob()
+{
+}
+
+void DialogAskPasswordJob::start()
+{
+ Q_ASSERT(!m_dialog);
+ // TODO: provide parent widget!
+ m_dialog = new KPasswordDialog;
+ m_dialog->setAttribute(Qt::WA_DeleteOnClose, true);
+ // TODO: needs proper string
+ m_dialog->setPrompt("Collection " + collection() + " wants password.");
+ connect(m_dialog, SIGNAL(finished(int)), this, SLOT(dialogFinished(int)));
+ m_dialog->show();
+}
+
+void DialogAskPasswordJob::dialogFinished(int result)
+{
+ Q_ASSERT(m_dialog);
+ if (result == QDialog::Accepted) {
+ setPassword(QCA::SecureArray(m_dialog->password().toUtf8()));
+ setResult(true);
+ } else {
+ setResult(false);
+ }
+ emitResult();
+}
+
+#include "dialoguifactory.moc"
diff --git a/backend/dialoguifactory.h b/backend/dialoguifactory.h
new file mode 100644
index 0000000..f3c4dc0
--- /dev/null
+++ b/backend/dialoguifactory.h
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2010, Michael Leupold <lemma@confuego.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) version 3 or any later version
+ * accepted by the membership of KDE e.V. (or its successor approved
+ * by the membership of KDE e.V.), which shall act as a proxy
+ * defined in Section 14 of version 3 of the license.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef DIALOGUIFACTORY_H
+#define DIALOGUIFACTORY_H
+
+#include "abstractuijobs.h"
+#include "abstractuimanager.h"
+
+class KPasswordDialog;
+
+class DialogAskPasswordJob : public AbstractAskPasswordJob
+{
+ Q_OBJECT
+
+public:
+ /**
+ * Constructor.
+ */
+ DialogAskPasswordJob(AbstractUiManager *manager, const QString& collection, bool secondTry);
+
+ /**
+ * Destructor.
+ */
+ ~DialogAskPasswordJob();
+
+ /**
+ * Contains the actual workload of showing the dialog.
+ */
+ virtual void start();
+
+private Q_SLOTS:
+ /**
+ * Called when the dialog shown is either accepted or rejected.
+ */
+ void dialogFinished(int result);
+
+private:
+ KPasswordDialog *m_dialog;
+};
+
+/**
+ * Implement AbstractUiFactory to provide a user interface using dialogs.
+ */
+class DialogUiFactory : public AbstractUiManager
+{
+public:
+ /**
+ * Destructor.
+ */
+ virtual ~DialogUiFactory() {};
+
+ /**
+ * Create a job to ask for a user's password to unlock a collection.
+ */
+ virtual AbstractAskPasswordJob* createAskPasswordJob(const QString& collection,
+ bool secondTry);
+};
+
+#endif
diff --git a/backend/tests/CMakeLists.txt b/backend/tests/CMakeLists.txt
index 3951b1d..c9106a5 100644
--- a/backend/tests/CMakeLists.txt
+++ b/backend/tests/CMakeLists.txt
@@ -12,4 +12,13 @@ ADD_TEST (BackendItemCollectionHandlingTest ksecretservice_backend_test)
KDE4_ADD_EXECUTABLE (securebuffer_test securebuffertest.cpp)
TARGET_LINK_LIBRARIES (securebuffer_test ksecretservicebackend ${QT_QTTEST_LIBRARIES})
+# involves dialogs, thus not an automatic test
+KDE4_ADD_EXECUTABLE (dialoguifactory_test dialoguifactorytest.cpp)
+TARGET_LINK_LIBRARIES (dialoguifactory_test
+ ksecretservicebackend
+ ksecretservicelib
+ ${QT_QTTEST_LIBRARIES}
+ ${KDE4_KDEUI_LIBS}
+)
+
ADD_TEST (SecureBufferTest securebuffer_test)
diff --git a/backend/tests/dialoguifactorytest.cpp b/backend/tests/dialoguifactorytest.cpp
new file mode 100644
index 0000000..73f1a02
--- /dev/null
+++ b/backend/tests/dialoguifactorytest.cpp
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2010, Michael Leupold <lemma@confuego.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) version 3 or any later version
+ * accepted by the membership of KDE e.V. (or its successor approved
+ * by the membership of KDE e.V.), which shall act as a proxy
+ * defined in Section 14 of version 3 of the license.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "dialoguifactorytest.h"
+
+#include <QtCrypto/QtCrypto>
+#include <qtest_kde.h>
+
+#include "../dialoguifactory.h"
+
+void DialogUiFactoryTest::initTestCase()
+{
+ QCA::init();
+}
+
+void DialogUiFactoryTest::testAskPassword()
+{
+ DialogUiFactory fact;
+
+ // ask for a password (asynchronously)
+ AbstractAskPasswordJob *asyncJob1 = fact.createAskPasswordJob("TESTCOLLECTION-ASYNC1", false);
+ AbstractAskPasswordJob *asyncJob2 = fact.createAskPasswordJob("TESTCOLLECTION-ASYNC2", false);
+ QEventLoop loop;
+ connect(asyncJob2, SIGNAL(result(QueuedJob*)), &loop, SLOT(quit()));
+ asyncJob1->enqueue();
+ asyncJob2->enqueue();
+ loop.exec();
+}
+
+void DialogUiFactoryTest::cleanupTestCase()
+{
+}
+
+QTEST_KDEMAIN(DialogUiFactoryTest, GUI)
+#include "dialoguifactorytest.moc"
diff --git a/backend/tests/dialoguifactorytest.h b/backend/tests/dialoguifactorytest.h
new file mode 100644
index 0000000..d862ea7
--- /dev/null
+++ b/backend/tests/dialoguifactorytest.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2010, Michael Leupold <lemma@confuego.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) version 3 or any later version
+ * accepted by the membership of KDE e.V. (or its successor approved
+ * by the membership of KDE e.V.), which shall act as a proxy
+ * defined in Section 14 of version 3 of the license.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef DIALOGUIFACTORYTEST_H
+#define DIALOGUIFACTORYTEST_H
+
+#include <QtTest>
+
+class DialogUiFactoryTest : public QObject
+{
+ Q_OBJECT
+
+private Q_SLOTS:
+ void initTestCase();
+
+ void testAskPassword();
+
+ void cleanupTestCase();
+};
+
+#endif // BACKENDTEST_H
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic