Bugfix for debug prompt
[theonering] / src / avatars.py
1 from __future__ import with_statement
2
3 import os
4 import logging
5
6 import telepathy
7
8 import tp
9 import util.misc as misc_utils
10
11
12 _moduleLogger = logging.getLogger(__name__)
13
14
15 class AvatarsMixin(tp.server.ConnectionInterfaceAvatars):
16
17         __SELF_AVATAR = "tor_self"
18         __MOBILE_AVATAR = "tor_handset"
19         __LANDLINE_AVATAR = "tor_phone"
20         __OTHER_AVATAR = "tor_question"
21
22         __LOOKUP_PATHS = (
23                 "/opt/theonering/share",
24                 "/usr/share/theonering",
25                 os.path.join(os.path.dirname(__file__), "../support/icons"),
26         )
27
28         def __init__(self):
29                 tp.server.ConnectionInterfaceAvatars.__init__(self)
30                 self._avatarCache = {}
31
32                 self._implement_property_get(
33                         telepathy.interfaces.CONNECTION_INTERFACE_AVATARS,
34                         {
35                                 'SupportedAvatarMimeTypes': lambda: ("image/png", ),
36                                 'MinimumAvatarHeight': lambda: 32,
37                                 'MinimumAvatarWidth': lambda: 32,
38                                 'RecommendedAvatarHeight': lambda: 32,
39                                 'RecommendedAvatarWidth': lambda: 32,
40                                 'MaximumAvatarHeight': lambda: 32,
41                                 'MaximumAvatarWidth': lambda: 32,
42                                 'MaximumAvatarBytes': lambda: 500 * 1024,
43                         },
44                 )
45
46         @property
47         def session(self):
48                 """
49                 @abstract
50                 """
51                 raise NotImplementedError("Abstract property called")
52
53         def get_handle_by_id(self, handleType, handleId):
54                 """
55                 @abstract
56                 """
57                 raise NotImplementedError("Abstract function called")
58
59         @misc_utils.log_exception(_moduleLogger)
60         def GetAvatarRequirements(self):
61                 mime_types = ("image/png", )
62                 return (mime_types, 32, 32, 64, 64, 500 * 1024)
63
64         @misc_utils.log_exception(_moduleLogger)
65         def GetAvatarTokens(self, contacts):
66                 result = {}
67                 for handleid in contacts:
68                         imageName = self._select_avatar(handleid)
69                         result[handleid] = imageName
70                 return result
71
72         @misc_utils.log_exception(_moduleLogger)
73         def GetKnownAvatarTokens(self, contacts):
74                 result = {}
75                 for handleid in contacts:
76                         imageName = self._select_avatar(handleid)
77                         result[handleid] = imageName
78                 return result
79
80         @misc_utils.log_exception(_moduleLogger)
81         def RequestAvatar(self, contact):
82                 imageName = self._select_avatar(contact)
83                 image = self._get_avatar(imageName)
84                 return image, "image/png"
85
86         @misc_utils.log_exception(_moduleLogger)
87         def RequestAvatars(self, contacts):
88                 for handleid in contacts:
89                         imageName = self._select_avatar(handleid)
90                         image = self._get_avatar(imageName)
91                         self.AvatarRetrieved(handleid, imageName, image, "image/png")
92
93         @misc_utils.log_exception(_moduleLogger)
94         def SetAvatar(self, avatar, mime_type):
95                 raise telepathy.errors.PermissionDenied
96
97         @misc_utils.log_exception(_moduleLogger)
98         def ClearAvatar(self):
99                 pass
100
101         def _select_avatar(self, handleId):
102                 handle = self.get_handle_by_id(telepathy.HANDLE_TYPE_CONTACT, handleId)
103
104                 if handle == self.GetSelfHandle():
105                         imageName = self.__SELF_AVATAR
106                 else:
107                         accountNumber = misc_utils.normalize_number(self.session.backend.get_account_number())
108                         phoneType = self.session.addressbook.get_phone_type(handle.phoneNumber)
109                         if handle.phoneNumber == accountNumber:
110                                 imageName = self.__SELF_AVATAR
111                         elif phoneType in ("mobile", ):
112                                 imageName = self.__MOBILE_AVATAR
113                         elif phoneType in ("home", "work"):
114                                 imageName = self.__LANDLINE_AVATAR
115                         else:
116                                 imageName = self.__OTHER_AVATAR
117
118                 return imageName
119
120         def _get_avatar(self, imageName):
121                 try:
122                         return self._avatarCache[imageName]
123                 except KeyError:
124                         image = self._load_avatar(imageName)
125                         self._avatarCache[imageName] = image
126                         return image
127
128         def _load_avatar(self, imageName):
129                 _moduleLogger.debug("Loading avatar %r from file" % (imageName, ))
130                 try:
131                         with open(os.sep.join([self.__LOOKUP_PATHS[0], imageName+".png"]), "rb") as f:
132                                 return f.read()
133                 except IOError:
134                         with open(os.sep.join([self.__LOOKUP_PATHS[1], "32-"+imageName+".png"]), "rb") as f:
135                                 return f.read()