Akonadi

recentcollectionaction.cpp
1 /*
2  * SPDX-FileCopyrightText: 2011-2022 Laurent Montel <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 #include "entitytreemodel.h"
8 #include "recentcollectionaction_p.h"
9 #include <KConfigGroup>
10 #include <KLocalizedString>
11 #include <KSharedConfig>
12 
13 #include <QAction>
14 #include <QMenu>
15 using namespace Akonadi;
16 
17 static const int s_maximumRecentCollection = 10;
18 
19 static QStringList readConfig()
20 {
21  const KSharedConfig::Ptr akonadiConfig = KSharedConfig::openConfig(QStringLiteral("akonadikderc"));
22  const KConfigGroup group(akonadiConfig, QStringLiteral("Recent Collections"));
23  return (group.readEntry("Collections", QStringList()));
24 }
25 
26 static void writeConfig(const QStringList &list)
27 {
28  KSharedConfig::Ptr akonadiConfig = KSharedConfig::openConfig(QStringLiteral("akonadikderc"));
29  KConfigGroup group(akonadiConfig, QStringLiteral("Recent Collections"));
30  group.writeEntry("Collections", list);
31  group.sync();
32 }
33 
34 RecentCollectionAction::RecentCollectionAction(Akonadi::StandardActionManager::Type type,
35  const Akonadi::Collection::List &selectedCollectionsList,
36  const QAbstractItemModel *model,
37  QMenu *menu)
38  : QObject(menu)
39  , mMenu(menu)
40  , mModel(model)
41 {
42  mListRecentCollection = readConfig();
43  mRecentAction = mMenu->addAction(i18n("Recent Folder"));
44  mMenu->addSeparator();
45  fillRecentCollection(type, selectedCollectionsList);
46 }
47 
48 RecentCollectionAction::~RecentCollectionAction()
49 {
50  // if (needToDeleteMenu) {
51  // delete mRecentAction->menu();
52  // }
53 }
54 
55 bool RecentCollectionAction::clear()
56 {
57  delete mRecentAction->menu();
58  needToDeleteMenu = false;
59  if (mListRecentCollection.isEmpty()) {
60  mRecentAction->setEnabled(false);
61  return true;
62  }
63  return false;
64 }
65 
66 void RecentCollectionAction::fillRecentCollection(Akonadi::StandardActionManager::Type type, const Akonadi::Collection::List &selectedCollectionsList)
67 {
68  if (clear()) {
69  return;
70  }
71 
72  auto popup = new QMenu;
73  mRecentAction->setMenu(popup);
74  needToDeleteMenu = true;
75 
76  const int numberOfRecentCollection(mListRecentCollection.count());
77  for (int i = 0; i < numberOfRecentCollection; ++i) {
78  const QModelIndex index = Akonadi::EntityTreeModel::modelIndexForCollection(mModel, Akonadi::Collection(mListRecentCollection.at(i).toLongLong()));
79  const auto collection = mModel->data(index, Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
80  if (index.isValid()) {
81  const bool collectionIsSelected = selectedCollectionsList.contains(collection);
82  if (type == Akonadi::StandardActionManager::MoveCollectionToMenu && collectionIsSelected) {
83  continue;
84  }
85 
86  const bool canCreateNewItems = (collection.rights() & Collection::CanCreateItem);
87  QAction *action = popup->addAction(actionName(index));
88  const auto icon = mModel->data(index, Qt::DecorationRole).value<QIcon>();
89  action->setIcon(icon);
90  action->setData(QVariant::fromValue<QModelIndex>(index));
91  action->setEnabled(canCreateNewItems);
92  }
93  }
94 }
95 
96 QString RecentCollectionAction::actionName(QModelIndex index)
97 {
98  QString name = index.data().toString();
99  name.replace(QLatin1Char('&'), QStringLiteral("&&"));
100 
101  index = index.parent();
102  QString topLevelName;
103  while (index != QModelIndex()) {
104  topLevelName = index.data().toString();
105  index = index.parent();
106  }
107  if (topLevelName.isEmpty()) {
108  return name;
109  } else {
110  topLevelName.replace(QLatin1Char('&'), QStringLiteral("&&"));
111  return QStringLiteral("%1 - %2").arg(name, topLevelName);
112  }
113 }
114 
115 void RecentCollectionAction::addRecentCollection(Akonadi::StandardActionManager::Type type, Akonadi::Collection::Id id)
116 {
117  mListRecentCollection = addRecentCollection(id);
118  fillRecentCollection(type, Akonadi::Collection::List());
119 }
120 
121 /* static */ QStringList RecentCollectionAction::addRecentCollection(Akonadi::Collection::Id id)
122 {
123  QStringList listRecentCollection = readConfig();
124 
125  const QString newCollectionID = QString::number(id);
126  if (listRecentCollection.contains(newCollectionID)) {
127  // first() is safe to use if we get here
128  if (listRecentCollection.first() == newCollectionID) {
129  // already most recently used, nothing to do
130  return (listRecentCollection);
131  }
132 
133  listRecentCollection.removeAll(newCollectionID);
134  }
135 
136  listRecentCollection.prepend(newCollectionID);
137  while (listRecentCollection.count() > s_maximumRecentCollection) {
138  listRecentCollection.removeLast();
139  }
140 
141  writeConfig(listRecentCollection);
142  return (listRecentCollection);
143 }
144 
145 void RecentCollectionAction::cleanRecentCollection()
146 {
147  mListRecentCollection.clear();
148  writeConfig(mListRecentCollection);
149  clear();
150 }
151 
152 #include "moc_recentcollectionaction_p.cpp"
T & first()
DecorationRole
QString number(int n, int base)
int removeAll(const T &value)
@ CanCreateItem
Can create new items in this collection.
Definition: collection.h:92
int count(const T &value) const const
T value() const const
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
void readConfig()
KGuiItem clear()
Clears all queries from current thread.
Definition: querycache.cpp:107
Represents a collection of PIM items.
Definition: collection.h:61
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
void removeLast()
QVariant data(int role) const const
void setIcon(const QIcon &icon)
void prepend(const T &value)
QString i18n(const char *text, const TYPE &arg...)
@ CollectionRole
The collection.
bool isEmpty() const const
bool contains(const T &value) const const
bool isValid() const const
QString & replace(int position, int n, QChar after)
QVariant data() const const
void setData(const QVariant &userData)
void setEnabled(bool)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString name(StandardShortcut id)
Type
Describes the supported actions.
static QModelIndex modelIndexForCollection(const QAbstractItemModel *model, const Collection &collection)
Returns a QModelIndex in model which points to collection.
QModelIndex parent() const const
@ MoveCollectionToMenu
Menu allowing to move a collection into another collection.
qint64 Id
Describes the unique id type.
Definition: collection.h:79
QString toString() const const
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Jun 27 2022 04:01:07 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.