From 470d278c1638ec6c03bc36c603411bf34cd99127 Mon Sep 17 00:00:00 2001 From: Oliver Sauder Date: Wed, 25 Feb 2026 23:00:11 +0400 Subject: [PATCH] Precomputed label and checksum for file and text clipboard items This change improves the code in the following way: * when generating label only the label is cleaned up by new lines not the whole text * _checksum is only computed once per clipboard item and memorized * _label is only computed once per clipboard item and memorized --- libdiodon/file-clipboard-item.vala | 35 +++++++++++++++++------------- libdiodon/text-clipboard-item.vala | 21 ++++++++++-------- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/libdiodon/file-clipboard-item.vala b/libdiodon/file-clipboard-item.vala index 53f41c8..d72f90c 100644 --- a/libdiodon/file-clipboard-item.vala +++ b/libdiodon/file-clipboard-item.vala @@ -36,6 +36,8 @@ namespace Diodon */ private string _paths; private string? _origin; + private string _label; + private string _checksum; private ClipboardType _clipboard_type; private DateTime _date_copied; @@ -52,6 +54,7 @@ namespace Diodon _paths = data; _origin = origin; _date_copied = date_copied; + _checksum = Checksum.compute_for_string(ChecksumType.SHA1, _paths); // check if all paths are available string[] paths = convert_to_paths(_paths); @@ -61,6 +64,21 @@ namespace Diodon throw new FileError.NOENT("No such file or directory " + path); } } + + // label should not be longer than 50 + string home = Environment.get_home_dir(); + _label = ""; + foreach(string p in paths) { + if(_label.length > 0) { + _label += " "; + } + _label += p.replace(home, "~"); + + if (_label.char_count() > 50) { + _label = _label.substring(0, 50) + "..."; + break; + } + } } /** @@ -100,20 +118,7 @@ namespace Diodon */ public string get_label() { - string home = Environment.get_home_dir(); - - // label should not be longer than 50 letters - string label = _paths.replace("\n", " "); - - // replacing home dir with common known tilde - label = label.replace(home, "~"); - - if (label.char_count() > 50) { - long index_char = label.index_of_nth_char(50); - label = label.substring(0, index_char) + "..."; - } - - return label; + return _label; } /** @@ -196,7 +201,7 @@ namespace Diodon */ public string get_checksum() { - return Checksum.compute_for_string(ChecksumType.SHA1, _paths); + return _checksum; } /** diff --git a/libdiodon/text-clipboard-item.vala b/libdiodon/text-clipboard-item.vala index 800df2b..3526507 100644 --- a/libdiodon/text-clipboard-item.vala +++ b/libdiodon/text-clipboard-item.vala @@ -28,6 +28,8 @@ namespace Diodon { private string _text; private string? _origin; + private string _label; + private string _checksum; private ClipboardType _clipboard_type; private DateTime _date_copied; @@ -44,6 +46,14 @@ namespace Diodon _text = data; _origin = origin; _date_copied = date_copied; + _checksum = Checksum.compute_for_string(ChecksumType.SHA1, _text); + + // label should not be longer than 50 letters + _label = _text; + if (_label.char_count() > 50) { + _label = _label.substring(0, 50) + "..."; + } + _label = _label.replace("\n", " "); } /** @@ -83,14 +93,7 @@ namespace Diodon */ public string get_label() { - // label should not be longer than 50 letters - string label = _text.replace("\n", " "); - if (label.char_count() > 50) { - long index_char = label.index_of_nth_char(50); - label = label.substring(0, index_char) + "..."; - } - - return label; + return _label; } /** @@ -138,7 +141,7 @@ namespace Diodon */ public string get_checksum() { - return Checksum.compute_for_string(ChecksumType.SHA1, _text); + return _checksum; } /**