-
Notifications
You must be signed in to change notification settings - Fork 0
/
deviceconfiguration.go
75 lines (63 loc) · 2.05 KB
/
deviceconfiguration.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
// Copyright (C) 2014 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package config
import (
"fmt"
"sort"
)
func (cfg DeviceConfiguration) Copy() DeviceConfiguration {
c := cfg
c.Addresses = make([]string, len(cfg.Addresses))
copy(c.Addresses, cfg.Addresses)
c.AllowedNetworks = make([]string, len(cfg.AllowedNetworks))
copy(c.AllowedNetworks, cfg.AllowedNetworks)
c.IgnoredFolders = make([]ObservedFolder, len(cfg.IgnoredFolders))
copy(c.IgnoredFolders, cfg.IgnoredFolders)
return c
}
func (cfg *DeviceConfiguration) prepare(sharedFolders []string) {
if len(cfg.Addresses) == 0 || len(cfg.Addresses) == 1 && cfg.Addresses[0] == "" {
cfg.Addresses = []string{"dynamic"}
}
ignoredFolders := deduplicateObservedFoldersToMap(cfg.IgnoredFolders)
for _, sharedFolder := range sharedFolders {
delete(ignoredFolders, sharedFolder)
}
cfg.IgnoredFolders = sortedObservedFolderSlice(ignoredFolders)
}
func (cfg *DeviceConfiguration) IgnoredFolder(folder string) bool {
for _, ignoredFolder := range cfg.IgnoredFolders {
if ignoredFolder.ID == folder {
return true
}
}
return false
}
func sortedObservedFolderSlice(input map[string]ObservedFolder) []ObservedFolder {
output := make([]ObservedFolder, 0, len(input))
for _, folder := range input {
output = append(output, folder)
}
sort.Slice(output, func(i, j int) bool {
return output[i].Time.Before(output[j].Time)
})
return output
}
func deduplicateObservedFoldersToMap(input []ObservedFolder) map[string]ObservedFolder {
output := make(map[string]ObservedFolder, len(input))
for _, folder := range input {
if existing, ok := output[folder.ID]; !ok || existing.Time.Before(folder.Time) {
output[folder.ID] = folder
}
}
return output
}
func (cfg *DeviceConfiguration) Description() string {
if cfg.Name == "" {
return cfg.DeviceID.Short().String()
}
return fmt.Sprintf("%s (%s)", cfg.Name, cfg.DeviceID.Short())
}