File: Port-to-geoclue-2.0.patch

package info (click to toggle)
qreator 16.06.1-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,764 kB
  • sloc: python: 1,833; xml: 18; makefile: 6
file content (122 lines) | stat: -rw-r--r-- 4,661 bytes parent folder | download | duplicates (2)
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
From: Chow Loong Jin <hyperair@debian.org>
Date: Sun, 15 Jan 2017 23:45:17 +0800
Subject: Port to geoclue-2.0

geoclue has been removed from Debian.
---
 qreator/qrcodes/QRCodeLocationGtk.py | 81 ++++++++++++++++--------------------
 1 file changed, 35 insertions(+), 46 deletions(-)

diff --git a/qreator/qrcodes/QRCodeLocationGtk.py b/qreator/qrcodes/QRCodeLocationGtk.py
index 9adb634..0ec70c3 100644
--- a/qreator/qrcodes/QRCodeLocationGtk.py
+++ b/qreator/qrcodes/QRCodeLocationGtk.py
@@ -14,9 +14,12 @@
 # with this program.  If not, see <http://www.gnu.org/licenses/>.
 ### END LICENSE
 
-from gi.repository import Gtk, GtkChamplain, Clutter, Champlain
+import gi
+
+gi.require_version('Geoclue', '2.0')
+from gi.repository import (
+    Geoclue, Gio, Gtk, GtkChamplain, GtkClutter, Clutter, Champlain)
 from qreator_lib.helpers import get_data_file
-from gi.repository import GtkClutter
 
 
 class QRCodeLocationGtk(object):
@@ -46,17 +49,14 @@ class QRCodeLocationGtk(object):
 
         # Get the current location, center the map on it, and initialize
         # other map features
-        latitude, longitude = get_current_location()
-        self.builder.get_object('lat_entry').set_text(str(latitude))
-        self.builder.get_object('lon_entry').set_text(str(longitude))
-        self.map_view.center_on(latitude, longitude)
-        if latitude == 0 and longitude == 0:
-            # In case something went wrong in getting the current location
-            self.map_view.set_zoom_level(1)
-        else:
-            self.map_view.set_zoom_level(15)
+        self.builder.get_object('lat_entry').set_text('0')
+        self.builder.get_object('lon_entry').set_text('0')
+        self.map_view.center_on(0.0, 0.0)
+        self.map_view.set_zoom_level(1)
         self.map_view.set_kinetic_mode(True)
 
+        get_current_location(self.on_location_obtained)
+
         scale = Champlain.Scale()
         scale.connect_view(self.map_view)
         self.map_view.bin_layout_add(scale, Clutter.BinAlignment.START,
@@ -64,6 +64,16 @@ class QRCodeLocationGtk(object):
 
         self.grid.show_all()
 
+    def on_location_obtained(self, latitude, longitude):
+        self.builder.get_object('lat_entry').set_text(str(latitude))
+        self.builder.get_object('lon_entry').set_text(str(longitude))
+        self.map_view.center_on(latitude, longitude)
+        if latitude == 0 and longitude == 0:
+            # In case something went wrong in getting the current location
+            self.map_view.set_zoom_level(1)
+        else:
+            self.map_view.set_zoom_level(15)
+
     def on_activated(self):
         pass
 
@@ -82,38 +92,17 @@ class QRCodeLocationGtk(object):
         return True
 
 
-def get_current_location():
-    '''Gets the current location from geolocation via IP (only method
-       currently supported)'''
-    #import Geoclue
-    #POS_PROVIDER = 'Ubuntu GeoIP'
-    #location = Geoclue.DiscoverLocation()
-    #location.init()
-    #location.set_position_provider(POS_PROVIDER)
-    #position = location.get_location_info()
-
-    import dbus
-    bus = dbus.SessionBus()
-
-    # For now we default to the UbuntuGeoIP provider and we fall back to
-    # Hostip. We should probably be cleverer about provider detection, but
-    # this solution works for now and does not rely solely on UbuntuGeoIP,
-    # which means qreator can run on other distros
-    try:
-        geoclue = bus.get_object(
-            'org.freedesktop.Geoclue.Providers.UbuntuGeoIP',
-            '/org/freedesktop/Geoclue/Providers/UbuntuGeoIP')
-    except dbus.exceptions.DBusException:
-        geoclue = bus.get_object(
-            'org.freedesktop.Geoclue.Providers.Hostip',
-            '/org/freedesktop/Geoclue/Providers/Hostip')
-    position_info = geoclue.GetPosition(
-        dbus_interface='org.freedesktop.Geoclue.Position')
-
-    position = {}
-    position['timestamp'] = position_info[1]
-    position['latitude'] = position_info[2]
-    position['longitude'] = position_info[3]
-    position['altitude'] = position_info[4]
-
-    return position['latitude'], position['longitude']
+def get_current_location(callback, cancellable=None):
+    '''Gets the current location from geolocation via using Geoclue-2.0'''
+    def finish_get_location(geoclue_simple, task):
+        location = geoclue_simple.get_location()
+
+        callback(location.get_property('latitude'),
+                 location.get_property('longitude'))
+
+    Geoclue.Simple.new(
+        desktop_id='qreator',
+        accuracy_level=Geoclue.AccuracyLevel.EXACT,
+        cancellable=cancellable,
+        callback=finish_get_location
+    )