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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243 | /*
* baul-extension.c - extension management functions
*
* Copyright (C) 2014 CAFE Desktop.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* Author: Alexander van der Meij <alexandervdm@gliese.me>
*/
#include "baul-extensions.h"
#include "baul-global-preferences.h"
#include "baul-module.h"
#include "baul-debug-log.h"
#include <string.h>
#define BAUL_EXTENSION_GROUP "Baul Extension"
static GList *baul_extensions = NULL;
static Extension *
extension_new (gchar *filename, gboolean state, gboolean python, GObject *module)
{
GError *error = NULL;
Extension *ext;
GKeyFile *extension_file;
gchar *extension_filename;
ext = g_new0 (Extension, 1);
ext->filename = filename;
ext->name = NULL;
ext->description = NULL;
ext->author = NULL;
ext->copyright = NULL;
ext->version = NULL;
ext->website = NULL;
ext->state = state;
ext->module = module;
extension_file = g_key_file_new ();
extension_filename = g_strdup_printf(BAUL_DATADIR "/extensions/%s.baul-extension", filename);
if (g_key_file_load_from_file (extension_file, extension_filename, G_KEY_FILE_NONE, &error))
{
ext->name = g_key_file_get_locale_string (extension_file, BAUL_EXTENSION_GROUP, "Name", NULL, NULL);
ext->description = g_key_file_get_locale_string (extension_file, BAUL_EXTENSION_GROUP, "Description", NULL, NULL);
ext->icon = g_key_file_get_string (extension_file, BAUL_EXTENSION_GROUP, "Icon", NULL);
ext->author = g_key_file_get_string_list (extension_file, BAUL_EXTENSION_GROUP, "Author", NULL, NULL);
ext->copyright = g_key_file_get_locale_string (extension_file, BAUL_EXTENSION_GROUP, "Copyright", NULL, NULL);
ext->version = g_key_file_get_string (extension_file, BAUL_EXTENSION_GROUP, "Version", NULL);
ext->website = g_key_file_get_string (extension_file, BAUL_EXTENSION_GROUP, "Website", NULL);
g_key_file_free (extension_file);
}
else
{
baul_debug_log (FALSE, BAUL_DEBUG_LOG_DOMAIN_USER, "Error loading keys from file: %s\n", error->message);
g_error_free (error);
}
g_free (extension_filename);
if (python)
{
ext->name = g_strconcat("Python: ", filename, NULL);
ext->description = "Python-baul extension";
}
return ext;
}
/* functions related to persistent configuration through gsettings: */
static gboolean
gsettings_key_has_value (const gchar *value)
{
gchar **list;
gint i;<--- The scope of the variable 'i' can be reduced. [+]The scope of the variable 'i' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
list = g_settings_get_strv (baul_extension_preferences,
BAUL_PREFERENCES_DISABLED_EXTENSIONS);
if (list != NULL)
{
for (i = 0; list[i]; i++)
{
if (g_ascii_strcasecmp (value, list[i]) == 0)
{
g_strfreev (list);
return TRUE;
}
}
}
g_strfreev (list);
return FALSE;
}
static gboolean
gsettings_append_to_list (const char *value)
{
gchar **current;
gchar **new;
gint size;
gboolean retval;
current = g_settings_get_strv (baul_extension_preferences,
BAUL_PREFERENCES_DISABLED_EXTENSIONS);
for (size = 0; current[size] != NULL; size++);
size += 1;
size += 1;
new = g_realloc_n (current, size, sizeof (gchar *));
new[size - 2] = g_strdup (value);
new[size - 1] = NULL;
retval = g_settings_set_strv (baul_extension_preferences,
BAUL_PREFERENCES_DISABLED_EXTENSIONS,
(const gchar **) new);
g_strfreev (new);
return retval;
}
static gboolean
gsettings_remove_from_list (const char *value)
{
gchar **current;
GArray *array;
gint i;
gboolean retval;
current = g_settings_get_strv (baul_extension_preferences,
BAUL_PREFERENCES_DISABLED_EXTENSIONS);
array = g_array_new (TRUE, TRUE, sizeof (gchar *));
for (i = 0; current[i] != NULL; i++)
{
if (g_strcmp0 (current[i], value) != 0)
array = g_array_append_val (array, current[i]);
}
retval = g_settings_set_strv (baul_extension_preferences,
BAUL_PREFERENCES_DISABLED_EXTENSIONS,
(const gchar **) array->data);
g_strfreev (current);
g_array_free (array, TRUE);
return retval;
}
/* functions related to the extension management */
static gboolean
baul_extension_get_state (const gchar *extname)
{
return !gsettings_key_has_value (extname);
}
GList *
baul_extensions_get_for_type (GType type)
{
GList *l;
GList *ret = NULL;
for (l = baul_extensions; l != NULL; l = l->next)
{
Extension *ext = l->data;
ext->state = baul_extension_get_state (ext->filename);
if (ext->state) // only load enabled extensions
{
if (G_TYPE_CHECK_INSTANCE_TYPE (G_OBJECT (ext->module), type))
{
g_object_ref (ext->module);
ret = g_list_prepend (ret, ext->module);
}
}
}
return ret;
}
GList *
baul_extensions_get_list (void)
{
return baul_extensions;
}
void
baul_extension_register (gchar *filename, GObject *module)
{
gboolean ext_state = TRUE; // new extensions are enabled by default.
gboolean ext_python = FALSE;
gchar *ext_filename;
ext_filename = g_strndup (filename, strlen(filename) - 3);
ext_state = baul_extension_get_state (ext_filename);
if (g_str_has_suffix (filename, ".py")) {
ext_python = TRUE;
}
Extension *ext = extension_new (ext_filename, ext_state, ext_python, module);
baul_extensions = g_list_append (baul_extensions, ext);
}
gboolean
baul_extension_set_state (Extension *ext, gboolean new_state)
{
if (ext)
{
g_return_val_if_fail (ext->state != new_state, FALSE);
ext->state = new_state;
}
gboolean retval;
if (new_state) {
retval = gsettings_remove_from_list (ext->filename);
}
else {
retval = gsettings_append_to_list (ext->filename);
}
g_return_val_if_fail (retval == TRUE, FALSE);
return TRUE;
}
|