summaryrefslogtreecommitdiff
path: root/lib/kolabaccount.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <mollekopf@kolabsys.com>2012-10-17 14:19:27 (GMT)
committerChristian Mollekopf <mollekopf@kolabsys.com>2012-10-17 14:19:27 (GMT)
commit5751be941b444a719eec44fe501ab5db0526a009 (patch)
treee98509d0b11c5b8e9a4753aa87d067b0c5270f3c /lib/kolabaccount.cpp
parent5aaed0a7f3b8cd8791438572aa4ad167135fa7bd (diff)
downloadkolab-utils-5751be941b444a719eec44fe501ab5db0526a009.tar.gz
Moved some shared code to new libkolabutils.
Diffstat (limited to 'lib/kolabaccount.cpp')
-rw-r--r--lib/kolabaccount.cpp314
1 files changed, 314 insertions, 0 deletions
diff --git a/lib/kolabaccount.cpp b/lib/kolabaccount.cpp
new file mode 100644
index 0000000..c55511f
--- /dev/null
+++ b/lib/kolabaccount.cpp
@@ -0,0 +1,314 @@
+/*
+ * Copyright (C) 2012 Christian Mollekopf <mollekopf@kolabsys.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "kolabaccount.h"
+#include "uiproxy.h"
+#include <jobs/setupkolabfoldersjob.h>
+#include <kimap/session.h>
+#include <kimap/logoutjob.h>
+#include <kimap/appendjob.h>
+#include <kimap/createjob.h>
+#include <kimap/deletejob.h>
+#include <kimap/listjob.h>
+#include <kimap/capabilitiesjob.h>
+#include <kimap/namespacejob.h>
+#include <kimap/storejob.h>
+#include <kimap/selectjob.h>
+#include <kimap/expungejob.h>
+#include <kimap/setmetadatajob.h>
+#include <kolab/errorhandler.h>
+#include <kolab/kolabdefinitions.h>
+#include <formathelpers.h>
+
+
+KolabAccount::KolabAccount(QObject* parent)
+: QObject(parent),
+ mSession(0),
+ mEncryptionMode(KIMAP::LoginJob::TlsV1),
+ mAuthenticationMode(KIMAP::LoginJob::Plain),
+ mDryRun(false)
+{
+
+}
+
+void KolabAccount::setHost(const QString& host, qint16 port)
+{
+ mHost = host;
+ mPort = port;
+}
+
+void KolabAccount::setCredentials(const QString& username, const QString& pw, const QString &authorizationName)
+{
+ mUsername = username;
+ mPw = pw;
+ mAuthorizationName = authorizationName;
+}
+
+void KolabAccount::setDryRun(bool enable)
+{
+ mDryRun = enable;
+}
+
+bool KolabAccount::init()
+{
+ if (mSession) {
+ return false;
+ }
+ mSession = new KIMAP::Session( mHost, mPort, this );
+ mSession->setUiProxy( KIMAP::SessionUiProxy::Ptr(new UiProxy()) );
+ KIMAP::LoginJob *loginJob = new KIMAP::LoginJob( mSession );
+ if (mAuthorizationName != mUsername) {
+ loginJob->setAuthorizationName(mAuthorizationName);
+ }
+ loginJob->setUserName( mUsername );
+ loginJob->setPassword( mPw );
+ loginJob->setEncryptionMode( mEncryptionMode );
+ loginJob->setAuthenticationMode( mAuthenticationMode );
+ loginJob->exec();
+
+ if ( loginJob->error() ) {
+ Error() << "Failed to login: " << loginJob->errorString();
+ mSession->close();
+ return false;
+ }
+ Debug() << "authentication successful";
+
+ KIMAP::CapabilitiesJob *capabilities = new KIMAP::CapabilitiesJob(mSession);
+ capabilities->exec();
+ mCapabilities = capabilities->capabilities();
+
+ if ( mCapabilities.contains( "NAMESPACE" ) ) {
+ KIMAP::NamespaceJob *nsJob = new KIMAP::NamespaceJob( mSession );
+ nsJob->exec();
+ mPersonalNamespaces = nsJob->personalNamespaces();
+ mExcludedNamespaces = nsJob->userNamespaces();
+ mExcludedNamespaces.append(nsJob->sharedNamespaces());
+// Debug() << "personal namespaces";
+// foreach (const KIMAP::MailBoxDescriptor &desc, nsJob->personalNamespaces()) {
+// Debug() << desc.name;
+// }
+// foreach (const KIMAP::MailBoxDescriptor &desc, nsJob->userNamespaces()) {
+// Debug() << desc.name;
+// }
+// foreach (const KIMAP::MailBoxDescriptor &desc, nsJob->sharedNamespaces()) {
+// Debug() << desc.name;
+// }
+ }
+ return true;
+}
+
+const char* FlagRecent2 = "\\Recent";
+
+void KolabAccount::appendObject(Object obj, const QString& folder)
+{
+ if (mDryRun) {
+ Debug() << "append object in folder: " << folder;
+ return;
+ }
+ Q_ASSERT(mFolders.contains(folder));
+ if (!obj.message) {
+ Error() << "got empty message";
+ return;
+ }
+ Q_ASSERT(obj.message.get());
+ KIMAP::AppendJob *job = new KIMAP::AppendJob( mSession );
+ job->setMailBox( folder );
+ job->setContent( obj.message->encodedContent( true ) );
+ //The Recent flag is a special case which is not allowed in the append command
+ obj.flags.removeAll(QByteArray(FlagRecent2));
+ job->setFlags( obj.flags );
+ job->exec();
+ Debug() << "appended object in folder: " << folder;
+ if (job->error()) {
+ Error() << job->errorString();
+ }
+}
+
+void KolabAccount::logout()
+{
+ Q_ASSERT(mSession);
+ KIMAP::LogoutJob *logoutJob = new KIMAP::LogoutJob(mSession);
+ logoutJob->exec();
+ mSession->close();
+// mSession->deleteLater();
+ mSession = 0;
+ Debug() << "logout done";
+}
+
+const char* FlagDeleted2 = "\\Deleted";
+
+void KolabAccount::cleanAccount()
+{
+ Q_ASSERT(mSession);
+ KIMAP::ListJob *listJob = new KIMAP::ListJob(mSession);
+ listJob->setOption(KIMAP::ListJob::IncludeUnsubscribed);
+ listJob->setQueriedNamespaces(mPersonalNamespaces);
+ QObject::connect( listJob, SIGNAL(mailBoxesReceived(QList<KIMAP::MailBoxDescriptor>,QList<QList<QByteArray> >)),
+ this, SLOT(mailBoxesReceived(QList<KIMAP::MailBoxDescriptor>,QList<QList<QByteArray> >)));
+ listJob->exec();
+ if (listJob->error()) {
+ Error() << listJob->errorString();
+ }
+ foreach (const KIMAP::MailBoxDescriptor &desc, mMailboxes) {
+ if (desc.name.contains(QLatin1String("inbox"), Qt::CaseInsensitive)) {
+ continue;
+ }
+ bool skip = false;
+ foreach(const KIMAP::MailBoxDescriptor &excluded, mExcludedNamespaces) {
+ if (desc.name.contains(excluded.name)) {
+ skip = true;
+ break;
+ }
+ }
+ if (skip) {
+ continue;
+ }
+
+ KIMAP::SelectJob *selectJob = new KIMAP::SelectJob(mSession);
+ selectJob->setMailBox(desc.name);
+ selectJob->exec();
+ if (selectJob->error()) {
+ Error() << selectJob->errorString();
+ }
+ const int messageCount = selectJob->messageCount();
+ if (mDryRun) {
+ Debug() << "removing folder: " << desc.name << messageCount;
+ continue;
+ }
+
+ if (messageCount > 0) {
+ KIMAP::StoreJob *storeJob = new KIMAP::StoreJob(mSession);
+ storeJob->setMode(KIMAP::StoreJob::AppendFlags);
+ storeJob->setFlags(KIMAP::MessageFlags() << FlagDeleted2);
+ storeJob->setSequenceSet( KIMAP::ImapSet( 1, messageCount ) );
+ storeJob->exec();
+ if (storeJob->error()) {
+ Error() << storeJob->errorString();
+ }
+
+ KIMAP::ExpungeJob *expungeJob = new KIMAP::ExpungeJob(mSession);
+ expungeJob->exec();
+ if (expungeJob->error()) {
+ Error() << expungeJob->errorString();
+ }
+ }
+
+ KIMAP::DeleteJob *deleteJob = new KIMAP::DeleteJob(mSession);
+ Debug() << "deleting: " << desc.name;
+ deleteJob->setMailBox(desc.name);
+ deleteJob->exec();
+ if (deleteJob->error()) {
+ Error() << deleteJob->errorString();
+ }
+ }
+ mFolders.clear();
+ mFolders.append(QLatin1String("inbox"));
+
+ //logging out and logging in again seems to help with the "NO Mailbox is locked" problem when creating folders afterwards.
+ logout();
+ init();
+}
+
+void KolabAccount::mailBoxesReceived(const QList<KIMAP::MailBoxDescriptor> &descriptors, const QList< QList< QByteArray > > &flags)
+{
+ mMailboxes.append(descriptors);
+}
+
+//TODO move to formathelpers.h or kolabdefinitions.h
+#define KOLAB_FOLDER_TYPE_DRAFTS_SUFFIX ".drafts"
+#define KOLAB_FOLDER_TYPE_SENT_SUFFIX ".sentitems"
+#define KOLAB_FOLDER_TYPE_OUTBOX_SUFFIX ".outbox"
+#define KOLAB_FOLDER_TYPE_TRASH_SUFFIX ".wastebasket"
+#define KOLAB_FOLDER_TYPE_JUNK_SUFFIX ".junkemail"
+
+
+/*
+ * This can fail with "NO Mailbox is locked" if the we deleted a selected folder before:
+ * http://lists.andrew.cmu.edu/pipermail/info-cyrus/2011-June/035049.html
+ *
+ */
+void KolabAccount::createFolder(const QString &name, const QByteArray &annotation)
+{
+ if (mFolders.contains(name, Qt::CaseInsensitive)) {
+ Warning() << "folder is already existing: " << name;
+ return;
+ }
+ if (mDryRun) {
+ Debug() << "creating folder: " << name << annotation;
+ return;
+ }
+
+ KIMAP::CreateJob *createJob = new KIMAP::CreateJob(mSession);
+ createJob->setMailBox(name);
+ createJob->exec();
+ Debug() << "created folder " << name;
+ if (createJob->error()) {
+ Error() << createJob->errorString();
+ return;
+ }
+
+ KIMAP::SetMetaDataJob *setMetadataJob = new KIMAP::SetMetaDataJob(mSession);
+ setMetadataJob->setMailBox(createJob->mailBox());
+ if ( mCapabilities.contains( "METADATA" ) ) {
+ setMetadataJob->setServerCapability( KIMAP::MetaDataJobBase::Metadata );
+ setMetadataJob->addMetaData( "/shared" KOLAB_FOLDER_TYPE_ANNOTATION, annotation );
+ } else {
+ setMetadataJob->setServerCapability( KIMAP::MetaDataJobBase::Annotatemore );
+ setMetadataJob->setEntry( KOLAB_FOLDER_TYPE_ANNOTATION );
+ setMetadataJob->addMetaData( "value.shared", annotation );
+ }
+ setMetadataJob->exec();
+ if (setMetadataJob->error()) {
+ Error() << setMetadataJob->errorString();
+ }
+
+ mFolders.append(name);
+}
+
+void KolabAccount::createFolder(const QString& name, Kolab::FolderType folderType)
+{
+ createFolder(name, QString::fromStdString(Kolab::folderAnnotation(folderType)).toLatin1());
+}
+
+
+void KolabAccount::setupFolders()
+{
+ if (mDryRun) {
+ Debug() << "setup default folders";
+ return;
+ }
+ SetupKolabFoldersJob *setupJob = new SetupKolabFoldersJob(mCapabilities, QString(), mSession, this);
+ setupJob->setKolabFolders(QStringList() << KOLAB_FOLDER_TYPE_CONTACT << KOLAB_FOLDER_TYPE_EVENT << KOLAB_FOLDER_TYPE_TASK << KOLAB_FOLDER_TYPE_JOURNAL << KOLAB_FOLDER_TYPE_NOTE);
+ setupJob->exec();
+ if (setupJob->error()) {
+ Error() << setupJob->errorString();
+ }
+ foreach (const QString &folder, setupJob->createdFolders().values()) {
+ mFolders.append(folder);
+ }
+ KIMAP::SelectJob *selectJob = new KIMAP::SelectJob(mSession);
+ selectJob->setMailBox(QString());
+ //TODO internationalize folder names
+ //TODO make part of setupkolabfoldersjob after adding types to folderTypeFromString in formathelpers.cpp
+ //TODO the suffixes should go in the private namespace
+ createFolder("Configuration", KOLAB_FOLDER_TYPE_CONFIGURATION KOLAB_FOLDER_TYPE_DEFAULT_SUFFIX);
+ createFolder("Drafts", KOLAB_FOLDER_TYPE_MAIL KOLAB_FOLDER_TYPE_DRAFTS_SUFFIX);
+ createFolder("Sent", KOLAB_FOLDER_TYPE_MAIL KOLAB_FOLDER_TYPE_SENT_SUFFIX);
+ createFolder("Trash", KOLAB_FOLDER_TYPE_MAIL KOLAB_FOLDER_TYPE_TRASH_SUFFIX);
+}
+
+