#include "qgsettings.h"

#include <QCoreApplication>
#include <QStandardPaths>
#include <QFile>

#include <QDebug>

static QString appIni("wb.ini");

static QString fixFilename(const QString& fname, QgSettings::IniScope scope)
{
    QString tmp=fname.isEmpty() ? appIni : fname;

    if (scope==QgSettings::IniUserScope)
    {
        QStringList lst=QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
        if (lst.isEmpty() || lst.at(0).isEmpty())
            return "./"+tmp;
        else
            return lst.at(0)+"/"+tmp;
    }
    else
        return "./"+tmp;
}

//QgSettings::QgSettings(QObject *parent):
//  QSettings(parent)
//{
//}

QgSettings::QgSettings(const QString& filename, IniScope iniScope, QObject* parent):
    QSettings(fixFilename(filename, iniScope), QSettings::IniFormat, parent)
{
    QFile tmp(fileName());
    bool exist=tmp.exists();
    qDebug()<<"QgSettings:"<<fileName()<<(exist ? "present" : "not present");
}

void QgSettings::setOrganizationName(const QString& str)
{
    QCoreApplication::setOrganizationName(str);
}

void QgSettings::setOrganizationDomain(const QString& str)
{
    QCoreApplication::setOrganizationDomain(str);
}

void QgSettings::setApplicationName(const QString& str)
{
    QCoreApplication::setApplicationName(str);
}

void QgSettings::setApplicationIni(const QString& fname)
{
    appIni=fname;
}
