akonadi
protocolhelper.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "protocolhelper.h"
00021
00022 #include "attributefactory.h"
00023 #include "imapparser_p.h"
00024
00025 #include <QtCore/QVarLengthArray>
00026
00027 #include <kdebug.h>
00028
00029 using namespace Akonadi;
00030
00031 int ProtocolHelper::parseCachePolicy(const QByteArray & data, CachePolicy & policy, int start)
00032 {
00033 QVarLengthArray<QByteArray,16> params;
00034 int end = Akonadi::ImapParser::parseParenthesizedList( data, params, start );
00035 for ( int i = 0; i < params.count() - 1; i += 2 ) {
00036 const QByteArray key = params[i];
00037 const QByteArray value = params[i + 1];
00038
00039 if ( key == "INHERIT" )
00040 policy.setInheritFromParent( value == "true" );
00041 else if ( key == "INTERVAL" )
00042 policy.setIntervalCheckTime( value.toInt() );
00043 else if ( key == "CACHETIMEOUT" )
00044 policy.setCacheTimeout( value.toInt() );
00045 else if ( key == "SYNCONDEMAND" )
00046 policy.setSyncOnDemand( value == "true" );
00047 else if ( key == "LOCALPARTS" ) {
00048 QVarLengthArray<QByteArray,16> tmp;
00049 QStringList parts;
00050 Akonadi::ImapParser::parseParenthesizedList( value, tmp );
00051 for ( int j=0; j<tmp.size(); j++ )
00052 parts << QString::fromLatin1( tmp[j] );
00053 policy.setLocalParts( parts );
00054 }
00055 }
00056 return end;
00057 }
00058
00059 QByteArray ProtocolHelper::cachePolicyToByteArray(const CachePolicy & policy)
00060 {
00061 QByteArray rv = "CACHEPOLICY (";
00062 if ( policy.inheritFromParent() ) {
00063 rv += "INHERIT true";
00064 } else {
00065 rv += "INHERIT false";
00066 rv += " INTERVAL " + QByteArray::number( policy.intervalCheckTime() );
00067 rv += " CACHETIMEOUT " + QByteArray::number( policy.cacheTimeout() );
00068 rv += " SYNCONDEMAND " + ( policy.syncOnDemand() ? QByteArray("true") : QByteArray("false") );
00069 rv += " LOCALPARTS (" + policy.localParts().join( QLatin1String(" ") ).toLatin1() + ')';
00070 }
00071 rv += ')';
00072 return rv;
00073 }
00074
00075 int ProtocolHelper::parseCollection(const QByteArray & data, Collection & collection, int start)
00076 {
00077 int pos = start;
00078
00079
00080 Collection::Id colId = -1;
00081 bool ok = false;
00082 pos = ImapParser::parseNumber( data, colId, &ok, pos );
00083 if ( !ok || colId <= 0 ) {
00084 kDebug( 5250 ) << "Could not parse collection id from response:" << data;
00085 return start;
00086 }
00087
00088 Collection::Id parentId = -1;
00089 pos = ImapParser::parseNumber( data, parentId, &ok, pos );
00090 if ( !ok || parentId < 0 ) {
00091 kDebug( 5250 ) << "Could not parse parent id from response:" << data;
00092 return start;
00093 }
00094
00095 collection = Collection( colId );
00096 collection.setParent( parentId );
00097
00098
00099 QVarLengthArray<QByteArray,16> attributes;
00100 pos = ImapParser::parseParenthesizedList( data, attributes, pos );
00101
00102 for ( int i = 0; i < attributes.count() - 1; i += 2 ) {
00103 const QByteArray key = attributes[i];
00104 const QByteArray value = attributes[i + 1];
00105
00106 if ( key == "NAME" ) {
00107 collection.setName( QString::fromUtf8( value ) );
00108 } else if ( key == "REMOTEID" ) {
00109 collection.setRemoteId( QString::fromUtf8( value ) );
00110 } else if ( key == "RESOURCE" ) {
00111 collection.setResource( QString::fromUtf8( value ) );
00112 } else if ( key == "MIMETYPE" ) {
00113 QVarLengthArray<QByteArray,16> ct;
00114 ImapParser::parseParenthesizedList( value, ct );
00115 QStringList ct2;
00116 for ( int j = 0; j < ct.size(); j++ )
00117 ct2 << QString::fromLatin1( ct[j] );
00118 collection.setContentMimeTypes( ct2 );
00119 } else if ( key == "CACHEPOLICY" ) {
00120 CachePolicy policy;
00121 ProtocolHelper::parseCachePolicy( value, policy );
00122 collection.setCachePolicy( policy );
00123 } else {
00124 Attribute* attr = AttributeFactory::createAttribute( key );
00125 Q_ASSERT( attr );
00126 attr->deserialize( value );
00127 collection.addAttribute( attr );
00128 }
00129 }
00130
00131 return pos;
00132 }
00133
00134 QByteArray ProtocolHelper::attributesToByteArray(const Entity & entity, bool ns )
00135 {
00136 QList<QByteArray> l;
00137 foreach ( const Attribute *attr, entity.attributes() ) {
00138 l << encodePartIdentifier( ns ? PartAttribute : PartGlobal, attr->type() );
00139 l << ImapParser::quote( attr->serialized() );
00140 }
00141 return ImapParser::join( l, " " );
00142 }
00143
00144 QByteArray ProtocolHelper::encodePartIdentifier(PartNamespace ns, const QByteArray & label, int version )
00145 {
00146 const QByteArray versionString( version != 0 ? '[' + QByteArray::number( version ) + ']' : "" );
00147 switch ( ns ) {
00148 case PartGlobal:
00149 return label + versionString;
00150 case PartPayload:
00151 return "PLD:" + label + versionString;
00152 case PartAttribute:
00153 return "ATR:" + label + versionString;
00154 default:
00155 Q_ASSERT( false );
00156 }
00157 return QByteArray();
00158 }
00159
00160 QByteArray ProtocolHelper::decodePartIdentifier( const QByteArray &data, PartNamespace & ns )
00161 {
00162 if ( data.startsWith( "PLD:" ) ) {
00163 ns = PartPayload;
00164 return data.mid( 4 );
00165 } else if ( data.startsWith( "ATR:" ) ) {
00166 ns = PartAttribute;
00167 return data.mid( 4 );
00168 } else {
00169 ns = PartGlobal;
00170 return data;
00171 }
00172 }