File: config_utils.cpp

package info (click to toggle)
scim-unikey 0.3.1%2Bdebian-3.2
  • links: PTS
  • area: main
  • in suites: bullseye, buster
  • size: 2,504 kB
  • sloc: sh: 10,650; cpp: 7,490; makefile: 171; sed: 16
file content (212 lines) | stat: -rw-r--r-- 5,508 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "dlg_main_setup.h"

#include "scim_unikey_const.h"

#define get_macro_file() (g_build_filename(g_getenv("HOME"), SCIM_IMENGINE_UNIKEY_MACROPATH, NULL))
void scim_set_config(const gchar* key, GType t, gpointer data);
gboolean scim_get_config(const gchar* key, GType t, gpointer data);

void set_default_config(UnikeyMainSetupOptions* opt)
{
    opt->input_method = 0;
    opt->output_charset = 0;

    opt->enableSpellcheck = SCIM_IMENGINE_UNIKEY_SPELLCHECKENABLED_DEF;
    opt->autoRestoreNonVn = SCIM_IMENGINE_UNIKEY_AUTONONVNRESTORE_DEF;
    opt->modernStyle = SCIM_IMENGINE_UNIKEY_MODERNSTYLE_DEF;
    opt->freeMarking = SCIM_IMENGINE_UNIKEY_FREEMARKING_DEF;
    opt->enableMacro = SCIM_IMENGINE_UNIKEY_MACROENABLED_DEF;

    opt->processwatbegin = SCIM_IMENGINE_UNIKEY_PROCESSWATWORDBEGIN_DEF;

    opt->macrofile = get_macro_file();
}

void read_config(UnikeyMainSetupOptions* opt)
{
    gboolean b;
    int k;
    gboolean t;
    //gchar* s;

    // get Input method
    b = scim_get_config(SCIM_IMENGINE_UNIKEY_INPUTMETHOD, G_TYPE_INT, &k);
    if (b == TRUE)
    {
        opt->input_method = k;
    }
    // END get Input method

    // get Output charset
    b = scim_get_config(SCIM_IMENGINE_UNIKEY_OUTPUTCHARSET, G_TYPE_INT, &k);
    if (b == TRUE)
    {
        opt->output_charset = k;
    }
    // END get Output charset

    // get Spellcheck
    b = scim_get_config(SCIM_IMENGINE_UNIKEY_SPELLCHECKENABLED, G_TYPE_BOOLEAN, &t);
    if (b == TRUE)
    {
        opt->enableSpellcheck = t;
    }
    // END get Spellcheck

    // get autoRestoreNonVn
    b = scim_get_config(SCIM_IMENGINE_UNIKEY_AUTONONVNRESTORE, G_TYPE_BOOLEAN, &t);
    if (b == TRUE)
    {
        opt->autoRestoreNonVn = t;
    }
    // END get autoRestoreNonVn

    // get modernStyle
    b = scim_get_config(SCIM_IMENGINE_UNIKEY_MODERNSTYLE, G_TYPE_BOOLEAN, &t);
    if (b == TRUE)
    {
        opt->modernStyle = t;
    }
    // END get modernStyle

    // get freeMarking
    b = scim_get_config(SCIM_IMENGINE_UNIKEY_FREEMARKING, G_TYPE_BOOLEAN, &t);
    if (b == TRUE)
    {
        opt->freeMarking = t;
    }
    // END get freeMarking

    // get enableMacro
    b = scim_get_config(SCIM_IMENGINE_UNIKEY_MACROENABLED, G_TYPE_BOOLEAN, &t);
    if (b == TRUE)
    {
        opt->enableMacro = t;
    }
    // END get enableMacro

    // get ProcessWAtBegin
    b = scim_get_config(SCIM_IMENGINE_UNIKEY_PROCESSWATWORDBEGIN, G_TYPE_BOOLEAN, &t);
    if (b == TRUE)
    {
        opt->processwatbegin = t;
    }
    // END get ProcessWAtBegin
}

void write_config(UnikeyMainSetupOptions* opt)
{
    scim_set_config(SCIM_IMENGINE_UNIKEY_INPUTMETHOD, G_TYPE_INT, &opt->input_method);
    scim_set_config(SCIM_IMENGINE_UNIKEY_OUTPUTCHARSET, G_TYPE_INT, &opt->output_charset);
    scim_set_config(SCIM_IMENGINE_UNIKEY_SPELLCHECKENABLED, G_TYPE_BOOLEAN, &opt->enableSpellcheck);
    scim_set_config(SCIM_IMENGINE_UNIKEY_AUTONONVNRESTORE, G_TYPE_BOOLEAN, &opt->autoRestoreNonVn);
    scim_set_config(SCIM_IMENGINE_UNIKEY_MODERNSTYLE, G_TYPE_BOOLEAN, &opt->modernStyle);
    scim_set_config(SCIM_IMENGINE_UNIKEY_FREEMARKING, G_TYPE_BOOLEAN, &opt->freeMarking);
    scim_set_config(SCIM_IMENGINE_UNIKEY_MACROENABLED, G_TYPE_BOOLEAN, &opt->enableMacro);
    scim_set_config(SCIM_IMENGINE_UNIKEY_PROCESSWATWORDBEGIN, G_TYPE_BOOLEAN, &opt->processwatbegin);
}

int force_engine_to_reload_config()
{
    return system("scim-config-agent --reload > /dev/null");
}

void scim_set_config(const gchar* key, GType t, gpointer data)
{
    gchar** argv;
    gchar s[1024];
    gchar* output;

    argv = (gchar**)g_malloc(sizeof(gchar*)*4);
    argv[0] = (gchar*)"scim-config-agent";
    argv[1] = (gchar*)"--set";
    argv[3] = (gchar*)NULL;

    strcpy(s, key);
    strcat(s, "=");

    switch (t)
    {
    case G_TYPE_BOOLEAN:
        strcat(s, *(gboolean*)data?"true":"false");
        break;
    case G_TYPE_INT:
        sprintf(s+strlen(s), "%d", *(gint*)data);
        break;
    case G_TYPE_STRING:
        strcat(s, *(gchar**)data);
        break;
    }

    argv[2] = (gchar*)s;

    g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &output, NULL, NULL, NULL);
    free(output);
}

gboolean scim_get_config(const gchar* key, GType t, gpointer data)
{
    gchar** argv;
    gchar* output;

    argv = (gchar**)g_malloc(sizeof(gchar*)*4);
    argv[0] = (gchar*)"scim-config-agent";
    argv[1] = (gchar*)"--get";
    argv[2] = (gchar*)key;
    argv[3] = (gchar*)NULL;

    g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &output, NULL, NULL, NULL);

    free(argv);

    output[strlen(output)-1] = 0;

    if (strcmp(output, "Failed to get key value.") == 0)
    {
        free(output);
        return FALSE;
    }

    gboolean* pb;
    gint* pi;
    gchar** pc;

    switch(t)
    {
    case G_TYPE_BOOLEAN:
        pb = (gboolean*)data;
        if (strcmp(output, "true") == 0)
            *pb = TRUE;
        else if (strcmp(output, "false") == 0)
            *pb = FALSE;
        else
        {
            free(output);
            return FALSE;
        }
        break;
    case G_TYPE_INT:
        pi = (gint*)data;
        if (isalpha(output[0]))
        {
            free(output);
            return FALSE;
        }
        else
            *pi = atoi(output);
        break;
    case G_TYPE_STRING:
        pc = (gchar**)data;
        *pc = output;
        return TRUE;
    }

    free(output);

    return TRUE;
}