akonadi
recentcollectionaction.cpp
00001 /* 00002 * Copyright (c) 2011 Laurent Montel <montel@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 * This library is distributed in the hope that it will be useful, but WITHOUT 00009 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00010 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00011 * License for more details. 00012 * 00013 * You should have received a copy of the GNU Library General Public License 00014 * along with this library; see the file COPYING.LIB. If not, write to the 00015 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00016 * 02110-1301, USA. 00017 */ 00018 00019 #include "recentcollectionaction_p.h" 00020 #include "metatypes.h" 00021 #include <akonadi/entitytreemodel.h> 00022 #include <akonadi/collectionmodel.h> 00023 #include <KConfig> 00024 #include <KConfigGroup> 00025 #include <KLocale> 00026 00027 #include <QMenu> 00028 #include <QAction> 00029 using namespace Akonadi; 00030 00031 static int s_maximumRecentCollection = 10; 00032 00033 RecentCollectionAction::RecentCollectionAction(const QAbstractItemModel *model, QMenu *menu) 00034 :QObject( menu ), 00035 mMenu( menu ), 00036 mModel( model ), 00037 mRecentAction( 0 ) 00038 { 00039 mAkonadiConfig = KSharedConfig::openConfig( QLatin1String( "akonadikderc" ) ); 00040 KConfigGroup group( mAkonadiConfig, QLatin1String( "Recent Collections" ) ); 00041 00042 mListRecentCollection = group.readEntry( "Collections", QStringList() ); 00043 mRecentAction = mMenu->addAction( i18n( "Recent Folder" ) ); 00044 mMenu->addSeparator(); 00045 fillRecentCollection(); 00046 } 00047 00048 RecentCollectionAction::~RecentCollectionAction() 00049 { 00050 } 00051 00052 void RecentCollectionAction::fillRecentCollection() 00053 { 00054 delete mRecentAction->menu(); 00055 if ( mListRecentCollection.isEmpty() ) { 00056 mRecentAction->setEnabled( false ); 00057 return; 00058 } 00059 00060 QMenu* popup = new QMenu; 00061 mRecentAction->setMenu( popup ); 00062 00063 const int numberOfRecentCollection(mListRecentCollection.count()); 00064 for ( int i=0; i < numberOfRecentCollection; ++i ) 00065 { 00066 const QModelIndex index = Akonadi::EntityTreeModel::modelIndexForCollection( mModel, Akonadi::Collection( mListRecentCollection.at( i ).toLongLong() ) ); 00067 const Akonadi::Collection collection = mModel->data( index, Akonadi::CollectionModel::CollectionRole ).value<Akonadi::Collection>(); 00068 if ( index.isValid() ) { 00069 const bool canCreateNewItems = (collection.rights() & Collection::CanCreateItem); 00070 QAction *action = popup->addAction( actionName( index ) ); 00071 const QIcon icon = mModel->data( index, Qt::DecorationRole ).value<QIcon>(); 00072 action->setIcon( icon ); 00073 action->setData( QVariant::fromValue<QModelIndex>( index ) ); 00074 action->setEnabled(canCreateNewItems); 00075 } 00076 } 00077 } 00078 00079 QString RecentCollectionAction::actionName(QModelIndex index) 00080 { 00081 QString name = index.data().toString(); 00082 name.replace( QLatin1String( "&" ), QLatin1String( "&&" ) ); 00083 00084 index = index.parent(); 00085 QString topLevelName; 00086 while ( index != QModelIndex() ) { 00087 topLevelName = index.data().toString(); 00088 index = index.parent(); 00089 } 00090 if ( topLevelName.isEmpty() ) 00091 return QString::fromLatin1( "%1" ).arg( name ); 00092 else { 00093 topLevelName.replace( QLatin1String( "&" ), QLatin1String( "&&" ) ); 00094 return QString::fromLatin1( "%1 - %2" ).arg( name ).arg( topLevelName ); 00095 } 00096 } 00097 00098 void RecentCollectionAction::addRecentCollection( Akonadi::Collection::Id id ) 00099 { 00100 const QString newCollectionID = QString::number( id ); 00101 if ( mListRecentCollection.isEmpty() || 00102 !mListRecentCollection.contains( newCollectionID ) ) { 00103 if ( mListRecentCollection.count() == s_maximumRecentCollection ) 00104 mListRecentCollection.removeFirst(); 00105 mListRecentCollection.append( newCollectionID ); 00106 writeConfig(); 00107 fillRecentCollection(); 00108 } 00109 } 00110 00111 void RecentCollectionAction::writeConfig() 00112 { 00113 KConfigGroup group( mAkonadiConfig, QLatin1String( "Recent Collections" ) ); 00114 group.writeEntry( "Collections", mListRecentCollection ); 00115 group.sync(); 00116 } 00117 00118 void RecentCollectionAction::cleanRecentCollection() 00119 { 00120 mListRecentCollection.clear(); 00121 writeConfig(); 00122 fillRecentCollection(); 00123 } 00124 00125 #include "recentcollectionaction_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu Aug 2 2012 15:25:19 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu Aug 2 2012 15:25:19 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.