deviceconfiguration.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package config
  7. import "github.com/syncthing/syncthing/lib/protocol"
  8. type DeviceConfiguration struct {
  9. DeviceID protocol.DeviceID `xml:"id,attr" json:"deviceID"`
  10. Name string `xml:"name,attr,omitempty" json:"name"`
  11. Addresses []string `xml:"address,omitempty" json:"addresses"`
  12. Compression protocol.Compression `xml:"compression,attr" json:"compression"`
  13. CertName string `xml:"certName,attr,omitempty" json:"certName"`
  14. Introducer bool `xml:"introducer,attr" json:"introducer"`
  15. SkipIntroductionRemovals bool `xml:"skipIntroductionRemovals,attr" json:"skipIntroductionRemovals"`
  16. IntroducedBy protocol.DeviceID `xml:"introducedBy,attr" json:"introducedBy"`
  17. Paused bool `xml:"paused" json:"paused"`
  18. AllowedNetworks []string `xml:"allowedNetwork,omitempty" json:"allowedNetworks"`
  19. AutoAcceptFolders bool `xml:"autoAcceptFolders" json:"autoAcceptFolders"`
  20. }
  21. func NewDeviceConfiguration(id protocol.DeviceID, name string) DeviceConfiguration {
  22. d := DeviceConfiguration{
  23. DeviceID: id,
  24. Name: name,
  25. }
  26. d.prepare()
  27. return d
  28. }
  29. func (cfg DeviceConfiguration) Copy() DeviceConfiguration {
  30. c := cfg
  31. c.Addresses = make([]string, len(cfg.Addresses))
  32. copy(c.Addresses, cfg.Addresses)
  33. c.AllowedNetworks = make([]string, len(cfg.AllowedNetworks))
  34. copy(c.AllowedNetworks, cfg.AllowedNetworks)
  35. return c
  36. }
  37. func (cfg *DeviceConfiguration) prepare() {
  38. if len(cfg.Addresses) == 0 || len(cfg.Addresses) == 1 && cfg.Addresses[0] == "" {
  39. cfg.Addresses = []string{"dynamic"}
  40. }
  41. if len(cfg.AllowedNetworks) == 0 {
  42. cfg.AllowedNetworks = []string{}
  43. }
  44. }
  45. type DeviceConfigurationList []DeviceConfiguration
  46. func (l DeviceConfigurationList) Less(a, b int) bool {
  47. return l[a].DeviceID.Compare(l[b].DeviceID) == -1
  48. }
  49. func (l DeviceConfigurationList) Swap(a, b int) {
  50. l[a], l[b] = l[b], l[a]
  51. }
  52. func (l DeviceConfigurationList) Len() int {
  53. return len(l)
  54. }