GCC Code Coverage Report


Directory: ./
File: panels/keyboard/cc-input-source-ibus.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 56 0.0%
Functions: 0 14 0.0%
Branches: 0 27 0.0%

Line Branch Exec Source
1 /*
2 * Copyright © 2018 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "cc-input-source-ibus.h"
19 #ifdef HAVE_IBUS
20 #include "cc-ibus-utils.h"
21 #endif
22
23 struct _CcInputSourceIBus
24 {
25 CcInputSource parent_instance;
26
27 gchar *engine_name;
28 #ifdef HAVE_IBUS
29 IBusEngineDesc *engine_desc;
30 #endif
31 };
32
33 G_DEFINE_TYPE (CcInputSourceIBus, cc_input_source_ibus, CC_TYPE_INPUT_SOURCE)
34
35 static gchar *
36 cc_input_source_ibus_get_label (CcInputSource *source)
37 {
38 CcInputSourceIBus *self = CC_INPUT_SOURCE_IBUS (source);
39 #ifdef HAVE_IBUS
40 if (self->engine_desc)
41 return g_strdup (engine_get_display_name (self->engine_desc));
42 else
43 #endif
44 return g_strdup (self->engine_name);
45 }
46
47 static gboolean
48 cc_input_source_ibus_matches (CcInputSource *source,
49 CcInputSource *source2)
50 {
51 if (!CC_IS_INPUT_SOURCE_IBUS (source2))
52 return FALSE;
53
54 return g_strcmp0 (CC_INPUT_SOURCE_IBUS (source)->engine_name, CC_INPUT_SOURCE_IBUS (source2)->engine_name) == 0;
55 }
56
57 static const gchar *
58 cc_input_source_ibus_get_layout (CcInputSource *source)
59 {
60 #ifdef HAVE_IBUS
61 CcInputSourceIBus *self = CC_INPUT_SOURCE_IBUS (source);
62 if (self->engine_desc != NULL)
63 return ibus_engine_desc_get_layout (self->engine_desc);
64 else
65 #endif
66 return NULL;
67 }
68
69 static const gchar *
70 cc_input_source_ibus_get_layout_variant (CcInputSource *source)
71 {
72 #ifdef HAVE_IBUS
73 CcInputSourceIBus *self = CC_INPUT_SOURCE_IBUS (source);
74 if (self->engine_desc != NULL)
75 return ibus_engine_desc_get_layout_variant (self->engine_desc);
76 else
77 #endif
78 return NULL;
79 }
80
81 static void
82 cc_input_source_ibus_dispose (GObject *object)
83 {
84 CcInputSourceIBus *self = CC_INPUT_SOURCE_IBUS (object);
85
86 g_clear_pointer (&self->engine_name, g_free);
87 #ifdef HAVE_IBUS
88 g_clear_object (&self->engine_desc);
89 #endif
90
91 G_OBJECT_CLASS (cc_input_source_ibus_parent_class)->dispose (object);
92 }
93
94 void
95 cc_input_source_ibus_class_init (CcInputSourceIBusClass *klass)
96 {
97 CcInputSourceClass *input_source_class = CC_INPUT_SOURCE_CLASS (klass);
98 GObjectClass *object_class = G_OBJECT_CLASS (klass);
99
100 input_source_class->get_label = cc_input_source_ibus_get_label;
101 input_source_class->matches = cc_input_source_ibus_matches;
102 input_source_class->get_layout = cc_input_source_ibus_get_layout;
103 input_source_class->get_layout_variant = cc_input_source_ibus_get_layout_variant;
104 object_class->dispose = cc_input_source_ibus_dispose;
105 }
106
107 void
108 cc_input_source_ibus_init (CcInputSourceIBus *source)
109 {
110 }
111
112 CcInputSourceIBus *
113 cc_input_source_ibus_new (const gchar *engine_name)
114 {
115 CcInputSourceIBus *source;
116
117 source = g_object_new (CC_TYPE_INPUT_SOURCE_IBUS, NULL);
118 source->engine_name = g_strdup (engine_name);
119
120 return source;
121 }
122
123 #ifdef HAVE_IBUS
124 void
125 cc_input_source_ibus_set_engine_desc (CcInputSourceIBus *source,
126 IBusEngineDesc *engine_desc)
127 {
128 g_return_if_fail (CC_IS_INPUT_SOURCE_IBUS (source));
129
130 g_clear_object (&source->engine_desc);
131 source->engine_desc = g_object_ref (engine_desc);
132 cc_input_source_emit_label_changed (CC_INPUT_SOURCE (source));
133 }
134 #endif
135
136 const gchar *
137 cc_input_source_ibus_get_engine_name (CcInputSourceIBus *source)
138 {
139 g_return_val_if_fail (CC_IS_INPUT_SOURCE_IBUS (source), NULL);
140 return source->engine_name;
141 }
142
143 GDesktopAppInfo *
144 cc_input_source_ibus_get_app_info (CcInputSourceIBus *source)
145 {
146 g_auto(GStrv) tokens = NULL;
147 g_autofree gchar *desktop_file_name = NULL;
148
149 g_return_val_if_fail (CC_IS_INPUT_SOURCE_IBUS (source), NULL);
150
151 tokens = g_strsplit (source->engine_name, ":", 2);
152 desktop_file_name = g_strdup_printf ("ibus-setup-%s.desktop", tokens[0]);
153
154 return g_desktop_app_info_new (desktop_file_name);
155 }
156