KIO

krecentdirs.cpp
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
13static constexpr int s_maxDirHistory = 3;
14
15static 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(0, 1);
24 }
25
26 result = cg.readPathEntry(key, QStringList());
27 if (result.isEmpty()) {
29 }
30 return cg;
31}
32
34{
35 QString key = fileClass;
36 QStringList result;
37 recentdirs_readList(key, result).sync();
38 return result;
39}
40
42{
43 const QStringList result = list(fileClass);
44 return result[0];
45}
46
47void 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(directory);
54 result.prepend(directory);
55 if (result.size() > s_maxDirHistory) {
56 result.erase(result.begin() + s_maxDirHistory, result.end());
57 }
58
59 config.writePathEntry(key, result);
60 config.sync();
61}
void writePathEntry(const char *Key, const QString &path, WriteConfigFlags pFlags=Normal)
bool sync() override
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
KIOCORE_EXPORT QString dir(const QString &fileClass)
Returns the most recently used directory associated with this file-class.
KIOCORE_EXPORT QStringList list(const QString &fileClass)
Returns a list of directories associated with this file-class.
KIOCORE_EXPORT void add(const QString &fileClass, const QString &directory)
Associates directory with fileClass.
void append(QList< T > &&value)
iterator begin()
iterator end()
iterator erase(const_iterator begin, const_iterator end)
bool isEmpty() const const
void prepend(parameter_type value)
qsizetype removeAll(const AT &t)
qsizetype size() const const
QString writableLocation(StandardLocation type)
qsizetype length() const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:27 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.