2018-09-05 02:59:34 +02:00
|
|
|
# Copyright (C) 2009-2014 Yann Leboulanger <asterix AT lagaule.org>
|
|
|
|
#
|
|
|
|
# This file is part of Gajim.
|
|
|
|
#
|
|
|
|
# Gajim 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; version 3 only.
|
|
|
|
#
|
|
|
|
# Gajim 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 Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2009-12-07 23:16:30 +01:00
|
|
|
|
2017-12-03 14:59:52 +01:00
|
|
|
import logging
|
2018-10-26 01:45:43 +02:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from gi.repository import GLib
|
2019-02-02 00:46:23 +01:00
|
|
|
from nbxmpp.structs import LocationData
|
2010-04-29 21:20:07 +02:00
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2017-12-03 14:59:52 +01:00
|
|
|
|
2018-10-26 01:49:15 +02:00
|
|
|
if app.is_installed('GEOCLUE'):
|
|
|
|
from gi.repository import Geoclue
|
2017-12-03 14:59:52 +01:00
|
|
|
|
2018-10-26 01:19:47 +02:00
|
|
|
log = logging.getLogger('gajim.c.dbus.location')
|
2017-12-03 14:59:52 +01:00
|
|
|
|
2009-12-07 23:16:30 +01:00
|
|
|
|
|
|
|
class LocationListener:
|
2010-02-08 15:08:40 +01:00
|
|
|
_instance = None
|
2017-12-03 14:59:52 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
@classmethod
|
|
|
|
def get(cls):
|
|
|
|
if cls._instance is None:
|
|
|
|
cls._instance = cls()
|
|
|
|
return cls._instance
|
2009-12-07 23:16:30 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self):
|
|
|
|
self._data = {}
|
2009-12-07 23:16:30 +01:00
|
|
|
|
2018-01-19 01:50:11 +01:00
|
|
|
# Note: do not remove third parameter `paramSpec`
|
|
|
|
# because notify signal expects three parameters
|
|
|
|
def _on_location_update(self, simple, paramSpec=None):
|
2017-12-03 14:59:52 +01:00
|
|
|
location = simple.get_location()
|
|
|
|
timestamp = location.get_property("timestamp")[0]
|
|
|
|
lat = location.get_property("latitude")
|
|
|
|
lon = location.get_property("longitude")
|
|
|
|
alt = location.get_property("altitude")
|
|
|
|
# in XEP-0080 it's horizontal accuracy
|
|
|
|
acc = location.get_property("accuracy")
|
|
|
|
|
|
|
|
# update data with info we just received
|
|
|
|
self._data = {'lat': lat, 'lon': lon, 'alt': alt, 'accuracy': acc}
|
|
|
|
self._data['timestamp'] = self._timestamp_to_utc(timestamp)
|
|
|
|
self._send_location()
|
|
|
|
|
|
|
|
def _on_simple_ready(self, obj, result):
|
2010-04-24 09:21:09 +02:00
|
|
|
try:
|
2017-12-03 14:59:52 +01:00
|
|
|
self.simple = Geoclue.Simple.new_finish(result)
|
|
|
|
except GLib.Error as e:
|
|
|
|
if e.domain == 'g-dbus-error-quark':
|
|
|
|
log.warning("Could not enable geolocation: %s", e.message)
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
self.simple.connect('notify::location', self._on_location_update)
|
|
|
|
self._on_location_update(self.simple)
|
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
Geoclue.Simple.new("org.gajim.Gajim",
|
|
|
|
Geoclue.AccuracyLevel.EXACT,
|
|
|
|
None,
|
|
|
|
self._on_simple_ready)
|
2009-12-07 23:16:30 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def start(self):
|
2010-04-29 23:27:34 +02:00
|
|
|
self.location_info = {}
|
2010-02-08 15:08:40 +01:00
|
|
|
self.get_data()
|
2009-12-07 23:16:30 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def _send_location(self):
|
2017-08-13 13:18:56 +02:00
|
|
|
accounts = app.connections.keys()
|
2010-02-08 15:08:40 +01:00
|
|
|
for acct in accounts:
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.account_is_connected(acct):
|
2010-02-08 15:08:40 +01:00
|
|
|
continue
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.config.get_per('accounts', acct, 'publish_location'):
|
2010-02-08 15:08:40 +01:00
|
|
|
continue
|
2010-04-29 23:27:34 +02:00
|
|
|
if self.location_info == self._data:
|
2010-02-08 15:08:40 +01:00
|
|
|
continue
|
2010-05-02 11:10:42 +02:00
|
|
|
if 'timestamp' in self.location_info and 'timestamp' in self._data:
|
|
|
|
last_data = self.location_info.copy()
|
|
|
|
del last_data['timestamp']
|
|
|
|
new_data = self._data.copy()
|
|
|
|
del new_data['timestamp']
|
|
|
|
if last_data == new_data:
|
|
|
|
continue
|
2019-02-02 00:46:23 +01:00
|
|
|
app.connections[acct].get_module('UserLocation').set_location(
|
|
|
|
LocationData(**self._data))
|
2010-04-29 23:27:34 +02:00
|
|
|
self.location_info = self._data.copy()
|
2009-12-07 23:40:28 +01:00
|
|
|
|
2010-04-29 21:20:07 +02:00
|
|
|
def _timestamp_to_utc(self, timestamp):
|
|
|
|
time = datetime.utcfromtimestamp(timestamp)
|
|
|
|
return time.strftime('%Y-%m-%dT%H:%MZ')
|
|
|
|
|
2017-12-03 14:59:52 +01:00
|
|
|
|
2009-12-07 23:40:28 +01:00
|
|
|
def enable():
|
2018-10-26 01:45:43 +02:00
|
|
|
if not app.is_installed('GEOCLUE'):
|
|
|
|
log.warning('GeoClue not installed')
|
|
|
|
return
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
listener = LocationListener.get()
|
|
|
|
listener.start()
|