4 from qtpy
import QtCore
6 from qtpy
import QtWidgets
7 from qtpy
import QtNetwork
12 from .compat
import parse
13 from .models
import prefs
14 from .widgets
import defs
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
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
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.
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
)
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()
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
62 self
.imgsize
= defs
.medium_icon
64 self
._default
_pixmap
_bytes
= None
66 self
.network
= QtNetwork
.QNetworkAccessManager()
67 # pylint: disable=no-member
68 self
.network
.finished
.connect(self
.network_finished
)
70 def set_email(self
, email
):
71 """Update the author icon based on the specified email"""
72 pixmap
= self
.pixmaps
.get(email
, None)
73 if pixmap
is not None:
74 self
.setPixmap(pixmap
)
76 if self
.timeout
> 0 and (int(time
.time()) - self
.timeout
) < (5 * 60):
77 self
.set_pixmap_from_response()
79 if email
== self
.email
and self
.response
is not None:
80 self
.set_pixmap_from_response()
85 def request(self
, email
):
86 if prefs
.enable_gravatar(self
.context
):
87 url
= Gravatar
.url_for_email(email
, self
.imgsize
)
88 self
.network
.get(QtNetwork
.QNetworkRequest(QtCore
.QUrl(url
)))
90 self
.pixmaps
[email
] = self
.set_pixmap_from_response()
92 def default_pixmap_as_bytes(self
):
93 if self
._default
_pixmap
_bytes
is None:
95 pixmap
= icons
.cola().pixmap(xres
)
96 byte_array
= QtCore
.QByteArray()
97 buf
= QtCore
.QBuffer(byte_array
)
98 buf
.open(QtCore
.QIODevice
.WriteOnly
)
99 pixmap
.save(buf
, 'PNG')
101 self
._default
_pixmap
_bytes
= byte_array
103 byte_array
= self
._default
_pixmap
_bytes
106 def network_finished(self
, reply
):
109 header
= QtCore
.QByteArray(b
'Location')
110 location
= core
.decode(bytes(reply
.rawHeader(header
))).strip()
112 request_location
= Gravatar
.url_for_email(self
.email
, self
.imgsize
)
113 relocated
= location
!= request_location
116 no_error
= qtutils
.enum_value(
117 QtNetwork
.QNetworkReply
.NetworkError
.NoError
# pylint: disable=no-member
119 reply_error
= qtutils
.enum_value(reply
.error())
120 if reply_error
== no_error
:
122 # We could do get_url(parse.unquote(location)) to
123 # download the default image.
124 # Save bandwidth by using a pixmap.
125 self
.response
= self
.default_pixmap_as_bytes()
127 self
.response
= reply
.readAll()
130 self
.response
= self
.default_pixmap_as_bytes()
131 self
.timeout
= int(time
.time())
133 pixmap
= self
.set_pixmap_from_response()
135 # If the email has not changed (e.g. no other requests)
136 # then we know that this pixmap corresponds to this specific
137 # email address. We can't blindly trust self.email else
138 # we may add cache entries for thee wrong email address.
139 url
= Gravatar
.url_for_email(email
, self
.imgsize
)
140 if url
== reply
.url().toString():
141 self
.pixmaps
[email
] = pixmap
143 def set_pixmap_from_response(self
):
144 if self
.response
is None:
145 self
.response
= self
.default_pixmap_as_bytes()
146 pixmap
= QtGui
.QPixmap()
147 pixmap
.loadFromData(self
.response
)
148 self
.setPixmap(pixmap
)