Skip to content

Commit

Permalink
Update determineTerminalEmulator. Add convenient functions to get app…
Browse files Browse the repository at this point in the history
…lications paths on freedesktop
  • Loading branch information
FreeSlave committed Aug 14, 2015
1 parent 832c3a9 commit fdca71d
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 39 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ __dummy.html
bin/
lib/
docs/
dub.selections.json
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ Ddox:
### Desktop util

Utility that can parse, execute and rewrites .desktop files.
This will start vlc with the first parameter set to ~/Music:
This will start vlc with the first parameter set to $HOME/Music:

dub run desktopfile:desktoputil -- exec /usr/share/applications/vlc.desktop ~/Music
dub run desktopfile:desktoputil -- exec /usr/share/applications/vlc.desktop $HOME/Music

Should start command line application in terminal emulator:

Expand All @@ -50,7 +50,7 @@ Starts .desktop file defined executable or opens link:

Parse and write .desktop file to new location:

dub run desktopfile:desktoputil -- write /usr/share/applications/vlc.desktop ~/Desktop/vlc.desktop
dub run desktopfile:desktoputil -- write /usr/share/applications/vlc.desktop $HOME/Desktop/vlc.desktop

Read basic information about desktop file:

Expand Down
4 changes: 3 additions & 1 deletion dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
"name": "desktopfile",
"description": "Desktop Entry Specification implementation",
"license" : "BSL-1.0",
"copyright": "Copyright © 2015, Roman Chistokhodov",
"authors": ["Roman Chistokhodov"],
"dependencies": {
"inilike": "~>0.1.1"
"inilike": "~>0.2.0",
"standardpaths": "~>0.2.0"
},
"targetName" : "desktopfile",
"targetPath" : "lib",
Expand Down
7 changes: 7 additions & 0 deletions dub.selections.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"fileVersion": 1,
"versions": {
"standardpaths": "0.2.0",
"inilike": "0.2.0"
}
}
3 changes: 1 addition & 2 deletions examples/desktoptest/dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"copyright": "Copyright © 2015, freeslave",
"authors": ["freeslave"],
"dependencies": {
"desktopfile" : "*",
"standardpaths" : "~>0.1.2"
"desktopfile" : "*"
},
"targetPath" : "bin",
"targetType" : "executable",
Expand Down
7 changes: 7 additions & 0 deletions examples/desktoptest/dub.selections.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"fileVersion": 1,
"versions": {
"standardpaths": "0.2.0",
"inilike": "0.2.0"
}
}
7 changes: 5 additions & 2 deletions examples/desktoptest/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import desktopfile;

string[] desktopDirs()
{
return standardPaths(StandardPath.Applications) ~ writablePath(StandardPath.Desktop);
return applicationsPaths() ~ writablePath(StandardPath.Desktop);
}

void main(string[] args)
Expand All @@ -24,6 +24,9 @@ void main(string[] args)
catch(IniLikeException e) {
stderr.writefln("Error reading %s: at %s: %s", entry, e.lineNumber, e.msg);
}
catch(Exception e) {
stderr.writefln("Error reading %s: %s", entry, e.msg);
}
}
}
}
}
7 changes: 7 additions & 0 deletions examples/desktoputil/dub.selections.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"fileVersion": 1,
"versions": {
"standardpaths": "0.2.0",
"inilike": "0.2.0"
}
}
2 changes: 1 addition & 1 deletion examples/desktoputil/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ void main(string[] args)
} else {
writefln("unknown command '%s'", command);
}
}
}
68 changes: 39 additions & 29 deletions source/desktopfile.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Reading, writing and executing .desktop file
* Authors:
* $(LINK2 https://github.com/MyLittleRobo, Roman Chistokhodov).
* Copyright:
* Roman Chistokhodov, 2015
* License:
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
* See_Also:
Expand All @@ -10,6 +12,8 @@

module desktopfile;

import standardpaths;

public import inilike;

private {
Expand All @@ -29,28 +33,34 @@ private {
static if( __VERSION__ < 2066 ) enum nogc = 1;
}

version(Posix)
/**
* Applications paths based on data paths.
* This function is available on all platforms, but requires dataPaths argument (e.g. C:\ProgramData\KDE\share on Windows)
* Returns: Array of paths, based on dataPaths with "applications" directory appended.
*/
@trusted string[] applicationsPaths(in string[] dataPaths) nothrow {
return dataPaths.map!(p => buildPath(p, "applications")).array;
}

version(OSX) {}
else version(Posix)
{
private bool isExecutable(string filePath) nothrow @trusted {
import core.sys.posix.unistd;
return access(toStringz(filePath), X_OK) == 0;
/**
* ditto, but returns paths based on known data paths. It's practically the same as standardPaths(StandardPath.applications).
* This function is defined only on freedesktop systems to avoid confusion with other systems that have data paths not compatible with Desktop Entry Spec.
*/
@trusted string[] applicationsPaths() nothrow {
return standardPaths(StandardPath.applications);
}

/**
* Checks if the program exists and is executable.
* If the programPath is not an absolute path, the file is looked up in the $PATH environment variable.
* This function is defined only on Posix.
*/
bool checkTryExec(string programPath) @trusted {
if (programPath.isAbsolute()) {
return isExecutable(programPath);
}

foreach(path; environment.get("PATH").splitter(':')) {
if (isExecutable(buildPath(path, programPath))) {
return true;
}
}
return false;
* Path where .desktop files can be stored without requiring of root privileges.
* It's practically the same as writablePath(StandardPath.applications).
* This function is defined only on freedesktop systems to avoid confusion with other systems that have data paths not compatible with Desktop Entry Spec.
* Note: it does not check if returned path exists and appears to be directory.
*/
@trusted string writableApplicationPath() nothrow {
return writablePath(StandardPath.applications);
}
}

Expand Down Expand Up @@ -79,27 +89,27 @@ version(Posix)

/**
* Get terminal emulator.
* First, it probes $(B TERM) environment variable. If not found, checks if /usr/bin/x-terminal-emulator exists on Linux and use it on success.
* $(I xterm) is used by default, if could not determine other terminal emulator.
* It probes various alternatives in this order: x-terminal-emulator (Linux-only), xdg-terminal (Linux-only), TERM (environment variable).
* If all guesses failed, it uses xterm as fallback.
* Returns: Terminal emulator command name.
*/
string determineTerminalEmulator() nothrow @trusted
{
string term;
collectException(environment.get("TERM"), term);
version(linux) {
if (term.empty) {
string debianTerm = "/usr/bin/x-terminal-emulator";
if (debianTerm.isExecutable()) {
term = debianTerm;
}
term = findExecutable("x-terminal-emulator");
}
if (term.empty) {
term = findExecutable("xdg-terminal");
}
}

if (term.empty) {
collectException(environment.get("TERM"), term);
}
if (term.empty) {
term = "xterm";
}

return term;
}

Expand Down Expand Up @@ -506,7 +516,7 @@ public:
toReturn ~= iconStr;
}
} else if (token == "%c") {
toReturn ~= localizedValue("Name");
toReturn ~= localizedValue("Name", currentLocale());
} else if (token == "%k") {
toReturn ~= fileName();
} else if (token == "%d" || token == "%D" || token == "%n" || token == "%N" || token == "%m" || token == "%v") {
Expand Down

0 comments on commit fdca71d

Please sign in to comment.