~ubuntu-branches/ubuntu/quantal/kdepimlibs/quantal-proposed

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
/*
    Copyright (c) 2009 Tobias Koenig <tokoe@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 "itemsearchjob.h"

#include "imapparser_p.h"
#include "itemfetchscope.h"
#include "job_p.h"
#include "protocolhelper_p.h"

#include <QtCore/QTimer>

using namespace Akonadi;

class Akonadi::ItemSearchJobPrivate : public JobPrivate
{
  public:
    ItemSearchJobPrivate( ItemSearchJob *parent, const QString &query )
      : JobPrivate( parent ), mQuery( query )
    {
    }

    void timeout()
    {
      Q_Q( Akonadi::ItemSearchJob );

      mEmitTimer->stop(); // in case we are called by result()
      if ( !mPendingItems.isEmpty() ) {
        if ( !q->error() )
          emit q->itemsReceived( mPendingItems );
        mPendingItems.clear();
      }
    }

    Q_DECLARE_PUBLIC( ItemSearchJob )

    QString mQuery;
    Item::List mItems;
    ItemFetchScope mFetchScope;
    Item::List mPendingItems; // items pending for emitting itemsReceived()
    QTimer* mEmitTimer;
};

ItemSearchJob::ItemSearchJob( const QString & query, QObject * parent )
  : Job( new ItemSearchJobPrivate( this, query ), parent )
{
  Q_D( ItemSearchJob );

  d->mEmitTimer = new QTimer( this );
  d->mEmitTimer->setSingleShot( true );
  d->mEmitTimer->setInterval( 100 );
  connect( d->mEmitTimer, SIGNAL(timeout()), this, SLOT(timeout()) );
  connect( this, SIGNAL(result(KJob*)), this, SLOT(timeout()) );
}

ItemSearchJob::~ItemSearchJob()
{
}

void ItemSearchJob::setQuery( const QString &query )
{
  Q_D( ItemSearchJob );

  d->mQuery = query;
}

void ItemSearchJob::setFetchScope( const ItemFetchScope &fetchScope )
{
  Q_D( ItemSearchJob );

  d->mFetchScope = fetchScope;
}

ItemFetchScope &ItemSearchJob::fetchScope()
{
  Q_D( ItemSearchJob );

  return d->mFetchScope;
}

void ItemSearchJob::doStart()
{
  Q_D( ItemSearchJob );

  QByteArray command = d->newTag() + " SEARCH ";
  command += ImapParser::quote( d->mQuery.toUtf8() );
  command += ' ' + ProtocolHelper::itemFetchScopeToByteArray( d->mFetchScope );
  command += '\n';
  d->writeData( command );
}

void ItemSearchJob::doHandleResponse( const QByteArray & tag, const QByteArray & data )
{
  Q_D( ItemSearchJob );

  if ( tag == "*" ) {
    int begin = data.indexOf( "SEARCH" );
    if ( begin >= 0 ) {

      // split fetch response into key/value pairs
      QList<QByteArray> fetchResponse;
      ImapParser::parseParenthesizedList( data, fetchResponse, begin + 7 );

      Item item;
      ProtocolHelper::parseItemFetchResult( fetchResponse, item );
      if ( !item.isValid() )
        return;

      d->mItems.append( item );
      d->mPendingItems.append( item );
      if ( !d->mEmitTimer->isActive() )
        d->mEmitTimer->start();
      return;
    }
  }
  kDebug() << "Unhandled response: " << tag << data;
}

Item::List ItemSearchJob::items() const
{
  Q_D( const ItemSearchJob );

  return d->mItems;
}

QUrl ItemSearchJob::akonadiItemIdUri()
{
  return QUrl( QLatin1String( "http://akonadi-project.org/ontologies/aneo#akonadiItemId" ) );
}

#include "itemsearchjob.moc"