DesktopFile

Represents .desktop file.

Constructors

this
this(string fileName, DesktopReadOptions options)

Reads desktop file from file.

this
this(IniLikeReader reader, DesktopReadOptions options, string fileName)

Reads desktop file from IniLikeReader, e.g. acquired from iniLikeFileReader or iniLikeStringReader.

this
this(IniLikeReader reader, string fileName, DesktopReadOptions options)

Reads desktop file from IniLikeReader, e.g. acquired from iniLikeFileReader or iniLikeStringReader.

this
this()

Constructs DesktopFile with "Desktop Entry" group and Version set to 1.1

Alias This

desktopEntry

This alias allows to call functions related to "Desktop Entry" group without need to call desktopEntry explicitly.

Members

Aliases

Type
alias Type = DesktopEntry.Type

Alias for backward compatibility.

Enums

ActionGroupPolicy
enum ActionGroupPolicy

Policy about reading Desktop Action groups.

ExtensionGroupPolicy
enum ExtensionGroupPolicy

Policy about reading extension groups (those start with 'X-').

UnknownGroupPolicy
enum UnknownGroupPolicy

Policy about reading groups with names which meaning is unknown, i.e. it's not extension nor Desktop Action.

Functions

action
inout(DesktopAction) action(string actionName)

Get additional application action by name.

byAction
auto byAction()

Iterating over existing actions.

createGroupByName
IniLikeGroup createGroupByName(string groupName)
Undocumented in source. Be warned that the author may not have intended to support it.
desktopEntry
inout(DesktopEntry) desktopEntry()
expandExecValue
string[] expandExecValue(const(string)[] urls, string locale)

Expand "Exec" value into the array of command line arguments to use to start the program. It applies unquoting and unescaping.

id
string id()

See Desktop File ID

id
string id(Range appPaths)

See Desktop File ID

removeGroup
bool removeGroup(string groupName)

Removes group by name. You can't remove "Desktop Entry" group with this function.

start
void start()

Starts application or open link depending on desktop entry type.

startApplication
void startApplication(string url, string locale, const(string)[] terminalCommand)

Starts the application associated with this .desktop file using url as command line params.

startApplication
void startApplication(const(string)[] urls, string locale, const(string)[] terminalCommand)

Starts the application associated with this .desktop file using urls as command line params.

startLink
void startLink()

Opens url defined in .desktop file using xdg-open. Note: This function does not check if the type of desktop file is Link. It relies only on "URL" value.

type
Type type()

Type of desktop entry.

type
Type type(Type t)
Undocumented in source. Be warned that the author may not have intended to support it.

Static functions

isActionName
bool isActionName(string groupName)

Check if groupName is name of Desktop Action group.

joinValues
string joinValues(Range values)

Join range of multiple values into a string using semicolon as separator. Adds trailing semicolon.

splitValues
auto splitValues(string values)

Some keys can have multiple values, separated by semicolon. This function helps to parse such kind of strings into the range.

Structs

DesktopReadOptions
struct DesktopReadOptions

Options to manage desktop file reading

Examples

1     import std.file;
2     string desktopFileContents =
3 `[Desktop Entry]
4 # Comment
5 Name=Double Commander
6 Name[ru]=Двухпанельный коммандер
7 GenericName=File manager
8 GenericName[ru]=Файловый менеджер
9 Comment=Double Commander is a cross platform open source file manager with two panels side by side.
10 Comment[ru]=Double Commander - кроссплатформенный файловый менеджер.
11 Terminal=false
12 Icon=doublecmd
13 Icon[ru]=doublecmd_ru
14 Exec=doublecmd %f
15 TryExec=doublecmd
16 Type=Application
17 Categories=Application;Utility;FileManager;
18 Keywords=folder;manager;disk;filesystem;operations;
19 Keywords[ru]=папка;директория;диск;файловый;менеджер;
20 Actions=OpenDirectory;NotPresented;Settings;X-NoName;
21 MimeType=inode/directory;application/x-directory;
22 NoDisplay=false
23 Hidden=false
24 StartupNotify=true
25 DBusActivatable=true
26 Path=/opt/doublecmd
27 OnlyShowIn=GNOME;XFCE;LXDE;
28 NotShowIn=KDE;
29 
30 [Desktop Action OpenDirectory]
31 Name=Open directory
32 Name[ru]=Открыть папку
33 Icon=open
34 Exec=doublecmd %u
35 
36 [X-NoName]
37 Icon=folder
38 
39 [Desktop Action Settings]
40 Name=Settings
41 Name[ru]=Настройки
42 Icon=edit
43 Exec=doublecmd settings
44 
45 [Desktop Action Notspecified]
46 Name=Notspecified Action`;
47 
48     auto df = new DesktopFile(iniLikeStringReader(desktopFileContents), "doublecmd.desktop");
49     assert(df.desktopEntry().groupName() == "Desktop Entry");
50     assert(df.fileName() == "doublecmd.desktop");
51     assert(df.displayName() == "Double Commander");
52     assert(df.localizedDisplayName("ru_RU") == "Двухпанельный коммандер");
53     assert(df.genericName() == "File manager");
54     assert(df.localizedGenericName("ru_RU") == "Файловый менеджер");
55     assert(df.comment() == "Double Commander is a cross platform open source file manager with two panels side by side.");
56     assert(df.localizedComment("ru_RU") == "Double Commander - кроссплатформенный файловый менеджер.");
57     assert(df.iconName() == "doublecmd");
58     assert(df.localizedIconName("ru_RU") == "doublecmd_ru");
59     assert(df.tryExecValue() == "doublecmd");
60     assert(!df.terminal());
61     assert(!df.noDisplay());
62     assert(!df.hidden());
63     assert(df.startupNotify());
64     assert(df.dbusActivable());
65     assert(df.workingDirectory() == "/opt/doublecmd");
66     assert(df.type() == DesktopFile.Type.Application);
67     assert(equal(df.keywords(), ["folder", "manager", "disk", "filesystem", "operations"]));
68     assert(equal(df.localizedKeywords("ru_RU"), ["папка", "директория", "диск", "файловый", "менеджер"]));
69     assert(equal(df.categories(), ["Application", "Utility", "FileManager"]));
70     assert(equal(df.actions(), ["OpenDirectory", "NotPresented", "Settings", "X-NoName"]));
71     assert(equal(df.mimeTypes(), ["inode/directory", "application/x-directory"]));
72     assert(equal(df.onlyShowIn(), ["GNOME", "XFCE", "LXDE"]));
73     assert(equal(df.notShowIn(), ["KDE"]));
74     assert(df.group("X-NoName") !is null);
75 
76     assert(equal(df.byAction().map!(desktopAction =>
77     tuple(desktopAction.displayName(), desktopAction.localizedDisplayName("ru"), desktopAction.iconName(), desktopAction.execValue())),
78                  [tuple("Open directory", "Открыть папку", "open", "doublecmd %u"), tuple("Settings", "Настройки", "edit", "doublecmd settings")]));
79 
80     DesktopAction desktopAction = df.action("OpenDirectory");
81     assert(desktopAction !is null);
82     assert(desktopAction.expandExecValue(["path/to/file"]) == ["doublecmd", "path/to/file"]);
83 
84     assert(df.action("NotPresented") is null);
85     assert(df.action("Notspecified") is null);
86     assert(df.action("X-NoName") is null);
87     assert(df.action("Settings") !is null);
88 
89     assert(df.saveToString() == desktopFileContents);
90 
91     assert(df.contains("Icon"));
92     df.removeEntry("Icon");
93     assert(!df.contains("Icon"));
94     df.setEscapedValue("Icon", "files");
95     assert(df.contains("Icon"));
96 
97     string contents =
98 `# First comment
99 [Desktop Entry]
100 Key=Value
101 # Comment in group`;
102 
103     df = new DesktopFile(iniLikeStringReader(contents), "test.desktop");
104     assert(df.fileName() == "test.desktop");
105     df.removeGroup("Desktop Entry");
106     assert(df.group("Desktop Entry") !is null);
107     assert(df.desktopEntry() !is null);
108 
109     contents =
110 `[X-SomeGroup]
111 Key=Value`;
112 
113     auto thrown = collectException!IniLikeReadException(new DesktopFile(iniLikeStringReader(contents)));
114     assert(thrown !is null);
115     assert(thrown.lineNumber == 0);
116 
117     df = new DesktopFile();
118     df.desktopEntry().setUnescapedValue("$Invalid", "Valid value", IniLikeGroup.InvalidKeyPolicy.save);
119     assert(df.desktopEntry().escapedValue("$Invalid") == "Valid value");
120     df.desktopEntry().setUnescapedValue("Another$Invalid", "Valid value", IniLikeGroup.InvalidKeyPolicy.skip);
121     assert(df.desktopEntry().escapedValue("Another$Invalid") is null);
122     df.terminal = true;
123     df.type = DesktopFile.Type.Application;
124     df.categories = ["Development", "Compilers", "One;Two", "Three\\;Four", "New\nLine"];
125 
126     assert(df.terminal() == true);
127     assert(df.type() == DesktopFile.Type.Application);
128     assert(equal(df.categories(), ["Development", "Compilers", "One;Two", "Three\\;Four","New\nLine"]));
129 
130     df.displayName = "Program name";
131     assert(df.displayName() == "Program name");
132     df.genericName = "Program";
133     assert(df.genericName() == "Program");
134     df.comment = "Do\nthings";
135     assert(df.comment() == "Do\nthings");
136 
137     df.execValue = "utilname";
138     assert(df.execValue() == "utilname");
139 
140     df.noDisplay = true;
141     assert(df.noDisplay());
142     df.hidden = true;
143     assert(df.hidden());
144     df.dbusActivable = true;
145     assert(df.dbusActivable());
146     df.startupNotify = true;
147     assert(df.startupNotify());
148 
149     df.url = "/some/url";
150     assert(df.url == "/some/url");

Meta