summaryrefslogtreecommitdiff
path: root/vendor/github.com/gsterjov/go-libsecret/prompt.go
blob: 2c7d3f8f57b0f82c94c819d266ffb1857bef5038 (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
package libsecret

import (
  "github.com/godbus/dbus"
  "strings"
)


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


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


func (prompt Prompt) Path() dbus.ObjectPath {
  return prompt.dbus.Path()
}


func isPrompt(path dbus.ObjectPath) bool {
  promptPath := DBusPath + "/prompt/"
  return strings.HasPrefix(string(path), promptPath)
}


// Prompt (IN String window-id);
func (prompt *Prompt) Prompt() (*dbus.Variant, error) {
  // prompts are asynchronous so we connect to the signal
  // and block with a channel until we get a response
  c := make(chan *dbus.Signal, 10)
  defer close(c)

  prompt.conn.Signal(c)
  defer prompt.conn.RemoveSignal(c)

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

  for {
    if result := <-c; result.Path == prompt.Path() {
      value := result.Body[1].(dbus.Variant)
      return &value, nil
    }
  }
}