1 | /* -*- c++ -*- |
2 | SPDX-FileCopyrightText: 2000 Waldo Bastian <bastian@kde.org> |
3 | |
4 | SPDX-License-Identifier: BSD-2-Clause |
5 | */ |
6 | |
7 | #include "krecentdirs.h" |
8 | #include <KConfig> |
9 | #include <KConfigGroup> |
10 | #include <KSharedConfig> |
11 | #include <QDebug> |
12 | |
13 | static constexpr int s_maxDirHistory = 3; |
14 | |
15 | static KConfigGroup recentdirs_readList(QString &key, QStringList &result) |
16 | { |
17 | KConfigGroup cg(KSharedConfig::openConfig(), QStringLiteral("Recent Dirs" )); |
18 | if ((key.length() < 2) || (key[0] != QLatin1Char(':'))) { |
19 | key = QStringLiteral(":default" ); |
20 | } |
21 | if (key[1] == QLatin1Char(':')) { |
22 | } else { |
23 | key.remove(i: 0, len: 1); |
24 | } |
25 | |
26 | result = cg.readPathEntry(pKey: key, aDefault: QStringList()); |
27 | if (result.isEmpty()) { |
28 | result.append(t: QStandardPaths::writableLocation(type: QStandardPaths::DocumentsLocation)); |
29 | } |
30 | return cg; |
31 | } |
32 | |
33 | QStringList KRecentDirs::list(const QString &fileClass) |
34 | { |
35 | QString key = fileClass; |
36 | QStringList result; |
37 | recentdirs_readList(key, result).sync(); |
38 | return result; |
39 | } |
40 | |
41 | QString KRecentDirs::dir(const QString &fileClass) |
42 | { |
43 | const QStringList result = list(fileClass); |
44 | return result[0]; |
45 | } |
46 | |
47 | void KRecentDirs::add(const QString &fileClass, const QString &directory) |
48 | { |
49 | QString key = fileClass; |
50 | QStringList result; |
51 | KConfigGroup config = recentdirs_readList(key, result); |
52 | // make sure the dir is first in history |
53 | result.removeAll(t: directory); |
54 | result.prepend(t: directory); |
55 | if (result.size() > s_maxDirHistory) { |
56 | result.erase(abegin: result.begin() + s_maxDirHistory, aend: result.end()); |
57 | } |
58 | |
59 | config.writePathEntry(pKey: key, value: result); |
60 | config.sync(); |
61 | } |
62 | |