#include "app_settings.h"
#include <QDir>
#include <QtGui/QApplication>
//////////////////////////////////////////////////////////////////////////
const char kDeviceInfoGroup[] = "DeviceInfo";

const char kDeviceIPKey[] = "DeviceIP";
const char kDeviceIPDefault[] = "192.168.0.99";

const char kDevicePortKey[] = "DevicePort";
const int  kDevicePortDefault = 80;

const char kRtspPortKey[] = "RtspPort";
const int  kRtspPortDefault = 554;

const char kDeviceUsernameKey[] = "DeviceUsername";
const char kDeviceUsernameDefault[] = "admin";

const char kDevicePasswordKey[] = "DevicePassword";
const char kDevicePasswordDefault[] = "admin";

const char kDeviceTypeKey[] = "DeviceType";
const int  kDeviceTypeDefault = 0;
//////////////////////////////////////////////////////////////////////////

AppSettings::AppSettings(void)
{
    QString path;    
    path = QCoreApplication::applicationDirPath();
    path.append("//config.ini");
    settings_ = new QSettings(path,QSettings::IniFormat);
    //settings_ = new QSettings(QSettings::IniFormat,QSettings::UserScope,"huisun","demo");
}

AppSettings::~AppSettings(void)
{
    delete settings_;
}

void AppSettings::GetDeviceInfo( DeviceInfo* device_info )
{
    settings_->beginGroup(kDeviceInfoGroup);
    device_info->device_ip = settings_->value(kDeviceIPKey,kDeviceIPDefault).toString();
    device_info->device_port = settings_->value(kDevicePortKey,kDevicePortDefault).toUInt();
    device_info->device_username = settings_->value(kDeviceUsernameKey,kDeviceUsernameDefault).toString();
    device_info->device_password = settings_->value(kDevicePasswordKey,kDevicePasswordDefault).toString();
    device_info->device_rtspport = settings_->value(kRtspPortKey,kRtspPortDefault).toUInt();
    device_info->device_type = settings_->value(kDeviceTypeKey,kDeviceTypeDefault).toUInt();
    settings_->endGroup();
}

void AppSettings::SetDeviceInfo( const DeviceInfo& device_info )
{
    settings_->beginGroup(kDeviceInfoGroup);
    settings_->setValue(kDeviceIPKey,device_info.device_ip);
    settings_->setValue(kDevicePortKey,device_info.device_port);
    settings_->setValue(kRtspPortKey,device_info.device_rtspport);
    settings_->setValue(kDeviceUsernameKey,device_info.device_username);
    settings_->setValue(kDevicePasswordKey,device_info.device_password);
    settings_->setValue(kDeviceTypeKey,device_info.device_type);
    settings_->endGroup();
}

int AppSettings::GetTestInterval()
{
    settings_->beginGroup("test");
    int rv = settings_->value("interval",10).toUInt();
    settings_->endGroup();
    return rv;
}
