From 9ae85b31a3953b47ac5f01f6492a2fac57d2d121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Meyer?= Date: Sun, 10 Nov 2019 13:41:46 +0100 Subject: [PATCH 3/6] In filenames, replace the user directory with a tilde Goals are (1) to make filenames shorter so that they are more likely to be displayed in full, notably in the taskbar; (2) to make filenames easier to read by removing unnecessary information. We only do this if _not_ run by the root user, to avoid confusing the user. This affects the window title and tooltips in the notebook. We don't use shorter filenames when printing because it might be confusing for the user if he uses several computers with distinct user names. --- mousepad/mousepad-document.c | 100 +++++++++++++++++++++++++++++++---- mousepad/mousepad-document.h | 2 + mousepad/mousepad-window.c | 6 +-- 3 files changed, 96 insertions(+), 12 deletions(-) diff --git a/mousepad/mousepad-document.c b/mousepad/mousepad-document.c index 7a3c5fa..400f16d 100644 --- a/mousepad/mousepad-document.c +++ b/mousepad/mousepad-document.c @@ -89,8 +89,12 @@ struct _MousepadDocumentPrivate GtkWidget *ebox; GtkWidget *label; - /* utf-8 valid document names */ + /* utf-8 valid document names + utf8_short_filename might hold the same pointer as utf8_filename, so + a check is needed to prevent a double free. + */ gchar *utf8_filename; + gchar *utf8_short_filename; gchar *utf8_basename; }; @@ -173,6 +177,7 @@ mousepad_document_init (MousepadDocument *document) /* initialize the variables */ document->priv->utf8_filename = NULL; + document->priv->utf8_short_filename = NULL; document->priv->utf8_basename = NULL; document->priv->label = NULL; @@ -215,14 +220,26 @@ mousepad_document_init (MousepadDocument *document) +static void +mousepad_document_free_filenames (MousepadDocument *document) +{ + g_free (document->priv->utf8_filename); + if (document->priv->utf8_short_filename != document->priv->utf8_filename) + { + g_free (document->priv->utf8_short_filename); + } + g_free (document->priv->utf8_basename); +} + + + static void mousepad_document_finalize (GObject *object) { MousepadDocument *document = MOUSEPAD_DOCUMENT (object); /* cleanup */ - g_free (document->priv->utf8_filename); - g_free (document->priv->utf8_basename); + mousepad_document_free_filenames (document); /* release the file */ g_object_unref (G_OBJECT (document->file)); @@ -353,11 +370,61 @@ mousepad_document_drag_data_received (GtkWidget *widget, +/* Tries to make a filename shorter by replacing the leading $HOME directory + with a tilde. Returns a newly allocated filename if we could shorten the + filename, NULL otherwise. + + We only do this if _not_ run by the root user, to prevent confusion. + */ +static char * +mousepad_shorten_filename (const char *name) +{ + const char *home; + size_t home_len, name_len; + + /* do nothing if run by the root user */ + if (geteuid () == 0) + { + return NULL; + } + home = g_get_home_dir (); + if (!home) + { + return NULL; + } + /* trim trailing '/' just in case */ + home_len = strlen (home); + for (;;) + { + if (home_len == 0) + { + return NULL; + } + if (home[home_len - 1] != '/') + { + break; + } + home_len--; + } + name_len = strlen (name); + + if (home_len < name_len && memcmp (home, name, home_len) == 0 && name[home_len] == '/') + { + char *rv = g_malloc (name_len - home_len + 2); + rv[0] = '~'; + memcpy (&rv[1], &name[home_len], name_len - home_len + 1); + return rv; + } + return NULL; +} + + + static void mousepad_document_filename_changed (MousepadDocument *document, const gchar *filename) { - gchar *utf8_filename, *utf8_basename; + gchar *utf8_filename, *utf8_short_filename, *utf8_basename; g_return_if_fail (MOUSEPAD_IS_DOCUMENT (document)); g_return_if_fail (filename != NULL); @@ -367,15 +434,20 @@ mousepad_document_filename_changed (MousepadDocument *document, if (G_LIKELY (utf8_filename)) { - /* create the display name */ + /* create the display shorter filename and name */ + utf8_short_filename = mousepad_shorten_filename (utf8_filename); + if (!utf8_short_filename) + { + utf8_short_filename = utf8_filename; + } utf8_basename = g_filename_display_basename (utf8_filename); /* remove the old names */ - g_free (document->priv->utf8_filename); - g_free (document->priv->utf8_basename); + mousepad_document_free_filenames (document); /* set the new names */ document->priv->utf8_filename = utf8_filename; + document->priv->utf8_short_filename = utf8_short_filename; document->priv->utf8_basename = utf8_basename; /* update the tab label and tooltip */ @@ -385,7 +457,7 @@ mousepad_document_filename_changed (MousepadDocument *document, gtk_label_set_text (GTK_LABEL (document->priv->label), utf8_basename); /* set the tab tooltip */ - gtk_widget_set_tooltip_text (document->priv->ebox, utf8_filename); + gtk_widget_set_tooltip_text (document->priv->ebox, utf8_short_filename); /* update label color */ mousepad_document_label_color (document); @@ -479,7 +551,7 @@ mousepad_document_get_tab_label (MousepadDocument *document) /* the ebox */ document->priv->ebox = g_object_new (GTK_TYPE_EVENT_BOX, "border-width", 2, "visible-window", FALSE, NULL); gtk_box_pack_start (GTK_BOX (hbox), document->priv->ebox, TRUE, TRUE, 0); - gtk_widget_set_tooltip_text (document->priv->ebox, document->priv->utf8_filename); + gtk_widget_set_tooltip_text (document->priv->ebox, document->priv->utf8_short_filename); gtk_widget_show (document->priv->ebox); /* create the label */ @@ -542,3 +614,13 @@ mousepad_document_get_filename (MousepadDocument *document) return document->priv->utf8_filename; } + + + +const gchar * +mousepad_document_get_short_filename (MousepadDocument *document) +{ + g_return_val_if_fail (MOUSEPAD_IS_DOCUMENT (document), NULL); + + return document->priv->utf8_short_filename; +} diff --git a/mousepad/mousepad-document.h b/mousepad/mousepad-document.h index e936f3b..4465456 100644 --- a/mousepad/mousepad-document.h +++ b/mousepad/mousepad-document.h @@ -73,6 +73,8 @@ const gchar *mousepad_document_get_basename (MousepadDocument *document); const gchar *mousepad_document_get_filename (MousepadDocument *document); +const gchar *mousepad_document_get_short_filename (MousepadDocument *document); + gboolean mousepad_document_get_word_wrap (MousepadDocument *document); G_END_DECLS diff --git a/mousepad/mousepad-window.c b/mousepad/mousepad-window.c index 79acbe7..7fff0bc 100644 --- a/mousepad/mousepad-window.c +++ b/mousepad/mousepad-window.c @@ -1717,8 +1717,8 @@ mousepad_window_set_title (MousepadWindow *window) show_full_path = MOUSEPAD_SETTING_GET_BOOLEAN (PATH_IN_TITLE); /* name we display in the title */ - if (G_UNLIKELY (show_full_path && mousepad_document_get_filename (document))) - title = mousepad_document_get_filename (document); + if (G_UNLIKELY (show_full_path && mousepad_document_get_short_filename (document))) + title = mousepad_document_get_short_filename (document); else title = mousepad_document_get_basename (document); @@ -2797,7 +2797,7 @@ mousepad_window_update_gomenu_idle (gpointer user_data) /* get the name and file name */ title = mousepad_document_get_basename (document); - tooltip = mousepad_document_get_filename (document); + tooltip = mousepad_document_get_short_filename (document); /* create the radio action */ radio_action = gtk_radio_action_new (name, title, tooltip, NULL, n); -- 2.21.0