-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (109 loc) · 2.98 KB
/
main.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
package main
import (
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"runtime"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
var signal_bin string
const version = "0.1.0"
func chooseSignalBinary(w fyne.Window) {
callback := func(uri fyne.URIReadCloser, err error) {
if uri == nil {
dialog.ShowInformation("Error", "No file selected", w)
} else {
signal_bin = uri.URI().Path()
}
}
dialogOpen := dialog.NewFileOpen(callback, w)
dialogOpen.Show()
// call it later, then it will be showed on the top
dialog.ShowInformation("Error", "Signal application not found. Please specify", w)
}
func find_signal_executable(w fyne.Window) {
var location string
var err error
switch runtime.GOOS {
case "linux":
location, err = exec.LookPath("signal-desktop")
if err != nil {
chooseSignalBinary(w)
return
}
case "darwin":
location = "/Applications/Signal.app/Contents/MacOS/Signal"
case "windows":
cache_dir, err := os.UserCacheDir()
if err != nil {
chooseSignalBinary(w)
return
}
location = filepath.Join(cache_dir, "Programs", "signal-desktop", "Signal.exe")
}
if _, err := os.Stat(location); errors.Is(err, fs.ErrNotExist) {
chooseSignalBinary(w)
return
}
signal_bin = location
}
func get_data_dir(account_id int) (string, error) {
config_dir, err := os.UserConfigDir()
if err != nil {
return "", err
}
data_dir := filepath.Join(config_dir, fmt.Sprintf("Signal-Account-%d", account_id))
if _, err := os.Stat(data_dir); errors.Is(err, fs.ErrNotExist) {
if err := os.Mkdir(data_dir, os.ModePerm); err != nil {
return "", err
}
}
return data_dir, nil
}
func run_signal(account_id int, w fyne.Window) {
data_dir, err := get_data_dir(account_id)
if err != nil {
dialog.ShowInformation("Error", fmt.Sprintf("Error creating data_dir: %s", err), w)
return
}
dialog.ShowInformation("Info", fmt.Sprintf("Starting Signal Account #r%d with data_dir %q. Plase wait... ", account_id, data_dir), w)
cmd := exec.Command(signal_bin, "--user-data-dir="+data_dir)
err = cmd.Run()
if err != nil {
dialog.ShowInformation("Error", fmt.Sprintf("Error executing Signal: %s\n", err), w)
return
}
}
func main() {
app := app.New()
app.Settings().SetTheme(theme.DarkTheme())
w := app.NewWindow(fmt.Sprintf("Signal account switcher v%s", version))
w.Resize(fyne.NewSize(600, 800))
find_signal_executable(w)
account1 := widget.NewButton("Start signal account #1", func() {
go run_signal(1, w)
})
account2 := widget.NewButton("Start signal account #2", func() {
go run_signal(2, w)
})
account3 := widget.NewButton("Start signal account #3", func() {
go run_signal(3, w)
})
account4 := widget.NewButton("Start signal account #4", func() {
go run_signal(4, w)
})
grid := container.New(layout.NewGridLayout(1), account1, account2, account3, account4)
w.SetContent(grid)
w.Show()
w.ShowAndRun()
fmt.Println("Exiting ...")
}