akonadi
item.cpp
00001 /* 00002 Copyright (c) 2006 Volker Krause <vkrause@kde.org> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 00018 */ 00019 00020 #include "item.h" 00021 #include "item_p.h" 00022 #include "itemserializer.h" 00023 #include "protocol_p.h" 00024 00025 #include <kurl.h> 00026 00027 #include <QtCore/QStringList> 00028 00029 using namespace Akonadi; 00030 00031 // Change to something != RFC822 as soon as the server supports it 00032 const char* Item::FullPayload = "RFC822"; 00033 00034 Item::Item() 00035 : Entity( new ItemPrivate ) 00036 { 00037 } 00038 00039 Item::Item( Id id ) 00040 : Entity( new ItemPrivate( id ) ) 00041 { 00042 } 00043 00044 Item::Item( const QString & mimeType ) 00045 : Entity( new ItemPrivate ) 00046 { 00047 d_func()->mMimeType = mimeType; 00048 } 00049 00050 Item::Item( const Item &other ) 00051 : Entity( other ) 00052 { 00053 } 00054 00055 Item::~Item() 00056 { 00057 } 00058 00059 Item::Flags Item::flags() const 00060 { 00061 return d_func()->mFlags; 00062 } 00063 00064 void Item::setFlag( const QByteArray & name ) 00065 { 00066 Q_D( Item ); 00067 d->mFlags.insert( name ); 00068 if ( !d->mFlagsOverwritten ) 00069 d->mAddedFlags.insert( name ); 00070 } 00071 00072 void Item::clearFlag( const QByteArray & name ) 00073 { 00074 Q_D( Item ); 00075 d->mFlags.remove( name ); 00076 if ( !d->mFlagsOverwritten ) 00077 d->mDeletedFlags.insert( name ); 00078 } 00079 00080 void Item::setFlags( const Flags &flags ) 00081 { 00082 Q_D( Item ); 00083 d->mFlags = flags; 00084 d->mFlagsOverwritten = true; 00085 } 00086 00087 void Item::clearFlags() 00088 { 00089 Q_D( Item ); 00090 d->mFlags.clear(); 00091 d->mFlagsOverwritten = true; 00092 } 00093 00094 QDateTime Item::modificationTime() const 00095 { 00096 return d_func()->mModificationTime; 00097 } 00098 00099 void Item::setModificationTime( const QDateTime &datetime ) 00100 { 00101 d_func()->mModificationTime = datetime; 00102 } 00103 00104 bool Item::hasFlag( const QByteArray & name ) const 00105 { 00106 return d_func()->mFlags.contains( name ); 00107 } 00108 00109 QSet<QByteArray> Item::loadedPayloadParts() const 00110 { 00111 return ItemSerializer::parts( *this ); 00112 } 00113 00114 QByteArray Item::payloadData() const 00115 { 00116 int version = 0; 00117 QByteArray data; 00118 ItemSerializer::serialize( *this, FullPayload, data, version ); 00119 return data; 00120 } 00121 00122 void Item::setPayloadFromData( const QByteArray &data ) 00123 { 00124 ItemSerializer::deserialize( *this, FullPayload, data, 0 ); 00125 } 00126 00127 int Item::revision() const 00128 { 00129 return d_func()->mRevision; 00130 } 00131 00132 void Item::setRevision( int rev ) 00133 { 00134 d_func()->mRevision = rev; 00135 } 00136 00137 QString Item::mimeType() const 00138 { 00139 return d_func()->mMimeType; 00140 } 00141 00142 void Item::setSize( qint64 size ) 00143 { 00144 d_func()->mSize = size; 00145 } 00146 00147 qint64 Item::size() const 00148 { 00149 return d_func()->mSize; 00150 } 00151 00152 void Item::setMimeType( const QString & mimeType ) 00153 { 00154 d_func()->mMimeType = mimeType; 00155 } 00156 00157 bool Item::hasPayload() const 00158 { 00159 return d_func()->mPayload != 0; 00160 } 00161 00162 KUrl Item::url( UrlType type ) const 00163 { 00164 KUrl url; 00165 url.setProtocol( QString::fromLatin1("akonadi") ); 00166 url.addQueryItem( QLatin1String( "item" ), QString::number( id() ) ); 00167 00168 if ( type == UrlWithMimeType ) 00169 url.addQueryItem( QLatin1String( "type" ), mimeType() ); 00170 00171 return url; 00172 } 00173 00174 Item Item::fromUrl( const KUrl &url ) 00175 { 00176 if ( url.protocol() != QLatin1String( "akonadi" ) ) 00177 return Item(); 00178 00179 const QString itemStr = url.queryItem( QLatin1String( "item" ) ); 00180 bool ok = false; 00181 Item::Id itemId = itemStr.toLongLong( &ok ); 00182 if ( !ok ) 00183 return Item(); 00184 00185 return Item( itemId ); 00186 } 00187 00188 PayloadBase* Item::payloadBase() const 00189 { 00190 return d_func()->mPayload; 00191 } 00192 00193 void Item::setPayloadBase( PayloadBase* p ) 00194 { 00195 Q_D( Item ); 00196 delete d->mPayload; 00197 d->mPayload = p; 00198 } 00199 00200 AKONADI_DEFINE_PRIVATE( Item )