Skip to content

Commit

Permalink
Rewrite settings from settings.d only if there are new changes; move …
Browse files Browse the repository at this point in the history
…board mappings to settings.d
  • Loading branch information
Bernd Wachter committed Mar 19, 2013
1 parent 1b138e8 commit 9c87541
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
2 changes: 2 additions & 0 deletions constants.h
Expand Up @@ -18,6 +18,8 @@
#define SSU_DEFAULT_CONFIGURATION "/usr/share/ssu/ssu-defaults.ini"
/// Path to board / device family mappings file
#define SSU_BOARD_MAPPING_CONFIGURATION "/usr/share/ssu/board-mappings.ini"
/// Path to config.d for board mappings
#define SSU_BOARD_MAPPING_CONFIGURATION_DIR "/usr/share/ssu/board-mappings.d"
/// The SSU protocol version used by the ssu client libraries
#define SSU_PROTOCOL_VERSION "1"
#endif
3 changes: 2 additions & 1 deletion libssu/ssudeviceinfo.cpp
Expand Up @@ -10,13 +10,14 @@
#include <QDir>

#include "ssudeviceinfo.h"

#include "../constants.h"

QTM_USE_NAMESPACE

SsuDeviceInfo::SsuDeviceInfo(): QObject(){

boardMappings = new QSettings(SSU_BOARD_MAPPING_CONFIGURATION, QSettings::IniFormat);
boardMappings = new SsuSettings(SSU_BOARD_MAPPING_CONFIGURATION, SSU_BOARD_MAPPING_CONFIGURATION_DIR);
}

QString SsuDeviceInfo::deviceFamily(){
Expand Down
4 changes: 3 additions & 1 deletion libssu/ssudeviceinfo.h
Expand Up @@ -11,6 +11,8 @@
#include <QObject>
#include <QSettings>

#include <ssusettings.h>

class SsuDeviceInfo: public QObject {
Q_OBJECT

Expand All @@ -37,7 +39,7 @@ class SsuDeviceInfo: public QObject {
bool getValue(const QString& key, QString& value);

private:
QSettings *boardMappings;
SsuSettings *boardMappings;
QString cachedFamily, cachedModel, cachedVariant;
};
#endif
42 changes: 40 additions & 2 deletions libssu/ssusettings.cpp
Expand Up @@ -7,6 +7,8 @@

#include <QStringList>
#include <QDirIterator>
#include <QFileInfo>
#include <QDateTime>

#include "ssusettings.h"
#include "ssulog.h"
Expand All @@ -25,16 +27,52 @@ SsuSettings::SsuSettings(const QString &fileName, Format format, const QString &
upgrade();
}

SsuSettings::SsuSettings(const QString &fileName, const QString &settingsDirectory, QObject *parent):
QSettings(fileName, QSettings::IniFormat, parent){
settingsd = settingsDirectory;
merge();
}

void SsuSettings::merge(){
if (settingsd == "")
return;

bool skipMerge = true;

SsuLog *ssuLog = SsuLog::instance();

QDirIterator it(settingsd, QDirIterator::FollowSymlinks);
// TODO: only rewrite the settings file if something has changed
QStringList settingsFiles;

QFileInfo oldSettingsInfo(fileName());

while (it.hasNext()){
QString f = it.next();
QSettings settings(f, QSettings::IniFormat);

if (it.fileName() == "." || it.fileName() == "..") continue;

settingsFiles.append(it.filePath());

QFileInfo info(it.filePath());
if (info.lastModified() >= oldSettingsInfo.lastModified())
skipMerge = false;
}

if (skipMerge){
ssuLog->print(LOG_DEBUG, QString("Configuration file is newer than all config.d files, skipping merge"));
return;
}

settingsFiles.sort();

foreach (const QString &settingsFile, settingsFiles){
QSettings settings(settingsFile, QSettings::IniFormat);
QStringList groups = settings.childGroups();

ssuLog->print(LOG_DEBUG, QString("Merging %1 into %2")
.arg(settingsFile)
.arg(fileName()));

foreach (const QString &group, groups){
beginGroup(group);
settings.beginGroup(group);
Expand Down
9 changes: 9 additions & 0 deletions libssu/ssusettings.h
Expand Up @@ -16,7 +16,16 @@ class SsuSettings: public QSettings {
public:
SsuSettings();
SsuSettings(const QString &fileName, Format format, QObject *parent=0);
/**
* Initialize the settings object with a defaults settings file, resulting in
* update to the configuration file if needed
*/
SsuSettings(const QString &fileName, Format format, const QString &defaultFileName, QObject *parent=0);
/**
* Initialize the settings object from a settings.d structure, if needed. Only INI
* style settings are supported in this mode.
*/
SsuSettings(const QString &fileName, const QString &settingsDirectory, QObject *parent=0);

private:
QString defaultSettingsFile, settingsd;
Expand Down

0 comments on commit 9c87541

Please sign in to comment.