src/config.cpp

Wed, 21 Aug 2013 01:13:52 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Wed, 21 Aug 2013 01:13:52 +0300
changeset 467
59549731151a
parent 465
0f67ff766458
child 468
7b3159f1a9e2
permissions
-rw-r--r--

Added ListConfig type, using it in recent files now

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright (C) 2013 Santeri Piippo
 *
 *  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 <http://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include <QDir>
#include <QTextStream>
#include <QSettings>
#include "common.h"
#include "config.h"
#include "misc.h"
#include "gui.h"
#include "file.h"

Config* g_configPointers[MAX_CONFIG];
static ushort g_cfgPointerCursor = 0;

// =============================================================================
// -----------------------------------------------------------------------------
static QSettings* getSettingsObject() {
#ifdef PORTABLE
# ifdef _WIN32
#  define EXTENSION ".ini"
# else
#  define EXTENSION ".cfg"
# endif // _WIN32
	return new QSettings (str (APPNAME).toLower() + EXTENSION, QSettings::IniFormat);
#else
	return new QSettings;
#endif // PORTABLE
}

Config::Config (const char* name, const char* defstring) :
	name (name), m_defstring (defstring) {}

// =============================================================================
// -----------------------------------------------------------------------------
// Load the configuration from file
bool Config::load() {
	QSettings* settings = getSettingsObject();
	print ("config::load: Loading configuration file from %1...\n", settings->fileName());
	
	for (Config* cfg : g_configPointers) {
		if (!cfg)
			break;
		
		cfg->loadFromConfig (settings);
	}
	
	settings->deleteLater();
	return true;
}

// =============================================================================
// -----------------------------------------------------------------------------
void IntConfig::loadFromConfig (const QSettings* cfg) {
	QVariant val = cfg->value (name, str::number (defval));
	value = val.toInt();
}

// =============================================================================
// -----------------------------------------------------------------------------
void FloatConfig::loadFromConfig (const QSettings* cfg) {
	QVariant val = cfg->value (name, str::number (defval));
	value = val.toFloat();
}

// =============================================================================
// -----------------------------------------------------------------------------
void StringConfig::loadFromConfig (const QSettings* cfg) {
	QVariant val = cfg->value (name, defval);
	value = val.toString();
}

// =============================================================================
// -----------------------------------------------------------------------------
void BoolConfig::loadFromConfig (const QSettings* cfg) {
	QVariant val = cfg->value (name, str::number (defval));
	value = val.toBool();
}

// =============================================================================
// -----------------------------------------------------------------------------
void KeySequenceConfig::loadFromConfig (const QSettings* cfg) {
	QVariant val = cfg->value (name, defval.toString());
	value = QKeySequence (val.toString());
}

// =============================================================================
// -----------------------------------------------------------------------------
void ListConfig::loadFromConfig (const QSettings* cfg) {
	QVariant val = cfg->value (name, defval);
	value = val.toList();
}

// =============================================================================
// -----------------------------------------------------------------------------
// Save the configuration to disk
bool Config::save() {
	QSettings* settings = getSettingsObject();
	print ("Saving configuration to %1...\n", settings->fileName());
	
	for (Config* cfg : g_configPointers) {
		if (!cfg)
			break;
		
		settings->setValue (cfg->name, cfg->toVariant());
	}
	
	settings->sync();
	settings->deleteLater();
	return true;
}

// =============================================================================
// -----------------------------------------------------------------------------
void Config::reset() {
	for (Config* cfg : g_configPointers) {
		if (!cfg)
			break;
		
		cfg->resetValue();
	}
}

// =============================================================================
// -----------------------------------------------------------------------------
str Config::filepath (str file) {
	return Config::dirpath() + DIRSLASH + file;
}

// =============================================================================
// -----------------------------------------------------------------------------
str Config::dirpath() {
	QSettings* cfg = getSettingsObject();
	return dirname (cfg->fileName());
}

// =============================================================================
// -----------------------------------------------------------------------------
str Config::defaultString() const {
	str defstring = m_defstring;
	
	// String types inevitably get extra quotes in their default string due to
	// preprocessing stuff. We can only remove them now...
	if (getType() == String) {
		defstring.remove (0, 1);
		defstring.chop (1);
	}
	
	return defstring;
}

// =============================================================================
// -----------------------------------------------------------------------------
void Config::addToArray (Config* ptr) {
	if (g_cfgPointerCursor == 0)
		memset (g_configPointers, 0, sizeof g_configPointers);
	
	assert (g_cfgPointerCursor < MAX_CONFIG);
	g_configPointers[g_cfgPointerCursor++] = ptr;
}

mercurial