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

KIMAP Library

  • sources
  • kde-4.12
  • kdepimlibs
  • 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/KLocalizedString>
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  QByteArray charset;
93  QList<QByteArray> criterias;
94  QMap<SearchJob::SearchCriteria, QByteArray > criteriaMap;
95  QMap<int, QByteArray> months;
96  SearchJob::SearchLogic logic;
97  QList<QByteArray> contents;
98  QList<qint64> results;
99  uint nextContent;
100  bool uidBased;
101  };
102 }
103 
104 using namespace KIMAP;
105 
106 SearchJob::SearchJob( Session *session )
107  : Job( *new SearchJobPrivate( session, i18nc( "Name of the search job", "Search" ) ) )
108 {
109 }
110 
111 SearchJob::~SearchJob()
112 {
113 }
114 
115 void SearchJob::doStart()
116 {
117  Q_D( SearchJob );
118 
119  QByteArray searchKey;
120 
121  if ( !d->charset.isEmpty() ) {
122  searchKey = "CHARSET " + d->charset;
123  }
124 
125  if ( d->logic == SearchJob::Not ) {
126  searchKey += "NOT ";
127  } else if ( d->logic == SearchJob::Or && d->criterias.size() > 1 ) {
128  searchKey += "OR ";
129  }
130 
131  if ( d->logic == SearchJob::And ) {
132  for ( int i = 0; i < d->criterias.size(); i++ ) {
133  const QByteArray key = d->criterias.at( i );
134  if ( i > 0 ) {
135  searchKey += ' ';
136  }
137  searchKey += key;
138  }
139  } else {
140  for ( int i = 0; i < d->criterias.size(); i++ ) {
141  const QByteArray key = d->criterias.at( i );
142  if ( i > 0 ) {
143  searchKey += ' ';
144  }
145  searchKey += '(' + key + ')';
146  }
147  }
148 
149  QByteArray command = "SEARCH";
150  if ( d->uidBased ) {
151  command = "UID " + command;
152  }
153 
154  d->tags << d->sessionInternal()->sendCommand( command, searchKey );
155 }
156 
157 void SearchJob::handleResponse( const Message &response )
158 {
159  Q_D( SearchJob );
160 
161  if ( handleErrorReplies( response ) == NotHandled ) {
162  if ( response.content[0].toString() == "+" ) {
163  d->sessionInternal()->sendData( d->contents[d->nextContent] );
164  d->nextContent++;
165  } else if ( response.content[1].toString() == "SEARCH" ) {
166  for ( int i = 2; i < response.content.size(); i++ ) {
167  d->results.append( response.content[i].toString().toInt() );
168  }
169  }
170  }
171 }
172 
173 void SearchJob::setCharset( const QByteArray &charset )
174 {
175  Q_D( SearchJob );
176  d->charset = charset;
177 }
178 
179 QByteArray SearchJob::charset() const
180 {
181  Q_D( const SearchJob );
182  return d->charset;
183 }
184 
185 void SearchJob::setSearchLogic( SearchLogic logic )
186 {
187  Q_D( SearchJob );
188  d->logic = logic;
189 }
190 
191 void SearchJob::addSearchCriteria( SearchCriteria criteria )
192 {
193  Q_D( SearchJob );
194 
195  switch ( criteria ) {
196  case All:
197  case Answered:
198  case Deleted:
199  case Draft:
200  case Flagged:
201  case New:
202  case Old:
203  case Recent:
204  case Seen:
205  case Unanswered:
206  case Undeleted:
207  case Undraft:
208  case Unflagged:
209  case Unseen:
210  d->criterias.append( d->criteriaMap[criteria] );
211  break;
212  default:
213  //TODO Discuss if we keep error checking here, or accept anything, even if it is wrong
214  kDebug() << "Criteria " << d->criteriaMap[criteria] << " needs an argument, but none was specified.";
215  break;
216  }
217 }
218 
219 void SearchJob::addSearchCriteria( SearchCriteria criteria, int argument )
220 {
221  Q_D( SearchJob );
222  switch ( criteria ) {
223  case Larger:
224  case Smaller:
225  d->criterias.append( d->criteriaMap[criteria] + ' ' + QByteArray::number( argument ) );
226  break;
227  default:
228  //TODO Discuss if we keep error checking here, or accept anything, even if it is wrong
229  kDebug() << "Criteria " << d->criteriaMap[criteria] << " doesn't accept an integer as an argument.";
230  break;
231  }
232 }
233 
234 void SearchJob::addSearchCriteria( SearchCriteria criteria, const QByteArray &argument )
235 {
236  Q_D( SearchJob );
237  switch ( criteria ) {
238  case BCC:
239  case Body:
240  case CC:
241  case From:
242  case Subject:
243  case Text:
244  case To:
245  d->contents.append( argument );
246  d->criterias.append( d->criteriaMap[criteria] + " {" + QByteArray::number( argument.size() ) + '}' );
247  break;
248  case Keyword:
249  case Unkeyword:
250  case Header:
251  case Uid:
252  d->criterias.append( d->criteriaMap[criteria] + ' ' + argument );
253  break;
254  default:
255  //TODO Discuss if we keep error checking here, or accept anything, even if it is wrong
256  kDebug() << "Criteria " << d->criteriaMap[criteria] << " doesn't accept any argument.";
257  break;
258  }
259 }
260 
261 void SearchJob::addSearchCriteria( SearchCriteria criteria, const QDate &argument )
262 {
263  Q_D( SearchJob );
264  switch ( criteria ) {
265  case Before:
266  case On:
267  case SentBefore:
268  case SentSince:
269  case Since: {
270  QByteArray date = QByteArray::number( argument.day() ) + '-';
271  date += d->months[argument.month()] + '-';
272  date += QByteArray::number( argument.year() );
273  d->criterias.append( d->criteriaMap[criteria] + " \"" + date + '\"' );
274  break;
275  }
276  default:
277  //TODO Discuss if we keep error checking here, or accept anything, even if it is wrong
278  kDebug() << "Criteria " << d->criteriaMap[criteria] << " doesn't accept a date as argument.";
279  break;
280  }
281 }
282 
283 void SearchJob::addSearchCriteria( const QByteArray &searchCriteria )
284 {
285  Q_D( SearchJob );
286  d->criterias.append( searchCriteria );
287 }
288 
289 void SearchJob::setUidBased(bool uidBased)
290 {
291  Q_D( SearchJob );
292  d->uidBased = uidBased;
293 }
294 
295 bool SearchJob::isUidBased() const
296 {
297  Q_D( const SearchJob );
298  return d->uidBased;
299 }
300 
301 QList<qint64> SearchJob::results() const
302 {
303  Q_D( const SearchJob );
304  return d->results;
305 }
306 
307 QList<int> SearchJob::foundItems()
308 {
309  Q_D( const SearchJob );
310 
311  QList<int> results;
312  qCopy( d->results.begin(), d->results.end(), results.begin() );
313 
314  return results;
315 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:08 by doxygen 1.8.7 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 API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



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