From cbbd540477c1bd35b23bda0f03b5fcc2adec7cb4 Mon Sep 17 00:00:00 2001 From: Yogesh Kaushik <masterlukeskywalker02@gmail.com> Date: Thu, 12 May 2022 01:08:51 +0530 Subject: [PATCH] Usage of glist to concatinate statusbar strings * Usage of '|' as separator * Added method 'thunar_util_strjoin_list' * Refactored 'thunar_list_model_get_statusbar_text()' and 'thunar_list_model_get_statusbar_text_for_files()' MR !221 --- thunar/thunar-list-model.c | 172 +++++++++++++++---------------------- thunar/thunar-util.c | 47 ++++++++++ thunar/thunar-util.h | 2 + 3 files changed, 116 insertions(+), 105 deletions(-) diff --git a/thunar/thunar-list-model.c b/thunar/thunar-list-model.c index e6df5929a..8b61025d2 100644 --- a/thunar/thunar-list-model.c +++ b/thunar/thunar-list-model.c @@ -40,6 +40,7 @@ #include <thunar/thunar-private.h> #include <thunar/thunar-user.h> #include <thunar/thunar-simple-job.h> +#include <thunar/thunar-util.h> @@ -197,7 +198,7 @@ static gint sort_by_size_in_bytes (const ThunarF static gint sort_by_type (const ThunarFile *a, const ThunarFile *b, gboolean case_sensitive); - + static gboolean thunar_list_model_get_case_sensitive (ThunarListModel *store); static void thunar_list_model_set_case_sensitive (ThunarListModel *store, gboolean case_sensitive); @@ -2807,15 +2808,17 @@ thunar_list_model_get_paths_for_pattern (ThunarListModel *store, * Return value: the statusbar text for @store with the given @files. **/ static gchar* -thunar_list_model_get_statusbar_text_for_files (GList *files, - gboolean show_file_size_binary_format) +thunar_list_model_get_statusbar_text_for_files (ThunarListModel *store, + GList *files, + gboolean show_file_size_binary_format) { guint64 size_summary = 0; gint folder_count = 0; gint non_folder_count = 0; GList *lp; + GList *text_list = NULL; gchar *size_string = NULL; - gchar *text = ""; + gchar *temp_string = NULL; gchar *folder_text = NULL; gchar *non_folder_text = NULL; ThunarPreferences *preferences; @@ -2865,7 +2868,6 @@ thunar_list_model_get_statusbar_text_for_files (GList *files, } else non_folder_text = g_strdup_printf (ngettext ("%d file", "%d files", non_folder_count), non_folder_count); - } if (folder_count > 0) @@ -2875,24 +2877,16 @@ thunar_list_model_get_statusbar_text_for_files (GList *files, folder_count), folder_count); } - if (folder_text == NULL && non_folder_text == NULL) - text = g_strdup_printf (_("0 items")); - else if (folder_text == NULL) - text = non_folder_text; - else if (non_folder_text == NULL) - text = folder_text; - else - { - /* This is marked for translation in case a localizer - * needs to change ", " to something else. The comma - * is between the message about the number of folders - * and the number of items in the selection */ - /* TRANSLATORS: string moved from line 2573 to here */ - text = g_strdup_printf (_("%s, %s"), folder_text, non_folder_text); - g_free (folder_text); - g_free (non_folder_text); - } - return text; + if (non_folder_text == NULL && folder_text == NULL) + text_list = g_list_append (text_list, g_strdup_printf (_("0 items"))); + if (folder_text != NULL) + text_list = g_list_append (text_list, folder_text); + if (non_folder_text != NULL) + text_list = g_list_append (text_list, non_folder_text); + + temp_string = thunar_util_strjoin_list (text_list, " | "); + g_list_free_full (text_list, g_free); + return temp_string; } @@ -2926,13 +2920,9 @@ thunar_list_model_get_statusbar_text (ThunarListModel *store, ThunarFile *file; guint64 size; GList *lp; - gchar *absolute_path; - gchar *fspace_string; - gchar *display_name = ""; - gchar *size_string; - gchar *filetype_string = ""; - gchar *text; - gchar *s; + GList *text_list = NULL; + gchar *temp_string = NULL; + gchar *text = ""; gint height; gint width; GSequenceIter *row; @@ -2946,14 +2936,13 @@ thunar_list_model_get_statusbar_text (ThunarListModel *store, _thunar_return_val_if_fail (THUNAR_IS_LIST_MODEL (store), NULL); - show_file_size_binary_format = thunar_list_model_get_file_size_binary(store); - preferences = thunar_preferences_get (); g_object_get (G_OBJECT (preferences), "misc-status-bar-active-info", &active, NULL); show_size = thunar_status_bar_info_check_active (active, THUNAR_STATUS_BAR_INFO_SIZE); show_size_in_bytes = thunar_status_bar_info_check_active (active, THUNAR_STATUS_BAR_INFO_SIZE_IN_BYTES); show_filetype = thunar_status_bar_info_check_active (active, THUNAR_STATUS_BAR_INFO_FILETYPE); show_display_name = thunar_status_bar_info_check_active (active, THUNAR_STATUS_BAR_INFO_DISPLAY_NAME); + show_file_size_binary_format = thunar_list_model_get_file_size_binary(store); if (selected_items == NULL) /* nothing selected */ { @@ -2966,29 +2955,19 @@ thunar_list_model_get_statusbar_text (ThunarListModel *store, /* try to determine a file for the current folder */ file = (store->folder != NULL) ? thunar_folder_get_corresponding_file (store->folder) : NULL; + temp_string = thunar_list_model_get_statusbar_text_for_files (store, relevant_files, show_file_size_binary_format); + text_list = g_list_append (text_list, temp_string); /* check if we can determine the amount of free space for the volume */ - if (G_LIKELY (file != NULL - && thunar_g_file_get_free_space (thunar_file_get_file (file), &size, NULL))) + if (G_LIKELY (file != NULL && thunar_g_file_get_free_space (thunar_file_get_file (file), &size, NULL))) { - size_string = thunar_list_model_get_statusbar_text_for_files (relevant_files, show_file_size_binary_format); - /* humanize the free space */ - fspace_string = g_format_size_full (size, show_file_size_binary_format ? G_FORMAT_SIZE_IEC_UNITS : G_FORMAT_SIZE_DEFAULT); - - if (size_string[0] != '\0') - text = g_strdup_printf (_("%s, Free space: %s"), size_string, fspace_string); - else - text = g_strdup_printf (_("Free space: %s"), fspace_string); - - /* cleanup */ + gchar *size_string = g_format_size_full (size, show_file_size_binary_format ? G_FORMAT_SIZE_IEC_UNITS : G_FORMAT_SIZE_DEFAULT); + temp_string = g_strdup_printf (_("Free space: %s"), size_string); + text_list = g_list_append (text_list, temp_string); g_free (size_string); - g_free (fspace_string); - } - else - { - text = thunar_list_model_get_statusbar_text_for_files (relevant_files, show_file_size_binary_format); } + g_list_free (relevant_files); } else if (selected_items->next == NULL) /* only one item selected */ @@ -3002,65 +2981,52 @@ thunar_list_model_get_statusbar_text (ThunarListModel *store, /* determine the content type of the file */ content_type = thunar_file_get_content_type (file); - if (show_filetype == TRUE) + if (show_display_name == TRUE) { - if (G_UNLIKELY (content_type != NULL && g_str_equal (content_type, "inode/symlink"))) - filetype_string = g_strdup ("broken link"); - else if (G_UNLIKELY (thunar_file_is_symlink (file))) - filetype_string = g_strdup_printf (_("link to %s"), thunar_file_get_symlink_target (file)); - else if (G_UNLIKELY (thunar_file_get_kind (file) == G_FILE_TYPE_SHORTCUT)) - filetype_string = g_strdup ("shortcut"); - else if (G_UNLIKELY (thunar_file_get_kind (file) == G_FILE_TYPE_MOUNTABLE)) - filetype_string = g_strdup ("mountable"); - else - { - gchar *description = g_content_type_get_description (content_type); - filetype_string = g_strdup_printf (_("%s"), description); - g_free (description); - } + temp_string = g_strdup_printf (_("\"%s\""), thunar_file_get_display_name (file)); + text_list = g_list_append (text_list, temp_string); } - if (show_display_name == TRUE) - display_name = g_strdup_printf (_("\"%s\":"), thunar_file_get_display_name (file)); - if (thunar_file_is_regular (file) || G_UNLIKELY (thunar_file_is_symlink (file))) { if (show_size == TRUE) { if (show_size_in_bytes == TRUE) - size_string = thunar_file_get_size_string_long (file, show_file_size_binary_format); + temp_string = thunar_file_get_size_string_long (file, show_file_size_binary_format); else - size_string = thunar_file_get_size_string_formatted (file, show_file_size_binary_format); - - /* I18N, first %s is the display name of the file, 2nd the file size, 3rd the content type */ - text = g_strdup_printf (_("%s %s %s"), display_name, size_string, filetype_string); - g_free (size_string); + temp_string = thunar_file_get_size_string_formatted (file, show_file_size_binary_format); + text_list = g_list_append (text_list, temp_string); } - else - text = g_strdup_printf (_("%s %s"), display_name, filetype_string); } - else - { - /* I18N, first %s is the display name of the file, second the content type */ - text = g_strdup_printf (_("%s %s"), display_name, filetype_string); - } - - if (show_display_name == TRUE) - g_free (display_name); if (show_filetype == TRUE) - g_free (filetype_string); + { + if (G_UNLIKELY (content_type != NULL && g_str_equal (content_type, "inode/symlink"))) + temp_string = g_strdup ("broken link"); + else if (G_UNLIKELY (thunar_file_is_symlink (file))) + temp_string = g_strdup_printf (_("link to %s"), thunar_file_get_symlink_target (file)); + else if (G_UNLIKELY (thunar_file_get_kind (file) == G_FILE_TYPE_SHORTCUT)) + temp_string = g_strdup ("shortcut"); + else if (G_UNLIKELY (thunar_file_get_kind (file) == G_FILE_TYPE_MOUNTABLE)) + temp_string = g_strdup ("mountable"); + else + { + gchar *description = g_content_type_get_description (content_type); + temp_string = g_strdup_printf (_("%s"), description); + g_free (description); + } + text_list = g_list_append (text_list, temp_string); + } /* append the original path (if any) */ original_path = thunar_file_get_original_path (file); if (G_UNLIKELY (original_path != NULL)) { /* append the original path to the statusbar text */ - display_name = g_filename_display_name (original_path); - s = g_strdup_printf ("%s, %s %s", text, _("Original Path:"), display_name); - g_free (display_name); - g_free (text); - text = s; + gchar *original_path_string = g_filename_display_name (original_path); + temp_string = g_strdup_printf ("%s %s", _("Original Path:"), original_path_string); + text_list = g_list_append (text_list, temp_string); + g_free (original_path_string); } else if (thunar_file_is_local (file) && thunar_file_is_regular (file) @@ -3069,42 +3035,38 @@ thunar_list_model_get_statusbar_text (ThunarListModel *store, /* check if the size should be visible in the statusbar, disabled by * default to avoid high i/o */ g_object_get (preferences, "misc-image-size-in-statusbar", &show_image_size, NULL); - if (show_image_size) { /* check if we can determine the dimension of this file (only for image files) */ - absolute_path = g_file_get_path (thunar_file_get_file (file)); - if (absolute_path != NULL - && gdk_pixbuf_get_file_info (absolute_path, &width, &height) != NULL) + gchar *file_path = g_file_get_path (thunar_file_get_file (file)); + if (file_path != NULL && gdk_pixbuf_get_file_info (file_path, &width, &height) != NULL) { /* append the image dimensions to the statusbar text */ - s = g_strdup_printf ("%s, %s %dx%d", text, _("Image Size:"), width, height); - g_free (text); - text = s; + temp_string = g_strdup_printf ("%s %dx%d", _("Image Size:"), width, height); + text_list = g_list_append (text_list, temp_string); } - g_free (absolute_path); + g_free (file_path); } } } else /* more than one item selected */ { + gchar *selected_string; /* build GList of files from selection */ for (lp = selected_items; lp != NULL; lp = lp->next) { gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &iter, lp->data); relevant_files = g_list_append (relevant_files, g_sequence_get (iter.user_data)); } - - s = thunar_list_model_get_statusbar_text_for_files (relevant_files, show_file_size_binary_format); - if (s[0] != '\0') - text = g_strdup_printf (_("Selection: %s"), s); - else - text = g_strdup (""); - g_free (s); + selected_string = thunar_list_model_get_statusbar_text_for_files (store, relevant_files, show_file_size_binary_format); + temp_string = g_strdup_printf (_("Selection: %s"), selected_string); + text_list = g_list_append (text_list, temp_string); g_list_free (relevant_files); + g_free (selected_string); } + text = thunar_util_strjoin_list (text_list, " | "); + g_list_free_full (text_list, g_free); g_object_unref (preferences); - return text; } diff --git a/thunar/thunar-util.c b/thunar/thunar-util.c index a17cb53fb..ad6d88dcf 100644 --- a/thunar/thunar-util.c +++ b/thunar/thunar-util.c @@ -766,3 +766,50 @@ thunar_util_is_a_search_query (const gchar *string) { return strncmp (string, SEARCH_PREFIX, strlen (SEARCH_PREFIX)) == 0; } + + + +/** + * thunar_util_strjoin_list: + * @data : list of strings which needs to be appended. + * @separator : text which needs to be added as a seperator + * + * Joins a number of strings together to form one long string, with the optional separator + * inserted between each of them. It skips all the NULL values passed in the list. The returned + * string should be freed with g_free(). + * Similar to g_strjoin(). + * + * The caller is responsible to free the returned text using g_free() when it is no longer needed + * + * Return value: the concatenated string + **/ +gchar* +thunar_util_strjoin_list (GList *string_list, + const gchar *separator) +{ + GList *lp; + gchar *joined_string = NULL; + gchar *temp_string; + + for (lp = string_list; lp != NULL; lp = lp->next) + { + if (xfce_str_is_empty ((gchar *) lp->data)) + continue; + + if (joined_string == NULL) + { + joined_string = g_strdup (lp->data); + } + else + { + temp_string = g_strjoin (separator, joined_string, lp->data, NULL); + g_free (joined_string); + joined_string = temp_string; + } + } + + if (joined_string == NULL) + return g_strdup (""); + else + return joined_string; +} diff --git a/thunar/thunar-util.h b/thunar/thunar-util.h index 87f4952ce..62325ce63 100644 --- a/thunar/thunar-util.h +++ b/thunar/thunar-util.h @@ -78,6 +78,8 @@ gchar* thunar_util_next_new_file_name (ThunarFile *dir, const gchar *file_name, ThunarNextFileNameMode name_mode); gboolean thunar_util_is_a_search_query (const gchar *string); +gchar* thunar_util_strjoin_list (GList *string_list, + const gchar *separator); extern const char *SEARCH_PREFIX; -- GitLab