emailservices/nmclientapi/src/nmapienvelopelisting.cpp
changeset 54 997a02608b3a
parent 30 759dc5235cdb
equal deleted inserted replaced
53:bf7eb7911fc5 54:997a02608b3a
    12  * Contributors:
    12  * Contributors:
    13  *
    13  *
    14  * Description:
    14  * Description:
    15  *
    15  *
    16  */
    16  */
    17 
    17  
    18 #include "emailtrace.h"
    18 #include "nmapiheaders.h"
    19 
       
    20 #include <nmapienvelopelisting.h>
       
    21 #include "nmapienvelopelisting_p.h"
       
    22 
       
    23 #include <nmapicommonheader.h>
       
    24 
       
    25 #include "nmapiengine.h"
       
    26 
       
    27 
       
    28 
    19 
    29 namespace EmailClientApi
    20 namespace EmailClientApi
    30 {
    21 {
    31 /*!
    22 /*!
    32    Constructor of class. It set start values.
    23    Constructor of class. It set start values.
    36     const quint64 folderId,
    27     const quint64 folderId,
    37     const quint64 mailboxId) :
    28     const quint64 mailboxId) :
    38     NmApiMessageTask(parent)
    29     NmApiMessageTask(parent)
    39 {
    30 {
    40     NM_FUNCTION;
    31     NM_FUNCTION;
    41     
    32     mListingPrivate = new NmApiEnvelopeListingPrivate(folderId, mailboxId, this);
    42     mListingPrivate = new NmApiEnvelopeListingPrivate(this);
       
    43     mListingPrivate->mailboxId = mailboxId;
       
    44     mListingPrivate->folderId = folderId;
       
    45     mListingPrivate->mIsRunning = false;
       
    46 }
    33 }
    47 
    34 
    48 /*!
    35 /*!
    49    Destructor of class. It release engine to be safe if manual releasing won't work.
    36    Destructor of class. 
    50  */
    37  */
    51 NmApiEnvelopeListing::~NmApiEnvelopeListing()
    38 NmApiEnvelopeListing::~NmApiEnvelopeListing()
    52 {
    39 {
    53     NM_FUNCTION;
    40     NM_FUNCTION;
    54     
       
    55     if (mListingPrivate->mIsRunning) {
       
    56         mListingPrivate->releaseEngine();
       
    57     }
       
    58 }
    41 }
    59 
    42 
    60 /*!
    43 /*!
    61    \brief Starts gathering envelopes list.
    44    \brief Fetches envelope list. Emits envelopesListed signal when ready.
    62    
    45    Results can be fetched with getEnvelopes.
    63    In first turn it will get whole folderlist. 
       
    64    If start works, it do nothing.
       
    65    
    46    
    66    To asynchronous operation ce be used \sa QTimer::singleShot on this method.
    47    To asynchronous operation ce be used \sa QTimer::singleShot on this method.
    67    Example:
    48    Example:
    68    <code> 
    49    <code> 
    69    QTimer::singleShot(0,nmEnvelopeListing,SLOT(start());
    50    QTimer::singleShot(0,nmEnvelopeListing,SLOT(start());
    71    
    52    
    72  */
    53  */
    73 bool NmApiEnvelopeListing::start()
    54 bool NmApiEnvelopeListing::start()
    74 {
    55 {
    75     NM_FUNCTION;
    56     NM_FUNCTION;
    76     
       
    77     bool result = false;
       
    78 
    57 
    79     if (mListingPrivate->mIsRunning) {
    58     qint32 envelopesCount = mListingPrivate->listEnvelopes();
    80         result = true;
    59     QMetaObject::invokeMethod(this, "envelopesListed", Qt::QueuedConnection, Q_ARG(qint32,
    81     }
       
    82     else {
       
    83 
       
    84         bool started = mListingPrivate->initializeEngine();
       
    85         if (!started) {
       
    86             QMetaObject::invokeMethod(this, "envelopesListed", Qt::QueuedConnection, Q_ARG(qint32,
       
    87                 (qint32) EnvelopeListingFailed));
       
    88             result = false;
       
    89         }
       
    90         else {
       
    91             qint32 envelopesCount = mListingPrivate->grabEnvelopes();
       
    92 
       
    93             mListingPrivate->mIsRunning = true;
       
    94             QMetaObject::invokeMethod(this, "envelopesListed", Qt::QueuedConnection, Q_ARG(qint32,
       
    95                 envelopesCount));
    60                 envelopesCount));
    96             result = true;
    61     return true;
    97         }
       
    98     }
       
    99     return result;
       
   100 }
    62 }
   101 
    63 
   102 /*!
    64 /*!
   103    \brief Stop gathering envelope list.
    65    \brief Stop gathering envelope list.
   104    
    66 
   105    In first it change state of listing.
    67    Clears list of envelopes and emits \sa NmApiMessageTask::canceled() signal.
   106    Then it release engine.
       
   107    On end it clears list of envelopes and emits \sa NmApiMessageTask::canceled() signal.
       
   108  */
    68  */
   109 void NmApiEnvelopeListing::cancel()
    69 void NmApiEnvelopeListing::cancel()
   110 {
    70 {
   111     NM_FUNCTION;
    71     NM_FUNCTION;
   112     
    72     mListingPrivate->cancel();
   113     if (mListingPrivate->mIsRunning) {
    73     emit canceled();
   114         mListingPrivate->mIsRunning = false;
       
   115         mListingPrivate->releaseEngine();
       
   116         mListingPrivate->mEnvelopes.clear();
       
   117 
    74 
   118         emit canceled();
       
   119     }
       
   120 }
    75 }
   121 
    76 
   122 /*! 
    77 /*! 
   123    \brief Returns results after envelopesListed signal is received.
    78    \brief Returns results after envelopesListed signal is received.
   124    
    79    
   125     Caller gets ownership of envelopes. Returns true if results were available.
    80     Caller gets ownership of envelopes. Returns true if results were available.
   126     Before calling cancel and start should be called,
    81     Before calling start should be called, otherwise will return empty list.
   127     because after second calling it return empty list.
    82     It also clears inputlist of NmMessageEnvelope.
   128     It also at start clear inputlist of NmMessageEnvelope.
       
   129  */
    83  */
   130 bool NmApiEnvelopeListing::getEnvelopes(QList<EmailClientApi::NmApiMessageEnvelope> &envelopes)
    84 bool NmApiEnvelopeListing::getEnvelopes(QList<EmailClientApi::NmApiMessageEnvelope> &envelopes)
   131 {
    85 {
   132     NM_FUNCTION;
    86     NM_FUNCTION;
   133     
    87     return mListingPrivate->envelopes(envelopes);
   134     envelopes.clear();
       
   135 
       
   136     bool result = false;
       
   137 
       
   138     if (!mListingPrivate->mIsRunning) {
       
   139         result = false;
       
   140     }
       
   141     else {
       
   142         envelopes = mListingPrivate->mEnvelopes;
       
   143         mListingPrivate->mEnvelopes.clear();
       
   144         result = true;
       
   145     }
       
   146     return result;
       
   147 }
    88 }
   148 
    89 
   149 /*!
    90 /*!
   150    \brief Return info if listing is running
    91    \brief Return info if listing is running
   151  */
    92  */
   152 bool NmApiEnvelopeListing::isRunning() const
    93 bool NmApiEnvelopeListing::isRunning() const
   153 {
    94 {
   154     NM_FUNCTION;
    95     NM_FUNCTION;
   155     
    96     return mListingPrivate->isRunning();
   156     return mListingPrivate->mIsRunning;
       
   157 }
    97 }
   158 
    98 
   159 }
    99 }