f4ce3f437f1efa329c110e95abcddcd8f8e4f38a
[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('avatars')
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                 "/usr/share/theonering",
24                 os.path.join(os.path.dirname(__file__), "../support/icons"),
25         )
26
27         def __init__(self):
28                 tp.server.ConnectionInterfaceAvatars.__init__(self)
29
30                 self._implement_property_get(
31                         telepathy.interfaces.CONNECTION_INTERFACE_AVATARS,
32                         {
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,
41                         },
42                 )
43
44         @property
45         def session(self):
46                 """
47                 @abstract
48                 """
49                 raise NotImplementedError("Abstract property called")
50
51         def get_handle_by_id(self, handleType, handleId):
52                 """
53                 @abstract
54                 """
55                 raise NotImplementedError("Abstract function called")
56
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)
61
62         @misc_utils.log_exception(_moduleLogger)
63         def GetAvatarTokens(self, contacts):
64                 result = {}
65                 for handleid in contacts:
66                         imageName = self._select_avatar(handleid)
67                         result[handleid] = imageName
68                 return result
69
70         @misc_utils.log_exception(_moduleLogger)
71         def GetKnownAvatarTokens(self, contacts):
72                 result = {}
73                 for handleid in contacts:
74                         imageName = self._select_avatar(handleid)
75                         result[handleid] = imageName
76                 return result
77
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"
83
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")
90
91         @misc_utils.log_exception(_moduleLogger)
92         def SetAvatar(self, avatar, mime_type):
93                 raise telepathy.errors.PermissionDenied
94
95         @misc_utils.log_exception(_moduleLogger)
96         def ClearAvatar(self):
97                 pass
98
99         def _select_avatar(self, handleId):
100                 handle = self.get_handle_by_id(telepathy.HANDLE_TYPE_CONTACT, handleId)
101
102                 if handle == self.GetSelfHandle():
103                         imageName = self.__SELF_AVATAR
104                 else:
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
113                         else:
114                                 imageName = self.__OTHER_AVATAR
115
116                 return imageName
117
118         def _load_avatar(self, imageName):
119                 try:
120                         with open(os.sep.join([self.__LOOKUP_PATHS[0], imageName+".png"]), "rb") as f:
121                                 return f.read()
122                 except IOError:
123                         with open(os.sep.join([self.__LOOKUP_PATHS[1], "32-"+imageName+".png"]), "rb") as f:
124                                 return f.read()