Only auto-unread for conversations
[theonering] / src / location.py
1 import logging
2
3 import telepathy
4
5 import util.misc as misc_utils
6 import handle
7
8
9 _moduleLogger = logging.getLogger(__name__)
10
11
12 #class LocationMixin(tp.ConnectionInterfaceLocation):
13 class LocationMixin(object):
14
15         def __init__(self):
16                 #tp.ConnectionInterfaceLocation.__init__(self)
17                 pass
18
19         @property
20         def session(self):
21                 """
22                 @abstract
23                 """
24                 raise NotImplementedError("Abstract property called")
25
26         @misc_utils.log_exception(_moduleLogger)
27         def GetLocations(self, contacts):
28                 """
29                 @returns {Contact: {Location Type: Location}}
30                 """
31                 contactLocation = (
32                         (contact, self._get_location(contact))
33                         for contact in contacts
34                 )
35                 return dict(
36                         (contact, location)
37                         for (contact, location) in contactLocation
38                         if location
39                 )
40
41         @misc_utils.log_exception(_moduleLogger)
42         def RequestLocation(self, contact):
43                 """
44                 @returns {Location Type: Location}
45                 """
46                 return self._get_location(contact)
47
48         @misc_utils.log_exception(_moduleLogger)
49         def SetLocation(self, location):
50                 """
51                 Since presence is based off of phone numbers, not allowing the client to change it
52                 """
53                 raise telepathy.errors.PermissionDenied()
54
55         def _get_location(self, contact):
56                 h = self.get_handle_by_id(telepathy.HANDLE_TYPE_CONTACT, contact)
57                 if isinstance(h, handle.ConnectionHandle):
58                         number = self.session.backend.get_callback_number()
59                 else:
60                         number = h.phoneNumber
61
62                 rawData = self.session.location.request_location(number)
63                 if rawData is None:
64                         return {}
65
66                 data = {
67                         "country": rawData["country"],
68                         "city": rawData["city"],
69                         "region": rawData["region"],
70                 }