From d9af0b3b179509b681a942d6860b0d2e8c04ee68 Mon Sep 17 00:00:00 2001 From: FreeSlave Date: Thu, 17 Mar 2016 01:41:29 +0300 Subject: [PATCH] Improved showInFileManager for linux --- src/dlangui/platforms/sdl/sdlapp.d | 71 +++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 11 deletions(-) diff --git a/src/dlangui/platforms/sdl/sdlapp.d b/src/dlangui/platforms/sdl/sdlapp.d index 52147f4c..a8c19e80 100644 --- a/src/dlangui/platforms/sdl/sdlapp.d +++ b/src/dlangui/platforms/sdl/sdlapp.d @@ -1245,17 +1245,66 @@ class SDLPlatform : Platform { wait(pid); return true; } else { - // TODO: implement for POSIX - if (normalized.isFile) - normalized = normalized.dirName; - string exe = "xdg-open"; - string[] args; - args ~= exe; - args ~= normalized; - Log.d("Executing ", args); - auto pid = spawnProcess(args); - wait(pid); - return true; + import std.exception; + + /* + * Use xdg-mime to get default application for inode/directory. + * If application is known to be able to select the file, start it directly. + * Otherwise just open directory with xdg-open. + * + * So this relies on external tools like xdg-mime and xdg-open. + * To make implementation free from external tools we need to implement + * reading of mimeapps.list to find appropriate .desktop file, + * find it on the file system, parse its Exec value and run. + * That's basically what xdg-open is doing. + * + * We still will need to keep track of file managers capable of selecting file on opening, + * since there's no freedesktop specification on this feature. + */ + + static string[] getXdgOpen(string toOpen) + { + bool pathIsDir; + collectException(toOpen.isDir, pathIsDir); + if (!pathIsDir) { + toOpen = toOpen.dirName; + } + return ["xdg-open", toOpen]; + } + + string toOpen = normalized; + + auto p = pipe(); + spawnProcess(["xdg-mime", "query", "default", "inode/directory"], std.stdio.stdin, p.writeEnd); + auto fileManager = p.readEnd.readln().chomp(); + p.close(); + + if (fileManager.length) { + string[] arguments; + + switch(fileManager) { + //nautilus and nemo selects item if it's file + case "nautilus.desktop": + case "nautilus-classic.desktop": + arguments = ["nautilus", toOpen]; + break; + case "nemo.desktop": + arguments = ["nemo", toOpen]; + break; + //dolphin needs --select option + case "kde4-dolphin.desktop": + case "kde-dolphin.desktop": + arguments = ["dolphin", "--select", toOpen]; + break; + default: + arguments = getXdgOpen(toOpen); + break; + } + spawnProcess(arguments); + return true; + } else { //xdg-mime returned non-zero. + return false; + } } } catch (Exception e) { Log.e("showInFileManager -- exception while trying to open file browser");