Skip to content

Commit

Permalink
Merge pull request #52 from nemomobile-packages/idleKeepAlive
Browse files Browse the repository at this point in the history
[qmf] Add keepalive timer to IMAP IDLE service
  • Loading branch information
VDVsx committed Sep 11, 2014
2 parents 8601300 + 340e814 commit abda1a9
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 3 deletions.
4 changes: 4 additions & 0 deletions qmf/src/plugins/messageservices/imap/imap.pro
Expand Up @@ -21,6 +21,10 @@ equals(QT_MAJOR_VERSION, 5) {

QT = core network

contains(DEFINES, USE_KEEPALIVE) {
PKGCONFIG += keepalive
}

contains(DEFINES,QT_QMF_USE_ALIGNEDTIMER) {
QT += alignedtimer
}
Expand Down
5 changes: 4 additions & 1 deletion qmf/src/plugins/messageservices/imap/imapclient.cpp
Expand Up @@ -347,7 +347,7 @@ protected slots:

#ifdef USE_ACCOUNTS_QT
IdleProtocol::IdleProtocol(ImapClient *client, const QMailFolder &folder, const bool ssoAccount, QByteArray &ssoLogin)
:_idleRetryDelay(InitialIdleRetryDelay),
: _idleRetryDelay(InitialIdleRetryDelay),
_ssoAccount(ssoAccount),
_ssoLogin(ssoLogin)
{
Expand Down Expand Up @@ -2099,6 +2099,9 @@ void ImapClient::closeIdleConnections()
qMailLog(IMAP) << Q_FUNC_INFO << "Account was modified. Closing connections";

closeConnection();
#ifdef USE_KEEPALIVE
emit stopPushEmail();
#endif
// closing idle connections
foreach(const QMailFolderId &id, _monitored.keys()) {
IdleProtocol *protocol = _monitored.take(id);
Expand Down
6 changes: 6 additions & 0 deletions qmf/src/plugins/messageservices/imap/imapclient.h
Expand Up @@ -59,6 +59,9 @@
#include <ssosessionmanager.h>
#endif

#ifdef USE_KEEPALIVE
#include <keepalive/backgroundactivity.h>
#endif

class ImapStrategy;
class ImapStrategyContext;
Expand Down Expand Up @@ -115,6 +118,9 @@ class ImapClient : public QObject
void errorOccurred(QMailServiceAction::Status::ErrorCode, const QString &);
void updateStatus(const QString &);
void restartPushEmail();
#ifdef USE_KEEPALIVE
void stopPushEmail();
#endif

void progressChanged(uint, uint);
void retrievalCompleted();
Expand Down
64 changes: 64 additions & 0 deletions qmf/src/plugins/messageservices/imap/imapservice.cpp
Expand Up @@ -1507,6 +1507,14 @@ ImapService::ImapService(const QMailAccountId &accountId)
_accountWasPushEnabled(false),
_initiatePushEmailTimer(new QTimer(this))
{
#ifdef USE_KEEPALIVE
_lastSyncCounter = 0;
_idling = false;
_backgroundActivity = new BackgroundActivity(this);
_backgroundActivity->setWakeupFrequency(BackgroundActivity::ThirtySeconds);
connect(_backgroundActivity, SIGNAL(running()), this, SLOT(onUpdateLastSyncTime()));
#endif

QMailAccount account(accountId);
if (!(account.status() & QMailAccount::CanSearchOnServer)) {
account.setStatus(QMailAccount::CanSearchOnServer, true);
Expand Down Expand Up @@ -1537,6 +1545,9 @@ void ImapService::enable()
connect(_client, SIGNAL(errorOccurred(QMailServiceAction::Status::ErrorCode, QString)), this, SLOT(errorOccurred(QMailServiceAction::Status::ErrorCode, QString)));
connect(_client, SIGNAL(updateStatus(QString)), this, SLOT(updateStatus(QString)));
connect(_client, SIGNAL(restartPushEmail()), this, SLOT(restartPushEmail()));
#ifdef USE_KEEPALIVE
connect(_client, SIGNAL(stopPushEmail()), this, SLOT(stopPushEmail()));
#endif

QMailAccountConfiguration accountCfg(_accountId);
ImapConfiguration imapCfg(accountCfg);
Expand Down Expand Up @@ -1576,6 +1587,10 @@ void ImapService::disable()
_previousConnectionSettings = connectionSettings(imapCfg);
_restartPushEmailTimer->stop();
_initiatePushEmailTimer->stop();
#ifdef USE_KEEPALIVE
_idling = false;
_backgroundActivity->stop();
#endif
_source->setIntervalTimer(0);
_source->setPushIntervalTimer(0);
_source->retrievalTerminated();
Expand Down Expand Up @@ -1688,9 +1703,22 @@ void ImapService::initiatePushEmail()
<< QMailAccount(_accountId).name();
_restartPushEmailTimer->stop();
_initiatePushEmailTimer->stop();
#ifdef USE_KEEPALIVE
_idling = false;
if (_backgroundActivity->isRunning()) {
_backgroundActivity->stop();
qMailLog(Messaging) << Q_FUNC_INFO << "Stopping keepalive";
}
#endif
QMailFolderIdList ids(_client->configurationIdleFolderIds());
if (ids.count()) {
_establishingPushEmail = true;
#ifdef USE_KEEPALIVE
qMailLog(Messaging) << Q_FUNC_INFO << "Starting keepalive";
_lastSyncCounter = 0;
_idling = true;
_backgroundActivity->wait();
#endif
foreach(QMailFolderId id, ids) {
// Check for flag changes and new mail
_source->queueFlagsChangedCheck(id);
Expand Down Expand Up @@ -1738,6 +1766,42 @@ void ImapService::updateStatus(const QString &text)
updateStatus(QMailServiceAction::Status::ErrNoError, text, _accountId);
}

#ifdef USE_KEEPALIVE
void ImapService::onUpdateLastSyncTime()
{
if (_idling && _client->idlesEstablished()) {
_lastSyncCounter++;
if (_lastSyncCounter == 2) {
QMailAccount account(_accountId);
account.setLastSynchronized(QMailTimeStamp::currentDateTime());
if (!QMailStore::instance()->updateAccount(&account)) {
qWarning() << "Unable to update account" << account.id() << "to set lastSynchronized";
}
_lastSyncCounter = 0;
}
}

// start timer again if still in idle mode
if (_idling) {
_backgroundActivity->wait();
} else if (_backgroundActivity->isRunning()){
qMailLog(Messaging) << Q_FUNC_INFO << "Stopping keepalive";
_backgroundActivity->stop();
_lastSyncCounter = 0;
}
}

void ImapService::stopPushEmail()
{
qMailLog(Messaging) << "Stopping push email for account" << _accountId
<< QMailAccount(_accountId).name();
_idling = false;
_backgroundActivity->stop();
_restartPushEmailTimer->stop();
_initiatePushEmailTimer->stop();
}
#endif

class ImapConfigurator : public QMailMessageServiceConfigurator
{
public:
Expand Down
11 changes: 10 additions & 1 deletion qmf/src/plugins/messageservices/imap/imapservice.h
Expand Up @@ -76,9 +76,13 @@ protected slots:
virtual void accountsUpdated(const QMailAccountIdList &ids);
void errorOccurred(int code, const QString &text);
void errorOccurred(QMailServiceAction::Status::ErrorCode code, const QString &text);

void updateStatus(const QString& text);

#ifdef USE_KEEPALIVE
void onUpdateLastSyncTime();
void stopPushEmail();
#endif

private:
class Source;
friend class Source;
Expand All @@ -96,6 +100,11 @@ protected slots:
enum { ThirtySeconds = 30 };
static QMap<QMailAccountId, int> _initiatePushDelay; // Limit battery consumption
QTimer *_initiatePushEmailTimer;
#ifdef USE_KEEPALIVE
BackgroundActivity* _backgroundActivity;
int _lastSyncCounter;
bool _idling;
#endif
};

class ImapServicePlugin : public QMailMessageServicePlugin
Expand Down
4 changes: 3 additions & 1 deletion rpm/qmf-qt5.spec
@@ -1,6 +1,6 @@
Name: qmf-qt5
Summary: Qt Messaging Framework (QMF) Qt5
Version: 4.0.4+git27
Version: 4.0.4+git28
Release: 1
Group: System/Libraries
License: LGPLv2.1 with exception or GPLv3
Expand All @@ -17,6 +17,7 @@ BuildRequires: pkgconfig(Qt5Network)
BuildRequires: pkgconfig(Qt5Sql)
BuildRequires: pkgconfig(accounts-qt5)
BuildRequires: pkgconfig(libsignon-qt5)
BuildRequires: pkgconfig(keepalive)
#Needed for qhelpgenerator
BuildRequires: qt5-qttools-qthelp-devel
BuildRequires: qt5-plugin-platform-minimal
Expand Down Expand Up @@ -129,6 +130,7 @@ This package contains the documentation for Qt Messaging Framework (QMF).
DEFINES+=QMF_ENABLE_LOGGING \
DEFINES+=MESSAGESERVER_PLUGINS \
DEFINES+=QMF_NO_MESSAGE_SERVICE_EDITOR \
DEFINES+=USE_KEEPALIVE \
CONFIG+=syslog

make %{?_smp_mflags}
Expand Down

0 comments on commit abda1a9

Please sign in to comment.