[gnome-font-viewer] Use g_autoptr/g_autofree for memory management
- From: Cosimo Cecchi <cosimoc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-font-viewer] Use g_autoptr/g_autofree for memory management
- Date: Sat, 6 Jul 2019 15:49:38 +0000 (UTC)
commit 97ca035e09c084537a1ff7a117d0207019c3b27f
Author: Cosimo Cecchi <cosimoc gnome org>
Date: Sat Jul 6 05:49:58 2019 -0700
Use g_autoptr/g_autofree for memory management
Makes us more robust to leaks and simplifies the code.
src/font-model.c | 130 +++++++++-----------------
src/font-thumbnailer.c | 115 +++++++++--------------
src/font-utils.c | 31 +++----
src/font-view.c | 241 ++++++++++++++++++-------------------------------
4 files changed, 188 insertions(+), 329 deletions(-)
---
diff --git a/src/font-model.c b/src/font-model.c
index 0ed0739..0e8ac1f 100644
--- a/src/font-model.c
+++ b/src/font-model.c
@@ -96,7 +96,7 @@ iter_for_face_foreach (GtkTreeModel *model,
gpointer user_data)
{
IterForFaceData *data = user_data;
- gchar *font_name;
+ g_autofree gchar *font_name = NULL;
gboolean retval;
gtk_tree_model_get (GTK_TREE_MODEL (model), iter,
@@ -104,7 +104,6 @@ iter_for_face_foreach (GtkTreeModel *model,
-1);
retval = (g_strcmp0 (font_name, data->match_name) == 0);
- g_free (font_name);
if (retval) {
data->iter = *iter;
@@ -179,15 +178,13 @@ one_thumbnail_done (gpointer user_data)
static GdkPixbuf *
create_thumbnail (ThumbInfoData *thumb_info)
{
- GFile *file = thumb_info->font_file;
- GnomeDesktopThumbnailFactory *factory;
+ g_autoptr(GdkPixbuf) pixbuf = NULL;
+ g_autoptr(GError) error = NULL;
+ g_autoptr(GFileInfo) info = NULL;
+ g_autoptr(GnomeDesktopThumbnailFactory) factory = NULL;
guint64 mtime;
- GError *error = NULL;
-
- GdkPixbuf *pixbuf = NULL;
- GFileInfo *info = NULL;
- info = g_file_query_info (file, ATTRIBUTES_FOR_CREATING_THUMBNAIL,
+ info = g_file_query_info (thumb_info->font_file, ATTRIBUTES_FOR_CREATING_THUMBNAIL,
G_FILE_QUERY_INFO_NONE,
NULL, &error);
@@ -195,42 +192,31 @@ create_thumbnail (ThumbInfoData *thumb_info)
* thumbnail.
*/
if (info == NULL) {
- char *font_path = g_file_get_path (file);
- g_debug ("Can't query info for file %s: %s\n", font_path, error->message);
- g_free (font_path);
- goto out;
+ g_debug ("Can't query info for file %s: %s", thumb_info->uri, error->message);
+ return NULL;
}
- mtime = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
-
factory = gnome_desktop_thumbnail_factory_new (GNOME_DESKTOP_THUMBNAIL_SIZE_LARGE);
pixbuf = gnome_desktop_thumbnail_factory_generate_thumbnail
(factory,
thumb_info->uri, g_file_info_get_content_type (info));
- if (pixbuf != NULL)
+ mtime = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
+ if (pixbuf != NULL) {
+ GdkPixbuf *scaled = gdk_pixbuf_scale_simple (pixbuf,
+ 128 * thumb_info->self->priv->scale_factor,
+ 128 * thumb_info->self->priv->scale_factor,
+ GDK_INTERP_BILINEAR);
gnome_desktop_thumbnail_factory_save_thumbnail (factory, pixbuf,
thumb_info->uri, (time_t) mtime);
- else
+ g_object_unref (pixbuf);
+ pixbuf = scaled;
+ } else {
gnome_desktop_thumbnail_factory_create_failed_thumbnail (factory,
thumb_info->uri, (time_t) mtime);
+ }
- g_object_unref (factory);
-
- out:
- g_clear_error (&error);
- g_clear_object (&info);
-
- if (pixbuf != NULL) {
- GdkPixbuf *scaled = gdk_pixbuf_scale_simple (pixbuf,
- 128 * thumb_info->self->priv->scale_factor,
- 128 * thumb_info->self->priv->scale_factor,
- GDK_INTERP_BILINEAR);
- g_object_unref (pixbuf);
- pixbuf = scaled;
- }
-
- return pixbuf;
+ return g_steal_pointer (&pixbuf);
}
static void
@@ -239,21 +225,20 @@ ensure_thumbnails_job (GTask *task,
gpointer user_data,
GCancellable *cancellable)
{
+ FontViewModel *self = FONT_VIEW_MODEL (source_object);
GList *thumb_infos = user_data, *l;
+ gint scale_factor = self->priv->scale_factor;
for (l = thumb_infos; l != NULL; l = l->next) {
- gboolean thumb_failed;
- gchar *thumb_path = NULL;
+ g_autoptr(GdkPixbuf) pixbuf = NULL;
+ g_autoptr(GError) error = NULL;
+ g_autofree gchar *thumb_path = NULL;
ThumbInfoData *thumb_info = l->data;
- GError *error = NULL;
- GFile *thumb_file = NULL;
- GFileInputStream *is = NULL;
- GFileInfo *info = NULL;
- GdkPixbuf *pixbuf = NULL;
- gint scale_factor = thumb_info->self->priv->scale_factor;
-
if (thumb_info->face_index == 0) {
+ g_autoptr(GFileInfo) info = NULL;
+ gboolean thumb_failed;
+
thumb_info->uri = g_file_get_uri (thumb_info->font_file);
info = g_file_query_info (thumb_info->font_file,
ATTRIBUTES_FOR_EXISTING_THUMBNAIL,
@@ -261,12 +246,7 @@ ensure_thumbnails_job (GTask *task,
NULL, &error);
if (error != NULL) {
- gchar *font_path;
-
- font_path = g_file_get_path (thumb_info->font_file);
- g_debug ("Can't query info for file %s: %s\n", font_path, error->message);
- g_free (font_path);
-
+ g_debug ("Can't query info for file %s: %s", thumb_info->uri, error->message);
goto next;
}
@@ -276,37 +256,35 @@ ensure_thumbnails_job (GTask *task,
thumb_path = g_strdup (g_file_info_get_attribute_byte_string (info,
G_FILE_ATTRIBUTE_THUMBNAIL_PATH));
} else {
- gchar *file_uri;
- gchar *checksum;
- gchar *filename;
+ g_autofree gchar *checksum = NULL, *filename = NULL, *file_uri = NULL;
file_uri = g_file_get_uri (thumb_info->font_file);
thumb_info->uri = g_strdup_printf ("%s#0x%08X", file_uri, thumb_info->face_index);
- g_free (file_uri);
checksum = g_compute_checksum_for_data (G_CHECKSUM_MD5,
(const guchar *) thumb_info->uri,
strlen (thumb_info->uri));
filename = g_strdup_printf ("%s.png", checksum);
- g_free (checksum);
thumb_path = g_build_filename (g_get_user_cache_dir (),
"thumbnails",
"large",
filename,
NULL);
- g_free (filename);
if (!g_file_test (thumb_path, G_FILE_TEST_IS_REGULAR))
g_clear_pointer (&thumb_path, g_free);
}
if (thumb_path != NULL) {
+ g_autoptr(GFile) thumb_file = NULL;
+ g_autoptr(GFileInputStream) is = NULL;
+
thumb_file = g_file_new_for_path (thumb_path);
is = g_file_read (thumb_file, NULL, &error);
if (error != NULL) {
- g_debug ("Can't read file %s: %s\n", thumb_path, error->message);
+ g_debug ("Can't read file %s: %s", thumb_path, error->message);
goto next;
}
@@ -316,27 +294,18 @@ ensure_thumbnails_job (GTask *task,
NULL, &error);
if (error != NULL) {
- g_debug ("Can't read thumbnail pixbuf %s: %s\n", thumb_path, error->message);
+ g_debug ("Can't read thumbnail pixbuf %s: %s", thumb_path, error->message);
goto next;
}
} else {
pixbuf = create_thumbnail (thumb_info);
}
- if (pixbuf != NULL) {
+ if (pixbuf != NULL)
thumb_info->surface = gdk_cairo_surface_create_from_pixbuf (pixbuf, scale_factor, NULL);
- g_object_unref (pixbuf);
- }
next:
- g_clear_error (&error);
- g_clear_object (&is);
- g_clear_object (&thumb_file);
- g_clear_object (&info);
- g_clear_pointer (&thumb_path, g_free);
-
- g_main_context_invoke (NULL, one_thumbnail_done,
- thumb_info);
+ g_main_context_invoke (NULL, one_thumbnail_done, thumb_info);
}
g_list_free (thumb_infos);
@@ -361,10 +330,10 @@ font_info_data_free (gpointer user_data)
static void
ensure_fallback_icon (FontViewModel *self)
{
+ g_autoptr(GIcon) icon = NULL;
+ g_autoptr(GtkIconInfo) icon_info = NULL;
GtkIconTheme *icon_theme;
- GtkIconInfo *icon_info;
const char *mimetype = "font/ttf";
- GIcon *icon = NULL;
if (self->priv->fallback_icon != NULL)
return;
@@ -374,15 +343,12 @@ ensure_fallback_icon (FontViewModel *self)
icon_info = gtk_icon_theme_lookup_by_gicon_for_scale (icon_theme, icon,
128, self->priv->scale_factor,
GTK_ICON_LOOKUP_FORCE_SIZE);
- g_object_unref (icon);
-
if (!icon_info) {
g_warning ("Fallback icon for %s not found", mimetype);
return;
}
self->priv->fallback_icon = gtk_icon_info_load_surface (icon_info, NULL, NULL);
- g_object_unref (icon_info);
}
static void
@@ -391,7 +357,7 @@ font_infos_loaded (GObject *source_object,
gpointer user_data)
{
FontViewModel *self = FONT_VIEW_MODEL (source_object);
- GTask *task = NULL;
+ g_autoptr(GTask) task = NULL;
GList *l, *thumb_infos = NULL;
GList *font_infos = g_task_propagate_pointer (G_TASK (result), NULL);
@@ -399,7 +365,7 @@ font_infos_loaded (GObject *source_object,
for (l = font_infos; l != NULL; l = l->next) {
FontInfoData *font_info = l->data;
- gchar *collation_key;
+ g_autofree gchar *collation_key = NULL;
GtkTreeIter iter;
ThumbInfoData *thumb_info;
@@ -411,7 +377,6 @@ font_infos_loaded (GObject *source_object,
COLUMN_ICON, self->priv->fallback_icon,
COLUMN_COLLATION_KEY, collation_key,
-1);
- g_free (collation_key);
thumb_info = g_slice_new0 (ThumbInfoData);
thumb_info->font_file = g_file_new_for_path (font_info->font_path);
@@ -426,10 +391,9 @@ font_infos_loaded (GObject *source_object,
g_signal_emit (self, signals[CONFIG_CHANGED], 0);
g_list_free (font_infos);
- task = g_task_new (NULL, NULL, NULL, NULL);
+ task = g_task_new (self, NULL, NULL, NULL);
g_task_set_task_data (task, thumb_infos, NULL);
g_task_run_in_thread (task, ensure_thumbnails_job);
- g_object_unref (task);
}
static void
@@ -482,7 +446,7 @@ ensure_font_list (FontViewModel *self)
{
FcPattern *pat;
FcObjectSet *os;
- GTask *task;
+ g_autoptr(GTask) task = NULL;
/* always reinitialize the font database */
if (!FcInitReinitialize())
@@ -545,8 +509,7 @@ font_view_model_sort_func (GtkTreeModel *model,
GtkTreeIter *b,
gpointer user_data)
{
- gchar *key_a = NULL, *key_b = NULL;
- int retval;
+ g_autofree gchar *key_a = NULL, *key_b = NULL;
gtk_tree_model_get (model, a,
COLUMN_COLLATION_KEY, &key_a,
@@ -555,12 +518,7 @@ font_view_model_sort_func (GtkTreeModel *model,
COLUMN_COLLATION_KEY, &key_b,
-1);
- retval = g_strcmp0 (key_a, key_b);
-
- g_free (key_a);
- g_free (key_b);
-
- return retval;
+ return g_strcmp0 (key_a, key_b);
}
static void
diff --git a/src/font-thumbnailer.c b/src/font-thumbnailer.c
index dfd973d..821efb3 100644
--- a/src/font-thumbnailer.c
+++ b/src/font-thumbnailer.c
@@ -60,42 +60,39 @@ static gboolean
check_font_contain_text (FT_Face face,
const gchar *text)
{
- gunichar *string;
- glong len, idx, map;
- FT_CharMap charmap;
- gboolean retval = FALSE;
+ g_autofree gunichar *string = NULL;
+ glong len, map;
- string = g_utf8_to_ucs4_fast (text, -1, &len);
+ string = g_utf8_to_ucs4_fast (text, -1, &len);
- for (map = 0; map < face->num_charmaps; map++) {
- charmap = face->charmaps[map];
- FT_Set_Charmap (face, charmap);
+ for (map = 0; map < face->num_charmaps; map++) {
+ gboolean res = TRUE;
+ FT_CharMap charmap = face->charmaps[map];
+ gint idx;
- retval = TRUE;
+ FT_Set_Charmap (face, charmap);
- for (idx = 0; idx < len; idx++) {
- gunichar c = string[idx];
+ for (idx = 0; idx < len; idx++) {
+ gunichar c = string[idx];
- if (!FT_Get_Char_Index (face, c)) {
- retval = FALSE;
- break;
- }
- }
-
- if (retval)
- break;
- }
+ if (!FT_Get_Char_Index (face, c)) {
+ res = FALSE;
+ break;
+ }
+ }
- g_free (string);
+ if (res)
+ return TRUE;
+ }
- return retval;
+ return FALSE;
}
static gchar *
check_for_ascii_glyph_numbers (FT_Face face,
gboolean *found_ascii)
{
- GString *ascii_string, *string;
+ g_autoptr(GString) ascii_string = NULL, string = NULL;
gulong c;
guint glyph, found = 0;
@@ -120,27 +117,24 @@ check_for_ascii_glyph_numbers (FT_Face face,
if (found == 2) {
*found_ascii = TRUE;
- g_string_free (string, TRUE);
- return g_string_free (ascii_string, FALSE);
+ return g_strdup (ascii_string->str);
} else {
- g_string_free (ascii_string, TRUE);
- return g_string_free (string, FALSE);
+ return g_strdup (string->str);
}
}
static gchar *
build_fallback_thumbstr (FT_Face face)
{
- gchar *chars;
+ g_autoptr(GString) retval = NULL;
+ g_autofree gchar *chars = NULL;
gint idx, total_chars;
- GString *retval;
gchar *ptr, *end;
gboolean found_ascii;
chars = check_for_ascii_glyph_numbers (face, &found_ascii);
-
if (found_ascii)
- return chars;
+ return g_steal_pointer (&chars);
idx = 0;
retval = g_string_new (NULL);
@@ -155,31 +149,29 @@ build_fallback_thumbstr (FT_Face face)
idx++;
}
- return g_string_free (retval, FALSE);
+ return g_strdup (retval->str);
}
int
main (int argc,
char **argv)
{
+ g_autoptr(GError) gerror = NULL;
+ g_autoptr(GFile) file = NULL;
+ g_autoptr(GOptionContext) context = NULL;
+ g_autofree gchar *contents = NULL, *help = NULL, *str = NULL, *uri = NULL;
+ g_autofree gchar **arguments = NULL;
FT_Error error;
FT_Library library;
FT_Face face;
- GFile *file;
gint font_size, thumb_size = THUMB_SIZE;
- gchar *thumbstr_utf8 = NULL, *help, *uri;
- gchar **arguments = NULL;
- GOptionContext *context;
- GError *gerror = NULL;
- gchar *contents = NULL;
+ gchar *thumbstr_utf8 = NULL;
gboolean retval, default_thumbstr = TRUE;
- gint rv = 1;
GdkRGBA black = { 0.0, 0.0, 0.0, 1.0 };
cairo_surface_t *surface;
cairo_t *cr;
cairo_text_extents_t text_extents;
cairo_font_face_t *font;
- gchar *str = NULL;
gdouble scale, scale_x, scale_y;
gint face_index = 0;
gchar *fragment;
@@ -206,29 +198,22 @@ main (int argc,
retval = g_option_context_parse (context, &argc, &argv, &gerror);
if (!retval) {
g_printerr ("Error parsing arguments: %s\n", gerror->message);
-
- g_option_context_free (context);
- g_error_free (gerror);
return 1;
}
if (!arguments || g_strv_length (arguments) != 2) {
help = g_option_context_get_help (context, TRUE, NULL);
g_printerr ("%s", help);
-
- g_option_context_free (context);
- goto out;
+ return 1;
}
- g_option_context_free (context);
-
if (thumbstr_utf8 != NULL)
default_thumbstr = FALSE;
error = FT_Init_FreeType (&library);
if (error) {
- g_printerr("Could not initialise freetype: %s\n", get_ft_error (error));
- goto out;
+ g_printerr ("Could not initialise freetype: %s\n", get_ft_error (error));
+ return 1;
}
totem_resources_monitor_start (arguments[0], 30 * G_USEC_PER_SEC);
@@ -239,19 +224,13 @@ main (int argc,
file = g_file_new_for_commandline_arg (arguments[0]);
uri = g_file_get_uri (file);
- g_object_unref (file);
face = sushi_new_ft_face_from_uri (library, uri, face_index, &contents, &gerror);
if (gerror) {
- g_printerr ("Could not load face '%s': %s\n", uri,
- gerror->message);
- g_free (uri);
- g_error_free (gerror);
- goto out;
+ g_printerr ("Could not load face '%s': %s\n", uri, gerror->message);
+ return 1;
}
- g_free (uri);
-
if (default_thumbstr) {
if (check_font_contain_text (face, "Aa"))
str = g_strdup ("Aa");
@@ -302,24 +281,16 @@ main (int argc,
error = FT_Done_Face (face);
if (error) {
- g_printerr("Could not unload face: %s\n", get_ft_error (error));
- goto out;
+ g_printerr ("Could not unload face: %s\n", get_ft_error (error));
+ return 1;
}
error = FT_Done_FreeType (library);
if (error) {
- g_printerr ("Could not finalize freetype library: %s\n",
- get_ft_error (error));
- goto out;
+ g_printerr ("Could not finalize freetype library: %s\n", get_ft_error (error));
+ return 1;
}
- rv = 0; /* success */
-
- out:
-
- g_strfreev (arguments);
- g_free (str);
- g_free (contents);
-
- return rv;
+ /* success */
+ return 0;
}
diff --git a/src/font-utils.c b/src/font-utils.c
index 0e7b5c1..3cdd3c2 100644
--- a/src/font-utils.c
+++ b/src/font-utils.c
@@ -25,14 +25,10 @@
gchar *
font_utils_get_font_name (FT_Face face)
{
- gchar *name;
+ if (g_strcmp0 (face->style_name, "Regular") == 0)
+ return g_strdup (face->family_name);
- if (g_strcmp0 (face->style_name, "Regular") == 0)
- name = g_strdup (face->family_name);
- else
- name = g_strconcat (face->family_name, ", ", face->style_name, NULL);
-
- return name;
+ return g_strconcat (face->family_name, ", ", face->style_name, NULL);
}
gchar *
@@ -40,9 +36,10 @@ font_utils_get_font_name_for_file (FT_Library library,
const gchar *path,
gint face_index)
{
- GFile *file;
- gchar *uri, *contents = NULL, *name = NULL;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
+ g_autoptr(GFile) file = NULL;
+ g_autofree gchar *uri = NULL, *contents = NULL;
+ gchar *name = NULL;
FT_Face face;
file = g_file_new_for_path (path);
@@ -50,17 +47,13 @@ font_utils_get_font_name_for_file (FT_Library library,
face = sushi_new_ft_face_from_uri (library, uri, face_index, &contents,
&error);
- if (face != NULL) {
- name = font_utils_get_font_name (face);
- FT_Done_Face (face);
- } else if (error != NULL) {
- g_warning ("Can't get font name: %s\n", error->message);
- g_error_free (error);
+ if (error != NULL) {
+ g_warning ("Can't get font name: %s", error->message);
+ return NULL;
}
- g_free (uri);
- g_object_unref (file);
- g_free (contents);
+ name = font_utils_get_font_name (face);
+ FT_Done_Face (face);
return name;
}
diff --git a/src/font-view.c b/src/font-view.c
index b8c2063..259635d 100644
--- a/src/font-view.c
+++ b/src/font-view.c
@@ -116,8 +116,8 @@ static void ensure_window (FontViewApplication *self);
static void
strip_whitespace (gchar **original)
{
- GString *reassembled;
- gchar **split;
+ g_autofree gchar **split = NULL;
+ g_autoptr(GString) reassembled = NULL;
const gchar *str;
gint idx, n_stripped;
size_t len;
@@ -142,10 +142,8 @@ strip_whitespace (gchar **original)
g_string_append (reassembled, str);
}
- g_strfreev (split);
g_free (*original);
-
- *original = g_string_free (reassembled, FALSE);
+ *original = g_strdup (reassembled->str);
}
#define MATCH_VERSION_STR "Version"
@@ -277,7 +275,7 @@ describe_instance (FT_Face face,
int pos,
GString *s)
{
- char *str = NULL;
+ g_autofree char *str = NULL;
if (is_valid_subfamily_id (ns->strid))
str = get_sfnt_name (face, ns->strid);
@@ -288,8 +286,6 @@ describe_instance (FT_Face face,
if (s->len > 0)
g_string_append (s, ", ");
g_string_append (s, str);
-
- g_free (str);
}
#include "open-type-layout.h"
@@ -297,9 +293,9 @@ describe_instance (FT_Face face,
static char *
get_features (FT_Face face)
{
+ g_autoptr(GString) s = NULL;
hb_font_t *hb_font;
int i, j, k;
- GString *s;
s = g_string_new ("");
@@ -338,9 +334,7 @@ get_features (FT_Face face)
}
if (s->len > 0)
- return g_string_free (s, FALSE);
-
- g_string_free (s, TRUE);
+ return g_strdup (s->str);
return NULL;
}
@@ -350,8 +344,7 @@ populate_grid (FontViewApplication *self,
GtkWidget *grid,
FT_Face face)
{
- gchar *s;
- GFileInfo *info;
+ g_autoptr (GFileInfo) info = NULL;
PS_FontInfoRec ps_info;
add_row (grid, _("Name"), face->family_name, FALSE);
@@ -366,17 +359,14 @@ populate_grid (FontViewApplication *self,
NULL, NULL);
if (info != NULL) {
- s = g_content_type_get_description (g_file_info_get_content_type (info));
+ g_autofree gchar *s = g_content_type_get_description (g_file_info_get_content_type (info));
add_row (grid, _("Type"), s, FALSE);
- g_free (s);
-
- g_object_unref (info);
}
if (FT_IS_SFNT (face)) {
gint i, len;
- gchar *version = NULL, *copyright = NULL, *description = NULL;
- gchar *designer = NULL, *manufacturer = NULL, *license = NULL;
+ g_autofree gchar *version = NULL, *copyright = NULL, *description = NULL;
+ g_autofree gchar *designer = NULL, *manufacturer = NULL, *license = NULL;
len = FT_Get_Sfnt_Name_Count (face);
for (i = 0; i < len; i++) {
@@ -393,34 +383,34 @@ populate_grid (FontViewApplication *self,
switch (sname.name_id) {
case TT_NAME_ID_COPYRIGHT:
- g_free (copyright);
- copyright = g_convert ((gchar *)sname.string, sname.string_len,
- "UTF-8", "UTF-16BE", NULL, NULL, NULL);
+ if (!copyright)
+ copyright = g_convert ((gchar *)sname.string, sname.string_len,
+ "UTF-8", "UTF-16BE", NULL, NULL, NULL);
break;
case TT_NAME_ID_VERSION_STRING:
- g_free (version);
- version = g_convert ((gchar *)sname.string, sname.string_len,
- "UTF-8", "UTF-16BE", NULL, NULL, NULL);
+ if (!version)
+ version = g_convert ((gchar *)sname.string, sname.string_len,
+ "UTF-8", "UTF-16BE", NULL, NULL, NULL);
break;
case TT_NAME_ID_DESCRIPTION:
- g_free (description);
- description = g_convert ((gchar *)sname.string, sname.string_len,
- "UTF-8", "UTF-16BE", NULL, NULL, NULL);
+ if (!description)
+ description = g_convert ((gchar *)sname.string, sname.string_len,
+ "UTF-8", "UTF-16BE", NULL, NULL, NULL);
break;
case TT_NAME_ID_MANUFACTURER:
- g_free (manufacturer);
- manufacturer = g_convert ((gchar *)sname.string, sname.string_len,
- "UTF-8", "UTF-16BE", NULL, NULL, NULL);
+ if (!manufacturer)
+ manufacturer = g_convert ((gchar *)sname.string, sname.string_len,
+ "UTF-8", "UTF-16BE", NULL, NULL, NULL);
break;
case TT_NAME_ID_DESIGNER:
- g_free (designer);
- designer = g_convert ((gchar *)sname.string, sname.string_len,
- "UTF-8", "UTF-16BE", NULL, NULL, NULL);
+ if (!designer)
+ designer = g_convert ((gchar *)sname.string, sname.string_len,
+ "UTF-8", "UTF-16BE", NULL, NULL, NULL);
break;
case TT_NAME_ID_LICENSE:
- g_free (license);
- license = g_convert ((gchar *)sname.string, sname.string_len,
- "UTF-8", "UTF-16BE", NULL, NULL, NULL);
+ if (!license)
+ license = g_convert ((gchar *)sname.string, sname.string_len,
+ "UTF-8", "UTF-16BE", NULL, NULL, NULL);
break;
default:
break;
@@ -429,47 +419,37 @@ populate_grid (FontViewApplication *self,
if (version) {
strip_version (&version);
add_row (grid, _("Version"), version, FALSE);
- g_free (version);
}
if (copyright) {
strip_whitespace (©right);
add_row (grid, _("Copyright"), copyright, TRUE);
- g_free (copyright);
}
if (description) {
strip_whitespace (&description);
add_row (grid, _("Description"), description, TRUE);
- g_free (description);
}
if (manufacturer) {
strip_whitespace (&manufacturer);
add_row (grid, _("Manufacturer"), manufacturer, TRUE);
- g_free (manufacturer);
}
if (designer) {
strip_whitespace (&designer);
add_row (grid, _("Designer"), designer, TRUE);
- g_free (designer);
}
if (license) {
strip_whitespace (&license);
add_row (grid, _("License"), license, TRUE);
- g_free (license);
}
} else if (FT_Get_PS_Font_Info (face, &ps_info) == 0) {
- gchar *compressed;
-
if (ps_info.version && g_utf8_validate (ps_info.version, -1, NULL)) {
- compressed = g_strcompress (ps_info.version);
+ g_autofree gchar *compressed = g_strcompress (ps_info.version);
strip_version (&compressed);
add_row (grid, _("Version"), compressed, FALSE);
- g_free (compressed);
}
if (ps_info.notice && g_utf8_validate (ps_info.notice, -1, NULL)) {
- compressed = g_strcompress (ps_info.notice);
+ g_autofree gchar *compressed = g_strcompress (ps_info.notice);
strip_whitespace (&compressed);
add_row (grid, _("Copyright"), compressed, TRUE);
- g_free (compressed);
}
}
}
@@ -479,34 +459,30 @@ populate_details (FontViewApplication *self,
GtkWidget *grid,
FT_Face face)
{
- char *s;
+ g_autofree gchar *glyph_count = NULL, *features = NULL;
FT_MM_Var *ft_mm_var;
- s = g_strdup_printf ("%ld", face->num_glyphs);
- add_row (grid, _("Glyph Count"), s, FALSE);
- g_free (s);
+ glyph_count = g_strdup_printf ("%ld", face->num_glyphs);
+ add_row (grid, _("Glyph Count"), glyph_count, FALSE);
add_row (grid, _("Color Glyphs"), FT_HAS_COLOR (face) ? _("yes") : _("no"), FALSE);
- s = get_features (face);
- if (s)
- add_row (grid, _("Layout Features"), s, TRUE);
- g_free (s);
+ features = get_features (face);
+ if (features)
+ add_row (grid, _("Layout Features"), features, TRUE);
if (FT_Get_MM_Var (face, &ft_mm_var) == 0) {
int i;
for (i = 0; i < ft_mm_var->num_axis; i++) {
- s = describe_axis (&ft_mm_var->axis[i]);
- add_row (grid, i == 0 ? _("Variation Axes") : "", s, FALSE);
- g_free (s);
+ g_autofree gchar *s = describe_axis (&ft_mm_var->axis[i]);
+ add_row (grid, i == 0 ? _("Variation Axes") : "", s, FALSE);
}
{
- GString *str = g_string_new ("");
- for (i = 0; i < ft_mm_var->num_namedstyles; i++) {
- describe_instance (face, &ft_mm_var->namedstyle[i], i, str);
- }
- add_row (grid, _("Named Styles"), str->str, TRUE);
- g_string_free (str, TRUE);
+ g_autoptr(GString) str = g_string_new ("");
+ for (i = 0; i < ft_mm_var->num_namedstyles; i++)
+ describe_instance (face, &ft_mm_var->namedstyle[i], i, str);
+
+ add_row (grid, _("Named Styles"), str->str, TRUE);
}
free (ft_mm_var);
}
@@ -581,7 +557,6 @@ font_view_show_install_error (FontViewApplication *self,
{
install_button_refresh_appearance (self, error);
font_view_show_error (self, _("This font could not be installed."), error->message);
- g_error_free (error);
}
static void
@@ -590,7 +565,7 @@ font_install_finished (GObject *source_object,
gpointer user_data)
{
FontViewApplication *self = user_data;
- GError *err = NULL;
+ g_autoptr(GError) err = NULL;
g_task_propagate_boolean (G_TASK (res), &err);
@@ -608,16 +583,15 @@ install_font_job (GTask *task,
{
GFile *dest_location = user_data;
FontViewApplication *self = FONT_VIEW_APPLICATION (source_object);
- gchar *dest_basename = g_file_get_basename (self->font_file);
+ g_autofree gchar *dest_basename = g_file_get_basename (self->font_file);
+ g_autoptr(GError) error = NULL;
gboolean created = FALSE;
- GError *error = NULL;
gint i = 0;
while (!created) {
- gchar *dest_filename = (i == 0) ?
+ g_autofree gchar *dest_filename = (i == 0) ?
g_strdup (dest_basename) : g_strdup_printf ("%d%s", i, dest_basename);
- GFile *dest_file = g_file_get_child (dest_location, dest_filename);
- g_free (dest_filename);
+ g_autoptr(GFile) dest_file = g_file_get_child (dest_location, dest_filename);
created = g_file_copy (self->font_file,
dest_file,
@@ -625,7 +599,6 @@ install_font_job (GTask *task,
cancellable,
NULL, NULL,
&error);
- g_object_unref (dest_file);
if (error != NULL) {
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS)) {
@@ -638,25 +611,22 @@ install_font_job (GTask *task,
}
if (error != NULL)
- g_task_return_error (task, error);
+ g_task_return_error (task, g_steal_pointer (&error));
else
g_task_return_boolean (task, TRUE);
-
- g_free (dest_basename);
}
static void
font_view_install_font (FontViewApplication *self,
GFile *dest_location)
{
- GTask *task;
+ g_autoptr(GTask) task = NULL;
self->cancellable = g_cancellable_new ();
task = g_task_new (self, self->cancellable, font_install_finished, self);
g_task_set_task_data (task, g_object_ref (dest_location), g_object_unref);
g_task_run_in_thread (task, install_font_job);
- g_object_unref (task);
}
static void
@@ -664,13 +634,12 @@ install_button_clicked_cb (GtkButton *button,
gpointer user_data)
{
FontViewApplication *self = user_data;
- GError *err = NULL;
+ g_autoptr(GError) err = NULL;
+ g_autoptr(GFile) home_prefix = NULL, xdg_prefix = NULL;
+ g_autoptr(GFile) xdg_location = NULL, home_location = NULL, dest_location = NULL;
FcConfig *config;
FcStrList *str_list;
FcChar8 *path;
- GFile *xdg_prefix, *home_prefix, *file;
- GFile *xdg_location = NULL, *home_location = NULL;
- GFile *dest_location = NULL;
config = FcConfigGetCurrent ();
str_list = FcConfigGetFontDirs (config);
@@ -682,33 +651,26 @@ install_button_clicked_cb (GtkButton *button,
* under the home directory.
*/
while ((path = FcStrListNext (str_list)) != NULL) {
- file = g_file_new_for_path ((const gchar *) path);
+ g_autoptr(GFile) file = g_file_new_for_path ((const gchar *) path);
if (g_file_has_prefix (file, xdg_prefix)) {
- xdg_location = file;
+ xdg_location = g_steal_pointer (&file);
break;
}
if ((home_location == NULL) &&
g_file_has_prefix (file, home_prefix)) {
- home_location = file;
+ home_location = g_steal_pointer (&file);
break;
}
-
- g_object_unref (file);
}
FcStrListDone (str_list);
- g_object_unref (home_prefix);
- g_object_unref (xdg_prefix);
if (xdg_location != NULL)
- dest_location = g_object_ref (xdg_location);
+ dest_location = g_steal_pointer (&xdg_location);
else if (home_location != NULL)
- dest_location = g_object_ref (home_location);
-
- g_clear_object (&home_location);
- g_clear_object (&xdg_location);
+ dest_location = g_steal_pointer (&home_location);
if (dest_location == NULL) {
g_warning ("Install failed: can't find any configured user font directory.");
@@ -719,7 +681,6 @@ install_button_clicked_cb (GtkButton *button,
g_file_make_directory_with_parents (dest_location, NULL, &err);
if (err) {
font_view_show_install_error (self, err);
- g_object_unref (dest_location);
return;
}
}
@@ -727,26 +688,24 @@ install_button_clicked_cb (GtkButton *button,
font_view_install_font (self, dest_location);
install_button_refresh_appearance (self, NULL);
-
- g_object_unref (dest_location);
}
static void
font_view_show_font_error (FontViewApplication *self,
- const gchar *message)
+ GError *error)
{
- font_view_show_error (self, _("This font could not be displayed."), message);
+ font_view_show_error (self, _("This font could not be displayed."), error->message);
}
static void
font_widget_error_cb (SushiFontWidget *font_widget,
- const gchar *message,
+ GError *error,
gpointer user_data)
{
FontViewApplication *self = user_data;
font_view_application_do_overview (self);
- font_view_show_font_error (self, message);
+ font_view_show_font_error (self, error);
}
static void
@@ -817,32 +776,23 @@ font_visible_func (GtkTreeModel *model,
GtkTreeIter *iter,
gpointer data)
{
- FontViewApplication *self = data;
- gboolean ret;
- const char *search;
- char *name;
- char *cf_name;
- char *cf_search;
-
- if (!gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->search_toggle)))
- return TRUE;
-
- search = gtk_entry_get_text (GTK_ENTRY (self->search_entry));
+ FontViewApplication *self = data;
+ g_autofree gchar *name = NULL, *cf_name = NULL, *cf_search = NULL;
+ const char *search;
- gtk_tree_model_get (model, iter,
- COLUMN_NAME, &name,
- -1);
+ if (!gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->search_toggle)))
+ return TRUE;
- cf_name = g_utf8_casefold (name, -1);
- cf_search = g_utf8_casefold (search, -1);
+ search = gtk_entry_get_text (GTK_ENTRY (self->search_entry));
- ret = strstr (cf_name, cf_search) != NULL;
+ gtk_tree_model_get (model, iter,
+ COLUMN_NAME, &name,
+ -1);
- g_free (name);
- g_free (cf_name);
- g_free (cf_search);
+ cf_name = g_utf8_casefold (name, -1);
+ cf_search = g_utf8_casefold (search, -1);
- return ret;
+ return strstr (cf_name, cf_search) != NULL;
}
static void
@@ -868,15 +818,13 @@ font_view_application_do_open (FontViewApplication *self,
GFile *file,
gint face_index)
{
- GtkSizeGroup *install_size_group;
- GtkWidget *back_image;
- gchar *uri;
+ g_autofree gchar *uri = NULL;
font_view_ensure_model (self);
/* add install button */
if (self->install_button == NULL) {
- install_size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
+ g_autoptr(GtkSizeGroup) install_size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
self->install_label = g_object_ref_sink (gtk_label_new (_("Install")));
gtk_widget_show (self->install_label);
@@ -900,8 +848,6 @@ font_view_application_do_open (FontViewApplication *self,
g_signal_connect (self->install_button, "clicked",
G_CALLBACK (install_button_clicked_cb), self);
-
- g_object_unref (install_size_group);
}
if (self->info_button == NULL) {
@@ -916,6 +862,8 @@ font_view_application_do_open (FontViewApplication *self,
}
if (self->back_button == NULL) {
+ GtkWidget *back_image;
+
self->back_button = gtk_button_new ();
back_image = gtk_image_new_from_icon_name ("go-previous-symbolic",
GTK_ICON_SIZE_MENU);
@@ -952,8 +900,6 @@ font_view_application_do_open (FontViewApplication *self,
sushi_font_widget_load (SUSHI_FONT_WIDGET (self->font_widget));
}
- g_free (uri);
-
gtk_widget_show_all (self->main_window);
gtk_stack_set_visible_child_name (GTK_STACK (self->stack), "preview");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->info_button), FALSE);
@@ -965,12 +911,8 @@ icon_view_release_cb (GtkWidget *widget,
gpointer user_data)
{
FontViewApplication *self = user_data;
- GtkTreePath *path;
+ g_autoptr(GtkTreePath) path = NULL;
GtkTreeIter filter_iter;
- GtkTreeIter iter;
- gchar *font_path;
- gint face_index;
- GFile *file;
/* eat double/triple click events */
if (event->type != GDK_BUTTON_RELEASE)
@@ -981,6 +923,10 @@ icon_view_release_cb (GtkWidget *widget,
if (path != NULL &&
gtk_tree_model_get_iter (self->filter_model, &filter_iter, path)) {
+ g_autofree gchar *font_path = NULL;
+ GtkTreeIter iter;
+ gint face_index;
+
gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (self->filter_model),
&iter,
&filter_iter);
@@ -990,12 +936,9 @@ icon_view_release_cb (GtkWidget *widget,
-1);
if (font_path != NULL) {
- file = g_file_new_for_path (font_path);
+ g_autoptr(GFile) file = g_file_new_for_path (font_path);
font_view_application_do_open (self, file, face_index);
- g_object_unref (file);
}
- gtk_tree_path_free (path);
- g_free (font_path);
}
return FALSE;
@@ -1093,8 +1036,8 @@ query_info_ready_cb (GObject *object,
gpointer user_data)
{
FontViewApplication *self = user_data;
- GFileInfo *info;
- GError *error = NULL;
+ g_autoptr(GError) error = NULL;
+ g_autoptr(GFileInfo) info = NULL;
ensure_window (self);
g_application_release (G_APPLICATION (self));
@@ -1102,13 +1045,10 @@ query_info_ready_cb (GObject *object,
info = g_file_query_info_finish (G_FILE (object), res, &error);
if (error != NULL) {
font_view_application_do_overview (self);
- font_view_show_font_error (self, error->message);
- g_error_free (error);
+ font_view_show_font_error (self, error);
} else {
font_view_application_do_open (self, G_FILE (object), 0);
}
-
- g_clear_object (&info);
}
static void
@@ -1185,8 +1125,8 @@ search_text_changed (GtkEntry *entry,
static void
ensure_window (FontViewApplication *self)
{
+ g_autoptr(GtkBuilder) builder = NULL;
GtkWidget *window, *swin, *box, *image;
- GtkBuilder *builder;
GMenuModel *menu;
if (self->main_window)
@@ -1231,8 +1171,6 @@ ensure_window (FontViewApplication *self)
gtk_widget_show (self->menu_button);
gtk_header_bar_pack_end (GTK_HEADER_BAR (self->header), self->menu_button);
- g_object_unref (builder);
-
self->search_bar = gtk_search_bar_new ();
gtk_container_add (GTK_CONTAINER (box), self->search_bar);
self->search_entry = gtk_search_entry_new ();
@@ -1344,7 +1282,7 @@ int
main (int argc,
char **argv)
{
- GApplication *app;
+ g_autoptr(GApplication) app = NULL;
gint retval;
bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
@@ -1355,6 +1293,5 @@ main (int argc,
g_application_add_main_option_entries (app, goption_options);
retval = g_application_run (app, argc, argv);
- g_object_unref (app);
return retval;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]