1 from __future__ import with_statement
9 import util.misc as misc_utils
12 _moduleLogger = logging.getLogger(__name__)
15 class AvatarsMixin(tp.server.ConnectionInterfaceAvatars):
17 __SELF_AVATAR = "tor_self"
18 __MOBILE_AVATAR = "tor_handset"
19 __LANDLINE_AVATAR = "tor_phone"
20 __OTHER_AVATAR = "tor_question"
23 "/usr/share/theonering",
24 os.path.join(os.path.dirname(__file__), "../support/icons"),
28 tp.server.ConnectionInterfaceAvatars.__init__(self)
29 self._avatarCache = {}
31 self._implement_property_get(
32 telepathy.interfaces.CONNECTION_INTERFACE_AVATARS,
34 'SupportedAvatarMimeTypes': lambda: ("image/png", ),
35 'MinimumAvatarHeight': lambda: 32,
36 'MinimumAvatarWidth': lambda: 32,
37 'RecommendedAvatarHeight': lambda: 32,
38 'RecommendedAvatarWidth': lambda: 32,
39 'MaximumAvatarHeight': lambda: 32,
40 'MaximumAvatarWidth': lambda: 32,
41 'MaximumAvatarBytes': lambda: 500 * 1024,
50 raise NotImplementedError("Abstract property called")
52 def get_handle_by_id(self, handleType, handleId):
56 raise NotImplementedError("Abstract function called")
58 @misc_utils.log_exception(_moduleLogger)
59 def GetAvatarRequirements(self):
60 mime_types = ("image/png", )
61 return (mime_types, 32, 32, 64, 64, 500 * 1024)
63 @misc_utils.log_exception(_moduleLogger)
64 def GetAvatarTokens(self, contacts):
66 for handleid in contacts:
67 imageName = self._select_avatar(handleid)
68 result[handleid] = imageName
71 @misc_utils.log_exception(_moduleLogger)
72 def GetKnownAvatarTokens(self, contacts):
74 for handleid in contacts:
75 imageName = self._select_avatar(handleid)
76 result[handleid] = imageName
79 @misc_utils.log_exception(_moduleLogger)
80 def RequestAvatar(self, contact):
81 imageName = self._select_avatar(contact)
82 image = self._get_avatar(imageName)
83 return image, "image/png"
85 @misc_utils.log_exception(_moduleLogger)
86 def RequestAvatars(self, contacts):
87 for handleid in contacts:
88 imageName = self._select_avatar(handleid)
89 image = self._get_avatar(imageName)
90 self.AvatarRetrieved(handleid, imageName, image, "image/png")
92 @misc_utils.log_exception(_moduleLogger)
93 def SetAvatar(self, avatar, mime_type):
94 raise telepathy.errors.PermissionDenied
96 @misc_utils.log_exception(_moduleLogger)
97 def ClearAvatar(self):
100 def _select_avatar(self, handleId):
101 handle = self.get_handle_by_id(telepathy.HANDLE_TYPE_CONTACT, handleId)
103 if handle == self.GetSelfHandle():
104 imageName = self.__SELF_AVATAR
106 accountNumber = misc_utils.normalize_number(self.session.backend.get_account_number())
107 phoneType = self.session.addressbook.get_phone_type(handle.phoneNumber)
108 if handle.phoneNumber == accountNumber:
109 imageName = self.__SELF_AVATAR
110 elif phoneType in ("mobile", ):
111 imageName = self.__MOBILE_AVATAR
112 elif phoneType in ("home", "work"):
113 imageName = self.__LANDLINE_AVATAR
115 imageName = self.__OTHER_AVATAR
119 def _get_avatar(self, imageName):
121 return self._avatarCache[imageName]
123 image = self._load_avatar(imageName)
124 self._avatarCache[imageName] = image
127 def _load_avatar(self, imageName):
128 _moduleLogger.debug("Loading avatar %r from file" % (imageName, ))
130 with open(os.sep.join([self.__LOOKUP_PATHS[0], imageName+".png"]), "rb") as f:
133 with open(os.sep.join([self.__LOOKUP_PATHS[1], "32-"+imageName+".png"]), "rb") as f: