Package common :: Module location_listener
[hide private]
[frames] | no frames]

Source Code for Module common.location_listener

  1  # -*- coding: utf-8 -*- 
  2  ## src/common/location_listener.py 
  3  ## 
  4  ## Copyright (C) 2009-2010 Yann Leboulanger <asterix AT lagaule.org> 
  5  ## 
  6  ## This file is part of Gajim. 
  7  ## 
  8  ## Gajim is free software; you can redistribute it and/or modify 
  9  ## it under the terms of the GNU General Public License as published 
 10  ## by the Free Software Foundation; version 3 only. 
 11  ## 
 12  ## Gajim is distributed in the hope that it will be useful, 
 13  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 15  ## GNU General Public License for more details. 
 16  ## 
 17  ## You should have received a copy of the GNU General Public License 
 18  ## along with Gajim. If not, see <http://www.gnu.org/licenses/>. 
 19  ## 
 20   
 21  from datetime import datetime 
 22   
 23  from common import gajim 
 24  from common import pep 
 25  from common import dbus_support 
 26  if dbus_support.supported: 
 27      import dbus 
 28      import dbus.glib 
29 30 -class LocationListener:
31 _instance = None 32 @classmethod
33 - def get(cls):
34 if cls._instance is None: 35 cls._instance = cls() 36 return cls._instance
37
38 - def __init__(self):
39 self._data = {}
40
41 - def get_data(self):
42 bus = dbus.SessionBus() 43 try: 44 # Initializes Geoclue. 45 obj = bus.get_object('org.freedesktop.Geoclue.Master', 46 '/org/freedesktop/Geoclue/Master') 47 # get MasterClient path 48 path = obj.Create() 49 # get MasterClient 50 cli = bus.get_object('org.freedesktop.Geoclue.Master', path) 51 cli.SetRequirements(1, 0, True, 1023) 52 53 self._get_address(cli) 54 self._get_position(cli) 55 except: 56 self._on_geoclue_position_changed() 57 return
58 59
60 - def _get_address(self, cli):
61 bus = dbus.SessionBus() 62 cli.AddressStart() 63 # Check that there is a provider 64 name, description, service, path = cli.GetAddressProvider() 65 if path: 66 provider = bus.get_object(service, path) 67 timestamp, address, accuracy = provider.GetAddress() 68 self._on_geoclue_address_changed(timestamp, address, accuracy)
69
70 - def _get_position(self, cli):
71 bus = dbus.SessionBus() 72 cli.PositionStart() 73 # Check that there is a provider 74 name, description, service, path = cli.GetPositionProvider() 75 if path: 76 provider = bus.get_object(service, path) 77 fields, timestamp, lat, lon, alt, accuracy = provider.GetPosition() 78 self._on_geoclue_position_changed(fields, timestamp, lat, lon, alt, 79 accuracy)
80
81 - def start(self):
82 self.location_info = {} 83 self.get_data() 84 bus = dbus.SessionBus() 85 # Geoclue 86 bus.add_signal_receiver(self._on_geoclue_address_changed, 87 'AddressChanged', 'org.freedesktop.Geoclue.Address') 88 bus.add_signal_receiver(self._on_geoclue_position_changed, 89 'PositionChanged', 'org.freedesktop.Geoclue.Position')
90
91 - def shut_down(self):
92 pass
93
94 - def _on_geoclue_address_changed(self, timestamp=None, address={}, 95 accuracy=None):
96 # update data with info we just received 97 for field in ['country', 'countrycode', 'locality', 'postalcode', 98 'region', 'street']: 99 self._data[field] = address.get(field, None) 100 if timestamp: 101 self._data['timestamp'] = self._timestamp_to_utc(timestamp) 102 if accuracy: 103 # in PEP it's horizontal accuracy 104 self._data['accuracy'] = accuracy[1] 105 self._send_location()
106
107 - def _on_geoclue_position_changed(self, fields=[], timestamp=None, lat=None, 108 lon=None, alt=None, accuracy=None):
109 # update data with info we just received 110 _dict = {'lat': lat, 'lon': lon, 'alt': alt} 111 for field in _dict: 112 if _dict[field] is not None: 113 self._data[field] = _dict[field] 114 if timestamp: 115 self._data['timestamp'] = self._timestamp_to_utc(timestamp) 116 if accuracy: 117 # in PEP it's horizontal accuracy 118 self._data['accuracy'] = accuracy[1] 119 self._send_location()
120
121 - def _send_location(self):
122 accounts = gajim.connections.keys() 123 for acct in accounts: 124 if not gajim.account_is_connected(acct): 125 continue 126 if not gajim.config.get_per('accounts', acct, 'publish_location'): 127 continue 128 if self.location_info == self._data: 129 continue 130 if 'timestamp' in self.location_info and 'timestamp' in self._data: 131 last_data = self.location_info.copy() 132 del last_data['timestamp'] 133 new_data = self._data.copy() 134 del new_data['timestamp'] 135 if last_data == new_data: 136 continue 137 gajim.connections[acct].send_location(self._data) 138 self.location_info = self._data.copy()
139
140 - def _timestamp_to_utc(self, timestamp):
141 time = datetime.utcfromtimestamp(timestamp) 142 return time.strftime('%Y-%m-%dT%H:%MZ')
143
144 -def enable():
145 listener = LocationListener.get() 146 listener.start()
147
148 -def disable():
149 listener = LocationListener.get() 150 listener.shut_down()
151