• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

KIMAP Library

  • kimap
searchjob.cpp
1 /*
2  Copyright (c) 2009 Andras Mantia <amantia@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "searchjob.h"
21 
22 #include <KDE/KLocale>
23 #include <KDE/KDebug>
24 
25 #include <QtCore/QDate>
26 
27 #include "job_p.h"
28 #include "message_p.h"
29 #include "session_p.h"
30 
31 //TODO: when custom error codes are introduced, handle the NO [TRYCREATE] response
32 
33 namespace KIMAP
34 {
35  class SearchJobPrivate : public JobPrivate
36  {
37  public:
38  SearchJobPrivate( Session *session, const QString& name ) : JobPrivate( session, name ), logic( SearchJob::And ) {
39  criteriaMap[SearchJob::All] = "ALL";
40  criteriaMap[SearchJob::Answered] = "ANSWERED";
41  criteriaMap[SearchJob::BCC] = "BCC";
42  criteriaMap[SearchJob::Before] = "BEFORE";
43  criteriaMap[SearchJob::Body] = "BODY";
44  criteriaMap[SearchJob::CC] = "CC";
45  criteriaMap[SearchJob::Deleted] = "DELETED";
46  criteriaMap[SearchJob::Draft] = "DRAFT";
47  criteriaMap[SearchJob::Flagged] = "FLAGGED";
48  criteriaMap[SearchJob::From] = "FROM";
49  criteriaMap[SearchJob::Header] = "HEADER";
50  criteriaMap[SearchJob::Keyword] = "KEYWORD";
51  criteriaMap[SearchJob::Larger] = "LARGER";
52  criteriaMap[SearchJob::New] = "NEW";
53  criteriaMap[SearchJob::Old] = "OLD";
54  criteriaMap[SearchJob::On] = "ON";
55  criteriaMap[SearchJob::Recent] = "RECENT";
56  criteriaMap[SearchJob::Seen] = "SEEN";
57  criteriaMap[SearchJob::SentBefore] = "SENTBEFORE";
58  criteriaMap[SearchJob::SentOn] = "SENTON";
59  criteriaMap[SearchJob::SentSince] = "SENTSINCE";
60  criteriaMap[SearchJob::Since] = "SINCE";
61  criteriaMap[SearchJob::Smaller] = "SMALLER";
62  criteriaMap[SearchJob::Subject] = "SUBJECT";
63  criteriaMap[SearchJob::Text] = "TEXT";
64  criteriaMap[SearchJob::To] = "TO";
65  criteriaMap[SearchJob::Uid] = "UID";
66  criteriaMap[SearchJob::Unanswered] = "UNANSWERED";
67  criteriaMap[SearchJob::Undeleted] = "UNDELETED";
68  criteriaMap[SearchJob::Undraft] = "UNDRAFT";
69  criteriaMap[SearchJob::Unflagged] = "UNFLAGGED";
70  criteriaMap[SearchJob::Unkeyword] = "UNKEYWORD";
71  criteriaMap[SearchJob::Unseen] = "UNSEEN";
72 
73  //don't use QDate::shortMonthName(), it returns a localized month name
74  months[1] = "Jan";
75  months[2] = "Feb";
76  months[3] = "Mar";
77  months[4] = "Apr";
78  months[5] = "May";
79  months[6] = "Jun";
80  months[7] = "Jul";
81  months[8] = "Aug";
82  months[9] = "Sep";
83  months[10] = "Oct";
84  months[11] = "Nov";
85  months[12] = "Dec";
86 
87  nextContent = 0;
88  uidBased = false;
89  }
90  ~SearchJobPrivate() { }
91 
92 
93  QByteArray charset;
94  QList<QByteArray> criterias;
95  QMap<SearchJob::SearchCriteria, QByteArray > criteriaMap;
96  QMap<int, QByteArray> months;
97  SearchJob::SearchLogic logic;
98  QList<QByteArray> contents;
99  QList<qint64> results;
100  uint nextContent;
101  bool uidBased;
102  };
103 }
104 
105 using namespace KIMAP;
106 
107 SearchJob::SearchJob( Session *session )
108  : Job( *new SearchJobPrivate( session, i18nc( "Name of the search job", "Search" ) ) )
109 {
110 }
111 
112 SearchJob::~SearchJob()
113 {
114 }
115 
116 void SearchJob::doStart()
117 {
118  Q_D( SearchJob );
119 
120  QByteArray searchKey;
121 
122  if ( !d->charset.isEmpty() ) {
123  searchKey = "CHARSET " + d->charset;
124  }
125 
126  if ( d->logic == SearchJob::Not ) {
127  searchKey += "NOT";
128  } else if ( d->logic == SearchJob::Or ) {
129  searchKey += "OR";
130  }
131 
132  if ( d->logic == SearchJob::And ) {
133  for ( int i = 0; i < d->criterias.size(); i++ ) {
134  const QByteArray key = d->criterias.at( i );
135  if ( i > 0 ) {
136  searchKey += ' ';
137  }
138  searchKey += key;
139  }
140  } else {
141  for ( int i = 0; i < d->criterias.size(); i++ ) {
142  const QByteArray key = d->criterias.at( i );
143  if ( i > 0 ) {
144  searchKey += ' ';
145  }
146  searchKey += '(' + key + ')';
147  }
148  }
149 
150  QByteArray command = "SEARCH";
151  if ( d->uidBased ) {
152  command = "UID " + command;
153  }
154 
155  d->tags << d->sessionInternal()->sendCommand( command, searchKey );
156 }
157 
158 void SearchJob::handleResponse( const Message &response )
159 {
160  Q_D( SearchJob );
161 
162  if ( handleErrorReplies( response ) == NotHandled ) {
163  if ( response.content[0].toString() == "+" ) {
164  d->sessionInternal()->sendData( d->contents[d->nextContent] );
165  d->nextContent++;
166  } else if ( response.content[1].toString() == "SEARCH" ) {
167  for ( int i = 2; i < response.content.size(); i++ ) {
168  d->results.append( response.content[i].toString().toInt() );
169  }
170  }
171  }
172 }
173 
174 
175 void SearchJob::setCharset( const QByteArray &charset )
176 {
177  Q_D( SearchJob );
178  d->charset = charset;
179 }
180 
181 QByteArray SearchJob::charset() const
182 {
183  Q_D( const SearchJob );
184  return d->charset;
185 }
186 
187 void SearchJob::setSearchLogic( SearchLogic logic )
188 {
189  Q_D( SearchJob );
190  d->logic = logic;
191 }
192 
193 void SearchJob::addSearchCriteria( SearchCriteria criteria )
194 {
195  Q_D( SearchJob );
196 
197  switch ( criteria ) {
198  case All:
199  case Answered:
200  case Deleted:
201  case Draft:
202  case Flagged:
203  case New:
204  case Old:
205  case Recent:
206  case Seen:
207  case Unanswered:
208  case Undeleted:
209  case Undraft:
210  case Unflagged:
211  case Unseen:
212  d->criterias.append( d->criteriaMap[criteria] );
213  break;
214  default:
215  //TODO Discuss if we keep error checking here, or accept anything, even if it is wrong
216  kDebug() << "Criteria " << d->criteriaMap[criteria] << " needs an argument, but none was specified.";
217  break;
218  }
219 }
220 
221 void SearchJob::addSearchCriteria( SearchCriteria criteria, int argument )
222 {
223  Q_D( SearchJob );
224  switch ( criteria ) {
225  case Larger:
226  case Smaller:
227  d->criterias.append( d->criteriaMap[criteria] + ' ' + QByteArray::number( argument ) );
228  break;
229  default:
230  //TODO Discuss if we keep error checking here, or accept anything, even if it is wrong
231  kDebug() << "Criteria " << d->criteriaMap[criteria] << " doesn't accept an integer as an argument.";
232  break;
233  }
234 }
235 
236 void SearchJob::addSearchCriteria( SearchCriteria criteria, const QByteArray &argument )
237 {
238  Q_D( SearchJob );
239  switch ( criteria ) {
240  case BCC:
241  case Body:
242  case CC:
243  case From:
244  case Subject:
245  case Text:
246  case To:
247  d->contents.append( argument );
248  d->criterias.append( d->criteriaMap[criteria] + " {" + QByteArray::number( argument.size() ) + '}' );
249  break;
250  case Keyword:
251  case Unkeyword:
252  case Header:
253  case Uid:
254  d->criterias.append( d->criteriaMap[criteria] + ' ' + argument );
255  break;
256  default:
257  //TODO Discuss if we keep error checking here, or accept anything, even if it is wrong
258  kDebug() << "Criteria " << d->criteriaMap[criteria] << " doesn't accept any argument.";
259  break;
260  }
261 }
262 
263 void SearchJob::addSearchCriteria( SearchCriteria criteria, const QDate &argument )
264 {
265  Q_D( SearchJob );
266  switch ( criteria ) {
267  case Before:
268  case On:
269  case SentBefore:
270  case SentSince:
271  case Since: {
272  QByteArray date = QByteArray::number( argument.day() ) + '-';
273  date += d->months[argument.month()] + '-';
274  date += QByteArray::number( argument.year() );
275  d->criterias.append( d->criteriaMap[criteria] + " \"" + date + '\"' );
276  break;
277  }
278  default:
279  //TODO Discuss if we keep error checking here, or accept anything, even if it is wrong
280  kDebug() << "Criteria " << d->criteriaMap[criteria] << " doesn't accept a date as argument.";
281  break;
282  }
283 }
284 
285 void SearchJob::addSearchCriteria( const QByteArray &searchCriteria )
286 {
287  Q_D( SearchJob );
288  d->criterias.append( searchCriteria );
289 }
290 
291 void SearchJob::setUidBased(bool uidBased)
292 {
293  Q_D( SearchJob );
294  d->uidBased = uidBased;
295 }
296 
297 bool SearchJob::isUidBased() const
298 {
299  Q_D( const SearchJob );
300  return d->uidBased;
301 }
302 
303 QList<qint64> SearchJob::results() const
304 {
305  Q_D( const SearchJob );
306  return d->results;
307 }
308 
309 QList<int> SearchJob::foundItems()
310 {
311  Q_D( const SearchJob );
312 
313  QList<int> results;
314  qCopy( d->results.begin(), d->results.end(), results.begin() );
315 
316  return results;
317 }
318 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:25:17 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIMAP Library

Skip menu "KIMAP Library"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal