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 "/opt/theonering/share",
24 "/usr/share/theonering",
25 os.path.join(os.path.dirname(__file__), "../support/icons"),
29 tp.server.ConnectionInterfaceAvatars.__init__(self)
30 self._avatarCache = {}
32 self._implement_property_get(
33 telepathy.interfaces.CONNECTION_INTERFACE_AVATARS,
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,
51 raise NotImplementedError("Abstract property called")
53 def get_handle_by_id(self, handleType, handleId):
57 raise NotImplementedError("Abstract function called")
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)
64 @misc_utils.log_exception(_moduleLogger)
65 def GetAvatarTokens(self, contacts):
67 for handleid in contacts:
68 imageName = self._select_avatar(handleid)
69 result[handleid] = imageName
72 @misc_utils.log_exception(_moduleLogger)
73 def GetKnownAvatarTokens(self, contacts):
75 for handleid in contacts:
76 imageName = self._select_avatar(handleid)
77 result[handleid] = imageName
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"
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")
93 @misc_utils.log_exception(_moduleLogger)
94 def SetAvatar(self, avatar, mime_type):
95 raise telepathy.errors.PermissionDenied
97 @misc_utils.log_exception(_moduleLogger)
98 def ClearAvatar(self):
101 def _select_avatar(self, handleId):
102 handle = self.get_handle_by_id(telepathy.HANDLE_TYPE_CONTACT, handleId)
104 if handle == self.GetSelfHandle():
105 imageName = self.__SELF_AVATAR
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
116 imageName = self.__OTHER_AVATAR
120 def _get_avatar(self, imageName):
122 return self._avatarCache[imageName]
124 image = self._load_avatar(imageName)
125 self._avatarCache[imageName] = image
128 def _load_avatar(self, imageName):
129 _moduleLogger.debug("Loading avatar %r from file" % (imageName, ))
131 with open(os.sep.join([self.__LOOKUP_PATHS[0], imageName+".png"]), "rb") as f:
134 with open(os.sep.join([self.__LOOKUP_PATHS[1], "32-"+imageName+".png"]), "rb") as f: