! Please note that this is a snapshot of our old Bugzilla server, which is read only since May 29, 2020. Please go to gitlab.xfce.org for our new server !
Add a new widget for filename input
Status:
RESOLVED: FIXED
Severity:
enhancement
Product:
Libxfce4ui
Component:
General

Comments

Description Reuben Green editbugs 2020-03-12 19:03:58 CET
Created attachment 9586 
patch to add new widget

I propose that we add a new widget to libxfce4ui which provides a version of the GtkEntry widget especially for entering filenames for renaming or creating files, with as-you-type checking for errors such as a filename being too long or containing directory separator characters. The attached patch implements this feature, and a demonstrator program for this new widget is attached to the next comment.

The widget displays an error if the entered filename is longer than the given maximum value or contains the directory separator. It warns the user if the given filename starts or ends with whitespace.

The reason for adding this feature comes from trying to fix bug #13720 in Thunar. While investigating this, I realised that there are several places in the Thunar code where the user is asked for a filename for a new file or a new name for an existing file, and these do not behave the same. Thus the best option seemed to be a new widget featuring as-you-type checking of the filename, and alexxcons suggested that this new widget should be in libxfce4ui to allow reuse. If this widget is added to libxfce4ui, then I will patch Thunar to use it.

Now this is the first time I have tried to implement a new GObject class, and I have done my best to get all of the magical GObject hocus-pocus right - I have had a careful look at existing code in libxfce4ui and elsewhere in Xfce, and done lots of googling and reading of documents. The patch compiles and runs without errors or warnings on my system. However, it is still likely that I have messed things up or done something dumb. If anyone who knows about this stuff has time to have a quick look at my code, that would be great!

Comments and criticisms are most welcome!
Comment 1 Reuben Green editbugs 2020-03-12 19:05:30 CET
Created attachment 9587 
demonstration program for above patch

Here is a demonstrator for the new widget. On my system it compiles with
gcc demonstrator.c `pkg-config --cflags --libs libxfce4ui-2`
Comment 2 alexxcons editbugs 2020-03-13 00:35:19 CET
Created attachment 9588 
formatting patch

Nice, thanks alot for the patch !

Here some remarks:
- I applied some formatting ... see first patch attached
- There are two build warnings to be fixed "comparison of integer expressions of different signedness"
- All methods which are public API should get documentation in the *.c file
--  Use the following in the comments: "* Return value: (transfer none): blabla"  in order to get rid of the build warning "xfce_filename_input_get_entry: return value: Missing (transfer) annotation"
-- (https://gi.readthedocs.io/en/latest/annotations/giannotations.html#memory-and-lifecycle-management)

- There is a "tests" folder in the project .. I did not try, though I guess you can put the code of the demonstrator there
- Attached another patch which keeps the public API a bit smaller by using properties  .. like that you can create the filename_input with:
g_object_new (XFCE_TYPE_FILENAME_INPUT,  "original-file-name", orig_name, "max-length", max_len, NULL);

.... for some reason I dont understand, yet, I cannot test my changes with your demonstrator .. please feel free to test my patches (I only know that it compiles) and merge them into your patch, if you think it is apropriate.
Comment 3 alexxcons editbugs 2020-03-13 00:42:42 CET
Created attachment 9589 
Adding properties for max-length and original-filename

... forgot to mention:
- I used G_PARAM_CONSTRUCT_ONLY, like that you dont need to check for overwrite of the original-filename

- Would be good to have "g_return_if_fail"   and  "g_return_val_if_fail" on each method to check XFCE_IS_FILENAME_INPUT()
Comment 4 Reuben Green editbugs 2020-03-13 11:40:17 CET
Thanks Alex! I'll make the changes you suggest and get a revised patch up.

One question: I initially used properties for max-length and original-filename, but I changed to not using them after reading somewhere that it's recommended only to use properties when users might want to listen for changes to them, as otherwise you get overhead from all the "changed" signals that they generate. Is that right? I cannot remember where I read it, but I think it was in an answer on some forum or other so possibly not very reliable.

Thanks!
Comment 5 alexxcons editbugs 2020-03-13 22:49:30 CET
(In reply to Reuben Green from comment #4)
> One question: I initially used properties for max-length and
> original-filename, but I changed to not using them after reading somewhere
> that it's recommended only to use properties when users might want to listen
> for changes to them, as otherwise you get overhead from all the "changed"
> signals that they generate. Is that right? I cannot remember where I read
> it, but I think it was in an answer on some forum or other so possibly not
> very reliable.
Afaik you actively need to signal "change" for property-subscribers on property change. If you dont, nothing will be sent .. though I could be wrong.
Anyway, our properties are only changed once, at creation, so I dont think it should matter here.
Comment 6 Reuben Green editbugs 2020-03-16 17:14:49 CET
Created attachment 9612 
patch to add new widget

Here is a revised patch incorporating all of alexxcons's suggestions. Hopefully I've managed to get the formatting right this time!

In particular I have added the new widget to the test program, so you can test it using that.
Comment 7 alexxcons editbugs 2020-03-17 00:21:12 CET
Thanks alot, looks very nice now !

> In particular I have added the new widget to the test program, so you can test it using that.
That worked out nicely for me, thanks ! :)

If you dont plan to do further modifications on it, I would push the new widget.
Comment 8 Reuben Green editbugs 2020-03-17 01:03:03 CET
No, I don't have any further plans, unless anyone else has any checks they want added.

Thanks!
Comment 9 Git Bot editbugs 2020-03-17 11:45:38 CET
Reuben Green referenced this bugreport in commit 8f0ad0c23947545abf249aac56124cde74cdeb0a

Add a widget for filename input (Bug #16542)

https://git.xfce.org/xfce/libxfce4ui/commit?id=8f0ad0c23947545abf249aac56124cde74cdeb0a
Comment 10 alexxcons editbugs 2020-03-17 11:50:32 CET
Ok, pushed to master.

I justsaw that it might help to have "Since: 4.16" on the doc of all methods and added it.

Thanks alot for your work !
Comment 11 Reuben Green editbugs 2020-03-17 11:54:05 CET
Thanks Alex! I'll start preparing patches for Thunar to use this.

Bug #16542

Reported by:
Reuben Green
Reported on: 2020-03-12
Last modified on: 2020-03-17

People

Assignee:
Xfce Bug Triage
CC List:
1 user

Version

Attachments

patch to add new widget (17.33 KB, patch)
2020-03-12 19:03 CET , Reuben Green
no flags
demonstration program for above patch (2.37 KB, text/x-csrc)
2020-03-12 19:05 CET , Reuben Green
no flags
formatting patch (7.48 KB, patch)
2020-03-13 00:35 CET , alexxcons
no flags
Adding properties for max-length and original-filename (6.71 KB, patch)
2020-03-13 00:42 CET , alexxcons
no flags
patch to add new widget (24.20 KB, patch)
2020-03-16 17:14 CET , Reuben Green
no flags

Additional information