git-cola v4.0.0
[git-cola.git] / cola / gravatar.py
blobe1b6c84ac9033aadf9af595e853b927d0797e6a1
1 from __future__ import absolute_import, division, print_function, unicode_literals
2 import time
3 import hashlib
5 from qtpy import QtCore
6 from qtpy import QtGui
7 from qtpy import QtWidgets
8 from qtpy import QtNetwork
10 from . import compat
11 from . import core
12 from . import icons
13 from .compat import bstr
14 from .compat import ustr
15 from .compat import parse
16 from .widgets import defs
19 class Gravatar(object):
20 @staticmethod
21 def url_for_email(email, imgsize):
22 email_hash = md5_hexdigest(email)
23 # Python2.6 requires byte strings for urllib2.quote() so we have
24 # to force
25 default_url = 'https://git-cola.github.io/images/git-64x64.jpg'
26 encoded_url = parse.quote(core.encode(default_url), core.encode(''))
27 query = '?s=%d&d=%s' % (imgsize, core.decode(encoded_url))
28 url = 'https://gravatar.com/avatar/' + email_hash + query
29 return url
32 def md5_hexdigest(value):
33 """Return the md5 hexdigest for a value.
35 Used for implementing the gravatar API. Not used for security purposes.
36 """
37 encoded_value = core.encode(value)
38 result = ''
39 try:
40 result = hashlib.md5(encoded_value).hexdigest()
41 except ValueError:
42 pass
43 if not result and compat.PY_VERSION >= (3, 6):
44 try:
45 # pylint: disable=unexpected-keyword-arg
46 result = hashlib.md5(encoded_value, usedforsecurity=False).hexdigest()
47 except ValueError:
48 # https://github.com/git-cola/git-cola/issues/1157
49 # ValueError: error:060800A3:
50 # digital envelope routines: EVP_DigestInit_ex: disabled for fips
52 # Newer versions of Python, including Centos8's patched Python3.6 and
53 # mainline Python 3.9+ have a "usedoforsecurity" parameter which allows us
54 # to continue using hashlib.md5().
55 pass
56 return core.decode(result)
59 class GravatarLabel(QtWidgets.QLabel):
60 def __init__(self, parent=None):
61 QtWidgets.QLabel.__init__(self, parent)
63 self.email = None
64 self.response = None
65 self.timeout = 0
66 self.imgsize = defs.medium_icon
67 self.pixmaps = {}
68 self._default_pixmap_bytes = None
70 self.network = QtNetwork.QNetworkAccessManager()
71 # pylint: disable=no-member
72 self.network.finished.connect(self.network_finished)
74 def set_email(self, email):
75 if email in self.pixmaps:
76 self.setPixmap(self.pixmaps[email])
77 return
78 if self.timeout > 0 and (int(time.time()) - self.timeout) < (5 * 60):
79 self.set_pixmap_from_response()
80 return
81 if email == self.email and self.response is not None:
82 self.set_pixmap_from_response()
83 return
84 self.email = email
85 self.request(email)
87 def request(self, email):
88 url = Gravatar.url_for_email(email, self.imgsize)
89 self.network.get(QtNetwork.QNetworkRequest(QtCore.QUrl(url)))
91 def default_pixmap_as_bytes(self):
92 if self._default_pixmap_bytes is None:
93 xres = self.imgsize
94 pixmap = icons.cola().pixmap(xres)
95 byte_array = QtCore.QByteArray()
96 buf = QtCore.QBuffer(byte_array)
97 buf.open(QtCore.QIODevice.WriteOnly)
98 pixmap.save(buf, 'PNG')
99 buf.close()
100 self._default_pixmap_bytes = byte_array
101 else:
102 byte_array = self._default_pixmap_bytes
103 return byte_array
105 def network_finished(self, reply):
106 email = self.email
108 header = QtCore.QByteArray(bstr('Location'))
109 location = ustr(reply.rawHeader(header)).strip()
110 if location:
111 request_location = Gravatar.url_for_email(self.email, self.imgsize)
112 relocated = location != request_location
113 else:
114 relocated = False
116 if reply.error() == QtNetwork.QNetworkReply.NoError:
117 if relocated:
118 # We could do get_url(parse.unquote(location)) to
119 # download the default image.
120 # Save bandwidth by using a pixmap.
121 self.response = self.default_pixmap_as_bytes()
122 else:
123 self.response = reply.readAll()
124 self.timeout = 0
125 else:
126 self.response = self.default_pixmap_as_bytes()
127 self.timeout = int(time.time())
129 pixmap = self.set_pixmap_from_response()
131 # If the email has not changed (e.g. no other requests)
132 # then we know that this pixmap corresponds to this specific
133 # email address. We can't blindly trust self.email else
134 # we may add cache entries for thee wrong email address.
135 url = Gravatar.url_for_email(email, self.imgsize)
136 if url == reply.url().toString():
137 self.pixmaps[email] = pixmap
139 def set_pixmap_from_response(self):
140 if self.response is None:
141 self.response = self._default_pixmap_bytes()
142 pixmap = QtGui.QPixmap()
143 pixmap.loadFromData(self.response)
144 self.setPixmap(pixmap)
145 return pixmap