00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "collection.h"
00021 #include "collection_p.h"
00022
00023 #include "attributefactory.h"
00024 #include "cachepolicy.h"
00025 #include "collectionrightsattribute_p.h"
00026 #include "collectionstatistics.h"
00027 #include "entity_p.h"
00028
00029 #include <QtCore/QDebug>
00030 #include <QtCore/QHash>
00031 #include <QtCore/QString>
00032 #include <QtCore/QStringList>
00033
00034 #include <KUrl>
00035 #include <KGlobal>
00036
00037 using namespace Akonadi;
00038
00039 class CollectionRoot : public Collection
00040 {
00041 public:
00042 CollectionRoot()
00043 : Collection( 0 )
00044 {
00045 QStringList types;
00046 types << Collection::mimeType();
00047 setContentMimeTypes( types );
00048
00049
00050 Collection::Rights rights;
00051 rights |= Collection::ReadOnly;
00052 setRights( rights );
00053 }
00054 };
00055
00056 K_GLOBAL_STATIC( CollectionRoot, s_root )
00057
00058 Collection::Collection() :
00059 Entity( new CollectionPrivate )
00060 {
00061 Q_D( Collection );
00062 static int lastId = -1;
00063 d->mId = lastId--;
00064 }
00065
00066 Collection::Collection( Id id ) :
00067 Entity( new CollectionPrivate( id ) )
00068 {
00069 }
00070
00071 Collection::Collection(const Collection & other) :
00072 Entity( other )
00073 {
00074 }
00075
00076 Collection::~Collection()
00077 {
00078 }
00079
00080 QString Collection::name( ) const
00081 {
00082 return d_func()->name;
00083 }
00084
00085 void Collection::setName( const QString & name )
00086 {
00087 Q_D( Collection );
00088 d->name = name;
00089 }
00090
00091 Collection::Rights Collection::rights() const
00092 {
00093 CollectionRightsAttribute *attr = attribute<CollectionRightsAttribute>();
00094 if ( attr )
00095 return attr->rights();
00096 else
00097 return AllRights;
00098 }
00099
00100 void Collection::setRights( Rights rights )
00101 {
00102 CollectionRightsAttribute *attr = attribute<CollectionRightsAttribute>( AddIfMissing );
00103 attr->setRights( rights );
00104 }
00105
00106 QStringList Collection::contentMimeTypes() const
00107 {
00108 return d_func()->contentTypes;
00109 }
00110
00111 void Collection::setContentMimeTypes( const QStringList & types )
00112 {
00113 Q_D( Collection );
00114 if ( d->contentTypes != types ) {
00115 d->contentTypes = types;
00116 d->contentTypesChanged = true;
00117 }
00118 }
00119
00120 Collection::Id Collection::parent() const
00121 {
00122 return parentCollection().id();
00123 }
00124
00125 void Collection::setParent( Id parent )
00126 {
00127 parentCollection().setId( parent );
00128 }
00129
00130 void Collection::setParent(const Collection & collection)
00131 {
00132 setParentCollection( collection );
00133 }
00134
00135 QString Collection::parentRemoteId() const
00136 {
00137 return parentCollection().remoteId();
00138 }
00139
00140 void Collection::setParentRemoteId(const QString & remoteParent)
00141 {
00142 parentCollection().setRemoteId( remoteParent );
00143 }
00144
00145 KUrl Collection::url() const
00146 {
00147 KUrl url;
00148 url.setProtocol( QString::fromLatin1( "akonadi" ) );
00149 url.addQueryItem( QLatin1String( "collection" ), QString::number( id() ) );
00150 return url;
00151 }
00152
00153 Collection Collection::fromUrl( const KUrl &url )
00154 {
00155 if ( url.protocol() != QLatin1String( "akonadi" ) )
00156 return Collection();
00157
00158 const QString colStr = url.queryItem( QLatin1String( "collection" ) );
00159 bool ok = false;
00160 Collection::Id colId = colStr.toLongLong( &ok );
00161 if ( !ok )
00162 return Collection();
00163
00164 if ( colId == 0 )
00165 return Collection::root();
00166
00167 return Collection( colId );
00168 }
00169
00170 Collection Collection::root()
00171 {
00172 return *s_root;
00173 }
00174
00175 QString Collection::mimeType( )
00176 {
00177 return QString::fromLatin1( "inode/directory" );
00178 }
00179
00180 QString Collection::resource() const
00181 {
00182 return d_func()->resource;
00183 }
00184
00185 void Collection::setResource(const QString & resource)
00186 {
00187 Q_D( Collection );
00188 d->resource = resource;
00189 }
00190
00191 uint qHash( const Akonadi::Collection &collection )
00192 {
00193 return qHash( collection.id() );
00194 }
00195
00196 QDebug operator <<( QDebug d, const Akonadi::Collection &collection )
00197 {
00198 return d << "Collection ID:" << collection.id()
00199 << " remote ID:" << collection.remoteId() << endl
00200 << " name:" << collection.name() << endl
00201 << " url:" << collection.url() << endl
00202 << " parent:" << collection.parentCollection().id() << collection.parentCollection().remoteId() << endl
00203 << " resource:" << collection.resource() << endl
00204 << " rights:" << collection.rights() << endl
00205 << " contents mime type:" << collection.contentMimeTypes() << endl
00206 << " " << collection.cachePolicy() << endl
00207 << " " << collection.statistics();
00208 }
00209
00210 CollectionStatistics Collection::statistics() const
00211 {
00212 return d_func()->statistics;
00213 }
00214
00215 void Collection::setStatistics(const CollectionStatistics & statistics)
00216 {
00217 Q_D( Collection );
00218 d->statistics = statistics;
00219 }
00220
00221 CachePolicy Collection::cachePolicy() const
00222 {
00223 return d_func()->cachePolicy;
00224 }
00225
00226 void Collection::setCachePolicy(const CachePolicy & cachePolicy)
00227 {
00228 Q_D( Collection );
00229 d->cachePolicy = cachePolicy;
00230 d->cachePolicyChanged = true;
00231 }
00232
00233 bool Collection::isVirtual() const
00234 {
00235
00236 return ( (resource() == QLatin1String( "akonadi_search_resource" ) || resource() == QLatin1String( "akonadi_nepomuktag_resource" ) ) );
00237 }
00238
00239 AKONADI_DEFINE_PRIVATE( Akonadi::Collection )