• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 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  }
71  Q_FOREACH ( const QByteArray &entry, d->entries ) {
72  parameters += '\"' + entry + "\" ";
73  }
74  if ( d->entries.size() > 1 ) {
75  parameters[parameters.length() - 1 ] = ')';
76  } else {
77  parameters.truncate( parameters.length() - 1 );
78  }
79 
80  parameters += ' ';
81 
82  if ( d->attributes.size() > 1 ) {
83  parameters += '(';
84  }
85  Q_FOREACH ( const QByteArray &attribute, d->attributes ) {
86  parameters += '\"' + attribute + "\" ";
87  }
88  if ( d->attributes.size() > 1 ) {
89  parameters[parameters.length() - 1 ] = ')';
90  } else {
91  parameters.truncate( parameters.length() - 1 );
92  }
93 
94  } else {
95  if ( d->depth != "0" ) {
96  parameters += "(DEPTH " + d->depth;
97  }
98  if ( d->maxSize != -1 ) {
99  parameters += "(MAXSIZE " + QByteArray::number( d->maxSize ) + ')';
100  }
101  if ( d->depth != "0" ) {
102  parameters += " )";
103  }
104 
105  if ( d->entries.size() > 1 ) {
106  parameters += '(';
107  }
108  Q_FOREACH ( const QByteArray &entry, d->entries ) {
109  parameters += '\"' + entry + "\" ";
110  }
111  if ( d->entries.size() > 1 ) {
112  parameters[parameters.length() - 1 ] = ')';
113  }
114  }
115 
116  if ( d->entries.isEmpty() ) { {
117  parameters += ')';
118  }
119  }
120 
121  d->tags << d->sessionInternal()->sendCommand( command, parameters );
122 // kDebug() << "SENT: " << command << " " << parameters;
123 }
124 
125 void GetMetaDataJob::handleResponse( const Message &response )
126 {
127  Q_D( GetMetaDataJob );
128 // kDebug() << "GOT: " << response.toString();
129 
130  //TODO: handle NO error messages having [METADATA MAXSIZE NNN], [METADATA TOOMANY], [METADATA NOPRIVATE] (see rfc5464)
131  // or [ANNOTATEMORE TOOBIG], [ANNOTATEMORE TOOMANY] respectively
132  if ( handleErrorReplies( response ) == NotHandled ) {
133  if ( response.content.size() >= 4 ) {
134  if ( d->serverCapability == Annotatemore && response.content[1].toString() == "ANNOTATION" ) {
135  QString mailBox = QString::fromUtf8( KIMAP::decodeImapFolderName( response.content[2].toString() ) );
136 
137  int i = 3;
138  while ( i < response.content.size() - 1 ) {
139  QByteArray entry = response.content[i].toString();
140  QList<QByteArray> attributes = response.content[i + 1].toList();
141  int j = 0;
142  while ( j < attributes.size() - 1 ) {
143  d->metadata[mailBox][entry][attributes[j]] = attributes[j + 1];
144  j += 2;
145  }
146  i += 2;
147  }
148  } else if ( d->serverCapability == Metadata && response.content[1].toString() == "METADATA" ) {
149  QString mailBox = QString::fromUtf8( KIMAP::decodeImapFolderName( response.content[2].toString() ) );
150 
151  QList<QByteArray> entries = response.content[3].toList();
152  int i = 0;
153  while ( i < entries.size() - 1 ) {
154  d->metadata[mailBox][entries[i]][""] = entries[i + 1];
155  i += 2;
156  }
157  }
158  }
159  }
160 }
161 
162 void GetMetaDataJob::addEntry(const QByteArray &entry, const QByteArray &attribute)
163 {
164  Q_D( GetMetaDataJob );
165  if ( d->serverCapability == Annotatemore && attribute.isNull() ) {
166  qWarning() << "In ANNOTATEMORE mode an attribute must be specified with addEntry!";
167  }
168  d->entries.append( entry );
169  d->attributes.append( attribute );
170 }
171 
172 void GetMetaDataJob::setMaximumSize(qint64 size)
173 {
174  Q_D( GetMetaDataJob );
175  d->maxSize = size;
176 }
177 
178 void GetMetaDataJob::setDepth(Depth depth)
179 {
180  Q_D( GetMetaDataJob );
181 
182  switch ( depth ) {
183  case OneLevel:
184  d->depth = "1"; //krazy:exclude=doublequote_chars
185  break;
186  case AllLevels:
187  d->depth = "infinity";
188  break;
189  default:
190  d->depth = "0"; //krazy:exclude=doublequote_chars
191  }
192 }
193 
194 QByteArray GetMetaDataJob::metaData(const QString &mailBox, const QByteArray &entry, const QByteArray &attribute) const
195 {
196  Q_D( const GetMetaDataJob );
197  QByteArray attr = attribute;
198 
199  if ( d->serverCapability == Metadata ) {
200  attr = "";
201  }
202 
203  QByteArray result;
204  if ( d->metadata.contains( mailBox ) ) {
205  if ( d->metadata[mailBox].contains( entry ) ) {
206  result = d->metadata[mailBox][entry].value( attr );
207  }
208  }
209  return result;
210 }
211 
212 QMap<QByteArray, QMap<QByteArray, QByteArray> > GetMetaDataJob::allMetaData(const QString &mailBox) const
213 {
214  Q_D( const GetMetaDataJob );
215  return d->metadata[mailBox];
216 }
217 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:25:16 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