-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.go
475 lines (421 loc) · 11.6 KB
/
utils.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
//--------------------------------------------------------------------------------------------------
// This file is a part of Gorsync Backup project (backup RSYNC frontend).
// Copyright (c) 2017-2020 Denis Dyakov <denis.dyakov@gmail.com>
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//--------------------------------------------------------------------------------------------------
package gtkui
import (
"bytes"
"io/ioutil"
"os"
"strings"
"sync"
"time"
"github.com/d2r2/go-rsync/backup"
"github.com/d2r2/go-rsync/data"
"github.com/d2r2/go-rsync/locale"
"github.com/d2r2/gotk3/gdk"
"github.com/d2r2/gotk3/glib"
"github.com/d2r2/gotk3/gtk"
"github.com/davecgh/go-spew/spew"
)
func PixbufFromAssetsNew(assetIconName string) (*gdk.Pixbuf, error) {
file, err := data.Assets.Open(assetIconName)
if err != nil {
return nil, err
}
defer file.Close()
b, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
pb, err := getPixbufFromBytes(b)
if err != nil {
return nil, err
}
return pb, nil
}
func PixbufFromAssetsNewWithResize(assetIconName string,
resizeToWidth, resizeToHeight int) (*gdk.Pixbuf, error) {
file, err := data.Assets.Open(assetIconName)
if err != nil {
return nil, err
}
defer file.Close()
b, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
pb, err := getPixbufFromBytesWithResize(b, resizeToWidth, resizeToHeight)
if err != nil {
return nil, err
}
return pb, nil
}
func PixbufAnimationFromAssetsNew(assetIconName string) (*gdk.PixbufAnimation, error) {
file, err := data.Assets.Open(assetIconName)
if err != nil {
return nil, err
}
defer file.Close()
b, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
pb, err := getPixbufAnimationFromBytes(b)
if err != nil {
return nil, err
}
return pb, nil
}
func PixbufAnimationFromAssetsNewWithResize(assetIconName string,
resizeToWidth, resizeToHeight int) (*gdk.PixbufAnimation, error) {
file, err := data.Assets.Open(assetIconName)
if err != nil {
return nil, err
}
defer file.Close()
b, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
pb, err := getPixbufAnimationFromBytesWithResize(b, resizeToWidth, resizeToHeight)
if err != nil {
return nil, err
}
return pb, nil
}
func AnimationImageFromAssetsNew(assetIconName string) (*gtk.Image, error) {
pba, err := PixbufAnimationFromAssetsNew(assetIconName)
if err != nil {
return nil, err
}
img, err := gtk.ImageNewFromAnimation(pba)
if err != nil {
return nil, err
}
return img, nil
}
func AnimationImageFromAssetsNewWithResize(assetIconName string,
resizeToWidth, resizeToHeight int) (*gtk.Image, error) {
pba, err := PixbufAnimationFromAssetsNewWithResize(assetIconName, resizeToWidth, resizeToHeight)
if err != nil {
return nil, err
}
img, err := gtk.ImageNewFromAnimation(pba)
if err != nil {
return nil, err
}
return img, nil
}
func ImageFromAssetsNew(assetIconName string) (*gtk.Image, error) {
pb2, err := PixbufFromAssetsNew(assetIconName)
if err != nil {
return nil, err
}
img, err := gtk.ImageNewFromPixbuf(pb2)
if err != nil {
return nil, err
}
return img, nil
}
func ImageFromAssetsNewWithResize(assetIconName string, resizeToWidth, resizeToHeight int) (*gtk.Image, error) {
pb2, err := PixbufFromAssetsNewWithResize(assetIconName, resizeToWidth, resizeToHeight)
if err != nil {
return nil, err
}
img, err := gtk.ImageNewFromPixbuf(pb2)
if err != nil {
return nil, err
}
return img, nil
}
func SetEntryIconWithAssetImage(entry *gtk.Entry, iconPos gtk.EntryIconPosition, assetIconName string) error {
pb, err := PixbufFromAssetsNew(assetIconName)
if err != nil {
return err
}
entry.SetIconFromPixbuf(iconPos, pb)
return nil
}
func SetupButtonWithAssetAnimationImage(assetIconName string) (*gtk.Button, error) {
img, err := AnimationImageFromAssetsNew(assetIconName)
if err != nil {
return nil, err
}
btn, err := gtk.ButtonNew()
if err != nil {
return nil, err
}
btn.Add(img)
return btn, nil
}
// CheckSchemaSettingsIsInstalled verify, that GLib Setting's schema is installed, otherwise return false.
func CheckSchemaSettingsIsInstalled(settingsID string, app *gtk.Application, extraMsg *string) (bool, error) {
parent := app.GetActiveWindow()
// Verify that GSettingsSchema is installed
schemaSource := glib.SettingsSchemaSourceGetDefault()
if schemaSource == nil {
//title := "<span weight='bold' size='larger'>Schema settings configuration error</span>"
text := locale.T(MsgSchemaConfigDlgNoSchemaFoundError, nil)
err := schemaSettingsErrorDialog(parent, text, extraMsg)
if err != nil {
return false, err
}
return false, nil
}
schema := schemaSource.Lookup(settingsID, false)
if schema == nil {
//title := "<span weight='bold' size='larger'>Schema settings configuration error</span>"
text := locale.T(MsgSchemaConfigDlgSchemaDoesNotFoundError, nil)
err := schemaSettingsErrorDialog(parent, text, extraMsg)
if err != nil {
return false, err
}
return false, nil
}
return true, nil
}
// ProgressBarManage simplify setting up GtkProgressBar to pulse either progress mode.
type ProgressBarManage struct {
sync.Mutex
progressBar *gtk.ProgressBar
pulse *time.Ticker
stopPulse chan struct{}
}
func NewProgressBarManage(pb *gtk.ProgressBar) *ProgressBarManage {
p := &ProgressBarManage{progressBar: pb}
return p
}
func (v *ProgressBarManage) StartPulse() {
v.Lock()
defer v.Unlock()
if v.stopPulse == nil {
v.progressBar.SetPulseStep(0.5)
v.progressBar.Pulse()
//v.progressBar.Pulse()
v.stopPulse = make(chan struct{})
v.pulse = time.NewTicker(time.Millisecond * 2000)
go func(stopPulse chan struct{}) {
for {
select {
case <-v.pulse.C:
MustIdleAdd(func() {
v.progressBar.Pulse()
})
case <-stopPulse:
v.Lock()
v.pulse.Stop()
v.Unlock()
MustIdleAdd(func() {
v.progressBar.SetFraction(0)
})
return
}
}
}(v.stopPulse)
}
}
func (v *ProgressBarManage) StopPulse() {
v.Lock()
defer v.Unlock()
if v.stopPulse != nil {
close(v.stopPulse)
v.stopPulse = nil
}
}
func (v *ProgressBarManage) SetFraction(value float64) error {
v.StopPulse()
v.Lock()
defer v.Unlock()
MustIdleAdd(func() {
v.progressBar.SetFraction(value)
})
return nil
}
func (v *ProgressBarManage) AddProgressBarStyleClass(cssClass string) error {
v.Lock()
defer v.Unlock()
MustIdleAdd(func() {
err := AddStyleClass(&v.progressBar.Widget, cssClass)
if err != nil {
lg.Fatal(err)
}
})
return nil
}
func (v *ProgressBarManage) RemoveProgressBarStyleClass(cssClass string) error {
v.Lock()
defer v.Unlock()
MustIdleAdd(func() {
err := RemoveStyleClass(&v.progressBar.Widget, cssClass)
if err != nil {
lg.Fatal(err)
}
})
return nil
}
// ControlWithStatus wraps control to the box to attach extra status widget to the right.
// Status widget would be a error icon either spin control to show active process.
type ControlWithStatus struct {
box *gtk.Box
control *gtk.Widget
statusBox *gtk.Box
}
func NewControlWithStatus(control *gtk.Widget) (*ControlWithStatus, error) {
box, err := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 6)
if err != nil {
return nil, err
}
box.Add(control)
box.SetHExpand(true)
v := &ControlWithStatus{box: box, control: control}
return v, nil
}
func (v *ControlWithStatus) ReplaceStatus(statusBox *gtk.Box) {
if v.statusBox != nil {
v.statusBox.Destroy()
v.statusBox = nil
}
if statusBox != nil {
v.statusBox = statusBox
v.box.Add(statusBox)
v.box.ShowAll()
}
}
func (v *ControlWithStatus) GetBox() *gtk.Box {
return v.box
}
type GLibIdleCallStub struct {
sync.Mutex
}
// var glibIdleCallStub GLibIdleCallStub
var IdleAdd = func(f interface{}, args ...interface{}) (glib.SourceHandle, error) {
// glibIdleCallStub.Lock()
// defer glibIdleCallStub.Unlock()
return glib.IdleAdd(f, args...)
}
var MustIdleAdd = func(f interface{}, args ...interface{}) {
// glibIdleCallStub.Lock()
// defer glibIdleCallStub.Unlock()
_, err := glib.IdleAdd(f, args...)
if err != nil {
lg.Fatalf("error creating call glib.IdleAdd: %v", err)
}
}
/*
var MustIdleAddWait = func(call func()) {
glibIdleCallStub.Lock()
defer glibIdleCallStub.Unlock()
ch := make(chan struct{})
_, err := glib.IdleAdd(func() {
call()
close(ch)
})
if err != nil {
lg.Fatalf("error creating call glib.IdleAdd: %v", err)
}
<-ch
}
*/
// GetBaseApplicationCSS read from assets CSS file, which
// give UI styles used for customization of application interface.
func GetBaseApplicationCSS() (string, error) {
// Load CSS styles
file, err := data.Assets.Open("base.css")
if err != nil {
return "", err
}
defer file.Close()
b, err := ioutil.ReadAll(file)
if err != nil {
return "", err
}
buf := bytes.NewBuffer(b)
return buf.String(), nil
}
func normalizeSubpath(subpath string) string {
subpath = strings.TrimSpace(subpath)
subpath1 := []rune(subpath)
if len(subpath1) > 0 && subpath1[0] == rune(os.PathSeparator) {
subpath1 = subpath1[1:]
}
if len(subpath1) > 0 && subpath1[len(subpath1)-1] == rune(os.PathSeparator) {
subpath1 = subpath1[:len(subpath1)-1]
}
subpath = string(subpath1)
return subpath
}
// markupTooltip create hint for GtkWidget to display description
// with standard template: "Status: ... Description: ...".
func markupTooltip(status *Markup, description string) *Markup {
var mp *Markup
if status != nil {
mp = NewMarkup(0, 0, 0, nil, nil,
NewMarkup(0, MARKUP_COLOR_LIGHT_GRAY, 0,
spew.Sprintf("%s ", locale.T(MsgGeneralHintStatusCaption, nil)), nil),
status,
NewMarkup(0, MARKUP_COLOR_LIGHT_GRAY, 0,
spew.Sprintf("\n%s ", locale.T(MsgGeneralHintDescriptionCaption, nil)), nil),
NewMarkup(0, 0, 0, description, nil),
)
} else {
mp = NewMarkup(0, 0, 0, description, nil)
}
return mp
}
// isDestPathError verify file system path availability status.
// Returns error, if path isn't reachable.
func isDestPathError(destPath string, formatMultiline bool) (bool, string) {
if destPath == "" {
msg := locale.T(MsgAppWindowDestPathIsEmptyError1, nil)
return true, msg
} else {
_, err := os.Stat(destPath)
if err != nil {
var msg string
if os.IsNotExist(err) {
var buf bytes.Buffer
buf.WriteString(locale.T(MsgAppWindowDestPathIsNotExistError,
struct{ FolderPath string }{FolderPath: destPath}))
if formatMultiline {
buf.WriteString(spew.Sprintln())
} else {
buf.WriteString(" ")
}
buf.WriteString(locale.T(MsgAppWindowDestPathIsNotExistAdvise, nil))
msg = buf.String()
} else {
msg = err.Error()
}
return true, msg
}
}
return false, ""
}
func isModulesConfigError(modules []backup.Module, formatMultiline bool) (bool, string) {
for _, module := range modules {
// check for empty RSYNC source path
if module.SourceRsync == "" {
msg := locale.T(MsgAppWindowRsyncPathIsEmptyError, nil)
if !formatMultiline {
// Do not use strings.ReplaceAll() since it was implemented
// only in Go 1.12. Instead use strings.Replace().
msg = strings.Replace(msg, "\n", " ", -1)
}
return true, msg
}
}
return false, ""
}
// RestartTimer restart timer with call fire after specific millisecond period.
// Used as a trigger for validation events.
func RestartTimer(timer *time.Timer, milliseconds time.Duration) {
timer.Stop()
timer.Reset(time.Millisecond * milliseconds)
}