diff --git a/kimap/CMakeLists.txt b/kimap/CMakeLists.txt index 6af69bf06..b8f13d91b 100644 --- a/kimap/CMakeLists.txt +++ b/kimap/CMakeLists.txt @@ -1,64 +1,65 @@ project(kimap) add_definitions( -DKDE_DEFAULT_DEBUG_AREA=5327 ) add_subdirectory( tests ) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${KDE4_ENABLE_EXCEPTIONS}") set(kimap_LIB_SRCS acl.cpp imapset.cpp imapstreamparser.cpp job.cpp appendjob.cpp capabilitiesjob.cpp fetchjob.cpp + idlejob.cpp listjob.cpp loginjob.cpp logoutjob.cpp rfccodecs.cpp selectjob.cpp session.cpp sessionthread.cpp closejob.cpp expungejob.cpp deletejob.cpp createjob.cpp subscribejob.cpp unsubscribejob.cpp renamejob.cpp storejob.cpp copyjob.cpp searchjob.cpp acljobbase.cpp setacljob.cpp getacljob.cpp deleteacljob.cpp myrightsjob.cpp listrightsjob.cpp quotajobbase.cpp setquotajob.cpp getquotajob.cpp getquotarootjob.cpp metadatajobbase.cpp setmetadatajob.cpp getmetadatajob.cpp ) kde4_add_library(kimap SHARED ${kimap_LIB_SRCS}) target_link_libraries(kimap ${KDE4_KDECORE_LIBS} ${QT_QTNETWORK_LIBRARY} kmime ${SASL2_LIBRARIES}) include_directories( ${CMAKE_SOURCE_DIR}/kioslave ${SASL2_INCLUDE_DIR} ${Boost_INCLUDE_DIR} ) set_target_properties(kimap PROPERTIES VERSION ${GENERIC_LIB_VERSION} SOVERSION ${GENERIC_LIB_SOVERSION} ) install(TARGETS kimap EXPORT kdepimlibsLibraryTargets ${INSTALL_TARGETS_DEFAULT_ARGS}) ########### install files ############### -install( FILES kimap_export.h acl.h imapset.h job.h appendjob.h capabilitiesjob.h fetchjob.h listjob.h loginjob.h logoutjob.h rfccodecs.h +install( FILES kimap_export.h acl.h imapset.h job.h appendjob.h capabilitiesjob.h fetchjob.h idlejob.h listjob.h loginjob.h logoutjob.h rfccodecs.h selectjob.h closejob.h expungejob.h deletejob.h createjob.h subscribejob.h unsubscribejob.h renamejob.h session.h sessionuiproxy.h storejob.h copyjob.h searchjob.h acljobbase.h setacljob.h getacljob.h deleteacljob.h myrightsjob.h listrightsjob.h quotajobbase.h setquotajob.h getquotajob.h getquotarootjob.h metadatajobbase.h setmetadatajob.h getmetadatajob.h DESTINATION ${INCLUDE_INSTALL_DIR}/kimap COMPONENT Devel) diff --git a/kimap/idlejob.cpp b/kimap/idlejob.cpp new file mode 100644 index 000000000..7f9b6745b --- /dev/null +++ b/kimap/idlejob.cpp @@ -0,0 +1,119 @@ +/* + Copyright (c) 2009 Kevin Ottens + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library 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 Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include "idlejob.h" + +#include +#include + +#include "job_p.h" +#include "message_p.h" +#include "session_p.h" + +namespace KIMAP +{ + class IdleJobPrivate : public JobPrivate + { + public: + IdleJobPrivate( IdleJob *job, Session *session, const QString& name ) + : JobPrivate( session, name ), q(job), + messageCount( -1 ), recentCount( -1 ), + lastMessageCount( -1 ), lastRecentCount( -1 ) { } + ~IdleJobPrivate() { } + + IdleJob * const q; + + int messageCount; + int recentCount; + + int lastMessageCount; + int lastRecentCount; + }; +} + +using namespace KIMAP; + +IdleJob::IdleJob( Session *session ) + : Job( *new IdleJobPrivate(this, session, i18n("Idle")) ) +{ +} + +IdleJob::~IdleJob() +{ +} + +void KIMAP::IdleJob::stop() +{ + Q_D(IdleJob); + d->sessionInternal()->sendData( "DONE" ); +} + +void IdleJob::doStart() +{ + Q_D(IdleJob); + d->tag = d->sessionInternal()->sendCommand( "IDLE" ); +} + +void IdleJob::handleResponse( const Message &response ) +{ + Q_D(IdleJob); + + if (handleErrorReplies(response) == NotHandled ) { + if ( response.content.size() > 0 && response.content[0].toString()=="+" ) { + // Got the continuation all is fine + return; + + } else if ( response.content[2].toString()=="EXISTS" ) { + d->messageCount = response.content[1].toString().toInt(); + } else if ( response.content[2].toString()=="RECENT" ) { + d->recentCount = response.content[1].toString().toInt(); + } + + if ( d->messageCount>=0 && d->recentCount>=0 ) { + emit mailBoxStats(this, d->sessionInternal()->selectedMailBox(), + d->messageCount, d->recentCount); + + d->lastMessageCount = d->messageCount; + d->lastRecentCount = d->recentCount; + + d->messageCount = -1; + d->recentCount = -1; + } + } +} + +QString KIMAP::IdleJob::lastMailBox() const +{ + Q_D(const IdleJob); + return d->sessionInternal()->selectedMailBox(); +} + +int KIMAP::IdleJob::lastMessageCount() const +{ + Q_D(const IdleJob); + return d->lastMessageCount; +} + +int KIMAP::IdleJob::lastRecentCount() const +{ + Q_D(const IdleJob); + return d->lastRecentCount; +} + +#include "idlejob.moc" diff --git a/kimap/idlejob.h b/kimap/idlejob.h new file mode 100644 index 000000000..c925141e1 --- /dev/null +++ b/kimap/idlejob.h @@ -0,0 +1,65 @@ +/* + Copyright (c) 2009 Kevin Ottens + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library 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 Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#ifndef KIMAP_IDLEJOB_H +#define KIMAP_IDLEJOB_H + +#include "kimap_export.h" + +#include "imapset.h" +#include "job.h" + +#include "kmime/kmime_content.h" +#include "kmime/kmime_message.h" + +#include + +namespace KIMAP { + +class Session; +struct Message; +class IdleJobPrivate; + +class KIMAP_EXPORT IdleJob : public Job +{ + Q_OBJECT + Q_DECLARE_PRIVATE(IdleJob) + + public: + explicit IdleJob( Session *session ); + virtual ~IdleJob(); + + QString lastMailBox() const; + int lastMessageCount() const; + int lastRecentCount() const; + + public Q_SLOTS: + void stop(); + + Q_SIGNALS: + void mailBoxStats(KIMAP::IdleJob *job, const QString &mailBox, int messageCount, int recentCount); + + protected: + virtual void doStart(); + virtual void handleResponse(const Message &response); +}; + +} + +#endif diff --git a/kimap/tests/CMakeLists.txt b/kimap/tests/CMakeLists.txt index 9f69663c8..c561580f0 100644 --- a/kimap/tests/CMakeLists.txt +++ b/kimap/tests/CMakeLists.txt @@ -1,55 +1,56 @@ include_directories( ${CMAKE_SOURCE_DIR}/kimap ${Boost_INCLUDE_DIR}) set( EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR} ) MACRO(KIMAP_UNIT_TESTS) FOREACH(_testname ${ARGN}) kde4_add_unit_test(${_testname} TESTNAME kimap-${_testname} NOGUI ${_testname}.cpp) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${KDE4_ENABLE_EXCEPTIONS}") target_link_libraries(${_testname} ${KDE4_KDECORE_LIBS} ${QT_QTTEST_LIBRARY} kimap kmime) ENDFOREACH(_testname) ENDMACRO(KIMAP_UNIT_TESTS) MACRO(KIMAP_EXECUTABLE_TESTS) FOREACH(_testname ${ARGN}) kde4_add_executable(${_testname} NOGUI TEST ${_testname}.cpp) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${KDE4_ENABLE_EXCEPTIONS}") target_link_libraries(${_testname} ${KDE4_KDECORE_LIBS} ${KDE4_KIO_LIBS} kimap kmime) ENDFOREACH(_testname) ENDMACRO(KIMAP_EXECUTABLE_TESTS) ### convenience macro MACRO(ADD_IMAPLIB_TEST _source) set(_test ${_source} fakeserver.cpp ) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${KDE4_ENABLE_EXCEPTIONS}") get_filename_component(_name ${_source} NAME_WE) kde4_add_unit_test(${_name} TESTNAME kimap-${_name} ${_test}) target_link_libraries(${_name} kimap ${QT_QTTEST_LIBRARY} ${KDE4_KDECORE_LIBS} ${QT_QTGUI_LIBRARY} ${QT_QTNETWORK_LIBRARY} ) ENDMACRO(ADD_IMAPLIB_TEST) ### tests ADD_IMAPLIB_TEST(loginjobtest.cpp) ADD_IMAPLIB_TEST(logoutjobtest.cpp) ADD_IMAPLIB_TEST(capabilitiesjobtest.cpp) ADD_IMAPLIB_TEST(selectjobtest.cpp) ADD_IMAPLIB_TEST(createjobtest.cpp) ADD_IMAPLIB_TEST(deletejobtest.cpp) ADD_IMAPLIB_TEST(renamejobtest.cpp) ADD_IMAPLIB_TEST(subscribejobtest.cpp) ADD_IMAPLIB_TEST(unsubscribejobtest.cpp) ADD_IMAPLIB_TEST(listjobtest.cpp) ADD_IMAPLIB_TEST(storejobtest.cpp) ########### automated tests ############### KIMAP_UNIT_TESTS( testrfccodecs testsession ) ########### manual tests ############### KIMAP_EXECUTABLE_TESTS( + testimapidle testimapserver ) diff --git a/kimap/tests/testimapidle.cpp b/kimap/tests/testimapidle.cpp new file mode 100644 index 000000000..288d11335 --- /dev/null +++ b/kimap/tests/testimapidle.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "kimap/session.h" +#include "kimap/capabilitiesjob.h" +#include "kimap/idlejob.h" +#include "kimap/loginjob.h" +#include "kimap/logoutjob.h" +#include "kimap/selectjob.h" +#include "kimap/closejob.h" +#include "kimap/sessionuiproxy.h" + +using namespace KIMAP; + +class UiProxy: public SessionUiProxy { + public: + bool ignoreSslError(const KSslErrorUiData& errorData) { + if (KIO::SslUi::askIgnoreSslErrors(errorData, KIO::SslUi::StoreRules)) { + return true; + } else { + return false; + } + } +}; + +int main( int argc, char **argv ) +{ + KAboutData about("TestImapIdle", 0, ki18n("TestImapIdle"), "version"); + KComponentData cData(&about); + + if (argc < 4) { + kError() << "Not enough parameters, expecting: "; + } + + QString server = QString::fromLocal8Bit(argv[1]); + int port = 143; + if ( server.count( ':' ) == 1 ) { + port = server.split( ':' ).last().toInt(); + server = server.split( ':' ).first(); + } + QString user = QString::fromLocal8Bit(argv[2]); + QString password = QString::fromLocal8Bit(argv[3]); + + kDebug() << "Listening:" << server << port << user << password; + qDebug(); + + QApplication app(argc, argv); + Session session(server, port); + UiProxy *proxy = new UiProxy(); + session.setUiProxy(proxy); + + kDebug() << "Logging in..."; + LoginJob *login = new LoginJob(&session); + login->setUserName(user); + login->setPassword(password); + login->exec(); + Q_ASSERT_X(login->error()==0, "LoginJob", login->errorString().toLocal8Bit()); + Q_ASSERT(session.state()==Session::Authenticated); + qDebug(); + + kDebug() << "Asking for capabilities:"; + CapabilitiesJob *capabilities = new CapabilitiesJob(&session); + capabilities->exec(); + Q_ASSERT_X(capabilities->error()==0, "CapabilitiesJob", capabilities->errorString().toLocal8Bit()); + Q_ASSERT(session.state()==Session::Authenticated); + kDebug() << capabilities->capabilities(); + qDebug(); + + Q_ASSERT(capabilities->capabilities().contains("IDLE")); + + kDebug() << "Selecting INBOX:"; + SelectJob *select = new SelectJob(&session); + select->setMailBox("INBOX"); + select->exec(); + Q_ASSERT_X(select->error()==0, "SelectJob", select->errorString().toLocal8Bit()); + Q_ASSERT(session.state()==Session::Selected); + kDebug() << "Flags:" << select->flags(); + kDebug() << "Permanent flags:" << select->permanentFlags(); + kDebug() << "Total Number of Messages:" << select->messageCount(); + kDebug() << "Number of recent Messages:" << select->recentCount(); + kDebug() << "First Unseen Message Index:" << select->firstUnseenIndex(); + kDebug() << "UID validity:" << select->uidValidity(); + kDebug() << "Next UID:" << select->nextUid(); + qDebug(); + + kDebug() << "Start idling..."; + IdleJob *idle = new IdleJob(&session); + QObject::connect(idle, SIGNAL(mailBoxStats(IdleJob*, QString, int, int)), + idle, SLOT(stop())); + idle->exec(); + kDebug() << "Idling done for" << idle->lastMailBox() + << "message count:" << idle->lastMessageCount() + << "recent count:" << idle->lastRecentCount(); + + kDebug() << "Closing INBOX:"; + CloseJob *close = new CloseJob(&session); + close->exec(); + Q_ASSERT(session.state()==Session::Authenticated); + qDebug(); + + kDebug() << "Logging out..."; + LogoutJob *logout = new LogoutJob(&session); + logout->exec(); + Q_ASSERT_X(logout->error()==0, "LogoutJob", logout->errorString().toLocal8Bit()); + Q_ASSERT(session.state()==Session::Disconnected); + + return 0; +}