This file is indexed.

/usr/share/pyshared/Geoclue/Base.py is in python-geoclue 0.1.0-4build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# -*- coding: utf-8 -*-
# Copyright (c) 2009 - Paulo Cabido <paulo.cabido@gmail.com>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import sys
import math

import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gobject

import geoclue
from Signal import Signal

DBusGMainLoop(set_as_default=True)

class DiscoverLocation:
    """ Discovers the location form the best available provider

    L{DiscoverLocation} is a object that provides a nice API interface
    for Geoclue.
    """
    
    def __init__(self, providers_path="/usr/share/geoclue-providers"):
        """Construct a L{DiscoverLocation} object.
        
        @param providers_path: The path to the providers. The default
        path to the providers is /usr/share/geoclue-providers.
        """
        self.bus = dbus.SessionBus()
        
        self.signal = Signal()
        
        # stores the location info
        self.location_info = {}
        
        # Insipered by Pierre-Luc Beaudoin - geoclue_properties.py
        # TODO: add an exception to this part of the code in case of the wrong 
        # or nonexisting dir
        self.providers = []
        
        dir = os.listdir(providers_path)
        
        for filename in dir:
            (name, ext) = os.path.splitext(filename)

            if ext == ".provider":
                complete = os.path.join(providers_path, filename)
                provider = geoclue.GeoclueProvider (complete)
                self.providers.append([provider,
                  provider.name,
                  provider.interfaces & geoclue.INTERFACE_ADDRESS,
                  provider.interfaces & geoclue.INTERFACE_POSITION,
                  provider.interfaces & geoclue.INTERFACE_GEOCODE,
                  provider.interfaces & geoclue.INTERFACE_REVERSE_GEOCODE,
                  provider.service,
                  provider.path
                  ])
    
    def init(self, accuracy=geoclue.ACCURACY_LEVEL_COUNTRY, resource=geoclue.RESOURCE_NETWORK):
        """Initializes Geoclue.
        
        @param accuracy:  The desired accuracy.
        @param resource: The resource to be used.
        """
        self.accuracy = accuracy
        self.resource = resource
        
        try:
            self.master = self.bus.get_object(geoclue.MASTER_IFACE, geoclue.MASTER_PATH)
            self.client = self.bus.get_object(geoclue.MASTER_IFACE, self.master.Create())
            
            # connects to detect changes on the address and position providers
            self.client.connect_to_signal("AddressProviderChanged", self.on_address_provider_changed)
            self.client.connect_to_signal("PositionProviderChanged", self.on_position_provider_changed)
            
            self.address = dbus.Interface(self.client, dbus_interface=geoclue.ADDRESS_IFACE)
            self.address.connect_to_signal("AddressChanged", self.on_address_changed)
            self.client.AddressStart()
            
            self.position = dbus.Interface(self.client, dbus_interface=geoclue.POSITION_IFACE)
            self.position.connect_to_signal("PositionChanged", self.on_position_changed)
            self.client.PositionStart()
            
            self.client.SetRequirements(self.accuracy, 0, True, self.resource)
        
            try:
                self.on_address_changed(*self.address.GetAddress())
            except Exception, e:
                return False
                
            try:
                self.on_position_changed(*self.position.GetPosition())
            except Exception, e:
                return False
            
            return True
        except Exception, e:
            print "Error: %s" % e
            return False
       
    def provider_status(self, provider):
        """Checks a provider's status.
        
        @param provider: A provider instance.
        @return: The status.
        """
        obj = dbus.Interface(provider.get_proxy(), dbus_interface=geoclue.GEOCLUE_IFACE)
        status = obj.GetStatus()
        
        if status == geoclue.STATUS_ERROR:
            return "error"
        elif status == geoclue.STATUS_UNAVAILABLE:
            return "unavailable"
        elif status == geoclue.STATUS_ACQUIRING:
            return "acquiring"
        elif status == geoclue.STATUS_AVAILABLE:
            return "available"
        else:
            return "error"
    
    def provider_info(self, provider):
        """Returns the provider's Info.
        
        @return: A dictionary with the provider's name and descripiton.
        """
        obj = dbus.Interface(provider.get_proxy(), dbus_interface=geoclue.GEOCLUE_IFACE)
        info = obj.GetProviderInfo()
        tmp = {}
        tmp['name'] = str(info[0])
        tmp['description'] = str(info[1])
        return tmp

    def set_requirements(self, accuracy, time, require_updates, resource):
        """Set the client requirements.
        
        @param accuracy: The required minimum accuracy.
        @param time: The minimum time between update signals.
        @param require_updates: C{True} if updates are required or C{False} if updates 
        are not required.
        @param resource: The resources that are allowed to be used.
        """
        self.accuracy = accuracy
        self.resource = resource
        self.client.SetRequirements(accuracy, time, require_updates, allowed_resources)
            
    # provider changed methods, not really being used but it's useful to have 
    # them here just in case
    def on_address_provider_changed(self, name, description, service, path):
        #print "Address provider changed"
        pass
    
    def on_position_provider_changed(self, name, description, service, path):
        #print "Position provider changed"
        pass
    
    def update_location_address(self, address):
        """Updates the the location's address with the given C{address}.
        
        @address: The new address.
        """
        if address.has_key('street'):
            self.location_info['street'] = address['street']

        # TODO: postalcode ?
        
        if address.has_key('area'):
            self.locatio_infon['area'] = address['area']

        if address.has_key('locality'):
            self.location_info['locality'] = address['locality']

        if address.has_key('country'):
            self.location_info['country'] = address['country']

        if address.has_key('region'):
            self.location_info['region'] = address['region']
        
        if address.has_key('countrycode'):
            self.location_info['countrycode'] = address['countrycode']
    
    def on_address_changed(self, timestamp, address, accuracy):
        """When the address changes the location info dictionary is updated.
        
        @param timestamp: The timestamp.
        @param address: The new address.
        @accuracy: The accuracy.
        """
        self.location_info['address_timestamp'] = timestamp
        self.update_location_address(address)
        self.signal()
        
    def on_position_changed(self, fields, timestamp, latitude, longitude, altitude, accuracy):
        """When the position changes the location info dictionary is updated.
        
        @param fields: The fields.
        @param timestamp: The timestamp.
        @param latitude: The new latitude.
        @param longitude: The new longitude.
        @param altitude: The new altitude.
        @param accuracy: The accuracy.
        """
        #print accuracy # I used this print to check the accuracy format
        self.location_info['position_timestamp'] = timestamp
        self.location_info['latitude'] = latitude
        self.location_info['longitude'] = longitude
        self.location_info['altitude'] = altitude
        self.signal()

    # returns the current values for location and position
    def get_location_info(self):
        """Returns the location info dictionary.
        
        @return: The location info dictionary.
        """
        return self.location_info
    
    def get_available_providers(self):
        """Returns the available providers.
         
        @return: A list of dictionarys,
        [PROVIDER, ADDRESS, POSITION, GEOCODING, REVERSE GEOCODING],
        with the name and True of False for supporting each of them
        """ 
        current_providers = []
        for provider in self.providers:
            tmp = {}
            tmp['name'] = provider[1]
            if provider[2] != 0:
                tmp['address'] = True
            else:
                tmp['address'] = False
                
            if provider[3] != 0:
                tmp['position'] = True
            else:
                tmp['position'] = False
                
            if provider[4] != 0:
                tmp['geocoding'] = True
            else:
                tmp['geocoding'] = False
                
            if provider[5] != 0:
                tmp['revgeocoding'] = True
            else:
                tmp['revgeocoding'] = False
            
            tmp['object'] = provider[0]
            tmp['service'] = provider[6]
            tmp['path'] = provider[7]
                
            current_providers.append(tmp)    
        
        return current_providers
    
    def set_position_provider(self, provider_name):
        """Set the position provider to a given C{provider_name} (if exists).
        
        @param provider_name: The provider's name
        @return: C{True} if the provider exists or C{False} if a provider
        does not exist.
        """
        provider_exists = False
        for provider in self.providers:
            if provider[1].lower() == provider_name.lower() and provider[3] != 0:
                current_provider = provider
                provider_exists = True
                break
        
        if not provider_exists:
            return False
        
        try:
            tmp_provider = current_provider[0].get_proxy()
            self.position = dbus.Interface(tmp_provider, dbus_interface=geoclue.POSITION_IFACE)
        except Exception, e:
            print "D-Bus error: %s" % e
            return False
            
        try:
            self.on_position_changed(*self.position.GetPosition())
        except Exception, e:
            print e
            
        return True
    
    def validate_address(self, address):
        """Receives the address and validates/corrects it.
        
        @param address: The address dictionary.
        @return: The address (with possible corrections).
        """
        tmp_address = {}
        if address.has_key('street'):
            tmp_address['street'] = address['street']
        else:
            tmp_address['street'] = ""
        
        # TODO: postalcode ?
        
        if address.has_key('area'):
                tmp_address['area'] = address['area']
        else:
            tmp_address['area'] = ""
        
        if address.has_key('locality'):
                tmp_address['locality'] = address['locality']
        else:
            tmp_address['locality'] = ""
        
        if address.has_key('country'):
            tmp_address['country'] = address['country']
        else:
            tmp_address['country'] = ""
            
        if address.has_key('region'):
            tmp_address['region'] = address['region']
        else:
            tmp_address['region'] = ""
            
        if address.has_key('countrycode'):
            tmp_address['countrycode'] = address['countrycode']
        else:
            tmp_address['countrycode'] = ""
            
        return tmp_address
    
    # TODO: add "valid-for" to continue to use the given provider
    def set_address_provider(self, provider_name, address=None):
        """Set the address provider
        
        @param provider_name: the provider's name
        @param address: the new address (for Manual and Localnet providers)
        @returns:  C{True} if the provider exists or C{False} if a provider
        does not exist.
        """
        provider_exists = False
        for provider in self.providers:
            if provider[1].lower() == provider_name.lower() and provider[2] != 0:
                current_provider = provider
                provider_exists = True
                break
            
        if not provider_exists:
            return False
        
        try:
            if (provider_name.lower() == "manual" or provider_name.lower() == "localnet") and address != None:
                tmp_provider = current_provider[0].get_proxy()
                tmp_provider.SetAddress(0, self.validate_address(address))
                self.address = dbus.Interface(tmp_provider, dbus_interface=geoclue.ADDRESS_IFACE)
            elif (provider_name.lower() == "manual" or provider_name.lower() == "localnet") and address == None:
                return False
            else:
                self.address = dbus.Interface(current_provider[0].get_proxy(), dbus_interface=geoclue.ADDRESS_IFACE)
        except Exception, e:
            print "D-Bus error: %s" % e
            return False
            
        try:
            self.on_address_changed(*self.address.GetAddress())
        except Exception, e:
            print e
            
        return True
    
    def get_position_provider(self):
        """Returns the name of the current position provider.
        
        @return: The name of the current position provider.
        """
        return self.client.GetPositionProvider()[0]
    
    def get_address_provider(self):
        """Returns the name of the current address provider.
        
        @return: The name of the current address provider.
        """
        return self.client.GetAddressProvider()[0]
    
    def compare_position(self, latitude, longitude, proximity_factor=None):
        """Compare the current position to a given position.
        
        Note: ploum's contribution
        
        @param latitude: latitude of the position
        @param longitude: longitude of the position
        @param proximity_factor: the near by proximity factor. ie, 0.5 is 500 meters
        """
        if proximity_factor == None:
            # 500 meters
            dis_max = 0.5
        else:
            dis_max = proximity_factor
        
        # todo_later : (calibration must be done with a well known distance )
        #This method assumes a spheroidal model for the earth with 
        #an average radius of 6364.963 km. 
        #The formula is estimated to have an accuracy of about 200 metres 
        #over 50 km, but may deteriorate with longer distances.
        #source : http://www.ga.gov.au/geodesy/datums/distance.jsp
        dl = self.location_info['latitude'] - latitude
        dg = self.location_info['longitude'] - longitude
        term1 = 111.08956 * (dl + 0.000001)
        term2 = math.cos(self.location_info['latitude'] + (dl/2))
        term3 = (dg + 0.000001) / (dl + 0.000001)
        d = term1 / math.cos(math.atan(term2 * term3))
        result =  math.fabs(d) < dis_max 
        return result
    
    def reverse_position(self, latitude, longitude, accuracy):
        """Returns an address that corresponds to a given position.
        
        @param latitude: The position's latitude.  
        @param longitude: The position's longitude.
        @param accuracy: The accuracy.
        @return: An address.
        """
        provider_exists = False
        for provider in self.providers:
            if provider[1].lower() == "Geonames Provider".lower():
                current_provider = provider
                provider_exists = True
                break
            
        if not provider_exists:
            return None
        
        try:
            revgeocoder = dbus.Interface(current_provider[0].get_proxy(), geoclue.REVERSE_IFACE)
            revaddress = revgeocoder.PositionToAddress(float(latitude), float(longitude), (accuracy, 0, 0))
            
            #add the values to the address of the location variable
            tmp_address = {}
            for key, item in revaddress[0].items():
                tmp_address[unicode(key)] = unicode(item)
            
            return self.validate_address(tmp_address)
        except Exception, e:
            print "D-Bus error: %s" % e
            return None
    
    def connect(self, func):
        """Connects a given function to the signal.
        The signal action if any change to the info location dictionary.
        
        @param func: The function to connect to the signal.
        """
        self.signal.connect(func)
    
    def disconnect(self, func):
        """Disconnects a given function from the signal.
        
        @param func: The function to disconnect from the signal.
        """
        self.signal.disconnect(func)