Commit da854c94 authored by Alberto Mardegan's avatar Alberto Mardegan

Add Settings class

parent a0a0e6c8
/*
* Copyright (C) 2015 Alberto Mardegan <mardy@users.sourceforge.net>
*
* This file is part of Imaginario.
*
* Imaginario is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Imaginario 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Imaginario. If not, see <http://www.gnu.org/licenses/>.
*/
#include "settings.h"
#include <QDebug>
#include <QSettings>
#include <QStandardPaths>
using namespace Imaginario;
namespace Imaginario {
const QString keyEmbed = QStringLiteral("Embed");
const QString keyCopyFiles = QStringLiteral("CopyFiles");
const QString keyPhotoDir = QStringLiteral("PhotoDir");
const QString keyImportTags = QStringLiteral("ImportTags");
const QString keyImportParentTag = QStringLiteral("ImportParentTag");
class SettingsPrivate
{
Q_DECLARE_PUBLIC(Settings)
public:
SettingsPrivate(Settings *q);
~SettingsPrivate();
private:
QSettings m_settings;
mutable Settings *q_ptr;
};
} // namespace
SettingsPrivate::SettingsPrivate(Settings *q):
q_ptr(q)
{
}
SettingsPrivate::~SettingsPrivate()
{
}
Settings::Settings(QObject *parent):
QObject(parent),
d_ptr(new SettingsPrivate(this))
{
}
Settings::~Settings()
{
delete d_ptr;
}
void Settings::setEmbed(bool embed)
{
Q_D(Settings);
d->m_settings.setValue(keyEmbed, embed);
Q_EMIT embedChanged();
}
bool Settings::embed() const
{
Q_D(const Settings);
return d->m_settings.value(keyEmbed, false).toBool();
}
void Settings::setCopyFiles(bool copyFiles)
{
Q_D(Settings);
d->m_settings.setValue(keyCopyFiles, copyFiles);
Q_EMIT copyFilesChanged();
}
bool Settings::copyFiles() const
{
Q_D(const Settings);
return d->m_settings.value(keyCopyFiles, true).toBool();
}
void Settings::setPhotoDir(const QString &photoDir)
{
Q_D(Settings);
d->m_settings.setValue(keyPhotoDir, photoDir);
Q_EMIT photoDirChanged();
}
QString Settings::photoDir() const
{
Q_D(const Settings);
QString defaultPhotoDir =
QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
return d->m_settings.value(keyPhotoDir, defaultPhotoDir).toString();
}
void Settings::setImportTags(bool importTags)
{
Q_D(Settings);
d->m_settings.setValue(keyImportTags, importTags);
Q_EMIT importTagsChanged();
}
bool Settings::importTags() const
{
Q_D(const Settings);
return d->m_settings.value(keyImportTags, false).toBool();
}
/*
* Copyright (C) 2015 Alberto Mardegan <mardy@users.sourceforge.net>
*
* This file is part of Imaginario.
*
* Imaginario is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Imaginario 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Imaginario. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef IMAGINARIO_SETTINGS_H
#define IMAGINARIO_SETTINGS_H
#include "types.h"
#include <QObject>
#include <QString>
namespace Imaginario {
class SettingsPrivate;
class Settings: public QObject
{
Q_OBJECT
Q_PROPERTY(bool embed READ embed WRITE setEmbed NOTIFY embedChanged)
Q_PROPERTY(bool copyFiles READ copyFiles WRITE setCopyFiles \
NOTIFY copyFilesChanged)
Q_PROPERTY(QString photoDir READ photoDir WRITE setPhotoDir \
NOTIFY photoDirChanged)
Q_PROPERTY(bool importTags READ importTags WRITE setImportTags \
NOTIFY importTagsChanged)
public:
Settings(QObject *parent = 0);
~Settings();
void setEmbed(bool embed);
bool embed() const;
void setCopyFiles(bool copyFiles);
bool copyFiles() const;
void setPhotoDir(const QString &photoDir);
QString photoDir() const;
void setImportTags(bool importTags);
bool importTags() const;
Q_SIGNALS:
void embedChanged();
void copyFilesChanged();
void photoDirChanged();
void importTagsChanged();
private:
SettingsPrivate *d_ptr;
Q_DECLARE_PRIVATE(Settings)
};
} // namespace
#endif // IMAGINARIO_SETTINGS_H
......@@ -2,5 +2,6 @@
/tst_importer
/tst_metadata
/tst_photo_model
/tst_settings
/tst_tag_model
/tst_writer
......@@ -4,5 +4,6 @@ SUBDIRS = \
tst_importer.pro \
tst_metadata.pro \
tst_photo_model.pro \
tst_settings.pro \
tst_tag_model.pro \
tst_writer.pro
/*
* Copyright (C) 2015 Alberto Mardegan <mardy@users.sourceforge.net>
*
* This file is part of Imaginario.
*
* Imaginario is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Imaginario 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Imaginario. If not, see <http://www.gnu.org/licenses/>.
*/
#include "settings.h"
#include <QTemporaryDir>
#include <QTest>
using namespace Imaginario;
class SettingsTest: public QObject
{
Q_OBJECT
private Q_SLOTS:
void testProperties_data();
void testProperties();
};
void SettingsTest::testProperties_data()
{
QTest::addColumn<bool>("embed");
QTest::addColumn<bool>("copyFiles");
QTest::addColumn<QString>("photoDir");
QTest::addColumn<bool>("importTags");
QTest::newRow("false and nulls") <<
false << false << QString() << false;
QTest::newRow("trues and valid") <<
true << true << "/tmp/foo" << true;
QTest::newRow("mixed") <<
false << true << "/tmp/bar" << false;
QTest::newRow("mixed 2") <<
true << false << "" << true;
QTest::newRow("mixed 3") <<
true << false << "/home/dir/bar" << false;
}
void SettingsTest::testProperties()
{
QFETCH(bool, embed);
QFETCH(bool, copyFiles);
QFETCH(QString, photoDir);
QFETCH(bool, importTags);
QTemporaryDir tmpDir;
qputenv("XDG_CONFIG_HOME", tmpDir.path().toUtf8());
Settings settings;
settings.setEmbed(embed);
settings.setCopyFiles(copyFiles);
settings.setPhotoDir(photoDir);
settings.setImportTags(importTags);
QCOMPARE(settings.embed(), embed);
QCOMPARE(settings.copyFiles(), copyFiles);
QCOMPARE(settings.photoDir(), photoDir);
QCOMPARE(settings.importTags(), importTags);
}
QTEST_MAIN(SettingsTest)
#include "tst_settings.moc"
TARGET = tst_settings
include(tests.pri)
QT += \
qml
SOURCES += \
$${SRC_DIR}/settings.cpp \
tst_settings.cpp
HEADERS += \
$${SRC_DIR}/settings.h
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment