diff options
author | Robert Ancell <robert.ancell@canonical.com> | 2017-09-04 10:42:56 +1200 |
---|---|---|
committer | Robert Ancell <robert.ancell@canonical.com> | 2018-01-08 16:27:43 +1300 |
commit | 3c0508cd9d5f8489437dca1ec423385e4951aaae (patch) | |
tree | db843bf80e9e24b7fdfd9ff7c65ff2a21df3a8bd | |
parent | ba6251b22c7fea4680149df22ae438aa478547d5 (diff) | |
download | gnome-control-center-wip/rancell/background-g-auto.tar.gz |
background: Use g_auto for variableswip/rancell/background-g-auto
-rw-r--r-- | panels/background/bg-colors-source.c | 61 | ||||
-rw-r--r-- | panels/background/bg-pictures-source.c | 152 | ||||
-rw-r--r-- | panels/background/bg-wallpapers-source.c | 18 | ||||
-rw-r--r-- | panels/background/cc-background-chooser-dialog.c | 23 | ||||
-rw-r--r-- | panels/background/cc-background-grilo-miner.c | 22 | ||||
-rw-r--r-- | panels/background/cc-background-item.c | 63 | ||||
-rw-r--r-- | panels/background/cc-background-panel.c | 114 | ||||
-rw-r--r-- | panels/background/cc-background-xml.c | 135 | ||||
-rw-r--r-- | panels/background/test-chooser-dialog.c | 3 |
9 files changed, 218 insertions, 373 deletions
diff --git a/panels/background/bg-colors-source.c b/panels/background/bg-colors-source.c index f46acb6d0..34ab9ac7d 100644 --- a/panels/background/bg-colors-source.c +++ b/panels/background/bg-colors-source.c @@ -56,7 +56,7 @@ struct { { G_DESKTOP_BACKGROUND_SHADING_SOLID, -1, "#7a634b" }, }; -static char * +static gchar * get_colors_path (void) { return g_build_filename (g_get_user_config_dir (), "gnome-control-center", "backgrounds", "colors.ini", NULL); @@ -76,8 +76,8 @@ bg_colors_source_add_color (BgColorsSource *self, GtkTreeRowReference **ret_row_ref) { CcBackgroundItemFlags flags; - CcBackgroundItem *item; - GdkPixbuf *pixbuf; + g_autoptr(CcBackgroundItem) item = NULL; + g_autoptr(GdkPixbuf) pixbuf = NULL; cairo_surface_t *surface; int scale_factor; int thumbnail_height, thumbnail_width; @@ -115,6 +115,7 @@ bg_colors_source_add_color (BgColorsSource *self, 0, surface, 1, item, -1); + cairo_surface_destroy (surface); if (ret_row_ref) { @@ -124,21 +125,17 @@ bg_colors_source_add_color (BgColorsSource *self, *ret_row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (store), path); gtk_tree_path_free (path); } - - cairo_surface_destroy (surface); - g_object_unref (pixbuf); - g_object_unref (item); } static void bg_colors_source_constructed (GObject *object) { BgColorsSource *self = BG_COLORS_SOURCE (object); - GnomeDesktopThumbnailFactory *thumb_factory; + g_autoptr(GnomeDesktopThumbnailFactory) thumb_factory = NULL; guint i; GtkListStore *store; - GKeyFile *keyfile; - char *path; + g_autoptr(GKeyFile) keyfile = NULL; + g_autofree gchar *path = NULL; G_OBJECT_CLASS (bg_colors_source_parent_class)->constructed (object); @@ -154,21 +151,14 @@ bg_colors_source_constructed (GObject *object) path = get_colors_path (); if (g_key_file_load_from_file (keyfile, path, G_KEY_FILE_NONE, NULL)) { - char **colors; + g_auto(GStrv) colors = NULL; colors = g_key_file_get_string_list (keyfile, "Colors", "custom-colors", NULL, NULL); for (i = 0; colors != NULL && colors[i] != NULL; i++) { bg_colors_source_add_color (self, thumb_factory, store, colors[i], NULL); } - - if (colors) - g_strfreev (colors); } - g_key_file_unref (keyfile); - g_free (path); - - g_object_unref (thumb_factory); } gboolean @@ -176,14 +166,15 @@ bg_colors_source_add (BgColorsSource *self, GdkRGBA *rgba, GtkTreeRowReference **ret_row_ref) { - GnomeDesktopThumbnailFactory *thumb_factory; + g_autoptr(GnomeDesktopThumbnailFactory) thumb_factory = NULL; GtkListStore *store; - gchar *c; - char **colors; + g_autofree gchar *c = NULL; + g_auto(GStrv) colors = NULL; gsize len; - GKeyFile *keyfile; - GError *error = NULL; - char *path; + g_autoptr(GKeyFile) keyfile = NULL; + g_autoptr(GError) error = NULL; + g_autofree gchar *dir = NULL; + g_autofree gchar *path = NULL; c = g_strdup_printf ("#%02x%02x%02x", (int)(255*rgba->red), @@ -195,12 +186,9 @@ bg_colors_source_add (BgColorsSource *self, bg_colors_source_add_color (self, thumb_factory, store, c, ret_row_ref); - g_object_unref (thumb_factory); - /* Save to the keyfile */ - path = get_colors_dir (); - g_mkdir_with_parents (path, 0700); - g_free (path); + dir = get_colors_dir (); + g_mkdir_with_parents (dir, 0700); path = get_colors_path (); colors = NULL; @@ -211,15 +199,12 @@ bg_colors_source_add (BgColorsSource *self, colors = g_key_file_get_string_list (keyfile, "Colors", "custom-colors", &len, NULL); if (len == 0 && colors != NULL) - { - g_strfreev (colors); - colors = NULL; - } + g_clear_pointer (&colors, g_strfreev); if (colors == NULL) { colors = g_new0 (char *, 2); - colors[0] = c; + colors[0] = g_steal_pointer (&c); len = 1; } else @@ -234,7 +219,7 @@ bg_colors_source_add (BgColorsSource *self, colors[i] = NULL; } - new_colors[len] = c; + new_colors[len] = g_steal_pointer (&c); len++; g_strfreev (colors); @@ -244,13 +229,7 @@ bg_colors_source_add (BgColorsSource *self, g_key_file_set_string_list (keyfile, "Colors", "custom-colors", (const gchar * const*) colors, len); if (!g_key_file_save_to_file (keyfile, path, &error)) - { g_warning ("Could not save custom color: %s", error->message); - g_error_free (error); - } - - g_key_file_unref (keyfile); - g_strfreev (colors); return TRUE; } diff --git a/panels/background/bg-pictures-source.c b/panels/background/bg-pictures-source.c index 0dd2b7aeb..a37682d63 100644 --- a/panels/background/bg-pictures-source.c +++ b/panels/background/bg-pictures-source.c @@ -170,8 +170,8 @@ picture_scaled (GObject *source_object, { BgPicturesSource *bg_source; CcBackgroundItem *item; - GError *error = NULL; - GdkPixbuf *pixbuf = NULL; + g_autoptr(GError) error = NULL; + g_autoptr(GdkPixbuf) pixbuf = NULL; const char *software; const char *uri; GtkTreeIter iter; @@ -192,8 +192,7 @@ picture_scaled (GObject *source_object, remove_placeholder (BG_PICTURES_SOURCE (user_data), item); } - g_error_free (error); - goto out; + return; } /* since we were not cancelled, we can now cast user_data @@ -212,7 +211,7 @@ picture_scaled (GObject *source_object, { g_debug ("Ignored URL '%s' as it's a screenshot from gnome-screenshot", uri); remove_placeholder (BG_PICTURES_SOURCE (user_data), item); - goto out; + return; } /* Process embedded orientation */ @@ -222,7 +221,7 @@ picture_scaled (GObject *source_object, { /* the width and height of pixbuf we requested are wrong for EXIF * orientations 5, 6, 7 and 8. the file has to be reloaded. */ - GFile *file; + g_autoptr(GFile) file = NULL; file = g_file_new_for_uri (uri); g_object_set_data (G_OBJECT (item), "needs-rotation", GINT_TO_POINTER (TRUE)); @@ -230,8 +229,7 @@ picture_scaled (GObject *source_object, g_file_read_async (G_FILE (file), G_PRIORITY_DEFAULT, bg_source->cancellable, picture_opened_for_read, bg_source); - g_object_unref (file); - goto out; + return; } pixbuf = swap_rotated_pixbuf (pixbuf); @@ -265,10 +263,7 @@ picture_scaled (GObject *source_object, bg_pictures_source_get_unique_filename (uri), GINT_TO_POINTER (TRUE)); - - out: g_clear_pointer (&surface, (GDestroyNotify) cairo_surface_destroy); - g_clear_object (&pixbuf); } static void @@ -278,8 +273,8 @@ picture_opened_for_read (GObject *source_object, { BgPicturesSource *bg_source; CcBackgroundItem *item; - GFileInputStream *stream; - GError *error = NULL; + g_autoptr(GFileInputStream) stream = NULL; + g_autoptr(GError) error = NULL; gint thumbnail_height; gint thumbnail_width; gboolean needs_rotation; @@ -290,13 +285,11 @@ picture_opened_for_read (GObject *source_object, { if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { - char *filename = g_file_get_path (G_FILE (source_object)); + g_autofree gchar *filename = g_file_get_path (G_FILE (source_object)); g_warning ("Failed to load picture '%s': %s", filename, error->message); remove_placeholder (BG_PICTURES_SOURCE (user_data), item); - g_free (filename); } - g_error_free (error); return; } @@ -325,7 +318,6 @@ picture_opened_for_read (GObject *source_object, TRUE, bg_source->cancellable, picture_scaled, bg_source); - g_object_unref (stream); } static void @@ -335,22 +327,21 @@ picture_copied_for_read (GObject *source_object, { BgPicturesSource *bg_source; CcBackgroundItem *item; - GError *error = NULL; + g_autoptr(GError) error = NULL; GFile *thumbnail_file = G_FILE (source_object); GFile *native_file; if (!g_file_copy_finish (thumbnail_file, res, &error)) { if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - goto out; + return; else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS)) { - gchar *uri; + g_autofree gchar *uri = NULL; uri = g_file_get_uri (thumbnail_file); g_warning ("Failed to download '%s': %s", uri, error->message); - g_free (uri); - goto out; + return; } } @@ -364,9 +355,6 @@ picture_copied_for_read (GObject *source_object, bg_source->cancellable, picture_opened_for_read, bg_source); - - out: - g_clear_error (&error); } static gboolean @@ -393,9 +381,10 @@ static cairo_surface_t * get_content_loading_icon (BgSource *source) { GtkIconTheme *theme; - GtkIconInfo *icon_info; - GdkPixbuf *pixbuf, *ret; - GError *error = NULL; + g_autoptr(GtkIconInfo) icon_info = NULL; + g_autoptr(GdkPixbuf) pixbuf = NULL; + g_autoptr(GdkPixbuf) ret = NULL; + g_autoptr(GError) error = NULL; int scale_factor; cairo_surface_t *surface; int thumbnail_height; @@ -416,8 +405,6 @@ get_content_loading_icon (BgSource *source) if (pixbuf == NULL) { g_warning ("Failed to load placeholder icon: %s", error->message); - g_clear_error (&error); - g_clear_object (&icon_info); return NULL; } @@ -434,12 +421,9 @@ get_content_loading_icon (BgSource *source) ret, (thumbnail_width - gdk_pixbuf_get_width (pixbuf)) / 2, (thumbnail_height - gdk_pixbuf_get_height (pixbuf)) / 2); - g_object_unref (pixbuf); scale_factor = bg_source_get_scale_factor (source); surface = gdk_cairo_surface_create_from_pixbuf (ret, scale_factor, NULL); - g_object_unref (ret); - g_clear_object (&icon_info); return surface; } @@ -447,12 +431,11 @@ get_content_loading_icon (BgSource *source) static GFile * bg_pictures_source_get_cache_file (void) { - char *path; + g_autofree gchar *path = NULL; GFile *file; path = bg_pictures_source_get_cache_path (); file = g_file_new_for_path (path); - g_free (path); return file; } @@ -464,18 +447,19 @@ add_single_file (BgPicturesSource *bg_source, guint64 mtime, GtkTreeRowReference **ret_row_ref) { - CcBackgroundItem *item = NULL; + g_autoptr(CcBackgroundItem) item = NULL; CcBackgroundItemFlags flags = 0; GtkListStore *store; GtkTreeIter iter; GtkTreePath *path = NULL; GtkTreeRowReference *row_ref = NULL; cairo_surface_t *surface = NULL; - char *source_uri = NULL; - char *uri = NULL; + g_autofree gchar *source_uri = NULL; + g_autofree gchar *uri = NULL; gboolean needs_download; gboolean retval = FALSE; - GFile *pictures_dir, *cache_dir; + g_autoptr(GFile) pictures_dir = NULL; + g_autoptr(GFile) cache_dir = NULL; GrlMedia *media; /* find png and jpeg files */ @@ -491,8 +475,6 @@ add_single_file (BgPicturesSource *bg_source, cache_dir = bg_pictures_source_get_cache_file (); needs_download = !g_file_has_parent (file, pictures_dir) && !g_file_has_parent (file, cache_dir); - g_object_unref (pictures_dir); - g_object_unref (cache_dir); if (!needs_download) { @@ -501,8 +483,7 @@ add_single_file (BgPicturesSource *bg_source, } else { - source_uri = uri; - uri = NULL; + source_uri = g_steal_pointer (&uri); } item = cc_background_item_new (uri); @@ -545,10 +526,10 @@ add_single_file (BgPicturesSource *bg_source, } else { - GFile *native_file; - GFile *thumbnail_file = NULL; - gchar *native_dir; - gchar *native_path; + g_autoptr(GFile) native_file = NULL; + g_autoptr(GFile) thumbnail_file = NULL; + g_autofree gchar *native_dir = NULL; + g_autofree gchar *native_path = NULL; const gchar *title; const gchar *thumbnail_uri; @@ -578,11 +559,6 @@ add_single_file (BgPicturesSource *bg_source, NULL, picture_copied_for_read, bg_source); - - g_clear_object (&thumbnail_file); - g_object_unref (native_file); - g_free (native_dir); - g_free (native_path); } retval = TRUE; @@ -597,10 +573,6 @@ add_single_file (BgPicturesSource *bg_source, } gtk_tree_path_free (path); g_clear_pointer (&surface, (GDestroyNotify) cairo_surface_destroy); - g_clear_object (&item); - g_object_unref (file); - g_free (source_uri); - g_free (uri); return retval; } @@ -648,7 +620,7 @@ bg_pictures_source_add (BgPicturesSource *bg_source, const char *uri, GtkTreeRowReference **ret_row_ref) { - GFile *file; + g_autoptr(GFile) file = NULL; GFileInfo *info; gboolean retval; @@ -677,7 +649,7 @@ bg_pictures_source_remove (BgPicturesSource *bg_source, cont = gtk_tree_model_get_iter_first (model, &iter); while (cont) { - CcBackgroundItem *tmp_item; + g_autoptr(CcBackgroundItem) tmp_item = NULL; const char *tmp_uri; gtk_tree_model_get (model, &iter, 1, &tmp_item, -1); @@ -693,7 +665,6 @@ bg_pictures_source_remove (BgPicturesSource *bg_source, retval = TRUE; break; } - g_object_unref (tmp_item); cont = gtk_tree_model_iter_next (model, &iter); } return retval; @@ -721,7 +692,7 @@ file_info_async_ready (GObject *source, { BgPicturesSource *bg_source; GList *files, *l; - GError *err = NULL; + g_autoptr(GError) err = NULL; GFile *parent; files = g_file_enumerator_next_files_finish (G_FILE_ENUMERATOR (source), @@ -731,7 +702,6 @@ file_info_async_ready (GObject *source, { if (!g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) g_warning ("Could not get pictures file information: %s", err->message); - g_error_free (err); g_list_foreach (files, (GFunc) g_object_unref, NULL); g_list_free (files); @@ -748,7 +718,7 @@ file_info_async_ready (GObject *source, for (l = files; l; l = g_list_next (l)) { GFileInfo *info = l->data; - GFile *file; + g_autoptr(GFile) file = NULL; file = g_file_get_child (parent, g_file_info_get_name (info)); @@ -765,8 +735,8 @@ dir_enum_async_ready (GObject *s, gpointer user_data) { BgPicturesSource *source = (BgPicturesSource *) user_data; - GFileEnumerator *enumerator; - GError *err = NULL; + g_autoptr(GFileEnumerator) enumerator = NULL; + g_autoptr(GError) err = NULL; enumerator = g_file_enumerate_children_finish (G_FILE (s), res, &err); @@ -774,7 +744,6 @@ dir_enum_async_ready (GObject *s, { if (!g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) g_warning ("Could not fill pictures source: %s", err->message); - g_error_free (err); return; } @@ -785,7 +754,6 @@ dir_enum_async_ready (GObject *s, source->cancellable, file_info_async_ready, user_data); - g_object_unref (enumerator); } char * @@ -800,13 +768,12 @@ bg_pictures_source_get_cache_path (void) static char * bg_pictures_source_get_unique_filename (const char *uri) { - GChecksum *csum; + g_autoptr(GChecksum) csum = NULL; char *ret; csum = g_checksum_new (G_CHECKSUM_SHA256); g_checksum_update (csum, (guchar *) uri, -1); ret = g_strdup (g_checksum_get_string (csum)); - g_checksum_free (csum); return ret; } @@ -814,37 +781,29 @@ bg_pictures_source_get_unique_filename (const char *uri) char * bg_pictures_source_get_unique_path (const char *uri) { - GFile *parent, *file; - char *cache_path; - char *filename; - char *ret; + g_autoptr(GFile) parent = NULL; + g_autoptr(GFile) file = NULL; + g_autofree gchar *cache_path = NULL; + g_autofree gchar *filename = NULL; cache_path = bg_pictures_source_get_cache_path (); parent = g_file_new_for_path (cache_path); - g_free (cache_path); filename = bg_pictures_source_get_unique_filename (uri); file = g_file_get_child (parent, filename); - g_free (filename); - ret = g_file_get_path (file); - g_object_unref (file); - g_object_unref (parent); - return ret; + return g_file_get_path (file); } gboolean bg_pictures_source_is_known (BgPicturesSource *bg_source, const char *uri) { - gboolean retval; - char *uuid; + g_autofree gchar *uuid = NULL; uuid = bg_pictures_source_get_unique_filename (uri); - retval = (GPOINTER_TO_INT (g_hash_table_lookup (bg_source->known_items, uuid))); - g_free (uuid); - return retval; + return GPOINTER_TO_INT (g_hash_table_lookup (bg_source->known_items, uuid)); } static int @@ -853,8 +812,8 @@ sort_func (GtkTreeModel *model, GtkTreeIter *b, BgPicturesSource *bg_source) { - CcBackgroundItem *item_a; - CcBackgroundItem *item_b; + g_autoptr(CcBackgroundItem) item_a = NULL; + g_autoptr(CcBackgroundItem) item_b = NULL; guint64 modified_a; guint64 modified_b; int retval; @@ -871,9 +830,6 @@ sort_func (GtkTreeModel *model, retval = modified_b - modified_a; - g_object_unref (item_a); - g_object_unref (item_b); - return retval; } @@ -896,10 +852,6 @@ file_info_ready (GObject *object, return; } - /* Up the ref count so we can re-use the add_single_item code path which - * reduces the ref count. - */ - g_object_ref (file); add_single_file_from_info (BG_PICTURES_SOURCE (user_data), file, info, NULL); } @@ -907,7 +859,7 @@ static void file_added (GFile *file, BgPicturesSource *self) { - char *uri; + g_autofree gchar *uri = NULL; uri = g_file_get_uri (file); if (!bg_pictures_source_is_known (self, uri)) @@ -920,8 +872,6 @@ file_added (GFile *file, file_info_ready, self); } - - g_free (uri); } static void @@ -932,7 +882,7 @@ files_changed_cb (GFileMonitor *monitor, gpointer user_data) { BgPicturesSource *self = BG_PICTURES_SOURCE (user_data); - char *uri; + g_autofree gchar *uri = NULL; switch (event_type) { @@ -943,7 +893,6 @@ files_changed_cb (GFileMonitor *monitor, case G_FILE_MONITOR_EVENT_DELETED: uri = g_file_get_uri (file); bg_pictures_source_remove (self, uri); - g_free (uri); break; default: @@ -956,7 +905,7 @@ monitor_path (BgPicturesSource *self, const char *path) { GFileMonitor *monitor; - GFile *dir; + g_autoptr(GFile) dir = NULL; g_mkdir_with_parents (path, USER_DIR_MODE); @@ -978,15 +927,13 @@ monitor_path (BgPicturesSource *self, G_CALLBACK (files_changed_cb), self); - g_object_unref (dir); - return monitor; } static void media_found_cb (BgPicturesSource *self, GrlMedia *media) { - GFile *file = NULL; + g_autoptr(GFile) file = NULL; const gchar *uri; uri = grl_media_get_url (media); @@ -999,7 +946,7 @@ static void bg_pictures_source_init (BgPicturesSource *self) { const gchar *pictures_path; - char *cache_path; + g_autofree gchar *cache_path = NULL; GtkListStore *store; self->cancellable = g_cancellable_new (); @@ -1016,7 +963,6 @@ bg_pictures_source_init (BgPicturesSource *self) cache_path = bg_pictures_source_get_cache_path (); self->cache_dir_monitor = monitor_path (self, cache_path); - g_free (cache_path); self->grl_miner = cc_background_grilo_miner_new (); g_signal_connect_swapped (self->grl_miner, "media-found", G_CALLBACK (media_found_cb), self); diff --git a/panels/background/bg-wallpapers-source.c b/panels/background/bg-wallpapers-source.c index cfb032c72..50f40b369 100644 --- a/panels/background/bg-wallpapers-source.c +++ b/panels/background/bg-wallpapers-source.c @@ -43,9 +43,9 @@ load_wallpapers (gchar *key, BgWallpapersSource *source) { GtkTreeIter iter; - GdkPixbuf *pixbuf; + g_autoptr(GdkPixbuf) pixbuf = NULL; GtkListStore *store = bg_source_get_liststore (BG_SOURCE (source)); - cairo_surface_t *surface = NULL; + cairo_surface_t *surface; gboolean deleted; gint scale_factor; gint thumbnail_height; @@ -65,7 +65,7 @@ load_wallpapers (gchar *key, thumbnail_width, thumbnail_height, scale_factor); if (pixbuf == NULL) - goto out; + return; surface = gdk_cairo_surface_create_from_pixbuf (pixbuf, scale_factor, NULL); gtk_list_store_set (store, &iter, @@ -73,11 +73,7 @@ load_wallpapers (gchar *key, 1, item, 2, cc_background_item_get_name (item), -1); - - out: g_clear_pointer (&surface, (GDestroyNotify) cairo_surface_destroy); - if (pixbuf) - g_object_unref (pixbuf); } static void @@ -100,22 +96,20 @@ static void load_default_bg (BgWallpapersSource *self) { const char * const *system_data_dirs; - char *filename; guint i; /* FIXME We could do this nicer if we had the XML source in GSettings */ system_data_dirs = g_get_system_data_dirs (); for (i = 0; system_data_dirs[i]; i++) { + g_autofree gchar *filename = NULL; + filename = g_build_filename (system_data_dirs[i], "gnome-background-properties", "adwaita.xml", NULL); - if (cc_background_xml_load_xml (self->xml, filename)) { - g_free (filename); + if (cc_background_xml_load_xml (self->xml, filename)) break; - } - g_free (filename); } } diff --git a/panels/background/cc-background-chooser-dialog.c b/panels/background/cc-background-chooser-dialog.c index da4a05732..47329cae0 100644 --- a/panels/background/cc-background-chooser-dialog.c +++ b/panels/background/cc-background-chooser-dialog.c @@ -349,7 +349,7 @@ cc_background_panel_drag_items (GtkWidget *widget, { gint i; char *uri; - gchar **uris; + g_auto(GStrv) uris = NULL; gboolean ret = FALSE; if (info == COLOR) @@ -372,8 +372,6 @@ cc_background_panel_drag_items (GtkWidget *widget, } } - g_strfreev (uris); - out: gtk_drag_finish (context, ret, FALSE, time); } @@ -452,10 +450,12 @@ cc_background_chooser_dialog_init (CcBackgroundChooserDialog *chooser) GtkWidget *label; GtkWidget *switcher; GtkStyleContext *context; - gchar *markup, *href; + g_autofree gchar *markup = NULL; + g_autofree gchar *markup2 = NULL; + g_autofree gchar *href = NULL; const gchar *pictures_dir; - gchar *pictures_dir_basename; - gchar *pictures_dir_uri; + g_autofree gchar *pictures_dir_basename = NULL; + g_autofree gchar *pictures_dir_uri = NULL; GtkTargetList *target_list; chooser->wallpapers_source = bg_wallpapers_source_new (GTK_WINDOW (chooser)); @@ -526,8 +526,7 @@ cc_background_chooser_dialog_init (CcBackgroundChooserDialog *chooser) markup = g_markup_printf_escaped ("<b><span size='large'>%s</span></b>", /* translators: No pictures were found */ _("No Pictures Found")); - gtk_label_set_markup (GTK_LABEL (label), (const gchar *) markup); - g_free (markup); + gtk_label_set_markup (GTK_LABEL (label), markup); gtk_widget_show (label); gtk_container_add (GTK_CONTAINER (labels_grid), label); label = gtk_label_new (""); @@ -550,15 +549,11 @@ cc_background_chooser_dialog_init (CcBackgroundChooserDialog *chooser) pictures_dir_uri = g_filename_to_uri (pictures_dir, NULL, NULL); href = g_markup_printf_escaped ("<a href=\"%s\">%s</a>", pictures_dir_uri, pictures_dir_basename); - g_free (pictures_dir_uri); - g_free (pictures_dir_basename); /* translators: %s here is the name of the Pictures directory, the string should be translated in * the context "You can add images to your Pictures folder and they will show up here" */ - markup = g_strdup_printf (_("You can add images to your %s folder and they will show up here"), href); - g_free (href); - gtk_label_set_markup (GTK_LABEL (label), (const gchar *) markup); - g_free (markup); + markup2 = g_strdup_printf (_("You can add images to your %s folder and they will show up here"), href); + gtk_label_set_markup (GTK_LABEL (label), markup2); gtk_widget_show (label); gtk_container_add (GTK_CONTAINER (labels_grid), label); diff --git a/panels/background/cc-background-grilo-miner.c b/panels/background/cc-background-grilo-miner.c index 34995626b..85c09daf6 100644 --- a/panels/background/cc-background-grilo-miner.c +++ b/panels/background/cc-background-grilo-miner.c @@ -101,9 +101,9 @@ searched_online_source (GrlSource *source, const GError *error) { CcBackgroundGriloMiner *self = CC_BACKGROUND_GRILO_MINER (user_data); - GFile *cache_file = NULL; + g_autoptr(GFile) cache_file = NULL; const gchar *uri; - gchar *cache_path = NULL; + g_autofree gchar *cache_path = NULL; if (error != NULL) { @@ -129,8 +129,6 @@ searched_online_source (GrlSource *source, self); out: - g_clear_object (&cache_file); - g_free (cache_path); if (remaining == 0) g_object_unref (self); } @@ -165,7 +163,7 @@ add_online_source_cb (CcBackgroundGriloMiner *self, for (l = self->accounts; l != NULL && !found; l = l->next) { GoaObject *goa_object = GOA_OBJECT (l->data); - gchar *account_id; + g_autofree gchar *account_id = NULL; account_id = get_grilo_id (goa_object); if (g_strcmp0 (source_id, account_id) == 0) @@ -173,8 +171,6 @@ add_online_source_cb (CcBackgroundGriloMiner *self, query_online_source (self, source); found = TRUE; } - - g_free (account_id); } } @@ -184,7 +180,7 @@ client_async_ready (GObject *source, gpointer user_data) { CcBackgroundGriloMiner *self; - GError *error = NULL; + g_autoptr(GError) error = NULL; GList *accounts = NULL; GList *photo_accounts = NULL; GList *l; @@ -196,7 +192,6 @@ client_async_ready (GObject *source, { if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) g_warning ("Failed to create GoaClient: %s", error->message); - g_error_free (error); goto out; } @@ -227,14 +222,12 @@ client_async_ready (GObject *source, { GoaObject *goa_object = GOA_OBJECT (l->data); GrlSource *source; - gchar *account_id; + g_autofree gchar *account_id = NULL; account_id = get_grilo_id (goa_object); source = grl_registry_lookup_source (registry, account_id); if (source != NULL) query_online_source (self, source); - - g_free (account_id); } self->accounts = photo_accounts; @@ -283,7 +276,7 @@ cc_background_grilo_miner_init (CcBackgroundGriloMiner *self) static void cc_background_grilo_miner_class_init (CcBackgroundGriloMinerClass *klass) { - GError *error; + g_autoptr(GError) error = NULL; GObjectClass *object_class = G_OBJECT_CLASS (klass); GrlRegistry *registry; @@ -306,10 +299,7 @@ cc_background_grilo_miner_class_init (CcBackgroundGriloMinerClass *klass) error = NULL; if (!grl_registry_load_all_plugins (registry, FALSE, &error) || !grl_registry_activate_plugin_by_id (registry, "grl-flickr", &error)) - { g_warning ("%s", error->message); - g_error_free (error); - } } CcBackgroundGriloMiner * diff --git a/panels/background/cc-background-item.c b/panels/background/cc-background-item.c index 8d9698ab4..accf2ed19 100644 --- a/panels/background/cc-background-item.c +++ b/panels/background/cc-background-item.c @@ -76,8 +76,6 @@ enum { PROP_MODIFIED }; -static void cc_background_item_class_init (CcBackgroundItemClass *klass); -static void cc_background_item_init (CcBackgroundItem *background_item); static void cc_background_item_finalize (GObject *object); G_DEFINE_TYPE (CcBackgroundItem, cc_background_item, G_TYPE_OBJECT) @@ -87,9 +85,6 @@ static GdkPixbuf *slideshow_emblem = NULL; static GdkPixbuf * get_emblemed_pixbuf (CcBackgroundItem *item, GdkPixbuf *pixbuf, gint scale_factor) { - GdkPixbuf *retval; - GIcon *icon = NULL; - GtkIconInfo *icon_info = NULL; int eh; int ew; int h; @@ -97,12 +92,12 @@ get_emblemed_pixbuf (CcBackgroundItem *item, GdkPixbuf *pixbuf, gint scale_facto int x; int y; - retval = g_object_ref (pixbuf); - if (item->slideshow_emblem == NULL) { if (slideshow_emblem == NULL) { - GError *error = NULL; + g_autoptr(GIcon) icon = NULL; GtkIconTheme *theme; + g_autoptr(GtkIconInfo) icon_info = NULL; + g_autoptr(GError) error = NULL; icon = g_themed_icon_new ("slideshow-emblem"); theme = gtk_icon_theme_get_default (); @@ -115,14 +110,13 @@ get_emblemed_pixbuf (CcBackgroundItem *item, GdkPixbuf *pixbuf, gint scale_facto if (icon_info == NULL) { g_warning ("Your icon theme is missing the slideshow-emblem icon, " "please file a bug against it"); - goto out; + return g_object_ref (pixbuf); } slideshow_emblem = gtk_icon_info_load_icon (icon_info, &error); if (slideshow_emblem == NULL) { g_warning ("Failed to load slideshow emblem: %s", error->message); - g_error_free (error); - goto out; + return g_object_ref (pixbuf); } g_object_add_weak_pointer (G_OBJECT (slideshow_emblem), (gpointer *) (&slideshow_emblem)); @@ -141,10 +135,7 @@ get_emblemed_pixbuf (CcBackgroundItem *item, GdkPixbuf *pixbuf, gint scale_facto gdk_pixbuf_composite (slideshow_emblem, pixbuf, x, y, ew, eh, x, y, 1.0, 1.0, GDK_INTERP_BILINEAR, 255); - out: - g_clear_object (&icon_info); - g_clear_object (&icon); - return retval; + return g_object_ref (pixbuf); } static void @@ -154,15 +145,12 @@ set_bg_properties (CcBackgroundItem *item) GdkColor scolor = { 0, 0, 0, 0 }; if (item->uri) { - GFile *file; - char *filename; + g_autoptr(GFile) file = NULL; + g_autofree gchar *filename = NULL; file = g_file_new_for_commandline_arg (item->uri); filename = g_file_get_path (file); - g_object_unref (file); - gnome_bg_set_filename (item->bg, filename); - g_free (filename); } if (item->primary_color != NULL) { @@ -233,8 +221,8 @@ cc_background_item_get_frame_thumbnail (CcBackgroundItem *item, int frame, gboolean force_size) { - GdkPixbuf *pixbuf = NULL; - GdkPixbuf *retval = NULL; + g_autoptr(GdkPixbuf) pixbuf = NULL; + g_autoptr(GdkPixbuf) retval = NULL; g_return_val_if_fail (CC_IS_BACKGROUND_ITEM (item), NULL); g_return_val_if_fail (width > 0 && height > 0, NULL); @@ -271,9 +259,8 @@ cc_background_item_get_frame_thumbnail (CcBackgroundItem *item, && frame != -2 && gnome_bg_changes_with_time (item->bg)) { retval = get_emblemed_pixbuf (item, pixbuf, scale_factor); - g_object_unref (pixbuf); } else { - retval = pixbuf; + retval = g_steal_pointer (&pixbuf); } gnome_bg_get_image_size (item->bg, @@ -285,7 +272,7 @@ cc_background_item_get_frame_thumbnail (CcBackgroundItem *item, update_size (item); - return retval; + return g_steal_pointer (&retval); } @@ -303,10 +290,11 @@ static void update_info (CcBackgroundItem *item, GFileInfo *_info) { - GFile *file; - GFileInfo *info; + g_autoptr(GFileInfo) info = NULL; if (_info == NULL) { + g_autoptr(GFile) file = NULL; + file = g_file_new_for_uri (item->uri); info = g_file_query_info (file, @@ -318,7 +306,6 @@ update_info (CcBackgroundItem *item, G_FILE_QUERY_INFO_NONE, NULL, NULL); - g_object_unref (file); } else { info = g_object_ref (_info); } @@ -338,11 +325,8 @@ update_info (CcBackgroundItem *item, item->mime_type = g_strdup (g_file_info_get_content_type (info)); if (item->modified == 0) - item->modified = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED); + item->modified = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED); } - - if (info != NULL) - g_object_unref (info); } gboolean @@ -367,13 +351,12 @@ cc_background_item_load (CcBackgroundItem *item, /* FIXME we should handle XML files as well */ if (item->mime_type != NULL && g_str_has_prefix (item->mime_type, "image/")) { - char *filename; + g_autofree gchar *filename = NULL; filename = g_filename_from_uri (item->uri, NULL, NULL); gdk_pixbuf_get_file_info (filename, &item->width, &item->height); - g_free (filename); update_size (item); } @@ -689,8 +672,8 @@ cc_background_item_constructor (GType type, CcBackgroundItem *background_item; background_item = CC_BACKGROUND_ITEM (G_OBJECT_CLASS (cc_background_item_parent_class)->constructor (type, - n_construct_properties, - construct_properties)); + n_construct_properties, + construct_properties)); return G_OBJECT (background_item); } @@ -919,7 +902,7 @@ enum_to_str (GType type, void cc_background_item_dump (CcBackgroundItem *item) { - GString *flags; + g_autoptr(GString) flags = NULL; int i; g_return_if_fail (CC_IS_BACKGROUND_ITEM (item)); @@ -938,7 +921,6 @@ cc_background_item_dump (CcBackgroundItem *item) if (flags->len == 0) g_string_append (flags, "-none-"); g_debug ("flags:\t\t\t%s", flags->str); - g_string_free (flags, TRUE); if (item->primary_color) g_debug ("pcolor:\t\t\t%s", item->primary_color); if (item->secondary_color) @@ -961,7 +943,8 @@ static gboolean files_equal (const char *a, const char *b) { - GFile *file1, *file2; + g_autoptr(GFile) file1 = NULL; + g_autoptr(GFile) file2 = NULL; gboolean retval; if (a == NULL && @@ -978,8 +961,6 @@ files_equal (const char *a, retval = FALSE; else retval = TRUE; - g_object_unref (file1); - g_object_unref (file2); return retval; } diff --git a/panels/background/cc-background-panel.c b/panels/background/cc-background-panel.c index 0fa881dfb..8d54d5633 100644 --- a/panels/background/cc-background-panel.c +++ b/panels/background/cc-background-panel.c @@ -68,8 +68,6 @@ struct _CcBackgroundPanel CC_PANEL_REGISTER (CcBackgroundPanel, cc_background_panel) #define WID(y) (GtkWidget *) gtk_builder_get_object (panel->builder, y) -#define CURRENT_BG (settings == panel->settings ? panel->current_background : panel->current_lock_background) -#define SAVE_PATH (settings == panel->settings ? "last-edited.xml" : "last-edited-lock.xml") static const char * cc_background_panel_get_help_uri (CcPanel *panel) @@ -132,6 +130,15 @@ cc_background_panel_class_init (CcBackgroundPanelClass *klass) object_class->finalize = cc_background_panel_finalize; } +static CcBackgroundItem * +get_current_background (CcBackgroundPanel *panel, GSettings *settings) +{ + if (settings == panel->settings) + return panel->current_background; + else + return panel->current_lock_background; +} + static void update_preview (CcBackgroundPanel *panel, GSettings *settings, @@ -140,7 +147,7 @@ update_preview (CcBackgroundPanel *panel, gboolean changes_with_time; CcBackgroundItem *current_background; - current_background = CURRENT_BG; + current_background = get_current_background (panel, settings); if (item && current_background) { @@ -176,13 +183,13 @@ update_preview (CcBackgroundPanel *panel, } } -static char * -get_save_path (const char *filename) +static gchar * +get_save_path (CcBackgroundPanel *panel, GSettings *settings) { return g_build_filename (g_get_user_config_dir (), "gnome-control-center", "backgrounds", - filename, + settings == panel->settings ? "last-edited.xml" : "last-edited-lock.xml", NULL); } @@ -210,6 +217,7 @@ get_or_create_cached_pixbuf (CcBackgroundPanel *panel, -2, TRUE); g_object_set_data_full (G_OBJECT (background), "pixbuf", pixbuf, g_object_unref); } + return pixbuf; } @@ -254,13 +262,15 @@ static void reload_current_bg (CcBackgroundPanel *panel, GSettings *settings) { - CcBackgroundItem *saved, *configured; - gchar *uri, *pcolor, *scolor; + g_autoptr(CcBackgroundItem) saved = NULL; + CcBackgroundItem *configured; + g_autofree gchar *uri = NULL; + g_autofree gchar *pcolor = NULL; + g_autofree gchar *scolor = NULL; /* Load the saved configuration */ - uri = get_save_path (SAVE_PATH); + uri = get_save_path (panel, settings); saved = cc_background_xml_get_item (uri); - g_free (uri); /* initalise the current background information from settings */ uri = g_settings_get_string (settings, WP_URI_KEY); @@ -270,13 +280,10 @@ reload_current_bg (CcBackgroundPanel *panel, } else { - GFile *file; - + g_autoptr(GFile) file = NULL; file = g_file_new_for_commandline_arg (uri); - g_object_unref (file); } configured = cc_background_item_new (uri); - g_free (uri); pcolor = g_settings_get_string (settings, WP_PCOLOR_KEY); scolor = g_settings_get_string (settings, WP_SCOLOR_KEY); @@ -287,8 +294,6 @@ reload_current_bg (CcBackgroundPanel *panel, "primary-color", pcolor, "secondary-color", scolor, NULL); - g_free (pcolor); - g_free (scolor); if (saved != NULL && cc_background_item_compare (saved, configured)) { @@ -304,8 +309,6 @@ reload_current_bg (CcBackgroundPanel *panel, "source-xml", cc_background_item_get_source_xml (saved), NULL); } - if (saved != NULL) - g_object_unref (saved); if (settings == panel->settings) { @@ -323,7 +326,7 @@ reload_current_bg (CcBackgroundPanel *panel, static gboolean create_save_dir (void) { - char *path; + g_autofree char *path = NULL; path = g_build_filename (g_get_user_config_dir (), "gnome-control-center", @@ -332,10 +335,8 @@ create_save_dir (void) if (g_mkdir_with_parents (path, USER_DIR_MODE) < 0) { g_warning ("Failed to create directory '%s'", path); - g_free (path); return FALSE; } - g_free (path); return TRUE; } @@ -344,8 +345,8 @@ copy_finished_cb (GObject *source_object, GAsyncResult *result, gpointer pointer) { - GError *err = NULL; - CcBackgroundPanel *panel = (CcBackgroundPanel *) pointer; + g_autoptr(GError) err = NULL; + g_autoptr(CcBackgroundPanel) panel = (CcBackgroundPanel *) pointer; CcBackgroundItem *item; CcBackgroundItem *current_background; GSettings *settings; @@ -353,15 +354,13 @@ copy_finished_cb (GObject *source_object, if (!g_file_copy_finish (G_FILE (source_object), result, &err)) { if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { - g_error_free (err); return; } g_warning ("Failed to copy image to cache location: %s", err->message); - g_error_free (err); } item = g_object_get_data (source_object, "item"); settings = g_object_get_data (source_object, "settings"); - current_background = CURRENT_BG; + current_background = get_current_background (panel, settings); g_settings_apply (settings); @@ -379,19 +378,16 @@ copy_finished_cb (GObject *source_object, if (panel->builder) { - char *filename; + g_autofree gchar *filename = NULL; update_preview (panel, settings, item); - current_background = CURRENT_BG; + current_background = get_current_background (panel, settings); /* Save the source XML if there is one */ - filename = get_save_path (SAVE_PATH); + filename = get_save_path (panel, settings); if (create_save_dir ()) cc_background_xml_save (current_background, filename); } - - /* remove the reference taken when the copy was set up */ - g_object_unref (panel); } static void @@ -403,7 +399,6 @@ set_background (CcBackgroundPanel *panel, gboolean save_settings = TRUE; const char *uri; CcBackgroundItemFlags flags; - char *filename; if (item == NULL) return; @@ -419,34 +414,33 @@ set_background (CcBackgroundPanel *panel, else if (cc_background_item_get_source_url (item) != NULL && cc_background_item_get_needs_download (item)) { - GFile *source, *dest; - char *cache_path, *basename, *dest_path, *display_name, *dest_uri; - GdkPixbuf *pixbuf; + g_autoptr(GFile) source = NULL; + g_autoptr(GFile) dest = NULL; + g_autofree gchar *cache_path = NULL; + g_autofree gchar *basename = NULL; + g_autofree gchar *display_name = NULL; + g_autofree gchar *dest_path = NULL; + g_autofree gchar *dest_uri = NULL; + g_autoptr(GdkPixbuf) pixbuf = NULL; cache_path = bg_pictures_source_get_cache_path (); if (g_mkdir_with_parents (cache_path, USER_DIR_MODE) < 0) { g_warning ("Failed to create directory '%s'", cache_path); - g_free (cache_path); return; } - g_free (cache_path); dest_path = bg_pictures_source_get_unique_path (cc_background_item_get_source_url (item)); dest = g_file_new_for_path (dest_path); - g_free (dest_path); source = g_file_new_for_uri (cc_background_item_get_source_url (item)); basename = g_file_get_basename (source); display_name = g_filename_display_name (basename); dest_path = g_file_get_path (dest); - g_free (basename); /* create a blank image to use until the source image is ready */ pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, 1, 1); gdk_pixbuf_fill (pixbuf, 0x00000000); gdk_pixbuf_save (pixbuf, dest_path, "png", NULL, NULL); - g_object_unref (pixbuf); - g_free (dest_path); if (panel->copy_cancellable) { @@ -476,9 +470,7 @@ set_background (CcBackgroundPanel *panel, G_PRIORITY_DEFAULT, panel->copy_cancellable, NULL, NULL, copy_finished_cb, panel); - g_object_unref (source); dest_uri = g_file_get_uri (dest); - g_object_unref (dest); g_settings_set_string (settings, WP_URI_KEY, dest_uri); g_object_set (G_OBJECT (item), @@ -486,8 +478,6 @@ set_background (CcBackgroundPanel *panel, "needs-download", FALSE, "name", display_name, NULL); - g_free (display_name); - g_free (dest_uri); /* delay the updated drawing of the preview until the copy finishes */ save_settings = FALSE; @@ -518,31 +508,30 @@ set_background (CcBackgroundPanel *panel, /* update the preview information */ if (save_settings != FALSE) { + g_autofree gchar *filename = NULL; + /* Apply all changes */ g_settings_apply (settings); /* Save the source XML if there is one */ - filename = get_save_path (SAVE_PATH); + filename = get_save_path (panel, settings); if (create_save_dir ()) - cc_background_xml_save (CURRENT_BG, filename); + cc_background_xml_save (get_current_background (panel, settings), filename); } } static void on_chooser_dialog_response (GtkDialog *dialog, int response_id, - CcBackgroundPanel *self) + CcBackgroundPanel *panel) { if (response_id == GTK_RESPONSE_OK) { - CcBackgroundItem *item; + g_autoptr(CcBackgroundItem) item = NULL; item = cc_background_chooser_dialog_get_item (CC_BACKGROUND_CHOOSER_DIALOG (dialog)); if (item != NULL) - { - set_background (self, g_object_get_data (G_OBJECT (dialog), "settings"), item); - g_object_unref (item); - } + set_background (panel, g_object_get_data (G_OBJECT (dialog), "settings"), item); } gtk_widget_destroy (GTK_WIDGET (dialog)); @@ -564,32 +553,32 @@ launch_chooser (CcBackgroundPanel *panel, static void on_background_button_clicked (GtkButton *button, - CcBackgroundPanel *self) + CcBackgroundPanel *panel) { - launch_chooser (self, self->settings); + launch_chooser (panel, panel->settings); } static void on_lock_button_clicked (GtkButton *button, - CcBackgroundPanel *self) + CcBackgroundPanel *panel) { - launch_chooser (self, self->lock_settings); + launch_chooser (panel, panel->lock_settings); } static void on_settings_changed (GSettings *settings, gchar *key, - CcBackgroundPanel *self) + CcBackgroundPanel *panel) { - reload_current_bg (self, settings); - update_preview (self, settings, NULL); + reload_current_bg (panel, settings); + update_preview (panel, settings, NULL); } static void cc_background_panel_init (CcBackgroundPanel *panel) { gchar *objects[] = {"background-panel", NULL }; - GError *err = NULL; + g_autoptr(GError) err = NULL; GtkWidget *widget; panel->connection = g_application_get_dbus_connection (g_application_get_default ()); @@ -603,7 +592,6 @@ cc_background_panel_init (CcBackgroundPanel *panel) if (err) { g_warning ("Could not load ui: %s", err->message); - g_error_free (err); return; } diff --git a/panels/background/cc-background-xml.c b/panels/background/cc-background-xml.c index e525ddb28..4bcef3046 100644 --- a/panels/background/cc-background-xml.c +++ b/panels/background/cc-background-xml.c @@ -50,17 +50,13 @@ enum { static guint signals[LAST_SIGNAL] = { 0 }; -static void cc_background_xml_class_init (CcBackgroundXmlClass *klass); -static void cc_background_xml_init (CcBackgroundXml *background_item); -static void cc_background_xml_finalize (GObject *object); - G_DEFINE_TYPE (CcBackgroundXml, cc_background_xml, G_TYPE_OBJECT) static gboolean cc_background_xml_get_bool (const xmlNode *parent, const gchar *prop_name) { - xmlChar * prop; + xmlChar *prop; gboolean ret_val = FALSE; g_return_val_if_fail (parent != NULL, FALSE); @@ -73,7 +69,7 @@ cc_background_xml_get_bool (const xmlNode *parent, } else { ret_val = FALSE; } - g_free (prop); + xmlFree (prop); } return ret_val; @@ -115,15 +111,17 @@ enum_string_to_value (GType type, static gboolean idle_emit (CcBackgroundXml *xml) { - GObject *item; - guint i = NUM_ITEMS_PER_BATCH; + gint i; g_async_queue_lock (xml->item_added_queue); - while (i > 0 && (item = g_async_queue_try_pop_unlocked (xml->item_added_queue)) != NULL) { + for (i = 0; i < NUM_ITEMS_PER_BATCH; i++) { + g_autoptr(GObject) item = NULL; + + item = g_async_queue_try_pop_unlocked (xml->item_added_queue); + if (item == NULL) + break; g_signal_emit (G_OBJECT (xml), signals[ADDED], 0, item); - g_object_unref (item); - i--; } g_async_queue_unlock (xml->item_added_queue); @@ -175,12 +173,13 @@ cc_background_xml_load_xml_internal (CcBackgroundXml *xml, for (list = root->children; list != NULL; list = list->next) { if (!strcmp ((gchar *)list->name, "wallpaper")) { - CcBackgroundItem * item; + g_autoptr(CcBackgroundItem) item = NULL; CcBackgroundItemFlags flags; - char *uri, *cname, *id; + g_autofree gchar *uri = NULL; + g_autofree gchar *cname = NULL; + g_autofree gchar *id = NULL; flags = 0; - cname = NULL; item = cc_background_item_new (NULL); g_object_set (G_OBJECT (item), @@ -194,26 +193,24 @@ cc_background_xml_load_xml_internal (CcBackgroundXml *xml, } else if (!strcmp ((gchar *)wpa->name, "filename")) { if (wpa->last != NULL && wpa->last->content != NULL) { gchar *content = g_strstrip ((gchar *)wpa->last->content); - char *bg_uri; + g_autofree gchar *bg_uri = NULL; /* FIXME same rubbish as in other parts of the code */ if (strcmp (content, NONE) == 0) { bg_uri = NULL; } else { - GFile *file; + g_autoptr(GFile) file = NULL; file = g_file_new_for_commandline_arg (content); bg_uri = g_file_get_uri (file); - g_object_unref (file); } SET_FLAG(CC_BACKGROUND_ITEM_HAS_URI); g_object_set (G_OBJECT (item), "uri", bg_uri, NULL); - g_free (bg_uri); } else { break; } } else if (!strcmp ((gchar *)wpa->name, "name")) { if (wpa->last != NULL && wpa->last->content != NULL) { - char *name; + g_autofree gchar *name = NULL; nodelang = xmlNodeGetLang (wpa->last); g_object_get (G_OBJECT (item), "name", &name, NULL); @@ -232,7 +229,6 @@ cc_background_xml_load_xml_internal (CcBackgroundXml *xml, } } - g_free (name); xmlFree (nodelang); } else { break; @@ -279,21 +275,20 @@ cc_background_xml_load_xml_internal (CcBackgroundXml *xml, /* Check whether the target file exists */ { - GFile *file; const char *uri; uri = cc_background_item_get_uri (item); if (uri != NULL) { + g_autoptr(GFile) file = NULL; + file = g_file_new_for_uri (uri); if (g_file_query_exists (file, NULL) == FALSE) { - g_free (cname); - g_object_unref (file); - g_object_unref (item); + g_clear_pointer (&cname, g_free); + g_clear_object (&item); continue; } - g_object_unref (file); } } @@ -301,13 +296,9 @@ cc_background_xml_load_xml_internal (CcBackgroundXml *xml, * need to use proper code here */ uri = g_filename_to_uri (filename, NULL, NULL); id = g_strdup_printf ("%s#%s", uri, cname); - g_free (cname); - g_free (uri); /* Make sure we don't already have this one and that filename exists */ if (g_hash_table_lookup (xml->wp_hash, id) != NULL) { - g_object_unref (item); - g_free (id); continue; } @@ -319,9 +310,6 @@ cc_background_xml_load_xml_internal (CcBackgroundXml *xml, emit_added_in_idle (xml, g_object_ref (item)); else g_signal_emit (G_OBJECT (xml), signals[ADDED], 0, item); - - g_object_unref (item); - g_free (id); retval = TRUE; } } @@ -337,14 +325,13 @@ gnome_wp_file_changed (GFileMonitor *monitor, GFileMonitorEvent event_type, CcBackgroundXml *data) { - gchar *filename; + g_autofree gchar *filename = NULL; switch (event_type) { case G_FILE_MONITOR_EVENT_CHANGED: case G_FILE_MONITOR_EVENT_CREATED: filename = g_file_get_path (file); cc_background_xml_load_xml_internal (data, filename, FALSE); - g_free (filename); break; default: break; @@ -356,20 +343,18 @@ cc_background_xml_add_monitor (GFile *directory, CcBackgroundXml *data) { GFileMonitor *monitor; - GError *error = NULL; + g_autoptr(GError) error = NULL; monitor = g_file_monitor_directory (directory, G_FILE_MONITOR_NONE, NULL, &error); if (error != NULL) { - gchar *path; + g_autofree gchar *path = NULL; path = g_file_get_parse_name (directory); g_warning ("Unable to monitor directory %s: %s", path, error->message); - g_error_free (error); - g_free (path); return; } @@ -385,10 +370,9 @@ cc_background_xml_load_from_dir (const gchar *path, CcBackgroundXml *data, gboolean in_thread) { - GFile *directory; - GFileEnumerator *enumerator; - GError *error = NULL; - GFileInfo *info; + g_autoptr(GFile) directory = NULL; + g_autoptr(GFileEnumerator) enumerator = NULL; + g_autoptr(GError) error = NULL; if (!g_file_test (path, G_FILE_TEST_IS_DIR)) { return; @@ -402,28 +386,26 @@ cc_background_xml_load_from_dir (const gchar *path, &error); if (error != NULL) { g_warning ("Unable to check directory %s: %s", path, error->message); - g_error_free (error); - g_object_unref (directory); return; } - while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL))) { + while (TRUE) { + g_autoptr(GFileInfo) info = NULL; const gchar *filename; - gchar *fullpath; + g_autofree gchar *fullpath = NULL; + + info = g_file_enumerator_next_file (enumerator, NULL, NULL); + if (info == NULL) { + g_file_enumerator_close (enumerator, NULL, NULL); + cc_background_xml_add_monitor (directory, data); + return; + } filename = g_file_info_get_name (info); fullpath = g_build_filename (path, filename, NULL); - g_object_unref (info); cc_background_xml_load_xml_internal (data, fullpath, in_thread); - g_free (fullpath); } - g_file_enumerator_close (enumerator, NULL, NULL); - - cc_background_xml_add_monitor (directory, data); - - g_object_unref (directory); - g_object_unref (enumerator); } static void @@ -431,22 +413,21 @@ cc_background_xml_load_list (CcBackgroundXml *data, gboolean in_thread) { const char * const *system_data_dirs; - gchar * datadir; + g_autofree gchar *datadir = NULL; gint i; datadir = g_build_filename (g_get_user_data_dir (), "gnome-background-properties", NULL); cc_background_xml_load_from_dir (datadir, data, in_thread); - g_free (datadir); system_data_dirs = g_get_system_data_dirs (); for (i = 0; system_data_dirs[i]; i++) { - datadir = g_build_filename (system_data_dirs[i], + g_autofree gchar *sdatadir = NULL; + sdatadir = g_build_filename (system_data_dirs[i], "gnome-background-properties", NULL); - cc_background_xml_load_from_dir (datadir, data, in_thread); - g_free (datadir); + cc_background_xml_load_from_dir (sdatadir, data, in_thread); } } @@ -479,14 +460,13 @@ void cc_background_xml_load_list_async (CcBackgroundXml *xml, GAsyncReadyCallback callback, gpointer user_data) { - GSimpleAsyncResult *result; + g_autoptr(GSimpleAsyncResult) result = NULL; g_return_if_fail (CC_IS_BACKGROUND_XML (xml)); result = g_simple_async_result_new (G_OBJECT (xml), callback, user_data, cc_background_xml_load_list_async); g_simple_async_result_set_op_res_gpointer (result, xml, NULL); g_simple_async_result_run_in_thread (result, (GSimpleAsyncThreadFunc) load_list_thread, G_PRIORITY_LOW, cancellable); - g_object_unref (result); } gboolean @@ -513,20 +493,17 @@ single_xml_added (CcBackgroundXml *xml, CcBackgroundItem * cc_background_xml_get_item (const char *filename) { - CcBackgroundXml *xml; - CcBackgroundItem *item; + g_autoptr(CcBackgroundXml) xml = NULL; + CcBackgroundItem *item = NULL; if (g_file_test (filename, G_FILE_TEST_IS_REGULAR) == FALSE) return NULL; xml = cc_background_xml_new (); - item = NULL; g_signal_connect (G_OBJECT (xml), "added", G_CALLBACK (single_xml_added), &item); - if (cc_background_xml_load_xml (xml, filename) == FALSE) { - g_object_unref (xml); + if (cc_background_xml_load_xml (xml, filename) == FALSE) return NULL; - } return item; } @@ -555,7 +532,11 @@ cc_background_xml_save (CcBackgroundItem *item, xmlNode *xml_item G_GNUC_UNUSED; const char * none = "(none)"; const char *placement_str, *shading_str; - char *name, *pcolor, *scolor, *uri, *source_url; + g_autofree gchar *name = NULL; + g_autofree gchar *pcolor = NULL; + g_autofree gchar *scolor = NULL; + g_autofree gchar *uri = NULL; + g_autofree gchar *source_url = NULL; CcBackgroundItemFlags flags; GDesktopBackgroundStyle placement; GDesktopBackgroundShading shading; @@ -586,14 +567,12 @@ cc_background_xml_save (CcBackgroundItem *item, if (flags & CC_BACKGROUND_ITEM_HAS_URI && uri != NULL) { - GFile *file; - char *fname; + g_autoptr(GFile) file = NULL; + g_autofree gchar *fname = NULL; file = g_file_new_for_commandline_arg (uri); fname = g_file_get_path (file); - g_object_unref (file); xml_item = xmlNewTextChild (wallpaper, NULL, (xmlChar *)"filename", (xmlChar *)fname); - g_free (fname); } else if (flags & CC_BACKGROUND_ITEM_HAS_URI) { @@ -611,12 +590,6 @@ cc_background_xml_save (CcBackgroundItem *item, if (source_url != NULL) xml_item = xmlNewTextChild (wallpaper, NULL, (xmlChar *)"source_url", (xmlChar *)source_url); - g_free (name); - g_free (pcolor); - g_free (scolor); - g_free (uri); - g_free (source_url); - xmlSaveFormatFile (filename, wp, 1); xmlFreeDoc (wp); } @@ -661,9 +634,9 @@ static void cc_background_xml_init (CcBackgroundXml *xml) { xml->wp_hash = g_hash_table_new_full (g_str_hash, - g_str_equal, - (GDestroyNotify) g_free, - (GDestroyNotify) g_object_unref); + g_str_equal, + (GDestroyNotify) g_free, + (GDestroyNotify) g_object_unref); xml->item_added_queue = g_async_queue_new_full ((GDestroyNotify) g_object_unref); } diff --git a/panels/background/test-chooser-dialog.c b/panels/background/test-chooser-dialog.c index c936b184c..3c1e44ad4 100644 --- a/panels/background/test-chooser-dialog.c +++ b/panels/background/test-chooser-dialog.c @@ -8,11 +8,10 @@ on_dialog_response (GtkDialog *dialog, { g_debug ("response: %d", response_id); if (response_id == GTK_RESPONSE_OK) { - CcBackgroundItem *item; + g_autoptr(CcBackgroundItem) item = NULL; item = cc_background_chooser_dialog_get_item (CC_BACKGROUND_CHOOSER_DIALOG (dialog)); cc_background_item_dump (item); - g_object_unref (item); } gtk_widget_destroy (GTK_WIDGET (dialog)); gtk_main_quit (); |