KIO

krecentdirs.cpp
1 /* -*- c++ -*-
2  SPDX-FileCopyrightText: 2000 Waldo Bastian <[email protected]>
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 #if KIOFILEWIDGETS_BUILD_DEPRECATED_SINCE(5, 95)
23  qWarning() << "Using KRecentDirs with a global config file is deprecated. Remove the second colon to use the application-local file."
24  << "The requested key was" << key;
25  key.remove(0, 2);
26  cg = KConfigGroup(KSharedConfig::openConfig(QStringLiteral("krecentdirsrc")), QString());
27 #endif
28  } else {
29  key.remove(0, 1);
30  }
31 
32  result = cg.readPathEntry(key, QStringList());
33  if (result.isEmpty()) {
35  }
36  return cg;
37 }
38 
40 {
41  QString key = fileClass;
42  QStringList result;
43  recentdirs_readList(key, result).sync();
44  return result;
45 }
46 
47 QString KRecentDirs::dir(const QString &fileClass)
48 {
49  const QStringList result = list(fileClass);
50  return result[0];
51 }
52 
53 void KRecentDirs::add(const QString &fileClass, const QString &directory)
54 {
55  QString key = fileClass;
56  QStringList result;
57  KConfigGroup config = recentdirs_readList(key, result);
58  // make sure the dir is first in history
59  result.removeAll(directory);
60  result.prepend(directory);
61  if (result.size() > s_maxDirHistory) {
62  result.erase(result.begin() + s_maxDirHistory, result.end());
63  }
64 
65  config.writePathEntry(key, result);
66  config.sync();
67 }
void append(const T &value)
int removeAll(const T &value)
QString writableLocation(QStandardPaths::StandardLocation type)
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
Returns a list of directories associated with this file-class.
Definition: krecentdirs.cpp:39
KIOFILEWIDGETS_EXPORT void add(const QString &fileClass, const QString &directory)
Associates directory with fileClass.
Definition: krecentdirs.cpp:53
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
int size() const const
void prepend(const T &value)
int length() const const
bool isEmpty() const const
QString & remove(int position, int n)
KIOFILEWIDGETS_EXPORT QString dir(const QString &fileClass)
Returns the most recently used directory associated with this file-class.
Definition: krecentdirs.cpp:47
QList::iterator erase(QList::iterator pos)
QList::iterator begin()
bool sync() override
QList::iterator end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Nov 27 2022 03:56:30 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.