-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.d
129 lines (117 loc) · 4.04 KB
/
util.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/+dub.sdl:
name "util"
dependency "desktopfile" path="../"
+/
import std.stdio;
import std.getopt;
import std.process;
import std.path;
import desktopfile.file;
import findexecutable;
import isfreedesktop;
@safe string currentLocale() nothrow
{
try {
return environment.get("LC_ALL", environment.get("LC_MESSAGES", environment.get("LANG")));
}
catch(Exception e) {
return null;
}
}
void main(string[] args)
{
string action;
string[] appPaths;
getopt(args,
"action", "Desktop Action to run (only with 'exec' command)", &action,
"appPath", "Path of applications directory", &appPaths
);
if (args.length < 3) {
writefln("Usage: %s <read|exec|open|start|write> <desktop-file> <optional arguments>", args[0]);
return;
}
string command = args[1];
string inFile = args[2];
string locale = currentLocale();
if (appPaths.length == 0) {
static if (isFreedesktop) {
import desktopfile.paths;
appPaths = applicationsPaths();
}
version(Windows) {
try {
auto root = environment.get("SYSTEMDRIVE", "C:");
auto kdeAppDir = root ~ `\ProgramData\KDE\share\applications`;
if (kdeAppDir.isDir) {
appPaths = [kdeAppDir];
}
} catch(Exception e) {
}
}
}
if (inFile == inFile.baseName && inFile.extension == ".desktop") {
string desktopId = inFile;
inFile = findDesktopFile(desktopId, appPaths);
if (inFile is null) {
stderr.writeln("Could not find desktop file with such id: ", desktopId);
return;
}
}
if (command == "read") {
auto df = new DesktopFile(inFile);
writefln("Name: %s. Localized: %s", df.displayName(), df.localizedDisplayName(locale));
writefln("GenericName: %s. Localized: %s", df.genericName(), df.localizedGenericName(locale));
writefln("Comment: %s. Localized: %s", df.comment(), df.localizedComment(locale));
writeln("Type: ", df.desktopEntry().escapedValue("Type"));
writeln("Icon: ", df.iconName());
static if (isFreedesktop) {
writeln("Desktop ID: ", df.id());
}
writefln("Actions: %(%s %)", df.actions());
writefln("Categories: %(%s %)", df.categories());
writefln("MimeTypes: %(%s %)", df.mimeTypes());
if (df.type() == DesktopFile.Type.Application) {
writeln("Exec: ", df.execValue());
writeln("In terminal: ", df.terminal());
writeln("Trusted: ", isTrusted(df.fileName));
}
if (df.type() == DesktopFile.Type.Link) {
writeln("URL: ", df.url());
}
} else if (command == "exec") {
auto df = new DesktopFile(inFile);
if (action.length) {
auto desktopAction = df.action(action);
if (desktopAction is null) {
stderr.writefln("No such action (%s)", action);
} else {
string[] urls = args[3..$];
string[] actionArgs = desktopAction.expandExecValue(urls, locale);
writefln("Exec: %(%s %)", actionArgs);
desktopAction.start(urls, locale);
}
} else {
string[] urls = args[3..$];
string[] appArgs = df.expandExecValue(urls, locale);
writefln("Exec: %(%s %)", appArgs);
df.startApplication(urls, locale);
}
} else if (command == "open") {
auto df = new DesktopFile(inFile);
writeln("Link: ", df.url());
df.startLink();
} else if (command == "start") {
auto df = new DesktopFile(inFile);
df.start();
} else if (command == "write") {
auto df = new DesktopFile(inFile);
if (args.length > 3) {
string outFile = args[3];
df.saveToFile(outFile);
} else {
writeln(df.saveToString());
}
} else {
stderr.writefln("unknown command '%s'", command);
}
}