akonadi
collection.cpp
00001 /* 00002 Copyright (c) 2006 - 2007 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 "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 // The root collection is read-only for the users 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 return url( UrlShort ); 00148 } 00149 00150 KUrl Collection::url( UrlType type ) const 00151 { 00152 KUrl url; 00153 url.setProtocol( QString::fromLatin1( "akonadi" ) ); 00154 url.addQueryItem( QLatin1String( "collection" ), QString::number( id() ) ); 00155 00156 if ( type == UrlWithName ) 00157 url.addQueryItem( QLatin1String( "name" ), name() ); 00158 00159 return url; 00160 } 00161 00162 Collection Collection::fromUrl( const KUrl &url ) 00163 { 00164 if ( url.protocol() != QLatin1String( "akonadi" ) ) 00165 return Collection(); 00166 00167 const QString colStr = url.queryItem( QLatin1String( "collection" ) ); 00168 bool ok = false; 00169 Collection::Id colId = colStr.toLongLong( &ok ); 00170 if ( !ok ) 00171 return Collection(); 00172 00173 if ( colId == 0 ) 00174 return Collection::root(); 00175 00176 return Collection( colId ); 00177 } 00178 00179 Collection Collection::root() 00180 { 00181 return *s_root; 00182 } 00183 00184 QString Collection::mimeType( ) 00185 { 00186 return QString::fromLatin1( "inode/directory" ); 00187 } 00188 00189 QString Collection::resource() const 00190 { 00191 return d_func()->resource; 00192 } 00193 00194 void Collection::setResource(const QString & resource) 00195 { 00196 Q_D( Collection ); 00197 d->resource = resource; 00198 } 00199 00200 uint qHash( const Akonadi::Collection &collection ) 00201 { 00202 return qHash( collection.id() ); 00203 } 00204 00205 QDebug operator <<( QDebug d, const Akonadi::Collection &collection ) 00206 { 00207 return d << "Collection ID:" << collection.id() 00208 << " remote ID:" << collection.remoteId() << endl 00209 << " name:" << collection.name() << endl 00210 << " url:" << collection.url() << endl 00211 << " parent:" << collection.parentCollection().id() << collection.parentCollection().remoteId() << endl 00212 << " resource:" << collection.resource() << endl 00213 << " rights:" << collection.rights() << endl 00214 << " contents mime type:" << collection.contentMimeTypes() << endl 00215 << " " << collection.cachePolicy() << endl 00216 << " " << collection.statistics(); 00217 } 00218 00219 CollectionStatistics Collection::statistics() const 00220 { 00221 return d_func()->statistics; 00222 } 00223 00224 void Collection::setStatistics(const CollectionStatistics & statistics) 00225 { 00226 Q_D( Collection ); 00227 d->statistics = statistics; 00228 } 00229 00230 CachePolicy Collection::cachePolicy() const 00231 { 00232 return d_func()->cachePolicy; 00233 } 00234 00235 void Collection::setCachePolicy(const CachePolicy & cachePolicy) 00236 { 00237 Q_D( Collection ); 00238 d->cachePolicy = cachePolicy; 00239 d->cachePolicyChanged = true; 00240 } 00241 00242 bool Collection::isVirtual() const 00243 { 00244 // TODO make this a proper flag 00245 return ( (resource() == QLatin1String( "akonadi_search_resource" ) || resource() == QLatin1String( "akonadi_nepomuktag_resource" ) ) ); 00246 } 00247 00248 AKONADI_DEFINE_PRIVATE( Akonadi::Collection )