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 d->contentTypes = types; 00115 d->contentTypesChanged = true; 00116 } 00117 00118 Collection::Id Collection::parent() const 00119 { 00120 return d_func()->parentId; 00121 } 00122 00123 void Collection::setParent( Id parent ) 00124 { 00125 Q_D( Collection ); 00126 d->parentId = parent; 00127 } 00128 00129 void Collection::setParent(const Collection & collection) 00130 { 00131 Q_D( Collection ); 00132 d->parentId = collection.id(); 00133 d->parentRemoteId = collection.remoteId(); 00134 } 00135 00136 QString Collection::parentRemoteId() const 00137 { 00138 return d_func()->parentRemoteId; 00139 } 00140 00141 void Collection::setParentRemoteId(const QString & remoteParent) 00142 { 00143 Q_D( Collection ); 00144 d->parentRemoteId = remoteParent; 00145 } 00146 00147 KUrl Collection::url() const 00148 { 00149 KUrl url; 00150 url.setProtocol( QString::fromLatin1("akonadi") ); 00151 url.addQueryItem( QLatin1String("collection"), QString::number( id() ) ); 00152 return url; 00153 } 00154 00155 Collection Collection::fromUrl( const KUrl &url ) 00156 { 00157 if ( url.protocol() != QLatin1String( "akonadi" ) ) 00158 return Collection(); 00159 00160 const QString colStr = url.queryItem( QLatin1String( "collection" ) ); 00161 bool ok = false; 00162 Collection::Id colId = colStr.toLongLong( &ok ); 00163 if ( !ok ) 00164 return Collection(); 00165 00166 if ( colId == 0 ) 00167 return Collection::root(); 00168 00169 return Collection( colId ); 00170 } 00171 00172 Collection Collection::root() 00173 { 00174 return *s_root; 00175 } 00176 00177 QString Collection::mimeType( ) 00178 { 00179 return QString::fromLatin1("inode/directory"); 00180 } 00181 00182 QString Collection::resource() const 00183 { 00184 return d_func()->resource; 00185 } 00186 00187 void Collection::setResource(const QString & resource) 00188 { 00189 Q_D( Collection ); 00190 d->resource = resource; 00191 } 00192 00193 uint qHash( const Akonadi::Collection &collection ) 00194 { 00195 return qHash( collection.id() ); 00196 } 00197 00198 QDebug operator <<( QDebug d, const Akonadi::Collection &collection ) 00199 { 00200 return d << "Collection ID:" << collection.id() 00201 << " remote ID:" << collection.remoteId() << endl 00202 << " name:" << collection.name() << endl 00203 << " url:" << collection.url() << endl 00204 << " parent ID:" << collection.parent() 00205 << " parent remote ID: " << collection.parentRemoteId() << endl 00206 << " resource:" << collection.resource() << endl 00207 << " mime type:" << collection.mimeType() << endl 00208 << " rights:" << collection.rights() << endl 00209 << " contents mime type:" << collection.contentMimeTypes() << endl 00210 << " " << collection.cachePolicy() << endl 00211 << " " << collection.statistics(); 00212 } 00213 00214 CollectionStatistics Collection::statistics() const 00215 { 00216 return d_func()->statistics; 00217 } 00218 00219 void Collection::setStatistics(const CollectionStatistics & statistics) 00220 { 00221 Q_D( Collection ); 00222 d->statistics = statistics; 00223 } 00224 00225 CachePolicy Collection::cachePolicy() const 00226 { 00227 return d_func()->cachePolicy; 00228 } 00229 00230 void Collection::setCachePolicy(const CachePolicy & cachePolicy) 00231 { 00232 Q_D( Collection ); 00233 d->cachePolicy = cachePolicy; 00234 d->cachePolicyChanged = true; 00235 } 00236 00237 AKONADI_DEFINE_PRIVATE( Akonadi::Collection )