Skip to content

Commit

Permalink
Fix desktop file ID issue
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeSlave committed Oct 13, 2015
1 parent 41660cd commit 00105b4
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions source/desktopfile.d
Original file line number Diff line number Diff line change
Expand Up @@ -459,17 +459,37 @@ public:
return localizedValue("Name", locale);
}

/**
* Desktop file ID
* Returns: desktop file id as described in $(LINK2 http://standards.freedesktop.org/desktop-entry-spec/latest/ape.html, Desktop File ID) or empty string if file does not have an ID.
version(OSX) {} version(Posix) {
/**
* See $(LINK2 http://standards.freedesktop.org/desktop-entry-spec/latest/ape.html, Desktop File ID)
* Returns: desktop file ID or empty string if file does not have an ID.
* Note: This function retrieves applications paths each time it's called. To avoid this issue use overload with argument.
*/
@trusted string id() const nothrow {
return id(applicationsPaths());
}
}

/**
* See $(LINK2 http://standards.freedesktop.org/desktop-entry-spec/latest/ape.html, Desktop File ID)
* Returns: desktop file ID or empty string if file does not have an ID.
*/
@trusted string id() const nothrow {
@trusted string id(Range)(Range appPaths) const nothrow if (isInputRange!Range && is(ElementType!Range : string))
{
try {
string absolute = fileName.absolutePath;
enum applications = "/applications/";
auto index = absolute.indexOf(applications);
if (index != -1) {
return absolute[index + applications.length..$].replace("/", "-");
foreach (path; appPaths) {
auto pathSplit = pathSplitter(path);
auto fileSplit = pathSplitter(absolute);

while (!pathSplit.empty && !fileSplit.empty && pathSplit.front == fileSplit.front) {
pathSplit.popFront();
fileSplit.popFront();
}

if (pathSplit.empty) {
return to!string(fileSplit.join("-"));
}
}
} catch(Exception e) {

Expand All @@ -484,11 +504,16 @@ public:
`[Desktop Entry]
Name=Program
Type=Application`;

auto appPaths = ["/usr/share/applications", "/usr/local/share/applications"];
auto df = new DesktopFile(iniLikeStringReader(contents), DesktopFile.ReadOptions.noOptions, "/usr/share/applications/de/example.desktop");
assert(df.id() == "de-example.desktop");
assert(df.id(appPaths) == "de-example.desktop");

df = new DesktopFile(iniLikeStringReader(contents), DesktopFile.ReadOptions.noOptions, "/usr/local/share/applications/example.desktop");
assert(df.id(appPaths) == "example.desktop");

df = new DesktopFile(iniLikeStringReader(contents), DesktopFile.ReadOptions.noOptions, "/etc/desktop");
assert(df.id().empty);
df = new DesktopFile(iniLikeStringReader(contents), DesktopFile.ReadOptions.noOptions, "/etc/desktop/example.desktop");
assert(df.id(appPaths).empty);
}

/**
Expand Down

0 comments on commit 00105b4

Please sign in to comment.