[tepl] File: rework :short-name property implementation to fix a unit test
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tepl] File: rework :short-name property implementation to fix a unit test
- Date: Mon, 18 May 2020 17:22:00 +0000 (UTC)
commit 0d3322e725261c977ae5cbb8304094116cb8f53a
Author: Sébastien Wilmet <swilmet gnome org>
Date: Mon May 18 17:16:53 2020 +0200
File: rework :short-name property implementation to fix a unit test
tepl/tepl-buffer.c | 5 +-
tepl/tepl-close-confirm-dialog-single.c | 4 +-
tepl/tepl-file.c | 94 ++++++++++++++-------------------
tepl/tepl-file.h | 2 +-
testsuite/test-file.c | 5 +-
5 files changed, 51 insertions(+), 59 deletions(-)
---
diff --git a/tepl/tepl-buffer.c b/tepl/tepl-buffer.c
index 55806ce..22b3da6 100644
--- a/tepl/tepl-buffer.c
+++ b/tepl/tepl-buffer.c
@@ -553,7 +553,7 @@ gchar *
tepl_buffer_get_short_title (TeplBuffer *buffer)
{
TeplBufferPrivate *priv;
- const gchar *short_name;
+ gchar *short_name;
gchar *short_title;
g_return_val_if_fail (TEPL_IS_BUFFER (buffer), NULL);
@@ -565,10 +565,11 @@ tepl_buffer_get_short_title (TeplBuffer *buffer)
if (gtk_text_buffer_get_modified (GTK_TEXT_BUFFER (buffer)))
{
short_title = g_strconcat ("*", short_name, NULL);
+ g_free (short_name);
}
else
{
- short_title = g_strdup (short_name);
+ short_title = short_name;
}
return short_title;
diff --git a/tepl/tepl-close-confirm-dialog-single.c b/tepl/tepl-close-confirm-dialog-single.c
index f1daf53..7aeb0e7 100644
--- a/tepl/tepl-close-confirm-dialog-single.c
+++ b/tepl/tepl-close-confirm-dialog-single.c
@@ -99,7 +99,7 @@ create_dialog (GTask *task)
TeplBuffer *buffer;
TeplFile *file;
GFile *location;
- const gchar *file_short_name;
+ gchar *file_short_name;
GtkWidget *dialog;
tab = g_task_get_source_object (task);
@@ -116,6 +116,8 @@ create_dialog (GTask *task)
GTK_BUTTONS_NONE,
_("Save changes to file “%s” before closing?"),
file_short_name);
+ g_free (file_short_name);
+ file_short_name = NULL;
gtk_dialog_add_buttons (GTK_DIALOG (dialog),
_("Close _without Saving"), GTK_RESPONSE_CLOSE,
diff --git a/tepl/tepl-file.c b/tepl/tepl-file.c
index df8804d..a287efb 100644
--- a/tepl/tepl-file.c
+++ b/tepl/tepl-file.c
@@ -29,8 +29,9 @@ struct _TeplFilePrivate
GFile *location;
TeplNewlineType newline_type;
- gchar *short_name;
+ /* For the short-name. */
gint untitled_number;
+ gchar *display_name;
TeplMountOperationFactory mount_operation_factory;
gpointer mount_operation_userdata;
@@ -127,7 +128,7 @@ tepl_file_get_property (GObject *object,
break;
case PROP_SHORT_NAME:
- g_value_set_string (value, tepl_file_get_short_name (file));
+ g_value_take_string (value, tepl_file_get_short_name (file));
break;
default:
@@ -177,14 +178,14 @@ tepl_file_finalize (GObject *object)
{
TeplFile *file = TEPL_FILE (object);
- g_free (file->priv->short_name);
- g_free (file->priv->etag);
-
if (file->priv->untitled_number > 0)
{
release_untitled_number (file->priv->untitled_number);
}
+ g_free (file->priv->display_name);
+ g_free (file->priv->etag);
+
G_OBJECT_CLASS (tepl_file_parent_class)->finalize (object);
}
@@ -256,42 +257,20 @@ query_display_name_cb (GObject *source_object,
GFile *location = G_FILE (source_object);
TeplFile *file = TEPL_FILE (user_data);
GFileInfo *info;
- GError *error = NULL;
- info = g_file_query_info_finish (location, result, &error);
+ info = g_file_query_info_finish (location, result, NULL);
- if (error != NULL)
+ if (info != NULL)
{
- /* Ignore error, because there is no GError to report it. The
- * same error will probably occur when the user will load or
- * save the file, and in that case the FileLoader or FileSaver
- * can report a GError which can be displayed at an appropriate
- * place in the UI.
- *
- * Instead, use a fallback short-name.
- */
- g_clear_error (&error);
-
- g_free (file->priv->short_name);
- file->priv->short_name = _tepl_utils_get_fallback_basename_for_display (location);
- }
- else
- {
- g_free (file->priv->short_name);
- file->priv->short_name = g_strdup (g_file_info_get_display_name (info));
- }
+ g_free (file->priv->display_name);
+ file->priv->display_name = g_strdup (g_file_info_get_display_name (info));
- if (file->priv->untitled_number > 0)
- {
- release_untitled_number (file->priv->untitled_number);
- file->priv->untitled_number = 0;
+ g_object_unref (info);
}
g_object_notify_by_pspec (G_OBJECT (file), properties[PROP_SHORT_NAME]);
- g_clear_object (&info);
-
- /* Async operation finished */
+ /* Async operation finished. */
g_object_unref (file);
}
@@ -305,14 +284,17 @@ update_short_name (TeplFile *file)
file->priv->untitled_number = allocate_first_available_untitled_number ();
}
- g_free (file->priv->short_name);
- file->priv->short_name = g_strdup_printf (_("Untitled File %d"),
- file->priv->untitled_number);
-
g_object_notify_by_pspec (G_OBJECT (file), properties[PROP_SHORT_NAME]);
return;
}
+ if (file->priv->untitled_number > 0)
+ {
+ release_untitled_number (file->priv->untitled_number);
+ file->priv->untitled_number = 0;
+ }
+
+#if 0
/* Special case for URIs like "https://example.net". Querying the
* display-name for those URIs return "/", which can be confused with
* the local root directory.
@@ -320,18 +302,10 @@ update_short_name (TeplFile *file)
if (!g_file_has_uri_scheme (file->priv->location, "file") &&
!g_file_has_parent (file->priv->location, NULL))
{
- g_free (file->priv->short_name);
- file->priv->short_name = _tepl_utils_get_fallback_basename_for_display (file->priv->location);
-
- if (file->priv->untitled_number > 0)
- {
- release_untitled_number (file->priv->untitled_number);
- file->priv->untitled_number = 0;
- }
-
g_object_notify_by_pspec (G_OBJECT (file), properties[PROP_SHORT_NAME]);
return;
}
+#endif
g_file_query_info_async (file->priv->location,
G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
@@ -381,13 +355,15 @@ tepl_file_set_location (TeplFile *file,
if (g_set_object (&file->priv->location, location))
{
- g_object_notify_by_pspec (G_OBJECT (file), properties[PROP_LOCATION]);
-
/* The etag is for the old location. */
g_free (file->priv->etag);
file->priv->etag = NULL;
+ g_free (file->priv->display_name);
+ file->priv->display_name = NULL;
update_short_name (file);
+
+ g_object_notify_by_pspec (G_OBJECT (file), properties[PROP_LOCATION]);
}
}
@@ -413,18 +389,28 @@ tepl_file_get_location (TeplFile *file)
* Gets the @file short name. If the #TeplFile:location isn't %NULL,
* returns its display-name (see #G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME).
* Otherwise returns "Untitled File N", with N the Nth untitled file of the
- * application, starting at 1. When an untitled file is closed, its number is
- * released and can be used by a later untitled file.
+ * application, starting at 1. When an untitled file is closed or its location
+ * is set, its untitled number is released and can be used by a later file.
*
- * Returns: the @file short name.
- * Since: 1.0
+ * Returns: the @file short name. Free with g_free() when no longer needed.
+ * Since: 5.0
*/
-const gchar *
+gchar *
tepl_file_get_short_name (TeplFile *file)
{
g_return_val_if_fail (TEPL_IS_FILE (file), NULL);
- return file->priv->short_name;
+ if (file->priv->untitled_number > 0)
+ {
+ return g_strdup_printf (_("Untitled File %d"), file->priv->untitled_number);
+ }
+
+ if (file->priv->display_name != NULL)
+ {
+ return g_strdup (file->priv->display_name);
+ }
+
+ return _tepl_utils_get_fallback_basename_for_display (file->priv->location);
}
void
diff --git a/tepl/tepl-file.h b/tepl/tepl-file.h
index 6af87c1..f979ec2 100644
--- a/tepl/tepl-file.h
+++ b/tepl/tepl-file.h
@@ -95,7 +95,7 @@ void tepl_file_set_location (TeplFile *file,
GFile *location);
_TEPL_EXTERN
-const gchar * tepl_file_get_short_name (TeplFile *file);
+gchar * tepl_file_get_short_name (TeplFile *file);
_TEPL_EXTERN
TeplNewlineType tepl_file_get_newline_type (TeplFile *file);
diff --git a/testsuite/test-file.c b/testsuite/test-file.c
index edec01e..d0efcb1 100644
--- a/testsuite/test-file.c
+++ b/testsuite/test-file.c
@@ -11,14 +11,17 @@ check_short_name_is_untitled_file_number (TeplFile *file,
gint untitled_number)
{
gchar *expected_short_name;
+ gchar *received_short_name;
/* For the translation it needs to be the exact same string as in the
* TeplFile implementation, to be able to run the unit test with any
* locale.
*/
expected_short_name = g_strdup_printf (_("Untitled File %d"), untitled_number);
- g_assert_cmpstr (tepl_file_get_short_name (file), ==, expected_short_name);
+ received_short_name = tepl_file_get_short_name (file);
+ g_assert_cmpstr (received_short_name, ==, expected_short_name);
g_free (expected_short_name);
+ g_free (received_short_name);
}
static void
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]