Skip to content

Commit

Permalink
Code updates, add id member function
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeSlave committed Jun 14, 2015
1 parent 8a50c1c commit 832c3a9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ Parse and write .desktop file to new location:

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

Read basic information about desktop file:

dub run desktopfile:desktoputil -- read /usr/share/applications/kde4/kate.desktop

### Desktop test

Parses all .desktop files in system's applications paths (usually /usr/local/share/applicatons and /usr/share/applications) and on the user's Desktop.
Expand Down
28 changes: 21 additions & 7 deletions examples/desktoputil/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,41 @@ void main(string[] args)
return;
}

string inFile = args[2];
auto df = new DesktopFile(inFile, DesktopFile.ReadOptions.preserveComments);
string command = args[1];
string inFile = args[2];


if (command == "read") {
foreach(group; df.byGroup()) {
writefln("[%s]", group.name);
foreach(t; group.byKeyValue()) {
writefln("%s=%s", t.key, t.value);
}
auto df = new DesktopFile(inFile, DesktopFile.ReadOptions.preserveComments | DesktopFile.ReadOptions.firstGroupOnly);

writeln("Name: ", df.name());
writeln("GenericName: ", df.genericName());
writeln("Comment: ", df.comment());
writeln("Type: ", df.value("Type"));
writeln("Icon: ", df.iconName());
writeln("Desktop ID: ", df.id());

if (df.type() == DesktopFile.Type.Application) {
writeln("Exec: ", df.execString());
writeln("In terminal: ", df.terminal());
}
if (df.type() == DesktopFile.Type.Link) {
writeln("URL: ", df.url());
}
} else if (command == "exec") {
auto df = new DesktopFile(inFile, DesktopFile.ReadOptions.firstGroupOnly);
string[] urls = args[3..$];
writeln("Exec:", df.expandExecString(urls));
df.startApplication(urls);
} else if (command == "link") {
auto df = new DesktopFile(inFile, DesktopFile.ReadOptions.firstGroupOnly);
writeln("Link:", df.url());
df.startLink();
} else if (command == "start") {
auto df = new DesktopFile(inFile, DesktopFile.ReadOptions.firstGroupOnly);
df.start();
} else if (command == "write") {
auto df = new DesktopFile(inFile, DesktopFile.ReadOptions.preserveComments);
if (args.length > 3) {
string outFile = args[3];
df.saveToFile(outFile);
Expand Down
25 changes: 23 additions & 2 deletions source/desktopfile.d
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public:
* Throws:
* $(B IniLikeException) if error occured while parsing.
*/
@trusted this(Range)(Range byLine, ReadOptions options = ReadOptions.noOptions, string fileName = null) if(is(ElementType!Range == IniLikeLine))
@trusted this(Range)(Range byLine, ReadOptions options = ReadOptions.noOptions, string fileName = null) if(is(ElementType!Range : IniLikeLine))
{
super(byLine, options, fileName);
_desktopEntry = group("Desktop Entry");
Expand Down Expand Up @@ -247,6 +247,24 @@ public:
return localizedValue("Name", locale);
}

/**
* Desktop file ID
* Returns: desktop file id as described in $(LINK 2 http://standards.freedesktop.org/desktop-entry-spec/latest/ape.html, Desktop File ID) or empty string if file does not have an ID.
*/
@trusted string id() const nothrow {
try {
string absolute = fileName.absolutePath;
enum applications = "/applications/";
auto index = absolute.indexOf(applications);
if (index != -1) {
return absolute[index + applications.length..$].replace("/", "-");
}
} catch(Exception e) {

}
return null;
}

/**
* Generic name of the application, for example "Web Browser".
* Returns: The value associated with "GenericName" key.
Expand Down Expand Up @@ -554,10 +572,13 @@ public:
* Pid of started process.
* Throws:
* ProcessException on failure to start the process.
* Exception if desktop file does not define URL.
* See_Also: start
*/
@trusted Pid startLink() const {
return spawnProcess(["xdg-open", url()], null, Config.none);
string myurl = url();
enforce(myurl.length, "No URL to open");
return spawnProcess(["xdg-open", myurl], null, Config.none);
}

/**
Expand Down

0 comments on commit 832c3a9

Please sign in to comment.