Skip to content

Commit

Permalink
Use more g_autofree in advanced-exif..bar-sort
Browse files Browse the repository at this point in the history
Fix some memory leaks and simplify a bit.
  • Loading branch information
qarkai authored and caclark committed Dec 31, 2024
1 parent 2804517 commit d4babd3
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 274 deletions.
80 changes: 25 additions & 55 deletions src/advanced-exif.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,13 @@ static void advanced_exif_update(ExifWin *ew)
item = exif_get_first_item(exif_original);
while (item)
{
gchar *tag;
gchar *tag_name;
gchar *text;
gchar *utf8_text;
const gchar *format;
gint elements;
gchar *description;

tag = g_strdup_printf("0x%04x", exif_item_get_tag_id(item));
tag_name = exif_item_get_tag_name(item);
format = exif_item_get_format_name(item, TRUE);
text = exif_item_get_data_as_text(item, exif);
utf8_text = utf8_validate_or_convert(text);
g_free(text);
elements = exif_item_get_elements(item);
description = exif_item_get_description(item);
g_autofree gchar *tag = g_strdup_printf("0x%04x", exif_item_get_tag_id(item));
g_autofree gchar *tag_name = exif_item_get_tag_name(item);
g_autofree gchar *text = exif_item_get_data_as_text(item, exif);
g_autofree gchar *utf8_text = utf8_validate_or_convert(text);
const gchar *format = exif_item_get_format_name(item, TRUE);
const gint elements = exif_item_get_elements(item);
g_autofree gchar *description = exif_item_get_description(item);
if (!description || *description == '\0')
{
g_free(description);
Expand All @@ -162,10 +153,6 @@ static void advanced_exif_update(ExifWin *ew)
EXIF_ADVCOL_ELEMENTS, std::to_string(elements).c_str(),
EXIF_ADVCOL_DESCRIPTION, description,
-1);
g_free(tag);
g_free(utf8_text);
g_free(description);
g_free(tag_name);
item = exif_get_next_item(exif_original);
}
exif_free_fd(ew->fd, exif);
Expand Down Expand Up @@ -205,16 +192,14 @@ static void advanced_exif_dnd_get(GtkWidget *listview, GdkDragContext *,
GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(listview));
GtkTreeIter iter;

if (gtk_tree_selection_get_selected(sel, nullptr, &iter))
{
GtkTreeModel *store = gtk_tree_view_get_model(GTK_TREE_VIEW(listview));
gchar *key;
if (!gtk_tree_selection_get_selected(sel, nullptr, &iter)) return;

gtk_tree_model_get(store, &iter, EXIF_ADVCOL_NAME, &key, -1);
gtk_selection_data_set_text(selection_data, key, -1);
g_free(key);
}
GtkTreeModel *store = gtk_tree_view_get_model(GTK_TREE_VIEW(listview));

g_autofree gchar *key = nullptr;
gtk_tree_model_get(store, &iter, EXIF_ADVCOL_NAME, &key, -1);

gtk_selection_data_set_text(selection_data, key, -1);
}


Expand All @@ -223,16 +208,14 @@ static void advanced_exif_dnd_begin(GtkWidget *listview, GdkDragContext *context
GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(listview));
GtkTreeIter iter;

if (gtk_tree_selection_get_selected(sel, nullptr, &iter))
{
GtkTreeModel *store = gtk_tree_view_get_model(GTK_TREE_VIEW(listview));
gchar *key;
if (!gtk_tree_selection_get_selected(sel, nullptr, &iter)) return;

gtk_tree_model_get(store, &iter, EXIF_ADVCOL_NAME, &key, -1);
GtkTreeModel *store = gtk_tree_view_get_model(GTK_TREE_VIEW(listview));

dnd_set_drag_label(listview, context, key);
g_free(key);
}
g_autofree gchar *key = nullptr;
gtk_tree_model_get(store, &iter, EXIF_ADVCOL_NAME, &key, -1);

dnd_set_drag_label(listview, context, key);
}


Expand Down Expand Up @@ -326,7 +309,6 @@ static gboolean advanced_exif_mouseclick(GtkWidget *, GdkEventButton *, gpointer
GtkTreeViewColumn *column;
GtkTreeIter iter;
GtkTreeModel *store;
gchar *value;
GList *cols;
gint col_num;
GtkClipboard *clipboard;
Expand All @@ -339,13 +321,14 @@ static gboolean advanced_exif_mouseclick(GtkWidget *, GdkEventButton *, gpointer

cols = gtk_tree_view_get_columns(GTK_TREE_VIEW(ew->listview));
col_num = g_list_index(cols, column);

g_autofree gchar *value = nullptr;
gtk_tree_model_get(store, &iter, display_order[col_num], &value, -1);

clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
gtk_clipboard_set_text(clipboard, value, -1);

g_list_free(cols);
g_free(value);

gtk_tree_view_set_search_column(GTK_TREE_VIEW(ew->listview), gtk_tree_view_column_get_sort_column_id(column));
}
Expand Down Expand Up @@ -382,26 +365,13 @@ static gboolean advanced_exif_keypress(GtkWidget *, GdkEventKey *event, gpointer

static gboolean search_function_cb(GtkTreeModel *model, gint column, const gchar *key, GtkTreeIter *iter, gpointer)
{
gboolean ret = TRUE;
gchar *field_contents;
gchar *field_contents_nocase;
gchar *key_nocase;

g_autofree gchar *field_contents = nullptr;
gtk_tree_model_get(model, iter, column, &field_contents, -1);

field_contents_nocase = g_utf8_casefold(field_contents, -1);
key_nocase = g_utf8_casefold(key, -1);

if (g_strstr_len(field_contents_nocase, -1, key_nocase))
{
ret = FALSE;
}

g_free(field_contents);
g_free(field_contents_nocase);
g_free(key_nocase);
g_autofree gchar *field_contents_nocase = g_utf8_casefold(field_contents, -1);
g_autofree gchar *key_nocase = g_utf8_casefold(key, -1);

return ret;
return g_strstr_len(field_contents_nocase, -1, key_nocase) == nullptr;
}

static void exif_window_help_cb(GtkWidget *, gpointer)
Expand Down
23 changes: 7 additions & 16 deletions src/archives.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,27 +171,22 @@ gboolean extract(const char *filename, bool do_extract, int flags)
gchar *open_archive(const FileData *fd)
{
int flags;
gchar *current_dir;
gchar *destination_dir;
gboolean success;
gint error;

destination_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, fd->path, NULL);
g_autofree gchar *destination_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, fd->path, NULL);

if (!recursive_mkdir_if_not_exists(destination_dir, 0755))
{
log_printf("%s%s%s", _("Open Archive - Cannot create directory: "), destination_dir, "\n");
g_free(destination_dir);
log_printf("%s%s\n", _("Open Archive - Cannot create directory: "), destination_dir);
return nullptr;
}

current_dir = g_get_current_dir();
g_autofree gchar *current_dir = g_get_current_dir();
error = chdir(destination_dir);
if (error)
{
log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), destination_dir, _("\n Error code: "), strerror(errno), "\n");
g_free(destination_dir);
g_free(current_dir);
log_printf("%s%s%s%s\n", _("Open Archive - Cannot change directory to: "), destination_dir, _("\n Error code: "), strerror(errno));
return nullptr;
}

Expand All @@ -201,20 +196,16 @@ gchar *open_archive(const FileData *fd)
error = chdir(current_dir);
if (error)
{
log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), current_dir, _("\n Error code: "), strerror(errno), "\n");
g_free(destination_dir);
g_free(current_dir);
log_printf("%s%s%s%s\n", _("Open Archive - Cannot change directory to: "), current_dir, _("\n Error code: "), strerror(errno));
return nullptr;
}
g_free(current_dir);

if (!success)
{
g_free(destination_dir);
destination_dir = nullptr;
return nullptr;
}

return destination_dir;
return g_steal_pointer(&destination_dir);
}
#else
gchar *open_archive(const FileData *)
Expand Down
34 changes: 11 additions & 23 deletions src/bar-comment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,22 @@ struct PaneCommentData

static void bar_pane_comment_write(PaneCommentData *pcd)
{
gchar *comment;

if (!pcd->fd) return;

comment = text_widget_text_pull(pcd->comment_view);
g_autofree gchar *comment = text_widget_text_pull(pcd->comment_view);

metadata_write_string(pcd->fd, pcd->key, comment);
g_free(comment);
}


static void bar_pane_comment_update(PaneCommentData *pcd)
{
gchar *comment = nullptr;
gchar *orig_comment = nullptr;
g_autofree gchar *comment = nullptr;
const gchar *comment_not_null;
gshort rating;
GtkTextBuffer *comment_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));

orig_comment = text_widget_text_pull(pcd->comment_view);
g_autofree gchar *orig_comment = text_widget_text_pull(pcd->comment_view);
if (g_strcmp0(pcd->key, "Xmp.xmp.Rating") == 0)
{
rating = metadata_read_int(pcd->fd, pcd->key, 0);
Expand All @@ -108,8 +104,6 @@ static void bar_pane_comment_update(PaneCommentData *pcd)
gtk_text_buffer_set_text(comment_buffer, comment_not_null, -1);
g_signal_handlers_unblock_by_func(comment_buffer, (gpointer)bar_pane_comment_changed, pcd);
}
g_free(comment);
g_free(orig_comment);

gtk_widget_set_sensitive(pcd->comment_view, (pcd->fd != nullptr));
}
Expand All @@ -118,9 +112,8 @@ static void bar_pane_comment_set_selection(PaneCommentData *pcd, gboolean append
{
GList *list = nullptr;
GList *work;
gchar *comment = nullptr;

comment = text_widget_text_pull(pcd->comment_view);
g_autofree gchar *comment = text_widget_text_pull(pcd->comment_view);

list = layout_selection_list(pcd->pane.lw);
list = file_data_process_groups_in_selection(list, FALSE, nullptr);
Expand All @@ -143,7 +136,6 @@ static void bar_pane_comment_set_selection(PaneCommentData *pcd, gboolean append
}

filelist_free(list);
g_free(comment);
}

static void bar_pane_comment_sel_add_cb(GtkWidget *, gpointer data)
Expand Down Expand Up @@ -330,12 +322,11 @@ static GtkWidget *bar_pane_comment_new(const gchar *id, const gchar *title, cons

GtkWidget *bar_pane_comment_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
{
gchar *title = nullptr;
gchar *key = g_strdup(COMMENT_KEY);
g_autofree gchar *title = nullptr;
g_autofree gchar *key = g_strdup(COMMENT_KEY);
gboolean expanded = TRUE;
gint height = 50;
gchar *id = g_strdup("comment");
GtkWidget *ret;
g_autofree gchar *id = g_strdup("comment");

while (*attribute_names)
{
Expand Down Expand Up @@ -369,11 +360,8 @@ GtkWidget *bar_pane_comment_new_from_config(const gchar **attribute_names, const
}

bar_pane_translate_title(PANE_COMMENT, id, &title);
ret = bar_pane_comment_new(id, title, key, expanded, height);
g_free(title);
g_free(key);
g_free(id);
return ret;

return bar_pane_comment_new(id, title, key, expanded, height);
}

void bar_pane_comment_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
Expand All @@ -383,7 +371,7 @@ void bar_pane_comment_update_from_config(GtkWidget *pane, const gchar **attribut
pcd = static_cast<PaneCommentData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
if (!pcd) return;

gchar *title = nullptr;
g_autofree gchar *title = nullptr;

while (*attribute_names)
{
Expand All @@ -403,8 +391,8 @@ void bar_pane_comment_update_from_config(GtkWidget *pane, const gchar **attribut
{
bar_pane_translate_title(PANE_COMMENT, pcd->pane.id, &title);
gtk_label_set_text(GTK_LABEL(pcd->pane.title), title);
g_free(title);
}

gtk_widget_set_size_request(pcd->widget, -1, pcd->height);
bar_update_expander(pane);
bar_pane_comment_update(pcd);
Expand Down
Loading

0 comments on commit d4babd3

Please sign in to comment.