[gnome-control-center] search: Use g_auto for variables
- From: Georges Basile Stavracas Neto <gbsneto src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-control-center] search: Use g_auto for variables
- Date: Wed, 30 May 2018 23:29:34 +0000 (UTC)
commit 58a64323c492b0e46cf25951bbebcccfed1c3da3
Author: Robert Ancell <robert ancell canonical com>
Date: Tue May 29 12:20:38 2018 +1200
search: Use g_auto for variables
panels/search/cc-search-locations-dialog.c | 135 ++++++++++-------------------
panels/search/cc-search-panel.c | 121 ++++++++++----------------
2 files changed, 89 insertions(+), 167 deletions(-)
---
diff --git a/panels/search/cc-search-locations-dialog.c b/panels/search/cc-search-locations-dialog.c
index 132754722..2959e3b0f 100644
--- a/panels/search/cc-search-locations-dialog.c
+++ b/panels/search/cc-search-locations-dialog.c
@@ -87,35 +87,24 @@ place_free (Place * p)
g_slice_free (Place, p);
}
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (Place, place_free)
+
static GList *
get_bookmarks (void)
{
- GFile *file;
- gchar *contents;
- gchar *path;
- gchar **lines;
- GList *bookmarks;
+ g_autoptr(GFile) file = NULL;
+ g_autofree gchar *contents = NULL;
+ g_autofree gchar *path = NULL;
+ GList *bookmarks = NULL;
GError *error = NULL;
path = g_build_filename (g_get_user_config_dir (), "gtk-3.0",
"bookmarks", NULL);
file = g_file_new_for_path (path);
- g_free (path);
-
- contents = NULL;
- g_file_load_contents (file, NULL, &contents, NULL, NULL, &error);
- g_object_unref (file);
-
- bookmarks = NULL;
- lines = NULL;
-
- if (error != NULL)
- {
- g_error_free (error);
- }
- else
+ if (g_file_load_contents (file, NULL, &contents, NULL, NULL, &error))
{
gint idx;
+ g_auto(GStrv) lines = NULL;
lines = g_strsplit (contents, "\n", -1);
for (idx = 0; lines[idx]; idx++)
@@ -151,9 +140,6 @@ get_bookmarks (void)
}
}
- g_strfreev (lines);
- g_free (contents);
-
return g_list_reverse (bookmarks);
}
@@ -259,7 +245,7 @@ path_from_tracker_dir (const gchar *value)
static GList *
get_tracker_locations (void)
{
- gchar **locations;
+ g_auto(GStrv) locations = NULL;
GList *list;
gint idx;
Place *location;
@@ -280,16 +266,17 @@ get_tracker_locations (void)
list = g_list_prepend (list, location);
}
- g_strfreev (locations);
-
return g_list_reverse (list);
}
static GList *
get_places_list (void)
{
- GList *list, *l;
- GHashTable *places;
+ g_autoptr(GList) xdg_list = NULL;
+ g_autoptr(GList) tracker_list = NULL;
+ g_autoptr(GList) bookmark_list = NULL;
+ GList *l;
+ g_autoptr(GHashTable) places = NULL;
Place *place, *old_place;
GList *places_list;
@@ -303,53 +290,45 @@ get_places_list (void)
g_hash_table_insert (places, place->location, place);
/* first, load the XDG dirs */
- list = get_xdg_dirs ();
- for (l = list; l != NULL; l = l->next)
+ xdg_list = get_xdg_dirs ();
+ for (l = xdg_list; l != NULL; l = l->next)
{
place = l->data;
g_hash_table_insert (places, place->location, place);
}
- g_list_free (list);
/* then, insert all the tracker locations that are not XDG dirs */
- list = get_tracker_locations ();
- for (l = list; l != NULL; l = l->next)
+ tracker_list = get_tracker_locations ();
+ for (l = tracker_list; l != NULL; l = l->next)
{
- place = l->data;
- old_place = g_hash_table_lookup (places, place->location);
+ g_autoptr(Place) p = l->data;
+ old_place = g_hash_table_lookup (places, p->location);
if (old_place == NULL)
- g_hash_table_insert (places, place->location, place);
- else
- place_free (place);
+ g_hash_table_insert (places, p->location, g_steal_pointer (&p));
}
- g_list_free (list);
/* finally, load bookmarks, and possibly update attributes */
- list = get_bookmarks ();
- for (l = list; l != NULL; l = l->next)
+ bookmark_list = get_bookmarks ();
+ for (l = bookmark_list; l != NULL; l = l->next)
{
- place = l->data;
- old_place = g_hash_table_lookup (places, place->location);
+ g_autoptr(Place) p = l->data;
+ old_place = g_hash_table_lookup (places, p->location);
if (old_place == NULL)
{
- g_hash_table_insert (places, place->location, place);
+ g_hash_table_insert (places, p->location, g_steal_pointer (&p));
}
else
{
g_free (old_place->display_name);
- old_place->display_name = g_strdup (place->display_name);
+ old_place->display_name = g_strdup (p->display_name);
if (old_place->place_type == PLACE_OTHER)
old_place->place_type = PLACE_BOOKMARKS;
-
- place_free (place);
}
}
- g_list_free (list);
places_list = g_hash_table_get_values (places);
g_hash_table_steal_all (places);
- g_hash_table_unref (places);
return places_list;
}
@@ -360,7 +339,7 @@ switch_tracker_get_mapping (GValue *value,
gpointer user_data)
{
Place *place = user_data;
- const gchar **locations;
+ g_autofree const gchar **locations = NULL;
GFile *location;
gint idx;
gboolean found;
@@ -377,8 +356,6 @@ switch_tracker_get_mapping (GValue *value,
break;
}
- g_free (locations);
-
g_value_set_boolean (value, found);
return TRUE;
}
@@ -387,8 +364,8 @@ static GPtrArray *
place_get_new_settings_values (Place *place,
gboolean remove)
{
- gchar **values;
- gchar *path;
+ g_auto(GStrv) values = NULL;
+ g_autofree gchar *path = NULL;
GPtrArray *new_values;
const gchar *tracker_dir;
gboolean found;
@@ -419,9 +396,6 @@ place_get_new_settings_values (Place *place,
g_ptr_array_add (new_values, NULL);
- g_strfreev (values);
- g_free (path);
-
return new_values;
}
@@ -431,17 +405,12 @@ switch_tracker_set_mapping (const GValue *value,
gpointer user_data)
{
Place *place = user_data;
- GPtrArray *new_values;
+ g_autoptr(GPtrArray) new_values = NULL;
gboolean remove;
- GVariant *variant;
remove = !g_value_get_boolean (value);
new_values = place_get_new_settings_values (place, remove);
- variant = g_variant_new_strv ((const gchar **) new_values->pdata, -1);
-
- g_ptr_array_unref (new_values);
-
- return variant;
+ return g_variant_new_strv ((const gchar **) new_values->pdata, -1);
}
static void
@@ -451,8 +420,8 @@ place_query_info_ready (GObject *source,
{
GtkWidget *row, *box, *w;
Place *place;
- GFileInfo *info;
- gchar *path;
+ g_autoptr(GFileInfo) info = NULL;
+ g_autofree gchar *path = NULL;
info = g_file_query_info_finish (G_FILE (source), res, NULL);
if (!info)
@@ -470,8 +439,6 @@ place_query_info_ready (GObject *source,
else
place->settings_key = TRACKER_KEY_RECURSIVE_DIRECTORIES;
- g_free (path);
-
w = gtk_label_new (place->display_name);
gtk_container_add (GTK_CONTAINER (box), w);
@@ -486,7 +453,6 @@ place_query_info_ready (GObject *source,
place, NULL);
gtk_widget_show_all (row);
- g_object_unref (info);
}
static void
@@ -494,14 +460,12 @@ remove_button_clicked (GtkWidget *widget,
gpointer user_data)
{
GtkWidget *row = user_data;
- GPtrArray *new_values;
- Place *place;
+ g_autoptr(GPtrArray) new_values = NULL;
+ Place *place;
place = g_object_get_data (G_OBJECT (row), "place");
new_values = place_get_new_settings_values (place, TRUE);
g_settings_set_strv (tracker_preferences, place->settings_key, (const gchar **) new_values->pdata);
-
- g_ptr_array_unref (new_values);
}
static gint
@@ -511,7 +475,7 @@ place_compare_func (gconstpointer a,
{
GtkWidget *child_a, *child_b;
Place *place_a, *place_b;
- gchar *path;
+ g_autofree gchar *path = NULL;
gboolean is_home;
child_a = GTK_WIDGET (a);
@@ -522,7 +486,6 @@ place_compare_func (gconstpointer a,
path = g_file_get_path (place_a->location);
is_home = (g_strcmp0 (path, g_get_home_dir ()) == 0);
- g_free (path);
if (is_home)
return -1;
@@ -573,7 +536,8 @@ create_row_for_place (Place *place)
static void
populate_list_boxes (CcSearchLocationsDialog *self)
{
- GList *places, *l;
+ g_autoptr(GList) places = NULL;
+ GList *l;
Place *place;
GtkWidget *row;
@@ -598,8 +562,6 @@ populate_list_boxes (CcSearchLocationsDialog *self)
g_assert_not_reached ();
}
}
-
- g_list_free (places);
}
static void
@@ -607,8 +569,8 @@ add_file_chooser_response (GtkDialog *widget,
GtkResponseType response,
gpointer user_data)
{
- Place *place;
- GPtrArray *new_values;
+ g_autoptr(Place) place = NULL;
+ g_autoptr(GPtrArray) new_values = NULL;
if (response != GTK_RESPONSE_OK)
{
@@ -624,9 +586,7 @@ add_file_chooser_response (GtkDialog *widget,
new_values = place_get_new_settings_values (place, FALSE);
g_settings_set_strv (tracker_preferences, place->settings_key, (const gchar **) new_values->pdata);
- g_ptr_array_unref (new_values);
gtk_widget_destroy (GTK_WIDGET (widget));
- place_free (place);
}
static void
@@ -651,7 +611,8 @@ add_button_clicked (GtkWidget *widget,
static void
other_places_refresh (CcSearchLocationsDialog *self)
{
- GList *places, *l;
+ g_autoptr(GList) places = NULL;
+ GList *l;
Place *place;
GtkWidget *row;
@@ -667,8 +628,6 @@ other_places_refresh (CcSearchLocationsDialog *self)
row = create_row_for_place (place);
gtk_container_add (GTK_CONTAINER (self->others_list), row);
}
-
- g_list_free (places);
}
CcSearchLocationsDialog *
@@ -701,18 +660,14 @@ gboolean
cc_search_locations_dialog_is_available (void)
{
GSettingsSchemaSource *source;
- GSettingsSchema *schema;
+ g_autoptr(GSettingsSchema) schema = NULL;
source = g_settings_schema_source_get_default ();
if (!source)
return FALSE;
schema = g_settings_schema_source_lookup (source, TRACKER_SCHEMA, TRUE);
- if (!schema)
- return FALSE;
-
- g_settings_schema_unref (schema);
- return TRUE;
+ return schema != NULL;
}
static void
diff --git a/panels/search/cc-search-panel.c b/panels/search/cc-search-panel.c
index 96dc7541f..8cc26ef1b 100644
--- a/panels/search/cc-search-panel.c
+++ b/panels/search/cc-search-panel.c
@@ -96,7 +96,7 @@ list_sort_func (gconstpointer a,
static void
search_panel_invalidate_button_state (CcSearchPanel *self)
{
- GList *children;
+ g_autoptr(GList) children = NULL;
gboolean is_first, is_last;
GtkListBoxRow *row;
@@ -111,14 +111,12 @@ search_panel_invalidate_button_state (CcSearchPanel *self)
gtk_widget_set_sensitive (self->up_button, !is_first);
gtk_widget_set_sensitive (self->down_button, !is_last);
-
- g_list_free (children);
}
static void
search_panel_invalidate_sort_order (CcSearchPanel *self)
{
- gchar **sort_order;
+ g_auto(GStrv) sort_order = NULL;
gint idx;
g_hash_table_remove_all (self->sort_order);
@@ -128,7 +126,6 @@ search_panel_invalidate_sort_order (CcSearchPanel *self)
g_hash_table_insert (self->sort_order, g_strdup (sort_order[idx]), GINT_TO_POINTER (idx + 1));
gtk_list_box_invalidate_sort (GTK_LIST_BOX (self->list_box));
- g_strfreev (sort_order);
search_panel_invalidate_button_state (self);
}
@@ -151,8 +148,9 @@ propagate_compare_func (gconstpointer a,
static void
search_panel_propagate_sort_order (CcSearchPanel *self)
{
- GList *keys, *l;
- GPtrArray *sort_order;
+ g_autoptr(GList) keys = NULL;
+ GList *l;
+ g_autoptr(GPtrArray) sort_order = NULL;
sort_order = g_ptr_array_new ();
keys = g_hash_table_get_keys (self->sort_order);
@@ -164,9 +162,6 @@ search_panel_propagate_sort_order (CcSearchPanel *self)
g_ptr_array_add (sort_order, NULL);
g_settings_set_strv (self->search_settings, "sort-order",
(const gchar **) sort_order->pdata);
-
- g_ptr_array_unref (sort_order);
- g_list_free (keys);
}
static void
@@ -194,7 +189,8 @@ search_panel_move_selected (CcSearchPanel *self,
gint idx, other_idx;
gpointer idx_ptr;
gboolean found;
- GList *children, *l, *other;
+ g_autoptr(GList) children = NULL;
+ GList *l, *other;
row = gtk_list_box_get_selected_row (GTK_LIST_BOX (self->list_box));
app_info = g_object_get_data (G_OBJECT (row), "app-info");
@@ -288,8 +284,6 @@ search_panel_move_selected (CcSearchPanel *self,
g_hash_table_replace (self->sort_order, g_strdup (app_id), GINT_TO_POINTER (idx));
search_panel_propagate_sort_order (self);
-
- g_list_free (children);
}
static void
@@ -330,11 +324,10 @@ switch_settings_mapping_set_generic (const GValue *value,
{
CcSearchPanel *self = g_object_get_data (G_OBJECT (row), "self");
GAppInfo *app_info = g_object_get_data (G_OBJECT (row), "app-info");
- gchar **apps;
- GPtrArray *new_apps;
+ g_auto(GStrv) apps = NULL;
+ g_autoptr(GPtrArray) new_apps = NULL;
gint idx;
gboolean remove, found;
- GVariant *variant;
remove = !!g_value_get_boolean (value) == !!default_enabled;
found = FALSE;
@@ -360,11 +353,7 @@ switch_settings_mapping_set_generic (const GValue *value,
g_ptr_array_add (new_apps, NULL);
- variant = g_variant_new_strv ((const gchar **) new_apps->pdata, -1);
- g_ptr_array_unref (new_apps);
- g_strfreev (apps);
-
- return variant;
+ return g_variant_new_strv ((const gchar **) new_apps->pdata, -1);
}
static GVariant *
@@ -392,7 +381,7 @@ switch_settings_mapping_get_generic (GValue *value,
gboolean default_enabled)
{
GAppInfo *app_info = g_object_get_data (G_OBJECT (row), "app-info");
- const gchar **apps;
+ g_autofree const gchar **apps = NULL;
gint idx;
gboolean found;
@@ -408,7 +397,6 @@ switch_settings_mapping_get_generic (GValue *value,
}
}
- g_free (apps);
g_value_set_boolean (value, !!default_enabled != !!found);
return TRUE;
@@ -438,7 +426,7 @@ search_panel_add_one_app_info (CcSearchPanel *self,
gboolean default_enabled)
{
GtkWidget *row, *box, *w;
- GIcon *icon;
+ g_autoptr(GIcon) icon = NULL;
gint width, height;
/* gnome-control-center is special cased in the shell,
@@ -470,7 +458,6 @@ search_panel_add_one_app_info (CcSearchPanel *self,
gtk_icon_size_lookup (GTK_ICON_SIZE_DND, &width, &height);
gtk_image_set_pixel_size (GTK_IMAGE (w), MAX (width, height));
gtk_container_add (GTK_CONTAINER (box), w);
- g_object_unref (icon);
w = gtk_label_new (g_app_info_get_name (app_info));
gtk_container_add (GTK_CONTAINER (box), w);
@@ -505,10 +492,11 @@ static void
search_panel_add_one_provider (CcSearchPanel *self,
GFile *provider)
{
- gchar *path, *desktop_id;
- GKeyFile *keyfile;
- GAppInfo *app_info;
- GError *error = NULL;
+ g_autofree gchar *path = NULL;
+ g_autofree gchar *desktop_id = NULL;
+ g_autoptr(GKeyFile) keyfile = NULL;
+ g_autoptr(GAppInfo) app_info = NULL;
+ g_autoptr(GError) error = NULL;
gboolean default_disabled;
path = g_file_get_path (provider);
@@ -519,13 +507,13 @@ search_panel_add_one_provider (CcSearchPanel *self,
{
g_warning ("Error loading %s: %s - search provider will be ignored",
path, error->message);
- goto out;
+ return;
}
if (!g_key_file_has_group (keyfile, SHELL_PROVIDER_GROUP))
{
g_debug ("Shell search provider group missing from '%s', ignoring", path);
- goto out;
+ return;
}
desktop_id = g_key_file_get_string (keyfile, SHELL_PROVIDER_GROUP,
@@ -535,7 +523,7 @@ search_panel_add_one_provider (CcSearchPanel *self,
{
g_warning ("Unable to read desktop ID from %s: %s - search provider will be ignored",
path, error->message);
- goto out;
+ return;
}
app_info = G_APP_INFO (g_desktop_app_info_new (desktop_id));
@@ -544,20 +532,12 @@ search_panel_add_one_provider (CcSearchPanel *self,
{
g_debug ("Could not find application with desktop ID '%s' referenced in '%s', ignoring",
desktop_id, path);
- g_free (desktop_id);
- goto out;
+ return;
}
- g_free (desktop_id);
default_disabled = g_key_file_get_boolean (keyfile, SHELL_PROVIDER_GROUP,
"DefaultDisabled", NULL);
search_panel_add_one_app_info (self, app_info, !default_disabled);
- g_object_unref (app_info);
-
- out:
- g_free (path);
- g_clear_error (&error);
- g_key_file_unref (keyfile);
}
static void
@@ -565,18 +545,15 @@ search_providers_discover_ready (GObject *source,
GAsyncResult *result,
gpointer user_data)
{
- GList *providers, *l;
- GFile *provider;
+ g_autoptr(GList) providers = NULL;
+ GList *l;
CcSearchPanel *self = CC_SEARCH_PANEL (source);
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
providers = g_task_propagate_pointer (G_TASK (result), &error);
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
- {
- g_error_free (error);
- return;
- }
+ return;
g_clear_object (&self->load_cancellable);
@@ -588,16 +565,14 @@ search_providers_discover_ready (GObject *source,
for (l = providers; l != NULL; l = l->next)
{
- provider = l->data;
+ g_autoptr(GFile) provider = l->data;
search_panel_add_one_provider (self, provider);
- g_object_unref (provider);
}
/* propagate a write to GSettings, to make sure we always have
* all the providers in the list.
*/
search_panel_propagate_sort_order (self);
- g_list_free (providers);
}
static GList *
@@ -605,11 +580,10 @@ search_providers_discover_one_directory (const gchar *system_dir,
GCancellable *cancellable)
{
GList *providers = NULL;
- gchar *providers_path;
- GFile *providers_location, *provider;
- GFileInfo *info;
- GFileEnumerator *enumerator;
- GError *error = NULL;
+ g_autofree gchar *providers_path = NULL;
+ g_autoptr(GFile) providers_location = NULL;
+ g_autoptr(GFileEnumerator) enumerator = NULL;
+ g_autoptr(GError) error = NULL;
providers_path = g_build_filename (system_dir, "gnome-shell", "search-providers", NULL);
providers_location = g_file_new_for_path (providers_path);
@@ -625,32 +599,26 @@ search_providers_discover_one_directory (const gchar *system_dir,
!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
g_warning ("Error opening %s: %s - search provider configuration won't be possible",
providers_path, error->message);
- g_clear_error (&error);
- goto out;
+ return NULL;
}
- while ((info = g_file_enumerator_next_file (enumerator, cancellable, &error)) != NULL)
+ while (TRUE)
{
+ g_autoptr(GFileInfo) info = NULL;
+ GFile *provider;
+
+ info = g_file_enumerator_next_file (enumerator, cancellable, &error);
+ if (info == NULL)
+ {
+ if (error != NULL && !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+ g_warning ("Error reading from %s: %s - search providers might be missing from the panel",
+ providers_path, error->message);
+ return providers;
+ }
provider = g_file_get_child (providers_location, g_file_info_get_name (info));
providers = g_list_prepend (providers, provider);
- g_object_unref (info);
- }
-
- if (error != NULL)
- {
- if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
- g_warning ("Error reading from %s: %s - search providers might be missing from the panel",
- providers_path, error->message);
- g_clear_error (&error);
}
-
- out:
- g_clear_object (&enumerator);
- g_clear_object (&providers_location);
- g_free (providers_path);
-
- return providers;
}
static void
@@ -682,13 +650,12 @@ search_providers_discover_thread (GTask *task,
static void
populate_search_providers (CcSearchPanel *self)
{
- GTask *task;
+ g_autoptr(GTask) task = NULL;
self->load_cancellable = g_cancellable_new ();
task = g_task_new (self, self->load_cancellable,
search_providers_discover_ready, self);
g_task_run_in_thread (task, search_providers_discover_thread);
- g_object_unref (task);
}
static void
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]