summaryrefslogtreecommitdiff
path: root/kimap/idlejob.cpp
blob: 024869725f628a0b08f4e5dab4592fc4d5651113 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
    Copyright (c) 2009 Kevin Ottens <ervin@kde.org>

    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 <QtCore/QTimer>
#include <klocale.h>

#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 ),
          originalSocketTimeout( -1 ) { }
      ~IdleJobPrivate() { }

      void emitStats()
      {
        emitStatsTimer.stop();

        emit q->mailBoxStats(q, m_session->selectedMailBox(),
                             messageCount, recentCount);

        lastMessageCount = messageCount;
        lastRecentCount = recentCount;

        messageCount = -1;
        recentCount = -1;
      }

      IdleJob * const q;

      QTimer emitStatsTimer;

      int messageCount;
      int recentCount;

      int lastMessageCount;
      int lastRecentCount;

      int originalSocketTimeout;
  };
}

using namespace KIMAP;

IdleJob::IdleJob( Session *session )
  : Job( *new IdleJobPrivate(this, session, i18nc("name of the idle job", "Idle")) )
{
  Q_D(IdleJob);
  connect( &d->emitStatsTimer, SIGNAL(timeout()),
           this, SLOT(emitStats()) );
}

IdleJob::~IdleJob()
{
}

void KIMAP::IdleJob::stop()
{
  Q_D(IdleJob);
  d->sessionInternal()->setSocketTimeout( d->originalSocketTimeout );
  d->sessionInternal()->sendData( "DONE" );
}

void IdleJob::doStart()
{
  Q_D(IdleJob);
  d->originalSocketTimeout = d->sessionInternal()->socketTimeout();
  d->sessionInternal()->setSocketTimeout( -1 );
  d->tags << d->sessionInternal()->sendCommand( "IDLE" );
}

void IdleJob::handleResponse( const Message &response )
{
  Q_D(IdleJob);

  // We can predict it'll be handled by handleErrorReplies() so emit
  // pending signals now (if needed) so that result() will really be
  // the last emitted signal.
  if ( !response.content.isEmpty()
       && d->tags.size() == 1
       && d->tags.contains( response.content.first().toString() )
       && ( d->messageCount>=0 || d->recentCount>=0 ) ) {
    d->emitStats();
  }


  if (handleErrorReplies(response) == NotHandled ) {
    if ( response.content.size() > 0 && response.content[0].toString()=="+" ) {
      // Got the continuation all is fine
      return;

    } else if ( response.content.size() > 2 ) {
        if ( response.content[2].toString()=="EXISTS" ) {
        if ( d->messageCount>=0 ) {
          d->emitStats();
        }

        d->messageCount = response.content[1].toString().toInt();
      } else if ( response.content[2].toString()=="RECENT" ) {
        if ( d->recentCount>=0 ) {
          d->emitStats();
        }

        d->recentCount = response.content[1].toString().toInt();
      }
    }

    if ( d->messageCount>=0 && d->recentCount>=0 ) {
      d->emitStats();
    } else if ( d->messageCount>=0 || d->recentCount>=0 ) {
      d->emitStatsTimer.start( 200 );
    }
  }
}

QString KIMAP::IdleJob::lastMailBox() const
{
  Q_D(const IdleJob);
  return d->m_session->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"