This repository has been archived by the owner on Oct 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_gui.go
424 lines (335 loc) · 10.5 KB
/
handle_gui.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
package main
import (
"errors"
"fmt"
"path"
"time"
app "github.com/msepp/stopwatch/stopwatchapp"
"github.com/msepp/stopwatch/stopwatchdb"
)
// HandleGUIMessage is called when we receive messages from the user interface.
func HandleGUIMessage(msg *app.Message) (interface{}, error) {
switch msg.Key {
case app.RequestActiveTask:
return HandleGetActiveTask(msg)
case app.RequestAddGroup:
return HandleAddGroup(msg)
case app.RequestAddTask:
return HandleAddTask(msg)
case app.RequestAppVersions:
return HandleGetAppVersions(msg)
case app.RequestGetHistory:
return HandleGetHistory(msg)
case app.RequestGroups:
return HandleGetGroups(msg)
case app.RequestGroupTasks:
return HandleGetGroupTasks(msg)
case app.RequestGetTask:
return HandleGetTask(msg)
case app.RequestGetUsage:
return HandleGetUsage(msg)
case app.RequestOpenDatabase:
return HandleOpenDatabase(msg)
case app.RequestSetHistory:
return HandleSetHistory(msg)
case app.RequestStartTask:
return HandleStartTask(msg)
case app.RequestStopTask:
return HandleStopTask(msg)
case app.RequestUpdateGroup:
return HandleUpdateGroup(msg)
case app.RequestUpdateTask:
return HandleUpdateTask(msg)
default:
return nil, fmt.Errorf("Unrecognized GUI message key: %s", msg.Key)
}
}
// HandleGetAppVersions returns versions used in the updater
func HandleGetAppVersions(msg *app.Message) (interface{}, error) {
return map[string]string{
"app": app.Version(),
"build": app.Build(),
"electron": app.ElectronVersion(),
"astilectron": app.AstilectronVersion(),
}, nil
}
// HandleOpenDatabase attempts to open database. Returns if the operation
// succeeded.
func HandleOpenDatabase(msg *app.Message) (interface{}, error) {
if gState.db == nil {
gState.db = stopwatchdb.New()
}
if gState.databasePath == "" {
rootdir := gState.app.WorkingDir()
gState.databasePath = path.Join(rootdir, "data.dat")
}
if err := gState.db.Open(gState.databasePath); err != nil {
gState.db = nil
return nil, fmt.Errorf("failed to open database: %s", err)
}
return nil, nil
}
// HandleGetHistory returns task history
func HandleGetHistory(msg *app.Message) (interface{}, error) {
if gState.db == nil {
return nil, fmt.Errorf("no database")
}
// Retrieve task usage
history, err := gState.db.ReadHistory()
if err != nil {
return nil, fmt.Errorf("Unable to read history: %s", err)
}
return history, nil
}
// HandleSetHistory saves history information
func HandleSetHistory(msg *app.Message) (interface{}, error) {
if gState.db == nil {
return nil, fmt.Errorf("no database")
}
var payload ReqPayloadSetHistory
if err := msg.Into(&payload); err != nil {
return nil, fmt.Errorf("payload invalid or missing: %s", err)
}
// Save history items
err := gState.db.SaveHistory(payload.History)
if err != nil {
return nil, fmt.Errorf("Unable to save history: %s", err)
}
return nil, nil
}
// HandleGetGroups returns all known groups
func HandleGetGroups(msg *app.Message) (interface{}, error) {
if gState.db == nil {
return nil, fmt.Errorf("no database")
}
// Retrieve groups
groups, err := gState.db.ReadGroups()
if err != nil {
return nil, fmt.Errorf("Unable to read groups: %s", err)
}
return groups, nil
}
// HandleGetTask returns details of a single task
func HandleGetTask(msg *app.Message) (interface{}, error) {
if gState.db == nil {
return nil, fmt.Errorf("no database")
}
var payload ReqPayloadGetTask
if err := msg.Into(&payload); err != nil || payload.TaskID <= 0 || payload.GroupID <= 0 {
return nil, fmt.Errorf("payload invalid or missing: %s", err)
}
// Retrieve task
task, err := gState.db.ReadTask(payload.GroupID, payload.TaskID)
if err != nil {
return nil, fmt.Errorf("Unable to read task: %s", err)
}
return task, nil
}
// HandleGetActiveTask returns current active task or nil if not set.
func HandleGetActiveTask(msg *app.Message) (interface{}, error) {
if gState.db == nil {
return nil, fmt.Errorf("no database")
}
// Get active task
at, err := gState.db.GetActiveTask()
if err != nil {
return nil, fmt.Errorf("Unable to read active task: %s", err)
}
return at, nil
}
// HandleGetGroupTasks returns list of tasks for a group
func HandleGetGroupTasks(msg *app.Message) (interface{}, error) {
if gState.db == nil {
return nil, fmt.Errorf("no database")
}
var payload ReqPayloadGetGroupTasks
if err := msg.Into(&payload); err != nil || payload.GroupID <= 0 {
return nil, fmt.Errorf("payload invalid or missing: %s", err)
}
// Retrieve tasks
tlist, err := gState.db.ReadTasks(payload.GroupID)
if err != nil {
return nil, fmt.Errorf("Unable to read tasks: %s", err)
}
return tlist, nil
}
// HandleAddGroup adds the group detailed in msg data
func HandleAddGroup(msg *app.Message) (interface{}, error) {
if gState.db == nil {
return nil, fmt.Errorf("no database")
}
var payload ReqPayloadAddGroup
if err := msg.Into(&payload); err != nil || payload.Name == "" {
return nil, fmt.Errorf("payload invalid or missing: %s", err)
}
p, err := gState.db.AddGroup(payload.Name)
if err != nil {
return nil, fmt.Errorf("failed to add group: %s", err)
}
return p, nil
}
// HandleUpdateGroup updates group value in database
func HandleUpdateGroup(msg *app.Message) (interface{}, error) {
if gState.db == nil {
return nil, fmt.Errorf("no database")
}
var payload ReqPayloadUpdateGroup
if err := msg.Into(&payload); err != nil {
return nil, fmt.Errorf("payload invalid: %s", err)
}
if payload.GroupID <= 0 {
return nil, fmt.Errorf("group id must be non-zero positive integer")
}
// Locate task
grp, err := gState.db.GetGroup(payload.GroupID)
if err != nil {
return nil, fmt.Errorf("group not found: %s", err)
}
// Save with new name
grp.Name = payload.Name
return grp, gState.db.SaveGroup(grp)
}
// HandleUpdateTask updates task value in database
func HandleUpdateTask(msg *app.Message) (interface{}, error) {
if gState.db == nil {
return nil, fmt.Errorf("no database")
}
var payload ReqPayloadUpdateTask
if err := msg.Into(&payload); err != nil {
return nil, fmt.Errorf("payload invalid: %s", err)
}
if payload.GroupID <= 0 || payload.TaskID <= 0 {
return nil, fmt.Errorf("group id and task id must be non-zero positive integers")
}
if payload.Name == "" {
return nil, fmt.Errorf("name can't be empty")
}
// Locate task
task, err := gState.db.GetTask(payload.GroupID, payload.TaskID)
if err != nil {
return nil, fmt.Errorf("task not found: %s", err)
}
// Save with new name
task.Name = payload.Name
task.CostCode = payload.CostCode
return task, gState.db.SaveTask(task)
}
// HandleAddTask adds a task for a group using details from msg data
func HandleAddTask(msg *app.Message) (interface{}, error) {
if gState.db == nil {
return nil, fmt.Errorf("no database")
}
var payload ReqPayloadAddTask
if err := msg.Into(&payload); err != nil {
return nil, fmt.Errorf("payload invalid: %s", err)
}
if payload.Name == "" {
return nil, fmt.Errorf("name must be given and not empty")
}
if payload.GroupID <= 0 {
return nil, fmt.Errorf("groupid must be a non-zero positive integer")
}
t, err := gState.db.AddTask(payload.GroupID, payload.Name, payload.CostCode)
if err != nil {
return nil, fmt.Errorf("failed to add task: %s", err)
}
return t, nil
}
// HandleStartTask starts a task
func HandleStartTask(msg *app.Message) (interface{}, error) {
if gState.db == nil {
return nil, fmt.Errorf("no database")
}
var payload ReqPayloadSetTaskStatus
if err := msg.Into(&payload); err != nil {
return nil, fmt.Errorf("payload invalid: %s", err)
}
if payload.GroupID <= 0 || payload.TaskID <= 0 {
return nil, fmt.Errorf("group and task IDs must be non-zero positive integers")
}
// Locate task
task, err := gState.db.GetTask(payload.GroupID, payload.TaskID)
if err != nil {
return nil, fmt.Errorf("failure starting task: %s", err)
}
activeTask, err := gState.db.GetActiveTask()
if err != nil {
return nil, fmt.Errorf("Unable test active task: %s", err)
}
// Stop active task if not the same as current task
if activeTask != nil {
if activeTask.ID == task.ID && activeTask.GroupID == task.GroupID {
if activeTask.Running != nil {
return activeTask, nil
}
} else {
if _, err = gState.db.StopTask(activeTask.GroupID, activeTask.ID); err != nil {
return nil, fmt.Errorf("unable to stop current task: %s", err)
}
}
}
if err = gState.db.SetActiveTask(task.GroupID, task.ID); err != nil {
return nil, fmt.Errorf("unable to set active task: %s", err)
}
// Mark running
return gState.db.StartTask(task.GroupID, task.ID)
}
// HandleStopTask stops a task
func HandleStopTask(msg *app.Message) (interface{}, error) {
if gState.db == nil {
return nil, fmt.Errorf("no database")
}
var payload ReqPayloadSetTaskStatus
if err := msg.Into(&payload); err != nil {
return nil, fmt.Errorf("payload invalid: %s", err)
}
if payload.GroupID <= 0 || payload.TaskID <= 0 {
return nil, fmt.Errorf("group and task IDs must be non-zero positive integers")
}
// Locate task
task, err := gState.db.GetTask(payload.GroupID, payload.TaskID)
if err != nil {
return nil, fmt.Errorf("failure stopping task: %s", err)
}
// Stop active task if it matches current task
activeTask, err := gState.db.GetActiveTask()
if err != nil {
return nil, fmt.Errorf("Unable test active task: %s", err)
}
// Stop active task if it matches the given task
if activeTask != nil {
if activeTask.ID == task.ID && activeTask.GroupID == task.GroupID {
if err := gState.db.SetActiveTask(0, 0); err != nil {
return nil, fmt.Errorf("unable to clear active task: %s", err)
}
}
}
// Mark running
return gState.db.StopTask(task.GroupID, task.ID)
}
// HandleGetUsage handle request for usage statistics
func HandleGetUsage(msg *app.Message) (interface{}, error) {
if gState.db == nil {
return nil, fmt.Errorf("no database")
}
var payload ReqPayloadGetUsage
if err := msg.Into(&payload); err != nil {
return nil, fmt.Errorf("payload invalid: %s", err)
}
if payload.GroupID <= 0 {
return nil, errors.New("invalid group id")
}
var start time.Time
var end time.Time
var err error
if start, err = time.Parse("2006-01-02", payload.StartDate); err != nil {
return nil, fmt.Errorf("start date invalid: %s", err)
}
if end, err = time.Parse("2006-01-02", payload.EndDate); err != nil {
return nil, fmt.Errorf("end date invalid: %s", err)
}
if start.IsZero() || end.IsZero() {
return nil, errors.New("start and end must be defined and not be empty")
}
return gState.db.GetUsage(payload.GroupID, start, end)
}