Recent

Author Topic: OpenDocument doesn't open directory, only file  (Read 2872 times)

stem

  • Jr. Member
  • **
  • Posts: 88
OpenDocument doesn't open directory, only file
« on: November 15, 2021, 09:59:02 pm »
Hi,

the function OpenDocument quits with an error when the parameter is a directory but it works with a file.

Lazarus 2.0.10
FPC: 3.2.0
x86_64-linux-gtk2
Linux Mint 19.3 Xfce

DirectoryExists returns true.

What am I doing wrong? Thank you.


stem

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1122
  • Professional amateur ;-P
Re: OpenDocument doesn't open directory, only file
« Reply #1 on: November 15, 2021, 10:40:18 pm »
Hey Stem,

the function OpenDocument quits with an error when the parameter is a directory but it works with a file.

I'm gonna be a bit cheeky and a lot sarcastic and say: The solution is in the name mate...
The function's name is OpenDocument, emphasis on Document, not Folder.

That function is created to trigger the Operating System's default app to deal with a certain document, like when you double click on an *.xls file and the File Browser opens Microsoft Excel with the document inside. And it's meant to be used from any platform.

Tell me, what is the default application, in ANY OS, that deals with a folder?

Now if your goal is to trigger the system's default file browser, then I'll have to investigate how you do that, cuz it's not the purview of the OpenDocument function.

All joking and sarcasm aside, I sincerely hope this helps in any way or shape :)

Cheers,
Gus

Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: OpenDocument doesn't open directory, only file
« Reply #2 on: November 15, 2021, 11:24:04 pm »

What am I doing wrong? Thank you.


stem

Hi!

If you use a beer bottle instead of a hammer - 
there might be some pieces of broken glass.

Winni

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1122
  • Professional amateur ;-P
Re: OpenDocument doesn't open directory, only file
« Reply #3 on: November 15, 2021, 11:24:22 pm »
Hey Stem,

After Googling a bit more about the subject and looking at some of the documentation of OpenDocument, it seems that the only OS where it doesn't open the default file browser is on Linux.

Looks like it will open the default file manager under Windows and macOS.

I confirmed that from the source code:
Code: Pascal  [Select][+][-]
  1. If not FileExistsUTF8(aPath) then exit(False);
This code is on a unix only file. I would have to chase the Windows and macOS versions of the same file to confirm that this test is performed differently.

So, now, there should be egg on my face.  :-[

Nonetheless, I don't really have a solution for opening the default file manager under Linux.

The only thing I can think of is that you can use the xdg-open if it's installed.

I've tested the command: xdg-open /home/my_username on my Ubuntu 21.10 64b and it opened the file manager with my Home folder.

Hope this gives your more information than my previous attempt at helping :)

On a side note: Does anyone think this should be reported to the Devs ? Why does it bail if it's not a file?

Cheers,
Gus
« Last Edit: November 15, 2021, 11:26:11 pm by Gustavo 'Gus' Carreno »
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: OpenDocument doesn't open directory, only file
« Reply #4 on: November 15, 2021, 11:39:15 pm »
I'm not really sure if this should be considered to be a bug.
And if so, what part would be the bug: Windows openening the filemanager, or *nix not doing so.

Personally I would think that the behaviour is undefined if the parameter is not a valid document name.

The easiest fix would of course be to let the function retrun False if the suplied APath in fact is a directory (but then Windows users will complain about backwards incompatibility).

If you can come up with a reliable implementation for *nix, I would say that it would be welcome as a patch though.

Bart

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2023
  • Former Delphi 1-7, 10.2 user
Re: OpenDocument doesn't open directory, only file
« Reply #5 on: November 16, 2021, 01:13:43 am »
If you can come up with a reliable implementation for *nix, I would say that it would be welcome as a patch though.

My FreeBSD systems have no file manager, yep, none at all (I use ye olde twm, not Gnome, KDE et al). So, possible? Yeah, nah.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1122
  • Professional amateur ;-P
Re: OpenDocument doesn't open directory, only file
« Reply #6 on: November 16, 2021, 06:05:43 am »
Hey Bart,

I'm not really sure if this should be considered to be a bug.
And if so, what part would be the bug: Windows openening the filemanager, or *nix not doing so.

If the documentation is correct, and I'm not saying it is or it ain't, cuz I haven't done the deep dive on the source, then I'm guessing that if 2 OSs have it, so why not the 3rd?
I think it's a rather reasonable question to ask, no?

If you can come up with a reliable implementation for *nix, I would say that it would be welcome as a patch though.

Well, I don't really see it that hard:
Code: Pascal  [Select][+][-]
  1. if (not FileExistsUTF8(aPath)) and (not DirectoryExistsUTF8(aPath)) then exit(False);

And that would make it NOT exit in case you pass a valid folder.

Now @trev actually brings quite a valid point: What if the *nix does not implement the xdg-open, nor has a known file manage.
Well, my dear @trev, then it just returns from the function with a value of False and no harm done, since the issue is with the OS and not with the FPC implementation.

What do you all think about that?

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2023
  • Former Delphi 1-7, 10.2 user
Re: OpenDocument doesn't open directory, only file
« Reply #7 on: November 16, 2021, 07:30:30 am »
What do you all think about that?

I thought the Quixotic challenge was to come up with a way to make *nix systems "work reliably" like Windows rather than returning anything be it an an error or an elegant false ;-) Anyway, that's that end to which my comment was directed.

stem

  • Jr. Member
  • **
  • Posts: 88
Re: OpenDocument doesn't open directory, only file
« Reply #8 on: November 16, 2021, 09:30:52 am »
Thank you for your answers.

I was only wondering why "opening" directories with OpenDocument works
with Windows - and not with Linux and MacOS. So in my humble opinion one
should at least leave a note in the documentation that this "feature" is not
available in all platforms.

I'm planning to use $IFDEF in my application to treat the operating systems
differently concerning the use of OpenDocument.


stem

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2023
  • Former Delphi 1-7, 10.2 user
Re: OpenDocument doesn't open directory, only file
« Reply #9 on: November 16, 2021, 11:11:56 am »
I was only wondering why "opening" directories with OpenDocument works
with Windows - and not with Linux and MacOS.

It works perfectly with macOS, I just tried it (ie opening a directory which calls up Finder).

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: OpenDocument doesn't open directory, only file
« Reply #10 on: November 16, 2021, 11:55:24 am »
I was only wondering why "opening" directories with OpenDocument works
with Windows - and not with Linux and MacOS. So in my humble opinion one
should at least leave a note in the documentation that this "feature" is not
available in all platforms.

Generally speaking that will depend on at least one software layer provided by the desktop environment, i.e. in the case of Linux the Portland Group xdg stuff. Since the implementation of that varies (by design, since the important thing is standardisation of the command line) on every desktop and distro, it's not really the Lazarus project's job to document it.

This is also a point of contention, since a couple of releases ago FileExists() stopped matching directories (I was one of those who reacted to that with a measure of irritation). So broadly speaking. "FileExists()" and "OpenDocument()" now exclude directories by design, even if in some cases they might possibly include them by mistake.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

stem

  • Jr. Member
  • **
  • Posts: 88
Re: OpenDocument doesn't open directory, only file
« Reply #11 on: November 17, 2021, 07:18:34 pm »
I was only wondering why "opening" directories with OpenDocument works
with Windows - and not with Linux and MacOS.

It works perfectly with macOS, I just tried it (ie opening a directory which calls up Finder).

Unfortunately not with my system, SysErrorMessage(Getlastoserror()) reports
"no such file or directory".

I'm trying to open the directory "/Users/stem/.config/myapp/". It works with
the file "/Users/stem/.config/myapp/myconfigfile.txt".

MacOS 12.0.1
Lazarus 2.0.12
FPC 3.2.0

Edit: DirectoryExists returns true.


stem
« Last Edit: November 17, 2021, 08:05:50 pm by stem »

 

TinyPortal © 2005-2018