Akonadi

recentcollectionaction.cpp
1 /*
2  * Copyright (C) 2011-2020 Laurent Montel <[email protected]>
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 "entitytreemodel.h"
22 #include <KConfig>
23 #include <KConfigGroup>
24 #include <KLocalizedString>
25 
26 #include <QMenu>
27 #include <QAction>
28 using namespace Akonadi;
29 
30 static const int s_maximumRecentCollection = 10;
31 
32 RecentCollectionAction::RecentCollectionAction(Akonadi::StandardActionManager::Type type, const Akonadi::Collection::List &selectedCollectionsList, const QAbstractItemModel *model, QMenu *menu)
33  : QObject(menu)
34  , mMenu(menu)
35  , mModel(model)
36  , mRecentAction(nullptr)
37 {
38  mAkonadiConfig = KSharedConfig::openConfig(QStringLiteral("akonadikderc"));
39  KConfigGroup group(mAkonadiConfig, QStringLiteral("Recent Collections"));
40 
41  mListRecentCollection = group.readEntry("Collections", QStringList());
42  mRecentAction = mMenu->addAction(i18n("Recent Folder"));
43  mMenu->addSeparator();
44  fillRecentCollection(type, selectedCollectionsList);
45 }
46 
47 RecentCollectionAction::~RecentCollectionAction()
48 {
49 }
50 
51 bool RecentCollectionAction::clear()
52 {
53  delete mRecentAction->menu();
54  if (mListRecentCollection.isEmpty()) {
55  mRecentAction->setEnabled(false);
56  return true;
57  }
58  return false;
59 }
60 
61 void RecentCollectionAction::fillRecentCollection(Akonadi::StandardActionManager::Type type, const Akonadi::Collection::List &selectedCollectionsList)
62 {
63  if (clear()) {
64  return;
65  }
66 
67  QMenu *popup = 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 auto collection = mModel->data(index, Akonadi::EntityTreeModel::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(QLatin1Char('&'), QStringLiteral("&&"));
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 name;
103  } else {
104  topLevelName.replace(QLatin1Char('&'), QStringLiteral("&&"));
105  return QStringLiteral("%1 - %2").arg(name, 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, QStringLiteral("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"
Type
Describes the supported actions.
QString name(const QVariant &location)
Represents a collection of PIM items.
Definition: collection.h:76
qint64 Id
Describes the unique id type.
Definition: collection.h:82
void setIcon(const QIcon &icon)
QAction * addAction(const QString &text)
bool isValid() const const
QString number(int n, int base)
bool contains(const T &value) const const
KGuiItem clear()
Clears all queries from current thread.
Definition: querycache.cpp:120
bool isEmpty() const const
Can create new items in this collection.
Definition: collection.h:95
QModelIndex parent() const const
static QModelIndex modelIndexForCollection(const QAbstractItemModel *model, const Collection &collection)
Returns a QModelIndex in model which points to collection.
void setData(const QVariant &userData)
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString i18n(const char *text, const TYPE &arg...)
QString & replace(int position, int n, QChar after)
QVariant data(int role) const const
Helper integration between Akonadi and Qt.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString toString() const const
void setEnabled(bool)
Menu allowing to move a collection into another collection.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:08:55 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.