fetch: add ability to fetch into a remote tracking branch
[git-cola.git] / cola / gravatar.py
blobaaef85e6fd23b5bf8e8c39ff2ee3518636de4b8b
1 import time
2 import hashlib
4 from qtpy import QtCore
5 from qtpy import QtGui
6 from qtpy import QtWidgets
7 from qtpy import QtNetwork
9 from . import core
10 from . import icons
11 from . import qtutils
12 from .compat import parse
13 from .models import prefs
14 from .widgets import defs
17 class Gravatar:
18 @staticmethod
19 def url_for_email(email, imgsize):
20 email_hash = md5_hexdigest(email)
21 # Python2.6 requires byte strings for urllib2.quote() so we have
22 # to force
23 default_url = 'https://git-cola.github.io/images/git-64x64.jpg'
24 encoded_url = parse.quote(core.encode(default_url), core.encode(''))
25 query = '?s=%d&d=%s' % (imgsize, core.decode(encoded_url))
26 url = 'https://gravatar.com/avatar/' + email_hash + query
27 return url
30 def md5_hexdigest(value):
31 """Return the md5 hexdigest for a value.
33 Used for implementing the gravatar API. Not used for security purposes.
34 """
35 # https://github.com/git-cola/git-cola/issues/1157
36 # ValueError: error:060800A3:
37 # digital envelope routines: EVP_DigestInit_ex: disabled for fips
39 # Newer versions of Python, including Centos8's patched Python3.6 and
40 # mainline Python 3.9+ have a "usedoforsecurity" parameter which allows us
41 # to continue using hashlib.md5().
42 encoded_value = core.encode(value)
43 result = ''
44 try:
45 # This could raise ValueError in theory but we always use encoded bytes
46 # so that does not happen in practice.
47 result = hashlib.md5(encoded_value, usedforsecurity=False).hexdigest()
48 except TypeError:
49 # Fallback to trying hashlib.md5 directly.
50 result = hashlib.md5(encoded_value).hexdigest()
51 return core.decode(result)
54 class GravatarLabel(QtWidgets.QLabel):
55 def __init__(self, context, parent=None):
56 QtWidgets.QLabel.__init__(self, parent)
58 self.context = context
59 self.email = None
60 self.response = None
61 self.timeout = 0
62 self.imgsize = defs.medium_icon
63 self.pixmaps = {}
64 self._default_pixmap_bytes = None
66 self.network = QtNetwork.QNetworkAccessManager()
67 self.network.finished.connect(self.network_finished)
69 def set_email(self, email):
70 """Update the author icon based on the specified email"""
71 pixmap = self.pixmaps.get(email, None)
72 if pixmap is not None:
73 self.setPixmap(pixmap)
74 return
75 if self.timeout > 0 and (int(time.time()) - self.timeout) < (5 * 60):
76 self.set_pixmap_from_response()
77 return
78 if email == self.email and self.response is not None:
79 self.set_pixmap_from_response()
80 return
81 self.email = email
82 self.request(email)
84 def request(self, email):
85 if prefs.enable_gravatar(self.context):
86 url = Gravatar.url_for_email(email, self.imgsize)
87 self.network.get(QtNetwork.QNetworkRequest(QtCore.QUrl(url)))
88 else:
89 self.pixmaps[email] = self.set_pixmap_from_response()
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(b'Location')
109 location = core.decode(bytes(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
115 no_error = qtutils.enum_value(QtNetwork.QNetworkReply.NetworkError.NoError)
116 reply_error = qtutils.enum_value(reply.error())
117 if reply_error == no_error:
118 if relocated:
119 # We could do get_url(parse.unquote(location)) to
120 # download the default image.
121 # Save bandwidth by using a pixmap.
122 self.response = self.default_pixmap_as_bytes()
123 else:
124 self.response = reply.readAll()
125 self.timeout = 0
126 else:
127 self.response = self.default_pixmap_as_bytes()
128 self.timeout = int(time.time())
130 pixmap = self.set_pixmap_from_response()
132 # If the email has not changed (e.g. no other requests)
133 # then we know that this pixmap corresponds to this specific
134 # email address. We can't blindly trust self.email else
135 # we may add cache entries for thee wrong email address.
136 url = Gravatar.url_for_email(email, self.imgsize)
137 if url == reply.url().toString():
138 self.pixmaps[email] = pixmap
140 def set_pixmap_from_response(self):
141 if self.response is None:
142 self.response = self.default_pixmap_as_bytes()
143 pixmap = QtGui.QPixmap()
144 pixmap.loadFromData(self.response)
145 self.setPixmap(pixmap)
146 return pixmap