Krita Source Code Documentation
Loading...
Searching...
No Matches
KisRecentFilesManager.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2022 Alvin Wong <alvin@alvinhc.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
8
9#include <QApplication>
10#include <QDir>
11#include <QThread>
12#include <QTimer>
13#include <QDebug>
14
15#include <kconfig.h>
16#include <kconfiggroup.h>
17#include <ksharedconfig.h>
18#include <KisMpl.h>
19
21{
22public:
24 int m_maxItems {10};
26
27private:
29
30public:
32
33 bool containsUrl(const QUrl &url) const;
34 int indexOfUrl(const QUrl &url) const;
35
37}; /* class KisRecentFilesEntry::Private */
38
40 : m_q(q)
41 , m_saveOnIdleTimer(q)
42{
43 m_saveOnIdleTimer.setSingleShot(true);
44 m_saveOnIdleTimer.setInterval(0);
45 m_q->connect(&m_saveOnIdleTimer, &QTimer::timeout, [this]() {
46 m_q->saveEntries(KSharedConfig::openConfig()->group("RecentFiles"));
47 });
48}
49
51{
52 return indexOfUrl(url) >= 0;
53}
54
56{
57 auto found = std::find_if(m_entries.constBegin(), m_entries.constEnd(),
59 if (found == m_entries.constEnd()) {
60 return -1;
61 } else {
62 return found - m_entries.constBegin();
63 }
64}
65
67{
68 // If multiple saves are requested within the same tick, they will be
69 // consolidated by the timer.
70 m_saveOnIdleTimer.start();
71}
72
73
75 : m_d(new Private(this))
76{
77 loadEntries(KSharedConfig::openConfig()->group("RecentFiles"));
78}
79
84
86{
87 if (QThread::currentThread() != qApp->thread()) {
88 qWarning() << "KisRecentFilesManager::instance() called from non-GUI thread!";
89 return nullptr;
90 }
91 static KisRecentFilesManager s_instance;
92 return &s_instance;
93}
94
96{
97 m_d->m_entries.clear();
98 Q_EMIT listRenewed();
100}
101
102void KisRecentFilesManager::remove(const QUrl &url)
103{
104 int removeIndex = m_d->indexOfUrl(url);
105 if (removeIndex >= 0) {
106 m_d->m_entries.removeAt(removeIndex);
107 Q_EMIT fileRemoved(url);
109 }
110}
111
116
118{
119 // switch order so last opened file is first
120 QList<QUrl> sortedList;
121 for (int i = m_d->m_entries.length() - 1; i >= 0; i--) {
122 sortedList.append(m_d->m_entries[i].m_url);
123 }
124
125 return sortedList;
126}
127
128// The following file contains code copied and modified from LGPL-2.0-only
129// source:
int indexOfUrl(const QUrl &url) const
QVector< KisRecentFilesEntry > m_entries
bool containsUrl(const QUrl &url) const
void remove(const QUrl &url)
void loadEntries(const KConfigGroup &config)
QVector< KisRecentFilesEntry > recentFiles() const
void fileRemoved(const QUrl &url)
QList< QUrl > recentUrlsLatestFirst() const
static KisRecentFilesManager * instance()
void saveEntries(const KConfigGroup &config)
auto mem_equal_to(MemTypeNoRef Class::*ptr, MemType &&value)
mem_equal_to is an unary functor that compares a member of the object to a given value
Definition KisMpl.h:233