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 <KConfig> 00023 #include <KConfigGroup> 00024 #include <KLocale> 00025 00026 #include <QMenu> 00027 #include <QAction> 00028 using namespace Akonadi; 00029 00030 static int s_maximumRecentCollection = 10; 00031 00032 RecentCollectionAction::RecentCollectionAction(const QAbstractItemModel *model, QMenu *menu) 00033 :QObject( menu ), 00034 mMenu( menu ), 00035 mModel( model ), 00036 mRecentAction( 0 ) 00037 { 00038 mAkonadiConfig = KSharedConfig::openConfig( QLatin1String( "akonadikderc" ) ); 00039 KConfigGroup group( mAkonadiConfig, QLatin1String( "Recent Collections" ) ); 00040 00041 mListRecentCollection = group.readEntry( "Collections", QStringList() ); 00042 mRecentAction = mMenu->addAction( i18n( "Recent Folder" ) ); 00043 mMenu->addSeparator(); 00044 fillRecentCollection(); 00045 } 00046 00047 RecentCollectionAction::~RecentCollectionAction() 00048 { 00049 } 00050 00051 void RecentCollectionAction::fillRecentCollection() 00052 { 00053 delete mRecentAction->menu(); 00054 if ( mListRecentCollection.isEmpty() ) { 00055 mRecentAction->setEnabled( false ); 00056 return; 00057 } 00058 00059 QMenu* popup = new QMenu; 00060 mRecentAction->setMenu( popup ); 00061 00062 const int numberOfRecentCollection(mListRecentCollection.count()); 00063 for ( int i=0; i < numberOfRecentCollection; ++i ) 00064 { 00065 const QModelIndex index = Akonadi::EntityTreeModel::modelIndexForCollection( mModel, Akonadi::Collection( mListRecentCollection.at( i ).toLongLong() ) ); 00066 if ( index.isValid() ) { 00067 QAction *action = popup->addAction( actionName( index ) ); 00068 const QIcon icon = mModel->data( index, Qt::DecorationRole ).value<QIcon>(); 00069 action->setIcon( icon ); 00070 action->setData( QVariant::fromValue<QModelIndex>( index ) ); 00071 } 00072 } 00073 } 00074 00075 QString RecentCollectionAction::actionName(QModelIndex index) 00076 { 00077 QString name = index.data().toString(); 00078 name.replace( QLatin1String( "&" ), QLatin1String( "&&" ) ); 00079 00080 index = index.parent(); 00081 QString topLevelName; 00082 while ( index != QModelIndex() ) { 00083 topLevelName = index.data().toString(); 00084 index = index.parent(); 00085 } 00086 if ( topLevelName.isEmpty() ) 00087 return QString::fromLatin1( "%1" ).arg( name ); 00088 else { 00089 topLevelName.replace( QLatin1String( "&" ), QLatin1String( "&&" ) ); 00090 return QString::fromLatin1( "%1 - %2" ).arg( name ).arg( topLevelName ); 00091 } 00092 } 00093 00094 void RecentCollectionAction::addRecentCollection( Akonadi::Collection::Id id ) 00095 { 00096 const QString newCollectionID = QString::number( id ); 00097 if ( mListRecentCollection.isEmpty() || 00098 !mListRecentCollection.contains( newCollectionID ) ) { 00099 if ( mListRecentCollection.count() == s_maximumRecentCollection ) 00100 mListRecentCollection.removeFirst(); 00101 mListRecentCollection.append( newCollectionID ); 00102 writeConfig(); 00103 fillRecentCollection(); 00104 } 00105 } 00106 00107 void RecentCollectionAction::writeConfig() 00108 { 00109 KConfigGroup group( mAkonadiConfig, QLatin1String( "Recent Collections" ) ); 00110 group.writeEntry( "Collections", mListRecentCollection ); 00111 group.sync(); 00112 } 00113 00114 void RecentCollectionAction::cleanRecentCollection() 00115 { 00116 mListRecentCollection.clear(); 00117 writeConfig(); 00118 fillRecentCollection(); 00119 } 00120 00121 #include "recentcollectionaction_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:18:35 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:18:35 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.