1 from __future__ import with_statement
9 import util.misc as misc_utils
12 _moduleLogger = logging.getLogger('avatars')
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)
30 self._implement_property_get(
31 telepathy.interfaces.CONNECTION_INTERFACE_AVATARS,
33 'SupportedAvatarMimeTypes': lambda: ("image/png", ),
34 'MinimumAvatarHeight': lambda: 32,
35 'MinimumAvatarWidth': lambda: 32,
36 'RecommendedAvatarHeight': lambda: 32,
37 'RecommendedAvatarWidth': lambda: 32,
38 'MaximumAvatarHeight': lambda: 32,
39 'MaximumAvatarWidth': lambda: 32,
40 'MaximumAvatarBytes': lambda: 500 * 1024,
49 raise NotImplementedError("Abstract property called")
51 def get_handle_by_id(self, handleType, handleId):
55 raise NotImplementedError("Abstract function called")
57 @misc_utils.log_exception(_moduleLogger)
58 def GetAvatarRequirements(self):
59 mime_types = ("image/png", )
60 return (mime_types, 32, 32, 64, 64, 500 * 1024)
62 @misc_utils.log_exception(_moduleLogger)
63 def GetAvatarTokens(self, contacts):
65 for handleid in contacts:
66 imageName = self._select_avatar(handleid)
67 result[handleid] = imageName
70 @misc_utils.log_exception(_moduleLogger)
71 def GetKnownAvatarTokens(self, contacts):
73 for handleid in contacts:
74 imageName = self._select_avatar(handleid)
75 result[handleid] = imageName
78 @misc_utils.log_exception(_moduleLogger)
79 def RequestAvatar(self, contact):
80 imageName = self._select_avatar(contact)
81 image = self._load_avatar(imageName)
82 return image, "image/png"
84 @misc_utils.log_exception(_moduleLogger)
85 def RequestAvatars(self, contacts):
86 for handleid in contacts:
87 imageName = self._select_avatar(handleid)
88 image = self._load_avatar(imageName)
89 self.AvatarRetrieved(handleid, imageName, image, "image/png")
91 @misc_utils.log_exception(_moduleLogger)
92 def SetAvatar(self, avatar, mime_type):
93 raise telepathy.errors.PermissionDenied
95 @misc_utils.log_exception(_moduleLogger)
96 def ClearAvatar(self):
99 def _select_avatar(self, handleId):
100 handle = self.get_handle_by_id(telepathy.HANDLE_TYPE_CONTACT, handleId)
102 if handle == self.GetSelfHandle():
103 imageName = self.__SELF_AVATAR
105 accountNumber = misc_utils.normalize_number(self.session.backend.get_account_number())
106 phoneType = self.session.addressbook.get_phone_type(handle.phoneNumber)
107 if handle.phoneNumber == accountNumber:
108 imageName = self.__SELF_AVATAR
109 elif phoneType in ("mobile", ):
110 imageName = self.__MOBILE_AVATAR
111 elif phoneType in ("home", "work"):
112 imageName = self.__LANDLINE_AVATAR
114 imageName = self.__OTHER_AVATAR
118 def _load_avatar(self, imageName):
120 with open(os.sep.join([self.__LOOKUP_PATHS[0], imageName+".png"]), "rb") as f:
123 with open(os.sep.join([self.__LOOKUP_PATHS[1], "32-"+imageName+".png"]), "rb") as f: