DesktopFile

Represents .desktop file.

Constructors

this
this(string fileName, DesktopReadOptions options = DesktopReadOptions.init)

Reads desktop file from file.

this
this(IniLikeReader reader, DesktopReadOptions options = DesktopReadOptions.init, string fileName = null)

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

this
this(IniLikeReader reader, string fileName, DesktopReadOptions options = DesktopReadOptions.init)

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.0

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.

desktopEntry
inout(DesktopEntry) desktopEntry()
expandExecValue
string[] expandExecValue(in string[] urls = null, string locale = null)

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 = null, lazy const(string)[] terminalCommand = getTerminalCommand)

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

startApplication
void startApplication(in string[] urls = null, string locale = null, lazy const(string)[] terminalCommand = getTerminalCommand)

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.

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     assert(df.action("NotPresented") is null);
81     assert(df.action("Notspecified") is null);
82     assert(df.action("X-NoName") is null);
83     assert(df.action("Settings") !is null);
84 
85     assert(df.saveToString() == desktopFileContents);
86 
87     assert(df.contains("Icon"));
88     df.removeEntry("Icon");
89     assert(!df.contains("Icon"));
90     df.setEscapedValue("Icon", "files");
91     assert(df.contains("Icon"));
92 
93     string contents =
94 `# First comment
95 [Desktop Entry]
96 Key=Value
97 # Comment in group`;
98 
99     df = new DesktopFile(iniLikeStringReader(contents), "test.desktop");
100     assert(df.fileName() == "test.desktop");
101     df.removeGroup("Desktop Entry");
102     assert(df.group("Desktop Entry") !is null);
103     assert(df.desktopEntry() !is null);
104 
105     contents =
106 `[X-SomeGroup]
107 Key=Value`;
108 
109     auto thrown = collectException!IniLikeReadException(new DesktopFile(iniLikeStringReader(contents)));
110     assert(thrown !is null);
111     assert(thrown.lineNumber == 0);
112 
113     df = new DesktopFile();
114     df.desktopEntry().setUnescapedValue("$Invalid", "Valid value", IniLikeGroup.InvalidKeyPolicy.save);
115     assert(df.desktopEntry().escapedValue("$Invalid") == "Valid value");
116     df.desktopEntry().setUnescapedValue("Another$Invalid", "Valid value", IniLikeGroup.InvalidKeyPolicy.skip);
117     assert(df.desktopEntry().escapedValue("Another$Invalid") is null);
118     df.terminal = true;
119     df.type = DesktopFile.Type.Application;
120     df.categories = ["Development", "Compilers", "One;Two", "Three\\;Four", "New\nLine"];
121 
122     assert(df.terminal() == true);
123     assert(df.type() == DesktopFile.Type.Application);
124     assert(equal(df.categories(), ["Development", "Compilers", "One;Two", "Three\\;Four","New\nLine"]));
125 
126     df.displayName = "Program name";
127     assert(df.displayName() == "Program name");
128     df.genericName = "Program";
129     assert(df.genericName() == "Program");
130     df.comment = "Do\nthings";
131     assert(df.comment() == "Do\nthings");
132 
133     df.execValue = "utilname";
134     assert(df.execValue() == "utilname");
135 
136     df.noDisplay = true;
137     assert(df.noDisplay());
138     df.hidden = true;
139     assert(df.hidden());
140     df.dbusActivable = true;
141     assert(df.dbusActivable());
142     df.startupNotify = true;
143     assert(df.startupNotify());
144 
145     df.url = "/some/url";
146     assert(df.url == "/some/url");

Meta