• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.4 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 ) searchKey+= ' ';
136  searchKey += key;
137  }
138  } else {
139  for ( int i = 0; i<d->criterias.size(); i++ ) {
140  const QByteArray key = d->criterias.at( i );
141  if ( i>0 ) searchKey+= ' ';
142  searchKey += '(' + key + ')';
143  }
144  }
145 
146  QByteArray command = "SEARCH";
147  if ( d->uidBased ) {
148  command = "UID "+ command;
149  }
150 
151  d->tags << d->sessionInternal()->sendCommand( command, searchKey );
152 }
153 
154 void SearchJob::handleResponse( const Message &response )
155 {
156  Q_D(SearchJob);
157 
158  if (handleErrorReplies(response) == NotHandled ) {
159  if ( response.content[0].toString() == "+" ) {
160  d->sessionInternal()->sendData( d->contents[d->nextContent] );
161  d->nextContent++;
162  } else if ( response.content[1].toString() == "SEARCH" ) {
163  for(int i = 2; i < response.content.size(); i++) {
164  d->results.append(response.content[i].toString().toInt());
165  }
166  }
167  }
168 }
169 
170 
171 void SearchJob::setCharset( const QByteArray &charset )
172 {
173  Q_D(SearchJob);
174  d->charset = charset;
175 }
176 
177 QByteArray SearchJob::charset() const
178 {
179  Q_D(const SearchJob);
180  return d->charset;
181 }
182 
183 void SearchJob::setSearchLogic( SearchLogic logic )
184 {
185  Q_D(SearchJob);
186  d->logic = logic;
187 }
188 
189 void SearchJob::addSearchCriteria( SearchCriteria criteria )
190 {
191  Q_D(SearchJob);
192 
193  switch (criteria) {
194  case All:
195  case Answered:
196  case Deleted:
197  case Draft:
198  case Flagged:
199  case New:
200  case Old:
201  case Recent:
202  case Seen:
203  case Unanswered:
204  case Undeleted:
205  case Undraft:
206  case Unflagged:
207  case Unseen:
208  d->criterias.append(d->criteriaMap[criteria]);
209  break;
210  default:
211  //TODO Discuss if we keep error checking here, or accept anything, even if it is wrong
212  kDebug() << "Criteria " << d->criteriaMap[criteria] << " needs an argument, but none was specified.";
213  break;
214  }
215 }
216 
217 
218 void SearchJob::addSearchCriteria( SearchCriteria criteria, int argument )
219 {
220  Q_D(SearchJob);
221  switch (criteria) {
222  case Larger:
223  case Smaller:
224  d->criterias.append(d->criteriaMap[criteria] + ' ' + QByteArray::number(argument));
225  break;
226  default:
227  //TODO Discuss if we keep error checking here, or accept anything, even if it is wrong
228  kDebug() << "Criteria " << d->criteriaMap[criteria] << " doesn't accept an integer as an argument.";
229  break;
230  }
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 }
316 
317 #include "searchjob.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Dec 10 2012 13:47:03 by doxygen 1.8.1.2 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.9.4 API Reference

Skip menu "kdepimlibs-4.9.4 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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