-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfdo.go
634 lines (564 loc) · 17.2 KB
/
fdo.go
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
package appie
import (
"bufio"
"io/fs"
"math"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"fyne.io/fyne/v2"
_ "github.com/fyne-io/image/xpm" // load XPM icons to supported image format
)
var (
iconExtensions = []string{".png", ".svg", ".xpm"}
fallbackCategory = "Other"
supportedCategories = []string{
"AudioVideo", "Development", "Education", "Game", "Graphics", "Network",
"Office", "Science", "Settings", "System", "Utility"}
)
// fdoApplicationData is a structure that contains information about .desktop files
type fdoApplicationData struct {
name string // Application name
iconName string // Icon name
iconPath string // Icon path
exec string // Command to execute application
categories, mime []string
hide bool
iconCache fyne.Resource
source *AppSource
}
// Name returns the name associated with an fdo app
func (data *fdoApplicationData) Name() string {
return data.name
}
// Categories returns a list of the categories this icon has configured
func (data *fdoApplicationData) Categories() []string {
return data.categories
}
func (data *fdoApplicationData) Hidden() bool {
return data.hide
}
// IconName returns the name of the icon that an fdo app wishes to use
func (data *fdoApplicationData) IconName() string {
return data.iconName
}
// Icon returns the path of the icon that an fdo app wishes to use
func (data *fdoApplicationData) Icon(theme string, size int) fyne.Resource {
if data.iconCache != nil {
return data.iconCache
}
path := data.iconPath
if path == "" {
path = FdoLookupIconPath(theme, size, data.iconName)
if path == "" {
return nil
}
}
data.iconCache = loadIcon(path)
return data.iconCache
}
func (data *fdoApplicationData) MimeTypes() []string {
return data.mime
}
func (data *fdoApplicationData) Source() *AppSource {
return data.source
}
// extractArgs sanitises argument parameters from an Exec configuration
func extractArgs(args, params []string) []string {
paramIndex := 0
var ret []string
for _, arg := range args {
if len(arg) >= 2 && arg[0] == '%' {
switch arg[1] {
case 'u', 'f': // url, file
if paramIndex < len(params) {
ret = append(ret, params[paramIndex])
paramIndex++
}
case 'U', 'F': // url list, file list
if paramIndex < len(params) {
ret = append(ret, params[paramIndex:]...)
paramIndex = len(params)
}
}
continue
}
ret = append(ret, arg)
}
return ret
}
// Run executes the command for this fdo app.
// It also passes in the specified environment variables
func (data *fdoApplicationData) Run(env []string) error {
return data.RunWithParameters([]string{}, env)
}
// RunWithParameters executes the command for this fdo app.
// It passes any parameters specified and sets up the listed environment.
func (data *fdoApplicationData) RunWithParameters(params, env []string) error {
vars := os.Environ()
vars = append(vars, env...)
commands := strings.Split(data.exec, " ")
command := commands[0]
if command[0] == '"' {
command = command[1 : len(command)-1]
}
cmd := exec.Command(command)
if len(commands) > 1 {
cmd.Args = extractArgs(commands, params) // Args[0] should be binary path
}
cmd.Env = vars
return cmd.Start()
}
func (data *fdoApplicationData) mainCategory() string {
if len(data.Categories()) == 0 {
return fallbackCategory
}
// check that one of the app categories is supported
for _, cat := range data.Categories() {
for _, each := range supportedCategories {
if each == cat {
return cat
}
}
}
return fallbackCategory
}
func loadIcon(path string) fyne.Resource {
data, err := os.ReadFile(path)
if err != nil {
fyne.LogError("Failed to load image", err)
return nil
}
return fyne.NewStaticResource(filepath.Base(path), data)
}
// fdoLookupXdgDataDirs returns a string slice of all XDG_DATA_DIRS
func fdoLookupXdgDataDirs() []string {
dataLocation := os.Getenv("XDG_DATA_DIRS")
locationLookup := strings.Split(dataLocation, ":")
if len(locationLookup) == 0 || (len(locationLookup) == 1 && locationLookup[0] == "") {
var fallbackLocations []string
homeDir, err := os.UserHomeDir()
if err == nil {
fallbackLocations = append(fallbackLocations, filepath.Join(homeDir, ".local/share"))
}
fallbackLocations = append(fallbackLocations, "/usr/local/share")
fallbackLocations = append(fallbackLocations, "/usr/share")
return fallbackLocations
}
return locationLookup
}
func fdoForEachApplicationFile(f func(data AppData) bool) {
locationLookup := fdoLookupXdgDataDirs()
for _, dataDir := range locationLookup {
testLocation := filepath.Join(dataDir, "applications")
files, err := os.ReadDir(testLocation)
if err != nil {
continue
}
for _, file := range files {
if strings.HasPrefix(file.Name(), ".") || file.IsDir() || !strings.HasSuffix(file.Name(), ".desktop") {
continue
}
icon := newFdoIconData(filepath.Join(testLocation, file.Name()))
if icon == nil {
continue
}
if f(icon) {
return
}
}
}
}
// lookupApplicationByMetadata looks up an application by comparing the requested name to the contents of .desktop files
func (f *fdoIconProvider) lookupApplicationByMetadata(appName string) AppData {
var returnIcon AppData
f.cache.forEachCachedApplication(func(_ string, icon AppData) bool {
if icon.(*fdoApplicationData).name == appName || icon.(*fdoApplicationData).exec == appName {
returnIcon = icon
return true
}
return false
})
return returnIcon
}
// lookupApplication looks up an application by name and returns an fdoApplicationData struct
func (f *fdoIconProvider) lookupApplication(appName string) AppData {
if appName == "" {
return nil
}
var found AppData
f.cache.forEachCachedApplication(func(name string, icon AppData) bool {
if name == appName {
found = icon
return true
}
return false
})
if found != nil {
return found
}
//If no match was found checking by filenames, check by file contents
return f.lookupApplicationByMetadata(appName)
}
func fdoClosestSizeIcon(files []fs.DirEntry, iconSize int, format string, baseDir string, joiner string, iconName string) string {
var sizes []int
for _, f := range files {
if format == "32x32" {
size, err := strconv.Atoi(strings.Split(f.Name(), "x")[0])
if err != nil {
continue
}
sizes = append(sizes, size)
} else {
size, err := strconv.Atoi(f.Name())
if err != nil {
continue
}
sizes = append(sizes, size)
}
}
if len(sizes) == 0 {
return ""
}
var closestMatch string
var difference int
for _, size := range sizes {
sizeDir := strconv.Itoa(size)
if format == "32x32" {
sizeDir = sizeDir + "x" + sizeDir
}
matchDir := ""
if joiner != "" {
matchDir = filepath.Join(baseDir, sizeDir, joiner)
} else {
matchDir = filepath.Join(baseDir, sizeDir)
}
diff := int(math.Abs(float64(iconSize - size)))
testIcon := ""
for _, extension := range iconExtensions {
testIcon = filepath.Join(matchDir, iconName+extension)
if _, err := os.Stat(testIcon); os.IsNotExist(err) {
testIcon = ""
} else {
break
}
}
if closestMatch == "" && testIcon != "" {
closestMatch = testIcon
difference = diff
continue
}
if diff < difference && testIcon != "" {
closestMatch = testIcon
difference = diff
}
}
return closestMatch
}
func lookupAnyIconSizeInThemeDir(dir string, joiner string, iconName string, iconSize int) string {
files, err := os.ReadDir(dir)
if err != nil {
return ""
}
// Example is /usr/share/icons/icon_theme/<size>/<joiner>/xterm.png
closestMatch := fdoClosestSizeIcon(files, iconSize, "32x32", dir, joiner, iconName)
if closestMatch == "" {
closestMatch = fdoClosestSizeIcon(files, iconSize, "32", dir, joiner, iconName)
}
if closestMatch != "" {
return closestMatch
}
directory := filepath.Join(dir, joiner)
files, err = os.ReadDir(directory)
if err != nil {
return ""
}
// Example is /usr/share/icons/icon_theme/<joiner>/<size>/xterm.png
closestMatch = fdoClosestSizeIcon(files, iconSize, "32x32", directory, "", iconName)
if closestMatch == "" {
closestMatch = fdoClosestSizeIcon(files, iconSize, "32", directory, "", iconName)
}
return closestMatch
}
// FdoLookupIconPathInTheme searches icon locations to find a match using a provided theme directory.
// The dir path is the root of the theme, parentDir is used to look up inherited themes
func FdoLookupIconPathInTheme(iconSize string, dir string, parentDir string, iconName string) string {
if _, err := os.Stat(dir); os.IsNotExist(err) {
return ""
}
for _, extension := range iconExtensions {
// Example is /usr/share/icons/icon_theme/32/apps/xterm.png
testIcon := filepath.Join(dir, iconSize, "apps", iconName+extension)
if _, err := os.Stat(testIcon); err == nil {
return testIcon
}
// Example is /usr/share/icons/icon_theme/32x32/apps/xterm.png
testIcon = filepath.Join(dir, iconSize+"x"+iconSize, "apps", iconName+extension)
if _, err := os.Stat(testIcon); err == nil {
return testIcon
}
// Example is /usr/share/icons/icon_theme/apps/32/xterm.png
testIcon = filepath.Join(dir, "apps", iconSize, iconName+extension)
if _, err := os.Stat(testIcon); err == nil {
return testIcon
}
// Example is /usr/share/icons/icon_theme/apps/32x32/xterm.png
testIcon = filepath.Join(dir, "apps", iconSize+"x"+iconSize, iconName+extension)
if _, err := os.Stat(testIcon); err == nil {
return testIcon
}
}
// If the requested icon wasn't found in the specific size or scalable dirs
// of the theme try all sizes within theme and all icon dirs besides apps
var subIconDirs = []string{"apps", "actions", "devices", "emblems", "legacy", "mimetypes", "places", "status"}
iconSizeInt, err := strconv.Atoi(iconSize)
if err != nil {
iconSizeInt = 32
}
for _, joiner := range subIconDirs {
testIcon := lookupAnyIconSizeInThemeDir(dir, joiner, iconName, iconSizeInt)
if testIcon != "" {
return testIcon
}
}
//Lets check inherited themes for the match
indexTheme := filepath.Join(dir, "index.theme")
if _, err := os.Stat(indexTheme); !os.IsNotExist(err) {
file, err := os.Open(indexTheme)
if err == nil {
defer file.Close()
var inheritedThemes []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "Inherits=") {
inherits := strings.SplitAfter(line, "=")
inheritedThemes = strings.SplitN(inherits[1], ",", -1)
break
}
}
if len(inheritedThemes) > 0 {
for _, theme := range inheritedThemes {
childDir := filepath.Join(parentDir, "icons", theme)
iconPath := FdoLookupIconPathInTheme(iconSize, childDir, parentDir, iconName)
if iconPath != "" {
return iconPath
}
}
}
}
}
// Try this as a last resort
for _, extension := range iconExtensions {
// Example is /usr/share/icons/icon_theme/scalable/apps/xterm.png
testIcon := filepath.Join(dir, "scalable", "apps", iconName+extension)
if _, err := os.Stat(testIcon); err == nil {
return testIcon
}
// Example is /usr/share/icons/icon_theme/apps/scalable/xterm.png
testIcon = filepath.Join(dir, "apps", "scalable", iconName+extension)
if _, err := os.Stat(testIcon); err == nil {
return testIcon
}
}
return ""
}
// FdoLookupIconPath will take the name of an icon and find a matching image file
func FdoLookupIconPath(theme string, size int, iconName string) string {
locationLookup := fdoLookupXdgDataDirs()
iconTheme := theme
iconSize := strconv.Itoa(size)
for _, dataDir := range locationLookup {
//Example is /usr/share/icons/icon_theme
dir := filepath.Join(dataDir, "icons", iconTheme)
iconPath := FdoLookupIconPathInTheme(iconSize, dir, dataDir, iconName)
if iconPath != "" {
return iconPath
}
}
for _, dataDir := range locationLookup {
//Hicolor is the default fallback theme - Example /usr/share/icons/icon_theme/hicolor
dir := filepath.Join(dataDir, "icons", "hicolor")
iconPath := FdoLookupIconPathInTheme(iconSize, dir, dataDir, iconName)
if iconPath != "" {
return iconPath
}
}
for _, dataDir := range locationLookup {
//Icons may be in the pixmaps directory - test before we do our final fallback
for _, extension := range iconExtensions {
iconPath := filepath.Join(dataDir, "pixmaps", iconName+extension)
if _, err := os.Stat(iconPath); err == nil {
return iconPath
}
}
}
//No Icon Was Found
return ""
}
func fdoLookupAvailableThemes() []string {
var themes []string
locationLookup := fdoLookupXdgDataDirs()
for _, dataDir := range locationLookup {
files, err := os.ReadDir(filepath.Join(dataDir, "icons"))
if err != nil {
continue
}
//Enter icon theme
for _, f := range files {
if strings.HasPrefix(f.Name(), ".") || !f.IsDir() {
continue
}
//Example is /usr/share/icons/gnome
themes = append(themes, f.Name())
}
}
return themes
}
// newFdoIconData creates and returns a struct that contains needed fields from a .desktop file
func newFdoIconData(desktopPath string) AppData {
file, err := os.Open(desktopPath)
if err != nil {
fyne.LogError("Could not open file", err)
return nil
}
defer file.Close()
scanner := bufio.NewScanner(file)
fdoApp := fdoApplicationData{name: "", iconName: "", iconPath: "", exec: ""}
var currentSection string
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "[") {
currentSection = line
}
switch currentSection {
case "[X-Fyne Source]":
if fdoApp.source == nil {
fdoApp.source = &AppSource{}
}
if strings.HasPrefix(line, "Repo=") {
name := strings.SplitAfter(line, "=")
fdoApp.source.Repo = name[1]
}
if strings.HasPrefix(line, "Dir=") {
name := strings.SplitAfter(line, "=")
fdoApp.source.Dir = name[1]
}
case "[Desktop Entry]": // fall through to code below
default:
continue
}
if strings.HasPrefix(line, "Name=") {
name := strings.SplitAfter(line, "=")
fdoApp.name = name[1]
} else if strings.HasPrefix(line, "Icon=") {
icon := strings.SplitAfter(line, "=")
fdoApp.iconName = icon[1]
if _, err := os.Stat(icon[1]); err == nil {
fdoApp.iconPath = icon[1]
}
} else if strings.HasPrefix(line, "Exec=") {
exec := strings.SplitAfter(line, "=")
fdoApp.exec = exec[1]
} else if strings.HasPrefix(line, "Categories=") {
cats := strings.SplitAfter(line, "=")
fdoApp.categories = strings.Split(cats[1], ";")
} else if strings.HasPrefix(line, "MimeType=") {
mimes := strings.SplitAfter(line, "=")
fdoApp.mime = strings.Split(mimes[1], ";")
} else if strings.HasPrefix(line, "NoDisplay=") {
val := strings.Split(line, "=")
if strings.TrimSpace(val[1]) == "true" {
fdoApp.hide = true
}
}
}
if err := scanner.Err(); err != nil {
fyne.LogError("Could not read file", err)
return nil
}
return &fdoApp
}
type fdoIconProvider struct {
cache *appCache
}
// AvailableApps returns all of the available applications in a AppData slice
func (f *fdoIconProvider) AvailableApps() []AppData {
var icons []AppData
fdoForEachApplicationFile(func(icon AppData) bool {
if icon == nil {
return false
}
icons = append(icons, icon)
return false
})
return icons
}
// AvailableThemes returns all available icon themes in a string slice
func (f *fdoIconProvider) AvailableThemes() []string {
return fdoLookupAvailableThemes()
}
// FindAppFromName matches an icon name to a location and returns an AppData interface
func (f *fdoIconProvider) FindAppFromName(appName string) AppData {
return f.lookupApplication(appName)
}
// FindAppsMatching returns a list of icons that match a partial name of an app and returns an AppData slice
func (f *fdoIconProvider) FindAppsMatching(appName string) []AppData {
var icons []AppData
f.cache.forEachCachedApplication(func(_ string, icon AppData) bool {
if icon == nil {
return false
}
if strings.Contains(strings.ToLower(icon.(*fdoApplicationData).name), strings.ToLower(appName)) ||
strings.Contains(strings.ToLower(icon.(*fdoApplicationData).exec), strings.ToLower(appName)) {
icons = append(icons, icon)
}
return false
})
return icons
}
func findOneAppFromNames(f Provider, names ...string) AppData {
for _, name := range names {
app := f.FindAppFromName(name)
if app != nil {
return app
}
}
return nil
}
func appendAppIfExists(apps []AppData, app AppData) []AppData {
if app == nil {
return apps
}
return append(apps, app)
}
func (f *fdoIconProvider) DefaultApps() []AppData {
var apps []AppData
apps = appendAppIfExists(apps, findOneAppFromNames(f, "fyneterm", "xfce4-terminal", "gnome-terminal", "org.kde.konsole", "xterm"))
apps = appendAppIfExists(apps, findOneAppFromNames(f, "chromium", "google-chrome", "firefox"))
apps = appendAppIfExists(apps, findOneAppFromNames(f, "sylpheed", "thunderbird", "evolution"))
apps = appendAppIfExists(apps, f.FindAppFromName("gimp"))
return apps
}
func (f *fdoIconProvider) CategorizedApps() map[string][]AppData {
cats := map[string][]AppData{}
f.cache.forEachCachedApplication(func(_ string, app AppData) bool {
cat := app.(*fdoApplicationData).mainCategory()
var list []AppData
if c, ok := cats[cat]; ok {
list = c
}
list = append(list, app)
cats[cat] = list
return false
})
return cats
}
// NewFDOProvider returns a new application provider following the FreeDesktop.org specifications
func NewFDOProvider() Provider {
source := &fdoIconProvider{}
source.cache = newAppCache(source)
return source
}