GCC Code Coverage Report


Directory: ./
File: panels/applications/search.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 49 0.0%
Functions: 0 3 0.0%
Branches: 0 42 0.0%

Line Branch Exec Source
1 /* search.c
2 *
3 * Copyright 2018 Matthias Clasen <matthias.clasen@gmail.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #include <config.h>
22
23 #include "search.h"
24
25
26 #define SHELL_PROVIDER_GROUP "Shell Search Provider"
27
28 static void
29 add_one_provider (GHashTable *search_providers,
30 GFile *file)
31 {
32 g_autoptr(GKeyFile) keyfile = NULL;
33 g_autoptr(GError) error = NULL;
34 g_autofree gchar *app_id = NULL;
35 g_autofree gchar *path = NULL;
36 gboolean default_disabled;
37
38 path = g_file_get_path (file);
39 keyfile = g_key_file_new ();
40 g_key_file_load_from_file (keyfile, path, G_KEY_FILE_NONE, &error);
41
42 if (error != NULL)
43 {
44 g_warning ("Error loading %s: %s - search provider will be ignored",
45 path, error->message);
46 return;
47 }
48
49 if (!g_key_file_has_group (keyfile, SHELL_PROVIDER_GROUP))
50 {
51 g_debug ("Shell search provider group missing from '%s', ignoring", path);
52 return;
53 }
54
55 app_id = g_key_file_get_string (keyfile, SHELL_PROVIDER_GROUP, "DesktopId", &error);
56
57 if (error != NULL)
58 {
59 g_warning ("Unable to read desktop ID from %s: %s - search provider will be ignored",
60 path, error->message);
61 return;
62 }
63
64 if (g_str_has_suffix (app_id, ".desktop"))
65 app_id[strlen (app_id) - strlen (".desktop")] = '\0';
66
67 default_disabled = g_key_file_get_boolean (keyfile, SHELL_PROVIDER_GROUP, "DefaultDisabled", NULL);
68
69 g_hash_table_insert (search_providers, g_strdup (app_id), GINT_TO_POINTER (default_disabled));
70 }
71
72 static void
73 parse_search_providers_one_dir (GHashTable *search_providers,
74 const gchar *system_dir)
75 {
76 g_autoptr(GFileEnumerator) enumerator = NULL;
77 g_autoptr(GError) error = NULL;
78 g_autoptr(GFile) providers_location = NULL;
79 g_autofree gchar *providers_path = NULL;
80
81 providers_path = g_build_filename (system_dir, "gnome-shell", "search-providers", NULL);
82 providers_location = g_file_new_for_path (providers_path);
83
84 enumerator = g_file_enumerate_children (providers_location,
85 "standard::type,standard::name,standard::content-type",
86 G_FILE_QUERY_INFO_NONE,
87 NULL, &error);
88
89 if (error != NULL)
90 {
91 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) &&
92 !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
93 g_warning ("Error opening %s: %s - search provider configuration won't be possible",
94 providers_path, error->message);
95 return;
96 }
97
98 while (TRUE)
99 {
100 GFile *provider = NULL;
101
102 if (!g_file_enumerator_iterate (enumerator, NULL, &provider, NULL, &error))
103 {
104 g_warning ("Error while reading %s: %s - search provider configuration won't be possible",
105 providers_path, error->message);
106 return;
107 }
108
109 if (provider == NULL)
110 break;
111
112 add_one_provider (search_providers, provider);
113 }
114 }
115
116 /* parse gnome-shell/search-provider files and return a string->boolean hash table */
117 GHashTable *
118 parse_search_providers (void)
119 {
120 GHashTable *search_providers;
121 const gchar * const *dirs;
122 gint i;
123
124 search_providers = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
125
126 dirs = g_get_system_data_dirs ();
127
128 for (i = 0; dirs[i]; i++)
129 parse_search_providers_one_dir (search_providers, dirs[i]);
130
131 return search_providers;
132 }
133
134