shootDesktopFile

Read the desktop file and run application or open link depending on the type of the given desktop file.

  1. void shootDesktopFile(IniLikeReader reader, string fileName = null, ShootOptions options = ShootOptions.init)
    void
    shootDesktopFile
    (
    IniLikeReader
    )
    (
    IniLikeReader reader
    ,
    string fileName = null
    ,
    )
  2. void shootDesktopFile(string fileName, ShootOptions options = ShootOptions.init)

Parameters

reader
Type: IniLikeReader

inilike.range.IniLikeReader returned by inilike.range.iniLikeRangeReader or similar function.

fileName
Type: string

file name of desktop file where data read from. Can be used in field code expanding, should be set to the file name from which contents inilike.range.IniLikeReader was constructed.

options

options that set behavior of the function. Use this function to execute desktop file fast, without creating of DesktopFile instance.

Throws

ProcessException on failure to start the process. DesktopExecException if exec string is invalid. Exception on other errors.

Examples

1 string contents;
2 ShootOptions options;
3 
4 contents = "[Desktop Entry]\nURL=testurl";
5 options.flags = ShootOptions.FollowLink;
6 assertThrown(shootDesktopFile(iniLikeStringReader(contents), null, options));
7 
8 contents = "[Group]\nKey=Value";
9 options = ShootOptions.init;
10 assertThrown(shootDesktopFile(iniLikeStringReader(contents), null, options));
11 
12 contents = "[Desktop Entry]\nURL=testurl";
13 options = ShootOptions.init;
14 bool wasCalled;
15 options.opener = delegate void (string url) {
16     assert(url == "testurl");
17     wasCalled = true;
18 };
19 
20 shootDesktopFile(iniLikeStringReader(contents), null, options);
21 assert(wasCalled);
22 
23 contents = "[Desktop Entry]";
24 options = ShootOptions.init;
25 assertThrown(shootDesktopFile(iniLikeStringReader(contents), null, options));
26 
27 contents = "[Desktop Entry]\nURL=testurl";
28 options.flags = ShootOptions.Exec;
29 assertThrown(shootDesktopFile(iniLikeStringReader(contents), null, options));
30 
31 contents = "[Desktop Entry]\nExec=whoami";
32 options.flags = ShootOptions.Link;
33 assertThrown(shootDesktopFile(iniLikeStringReader(contents), null, options));
34 
35 version(desktopfileFileTest) static if (isFreedesktop) {
36     try {
37         contents = "[Desktop Entry]\nExec=whoami\nTerminal=true";
38         options.flags = ShootOptions.Exec;
39         wasCalled = false;
40         options.terminalDetector = delegate string[] () {wasCalled = true; return null;};
41         shootDesktopFile(iniLikeStringReader(contents), null, options);
42         assert(wasCalled);
43 
44         string tempPath = buildPath(tempDir(), "desktopfile-unittest-tempdir");
45         if (!tempPath.exists) {
46             mkdir(tempPath);
47         }
48         scope(exit) rmdir(tempPath);
49 
50         string tempDesktopFile = buildPath(tempPath, "followtest.desktop");
51         auto f = File(tempDesktopFile, "w");
52         scope(exit) remove(tempDesktopFile);
53         f.rawWrite("[Desktop Entry]\nURL=testurl");
54         f.flush();
55 
56         contents = "[Desktop Entry]\nURL=" ~ tempDesktopFile;
57         options.flags = ShootOptions.Link | ShootOptions.FollowLink;
58         options.opener = delegate void (string url) {
59             assert(url == "testurl");
60             wasCalled = true;
61         };
62 
63         shootDesktopFile(iniLikeStringReader(contents), null, options);
64         assert(wasCalled);
65     } catch(Exception e) {
66 
67     }
68 }

See Also

Meta