00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "item.h"
00021 #include "item_p.h"
00022 #include "itemserializer_p.h"
00023 #include "protocol_p.h"
00024
00025 #include <kurl.h>
00026
00027 #include <QtCore/QStringList>
00028
00029 using namespace Akonadi;
00030
00031
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 if ( d->mDeletedFlags.contains( name ) )
00070 d->mDeletedFlags.remove( name );
00071 else
00072 d->mAddedFlags.insert( name );
00073 }
00074 }
00075
00076 void Item::clearFlag( const QByteArray & name )
00077 {
00078 Q_D( Item );
00079 d->mFlags.remove( name );
00080 if ( !d->mFlagsOverwritten ) {
00081 if ( d->mAddedFlags.contains( name ) )
00082 d->mAddedFlags.remove( name );
00083 else
00084 d->mDeletedFlags.insert( name );
00085 }
00086 }
00087
00088 void Item::setFlags( const Flags &flags )
00089 {
00090 Q_D( Item );
00091 d->mFlags = flags;
00092 d->mFlagsOverwritten = true;
00093 }
00094
00095 void Item::clearFlags()
00096 {
00097 Q_D( Item );
00098 d->mFlags.clear();
00099 d->mFlagsOverwritten = true;
00100 }
00101
00102 QDateTime Item::modificationTime() const
00103 {
00104 return d_func()->mModificationTime;
00105 }
00106
00107 void Item::setModificationTime( const QDateTime &datetime )
00108 {
00109 d_func()->mModificationTime = datetime;
00110 }
00111
00112 bool Item::hasFlag( const QByteArray & name ) const
00113 {
00114 return d_func()->mFlags.contains( name );
00115 }
00116
00117 QSet<QByteArray> Item::loadedPayloadParts() const
00118 {
00119 return ItemSerializer::parts( *this );
00120 }
00121
00122 QByteArray Item::payloadData() const
00123 {
00124 int version = 0;
00125 QByteArray data;
00126 ItemSerializer::serialize( *this, FullPayload, data, version );
00127 return data;
00128 }
00129
00130 void Item::setPayloadFromData( const QByteArray &data )
00131 {
00132 ItemSerializer::deserialize( *this, FullPayload, data, 0, false );
00133 }
00134
00135 void Item::clearPayload()
00136 {
00137 d_func()->mClearPayload = true;
00138 }
00139
00140 int Item::revision() const
00141 {
00142 return d_func()->mRevision;
00143 }
00144
00145 void Item::setRevision( int rev )
00146 {
00147 d_func()->mRevision = rev;
00148 }
00149
00150 Entity::Id Item::storageCollectionId() const
00151 {
00152 return d_func()->mCollectionId;
00153 }
00154
00155 void Item::setStorageCollectionId( Entity::Id collectionId )
00156 {
00157 d_func()->mCollectionId = collectionId;
00158 }
00159
00160 QString Item::mimeType() const
00161 {
00162 return d_func()->mMimeType;
00163 }
00164
00165 void Item::setSize( qint64 size )
00166 {
00167 Q_D( Item );
00168 d->mSize = size;
00169 d->mSizeChanged = true;
00170 }
00171
00172 qint64 Item::size() const
00173 {
00174 return d_func()->mSize;
00175 }
00176
00177 void Item::setMimeType( const QString & mimeType )
00178 {
00179 d_func()->mMimeType = mimeType;
00180 }
00181
00182 bool Item::hasPayload() const
00183 {
00184 return d_func()->mPayload != 0;
00185 }
00186
00187 KUrl Item::url( UrlType type ) const
00188 {
00189 KUrl url;
00190 url.setProtocol( QString::fromLatin1( "akonadi" ) );
00191 url.addQueryItem( QLatin1String( "item" ), QString::number( id() ) );
00192
00193 if ( type == UrlWithMimeType )
00194 url.addQueryItem( QLatin1String( "type" ), mimeType() );
00195
00196 return url;
00197 }
00198
00199 Item Item::fromUrl( const KUrl &url )
00200 {
00201 if ( url.protocol() != QLatin1String( "akonadi" ) )
00202 return Item();
00203
00204 const QString itemStr = url.queryItem( QLatin1String( "item" ) );
00205 bool ok = false;
00206 Item::Id itemId = itemStr.toLongLong( &ok );
00207 if ( !ok )
00208 return Item();
00209
00210 return Item( itemId );
00211 }
00212
00213 PayloadBase* Item::payloadBase() const
00214 {
00215 return d_func()->mPayload;
00216 }
00217
00218 void Item::setPayloadBase( PayloadBase* p )
00219 {
00220 Q_D( Item );
00221 delete d->mPayload;
00222 d->mPayload = p;
00223 }
00224
00225 QSet<QByteArray> Item::availablePayloadParts() const
00226 {
00227 return ItemSerializer::availableParts( *this );
00228 }
00229
00230 void Item::apply( const Item &other )
00231 {
00232 Q_ASSERT( mimeType() == other.mimeType() && id() == other.id() );
00233 setRemoteId( other.remoteId() );
00234 setRevision( other.revision() );
00235 setFlags( other.flags() );
00236 setModificationTime( other.modificationTime() );
00237 setSize( other.size() );
00238 setParentCollection( other.parentCollection() );
00239 setStorageCollectionId( other.storageCollectionId() );
00240 setRemoteId( other.remoteId() );
00241
00242 QList<QByteArray> attrs;
00243 foreach ( Attribute *attribute, other.attributes() )
00244 {
00245 addAttribute( attribute->clone() );
00246 attrs.append( attribute->type() );
00247 }
00248
00249 QMutableHashIterator<QByteArray, Attribute*> it( d_ptr->mAttributes );
00250 while ( it.hasNext() ) {
00251 it.next();
00252 if ( !attrs.contains( it.key() ) ) {
00253 delete it.value();
00254 it.remove();
00255 }
00256 }
00257
00258 ItemSerializer::apply( *this, other );
00259 }
00260
00261 AKONADI_DEFINE_PRIVATE( Item )