summaryrefslogtreecommitdiff
path: root/vendor/github.com/gsterjov/go-libsecret/collection.go
blob: a835ae527f4f4841289951994ea249259e9b6b7a (plain)
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
package libsecret

import "github.com/godbus/dbus"


type Collection struct {
  conn *dbus.Conn
  dbus  dbus.BusObject
}


func NewCollection(conn *dbus.Conn, path dbus.ObjectPath) *Collection {
  return &Collection{
    conn: conn,
    dbus: conn.Object(DBusServiceName, path),
  }
}


func (collection Collection) Path() dbus.ObjectPath {
  return collection.dbus.Path()
}


// READ Array<ObjectPath> Items;
func (collection *Collection) Items() ([]Item, error) {
  val, err := collection.dbus.GetProperty("org.freedesktop.Secret.Collection.Items")
  if err != nil {
    return []Item{}, err
  }

  items := []Item{}
  for _, path := range val.Value().([]dbus.ObjectPath) {
    items = append(items, *NewItem(collection.conn, path))
  }

  return items, nil
}


// Delete (OUT ObjectPath prompt);
func (collection *Collection) Delete() error {
  var prompt dbus.ObjectPath

  err := collection.dbus.Call("org.freedesktop.Secret.Collection.Delete", 0).Store(&prompt)
  if err != nil {
    return err
  }

  if isPrompt(prompt) {
    prompt := NewPrompt(collection.conn, prompt)

    _, err := prompt.Prompt()
    if err != nil {
      return err
    }
  }

  return nil
}


// SearchItems (IN Dict<String,String> attributes, OUT Array<ObjectPath> results);
func (collection *Collection) SearchItems(profile string) ([]Item, error) {
  attributes := make(map[string]string)
  attributes["profile"] = profile

  var paths []dbus.ObjectPath

  err := collection.dbus.Call("org.freedesktop.Secret.Collection.SearchItems", 0, attributes).Store(&paths)
  if err != nil {
    return []Item{}, err
  }

  items := []Item{}
  for _, path := range paths {
    items = append(items, *NewItem(collection.conn, path))
  }

  return items, nil
}


// CreateItem (IN Dict<String,Variant> properties, IN Secret secret, IN Boolean replace, OUT ObjectPath item, OUT ObjectPath prompt);
func (collection *Collection) CreateItem(label string, secret *Secret, replace bool) (*Item, error) {
  properties := make(map[string]dbus.Variant)
  attributes := make(map[string]string)

  attributes["profile"] = label
  properties["org.freedesktop.Secret.Item.Label"] = dbus.MakeVariant(label)
  properties["org.freedesktop.Secret.Item.Attributes"] = dbus.MakeVariant(attributes)

  var path   dbus.ObjectPath
  var prompt dbus.ObjectPath

  err := collection.dbus.Call("org.freedesktop.Secret.Collection.CreateItem", 0, properties, secret, replace).Store(&path, &prompt)
  if err != nil {
    return &Item{}, err
  }

  if isPrompt(prompt) {
    prompt := NewPrompt(collection.conn, prompt)

    result, err := prompt.Prompt()
    if err != nil {
      return &Item{}, err
    }

    path = result.Value().(dbus.ObjectPath)
  }

  return NewItem(collection.conn, path), nil
}


// READ Boolean Locked;
func (collection *Collection) Locked() (bool, error) {
  val, err := collection.dbus.GetProperty("org.freedesktop.Secret.Collection.Locked")
  if err != nil {
    return true, err
  }

  return val.Value().(bool), nil
}