Commit 79f84aa3 authored by Jonathan Marten's avatar Jonathan Marten
Browse files

Make the recently accessed folder list globally accessible

This is normally managed by RecentCollectionAction, but is only updated
by StandardActionManager when its standard actions are used.  If a
caller uses its own actions, for example KMail's separate message reader
window, then the list is not updated.

Add a static function StandardActionManager::addRecentCollection(),
which simply calls RecentCollectionAction::addRecentCollection() to
update the global recent collection list.  Read and update this list in
RecentCollectionAction as before.

CCBUG:435410
parent 61b661ff
Pipeline #57392 passed with stage
in 13 minutes and 7 seconds
......@@ -6,9 +6,9 @@
#include "entitytreemodel.h"
#include "recentcollectionaction_p.h"
#include <KConfig>
#include <KConfigGroup>
#include <KLocalizedString>
#include <KSharedConfig>
#include <QAction>
#include <QMenu>
......@@ -16,6 +16,21 @@ using namespace Akonadi;
static const int s_maximumRecentCollection = 10;
static QStringList readConfig()
{
const KSharedConfig::Ptr akonadiConfig = KSharedConfig::openConfig(QStringLiteral("akonadikderc"));
const KConfigGroup group(akonadiConfig, QStringLiteral("Recent Collections"));
return (group.readEntry("Collections", QStringList()));
}
static void writeConfig(const QStringList &list)
{
KSharedConfig::Ptr akonadiConfig = KSharedConfig::openConfig(QStringLiteral("akonadikderc"));
KConfigGroup group(akonadiConfig, QStringLiteral("Recent Collections"));
group.writeEntry("Collections", list);
group.sync();
}
RecentCollectionAction::RecentCollectionAction(Akonadi::StandardActionManager::Type type,
const Akonadi::Collection::List &selectedCollectionsList,
const QAbstractItemModel *model,
......@@ -24,10 +39,7 @@ RecentCollectionAction::RecentCollectionAction(Akonadi::StandardActionManager::T
, mMenu(menu)
, mModel(model)
{
mAkonadiConfig = KSharedConfig::openConfig(QStringLiteral("akonadikderc"));
KConfigGroup group(mAkonadiConfig, QStringLiteral("Recent Collections"));
mListRecentCollection = group.readEntry("Collections", QStringList());
mListRecentCollection = readConfig();
mRecentAction = mMenu->addAction(i18n("Recent Folder"));
mMenu->addSeparator();
fillRecentCollection(type, selectedCollectionsList);
......@@ -102,37 +114,38 @@ QString RecentCollectionAction::actionName(QModelIndex index)
void RecentCollectionAction::addRecentCollection(Akonadi::StandardActionManager::Type type, Akonadi::Collection::Id id)
{
mListRecentCollection = addRecentCollection(id);
fillRecentCollection(type, Akonadi::Collection::List());
}
/* static */ QStringList RecentCollectionAction::addRecentCollection(Akonadi::Collection::Id id)
{
QStringList listRecentCollection = readConfig();
const QString newCollectionID = QString::number(id);
if (mListRecentCollection.contains(newCollectionID)) {
if (listRecentCollection.contains(newCollectionID)) {
// first() is safe to use if we get here
if (mListRecentCollection.first() == newCollectionID) {
if (listRecentCollection.first() == newCollectionID) {
// already most recently used, nothing to do
return;
return (listRecentCollection);
}
mListRecentCollection.removeAll(newCollectionID);
listRecentCollection.removeAll(newCollectionID);
}
mListRecentCollection.prepend(newCollectionID);
while (mListRecentCollection.count() > s_maximumRecentCollection) {
mListRecentCollection.removeLast();
listRecentCollection.prepend(newCollectionID);
while (listRecentCollection.count() > s_maximumRecentCollection) {
listRecentCollection.removeLast();
}
writeConfig();
fillRecentCollection(type, Akonadi::Collection::List());
}
void RecentCollectionAction::writeConfig()
{
KConfigGroup group(mAkonadiConfig, QStringLiteral("Recent Collections"));
group.writeEntry("Collections", mListRecentCollection);
group.sync();
writeConfig(listRecentCollection);
return (listRecentCollection);
}
void RecentCollectionAction::cleanRecentCollection()
{
mListRecentCollection.clear();
writeConfig();
writeConfig(mListRecentCollection);
clear();
}
......
......@@ -6,7 +6,6 @@
#pragma once
#include "collection.h"
#include <KSharedConfig>
#include <QModelIndex>
#include <QStringList>
#include <standardactionmanager.h>
......@@ -46,8 +45,15 @@ public:
void cleanRecentCollection();
/**
* Add a new collection to the global list.
*
* @param id the collection ID
* @since 5.18
*/
static QStringList addRecentCollection(Akonadi::Collection::Id id);
private:
void writeConfig();
void fillRecentCollection(Akonadi::StandardActionManager::Type type, const Akonadi::Collection::List &selectedCollectionsList);
QString actionName(QModelIndex index);
bool clear();
......@@ -57,8 +63,6 @@ private:
QMenu *const mMenu;
const QAbstractItemModel *mModel = nullptr;
QAction *mRecentAction = nullptr;
KSharedConfig::Ptr mAkonadiConfig;
bool needToDeleteMenu = false;
};
}
......@@ -31,6 +31,7 @@
#include <KActionCollection>
#include <KActionMenu>
#include <KConfig>
#include <KConfigGroup>
#include <KLocalizedString>
#include <KMessageBox>
......@@ -1977,4 +1978,9 @@ void StandardActionManager::createActionFolderMenu(QMenu *menu, Type type)
d->createActionFolderMenu(menu, type);
}
void StandardActionManager::addRecentCollection(Akonadi::Collection::Id id) const
{
RecentCollectionAction::addRecentCollection(id);
}
#include "moc_standardactionmanager.cpp"
......@@ -341,6 +341,14 @@ public:
*/
void createActionFolderMenu(QMenu *menu, Type type);
/**
* Add a collection to the global recent collection list.
*
* @param id the collection ID
* @since 5.18
*/
void addRecentCollection(Akonadi::Collection::Id id) const;
Q_SIGNALS:
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment