aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/async
diff options
context:
space:
mode:
Diffstat (limited to 'src/async')
-rw-r--r--src/async/imap/MCIMAPAsyncConnection.cc75
-rw-r--r--src/async/imap/MCIMAPAsyncConnection.h15
-rw-r--r--src/async/imap/MCIMAPAsyncSession.cc2
-rw-r--r--src/async/imap/MCIMAPAsyncSession.h8
-rw-r--r--src/async/imap/MCIMAPDisconnectOperation.cc28
-rw-r--r--src/async/imap/MCIMAPDisconnectOperation.h32
-rw-r--r--src/async/pop/MCPOPAsyncSession.cc28
-rw-r--r--src/async/pop/MCPOPAsyncSession.h4
-rw-r--r--src/async/smtp/MCSMTPAsyncSession.cc67
-rw-r--r--src/async/smtp/MCSMTPAsyncSession.h12
-rw-r--r--src/async/smtp/MCSMTPDisconnectOperation.cc28
-rw-r--r--src/async/smtp/MCSMTPDisconnectOperation.h32
-rw-r--r--src/async/smtp/MCSMTPOperation.cc1
13 files changed, 295 insertions, 37 deletions
diff --git a/src/async/imap/MCIMAPAsyncConnection.cc b/src/async/imap/MCIMAPAsyncConnection.cc
index 5b1269da..ed40932c 100644
--- a/src/async/imap/MCIMAPAsyncConnection.cc
+++ b/src/async/imap/MCIMAPAsyncConnection.cc
@@ -30,9 +30,36 @@
#include "MCIMAPIdleOperation.h"
#include "MCIMAPIdentityOperation.h"
#include "MCIMAPCapabilityOperation.h"
+#include "MCOperationQueueCallback.h"
+#include "MCIMAPDisconnectOperation.h"
+#include "MCIMAPAsyncSession.h"
using namespace mailcore;
+namespace mailcore {
+ class IMAPOperationQueueCallback : public Object, public OperationQueueCallback {
+ public:
+ IMAPOperationQueueCallback(IMAPAsyncConnection * connection) {
+ mConnection = connection;
+ }
+
+ virtual ~IMAPOperationQueueCallback() {
+ }
+
+ virtual void queueStartRunning() {
+ mConnection->queueStartRunning();
+ }
+
+ virtual void queueStoppedRunning() {
+ mConnection->tryAutomaticDisconnect();
+ mConnection->queueStoppedRunning();
+ }
+
+ private:
+ IMAPAsyncConnection * mConnection;
+ };
+}
+
IMAPAsyncConnection::IMAPAsyncConnection()
{
mSession = new IMAPSession();
@@ -40,10 +67,15 @@ IMAPAsyncConnection::IMAPAsyncConnection()
mDefaultNamespace = NULL;
mDelimiter = 0;
mLastFolder = NULL;
+ mQueueCallback = new IMAPOperationQueueCallback(this);
+ mQueue->setCallback(mQueueCallback);
+ mOwner = NULL;
}
IMAPAsyncConnection::~IMAPAsyncConnection()
{
+ cancelDelayedPerformMethod((Object::Method) &IMAPAsyncConnection::tryAutomaticDisconnectAfterDelay, NULL);
+ MC_SAFE_RELEASE(mQueueCallback);
MC_SAFE_RELEASE(mLastFolder);
MC_SAFE_RELEASE(mDefaultNamespace);
MC_SAFE_RELEASE(mQueue);
@@ -439,10 +471,40 @@ unsigned int IMAPAsyncConnection::operationsCount()
void IMAPAsyncConnection::runOperation(IMAPOperation * operation)
{
-#warning disconnect after delay
+ cancelDelayedPerformMethod((Object::Method) &IMAPAsyncConnection::tryAutomaticDisconnectAfterDelay, NULL);
mQueue->addOperation(operation);
}
+void IMAPAsyncConnection::tryAutomaticDisconnect()
+{
+ // It's safe since no thread is running when this function is called.
+ if (mSession->isDisconnected()) {
+ return;
+ }
+
+ performMethodAfterDelay((Object::Method) &IMAPAsyncConnection::tryAutomaticDisconnectAfterDelay, NULL, 30);
+}
+
+void IMAPAsyncConnection::tryAutomaticDisconnectAfterDelay(void * context)
+{
+ IMAPDisconnectOperation * op = new IMAPDisconnectOperation();
+ op->setSession(this);
+ op->autorelease();
+ op->start();
+}
+
+void IMAPAsyncConnection::queueStartRunning()
+{
+ this->retain();
+ mOwner->retain();
+}
+
+void IMAPAsyncConnection::queueStoppedRunning()
+{
+ mOwner->release();
+ this->release();
+}
+
void IMAPAsyncConnection::setLastFolder(String * folder)
{
MC_SAFE_REPLACE_COPY(String, mLastFolder, folder);
@@ -453,3 +515,14 @@ String * IMAPAsyncConnection::lastFolder()
return mLastFolder;
}
+void IMAPAsyncConnection::setOwner(IMAPAsyncSession * owner)
+{
+ mOwner = owner;
+}
+
+IMAPAsyncSession * IMAPAsyncConnection::owner()
+{
+ return mOwner;
+}
+
+
diff --git a/src/async/imap/MCIMAPAsyncConnection.h b/src/async/imap/MCIMAPAsyncConnection.h
index 4843702b..d6ef6a91 100644
--- a/src/async/imap/MCIMAPAsyncConnection.h
+++ b/src/async/imap/MCIMAPAsyncConnection.h
@@ -25,6 +25,8 @@ namespace mailcore {
class IMAPFetchNamespaceOperation;
class IMAPIdentityOperation;
class IMAPCapabilityOperation;
+ class IMAPOperationQueueCallback;
+ class IMAPAsyncSession;
class IMAPAsyncConnection : public Object {
public:
@@ -116,8 +118,10 @@ namespace mailcore {
char mDelimiter;
IMAPNamespace * mDefaultNamespace;
String * mLastFolder;
-
- void queue(IMAPOperation * op);
+ IMAPOperationQueueCallback * mQueueCallback;
+ IMAPAsyncSession * mOwner;
+
+ virtual void tryAutomaticDisconnectAfterDelay(void * context);
public: // private
virtual void runOperation(IMAPOperation * operation);
@@ -127,6 +131,13 @@ namespace mailcore {
virtual void setLastFolder(String * folder);
virtual String * lastFolder();
+
+ virtual void tryAutomaticDisconnect();
+ virtual void queueStartRunning();
+ virtual void queueStoppedRunning();
+
+ virtual void setOwner(IMAPAsyncSession * owner);
+ virtual IMAPAsyncSession * owner();
};
}
diff --git a/src/async/imap/MCIMAPAsyncSession.cc b/src/async/imap/MCIMAPAsyncSession.cc
index 766b6fe6..7c085e4a 100644
--- a/src/async/imap/MCIMAPAsyncSession.cc
+++ b/src/async/imap/MCIMAPAsyncSession.cc
@@ -10,6 +10,7 @@
#include "MCIMAPAsyncConnection.h"
#include "MCIMAPNamespace.h"
+#include "MCOperationQueueCallback.h"
#define DEFAULT_MAX_CONNECTIONS 3
@@ -173,6 +174,7 @@ unsigned int IMAPAsyncSession::maximumConnections()
IMAPAsyncConnection * IMAPAsyncSession::session()
{
IMAPAsyncConnection * session = new IMAPAsyncConnection();
+ session->setOwner(this);
session->autorelease();
session->setHostname(mHostname);
diff --git a/src/async/imap/MCIMAPAsyncSession.h b/src/async/imap/MCIMAPAsyncSession.h
index a2bc05f8..390fdab8 100644
--- a/src/async/imap/MCIMAPAsyncSession.h
+++ b/src/async/imap/MCIMAPAsyncSession.h
@@ -142,10 +142,10 @@ namespace mailcore {
bool mAllowsFolderConcurrentAccessEnabled;
unsigned int mMaximumConnections;
- IMAPAsyncConnection * sessionForFolder(String * folder, bool urgent = false);
- IMAPAsyncConnection * session();
- IMAPAsyncConnection * matchingSessionForFolder(String * folder);
- IMAPAsyncConnection * availableSession();
+ virtual IMAPAsyncConnection * sessionForFolder(String * folder, bool urgent = false);
+ virtual IMAPAsyncConnection * session();
+ virtual IMAPAsyncConnection * matchingSessionForFolder(String * folder);
+ virtual IMAPAsyncConnection * availableSession();
};
}
diff --git a/src/async/imap/MCIMAPDisconnectOperation.cc b/src/async/imap/MCIMAPDisconnectOperation.cc
new file mode 100644
index 00000000..070eeaae
--- /dev/null
+++ b/src/async/imap/MCIMAPDisconnectOperation.cc
@@ -0,0 +1,28 @@
+//
+// MCIMAPDisconnectOperation.cc
+// mailcore2
+//
+// Created by DINH Viêt Hoà on 6/22/13.
+// Copyright (c) 2013 MailCore. All rights reserved.
+//
+
+#include "MCIMAPDisconnectOperation.h"
+
+#include "MCIMAPAsyncConnection.h"
+#include "MCIMAPSession.h"
+
+using namespace mailcore;
+
+IMAPDisconnectOperation::IMAPDisconnectOperation()
+{
+}
+
+IMAPDisconnectOperation::~IMAPDisconnectOperation()
+{
+}
+
+void IMAPDisconnectOperation::main()
+{
+ session()->session()->disconnect();
+ setError(ErrorCode::ErrorNone);
+}
diff --git a/src/async/imap/MCIMAPDisconnectOperation.h b/src/async/imap/MCIMAPDisconnectOperation.h
new file mode 100644
index 00000000..5d572def
--- /dev/null
+++ b/src/async/imap/MCIMAPDisconnectOperation.h
@@ -0,0 +1,32 @@
+//
+// MCIMAPDisconnectOperation.h
+// mailcore2
+//
+// Created by DINH Viêt Hoà on 6/22/13.
+// Copyright (c) 2013 MailCore. All rights reserved.
+//
+
+#ifndef __MAILCORE_MCIMAPDISCONNECTOPERATION_H_
+#define __MAILCORE_MCIMAPDISCONNECTOPERATION_H_
+
+#include <MailCore/MCBaseTypes.h>
+#include <MailCore/MCAbstract.h>
+#include <MailCore/MCIMAPOperation.h>
+
+#ifdef __cplusplus
+
+namespace mailcore {
+
+ class IMAPDisconnectOperation : public IMAPOperation {
+ public:
+ IMAPDisconnectOperation();
+ virtual ~IMAPDisconnectOperation();
+
+ public: // subclass behavior
+ virtual void main();
+ };
+}
+
+#endif
+
+#endif \ No newline at end of file
diff --git a/src/async/pop/MCPOPAsyncSession.cc b/src/async/pop/MCPOPAsyncSession.cc
index 39f356db..1269a34b 100644
--- a/src/async/pop/MCPOPAsyncSession.cc
+++ b/src/async/pop/MCPOPAsyncSession.cc
@@ -14,17 +14,44 @@
#include "MCPOPDeleteMessagesOperation.h"
#include "MCPOPFetchMessagesOperation.h"
#include "MCPOPCheckAccountOperation.h"
+#include "MCOperationQueueCallback.h"
using namespace mailcore;
+namespace mailcore {
+ class POPOperationQueueCallback : public Object, public OperationQueueCallback {
+ public:
+ POPOperationQueueCallback(POPAsyncSession * session) {
+ mSession = session;
+ }
+
+ virtual ~POPOperationQueueCallback() {
+ }
+
+ virtual void queueStartRunning() {
+ mSession->retain();
+ }
+
+ virtual void queueStoppedRunning() {
+ mSession->release();
+ }
+
+ private:
+ POPAsyncSession * mSession;
+ };
+}
+
POPAsyncSession::POPAsyncSession()
{
mSession = new POPSession();
mQueue = new OperationQueue();
+ mQueueCallback = new POPOperationQueueCallback(this);
+ mQueue->setCallback(mQueueCallback);
}
POPAsyncSession::~POPAsyncSession()
{
+ MC_SAFE_RELEASE(mQueueCallback);
MC_SAFE_RELEASE(mSession);
MC_SAFE_RELEASE(mQueue);
}
@@ -164,6 +191,5 @@ POPSession * POPAsyncSession::session()
void POPAsyncSession::runOperation(POPOperation * operation)
{
-#warning disconnect after delay
mQueue->addOperation(operation);
}
diff --git a/src/async/pop/MCPOPAsyncSession.h b/src/async/pop/MCPOPAsyncSession.h
index 166675e0..452d4d75 100644
--- a/src/async/pop/MCPOPAsyncSession.h
+++ b/src/async/pop/MCPOPAsyncSession.h
@@ -22,7 +22,8 @@ namespace mailcore {
class POPFetchMessageOperation;
class POPDeleteMessagesOperation;
class POPFetchMessagesOperation;
-
+ class POPOperationQueueCallback;
+
class POPAsyncSession : public Object {
public:
POPAsyncSession();
@@ -68,6 +69,7 @@ namespace mailcore {
private:
POPSession * mSession;
OperationQueue * mQueue;
+ POPOperationQueueCallback * mQueueCallback;
public: // private
virtual void runOperation(POPOperation * operation);
diff --git a/src/async/smtp/MCSMTPAsyncSession.cc b/src/async/smtp/MCSMTPAsyncSession.cc
index 95101a50..73bc2a54 100644
--- a/src/async/smtp/MCSMTPAsyncSession.cc
+++ b/src/async/smtp/MCSMTPAsyncSession.cc
@@ -1,22 +1,50 @@
#include "MCSMTPAsyncSession.h"
#include "MCSMTPSession.h"
-//#include "MCSMTPSendWithRecipientOperation.h"
#include "MCSMTPSendWithDataOperation.h"
-//#include "MCSMTPSendWithBuilderOperation.h"
#include "MCSMTPCheckAccountOperation.h"
+#include "MCSMTPDisconnectOperation.h"
#include "MCSMTPOperation.h"
+#include "MCOperationQueueCallback.h"
using namespace mailcore;
+namespace mailcore {
+ class SMTPOperationQueueCallback : public Object, public OperationQueueCallback {
+ public:
+ SMTPOperationQueueCallback(SMTPAsyncSession * session) {
+ mSession = session;
+ }
+
+ virtual ~SMTPOperationQueueCallback() {
+ }
+
+ virtual void queueStartRunning() {
+ mSession->retain();
+ }
+
+ virtual void queueStoppedRunning() {
+ mSession->tryAutomaticDisconnect();
+ mSession->release();
+ }
+
+ private:
+ SMTPAsyncSession * mSession;
+ };
+}
+
SMTPAsyncSession::SMTPAsyncSession()
{
mSession = new SMTPSession();
mQueue = new OperationQueue();
+ mQueueCallback = new SMTPOperationQueueCallback(this);
+ mQueue->setCallback(mQueueCallback);
}
SMTPAsyncSession::~SMTPAsyncSession()
{
+ cancelDelayedPerformMethod((Object::Method) &SMTPAsyncSession::tryAutomaticDisconnectAfterDelay, NULL);
+ MC_SAFE_RELEASE(mQueueCallback);
MC_SAFE_RELEASE(mQueue);
MC_SAFE_RELEASE(mSession);
}
@@ -113,7 +141,7 @@ bool SMTPAsyncSession::useHeloIPEnabled()
void SMTPAsyncSession::runOperation(SMTPOperation * operation)
{
-#warning disconnect after delay
+ cancelDelayedPerformMethod((Object::Method) &SMTPAsyncSession::tryAutomaticDisconnectAfterDelay, NULL);
mQueue->addOperation(operation);
}
@@ -122,35 +150,31 @@ SMTPSession * SMTPAsyncSession::session()
return mSession;
}
-#if 0
-SMTPOperation * SMTPAsyncSession::sendMessageOperationWithFromAndRecipient(Address * from, Array * recipients, Data * messageData)
+void SMTPAsyncSession::tryAutomaticDisconnect()
{
- SMTPSendWithRecipientOperation * op = new SMTPSendWithRecipientOperation();
- op->setSession(this);
- op->setFrom(from);
- op->setRecipients(recipients);
- op->setMessageData(messageData);
- return (SMTPOperation *) op->autorelease();
+ // It's safe since no thread is running when this function is called.
+ if (mSession->isDisconnected()) {
+ return;
+ }
+
+ performMethodAfterDelay((Object::Method) &SMTPAsyncSession::tryAutomaticDisconnectAfterDelay, NULL, 30);
}
-#endif
-SMTPOperation * SMTPAsyncSession::sendMessageOperation(Data * messageData)
+void SMTPAsyncSession::tryAutomaticDisconnectAfterDelay(void * context)
{
- SMTPSendWithDataOperation * op = new SMTPSendWithDataOperation();
+ SMTPDisconnectOperation * op = new SMTPDisconnectOperation();
op->setSession(this);
- op->setMessageData(messageData);
- return (SMTPOperation *) op->autorelease();
+ op->autorelease();
+ op->start();
}
-#if 0
-SMTPOperation * SMTPAsyncSession::sendMessageOperation(MessageBuilder * msg)
+SMTPOperation * SMTPAsyncSession::sendMessageOperation(Data * messageData)
{
- SMTPSendWithBuilderOperation * op = new SMTPSendWithBuilderOperation();
+ SMTPSendWithDataOperation * op = new SMTPSendWithDataOperation();
op->setSession(this);
- op->setBuilder(msg);
+ op->setMessageData(messageData);
return (SMTPOperation *) op->autorelease();
}
-#endif
SMTPOperation * SMTPAsyncSession::checkAccountOperation(Address * from)
{
@@ -159,4 +183,3 @@ SMTPOperation * SMTPAsyncSession::checkAccountOperation(Address * from)
op->setSession(this);
return (SMTPOperation *) op->autorelease();
}
-
diff --git a/src/async/smtp/MCSMTPAsyncSession.h b/src/async/smtp/MCSMTPAsyncSession.h
index 0be287af..9cd7a8f0 100644
--- a/src/async/smtp/MCSMTPAsyncSession.h
+++ b/src/async/smtp/MCSMTPAsyncSession.h
@@ -13,6 +13,7 @@ namespace mailcore {
class SMTPOperation;
class SMTPSession;
class Address;
+ class SMTPOperationQueueCallback;
class SMTPAsyncSession : public Object {
public:
@@ -52,13 +53,14 @@ namespace mailcore {
public: // private
virtual void runOperation(SMTPOperation * operation);
virtual SMTPSession * session();
+ virtual void tryAutomaticDisconnect();
private:
- SMTPSession * mSession;
- OperationQueue * mQueue;
-
- void queue(SMTPOperation * op);
-
+ SMTPSession * mSession;
+ OperationQueue * mQueue;
+ SMTPOperationQueueCallback * mQueueCallback;
+
+ virtual void tryAutomaticDisconnectAfterDelay(void * context);
};
}
diff --git a/src/async/smtp/MCSMTPDisconnectOperation.cc b/src/async/smtp/MCSMTPDisconnectOperation.cc
new file mode 100644
index 00000000..592ceb48
--- /dev/null
+++ b/src/async/smtp/MCSMTPDisconnectOperation.cc
@@ -0,0 +1,28 @@
+//
+// SMTPDisconnectOperation.cpp
+// mailcore2
+//
+// Created by DINH Viêt Hoà on 6/22/13.
+// Copyright (c) 2013 MailCore. All rights reserved.
+//
+
+#include "MCSMTPDisconnectOperation.h"
+
+#include "MCSMTPAsyncSession.h"
+#include "MCSMTPSession.h"
+
+using namespace mailcore;
+
+SMTPDisconnectOperation::SMTPDisconnectOperation()
+{
+}
+
+SMTPDisconnectOperation::~SMTPDisconnectOperation()
+{
+}
+
+void SMTPDisconnectOperation::main()
+{
+ session()->session()->disconnect();
+ setError(ErrorCode::ErrorNone);
+}
diff --git a/src/async/smtp/MCSMTPDisconnectOperation.h b/src/async/smtp/MCSMTPDisconnectOperation.h
new file mode 100644
index 00000000..dacf72a6
--- /dev/null
+++ b/src/async/smtp/MCSMTPDisconnectOperation.h
@@ -0,0 +1,32 @@
+//
+// SMTPDisconnectOperation.h
+// mailcore2
+//
+// Created by DINH Viêt Hoà on 6/22/13.
+// Copyright (c) 2013 MailCore. All rights reserved.
+//
+
+#ifndef __MAILCORE_MCSMTPDISCONNECTOPERATION_H_
+#define __MAILCORE_MCSMTPDISCONNECTOPERATION_H_
+
+#include <MailCore/MCBaseTypes.h>
+#include <MailCore/MCAbstract.h>
+#include <MailCore/MCSMTPOperation.h>
+
+#ifdef __cplusplus
+
+namespace mailcore {
+
+ class SMTPDisconnectOperation : public SMTPOperation {
+ public:
+ SMTPDisconnectOperation();
+ virtual ~SMTPDisconnectOperation();
+
+ public: // subclass behavior
+ virtual void main();
+ };
+}
+
+#endif
+
+#endif \ No newline at end of file
diff --git a/src/async/smtp/MCSMTPOperation.cc b/src/async/smtp/MCSMTPOperation.cc
index 532c6ab4..e8211a24 100644
--- a/src/async/smtp/MCSMTPOperation.cc
+++ b/src/async/smtp/MCSMTPOperation.cc
@@ -82,4 +82,3 @@ void SMTPOperation::bodyProgressOnMainThread(void * ctx)
}
free(context);
}
-