#include "ApplicationConfigurator.h"

#include <QCoreApplication>
#include <QTextCodec>
#include <QFile>
#include <QDir>
#include <QDebug>

ApplicationConfigurator::ApplicationConfigurator() :
    iniSettings(0)
{

}

ApplicationConfigurator::~ApplicationConfigurator()
{
    if(iniSettings)
    {
        delete iniSettings;
    }
}


/**
 * @brief ApplicationConfigurator::init 初始化配置文件并加载配置参数
 */
void ApplicationConfigurator::init()
{
    QString filename = QCoreApplication::applicationDirPath() + "\\config.ini";
    iniSettings = new QSettings(filename, QSettings::IniFormat);
    iniSettings->setIniCodec(QTextCodec::codecForName("System"));

    // 数据库连接参数
    dbName = iniSettings->value("Database/dbName", "").toString();
	dbPort = iniSettings->value("Database/dbPort", 1521).toUInt();
    dbHostName = iniSettings->value("Database/dbHostName", "").toString();
    dbUserName = iniSettings->value("Database/dbUserName", "").toString();
    dbPassword = iniSettings->value("Database/dbPassword", "").toString();

}

/**
 * @brief ApplicationConfigurator::save 保存配置参数到配置文件
 */
void ApplicationConfigurator::save()
{
    // 数据库连接参数
    iniSettings->beginGroup("Database");

    iniSettings->setValue("dbName", dbName);
	iniSettings->setValue("dbPort", dbPort);
    iniSettings->setValue("dbHostName", dbHostName);
    iniSettings->setValue("dbUserName", dbUserName);
    iniSettings->setValue("dbPassword", dbPassword);

	iniSettings->endGroup();
}


