#include "gsettingmanager.h"

/* qt会将glib里的signals成员识别为宏，所以取消该宏
 * 后面如果用到signals时，使用Q_SIGNALS代替即可
 **/
#ifdef signals
#undef signals
#endif

extern "C" {
#include <glib.h>
#include <gio/gio.h>
#include <dconf/dconf.h>
}

//! 控制中心-个性化部分
Q_GLOBAL_STATIC(ControlCenterPersonGsetting, controlCenterPersonGsetting)

ControlCenterPersonGsetting::ControlCenterPersonGsetting()
{
    const QByteArray id(CONTROL_CENTER_PERSONALISE_SETTING);
    if (QGSettings::isSchemaInstalled(id)) {
        m_pControlCenterSetting = new QGSettings(id);
        connect(m_pControlCenterSetting, &QGSettings::changed, this, [=](QString key) {
            emit Sig_personChanged(key);
        });
    }
}

ControlCenterPersonGsetting::~ControlCenterPersonGsetting()
{
    if (m_pControlCenterSetting != nullptr) {
        delete  m_pControlCenterSetting;
        m_pControlCenterSetting = nullptr;
    }
}

ControlCenterPersonGsetting *ControlCenterPersonGsetting::getInstance()
{
    return controlCenterPersonGsetting();
}

double ControlCenterPersonGsetting::getTransparency()
{
    const QByteArray id(CONTROL_CENTER_PERSONALISE_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pControlCenterSetting != nullptr \
            && m_pControlCenterSetting->keys().contains(CONTROL_CENTER_PERSONALISE_TRANS_KEY)) {
        return m_pControlCenterSetting->get(CONTROL_CENTER_PERSONALISE_TRANS_KEY).toDouble();
    } else {
        qWarning() << "get transparency error , set 1 ";
        return 1;
    }
}

void ControlCenterPersonGsetting::setIschecked(bool state)
{
    const QByteArray id(CONTROL_CENTER_PERSONALISE_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pControlCenterSetting != nullptr \
            && m_pControlCenterSetting->keys().contains(CONTROL_CENTER_PERSONALISE_ISCHECKED_KEY)) {
          m_pControlCenterSetting->set(CONTROL_CENTER_PERSONALISE_ISCHECKED_KEY,state);
    } else {
        qWarning() << "setIschecked error ";

    }
}

//! 控制中心-消息通知部分
Q_GLOBAL_STATIC(ControlCenterNotiGsetting, controlCenterNotiGsetting)

ControlCenterNotiGsetting::ControlCenterNotiGsetting()
{
    QList<char*> existsPath = listExistsPath();
    for (char* path : existsPath) {
        char* prepath = QString(CONTROL_CENTER_NOTIFICATION_PATH).toLatin1().data();
        char* allpath = strcat(prepath, path);

        const QByteArray ba(CONTROL_CENTER_NOTIFICATION_SETTING);
        const QByteArray bba(allpath);
        m_pControlCenterSetting = new QGSettings(ba, bba, this);

        /* 监听setting的状态和最大数目改变 */
         connect(m_pControlCenterSetting,&QGSettings::changed,[=](){
            emit Sig_appNotifySettingChanged();
        });
    }
}

ControlCenterNotiGsetting::~ControlCenterNotiGsetting()
{
    if (m_pControlCenterSetting != nullptr) {
        delete  m_pControlCenterSetting;
        m_pControlCenterSetting = nullptr;
    }
}

/**
 * @brief NotificationDbus::listExistsPath
 * 列出存在的路径
 * @return
 */
QList<char*> ControlCenterNotiGsetting::listExistsPath()
{
    char** childs;
    int len;

    DConfClient * client = dconf_client_new();
    childs = dconf_client_list(client, CONTROL_CENTER_NOTIFICATION_PATH, &len);
    g_object_unref(client);

    QList<char*> vals;

    for (int i = 0; childs[i] != NULL; i++) {
        if (dconf_is_rel_dir(childs[i], NULL)) {
            char* val = g_strdup(childs[i]);
            vals.append(val);
        }
    }
    g_strfreev(childs);
    return vals;
}

ControlCenterNotiGsetting *ControlCenterNotiGsetting::getInstance()
{
    return controlCenterNotiGsetting();
}

QString ControlCenterNotiGsetting::getNotificationName()
{
    if (m_pControlCenterSetting != nullptr \
            && m_pControlCenterSetting->keys().contains(CONTROL_CENTER_NOTIFICATION_NAME_KEY)) {
        return m_pControlCenterSetting->get(CONTROL_CENTER_NOTIFICATION_NAME_KEY).toString();
    } else {
        qWarning()<<"get name error , return NULL";
        return "NULL";
    }
}

int ControlCenterNotiGsetting::getMixNum()
{
    if (m_pControlCenterSetting != nullptr \
            && m_pControlCenterSetting->keys().contains(CONTROL_CENTER_NOTIFICATION_MAXIMINE_KEY)) {
        return m_pControlCenterSetting->get(CONTROL_CENTER_NOTIFICATION_MAXIMINE_KEY).toInt();
    } else {
        qWarning()<<"get Notification Number error, return 0";
        return 0;
    }
}

bool ControlCenterNotiGsetting::getMessage()
{
    if (m_pControlCenterSetting != nullptr \
            && m_pControlCenterSetting->keys().contains(CONTROL_CENTER_NOTIFICATION_MESSAGE_KEY)) {
        return m_pControlCenterSetting->get(CONTROL_CENTER_NOTIFICATION_MESSAGE_KEY).toBool();
    } else {
        qWarning()<<"get Message state error , return false";
        return false;
    }
}


//! 控制中心-面板部分
Q_GLOBAL_STATIC(ControlCenterPanelGsetting, controlCenterPanelGsetting)

ControlCenterPanelGsetting::ControlCenterPanelGsetting()
{
    const QByteArray id(CONTROL_CENTER_PANEL_SETTING);
    if (QGSettings::isSchemaInstalled(id)) {
        m_pControlCenterSetting = new QGSettings(id);

        connect(m_pControlCenterSetting, &QGSettings::changed, this, [=](QString key) {
            emit Sig_appNotifySettingChanged(key);
        });
    }
}

ControlCenterPanelGsetting::~ControlCenterPanelGsetting()
{
    if (m_pControlCenterSetting != nullptr) {
        delete  m_pControlCenterSetting;
        m_pControlCenterSetting = nullptr;
    }
}

ControlCenterPanelGsetting *ControlCenterPanelGsetting::getInstance()
{
    return controlCenterPanelGsetting();
}

QString ControlCenterPanelGsetting::getHourSystem()
{
    const QByteArray id(CONTROL_CENTER_PANEL_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pControlCenterSetting != nullptr \
            && m_pControlCenterSetting->keys().contains(CONTROL_CENTER_PANEL_HOUR_KEY)) {
        return m_pControlCenterSetting->get(CONTROL_CENTER_PANEL_HOUR_KEY).toString();
    } else {
        qWarning()<<"get hoursystem error ,return 12";
        return TIME_FORMAT;
    }
}

QString ControlCenterPanelGsetting::getDateSystem()
{
    const QByteArray id(CONTROL_CENTER_PANEL_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pControlCenterSetting != nullptr \
            && m_pControlCenterSetting->keys().contains(CONTROL_CENTER_PANEL_DATE_KEY)) {
        return m_pControlCenterSetting->get(CONTROL_CENTER_PANEL_DATE_KEY).toString();
    } else {
        qWarning()<<"get date error ,return cn";
        return DATE_FORMAT;
    }
}



//! 消息通知
Q_GLOBAL_STATIC(NotificationGsetting, notificationGsetting)

NotificationGsetting::NotificationGsetting()
{
    const QByteArray id(UKUI_NOTIFICATION_SETTING);
    if (QGSettings::isSchemaInstalled(id)) {
        m_pNotificationSetting = new QGSettings(id);
    }
}

NotificationGsetting::~NotificationGsetting()
{
    if (m_pNotificationSetting != nullptr) {
        delete  m_pNotificationSetting;
        m_pNotificationSetting = nullptr;
    }
}

NotificationGsetting *NotificationGsetting::getInstance()
{
    return notificationGsetting();
}

void NotificationGsetting::setShowFlag(bool showFlag)
{
    const QByteArray id(UKUI_NOTIFICATION_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pNotificationSetting != nullptr \
            && m_pNotificationSetting->keys().contains(UKUI_NOTIFICATION_KEY)) {
        m_pNotificationSetting->set(UKUI_NOTIFICATION_KEY,showFlag);
    } else {
        qWarning()<<"setShowFlag error ";
    }
}

void NotificationGsetting::setState(bool state)
{
    const QByteArray id(UKUI_NOTIFICATION_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pNotificationSetting != nullptr \
            && m_pNotificationSetting->keys().contains(UKUI_NOTIFICATION_STATE_KEY)) {
        m_pNotificationSetting->set(UKUI_NOTIFICATION_STATE_KEY,state);
    } else {
        qWarning()<<"set Notification state error ";
    }
}

//! 任务栏
Q_GLOBAL_STATIC(PanelGsetting, panelGsetting)

PanelGsetting::PanelGsetting()
{
    const QByteArray id(UKUI_PANEL_SETTING);
    if (QGSettings::isSchemaInstalled(id)) {
        m_pPanelSetting = new QGSettings(id);
        connect(m_pPanelSetting,&QGSettings::changed,[=](QString key){
            emit Sig_panelGsettingChanged(key);
        });
    }
}

PanelGsetting::~PanelGsetting()
{
    if (m_pPanelSetting != nullptr) {
        delete  m_pPanelSetting;
        m_pPanelSetting = nullptr;
    }
}

PanelGsetting *PanelGsetting::getInstance()
{
    return panelGsetting();
}

int PanelGsetting::getPanelSize()
{
    const QByteArray id(UKUI_PANEL_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pPanelSetting != nullptr \
            && m_pPanelSetting->keys().contains(UKUI_PANEL_SIZE_KEY)) {
        return m_pPanelSetting->get(UKUI_PANEL_SIZE_KEY).toInt();
    } else {
        qWarning()<<"get panelsize error , set 46 ";
        return 46;
    }
}

int PanelGsetting::getPanelPosition()
{
    const QByteArray id(UKUI_PANEL_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pPanelSetting != nullptr \
            && m_pPanelSetting->keys().contains(UKUI_PANEL_POSITION_KEY)) {
        return m_pPanelSetting->get(UKUI_PANEL_POSITION_KEY).toInt();
    } else {
        qWarning()<<"get panelposition error , set down ";
        return 0;
    }
}


//! 锁屏
Q_GLOBAL_STATIC(ScreensaverGsetting, screensaverGsetting)

ScreensaverGsetting::ScreensaverGsetting()
{
    const QByteArray id(UKUI_SCREENSAVER_SETTING);
    if (QGSettings::isSchemaInstalled(id)) {
        m_pScreensaverSetting = new QGSettings(id);

    }
}

ScreensaverGsetting::~ScreensaverGsetting()
{
    if (m_pScreensaverSetting != nullptr) {
        delete  m_pScreensaverSetting;
        m_pScreensaverSetting = nullptr;
    }
}

ScreensaverGsetting *ScreensaverGsetting::getInstance()
{
    return screensaverGsetting();
}

void ScreensaverGsetting::setMessageNum(int num)
{
    const QByteArray id(UKUI_SCREENSAVER_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pScreensaverSetting != nullptr
            && m_pScreensaverSetting->keys().contains(UKUI_SCREENSAVER_MESSAGE_KEY)) {
        m_pScreensaverSetting->set(UKUI_SCREENSAVER_MESSAGE_KEY,num);
    } else {
        qWarning()<<"set message number error";
    }
}


//! style
Q_GLOBAL_STATIC(StyleGsetting, styleGsetting)

StyleGsetting::StyleGsetting()
{
    const QByteArray id(UKUI_STYLE_SETTING);
    if (QGSettings::isSchemaInstalled(id)) {
        m_pStyleSetting = new QGSettings(id);
        connect(m_pStyleSetting,&QGSettings::changed,[=](QString key){
                emit Sig_styleGsettingChanged(key);
        });
    }
}

StyleGsetting::~StyleGsetting()
{
    if (m_pStyleSetting != nullptr) {
        delete  m_pStyleSetting;
        m_pStyleSetting = nullptr;
    }
}

StyleGsetting *StyleGsetting::getInstance()
{
    return styleGsetting();
}

QString StyleGsetting::getStyleName()
{
    const QByteArray id(UKUI_STYLE_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pStyleSetting != nullptr \
            && m_pStyleSetting->keys().contains(UKUI_STYLE_NAME_KEY)) {
        return m_pStyleSetting->get(UKUI_STYLE_NAME_KEY).toString();
    } else {
        qWarning()<<"get StyleName error ,return ukui-default ";
        return "ukui-default";
    }
}

void StyleGsetting::setStyleName(QString styleName)
{
    const QByteArray id(UKUI_STYLE_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pStyleSetting != nullptr \
            && m_pStyleSetting->keys().contains(UKUI_STYLE_NAME_KEY)) {
        m_pStyleSetting->set(UKUI_STYLE_NAME_KEY,styleName);
    } else {
        qWarning()<<"set StyleName error  ";
    }
}

//! 侧边栏快捷操作面板部分gsetting
Q_GLOBAL_STATIC(QuickOperationGsetting, quickOperationGsetting)

QuickOperationGsetting::QuickOperationGsetting()
{
    const QByteArray id(QUICK_OPERATION_SETTING);
    if (QGSettings::isSchemaInstalled(id)) {
        m_pQuickOperationSetting = new QGSettings(id);
        connect(m_pQuickOperationSetting,&QGSettings::changed,[=](QString key){
            emit Sig_quickOperationChanged(key);
        });
    }
}

QuickOperationGsetting::~QuickOperationGsetting()
{
    if (m_pQuickOperationSetting != nullptr) {
        delete  m_pQuickOperationSetting;
        m_pQuickOperationSetting = nullptr;
    }
}

QuickOperationGsetting *QuickOperationGsetting::getInstance()
{
    return quickOperationGsetting();
}

bool QuickOperationGsetting::getModeStatus()
{
    const QByteArray id(QUICK_OPERATION_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pQuickOperationSetting != nullptr \
            && m_pQuickOperationSetting->keys().contains(QUICK_OPERATION_GSETTING_VALUE_STATUS)) {
         return m_pQuickOperationSetting->get(QUICK_OPERATION_GSETTING_VALUE_STATUS).toBool();
    } else {
        qWarning()<<"get Mode Status error ,set false";
        return false;
    }
}

void QuickOperationGsetting::setModeStatus(bool state)
{
    const QByteArray id(QUICK_OPERATION_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pQuickOperationSetting != nullptr \
            && m_pQuickOperationSetting->keys().contains(QUICK_OPERATION_GSETTING_VALUE_STATUS)) {
        m_pQuickOperationSetting->set(QUICK_OPERATION_GSETTING_VALUE_STATUS,state);
    } else {
        qWarning()<<"set Mode Status error";
    }
}

void QuickOperationGsetting::setModeSoundState(bool state)
{
    const QByteArray id(QUICK_OPERATION_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pQuickOperationSetting != nullptr \
            && m_pQuickOperationSetting->keys().contains(QUICK_OPERATION_GSETTING_VALUE_SOUNDOFF)) {
        m_pQuickOperationSetting->set(QUICK_OPERATION_GSETTING_VALUE_SOUNDOFF,state);
    } else {
        qWarning()<<"set ModeSoundState error";
    }
}

void QuickOperationGsetting::setModeNotifyState(bool state)
{
    const QByteArray id(QUICK_OPERATION_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pQuickOperationSetting != nullptr \
            && m_pQuickOperationSetting->keys().contains(QUICK_OPERATION_GSETTING_VALUE_NOTIFYCLOSE)) {
        m_pQuickOperationSetting->set(QUICK_OPERATION_GSETTING_VALUE_NOTIFYCLOSE,state);
    } else {
        qWarning()<<"set Mode NotifyState error";
    }
}

//! 电源管理-
Q_GLOBAL_STATIC(PowerModeGsetting, powerModeGsetting)

PowerModeGsetting::PowerModeGsetting()
{
    const QByteArray id(POWER_MODE_SETTING);
    if (QGSettings::isSchemaInstalled(id)) {
        m_pPowerModeSetting = new QGSettings(id);
        connect(m_pPowerModeSetting,&QGSettings::changed,[=](QString key){
            emit Sig_powerModeChanged(key);
        });
    }
}

PowerModeGsetting::~PowerModeGsetting()
{
    if (m_pPowerModeSetting != nullptr) {
        delete  m_pPowerModeSetting;
        m_pPowerModeSetting = nullptr;
    }
}

PowerModeGsetting *PowerModeGsetting::getInstance()
{
    return powerModeGsetting();
}

void PowerModeGsetting::setBrightnessAc(int num)
{
    const QByteArray id(POWER_MODE_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pPowerModeSetting != nullptr \
            && m_pPowerModeSetting->keys().contains(POWER_MODE_BRIGHT_AC_KEY)) {
         m_pPowerModeSetting->set(POWER_MODE_BRIGHT_AC_KEY,num);
    } else {
        qWarning()<<"set Brightness Ac error ";
    }
}

int PowerModeGsetting::getBrightnessAc()
{
    const QByteArray id(POWER_MODE_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pPowerModeSetting != nullptr \
            && m_pPowerModeSetting->keys().contains(POWER_MODE_BRIGHT_AC_KEY)) {
         return m_pPowerModeSetting->get(POWER_MODE_BRIGHT_AC_KEY).toInt();
    } else {
        qWarning()<<"get Brightness Ac error ";
        return 1;
    }
}

int PowerModeGsetting::getPowerPolicyBattery()
{
    const QByteArray id(POWER_MODE_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pPowerModeSetting != nullptr \
            && m_pPowerModeSetting->keys().contains(POWER_MODE_POWERPOLICYBATTERY)) {
         return m_pPowerModeSetting->get(POWER_MODE_POWERPOLICYBATTERY).toInt();
    } else {
        qWarning()<<"get Power Policy Battery Ac error ";
        return 0;
    }
}

void PowerModeGsetting::setPowerPolicyBattery(int num)
{
    const QByteArray id(POWER_MODE_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pPowerModeSetting != nullptr \
            && m_pPowerModeSetting->keys().contains(POWER_MODE_POWERPOLICYBATTERY)) {
         m_pPowerModeSetting->set(POWER_MODE_POWERPOLICYBATTERY,num);
    } else {
        qWarning()<<"set Power Policy Battery error ";
    }
}

int PowerModeGsetting::getPowerPolicyAc()
{
    const QByteArray id(POWER_MODE_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pPowerModeSetting != nullptr \
            && m_pPowerModeSetting->keys().contains(POWER_MODE_POWERPOLICYAC)) {
         return m_pPowerModeSetting->get(POWER_MODE_POWERPOLICYAC).toInt();
    } else {
        qWarning()<<"get Power Policy Ac error ";
        return 0;
    }
}

void PowerModeGsetting::setPowerPolicyAc(int num)
{
    const QByteArray id(POWER_MODE_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pPowerModeSetting != nullptr \
            && m_pPowerModeSetting->keys().contains(POWER_MODE_POWERPOLICYAC)) {
         m_pPowerModeSetting->set(POWER_MODE_POWERPOLICYAC,num);
    } else {
        qWarning()<<"set Power Policy Ac error ";
    }
}


//! 蓝牙
Q_GLOBAL_STATIC(BluetoothGsetting, bluetoothGsetting)

BluetoothGsetting::BluetoothGsetting()
{
    const QByteArray id(BLUE_TOOTH_SETTING);
    if (QGSettings::isSchemaInstalled(id)) {
        m_pBluetoothSetting = new QGSettings(id);
        connect(m_pBluetoothSetting,&QGSettings::changed,[=](QString key){
            emit Sig_bluetoothChanged(key);
        });
    }
}

BluetoothGsetting::~BluetoothGsetting()
{
    if (m_pBluetoothSetting != nullptr) {
        delete  m_pBluetoothSetting;
        m_pBluetoothSetting = nullptr;
    }
}

BluetoothGsetting *BluetoothGsetting::getInstance()
{
    return bluetoothGsetting();
}

bool BluetoothGsetting::getSwitch()
{
    const QByteArray id(BLUE_TOOTH_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pBluetoothSetting != nullptr \
            && m_pBluetoothSetting->keys().contains(BLUE_TOOTH_SWITCH_KEY)) {
         return m_pBluetoothSetting->get(BLUE_TOOTH_SWITCH_KEY).toBool();
    } else {
        qWarning()<<"get bluetooth switch error return false";
        return false;
    }
}

void BluetoothGsetting::setSwitch(bool state)
{
    const QByteArray id(BLUE_TOOTH_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pBluetoothSetting != nullptr \
            && m_pBluetoothSetting->keys().contains(BLUE_TOOTH_SWITCH_KEY)) {
          m_pBluetoothSetting->set(BLUE_TOOTH_SWITCH_KEY,state);
    } else {
        qWarning()<<"set bluetooth switch error";
    }
}

//! mate 主题
Q_GLOBAL_STATIC(GtkStyleGsetting, gtkStyleGsetting)

GtkStyleGsetting::GtkStyleGsetting()
{
    const QByteArray id(GTK_STYLE_SETTING);
    if (QGSettings::isSchemaInstalled(id)) {
        m_pGtkStyleSetting = new QGSettings(id);
    }
}

GtkStyleGsetting::~GtkStyleGsetting()
{
    if (m_pGtkStyleSetting != nullptr) {
        delete  m_pGtkStyleSetting;
        m_pGtkStyleSetting = nullptr;
    }
}

GtkStyleGsetting *GtkStyleGsetting::getInstance()
{
    return gtkStyleGsetting();
}

void GtkStyleGsetting::setGtkTheme(QString styleName)
{
    const QByteArray id(GTK_STYLE_SETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pGtkStyleSetting != nullptr \
            && m_pGtkStyleSetting->keys().contains(GTK_STYLE_KEY)) {
          m_pGtkStyleSetting->set(GTK_STYLE_KEY,styleName);
    } else {
        qWarning()<<"set GtkTheme  error";
    }
}

//! 麒麟天气
Q_GLOBAL_STATIC(WeatherGsetting, weatherGsetting)

WeatherGsetting::WeatherGsetting()
{
    const QByteArray id(WEATHER_GSETTING_NEW);
    if (QGSettings::isSchemaInstalled(id)) {
        m_pWeatherSetting = new QGSettings(id);
        connect(m_pWeatherSetting,&QGSettings::changed,[=](QString key){
            emit Sig_weatherChanged(key);
        });
    }
}

WeatherGsetting::~WeatherGsetting()
{
    if (m_pWeatherSetting != nullptr) {
        delete  m_pWeatherSetting;
        m_pWeatherSetting = nullptr;
    }
}

WeatherGsetting *WeatherGsetting::getInstance()
{
    return weatherGsetting();
}

QString WeatherGsetting::getWeather()
{
    const QByteArray id(WEATHER_GSETTING_NEW);
    if (QGSettings::isSchemaInstalled(id) && m_pWeatherSetting != nullptr \
            && m_pWeatherSetting->keys().contains(WEATHER_GSETTING_KEY)) {
        QString weatherString = m_pWeatherSetting->get(WEATHER_GSETTING_KEY).toString();
        QStringList weatherInfoList = weatherString.split(",");
        return QStringLiteral("%1 %2 %3").arg(weatherInfoList.at(2)).arg(weatherInfoList.at(3)).arg(weatherInfoList.at(5));
    } else {
        qWarning()<<"get Weather error";
        return "获取天气信息失败";
    }
}

QString WeatherGsetting::getIcon()
{
    const QByteArray id(WEATHER_GSETTING_NEW);
    if (QGSettings::isSchemaInstalled(id) && m_pWeatherSetting != nullptr \
            && m_pWeatherSetting->keys().contains(WEATHER_GSETTING_KEY)) {
        QString weatherString = m_pWeatherSetting->get(WEATHER_GSETTING_KEY).toString();
        QStringList weatherInfoList = weatherString.split(",");
        return weatherInfoList.at(8);
    } else {
        qWarning()<<"get Icon error";
        return "NULL";
    }
}

//! 护眼中心
Q_GLOBAL_STATIC(EyeprotectionGsetting, eyeprotectionGsetting)

EyeprotectionGsetting::EyeprotectionGsetting()
{
    const QByteArray id(EYEPROTECTION_GSETTING);
    if (QGSettings::isSchemaInstalled(id)) {
        m_pEyeprotectionSetting = new QGSettings(id);
        if (QGSettings::isSchemaInstalled(id)) {
            m_pEyeprotectionSetting = new QGSettings(id);
            connect(m_pEyeprotectionSetting,&QGSettings::changed,[=](QString key){
                    emit Sig_eyeprotectionChanged(key);
            });
        }
    }
}

EyeprotectionGsetting::~EyeprotectionGsetting()
{
    if (m_pEyeprotectionSetting != nullptr) {
        delete  m_pEyeprotectionSetting;
        m_pEyeprotectionSetting = nullptr;
    }
}

EyeprotectionGsetting *EyeprotectionGsetting::getInstance()
{
    return eyeprotectionGsetting();
}

bool EyeprotectionGsetting::getTimeReminder()
{
    const QByteArray id(EYEPROTECTION_GSETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pEyeprotectionSetting != nullptr \
            && m_pEyeprotectionSetting->keys().contains(EYEPROTECTION_TIME_KEY)) {
          return m_pEyeprotectionSetting->get(EYEPROTECTION_TIME_KEY).toBool();
    } else {
        qWarning()<<"get Time Reminder error,return false";
    }
}

bool EyeprotectionGsetting::getDistanceReminder()
{
    const QByteArray id(EYEPROTECTION_GSETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pEyeprotectionSetting != nullptr \
            && m_pEyeprotectionSetting->keys().contains(EYEPROTECTION_DISTANCE_KEY)) {
          return m_pEyeprotectionSetting->get(EYEPROTECTION_DISTANCE_KEY).toBool();
    } else {
        qWarning()<<"get distance Reminder error,return false";
    }
}

void EyeprotectionGsetting::setTimeReminder(bool state)
{
    const QByteArray id(EYEPROTECTION_GSETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pEyeprotectionSetting != nullptr \
            && m_pEyeprotectionSetting->keys().contains(EYEPROTECTION_TIME_KEY)) {
        m_pEyeprotectionSetting->set(EYEPROTECTION_TIME_KEY,state);
    } else {
        qWarning()<<"set Time Reminder error";
    }
}

void EyeprotectionGsetting::setDistanceReminder(bool state)
{
    const QByteArray id(EYEPROTECTION_GSETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pEyeprotectionSetting != nullptr \
            && m_pEyeprotectionSetting->keys().contains(EYEPROTECTION_DISTANCE_KEY)) {
        m_pEyeprotectionSetting->set(EYEPROTECTION_DISTANCE_KEY,state);
    } else {
        qWarning()<<"set distance Reminder error";
    }
}

//! settingDaemon
Q_GLOBAL_STATIC(SettingsDaemonGsetting, settingsDaemonGsetting)

SettingsDaemonGsetting::SettingsDaemonGsetting()
{
    const QByteArray id(SETTINGSDAEMON_GSETTING);
    if (QGSettings::isSchemaInstalled(id)) {
        m_pSettingsDaemonSetting = new QGSettings(id);
        connect(m_pSettingsDaemonSetting,&QGSettings::changed,[=](QString key){
                emit Sig_settingsDaemonChanged(key);
        });
    }
}

SettingsDaemonGsetting::~SettingsDaemonGsetting()
{
    if (m_pSettingsDaemonSetting != nullptr) {
        delete  m_pSettingsDaemonSetting;
        m_pSettingsDaemonSetting = nullptr;
    }
}

SettingsDaemonGsetting *SettingsDaemonGsetting::getInstance()
{
    return settingsDaemonGsetting();
}

bool SettingsDaemonGsetting::getnightLightEnabled()
{
    const QByteArray id(SETTINGSDAEMON_GSETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pSettingsDaemonSetting != nullptr \
            && m_pSettingsDaemonSetting->keys().contains(SETTINGSDAEMON_KEY)) {
          return m_pSettingsDaemonSetting->get(SETTINGSDAEMON_KEY).toBool();
    } else {
        qWarning()<<"get night Light Enabled,return false";
        return false;
    }
}

void SettingsDaemonGsetting::setnightLightEnabled(bool state)
{
    const QByteArray id(SETTINGSDAEMON_GSETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pSettingsDaemonSetting != nullptr \
            && m_pSettingsDaemonSetting->keys().contains(SETTINGSDAEMON_KEY)) {
        m_pSettingsDaemonSetting->set(SETTINGSDAEMON_KEY,state);
    } else {
        qWarning()<<"set night Light Enabled ";
    }
}

bool SettingsDaemonGsetting::getThemeScheduleAutomatic()
{
    const QByteArray id(SETTINGSDAEMON_GSETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pSettingsDaemonSetting != nullptr \
            && m_pSettingsDaemonSetting->keys().contains(SETTINGSDAEMON_THEME_KEY)) {
          return m_pSettingsDaemonSetting->get(SETTINGSDAEMON_THEME_KEY).toBool();
    } else {
        qWarning()<<"get Theme Schedule Automatic,return false";
        return false;
    }
}

void SettingsDaemonGsetting::setThemeScheduleAutomatic(bool state)
{
    const QByteArray id(SETTINGSDAEMON_GSETTING);
    if (QGSettings::isSchemaInstalled(id) && m_pSettingsDaemonSetting != nullptr \
            && m_pSettingsDaemonSetting->keys().contains(SETTINGSDAEMON_THEME_KEY)) {
        m_pSettingsDaemonSetting->set(SETTINGSDAEMON_THEME_KEY,state);
    } else {
        qWarning()<<"set Theme Schedule Automatic ";
    }
}
