Krita Source Code Documentation
Loading...
Searching...
No Matches
KisRecentFilesManager_p.h
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 1999 Reginald Stadlbauer <reggie@kde.org>
3 SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org>
4 SPDX-FileCopyrightText: 2000 Nicolas Hadacek <haadcek@kde.org>
5 SPDX-FileCopyrightText: 2000 Kurt Granroth <granroth@kde.org>
6 SPDX-FileCopyrightText: 2000 Michael Koch <koch@kde.org>
7 SPDX-FileCopyrightText: 2001 Holger Freyther <freyther@kde.org>
8 SPDX-FileCopyrightText: 2002 Ellis Whitehead <ellis@kde.org>
9 SPDX-FileCopyrightText: 2002 Joseph Wenninger <jowenn@kde.org>
10 SPDX-FileCopyrightText: 2003 Andras Mantia <amantia@kde.org>
11 SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <rodda@kde.org>
12 SPDX-FileCopyrightText: 2022 Alvin Wong <alvin@alvinhc.com>
13
14 SPDX-License-Identifier: LGPL-2.0-only
15*/
16
17/*
18 * The functions in this file was refactored out of `krecentfilesaction.cpp`.
19 *
20 * `krecentfilesaction.cpp` was forked from KConfigWidgets. Due to historical
21 * reasons, it was licensed under LGPL 2.0 only (not LGPL 2.1 or later). Until
22 * the original file has been relicensed, keep these code snippets in a
23 * separate file.
24 *
25 * The relicensing issue is tracked at https://invent.kde.org/teams/licensing/issues/-/issues/34
26 */
27
28void KisRecentFilesManager::loadEntries(const KConfigGroup &_config)
29{
30 m_d->m_entries.clear();
31
32 KConfigGroup cg = _config;
33 if (cg.name().isEmpty()) {
34 cg = KConfigGroup(cg.config(), "RecentFiles");
35 }
36
37 m_d->m_maxItems = cg.readEntry("maxRecentFileItems", 100);
38
39 for (int i = 0; i < m_d->m_maxItems; ++i) {
40 const QString key = QString("File%1").arg(i+1);
41 QString value;
42#ifdef Q_OS_ANDROID
43 value = cg.readEntry(key, QString());
44#else
45 value = cg.readPathEntry(key, QString());
46#endif
47 if (value.isEmpty()) {
48 continue;
49 }
50 QUrl url = QUrl::fromUserInput(value);
51
52 if (url.isLocalFile()) {
53 QString localFilePath = url.toLocalFile();
54 QFileInfo fileInfo = QFileInfo(localFilePath);
55
56 // Don't restore if file doesn't exist anymore
57 if (!fileInfo.exists()) {
58 continue;
59 }
60
61 // When KConfigGroup substitutes $HOME, it may produce a path
62 // with two consecutive forward slashes. This may cause duplicated
63 // entries of the same file when opened using the file selector,
64 // in which the path does not include this double slash.
65 // `absoluteFilePath` replaces the double slash to a single slash.
66 value = fileInfo.absoluteFilePath();
67 url = QUrl::fromLocalFile(value);
68 }
69
70 // Don't restore where the url is already known (eg. broken config)
71 if (m_d->containsUrl(url)) {
72 continue;
73 }
74
75#ifdef Q_OS_WIN
76 // convert to backslashes
77 if (url.isLocalFile()) {
78 value = QDir::toNativeSeparators(value);
79 }
80#endif
81
82 const QString nameKey = QString("Name%1").arg(i+1);
83 const QString nameValue = cg.readEntry(nameKey, url.fileName());
85 url, // m_url
86 nameValue, // m_displayName
87 });
88 }
89
90 Q_EMIT listRenewed();
91}
92
93void KisRecentFilesManager::saveEntries(const KConfigGroup &_cg)
94{
95 KConfigGroup cg = _cg;
96 if (cg.name().isEmpty()) {
97 cg = KConfigGroup(cg.config(), "RecentFiles");
98 }
99
100 cg.deleteGroup();
101
102 cg.writeEntry("maxRecentFileItems", m_d->m_maxItems);
103
104 // write file list
105 for (int i = 0; i < m_d->m_entries.count(); ++i) {
106 const QString key = QString("File%1").arg(i+1);
107 QString value;
108#ifdef Q_OS_ANDROID
109 value = m_d->m_entries[i].m_url.toDisplayString();
110 cg.writeEntry(key, value);
111#else
112 value = m_d->m_entries[i].m_url.toDisplayString(QUrl::PreferLocalFile);
113 cg.writePathEntry(key, value);
114#endif
115 const QString nameKey = QString("Name%1").arg(i+1);
116 const QString nameValue = m_d->m_entries[i].m_displayName;
117 cg.writeEntry(nameKey, nameValue);
118 }
119}
120
121void KisRecentFilesManager::add(const QUrl &url)
122{
123 const QString name; // Dummy
124
125 if (m_d->m_maxItems <= 0) {
126 return;
127 }
128
129 if (url.isLocalFile() && url.toLocalFile().startsWith(QDir::tempPath())) {
130 return;
131 }
132 const QString tmpName = name.isEmpty() ? url.fileName() : name;
133 const QString pathOrUrl(url.toDisplayString(QUrl::PreferLocalFile));
134
135#ifdef Q_OS_WIN
136 const QString file = url.isLocalFile() ? QDir::toNativeSeparators(pathOrUrl) : pathOrUrl;
137#else
138 const QString file = pathOrUrl;
139#endif
140
141 // remove file if already in list
142 {
143 int removeIndex = m_d->indexOfUrl(url);
144 if (removeIndex >= 0) {
145 m_d->m_entries.removeAt(removeIndex);
146 Q_EMIT fileRemoved(url);
147 }
148 }
149
150 // remove oldest item if already maxitems in list
151 if (m_d->m_entries.count() >= m_d->m_maxItems) {
152 // remove oldest added item
153 m_d->m_entries.removeFirst();
154 }
155
157 url, // m_url
158 tmpName, // m_displayName
159 });
160 Q_EMIT fileAdded(url);
162}
float value(const T *src, size_t ch)
int indexOfUrl(const QUrl &url) const
QVector< KisRecentFilesEntry > m_entries
bool containsUrl(const QUrl &url) const
void loadEntries(const KConfigGroup &config)
void fileAdded(const QUrl &url)
void fileRemoved(const QUrl &url)
void saveEntries(const KConfigGroup &config)