generic: implement KSettings class

Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
This commit is contained in:
Ivailo Monev 2019-07-12 14:09:24 +00:00
parent aea83f844f
commit c2f8387fa6
4 changed files with 161 additions and 2 deletions

View file

@ -140,6 +140,7 @@ set(kdecore_LIB_SRCS
config/kdesktopfile.cpp
config/ksharedconfig.cpp
config/kcoreconfigskeleton.cpp
config/ksettings.cpp
date/kcalendarera.cpp
date/kcalendarsystem.cpp
date/kcalendarsystemcoptic.cpp
@ -379,6 +380,7 @@ install(
config/kdesktopfile.h
config/ksharedconfig.h
config/kcoreconfigskeleton.h
config/ksettings.h
date/kcalendarsystem.h
date/kdatetime.h
date/klocalizeddate.h

View file

@ -0,0 +1,65 @@
/*
This file is part of the KDE libraries
Copyright (C) 2019 Ivailo Monev <xakepa10@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "ksettings.h"
#include "kstandarddirs.h"
#include <QApplication>
#include <QFile>
class KSettingsPrivate {
public:
KSettings::OpenFlags m_mode;
};
static QString getSettingsPath(const QString &filename)
{
if (filename.isEmpty()) {
return KStandardDirs::locateLocal("config", QApplication::applicationName());
} else if (QFile::exists(filename)) {
return filename;
}
return KStandardDirs::locateLocal("config", filename);
}
KSettings::KSettings(const QString& file, const OpenFlags mode)
: d_ptr(new KSettingsPrivate()), QSettings(getSettingsPath(file), QSettings::NativeFormat)
{
d_ptr->m_mode = mode;
if ((mode & IncludeGlobals) != mode) {
addSource(KStandardDirs::locateLocal("config", QLatin1String("kdeglobals")));
}
}
KSettings::~KSettings()
{
delete d_ptr;
}
void KSettings::addSource(const QString &source)
{
QSettings settings(source, QSettings::NativeFormat);
foreach (const QString &key, settings.allKeys()) {
if (!QSettings::contains(key)) {
QSettings::setValue(key, settings.value(key));
}
}
}

View file

@ -0,0 +1,91 @@
/*
This file is part of the KDE libraries
Copyright (C) 2019 Ivailo Monev <xakepa10@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef KSETTINGS_H
#define KSETTINGS_H
#include "kdecore_export.h"
#include <QSettings>
#include <QStringList>
class KSettingsPrivate;
/**
* \class KSettings ksettings.h <KSettings>
*
* \brief The preferred class of the KDE configuration data system.
*/
class KDECORE_EXPORT KSettings : public QSettings
{
Q_OBJECT
public:
/**
* Determines how the system-wide and user's global settings will affect
* the reading of the configuration.
*
* IncludeGlobals does the same, but with the global settings sources.
*
* Note that the main configuration source overrides the global sources,
*/
enum OpenFlag {
SimpleConfig = 0x00, ///< Just a single config file.
IncludeGlobals = 0x01, ///< Blend kdeglobals into the config object.
FullConfig = SimpleConfig|IncludeGlobals ///< Fully-fledged config, including globals
};
Q_DECLARE_FLAGS(OpenFlags, OpenFlag)
/**
* Creates a KSettings object to manipulate a configuration file
*
* @param file If an absolute path is specified for @p file, that file will be used
* as the store for the configuration settings. If a non-absolute path
* is provided, the file will be looked for in the standard config
* directory. If path is empty the application name is used instead.
*
* @param mode determines whether the user or global settings will be allowed
* to influence the values returned by this object. See OpenFlags for
* more details.
*/
explicit KSettings(const QString& file = QString(), const OpenFlags mode = FullConfig);
~KSettings();
/**
* Adds configuration source to the merge stack.
*
* Currently only files are accepted as configuration sources.
*
* Ssource will be merged into the current stack not overriding from the
* current settings stack thus order is important.
*
* @param source Extra config source.
*/
void addSource(const QString &source);
private:
KSettingsPrivate *d_ptr;
Q_DISABLE_COPY(KSettings)
Q_DECLARE_PRIVATE(KSettings)
};
Q_DECLARE_OPERATORS_FOR_FLAGS(KSettings::OpenFlags)
#endif // KSETTINGS_H

View file

@ -18,6 +18,7 @@
#include "kdebug.h"
#include "klocale.h"
#include "ksettings.h"
#include "kmediaplayer.h"
#include <QApplication>
@ -191,7 +192,7 @@ public:
mpv_handle *m_handle;
#endif
QString m_appname;
QSettings *m_settings;
KSettings *m_settings;
// the handle pointer is not NULL-ed once mpv_terminate_destroy() has been
// called, doing it manually is a race because _processHandleEvents() is
// called asynchronous
@ -200,7 +201,7 @@ public:
KAbstractPlayerPrivate::KAbstractPlayerPrivate()
: m_appname(QApplication::applicationName()),
m_settings(new QSettings("KMediaPlayer", "kmediaplayer"))
m_settings(new KSettings())
{
kDebug() << i18n("initializing player");
m_stopprocessing = false;