Debian Codegraph @

desktop/entry.go

raw
1 package desktop
2
3 import (
4 "errors"
5 "github.com/rkoesters/xdg/keyfile"
6 "io"
7 "strings"
8 )
9
10 var (
11 // ErrMissingType means that the desktop entry is missing the
12 // Type key, which is always required.
13 ErrMissingType = errors.New("missing entry type")
14
15 // ErrMissingName means that the desktop entry is missing the
16 // Name key, which is required by the types Application, Link,
17 // and Directory.
18 ErrMissingName = errors.New("missing entry name")
19
20 // ErrMissingURL means that the desktop entry is missing the URL
21 // key, which is required by the type Link.
22 ErrMissingURL = errors.New("missing entry url")
23 )
24
25 const (
26 groupDesktopEntry = "Desktop Entry"
27 groupDesktopActionPrefix = "Desktop Action "
28
29 keyType = "Type"
30 keyVersion = "Version"
31 keyName = "Name"
32 keyGenericName = "GenericName"
33 keyNoDisplay = "NoDisplay"
34 keyComment = "Comment"
35 keyIcon = "Icon"
36 keyHidden = "Hidden"
37 keyOnlyShowIn = "OnlyShowIn"
38 keyNotShowIn = "NotShowIn"
39 keyDBusActivatable = "DBusActivatable"
40 keyTryExec = "TryExec"
41 keyExec = "Exec"
42 keyPath = "Path"
43 keyTerminal = "Terminal"
44 keyActions = "Actions"
45 keyMimeType = "MimeType"
46 keyCategories = "Categories"
47 keyImplements = "Implements"
48 keyKeywords = "Keywords"
49 keyStartupNotify = "StartupNotify"
50 keyStartupWMClass = "StartupWMClass"
51 keyURL = "URL"
52 )
53
54 // Entry represents a desktop entry file.
55 type Entry struct {
56 // The type of desktop entry. It can be: Application, Link, or
57 // Directory.
58 Type Type
59 // The version of spec that the file conforms to.
60 Version string
61
62 // The real name of the desktop entry.
63 Name string
64 // A generic name, for example: Text Editor or Web Browser.
65 GenericName string
66 // A short comment that describes the desktop entry.
67 Comment string
68 // The name of an icon that should be used for this desktop
69 // entry. If it is not an absolute path, it should be searched
70 // for using the Icon Theme Specification.
71 Icon string
72 // The URL for a Link type entry.
73 URL string
74
75 // Whether or not to display the file in menus.
76 NoDisplay bool
77 // Whether the use has deleted the desktop entry.
78 Hidden bool
79 // A list of desktop environments that the desktop entry should
80 // only be shown in.
81 OnlyShowIn []string
82 // A list of desktop environments that the desktop entry should
83 // not be shown in.
84 NotShowIn []string
85
86 // Whether DBus Activation is supported by this application.
87 DBusActivatable bool
88 // The path to an executable to test if the program is
89 // installed.
90 TryExec string
91 // Program to execute.
92 Exec string
93 // The path that should be the programs working directory.
94 Path string
95 // Whether the program should be run in a terminal window.
96 Terminal bool
97
98 // A slice of actions.
99 Actions []*Action
100 // A slice of mimetypes supported by this program.
101 MimeType []string
102 // A slice of categories that the desktop entry should be shown
103 // in in a menu.
104 Categories []string
105 // A slice of interfaces that this application implements.
106 Implements []string
107 // A slice of keywords.
108 Keywords []string
109
110 // Whether the program will send a "remove" message when started
111 // with the DESKTOP_STARTUP_ID env variable is set.
112 StartupNotify bool
113 // The string that the program will set as WM Class or WM name
114 // hint.
115 StartupWMClass string
116
117 // Extended pairs. These are all of the key=value pairs in which
118 // the key follows the format X-PRODUCT-KEY. For example,
119 // accessing X-Unity-IconBackgroundColor can be done with:
120 //
121 // entry.X["Unity"]["IconBackgroundColor"]
122 //
123 X map[string]map[string]string
124 }
125
126 // New reads a desktop file from r and returns an Entry that represents
127 // the desktop file using the default locale.
128 func New(r io.Reader) (*Entry, error) {
129 return NewWithLocale(r, keyfile.DefaultLocale())
130 }
131
132 // NewWithLocale reads a desktop file from r and returns an Entry that
133 // represents the desktop file using the given locale l.
134 func NewWithLocale(r io.Reader, l keyfile.Locale) (*Entry, error) {
135 kf, err := keyfile.New(r)
136 if err != nil {
137 return nil, err
138 }
139
140 // Create the entry.
141 e := new(Entry)
142
143 e.Type = ParseType(kf.Value(groupDesktopEntry, keyType))
144 if kf.KeyExists(groupDesktopEntry, keyVersion) {
145 e.Version, err = kf.String(groupDesktopEntry, keyVersion)
146 if err != nil {
147 return nil, err
148 }
149 }
150 e.Name, err = kf.LocaleStringWithLocale(groupDesktopEntry, keyName, l)
151 if err != nil {
152 return nil, err
153 }
154 if kf.KeyExists(groupDesktopEntry, keyGenericName) {
155 e.GenericName, err = kf.LocaleStringWithLocale(groupDesktopEntry, keyGenericName, l)
156 if err != nil {
157 return nil, err
158 }
159 }
160 if kf.KeyExists(groupDesktopEntry, keyNoDisplay) {
161 e.NoDisplay, err = kf.Bool(groupDesktopEntry, keyNoDisplay)
162 if err != nil {
163 return nil, err
164 }
165 }
166 if kf.KeyExists(groupDesktopEntry, keyComment) {
167 e.Comment, err = kf.LocaleStringWithLocale(groupDesktopEntry, keyComment, l)
168 if err != nil {
169 return nil, err
170 }
171 }
172 if kf.KeyExists(groupDesktopEntry, keyIcon) {
173 e.Icon, err = kf.LocaleStringWithLocale(groupDesktopEntry, keyIcon, l)
174 if err != nil {
175 return nil, err
176 }
177 }
178 if kf.KeyExists(groupDesktopEntry, keyHidden) {
179 e.Hidden, err = kf.Bool(groupDesktopEntry, keyHidden)
180 if err != nil {
181 return nil, err
182 }
183 }
184 if kf.KeyExists(groupDesktopEntry, keyOnlyShowIn) {
185 e.OnlyShowIn, err = kf.StringList(groupDesktopEntry, keyOnlyShowIn)
186 if err != nil {
187 return nil, err
188 }
189 }
190 if kf.KeyExists(groupDesktopEntry, keyNotShowIn) {
191 e.NotShowIn, err = kf.StringList(groupDesktopEntry, keyNotShowIn)
192 if err != nil {
193 return nil, err
194 }
195 }
196 if kf.KeyExists(groupDesktopEntry, keyDBusActivatable) {
197 e.DBusActivatable, err = kf.Bool(groupDesktopEntry, keyDBusActivatable)
198 if err != nil {
199 return nil, err
200 }
201 }
202 if kf.KeyExists(groupDesktopEntry, keyTryExec) {
203 e.TryExec, err = kf.String(groupDesktopEntry, keyTryExec)
204 if err != nil {
205 return nil, err
206 }
207 }
208 if kf.KeyExists(groupDesktopEntry, keyExec) {
209 e.Exec, err = kf.String(groupDesktopEntry, keyExec)
210 if err != nil {
211 return nil, err
212 }
213 }
214 if kf.KeyExists(groupDesktopEntry, keyPath) {
215 e.Path, err = kf.String(groupDesktopEntry, keyPath)
216 if err != nil {
217 return nil, err
218 }
219 }
220 if kf.KeyExists(groupDesktopEntry, keyTerminal) {
221 e.Terminal, err = kf.Bool(groupDesktopEntry, keyTerminal)
222 if err != nil {
223 return nil, err
224 }
225 }
226 if kf.KeyExists(groupDesktopEntry, keyActions) {
227 e.Actions, err = getActions(kf)
228 if err != nil {
229 return nil, err
230 }
231 }
232 if kf.KeyExists(groupDesktopEntry, keyMimeType) {
233 e.MimeType, err = kf.StringList(groupDesktopEntry, keyMimeType)
234 if err != nil {
235 return nil, err
236 }
237 }
238 if kf.KeyExists(groupDesktopEntry, keyCategories) {
239 e.Categories, err = kf.StringList(groupDesktopEntry, keyCategories)
240 if err != nil {
241 return nil, err
242 }
243 }
244 if kf.KeyExists(groupDesktopEntry, keyImplements) {
245 e.Implements, err = kf.StringList(groupDesktopEntry, keyImplements)
246 if err != nil {
247 return nil, err
248 }
249 }
250 if kf.KeyExists(groupDesktopEntry, keyKeywords) {
251 e.Keywords, err = kf.LocaleStringListWithLocale(groupDesktopEntry, keyKeywords, l)
252 if err != nil {
253 return nil, err
254 }
255 }
256 if kf.KeyExists(groupDesktopEntry, keyStartupNotify) {
257 e.StartupNotify, err = kf.Bool(groupDesktopEntry, keyStartupNotify)
258 if err != nil {
259 return nil, err
260 }
261 }
262 if kf.KeyExists(groupDesktopEntry, keyStartupWMClass) {
263 e.StartupWMClass, err = kf.String(groupDesktopEntry, keyStartupWMClass)
264 if err != nil {
265 return nil, err
266 }
267 }
268 if e.Type == Link {
269 e.URL, err = kf.String(groupDesktopEntry, keyURL)
270 if err != nil {
271 return nil, err
272 }
273 }
274
275 // Validate the entry.
276 if e.Type == None {
277 return nil, ErrMissingType
278 }
279 if e.Type > None && e.Type < Unknown && e.Name == "" {
280 return nil, ErrMissingName
281 }
282 if e.Type == Link && e.URL == "" {
283 return nil, ErrMissingURL
284 }
285
286 // Search for extended keys.
287 e.X = make(map[string]map[string]string)
288 for _, k := range kf.Keys(groupDesktopEntry) {
289 a := strings.SplitN(k, "-", 3)
290 if a[0] != "X" || len(a) < 3 {
291 continue
292 }
293 if e.X[a[1]] == nil {
294 e.X[a[1]] = make(map[string]string)
295 }
296 e.X[a[1]][a[2]] = kf.Value(groupDesktopEntry, k)
297 }
298
299 return e, nil
300 }
301
302 // Action is an Action group.
303 type Action struct {
304 Name string
305 Icon string
306 Exec string
307 }
308
309 func getActions(kf *keyfile.KeyFile) ([]*Action, error) {
310 var acts []*Action
311 var act *Action
312 var err error
313 var list []string
314
315 list, err = kf.StringList(groupDesktopEntry, keyActions)
316 if err != nil {
317 return nil, err
318 }
319 for _, a := range list {
320 g := groupDesktopActionPrefix + a
321
322 act = new(Action)
323
324 act.Name, err = kf.String(g, keyName)
325 if err != nil {
326 return nil, err
327 }
328 act.Icon, err = kf.String(g, keyIcon)
329 if err != nil {
330 return nil, err
331 }
332 act.Exec, err = kf.String(g, keyExec)
333 if err != nil {
334 return nil, err
335 }
336
337 acts = append(acts, act)
338 }
339 return acts, nil
340 }