-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
213 lines (179 loc) · 4.77 KB
/
main.go
File metadata and controls
213 lines (179 loc) · 4.77 KB
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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/DataManager-Go/DataManagerGUI/actions"
dmlib "github.com/DataManager-Go/libdatamanager"
dmConfig "github.com/DataManager-Go/libdatamanager/config"
"github.com/asticode/go-astikit"
"github.com/asticode/go-astilectron"
"github.com/sqweek/dialog"
)
var (
app *astilectron.Astilectron
userToken string
appName = "DataManager"
)
func main() {
// Check for first start
path := "./example/vendor/"
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
path = filepath.Join(home, "AppData", "Roaming", appName, "vendor")
}
if _, err := os.Stat(path); os.IsNotExist(err) {
// First start
ok := dialog.Message("%s %s %s", "For your first start", appName, "needs to download additional data. Download now?").Title("Additional data required").YesNo()
if !ok {
return
}
}
// Create astilectron
var err error
app, err = astilectron.New(nil, astilectron.Options{
AppName: appName,
BaseDirectoryPath: "example",
AppIconDarwinPath: "./resources/icon.icns",
AppIconDefaultPath: "./resources/icon.png",
})
if err != nil {
fmt.Println(err.Error())
}
defer app.Close()
// Handle signals
app.HandleSignals()
// Start
if err = app.Start(); err != nil {
fmt.Println(err.Error())
}
// ------------- Main Stuff ----------------
//Init config
actions.Config, err = dmConfig.InitConfig(dmConfig.GetDefaultConfigFile(), "")
if err != nil {
fmt.Println(err.Error())
return
}
// No config found: use the newly created one>
if actions.Config == nil {
actions.Config, err = dmConfig.InitConfig(dmConfig.GetDefaultConfigFile(), "")
if err != nil {
fmt.Println(err.Error())
return
}
}
// Try to get config
rconf, err := actions.Config.ToRequestConfig()
if err != nil {
// TODO Display error here. Also in login.go:119
fmt.Println(err.Error())
}
// Create corresponding manager
actions.Manager = dmlib.NewLibDM(rconf)
if actions.Config.IsLoggedIn() {
StartMainWindow(app)
} else {
StartLoginWindow(app)
}
}
// StartLoginWindow opens the login window
func StartLoginWindow(a *astilectron.Astilectron) {
// New window
var err error
var width, height int
for _, x := range a.Displays() {
if x.IsPrimary() {
width = x.Bounds().Width / 3
}
}
// width = 900 height = 689
height = 650
if actions.Window, err = a.NewWindow("./resources/app/login/index.html", &astilectron.WindowOptions{
Center: astikit.BoolPtr(true),
Height: &height,
Width: &width,
MinHeight: &height,
MinWidth: &width,
MaxHeight: &height,
MaxWidth: &width,
}); err != nil {
fmt.Println(err.Error())
}
// Create windows
if err := actions.Window.Create(); err != nil {
fmt.Println(err.Error())
}
// Message handler
actions.Window.OnMessage(HandleLogin)
// Server IP
if len(actions.Config.Server.URL) > 0 && !strings.Contains(actions.Config.Server.URL, "localhost") {
actions.SendString("URL%%"+actions.Config.Server.URL, actions.HandleResponses)
}
// Blocking pattern
// actions.Window.OpenDevTools()
a.Wait()
}
// StartMainWindow starts the main programm (file explorer)
func StartMainWindow(a *astilectron.Astilectron) {
// New window
var err error
if actions.Window, err = a.NewWindow("./resources/app/main/index.html", &astilectron.WindowOptions{
Center: astikit.BoolPtr(true),
Height: astikit.IntPtr(700),
Width: astikit.IntPtr(1300),
MinHeight: astikit.IntPtr(500),
MinWidth: astikit.IntPtr(500),
Icon: astikit.StrPtr("./resources/icon.png"), // Windows only
}); err != nil {
fmt.Println(err.Error())
}
// Create windows
if err := actions.Window.Create(); err != nil {
fmt.Println(err.Error())
}
// Message handler
actions.Window.OnMessage(func(m *astilectron.EventMessage) (v interface{}) {
ret, err := actions.HandleMessages(m)
if err != nil {
errText := err.Error()
// If error is a request error, print its reason
if e, ok := err.(*dmlib.ResponseErr); ok {
errText = e.Response.Message
}
log.Println(errText)
actions.SendAlert("danger", "Error!", errText)
return nil
}
return ret
})
// Receive namespaces and groups
err = actions.SendInitialData()
// Error in config / server
if err != nil {
fmt.Println(err)
var w = actions.Window
go (func() {
time.Sleep(time.Millisecond * 5)
w.Destroy()
})()
StartLoginWindow(app)
return
}
// Receive initial files data
json, err := actions.GetFiles("", 0, false, dmlib.FileAttributes{Namespace: actions.Config.Default.Namespace}, 3)
if err != nil {
fmt.Println(err.Error())
} else {
actions.SendMessage("files", json, actions.HandleResponses)
}
// Blocking pattern
actions.Window.OpenDevTools()
a.Wait()
}