-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
file_other.go
97 lines (79 loc) · 1.97 KB
/
file_other.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
// +build !windows,!android,!ios
package dialog
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"fyne.io/fyne"
"fyne.io/fyne/theme"
)
func (f *fileDialog) loadPlaces() []fyne.CanvasObject {
return []fyne.CanvasObject{makeFavoriteButton("Computer", theme.ComputerIcon(), func() {
f.setDirectory("/")
})}
}
func isHidden(file, _ string) bool {
return len(file) == 0 || file[0] == '.'
}
func fileOpenOSOverride(*FileDialog) bool {
return false
}
func fileSaveOSOverride(*FileDialog) bool {
return false
}
func getFavoriteLocationLinux(homeDir, name string) (string, error) {
cmdName := "xdg-user-dir"
fallback := filepath.Join(homeDir, name)
if _, err := exec.LookPath(cmdName); err != nil {
return fallback, fmt.Errorf("%s not found in PATH. using fallback paths", cmdName)
}
cmd := exec.Command(cmdName, name)
stdout := bytes.NewBufferString("")
cmd.Stdout = stdout
err := cmd.Run()
if err != nil {
return fallback, err
}
loc := stdout.String()
// Remove \n at the end
loc = loc[:len(loc)-1]
if loc == homeDir {
return fallback, fmt.Errorf("this computer does not have a %s folder", name)
}
return stdout.String(), nil
}
func getFavoriteLocations() (map[string]string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
if runtime.GOOS == "linux" {
rv := map[string]string{
"Home": homeDir,
}
var err error
documentsDir, err1 := getFavoriteLocationLinux(homeDir, "DOCUMENTS")
if err == nil {
rv["Documents"] = documentsDir
} else {
rv["Documents"] = filepath.Join(homeDir, "Documents")
err = err1
}
downloadsDir, err1 := getFavoriteLocationLinux(homeDir, "DOWNLOADS")
if err == nil {
rv["Downloads"] = downloadsDir
} else {
rv["Downloads"] = filepath.Join(homeDir, "Downloads")
err = err1
}
return rv, err
}
return map[string]string{
"Home": homeDir,
"Documents": filepath.Join(homeDir, "Documents"),
"Downloads": filepath.Join(homeDir, "Downloads"),
}, nil
}