Skip to content

Commit 5584d60

Browse files
qarkaicaclark
authored andcommitted
Add collection_by_index_add_filelist() to remove dependency from menu
Fix memory leak.
1 parent 45c2234 commit 5584d60

File tree

12 files changed

+51
-69
lines changed

12 files changed

+51
-69
lines changed

src/collect-io.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,4 +1156,18 @@ void collect_manager_list(GList **names_exc, GList **names_inc, GList **paths)
11561156
}
11571157
}
11581158
}
1159+
1160+
gchar *collection_manager_path_by_index(gint index)
1161+
{
1162+
if (index < 0) return nullptr;
1163+
1164+
GList *collection_list = nullptr;
1165+
collect_manager_list(&collection_list, nullptr, nullptr);
1166+
1167+
auto *collection_name = static_cast<gchar *>(g_list_nth_data(collection_list, index));
1168+
gchar *path = collection_path(collection_name);
1169+
g_list_free_full(collection_list, g_free);
1170+
1171+
return path;
1172+
}
11591173
/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */

src/collect-io.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ void collect_manager_flush();
5757

5858
void collect_manager_notify_cb(FileData *fd, NotifyType type, gpointer data);
5959
void collect_manager_list(GList **names_exc, GList **names_inc, GList **paths);
60+
gchar *collection_manager_path_by_index(gint index);
6061

6162
#endif
6263
/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */

src/collect.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,20 @@ GList *collection_contents_fd(const gchar *name)
414414
return list;
415415
}
416416

417+
/**
418+
* @brief Add file selection list to a collection
419+
* @param[in] index Index to the collection list, or -1 for new collection
420+
* @param[in] list List of ::_FileData
421+
*
422+
*/
423+
void collection_by_index_add_filelist(gint index, GList *list)
424+
{
425+
g_autofree gchar *path = collection_manager_path_by_index(index);
426+
CollectWindow *cw = collection_window_new(path);
427+
428+
collection_table_add_filelist(cw->table, list);
429+
}
430+
417431
/*
418432
*-------------------------------------------------------------------
419433
* please use these to actually add/remove stuff

src/collect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ gboolean is_collection(const gchar *param);
135135
gchar *collection_path(const gchar *param);
136136
GString *collection_contents(const gchar *name, GString *contents) G_GNUC_WARN_UNUSED_RESULT;
137137
GList *collection_contents_fd(const gchar *name);
138+
void collection_by_index_add_filelist(gint index, GList *list);
138139

139140
#endif
140141
/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */

src/dupe.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include <glib-object.h>
3535

3636
#include "cache.h"
37-
#include "collect-table.h"
3837
#include "collect.h"
3938
#include "compat-deprecated.h"
4039
#include "compat.h"
@@ -3172,9 +3171,8 @@ static void dupe_window_edit_selected(DupeWindow *dw, const gchar *key)
31723171

31733172
static void dupe_window_collection_from_selection(DupeWindow *dw)
31743173
{
3175-
CollectWindow *w = collection_window_new(nullptr);
31763174
g_autoptr(FileDataList) list = dupe_listview_get_selection(dw->listview);
3177-
collection_table_add_filelist(w->table, list);
3175+
collection_by_index_add_filelist(-1, list);
31783176
}
31793177

31803178
static void dupe_window_append_file_list(DupeWindow *dw, gint on_second)
@@ -3330,7 +3328,7 @@ static void dupe_pop_menu_collections_cb(GtkWidget *widget, gpointer data)
33303328
auto *dw = static_cast<DupeWindow *>(submenu_item_get_data(widget));
33313329

33323330
g_autoptr(FileDataList) selection_list = dupe_listview_get_selection(dw->listview);
3333-
pop_menu_collections(selection_list, data);
3331+
collection_by_index_add_filelist(GPOINTER_TO_INT(data), selection_list);
33343332
}
33353333

33363334
static GtkWidget *dupe_menu_popup_main(DupeWindow *dw, DupeItem *di)

src/img-view.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,7 @@ static void image_pop_menu_collections_cb(GtkWidget *widget, gpointer data)
13221322
fd = image_get_fd(imd);
13231323

13241324
g_autoptr(FileDataList) selection_list = g_list_append(nullptr, fd);
1325-
pop_menu_collections(selection_list, data);
1325+
collection_by_index_add_filelist(GPOINTER_TO_INT(data), selection_list);
13261326
}
13271327

13281328
static GtkWidget *view_popup_menu(ViewWindow *vw)

src/layout-image.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ static void layout_pop_menu_collections_cb(GtkWidget *widget, gpointer data)
754754
auto *lw = static_cast<LayoutWindow *>(submenu_item_get_data(widget));
755755

756756
g_autoptr(FileDataList) selection_list = g_list_append(nullptr, layout_image_get_fd(lw));
757-
pop_menu_collections(selection_list, data);
757+
collection_by_index_add_filelist(GPOINTER_TO_INT(data), selection_list);
758758
}
759759

760760
static void li_pop_menu_selectable_toolbars_toggle_cb(GtkWidget *, gpointer)

src/menu.cc

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
#include <gdk/gdk.h>
2525

2626
#include "collect-io.h"
27-
#include "collect-table.h"
28-
#include "collect.h"
2927
#include "editors.h"
3028
#include "intl.h"
3129
#include "pixbuf-util.h"
@@ -332,29 +330,12 @@ GtkWidget *submenu_add_alter(GtkWidget *menu, GCallback func, gpointer data)
332330
/**
333331
* @brief Add submenu consisting of "New collection", and list of existing collections to a right-click menu.
334332
* @param[in] menu
333+
* @param[out] menu_item
335334
* @param[in] func
336-
* @param[in] collection_list Type gchar
337335
* @param[in] data
338336
*
339337
* Used by all image windows
340338
*/
341-
static void add_collection_list(GtkWidget *menu, GCallback func, GList *collection_list, gpointer)
342-
{
343-
GList *work;
344-
gint index = 0; /* index to existing collection list menu item selected */
345-
346-
work = collection_list;
347-
while (work)
348-
{
349-
auto collection_name = static_cast<const gchar *>(work->data);
350-
351-
menu_item_add(menu, collection_name, func,
352-
GINT_TO_POINTER(index));
353-
work = work->next;
354-
index++;
355-
}
356-
}
357-
358339
GtkWidget *submenu_add_collections(GtkWidget *menu, GtkWidget **menu_item,
359340
GCallback func, gpointer data)
360341
{
@@ -367,49 +348,25 @@ GtkWidget *submenu_add_collections(GtkWidget *menu, GtkWidget **menu_item,
367348
submenu = gtk_menu_new();
368349
g_object_set_data(G_OBJECT(submenu), "submenu_data", data);
369350

370-
menu_item_add_icon_sensitive(submenu, _("New collection"),
371-
PIXBUF_INLINE_COLLECTION, TRUE, G_CALLBACK(func), GINT_TO_POINTER(-1));
351+
menu_item_add_icon_sensitive(submenu, _("New collection"), PIXBUF_INLINE_COLLECTION,
352+
TRUE, G_CALLBACK(func), GINT_TO_POINTER(-1));
372353
menu_item_add_divider(submenu);
373354

374355
collect_manager_list(&collection_list,nullptr,nullptr);
375-
add_collection_list(submenu, func, collection_list, data);
356+
357+
gint index = 0; /* index to existing collection list menu item selected */
358+
for (GList *work = collection_list; work; work = work->next, index++)
359+
{
360+
auto *collection_name = static_cast<gchar *>(work->data);
361+
menu_item_add(submenu, collection_name, func, GINT_TO_POINTER(index));
362+
}
376363

377364
gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
378365
if (menu_item) *menu_item = item;
379366

380-
g_list_free(collection_list);
367+
g_list_free_full(collection_list, g_free);
381368

382369
return submenu;
383370
}
384371

385-
/**
386-
* @brief Add file selection list to a collection
387-
* @param[in] selection_list Selection list of ::_FileData
388-
* @param[in] data Index to the collection list menu item selected, or -1 for new collection
389-
*
390-
*
391-
*/
392-
void pop_menu_collections(GList *selection_list, gpointer data)
393-
{
394-
CollectWindow *cw;
395-
gchar *collection_name;
396-
GList *collection_list = nullptr;
397-
const gint index = GPOINTER_TO_INT(data);
398-
399-
if (index >= 0)
400-
{
401-
collect_manager_list(&collection_list, nullptr, nullptr);
402-
collection_name = static_cast<gchar *>(g_list_nth_data(collection_list, index));
403-
g_autofree gchar *name = collection_path(collection_name);
404-
cw = collection_window_new(name);
405-
g_list_free_full(collection_list, g_free);
406-
}
407-
else
408-
{
409-
cw = collection_window_new(nullptr);
410-
}
411-
412-
collection_table_add_filelist(cw->table, selection_list);
413-
}
414-
415372
/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */

src/menu.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,5 @@ GtkWidget *submenu_add_alter(GtkWidget *menu, GCallback func, gpointer data);
4747

4848
GtkWidget *submenu_add_collections(GtkWidget *menu, GtkWidget **menu_item,
4949
GCallback func, gpointer data);
50-
void pop_menu_collections(GList *selection_list, gpointer data);
5150
#endif
5251
/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */

src/pan-view/pan-view.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "bar-exif.h"
3636
#include "cache-loader.h"
3737
#include "cache.h"
38+
#include "collect.h"
3839
#include "compat.h"
3940
#include "dnd.h"
4041
#include "editors.h"
@@ -2281,7 +2282,7 @@ static void pan_pop_menu_collections_cb(GtkWidget *widget, gpointer data)
22812282
auto *pw = static_cast<PanWindow *>(submenu_item_get_data(widget));
22822283

22832284
g_autoptr(FileDataList) selection_list = g_list_append(nullptr, pan_menu_click_fd(pw));
2284-
pop_menu_collections(selection_list, data);
2285+
collection_by_index_add_filelist(GPOINTER_TO_INT(data), selection_list);
22852286
}
22862287

22872288
static GtkWidget *pan_popup_menu(PanWindow *pw)

0 commit comments

Comments
 (0)