Skip to content

Commit

Permalink
Update examples. Add localized keywords version
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeSlave committed Nov 8, 2015
1 parent 226eb35 commit 539559b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
71 changes: 69 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The library is crossplatform for the most part, though there's little sense to u

### Implemented features

**desktopfile** provides basic features like reading and running desktop files, and more:
**desktopfile** provides basic features like reading and executing desktop files, and more:

* [Exec](http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s06.html) value unquoting and unescaping. Expanding field codes.
* Can rewrite desktop files preserving all comments and the original order of groups.
Expand Down Expand Up @@ -48,6 +48,73 @@ Ddox:

dub test

## Brief

```d
import std.stdio;
import std.array;
import std.process;
import desktopfile;
string filePath = ...;
string[] arguments = ...;
try {
auto df = new DesktopFile(filePath);
string locale = environment.get("LC_CTYPE", environment.get("LC_ALL", environment.get("LANG"))); //Detect current locale.
string name = df.localizedName(locale); //Specific name of the application.
string genericName = df.localizedGenericName(locale); //Generic name of the application. Show it in menu under the specific name.
string comment = df.localizedComment(locale); //Show it as tooltip or description.
string iconName = df.iconName(); //Freedesktop icon name.
if (df.hidden()) {
//User uninstalled desktop file and it should be shown in menus.
}
string[] onlyShowIn = df.onlyShowIn().array; //If not empty, show this application only in listed desktop environments.
string[] notShowIn = df.notShowIn().array; //Don't show this application in listed desktop environments.
string[] mimeTypes = df.mimeTypes().array; //MIME types supported by application.
string[] categories = df.categories().array; //Menu entries where this application should be shown.
string[] keywords = df.keywords().array; //Keywords can be used to improve searching of the application.
foreach(action; df.byAction()) { //Supported actions.
string actionName = action.name();
}
if (df.type() == DesktopFile.Type.Application) {
//This is application
string commandLine = df.execString(); //Command line pattern used to start the application.
try {
df.startApplication(arguments); //Start application using given arguments. It will be automatically started in terminal emulator if required.
}
catch(ProcessException e) { //Failed to start the application.
stderr.writeln(e.msg);
}
catch(DesktopExecException e) { //Malformed command line pattern.
stderr.writeln(e.msg);
}
} else if (df.type() == DesktopFile.Type.Link) {
//This is link to file or web resource.
string url = df.url(); //URL to open
} else if (df.type() == DesktopFile.Type.Directory) {
//This is directory or menu section description.
} else {
//Type is not defined or unknown, e.g. KDE Service.
string type = df.value("Type"); //Retrieve value manually as string if you know how to deal with non-standard types.
}
}
catch (IniLikeException e) { //Parsing error - file is not desktop file or has errors.
stderr.writeln(e.msg);
}
```

## Examples

### Desktop util
Expand Down Expand Up @@ -88,7 +155,7 @@ Parses all .desktop files in system's applications paths (usually /usr/local/sha
Writes errors (if any) to stderr.
Use this example to check if the desktopfile library can parse all .desktop files on your system.

dub run desktopfile:desktoptest --build=release
dub run desktopfile:desktoptest

To print all directories examined by desktoptest to stdout, add --verbose flag:

Expand Down
1 change: 0 additions & 1 deletion examples/desktoputil/dub.selections.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"fileVersion": 1,
"versions": {
"standardpaths": "0.2.0",
"inilike": "0.3.0"
}
}
8 changes: 4 additions & 4 deletions examples/desktoputil/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void main(string[] args)
string locale = currentLocale();

if (command == "read") {
auto df = new DesktopFile(inFile, DesktopFile.ReadOptions.preserveComments | DesktopFile.ReadOptions.firstGroupOnly);
auto df = new DesktopFile(inFile);

writefln("Name: %s. Localized: %s", df.name(), df.localizedName(locale));
writefln("GenericName: %s. Localized: %s", df.genericName(), df.localizedGenericName(locale));
Expand Down Expand Up @@ -59,17 +59,17 @@ void main(string[] args)
}
} else {
string[] urls = args[3..$];
writeln("Exec:", df.expandExecString(urls));
writeln("Exec:", df.expandExecString(urls, locale));
df.startApplication(urls, locale);
}


} else if (command == "link") {
auto df = new DesktopFile(inFile, DesktopFile.ReadOptions.firstGroupOnly);
auto df = new DesktopFile(inFile);
writeln("Link:", df.url());
df.startLink();
} else if (command == "start") {
auto df = new DesktopFile(inFile, DesktopFile.ReadOptions.firstGroupOnly);
auto df = new DesktopFile(inFile);
df.start();
} else if (command == "write") {
auto df = new DesktopFile(inFile, DesktopFile.ReadOptions.preserveComments);
Expand Down
10 changes: 10 additions & 0 deletions source/desktopfile.d
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,14 @@ Type=Directory`;
return splitValues(value("Keywords"));
}

/**
* A list of localied strings which may be used in addition to other metadata to describe this entry.
* Returns: The range of multiple values associated with "Keywords" key in given locale.
*/
@safe auto localizedKeywords(string locale) const {
return splitValues(localizedValue("Keywords", locale));
}

/**
* Sets the list of values for the "Keywords" list.
*/
Expand Down Expand Up @@ -1246,6 +1254,7 @@ TryExec=doublecmd
Type=Application
Categories=Application;Utility;FileManager;
Keywords=folder;manager;disk;filesystem;operations;
Keywords[ru]=папка;директория;диск;файловый;менеджер;
Actions=OpenDirectory;NotPresented;Settings;NoName;
MimeType=inode/directory;application/x-directory;
NoDisplay=false
Expand Down Expand Up @@ -1292,6 +1301,7 @@ Name=Notspecified Action`;
assert(df.workingDirectory() == "/opt/doublecmd");
assert(df.type() == DesktopFile.Type.Application);
assert(equal(df.keywords(), ["folder", "manager", "disk", "filesystem", "operations"]));
assert(equal(df.localizedKeywords("ru_RU"), ["папка", "директория", "диск", "файловый", "менеджер"]));
assert(equal(df.categories(), ["Application", "Utility", "FileManager"]));
assert(equal(df.actions(), ["OpenDirectory", "NotPresented", "Settings", "NoName"]));
assert(equal(df.mimeTypes(), ["inode/directory", "application/x-directory"]));
Expand Down

0 comments on commit 539559b

Please sign in to comment.