summaryrefslogtreecommitdiff
path: root/kimap/searchjob.cpp
blob: ede53a33f63fbb4d91a0a4503099d185e9b4b839 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
    Copyright (c) 2009 Andras Mantia <amantia@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 "searchjob.h"

#include <klocale.h>
#include <kdebug.h>

#include <QtCore/QDate>

#include "job_p.h"
#include "message_p.h"
#include "session_p.h"

//TODO: when custom error codes are introduced, handle the NO [TRYCREATE] response

namespace KIMAP
{
  class SearchJobPrivate : public JobPrivate
  {
    public:
      SearchJobPrivate( Session *session, const QString& name ) : JobPrivate(session, name), logic(SearchJob::And) {
        criteriaMap[SearchJob::All]  = "ALL";
        criteriaMap[SearchJob::Answered] = "ANSWERED";
        criteriaMap[SearchJob::BCC] = "BCC";
        criteriaMap[SearchJob::Before] = "BEFORE";
        criteriaMap[SearchJob::Body] = "BODY";
        criteriaMap[SearchJob::CC] = "CC";
        criteriaMap[SearchJob::Deleted] = "DELETED";
        criteriaMap[SearchJob::Draft] = "DRAFT";
        criteriaMap[SearchJob::Flagged] = "FLAGGED";
        criteriaMap[SearchJob::From] = "FROM";
        criteriaMap[SearchJob::Header] = "HEADER";
        criteriaMap[SearchJob::Keyword] = "KEYWORD";
        criteriaMap[SearchJob::Larger] = "LARGER";
        criteriaMap[SearchJob::New] = "NEW";
        criteriaMap[SearchJob::Old] = "OLD";
        criteriaMap[SearchJob::On] = "ON";
        criteriaMap[SearchJob::Recent] = "RECENT";
        criteriaMap[SearchJob::Seen] = "SEEN";
        criteriaMap[SearchJob::SentBefore] = "SENTBEFORE";
        criteriaMap[SearchJob::SentOn] = "SENTON";
        criteriaMap[SearchJob::SentSince] = "SENTSINCE";
        criteriaMap[SearchJob::Since] = "SINCE";
        criteriaMap[SearchJob::Smaller] = "SMALLER";
        criteriaMap[SearchJob::Subject] = "SUBJECT";
        criteriaMap[SearchJob::Text] = "TEXT";
        criteriaMap[SearchJob::To] = "TO";
        criteriaMap[SearchJob::Uid] = "UID";
        criteriaMap[SearchJob::Unanswered] = "UNANSWERED";
        criteriaMap[SearchJob::Undeleted] = "UNDELETED";
        criteriaMap[SearchJob::Undraft] = "UNDRAFT";
        criteriaMap[SearchJob::Unflagged] = "UNFLAGGED";
        criteriaMap[SearchJob::Unkeyword] = "UNKEYWORD";
        criteriaMap[SearchJob::Unseen] = "UNSEEN";

        //don't use QDate::shortMonthName(), it returns a localized month name
        months[1] = "Jan";
        months[2] = "Feb";
        months[3] = "Mar";
        months[4] = "Apr";
        months[5] = "May";
        months[6] = "Jun";
        months[7] = "Jul";
        months[8] = "Aug";
        months[9] = "Sep";
        months[10] = "Oct";
        months[11] = "Nov";
        months[12] = "Dec";

        nextContent = 0;
        uidBased = false;
      }
      ~SearchJobPrivate() { }


      QByteArray charset;
      QList<QByteArray> criterias;
      QMap<SearchJob::SearchCriteria, QByteArray > criteriaMap;
      QMap<int, QByteArray> months;
      SearchJob::SearchLogic logic;
      QList<QByteArray> contents;
      QList<qint64> results;
      uint nextContent;
      bool uidBased;
  };
}

using namespace KIMAP;

SearchJob::SearchJob( Session *session )
  : Job( *new SearchJobPrivate(session, i18nc("Name of the search job", "Search")) )
{
}

SearchJob::~SearchJob()
{
}

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

  QByteArray searchKey;

  if (!d->charset.isEmpty()) {
    searchKey = "CHARSET " + d->charset;
  }

  if (d->logic == SearchJob::Not) {
    searchKey += "NOT";
  } else if (d->logic == SearchJob::Or) {
    searchKey += "OR";
  }

  if ( d->logic == SearchJob::And ) {
    for ( int i = 0; i<d->criterias.size(); i++ ) {
      const QByteArray key = d->criterias.at( i );
      if ( i>0 ) searchKey+= ' ';
      searchKey += key;
    }
  } else {
    for ( int i = 0; i<d->criterias.size(); i++ ) {
      const QByteArray key = d->criterias.at( i );
      if ( i>0 ) searchKey+= ' ';
      searchKey += '(' + key + ')';
    }
  }

  QByteArray command = "SEARCH";
  if ( d->uidBased ) {
    command = "UID "+ command;
  }

  d->tags << d->sessionInternal()->sendCommand( command, searchKey );
}

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

  if (handleErrorReplies(response) == NotHandled ) {
    if ( response.content[0].toString() == "+" ) {
      d->sessionInternal()->sendData( d->contents[d->nextContent] );
      d->nextContent++;
    } else if ( response.content[1].toString() == "SEARCH" ) {
      for(int i = 2; i < response.content.size(); i++) {
        d->results.append(response.content[i].toString().toInt());
      }
    }
  }
}


void SearchJob::setCharset( const QByteArray &charset )
{
  Q_D(SearchJob);
  d->charset = charset;
}

QByteArray SearchJob::charset() const
{
  Q_D(const SearchJob);
  return d->charset;
}

void SearchJob::setSearchLogic( SearchLogic logic )
{
  Q_D(SearchJob);
  d->logic = logic;
}

void SearchJob::addSearchCriteria( SearchCriteria criteria )
{
  Q_D(SearchJob);

  switch (criteria) {
    case All:
    case Answered:
    case Deleted:
    case Draft:
    case Flagged:
    case New:
    case Old:
    case Recent:
    case Seen:
    case Unanswered:
    case Undeleted:
    case Undraft:
    case Unflagged:
    case Unseen:
      d->criterias.append(d->criteriaMap[criteria]);
      break;
    default:
      //TODO Discuss if we keep error checking here, or accept anything, even if it is wrong
      qDebug() << "Criteria " << d->criteriaMap[criteria] << " needs an argument, but none was specified.";
      break;
  }
}


void SearchJob::addSearchCriteria( SearchCriteria criteria, int argument )
{
  Q_D(SearchJob);
  switch (criteria) {
    case Larger:
    case Smaller:
      d->criterias.append(d->criteriaMap[criteria] + ' ' + QByteArray::number(argument));
      break;
    default:
      //TODO Discuss if we keep error checking here, or accept anything, even if it is wrong
      qDebug() << "Criteria " << d->criteriaMap[criteria] << " doesn't accept an integer as an argument.";
      break;
  }
}


void SearchJob::addSearchCriteria( SearchCriteria criteria, const QByteArray &argument )
{
  Q_D(SearchJob);
  switch (criteria) {
    case BCC:
    case Body:
    case CC:
    case From:
    case Subject:
    case Text:
    case To:
      d->contents.append(argument);
      d->criterias.append(d->criteriaMap[criteria] + " {" + QByteArray::number(argument.size()) + '}');
      break;
    case Keyword:
    case Unkeyword:
    case Header:
    case Uid:
      d->criterias.append(d->criteriaMap[criteria] + ' ' + argument);
      break;
    default:
      //TODO Discuss if we keep error checking here, or accept anything, even if it is wrong
      qDebug() << "Criteria " << d->criteriaMap[criteria] << " doesn't accept any argument.";
      break;
  }
}

void SearchJob::addSearchCriteria( SearchCriteria criteria, const QDate &argument )
{
  Q_D(SearchJob);
  switch (criteria) {
    case Before:
    case On:
    case SentBefore:
    case SentSince:
    case Since: {
      QByteArray date = QByteArray::number(argument.day()) + '-';
      date += d->months[argument.month()] + '-';
      date += QByteArray::number(argument.year());
      d->criterias.append(d->criteriaMap[criteria] + " \"" + date + '\"');
      break;
    }
    default:
      //TODO Discuss if we keep error checking here, or accept anything, even if it is wrong
      qDebug() << "Criteria " << d->criteriaMap[criteria] << " doesn't accept a date as argument.";
      break;
  }
}

void SearchJob::addSearchCriteria( const QByteArray &searchCriteria )
{
  Q_D(SearchJob);
  d->criterias.append(searchCriteria);
}

void SearchJob::setUidBased(bool uidBased)
{
  Q_D(SearchJob);
  d->uidBased = uidBased;
}

bool SearchJob::isUidBased() const
{
  Q_D(const SearchJob);
  return d->uidBased;
}

QList<qint64> SearchJob::results() const
{
  Q_D(const SearchJob);
  return d->results;
}

QList<int> SearchJob::foundItems()
{
  Q_D(const SearchJob);

  QList<int> results;
  qCopy( d->results.begin(), d->results.end(), results.begin() );

  return results;
}

#include "searchjob.moc"