/* * Copyright 2008-2009 Ben Boeckel * * This program 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. * * This program 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 this program. If not, see . */ // Header include #include "Config.h" // Qt includes #include using namespace Sigscript; Config::Config(Config* parent) : QObject(parent), m_lock(QReadWriteLock::Recursive), m_parent(parent) { } bool Config::addValue(const QString& name, const QVariant& value) { if (!m_values.contains(name) || (m_values[name].second & Deleted)) return setValue(name, value); return false; } bool Config::setValue(const QString& name, const QVariant& value) { QWriteLocker locker(&m_lock); if (m_values.contains(name) && (m_values[name].second & ReadOnly)) return false; if (!m_values.contains(name) || (m_values[name].second & Deleted)) { m_values[name].second &= ~Deleted; emit(valueAdded(name, value)); } m_values[name] = Value(value, 0); emit(valueChanged(name, value)); return true; } bool Config::removeValue(const QString& name, const bool shadow) { QWriteLocker locker(&m_lock); if (shadow) setOptions(name, Deleted); else if (m_values.contains(name)) m_values.remove(name); else return false; emit(valueRemoved(name)); return true; } QVariant Config::value(const QString& name, const bool recursive) const { QReadLocker locker(&m_lock); if (m_values.contains(name)) { if (m_values[name].second & (Deleted | Hidden)) return QVariant(); return m_values[name].first; } if (recursive && m_parent) return m_parent->value(name, true); return QVariant(); } bool Config::hasValue(const QString& name, const bool recursive) const { QReadLocker locker(&m_lock); if (m_values.contains(name)) return !(m_values[name].second & (Deleted | Hidden)); if (recursive && m_parent) return m_parent->hasValue(name, true); return false; } QStringList Config::values(const bool recursive) const { QReadLocker locker(&m_lock); QStringList values = m_values.keys(); if (recursive && m_parent) values += m_parent->values(true); for (QMutableStringListIterator i(values); i.hasNext(); i.next()) { if (m_values.contains(i.value()) && (m_values[i.value()].second & (Deleted | Hidden))) i.remove(); } return values.toSet().toList(); } bool Config::setOptions(const QString& name, const Options options) { QWriteLocker locker(&m_lock); if (m_values.contains(name) && ((~m_values[name].second) & options)) { m_values[name].second |= options; emit(optionsChanged(name, options)); return true; } return false; } bool Config::unsetOptions(const QString& name, const Options options) { QWriteLocker locker(&m_lock); if (m_values.contains(name) && (m_values[name].second & options)) { m_values[name].second &= ~options; emit(optionsChanged(name, options)); return true; } return false; } Config::Options Config::options(const QString& name, const bool recursive) const { QReadLocker locker(&m_lock); if (m_values.contains(name)) return m_values[name].second; if (recursive && m_parent) return m_parent->options(name, recursive); return 0; } void Config::clean() { // QWriteLocker locker(&m_lock); for (QMutableMapIterator i(m_values); i.hasNext(); i.next()) { unsetOptions(i.key(), Hidden); if (i.value().second & Temporary) removeValue(i.key(), false); } } void Config::writeBack() { QWriteLocker locker(&m_lock); }