1 /*
2 * Copyright © 2014 Red Hat, Inc. All rights reserved.
3 * Copyright © 2014 Ding-Yi Chen <dchen@redhat.com>
4 *
5 * This file is part of the ibus-chewing Project.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22 #include <ibus.h>
23 #include "MakerDialogUtil.h"
24 #include "GSettingsBackend.h"
25 #include "IBusChewingUtil.h"
26 #include "IBusConfigBackend.h"
27 #include "IBusChewingProperties.h"
28
29 /*============================================
30 * Class methods
31 */
32
33
34 #define KEY_BUFFER_SIZE 100
to_real_section(gchar * confSection,MkdgBackend * backend,const gchar * section)35 static gchar *to_real_section(gchar * confSection, MkdgBackend * backend,
36 const gchar * section)
37 {
38
39 if (!STRING_IS_EMPTY(backend->basePath)) {
40 g_strlcpy(confSection, backend->basePath, KEY_BUFFER_SIZE);
41 } else {
42 g_strlcpy(confSection, "", KEY_BUFFER_SIZE);
43 }
44
45 if (!STRING_IS_EMPTY(section)) {
46 g_strlcat(confSection, section, KEY_BUFFER_SIZE);
47 g_strlcat(confSection, "/", KEY_BUFFER_SIZE);
48 }
49 return confSection;
50 }
51
ibus_config_backend_read_value(MkdgBackend * backend,GValue * value,const gchar * section,const gchar * key,gpointer userData)52 GValue *ibus_config_backend_read_value(MkdgBackend * backend,
53 GValue * value,
54 const gchar * section,
55 const gchar * key,
56 gpointer userData)
57 {
58 IBusConfig *config = (IBusConfig *) backend->config;
59 gchar confSection[KEY_BUFFER_SIZE];
60 to_real_section(confSection, backend, section);
61 printf("confSection=%s\n", confSection);
62 #if IBUS_CHECK_VERSION(1, 4, 0)
63 GVariant *gVar = ibus_config_get_value(config, confSection,
64 key);
65 if (gVar == NULL) {
66 return NULL;
67 }
68 g_variant_ref_sink(gVar);
69 mkdg_g_variant_to_g_value(gVar, value);
70 g_variant_unref(gVar);
71 return value;
72 #else
73 return ibus_config_get_value(config, confSection, key, value);
74 #endif
75 }
76
77
ibus_config_backend_write_value(MkdgBackend * backend,GValue * value,const gchar * section,const gchar * key,gpointer userData)78 gboolean ibus_config_backend_write_value(MkdgBackend * backend,
79 GValue * value,
80 const gchar * section,
81 const gchar * key,
82 gpointer userData)
83 {
84 gboolean result = FALSE;
85 IBusConfig *config = (IBusConfig *) backend->config;
86 gchar confSection[KEY_BUFFER_SIZE];
87 to_real_section(confSection, backend, section);
88 #if IBUS_CHECK_VERSION(1, 4, 0)
89 GVariant *gVar = g_variant_ref_sink(mkdg_g_value_to_g_variant(value));
90 if (gVar != NULL) {
91 result = ibus_config_set_value(config, confSection, key, gVar);
92 }
93 #else
94 result = ibus_config_set_value(config, confSection, key, value);
95 #endif
96
97 if (result == FALSE) {
98 mkdg_log(WARN,
99 "ibus_config_backend_write_value(-, %s, -) %s %s",
100 key, "Failed to set variable", key);
101 return FALSE;
102 }
103
104 if (!config) {
105 mkdg_log(WARN,
106 "ibus_config_backend_write_value(-, %s, -) %s",
107 key, "Failed to connect to IBusService");
108 return FALSE;
109 }
110 return TRUE;
111 }
112
113 /*
114 * basePath is shorter that other backend, as ibus-config should provide ibus level base
115 */
ibus_config_backend_new(IBusService * service,const gchar * basePath,gpointer auxData)116 MkdgBackend *ibus_config_backend_new(IBusService * service,
117 const gchar * basePath,
118 gpointer auxData)
119 {
120 IBusConfig *config = NULL;
121 #if IBUS_CHECK_VERSION(1, 4, 0)
122 GError *error = NULL;
123 GDBusConnection *connection = ibus_service_get_connection(service);
124 config = g_object_ref_sink(ibus_config_new(connection, NULL, &error));
125 g_assert(error == NULL);
126 #else
127 GList *connections_list = ibus_service_get_connections(service);
128 g_assert(connections_list);
129 g_assert(connections_list->data);
130 IBusConnection *iConnection =
131 (IBusConnection *) connections_list->data;
132 config = g_object_ref_sink(ibus_config_new(iConnection));
133 #endif
134 MkdgBackend *result =
135 mkdg_backend_new(IBUS_CONFIG_BACKEND_ID, (gpointer) config,
136 basePath, auxData);
137 result->readFunc = ibus_config_backend_read_value;
138 result->writeFunc = ibus_config_backend_write_value;
139 return result;
140 }
141