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

KIMAP Library

  • kimap
getmetadatajob.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 "getmetadatajob.h"
21 
22 #include <KDE/KLocale>
23 #include <KDE/KDebug>
24 
25 #include "metadatajobbase_p.h"
26 #include "message_p.h"
27 #include "session_p.h"
28 #include "rfccodecs.h"
29 
30 namespace KIMAP
31 {
32  class GetMetaDataJobPrivate : public MetaDataJobBasePrivate
33  {
34  public:
35  GetMetaDataJobPrivate( Session *session, const QString& name ) : MetaDataJobBasePrivate(session, name), maxSize(-1), depth("0") { }
36  ~GetMetaDataJobPrivate() { }
37 
38  qint64 maxSize;
39  QByteArray depth;
40  QList<QByteArray> entries;
41  QList<QByteArray> attributes;
42  QMap<QString, QMap<QByteArray, QMap<QByteArray, QByteArray> > > metadata;
43  // ^ mailbox ^ entry ^attribute ^ value
44  };
45 }
46 
47 using namespace KIMAP;
48 
49 GetMetaDataJob::GetMetaDataJob( Session *session )
50  : MetaDataJobBase( *new GetMetaDataJobPrivate(session, i18n("GetMetaData")) )
51 {
52 }
53 
54 GetMetaDataJob::~GetMetaDataJob()
55 {
56 }
57 
58 void GetMetaDataJob::doStart()
59 {
60  Q_D(GetMetaDataJob);
61  QByteArray parameters;
62  parameters = '\"' + KIMAP::encodeImapFolderName( d->mailBox.toUtf8() ) + "\" ";
63 
64  QByteArray command = "GETMETADATA";
65  if (d->serverCapability == Annotatemore) {
66  d->m_name = i18n("GetAnnotation");
67  command = "GETANNOTATION";
68  if (d->entries.size() > 1)
69  parameters += '(';
70  Q_FOREACH(const QByteArray &entry, d->entries) {
71  parameters += '\"' + entry + "\" ";
72  }
73  if (d->entries.size() > 1)
74  parameters[parameters.length() -1 ] = ')';
75  else
76  parameters.truncate(parameters.length() -1);
77 
78  parameters += ' ';
79 
80  if (d->attributes.size() > 1)
81  parameters += '(';
82  Q_FOREACH(const QByteArray &attribute, d->attributes) {
83  parameters += '\"' + attribute + "\" ";
84  }
85  if (d->attributes.size() > 1)
86  parameters[parameters.length() -1 ] = ')';
87  else
88  parameters.truncate(parameters.length() -1);
89 
90  } else {
91  if (d->depth != "0") {
92  parameters += "(DEPTH " + d->depth;
93  }
94  if (d->maxSize != -1) {
95  parameters += "(MAXSIZE " + QByteArray::number(d->maxSize) + ')';
96  }
97  if (d->depth != "0") {
98  parameters += " )";
99  }
100 
101  if (d->entries.size() > 1)
102  parameters += '(';
103  Q_FOREACH(const QByteArray &entry, d->entries) {
104  parameters += '\"' + entry + "\" ";
105  }
106  if (d->entries.size() > 1)
107  parameters[parameters.length() -1 ] = ')';
108  }
109 
110  if (d->entries.isEmpty()) {
111  parameters += ')';
112  }
113 
114  d->tags << d->sessionInternal()->sendCommand( command, parameters );
115 // kDebug() << "SENT: " << command << " " << parameters;
116 }
117 
118 void GetMetaDataJob::handleResponse( const Message &response )
119 {
120  Q_D(GetMetaDataJob);
121 // kDebug() << "GOT: " << response.toString();
122 
123  //TODO: handle NO error messages having [METADATA MAXSIZE NNN], [METADATA TOOMANY], [METADATA NOPRIVATE] (see rfc5464)
124  // or [ANNOTATEMORE TOOBIG], [ANNOTATEMORE TOOMANY] respectively
125  if (handleErrorReplies(response) == NotHandled ) {
126  if ( response.content.size() >= 4 ) {
127  if (d->serverCapability == Annotatemore && response.content[1].toString() == "ANNOTATION" ) {
128  QString mailBox = QString::fromUtf8( KIMAP::decodeImapFolderName( response.content[2].toString() ) );
129 
130  int i = 3;
131  while (i < response.content.size() - 1) {
132  QByteArray entry = response.content[i].toString();
133  QList<QByteArray> attributes = response.content[i + 1].toList();
134  int j = 0;
135  while ( j < attributes.size() - 1) {
136  d->metadata[mailBox][entry][attributes[j]] = attributes[j + 1];
137  j += 2;
138  }
139  i += 2;
140  }
141  } else
142  if (d->serverCapability == Metadata && response.content[1].toString() == "METADATA" ) {
143  QString mailBox = QString::fromUtf8( KIMAP::decodeImapFolderName( response.content[2].toString() ) );
144 
145  QList<QByteArray> entries = response.content[3].toList();
146  int i = 0;
147  while ( i < entries.size() - 1) {
148  d->metadata[mailBox][entries[i]][""] = entries[i + 1];
149  i += 2;
150  }
151  }
152  }
153  }
154 }
155 
156 void GetMetaDataJob::addEntry(const QByteArray &entry, const QByteArray &attribute)
157 {
158  Q_D(GetMetaDataJob);
159  if (d->serverCapability == Annotatemore && attribute.isNull())
160  qWarning() << "In ANNOTATEMORE mode an attribute must be specified with addEntry!";
161  d->entries.append(entry);
162  d->attributes.append(attribute);
163 }
164 
165 void GetMetaDataJob::setMaximumSize(qint64 size)
166 {
167  Q_D(GetMetaDataJob);
168  d->maxSize = size;
169 }
170 
171 void GetMetaDataJob::setDepth(Depth depth)
172 {
173  Q_D(GetMetaDataJob);
174 
175  switch (depth)
176  {
177  case OneLevel:
178  d->depth = "1"; //krazy:exclude=doublequote_chars
179  break;
180  case AllLevels:
181  d->depth = "infinity";
182  break;
183  default:
184  d->depth = "0"; //krazy:exclude=doublequote_chars
185  }
186 }
187 
188 QByteArray GetMetaDataJob::metaData(const QString &mailBox, const QByteArray &entry, const QByteArray &attribute) const
189 {
190  Q_D(const GetMetaDataJob);
191  QByteArray attr = attribute;
192 
193  if (d->serverCapability == Metadata)
194  attr = "";
195 
196  QByteArray result;
197  if (d->metadata.contains(mailBox)) {
198  if (d->metadata[mailBox].contains(entry)) {
199  result = d->metadata[mailBox][entry].value(attr);
200  }
201  }
202 
203  return result;
204 }
205 
206 QMap<QByteArray, QMap<QByteArray, QByteArray> > GetMetaDataJob::allMetaData(const QString &mailBox) const
207 {
208  Q_D(const GetMetaDataJob);
209  return d->metadata[mailBox];
210 }
211 
212 #include "getmetadatajob.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue Dec 4 2012 14:35:07 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