|
| 1 | +package geoclue2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "github.com/godbus/dbus/v5" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// Paths of methods and properties |
| 12 | +const ( |
| 13 | + GeoclueClientInterface = GeoclueInterface + ".Client" |
| 14 | + GeoclueClientObjectPath = GeoclueObjectPath + "/Client" |
| 15 | + |
| 16 | + /* Methods */ |
| 17 | + GeoclueClientStart = GeoclueClientInterface + ".Start" |
| 18 | + GeoclueClientStop = GeoclueClientInterface + ".Stop" |
| 19 | + |
| 20 | + /* Property */ |
| 21 | + GeoclueClientPropertyLocation = GeoclueClientInterface + ".Location" // readable o |
| 22 | + GeoclueClientPropertyDistanceThreshold = GeoclueClientInterface + ".DistanceThreshold" // readwrite u |
| 23 | + GeoclueClientPropertyTimeThreshold = GeoclueClientInterface + ".TimeThreshold" // readwrite u |
| 24 | + GeoclueClientPropertyDesktopId = GeoclueClientInterface + ".DesktopId" // readwrite s |
| 25 | + GeoclueClientPropertyRequestedAccuracyLevel = GeoclueClientInterface + ".RequestedAccuracyLevel" // readwrite u |
| 26 | + GeoclueClientPropertyActive = GeoclueClientInterface + ".Active" // readable b |
| 27 | + |
| 28 | + /* SIGNAL */ |
| 29 | + GeoclueClientSignalLocationUpdated = "LocationUpdated" |
| 30 | +) |
| 31 | + |
| 32 | +// This is the interface you use to retrieve location information and receive location update signals from GeoClue service. |
| 33 | +// You get the client object to use this interface on from org.freedesktop.GeoClue2.Manager.GetClient() method. |
| 34 | +type GeoclueClient interface { |
| 35 | + /* METHODS */ |
| 36 | + |
| 37 | + // Start receiving events about the current location. Applications should hook-up to |
| 38 | + // LocationUpdated" signal before calling this method. |
| 39 | + Start() error |
| 40 | + |
| 41 | + // Stop receiving events about the current location. |
| 42 | + Stop() error |
| 43 | + |
| 44 | + /* PROPERTIES */ |
| 45 | + |
| 46 | + // Current location as path to a org.freedesktop.Geoclue2.Location object. Please note that this property will |
| 47 | + // be set to "/" (D-Bus equivalent of null) initially, until Geoclue finds user's location. |
| 48 | + // You want to delay reading this property until your callback to "LocationUpdated" signal is |
| 49 | + // called for the first time after starting the client. |
| 50 | + GetLocation() (GeoclueLocation, error) |
| 51 | + |
| 52 | + // Contains the current distance threshold in meters. This value is used by the service when it gets new location info. |
| 53 | + // If the distance moved is below the threshold, it won't emit the LocationUpdated signal. The default value is 0. |
| 54 | + // When TimeThreshold is zero, it always emits the signal. |
| 55 | + GetDistanceThreshold() (uint32, error) |
| 56 | + SetDistanceThreshold(uint32) error |
| 57 | + |
| 58 | + // Contains the current time threshold in seconds. This value is used by the service when it gets new location info. |
| 59 | + // If the time since the last update is below the threshold, it won't emit the LocationUpdated signal. |
| 60 | + // The default value is 0. When TimeThreshold is zero, it always emits the signal. |
| 61 | + GetTimeThreshold() (uint32, error) |
| 62 | + SetTimeThreshold(uint32) error |
| 63 | + |
| 64 | + // The desktop file id (the basename of the desktop file). |
| 65 | + // This property must be set by applications for authorization to work. |
| 66 | + // e.g. firefox |
| 67 | + GetDesktopId() (string, error) |
| 68 | + SetDesktopId(string) error |
| 69 | + |
| 70 | + // The level of accuracy requested by client, as GClueAccuracyLevel. |
| 71 | + // Please keep in mind that the actual accuracy of location information is dependent on available hardware on |
| 72 | + // your machine, external resources and/or how much accuracy user agrees to be confortable with. |
| 73 | + GetRequestedAccuracyLevel() (GClueAccuracyLevel, error) |
| 74 | + SetRequestedAccuracyLevel(level GClueAccuracyLevel) error |
| 75 | + |
| 76 | + // If client is active, i-e started successfully using Start() and receiving location updates. |
| 77 | + // Please keep in mind that geoclue can at any time stop and start the client on user (agent) request. |
| 78 | + // Applications that are interested in in these changes, should watch for changes in this property. |
| 79 | + IsActive() (bool, error) |
| 80 | + |
| 81 | + MarshalJSON() ([]byte, error) |
| 82 | + |
| 83 | + /* SIGNALS */ |
| 84 | + // todo |
| 85 | + // LocationUpdated signal: The signal is emitted every time the location changes. The client should set the |
| 86 | + // DistanceThreshold property to control how often this signal is emitted. |
| 87 | + // o old: old location as path to a #org.freedesktop.Geoclue2.Location object |
| 88 | + // o new: new location as path to a #org.freedesktop.Geoclue2.Location object |
| 89 | + SubscribeLocationUpdated() <-chan *dbus.Signal |
| 90 | + // Parse the signal and return the old and new Location |
| 91 | + ParseLocationUpdated(v *dbus.Signal) (oldLocation GeoclueLocation, newLocation GeoclueLocation, err error) |
| 92 | + |
| 93 | + Unsubscribe() |
| 94 | +} |
| 95 | + |
| 96 | +// NewGeoclueClient returns new GeoclueClient Interface |
| 97 | +func NewGeoclueClient(objectPath dbus.ObjectPath) (GeoclueClient, error) { |
| 98 | + var gcc geoclueClient |
| 99 | + return &gcc, gcc.init(GeoclueInterface, objectPath) |
| 100 | +} |
| 101 | + |
| 102 | +type geoclueClient struct { |
| 103 | + dbusBase |
| 104 | + sigChan chan *dbus.Signal |
| 105 | +} |
| 106 | + |
| 107 | +func (gcc geoclueClient) Start() error { |
| 108 | + err := gcc.call(GeoclueClientStart) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + return err |
| 113 | +} |
| 114 | + |
| 115 | +func (gcc geoclueClient) Stop() error { |
| 116 | + err := gcc.call(GeoclueClientStop) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + return err |
| 121 | +} |
| 122 | + |
| 123 | +func (gcc geoclueClient) GetLocation() (GeoclueLocation, error) { |
| 124 | + var objPath dbus.ObjectPath |
| 125 | + |
| 126 | + cActive, err := gcc.IsActive() |
| 127 | + if !cActive { |
| 128 | + return nil, errors.New("client must be started before gathering the location") |
| 129 | + } |
| 130 | + |
| 131 | + timeout := time.After(5 * time.Second) |
| 132 | + // Please note that this property will be set to "/" (D-Bus equivalent of null) initially, |
| 133 | + // until Geoclue finds user's location. You want to delay reading this property until |
| 134 | + // your callback to "LocationUpdated" signal is called for the first time after starting the client. |
| 135 | + c := gcc.SubscribeLocationUpdated() |
| 136 | + select { |
| 137 | + case <-timeout: |
| 138 | + return nil, errors.New("timed out gathering location object") |
| 139 | + case <-c: |
| 140 | + objPath, err = gcc.getObjectProperty(GeoclueClientPropertyLocation) |
| 141 | + if err != nil { |
| 142 | + return nil, err |
| 143 | + } |
| 144 | + if len(fmt.Sprint(objPath)) > 1 { |
| 145 | + break |
| 146 | + } |
| 147 | + } |
| 148 | + gcc.Unsubscribe() |
| 149 | + |
| 150 | + return NewGeoclueLocation(objPath) |
| 151 | +} |
| 152 | + |
| 153 | +func (gcc geoclueClient) GetDistanceThreshold() (uint32, error) { |
| 154 | + v, err := gcc.getUint32Property(GeoclueClientPropertyDistanceThreshold) |
| 155 | + return v, err |
| 156 | +} |
| 157 | + |
| 158 | +func (gcc geoclueClient) SetDistanceThreshold(value uint32) error { |
| 159 | + return gcc.setProperty(GeoclueClientPropertyDistanceThreshold, value) |
| 160 | + |
| 161 | +} |
| 162 | + |
| 163 | +func (gcc geoclueClient) GetTimeThreshold() (uint32, error) { |
| 164 | + v, err := gcc.getUint32Property(GeoclueClientPropertyTimeThreshold) |
| 165 | + return v, err |
| 166 | +} |
| 167 | + |
| 168 | +func (gcc geoclueClient) SetTimeThreshold(value uint32) error { |
| 169 | + return gcc.setProperty(GeoclueClientPropertyTimeThreshold, value) |
| 170 | +} |
| 171 | + |
| 172 | +func (gcc geoclueClient) GetDesktopId() (string, error) { |
| 173 | + v, err := gcc.getStringProperty(GeoclueClientPropertyDesktopId) |
| 174 | + return v, err |
| 175 | +} |
| 176 | + |
| 177 | +func (gcc geoclueClient) SetDesktopId(value string) error { |
| 178 | + return gcc.setProperty(GeoclueClientPropertyDesktopId, value) |
| 179 | +} |
| 180 | + |
| 181 | +func (gcc geoclueClient) GetRequestedAccuracyLevel() (GClueAccuracyLevel, error) { |
| 182 | + v, err := gcc.getUint32Property(GeoclueClientPropertyRequestedAccuracyLevel) |
| 183 | + return GClueAccuracyLevel(v), err |
| 184 | +} |
| 185 | + |
| 186 | +func (gcc geoclueClient) SetRequestedAccuracyLevel(level GClueAccuracyLevel) error { |
| 187 | + return gcc.setProperty(GeoclueClientPropertyRequestedAccuracyLevel, level) |
| 188 | +} |
| 189 | + |
| 190 | +func (gcc geoclueClient) IsActive() (bool, error) { |
| 191 | + v, err := gcc.getBoolProperty(GeoclueClientPropertyActive) |
| 192 | + return v, err |
| 193 | +} |
| 194 | + |
| 195 | +func (gcc geoclueClient) SubscribeLocationUpdated() <-chan *dbus.Signal { |
| 196 | + if gcc.sigChan != nil { |
| 197 | + return gcc.sigChan |
| 198 | + } |
| 199 | + rule := fmt.Sprintf("type='signal', member='%s',path_namespace='%s'", GeoclueClientSignalLocationUpdated, GeoclueClientObjectPath) |
| 200 | + gcc.conn.BusObject().Call(dbusMethodAddMatch, 0, rule) |
| 201 | + gcc.sigChan = make(chan *dbus.Signal, 10) |
| 202 | + gcc.conn.Signal(gcc.sigChan) |
| 203 | + return gcc.sigChan |
| 204 | +} |
| 205 | +func (gcc geoclueClient) ParseLocationUpdated(v *dbus.Signal) (oldLocation GeoclueLocation, newLocation GeoclueLocation, err error) { |
| 206 | + if len(v.Body) != 2 { |
| 207 | + err = errors.New("error by parsing activation changed signal") |
| 208 | + return |
| 209 | + } |
| 210 | + oPath, ok := v.Body[0].(dbus.ObjectPath) |
| 211 | + if !ok { |
| 212 | + err = errors.New("error by parsing old object path") |
| 213 | + return |
| 214 | + } |
| 215 | + oldLocation, err = NewGeoclueLocation(oPath) |
| 216 | + if err != nil { |
| 217 | + return |
| 218 | + } |
| 219 | + nPath, ok := v.Body[1].(dbus.ObjectPath) |
| 220 | + if !ok { |
| 221 | + err = errors.New("error by parsing new object path") |
| 222 | + return |
| 223 | + } |
| 224 | + newLocation, err = NewGeoclueLocation(nPath) |
| 225 | + if err != nil { |
| 226 | + return |
| 227 | + } |
| 228 | + return |
| 229 | +} |
| 230 | + |
| 231 | +func (gcc geoclueClient) Unsubscribe() { |
| 232 | + gcc.conn.RemoveSignal(gcc.sigChan) |
| 233 | + gcc.sigChan = nil |
| 234 | +} |
| 235 | + |
| 236 | +func (gcc geoclueClient) MarshalJSON() ([]byte, error) { |
| 237 | + location, err := gcc.GetLocation() |
| 238 | + if err != nil { |
| 239 | + return nil, err |
| 240 | + } |
| 241 | + mLocation, err := location.MarshalJSON() |
| 242 | + if err != nil { |
| 243 | + return nil, err |
| 244 | + } |
| 245 | + distanceThreshold, err := gcc.GetDistanceThreshold() |
| 246 | + if err != nil { |
| 247 | + return nil, err |
| 248 | + } |
| 249 | + |
| 250 | + timeThreshold, err := gcc.GetTimeThreshold() |
| 251 | + if err != nil { |
| 252 | + return nil, err |
| 253 | + } |
| 254 | + desktopId, err := gcc.GetDesktopId() |
| 255 | + if err != nil { |
| 256 | + return nil, err |
| 257 | + } |
| 258 | + requestedAccuracyLevel, err := gcc.GetRequestedAccuracyLevel() |
| 259 | + if err != nil { |
| 260 | + return nil, err |
| 261 | + } |
| 262 | + isActive, err := gcc.IsActive() |
| 263 | + if err != nil { |
| 264 | + return nil, err |
| 265 | + } |
| 266 | + |
| 267 | + return json.Marshal(map[string]interface{}{ |
| 268 | + "Location": mLocation, |
| 269 | + "DistanceThreshold ": distanceThreshold, |
| 270 | + "TimeThreshold": timeThreshold, |
| 271 | + "DesktopId": desktopId, |
| 272 | + "RequestedAccuracyLevel": requestedAccuracyLevel, |
| 273 | + "Active": isActive, |
| 274 | + }) |
| 275 | +} |
0 commit comments