1 | /* |
2 | * Copyright (c) 2011 Laurent Montel <montel@kde.org> |
3 | * |
4 | * This library is free software; you can redistribute it and/or modify it |
5 | * under the terms of the GNU Library General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or (at your |
7 | * option) any later version. |
8 | * This library is distributed in the hope that it will be useful, but WITHOUT |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
11 | * License for more details. |
12 | * |
13 | * You should have received a copy of the GNU Library General Public License |
14 | * along with this library; see the file COPYING.LIB. If not, write to the |
15 | * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301, USA. |
17 | */ |
18 | |
19 | #include "recentcollectionaction_p.h" |
20 | #include "metatypes.h" |
21 | #include <akonadi/entitytreemodel.h> |
22 | #include <akonadi/collectionmodel.h> |
23 | #include <KConfig> |
24 | #include <KConfigGroup> |
25 | #include <KLocalizedString> |
26 | |
27 | #include <QMenu> |
28 | #include <QAction> |
29 | using namespace Akonadi; |
30 | |
31 | static int s_maximumRecentCollection = 10; |
32 | |
33 | RecentCollectionAction::RecentCollectionAction(Akonadi::StandardActionManager::Type type, const Akonadi::Collection::List &selectedCollectionsList,const QAbstractItemModel *model, QMenu *) |
34 | : QObject(menu) |
35 | , mMenu(menu) |
36 | , mModel(model) |
37 | , mRecentAction(0) |
38 | { |
39 | mAkonadiConfig = KSharedConfig::openConfig(QLatin1String("akonadikderc" )); |
40 | KConfigGroup group(mAkonadiConfig, QLatin1String("Recent Collections" )); |
41 | |
42 | mListRecentCollection = group.readEntry("Collections" , QStringList()); |
43 | mRecentAction = mMenu->addAction(i18n("Recent Folder" )); |
44 | mMenu->addSeparator(); |
45 | fillRecentCollection(type, selectedCollectionsList); |
46 | } |
47 | |
48 | RecentCollectionAction::~RecentCollectionAction() |
49 | { |
50 | } |
51 | |
52 | bool RecentCollectionAction::clear() |
53 | { |
54 | delete mRecentAction->menu(); |
55 | if (mListRecentCollection.isEmpty()) { |
56 | mRecentAction->setEnabled(false); |
57 | return true; |
58 | } |
59 | return false; |
60 | } |
61 | |
62 | void RecentCollectionAction::fillRecentCollection(Akonadi::StandardActionManager::Type type, const Akonadi::Collection::List &selectedCollectionsList) |
63 | { |
64 | if (clear()) |
65 | return; |
66 | |
67 | QMenu * = new QMenu; |
68 | mRecentAction->setMenu(popup); |
69 | |
70 | const int numberOfRecentCollection(mListRecentCollection.count()); |
71 | for (int i = 0; i < numberOfRecentCollection; ++i) { |
72 | const QModelIndex index = Akonadi::EntityTreeModel::modelIndexForCollection(mModel, Akonadi::Collection(mListRecentCollection.at(i).toLongLong())); |
73 | const Akonadi::Collection collection = mModel->data(index, Akonadi::CollectionModel::CollectionRole).value<Akonadi::Collection>(); |
74 | if (index.isValid()) { |
75 | const bool collectionIsSelected = selectedCollectionsList.contains(collection); |
76 | if (type == Akonadi::StandardActionManager::MoveCollectionToMenu && collectionIsSelected) { |
77 | continue; |
78 | } |
79 | |
80 | const bool canCreateNewItems = (collection.rights() &Collection::CanCreateItem); |
81 | QAction *action = popup->addAction(actionName(index)); |
82 | const QIcon icon = mModel->data(index, Qt::DecorationRole).value<QIcon>(); |
83 | action->setIcon(icon); |
84 | action->setData(QVariant::fromValue<QModelIndex>(index)); |
85 | action->setEnabled(canCreateNewItems); |
86 | } |
87 | } |
88 | } |
89 | |
90 | QString RecentCollectionAction::actionName(QModelIndex index) |
91 | { |
92 | QString name = index.data().toString(); |
93 | name.replace(QLatin1String("&" ), QLatin1String("&&" )); |
94 | |
95 | index = index.parent(); |
96 | QString topLevelName; |
97 | while (index != QModelIndex()) { |
98 | topLevelName = index.data().toString(); |
99 | index = index.parent(); |
100 | } |
101 | if (topLevelName.isEmpty()) { |
102 | return QString::fromLatin1("%1" ).arg(name); |
103 | } else { |
104 | topLevelName.replace(QLatin1String("&" ), QLatin1String("&&" )); |
105 | return QString::fromLatin1("%1 - %2" ).arg(name).arg(topLevelName); |
106 | } |
107 | } |
108 | |
109 | void RecentCollectionAction::addRecentCollection(Akonadi::StandardActionManager::Type type, Akonadi::Collection::Id id) |
110 | { |
111 | const QString newCollectionID = QString::number(id); |
112 | if (mListRecentCollection.isEmpty() || |
113 | !mListRecentCollection.contains(newCollectionID)) { |
114 | if (mListRecentCollection.count() == s_maximumRecentCollection) { |
115 | mListRecentCollection.removeFirst(); |
116 | } |
117 | mListRecentCollection.append(newCollectionID); |
118 | writeConfig(); |
119 | fillRecentCollection(type, Akonadi::Collection::List()); |
120 | } |
121 | } |
122 | |
123 | void RecentCollectionAction::writeConfig() |
124 | { |
125 | KConfigGroup group(mAkonadiConfig, QLatin1String("Recent Collections" )); |
126 | group.writeEntry("Collections" , mListRecentCollection); |
127 | group.sync(); |
128 | } |
129 | |
130 | void RecentCollectionAction::cleanRecentCollection() |
131 | { |
132 | mListRecentCollection.clear(); |
133 | writeConfig(); |
134 | clear(); |
135 | } |
136 | |
137 | #include "moc_recentcollectionaction_p.cpp" |
138 | |