19 #include "recentcollectionaction_p.h"
20 #include "metatypes.h"
21 #include <akonadi/entitytreemodel.h>
22 #include <akonadi/collectionmodel.h>
24 #include <KConfigGroup>
29 using namespace Akonadi;
31 static int s_maximumRecentCollection = 10;
33 RecentCollectionAction::RecentCollectionAction(
const QAbstractItemModel *model, QMenu *menu)
39 mAkonadiConfig = KSharedConfig::openConfig( QLatin1String(
"akonadikderc" ) );
40 KConfigGroup group( mAkonadiConfig, QLatin1String(
"Recent Collections" ) );
42 mListRecentCollection = group.readEntry(
"Collections", QStringList() );
43 mRecentAction = mMenu->addAction( i18n(
"Recent Folder" ) );
44 mMenu->addSeparator();
45 fillRecentCollection();
48 RecentCollectionAction::~RecentCollectionAction()
52 void RecentCollectionAction::fillRecentCollection()
54 delete mRecentAction->menu();
55 if ( mListRecentCollection.isEmpty() ) {
56 mRecentAction->setEnabled(
false );
60 QMenu* popup =
new QMenu;
61 mRecentAction->setMenu( popup );
63 const int numberOfRecentCollection(mListRecentCollection.count());
64 for (
int i=0; i < numberOfRecentCollection; ++i )
68 if ( index.isValid() ) {
70 QAction *action = popup->addAction( actionName( index ) );
71 const QIcon icon = mModel->data( index, Qt::DecorationRole ).value<QIcon>();
72 action->setIcon( icon );
73 action->setData( QVariant::fromValue<QModelIndex>( index ) );
74 action->setEnabled(canCreateNewItems);
79 QString RecentCollectionAction::actionName(QModelIndex index)
81 QString name = index.data().toString();
82 name.replace( QLatin1String(
"&" ), QLatin1String(
"&&" ) );
86 while ( index != QModelIndex() ) {
87 topLevelName = index.data().toString();
88 index = index.parent();
90 if ( topLevelName.isEmpty() )
91 return QString::fromLatin1(
"%1" ).arg( name );
93 topLevelName.replace( QLatin1String(
"&" ), QLatin1String(
"&&" ) );
94 return QString::fromLatin1(
"%1 - %2" ).arg( name ).arg( topLevelName );
100 const QString newCollectionID = QString::number(
id );
101 if ( mListRecentCollection.isEmpty() ||
102 !mListRecentCollection.contains( newCollectionID ) ) {
103 if ( mListRecentCollection.count() == s_maximumRecentCollection )
104 mListRecentCollection.removeFirst();
105 mListRecentCollection.append( newCollectionID );
107 fillRecentCollection();
111 void RecentCollectionAction::writeConfig()
113 KConfigGroup group( mAkonadiConfig, QLatin1String(
"Recent Collections" ) );
114 group.writeEntry(
"Collections", mListRecentCollection );
118 void RecentCollectionAction::cleanRecentCollection()
120 mListRecentCollection.clear();
122 fillRecentCollection();
125 #include "moc_recentcollectionaction_p.cpp"