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