#include "../api/libkysettings.h"
#include <stdio.h>
#include <unistd.h>

//键值改变信号回调函数
static void on_key_changed(KSettings *setting, char *key, void *user_data)
{
    printf("on key change signal\n");
}

int main(int argc, char *argv[])
{
    KSettings *setting = kdk_conf2_new("systemd.powermanager", NULL);
    if(NULL == setting)
    {
        printf("get handle failed\n");
        return 0;
    }

    //链接键值改变信号
    /**
     * 只有已连接切读过一次配置时才能接收到该信号
     * 接收信号需要开启主程序的消息循环
     */
    // kdk_conf2_connect_signal(setting, "changed::sleep", on_key_changed, NULL);

    //读配置
    char *value = kdk_conf2_get_value(setting, "sleep");
    if(NULL != value)
    {
        printf("sleep user value: %s\n", value);
        free(value);
    }

    //读默认配置
    value = kdk_conf2_get_default_value(setting, "sleep");
    if (NULL != value)
    {
        printf("sleep default value: %s\n", value);
        free(value);
    }

    //读对应数据类型的值
    //调用获取对应数据类型的接口前，调用has_key确保键存在。否则无法保证返回值的准确性
    if(kdk_conf2_has_key(setting, "sleep"))
    {
        int b_sleep = kdk_conf2_get_boolean(setting, "sleep");
        printf("sleep user boolean: %s\n", b_sleep ? "true" : "false");

        b_sleep = kdk_conf2_get_default_boolean(setting, "sleep");
        printf("sleep defalut boolean: %s\n", b_sleep ? "true" : "false");
    }

    //设置键值
    int success = kdk_conf2_set_value(setting, "sleep", "true");
    if(0 == success)
    {
        printf("set value failed\n");
    }

    //其它接口参考头文件或接口文档

    usleep(10);
    //注销句柄
    kdk_conf2_ksettings_destroy(setting);

    return 0;
}