From 3bbefe2103e73e10631ee2188227a1a7b12f3918 Mon Sep 17 00:00:00 2001 From: Omari Stephens Date: Fri, 7 Jul 2017 13:57:17 +0000 Subject: [PATCH] deduplicate shared code in view_file_icon and view_file_list Also, moves view_file.c into the view_file subdir. --- src/Makefile.am | 1 - src/view_file/Makefile.am | 1 + src/{ => view_file}/view_file.c | 157 ++++++++++++-------------------- src/view_file/view_file_icon.c | 65 ------------- src/view_file/view_file_list.c | 59 ------------ 5 files changed, 60 insertions(+), 223 deletions(-) rename src/{ => view_file}/view_file.c (90%) diff --git a/src/Makefile.am b/src/Makefile.am index 15bdf1b1..f01fb199 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -251,7 +251,6 @@ geeqie_SOURCES = \ view_dir_list.h \ view_dir_tree.c \ view_dir_tree.h \ - view_file.c \ view_file.h \ window.c \ window.h \ diff --git a/src/view_file/Makefile.am b/src/view_file/Makefile.am index 6837e720..7c069856 100644 --- a/src/view_file/Makefile.am +++ b/src/view_file/Makefile.am @@ -1,4 +1,5 @@ module_view_file = \ + %D%/view_file.c \ %D%/view_file_icon.c \ %D%/view_file_icon.h \ %D%/view_file_list.c \ diff --git a/src/view_file.c b/src/view_file/view_file.c similarity index 90% rename from src/view_file.c rename to src/view_file/view_file.c index cd4cf09f..c9dbdef8 100644 --- a/src/view_file.c +++ b/src/view_file/view_file.c @@ -68,57 +68,53 @@ void vf_sort_set(ViewFile *vf, SortType type, gboolean ascend) FileData *vf_index_get_data(ViewFile *vf, gint row) { - FileData *fd = NULL; - - switch (vf->type) - { - case FILEVIEW_LIST: fd = vflist_index_get_data(vf, row); break; - case FILEVIEW_ICON: fd = vficon_index_get_data(vf, row); break; - } - - return fd; + return g_list_nth_data(vf->list, row); } gint vf_index_by_fd(ViewFile *vf, FileData *fd) { - gint index = -1; - switch (vf->type) { - case FILEVIEW_LIST: index = vflist_index_by_fd(vf, fd); break; - case FILEVIEW_ICON: index = vficon_index_by_fd(vf, fd); break; + case FILEVIEW_LIST: return vflist_index_by_fd(vf, fd); + case FILEVIEW_ICON: return vficon_index_by_fd(vf, fd); } - - return index; } guint vf_count(ViewFile *vf, gint64 *bytes) { - guint count = 0; + if (bytes) + { + gint64 b = 0; + GList *work; - switch (vf->type) - { - case FILEVIEW_LIST: count = vflist_count(vf, bytes); break; - case FILEVIEW_ICON: count = vficon_count(vf, bytes); break; - } + work = vf->list; + while (work) + { + FileData *fd = work->data; + work = work->next; + + b += fd->size; + } - return count; + *bytes = b; + } + + return g_list_length(vf->list); } GList *vf_get_list(ViewFile *vf) { GList *list = NULL; + GList *work; + for (work = vf->list; work; work = work->next) + { + FileData *fd = work->data; + list = g_list_prepend(list, file_data_ref(fd)); + } - switch (vf->type) - { - case FILEVIEW_LIST: list = vflist_get_list(vf); break; - case FILEVIEW_ICON: list = vficon_get_list(vf); break; - } - - return list; + return g_list_reverse(list); } - /* *------------------------------------------------------------------- * keyboard @@ -128,15 +124,12 @@ GList *vf_get_list(ViewFile *vf) static gboolean vf_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data) { ViewFile *vf = data; - gboolean ret = FALSE; switch (vf->type) { - case FILEVIEW_LIST: ret = vflist_press_key_cb(widget, event, data); break; - case FILEVIEW_ICON: ret = vficon_press_key_cb(widget, event, data); break; + case FILEVIEW_LIST: return vflist_press_key_cb(widget, event, data); + case FILEVIEW_ICON: return vficon_press_key_cb(widget, event, data); } - - return ret; } /* @@ -148,29 +141,23 @@ static gboolean vf_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer static gboolean vf_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data) { ViewFile *vf = data; - gboolean ret = FALSE; switch (vf->type) { - case FILEVIEW_LIST: ret = vflist_press_cb(widget, bevent, data); break; - case FILEVIEW_ICON: ret = vficon_press_cb(widget, bevent, data); break; + case FILEVIEW_LIST: return vflist_press_cb(widget, bevent, data); + case FILEVIEW_ICON: return vficon_press_cb(widget, bevent, data); } - - return ret; } static gboolean vf_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data) { ViewFile *vf = data; - gboolean ret = FALSE; switch (vf->type) { - case FILEVIEW_LIST: ret = vflist_release_cb(widget, bevent, data); break; - case FILEVIEW_ICON: ret = vficon_release_cb(widget, bevent, data); break; + case FILEVIEW_LIST: return vflist_release_cb(widget, bevent, data); + case FILEVIEW_ICON: return vficon_release_cb(widget, bevent, data); } - - return ret; } @@ -182,55 +169,39 @@ static gboolean vf_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointe gboolean vf_index_is_selected(ViewFile *vf, gint row) { - gboolean ret = FALSE; - switch (vf->type) { - case FILEVIEW_LIST: ret = vflist_index_is_selected(vf, row); break; - case FILEVIEW_ICON: ret = vficon_index_is_selected(vf, row); break; + case FILEVIEW_LIST: return vflist_index_is_selected(vf, row); + case FILEVIEW_ICON: return vficon_index_is_selected(vf, row); } - - return ret; } guint vf_selection_count(ViewFile *vf, gint64 *bytes) { - guint count = 0; - switch (vf->type) { - case FILEVIEW_LIST: count = vflist_selection_count(vf, bytes); break; - case FILEVIEW_ICON: count = vficon_selection_count(vf, bytes); break; + case FILEVIEW_LIST: return vflist_selection_count(vf, bytes); + case FILEVIEW_ICON: return vficon_selection_count(vf, bytes); } - - return count; } GList *vf_selection_get_list(ViewFile *vf) { - GList *list = NULL; - switch (vf->type) { - case FILEVIEW_LIST: list = vflist_selection_get_list(vf); break; - case FILEVIEW_ICON: list = vficon_selection_get_list(vf); break; + case FILEVIEW_LIST: return vflist_selection_get_list(vf); + case FILEVIEW_ICON: return vficon_selection_get_list(vf); } - - return list; } GList *vf_selection_get_list_by_index(ViewFile *vf) { - GList *list = NULL; - switch (vf->type) { - case FILEVIEW_LIST: list = vflist_selection_get_list_by_index(vf); break; - case FILEVIEW_ICON: list = vficon_selection_get_list_by_index(vf); break; + case FILEVIEW_LIST: return vflist_selection_get_list_by_index(vf); + case FILEVIEW_ICON: return vficon_selection_get_list_by_index(vf); } - - return list; } void vf_select_all(ViewFile *vf) @@ -311,28 +282,20 @@ static void vf_dnd_init(ViewFile *vf) GList *vf_pop_menu_file_list(ViewFile *vf) { - GList *ret = NULL; - switch (vf->type) { - case FILEVIEW_LIST: ret = vflist_pop_menu_file_list(vf); break; - case FILEVIEW_ICON: ret = vficon_pop_menu_file_list(vf); break; + case FILEVIEW_LIST: return vflist_pop_menu_file_list(vf); + case FILEVIEW_ICON: return vficon_pop_menu_file_list(vf); } - - return ret; } GList *vf_selection_get_one(ViewFile *vf, FileData *fd) { - GList *ret = NULL; - switch (vf->type) { - case FILEVIEW_LIST: ret = vflist_selection_get_one(vf, fd); break; - case FILEVIEW_ICON: ret = vficon_selection_get_one(vf, fd); break; + case FILEVIEW_LIST: return vflist_selection_get_one(vf, fd); + case FILEVIEW_ICON: return vficon_selection_get_one(vf, fd); } - - return ret; } static void vf_pop_menu_edit_cb(GtkWidget *widget, gpointer data) @@ -677,28 +640,20 @@ GtkWidget *vf_pop_menu(ViewFile *vf) gboolean vf_refresh(ViewFile *vf) { - gboolean ret = FALSE; - switch (vf->type) { - case FILEVIEW_LIST: ret = vflist_refresh(vf); break; - case FILEVIEW_ICON: ret = vficon_refresh(vf); break; + case FILEVIEW_LIST: return vflist_refresh(vf); + case FILEVIEW_ICON: return vficon_refresh(vf); } - - return ret; } gboolean vf_set_fd(ViewFile *vf, FileData *dir_fd) { - gboolean ret = FALSE; - switch (vf->type) { - case FILEVIEW_LIST: ret = vflist_set_fd(vf, dir_fd); break; - case FILEVIEW_ICON: ret = vficon_set_fd(vf, dir_fd); break; + case FILEVIEW_LIST: return vflist_set_fd(vf, dir_fd); + case FILEVIEW_ICON: return vficon_set_fd(vf, dir_fd); } - - return ret; } static void vf_destroy_cb(GtkWidget *widget, gpointer data) @@ -958,11 +913,17 @@ static gboolean vf_thumb_next(ViewFile *vf) static void vf_thumb_reset_all(ViewFile *vf) { - switch (vf->type) - { - case FILEVIEW_LIST: vflist_thumb_reset_all(vf); break; - case FILEVIEW_ICON: vficon_thumb_reset_all(vf); break; - } + GList *work; + + for (work = vf->list; work; work = work->next) + { + FileData *fd = work->data; + if (fd->thumb_pixbuf) + { + g_object_unref(fd->thumb_pixbuf); + fd->thumb_pixbuf = NULL; + } + } } void vf_thumb_update(ViewFile *vf) diff --git a/src/view_file/view_file_icon.c b/src/view_file/view_file_icon.c index 6fbc7d23..b60074c5 100644 --- a/src/view_file/view_file_icon.c +++ b/src/view_file/view_file_icon.c @@ -1750,38 +1750,12 @@ FileData *vficon_thumb_next_fd(ViewFile *vf) return NULL; } -void vficon_thumb_reset_all(ViewFile *vf) -{ - GList *work = vf->list; - - while (work) - { - FileData *fd = work->data; - if (fd->thumb_pixbuf) - { - g_object_unref(fd->thumb_pixbuf); - fd->thumb_pixbuf = NULL; - } - work = work->next; - } -} - - /* *----------------------------------------------------------------------------- * row stuff *----------------------------------------------------------------------------- */ -FileData *vficon_index_get_data(ViewFile *vf, gint row) -{ - FileData *fd; - - fd = g_list_nth_data(vf->list, row); - return fd ? fd : NULL; -} - - gint vficon_index_by_fd(ViewFile *vf, FileData *in_fd) { gint p = 0; @@ -1801,45 +1775,6 @@ gint vficon_index_by_fd(ViewFile *vf, FileData *in_fd) return -1; } -guint vficon_count(ViewFile *vf, gint64 *bytes) -{ - if (bytes) - { - gint64 b = 0; - GList *work; - - work = vf->list; - while (work) - { - FileData *fd = work->data; - work = work->next; - - b += fd->size; - } - - *bytes = b; - } - - return g_list_length(vf->list); -} - -GList *vficon_get_list(ViewFile *vf) -{ - GList *list = NULL; - GList *work; - - work = vf->list; - while (work) - { - FileData *fd = work->data; - work = work->next; - - list = g_list_prepend(list, file_data_ref(fd)); - } - - return g_list_reverse(list); -} - /* *----------------------------------------------------------------------------- * diff --git a/src/view_file/view_file_list.c b/src/view_file/view_file_list.c index 38be2a99..b56a025c 100644 --- a/src/view_file/view_file_list.c +++ b/src/view_file/view_file_list.c @@ -1144,33 +1144,12 @@ FileData *vflist_thumb_next_fd(ViewFile *vf) return fd; } - -void vflist_thumb_reset_all(ViewFile *vf) -{ - GList *work = vf->list; - while (work) - { - FileData *fd = work->data; - if (fd->thumb_pixbuf) - { - g_object_unref(fd->thumb_pixbuf); - fd->thumb_pixbuf = NULL; - } - work = work->next; - } -} - /* *----------------------------------------------------------------------------- * row stuff *----------------------------------------------------------------------------- */ -FileData *vflist_index_get_data(ViewFile *vf, gint row) -{ - return g_list_nth_data(vf->list, row); -} - gint vflist_index_by_fd(ViewFile *vf, FileData *fd) { gint p = 0; @@ -1201,44 +1180,6 @@ gint vflist_index_by_fd(ViewFile *vf, FileData *fd) return -1; } -guint vflist_count(ViewFile *vf, gint64 *bytes) -{ - if (bytes) - { - gint64 b = 0; - GList *work; - - work = vf->list; - while (work) - { - FileData *fd = work->data; - work = work->next; - b += fd->size; - } - - *bytes = b; - } - - return g_list_length(vf->list); -} - -GList *vflist_get_list(ViewFile *vf) -{ - GList *list = NULL; - GList *work; - - work = vf->list; - while (work) - { - FileData *fd = work->data; - work = work->next; - - list = g_list_prepend(list, file_data_ref(fd)); - } - - return g_list_reverse(list); -} - /* *----------------------------------------------------------------------------- * selections -- 2.20.1