How can I detect whether something is selected in Dolpin's right pane?

We don’t want to expose “selected” elements through dbus, this would be a security issue.
With it any other application would be able to snoop on the user activity.

You are missing reading the earlier conversation.

The question is what are trying to achieve ?

No, I’ve read the earlier conversation attentively.
I don’t need the list of selected items to be exposed to dbus, I understand your security concerns. I need to know the fact that something was selected (via dbus or by any other means).
Look, I develop service menu which calculates checksums for the files. If I select some files, the script, which is attached to the service menu, works properly. If I select some folders, it also works properly, it processes these folders recursively and generates checksums for all files inside these folders. Now what happens, if I call this script with empty selection? As you wrote previously, Dolphin returns current folder name in this case, that’s hardcoded. So my script receives current folder as its first positional parameter and starts generation of the checksums for this folder recursively which is not intended.
It would be good to disable this menu, if nothing is selected, but this also not possible, due to hardcoded MinNumberOfUrls=1 behavior, as we discussed before.
That’s why I need a reliable method, which permits execution of the script on non-empty selection and somehow prevents its execution, if selection is empty.

how are you evoking the service menu if not by right clicking on a file and passing the %f or %F param to your script?

if you have MimeType=inode/directory; in your service menu, it will appear in the context menu and pass that directory to the script with even if you just click on the blank area of an open view in dolphin (it selects for you the working directory of the view).

what you do with that is up to you.

i have a service menu that processes image files but it also sensitive to inode/directory; in case there is a folder of images… but the service menu has separate menu actions for what to do with a folder and what to do with a file

when the folder action selected, then the service menu passes %F/* and the first thing it does is parse the files in the folder for image files using

# take files passed by service menu and assign to an array
see=($@)

# find all items in directory with image mime type and strip off everything after the file name
for files in $(file -i ${see[@]} | grep image | sed -e "s/:.*$//"); do 

and if finds none it simply exits.

if i select the image action from the menu, then the service menu passes %f to the same script and it handles it like an image.

it was not obvious, to me at least, how i could pass the selection of both the folder contents %F/* and an image file %f with only a single service menu action.

As I told before, if the service menu desktop file has got MimeType=all/all in it, it will be shown always: if I right click on file, on group of files or on blank area (intentionally or accidentally). And I need this MimeType which comprise both files and folders, because my script should process files, folders or mix of files or folders.

I would welcome contribution for this.

I detailed a design here:

Code is there src/widgets/kfileitemactions.cpp · master · Frameworks / KIO · GitLab
It would need some documentation for the feature in addition.
This shouldn’t be a big task.

I don’t have the time to handle everybody asks but I would gladly review it.

I’d be glad to contribute, but I don’t know C++, only bash, QML & JS.

Until you learn more…

Can’t you understand what this does ?

    if (cfg.hasKey("X-KDE-MinNumberOfUrls")) {
        const int minNumber = cfg.readEntry("X-KDE-MinNumberOfUrls").toInt();
        if (urlList.count() < minNumber) {
            return false;
        }
    }

And cpp is strongly typed that means that your code editor with LSP can figure out the functions available for objects and have good completion.

No scary * pointers here, so the code could written the exact same way in JS.

Just saying, I wasn’t a cpp developer until I started contributing to KDE, and I learned because of it (and because I was learning Rust) and this was before LSP was a thing.

Well, I suppose the next code should be valid:

// If selection is empty, current working directory is returned
// To hide service menu in this case the next property is introduced
if (cfg.hasKey("X-KDE-DiscardCurrentWorkDir")) {
    const bool discardCWD = cfg.readEntry("X-KDE-DiscardCurrentWorkDir");
    if (discardCWD == true) {
        return false;
    }
}

But this code isn’t complete though. It lacks the check of CWD, i.e. if the first item of the list of URLs isn’t equal to CWD, the menu still will be hidden, if this property will be set in desktop file. So the condition should look something like this:

if (discardCWD == true && urlList[0] == variable_containing_CWD) {

But I don’t know how to properly access an array element in C++ and dunno which variable stores current working directory.

There isn’t a “current working directory” function in cpp, here we want the working directory that the user clicked in, so that won’t match the current working directory of the executable.

The current working directory, of the selection, needs to be passed-in to KFileItemActions::setItemListProperties or through KFileItemListProperties ideally.

That would mean adding a field, to src/core/kfileitemlistproperties.h · master · Frameworks / KIO · GitLab and accessors functions and correcting the calling code.
(grepping for KFileItemActions kde/src would give you some nice anwsers.
The design is now well layed-out.

Everything you’ve mentioned in your answer is way beyond my comprehension, sorry.

Everything. Really?

I don’t know your programming experience.
Still, it seems to me you are over-simplifying.

That’s fine if you feel that’s over your head.
That’s FOSS, contributors do what they want and can.

Well, “adding a field” and “accessors functions” say nothing to me, an in order to get some nice answers, I should at least know c++ syntax, which I’m not acquainted with. So this task definitely needs other level of expertise.

I recon I was using my own jargon.
Let me break down those.

I meant adding a member/property to an object.
Similarly to JS object.

I meant the pair setter/getter.

then you need to design your script to handle them differently using logic (if/then, case, etc)

when a folder is passed to the script it cycles thru the folder for files

and if a file (or selection of files) is passed to script, then processes each file in turn

if you want to handle the case where a folder is passed but had both files and folders with in it, then you need another nested layer of logic to detect and handle that event.

the simple trick you are looking for does not exist.

You are missing the essence of the problem. My script already properly handles files, folders and their mix. All working fine, no issues.
The problem is: if single folder is passed to the script, I cannot determine within my script whether it was passed as a single selected folder or nothing was selected at all and I’m presented with current workdir as a substitution.

the flaw in your question is in caring whether or not a selection was made in the current view or not, because as far as the system is concerned there is always something selected.

the current view in dolphin IS the selection… you just don’t see the selection highlight because you are inside of it… go up one directory (Alt+Up) and you will see the folder you were just viewing as highlighted… it is already selected.

from the system’s point of view those two situations are identical

you are just experiencing two different views of it, one from inside the folder and one from above… it matters not to the system which of those are on your screen.

You may call it as you wish: selected items, marked items or some other way. If you mark two folders, Dolphin says in its status bar: “2 Folders selected”, as @bigsilicon pointed earlier. If you clear selection, status bar shows the counter of files/folders contained in the current folder. So these situations aren’t identical, these 2 states clearly differ. And my task is to detect that difference within my script.
I found one way, it is working, but it’s cumbersome. If you have another viable proposals, you are welcome. If not–let’s stop debate about terms and get to the matter.

from bash or the dolphin service menu, i don’t see any way to tell what view dolphin is displaying on the screen, i can only tell what is selected and what is the working directory.

you would need to access something from inside of dolphin to detect what is shown on the screen… if you have found something that works then i would just run with that.