Chris@148: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@148: Chris@148: /* Chris@148: Sonic Visualiser Chris@148: An audio file viewer and annotation editor. Chris@148: Centre for Digital Music, Queen Mary, University of London. Chris@148: This file copyright 2006 Chris Cannam. Chris@148: Chris@148: This program is free software; you can redistribute it and/or Chris@148: modify it under the terms of the GNU General Public License as Chris@148: published by the Free Software Foundation; either version 2 of the Chris@148: License, or (at your option) any later version. See the file Chris@148: COPYING included with this distribution for more information. Chris@148: */ Chris@148: Chris@148: #include "ConfigFile.h" Chris@148: Chris@148: #include "base/Exceptions.h" Chris@148: Chris@148: #include Chris@148: Chris@148: #include Chris@148: #include Chris@148: #include Chris@148: #include Chris@148: Chris@148: ConfigFile::ConfigFile(QString filename) : Chris@148: m_filename(filename), Chris@148: m_loaded(false), Chris@148: m_modified(false) Chris@148: { Chris@148: } Chris@148: Chris@148: ConfigFile::~ConfigFile() Chris@148: { Chris@148: try { Chris@148: commit(); Chris@148: } catch (FileOperationFailed f) { Chris@148: std::cerr << "WARNING: ConfigFile::~ConfigFile: Commit failed for " Chris@148: << m_filename.toStdString() << std::endl; Chris@148: } Chris@148: } Chris@148: Chris@148: QString Chris@148: ConfigFile::get(QString key, QString deft) Chris@148: { Chris@148: if (!m_loaded) load(); Chris@148: Chris@148: QMutexLocker locker(&m_mutex); Chris@148: Chris@148: if (m_data.find(key) == m_data.end()) return deft; Chris@148: return m_data[key]; Chris@148: } Chris@148: Chris@148: int Chris@148: ConfigFile::getInt(QString key, int deft) Chris@148: { Chris@148: return get(key, QString("%1").arg(deft)).toInt(); Chris@148: } Chris@148: Chris@148: bool Chris@148: ConfigFile::getBool(QString key, bool deft) Chris@148: { Chris@148: QString value = get(key, deft ? "true" : "false").trimmed().toLower(); Chris@148: return (value == "true" || value == "yes" || value == "on" || value == "1"); Chris@148: } Chris@148: Chris@148: float Chris@148: ConfigFile::getFloat(QString key, float deft) Chris@148: { Chris@148: return get(key, QString("%1").arg(deft)).toFloat(); Chris@148: } Chris@148: Chris@148: QStringList Chris@148: ConfigFile::getStringList(QString key) Chris@148: { Chris@148: return get(key).split('|'); Chris@148: } Chris@148: Chris@148: void Chris@148: ConfigFile::set(QString key, QString value) Chris@148: { Chris@148: if (!m_loaded) load(); Chris@148: Chris@148: QMutexLocker locker(&m_mutex); Chris@148: Chris@148: m_data[key] = value; Chris@148: Chris@148: m_modified = true; Chris@148: } Chris@148: Chris@148: void Chris@148: ConfigFile::set(QString key, int value) Chris@148: { Chris@148: set(key, QString("%1").arg(value)); Chris@148: } Chris@148: Chris@148: void Chris@148: ConfigFile::set(QString key, bool value) Chris@148: { Chris@148: set(key, value ? QString("true") : QString("false")); Chris@148: } Chris@148: Chris@148: void Chris@148: ConfigFile::set(QString key, float value) Chris@148: { Chris@148: set(key, QString("%1").arg(value)); Chris@148: } Chris@148: Chris@148: void Chris@148: ConfigFile::set(QString key, const QStringList &values) Chris@148: { Chris@148: set(key, values.join("|")); Chris@148: } Chris@148: Chris@148: void Chris@148: ConfigFile::commit() Chris@148: { Chris@148: QMutexLocker locker(&m_mutex); Chris@148: Chris@148: if (!m_modified) return; Chris@148: Chris@148: // Really we should write to another file and then move to the Chris@148: // intended target, but I don't think we're all that particular Chris@148: // about reliability here at the moment Chris@148: Chris@148: QFile file(m_filename); Chris@148: Chris@148: if (!file.open(QFile::WriteOnly | QFile::Text)) { Chris@148: throw FileOperationFailed(m_filename, "open for writing"); Chris@148: } Chris@148: Chris@148: QTextStream out(&file); Chris@148: Chris@148: for (DataMap::const_iterator i = m_data.begin(); i != m_data.end(); ++i) { Chris@148: out << i->first << "=" << i->second << endl; Chris@148: } Chris@148: Chris@148: m_modified = false; Chris@148: } Chris@148: Chris@148: bool Chris@148: ConfigFile::load() Chris@148: { Chris@148: QMutexLocker locker(&m_mutex); Chris@148: Chris@148: if (m_loaded) return true; Chris@148: Chris@148: QFile file(m_filename); Chris@148: Chris@148: if (!file.open(QFile::ReadOnly | QFile::Text)) { Chris@148: return false; Chris@148: } Chris@148: Chris@148: QTextStream in(&file); Chris@148: Chris@148: m_data.clear(); Chris@148: Chris@148: while (!in.atEnd()) { Chris@148: Chris@148: QString line = in.readLine(2048); Chris@148: QString key = line.section('=', 0, 0); Chris@148: QString value = line.section('=', 1, -1); Chris@148: if (key == "") continue; Chris@148: Chris@148: m_data[key] = value; Chris@148: } Chris@148: Chris@148: m_loaded = true; Chris@148: m_modified = false; Chris@148: return true; Chris@148: } Chris@148: Chris@148: void Chris@148: ConfigFile::reset() Chris@148: { Chris@148: QMutexLocker locker(&m_mutex); Chris@148: m_loaded = false; Chris@148: m_modified = false; Chris@148: } Chris@148: