1 from __future__
import division
, absolute_import
, unicode_literals
5 from qtpy
import QtCore
7 from qtpy
import QtWidgets
8 from qtpy
import QtNetwork
12 from .compat
import bstr
13 from .compat
import ustr
14 from .compat
import parse
15 from .widgets
import defs
18 class Gravatar(object):
21 def url_for_email(email
, imgsize
):
22 email_hash
= core
.decode(hashlib
.md5(core
.encode(email
)).hexdigest())
23 # Python2.6 requires byte strings for urllib2.quote() so we have
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
32 class GravatarLabel(QtWidgets
.QLabel
):
34 def __init__(self
, parent
=None):
35 QtWidgets
.QLabel
.__init
__(self
, parent
)
40 self
.imgsize
= defs
.medium_icon
42 self
._default
_pixmap
_bytes
= None
44 self
.network
= QtNetwork
.QNetworkAccessManager()
45 self
.network
.finished
.connect(self
.network_finished
)
47 def set_email(self
, email
):
48 if email
in self
.pixmaps
:
49 self
.setPixmap(self
.pixmaps
[email
])
51 if (self
.timeout
> 0 and
52 (int(time
.time()) - self
.timeout
) < (5 * 60)):
53 self
.set_pixmap_from_response()
55 if email
== self
.email
and self
.response
is not None:
56 self
.set_pixmap_from_response()
61 def request(self
, email
):
62 url
= Gravatar
.url_for_email(email
, self
.imgsize
)
63 self
.network
.get(QtNetwork
.QNetworkRequest(QtCore
.QUrl(url
)))
65 def default_pixmap_as_bytes(self
):
66 if self
._default
_pixmap
_bytes
is None:
68 pixmap
= icons
.cola().pixmap(xres
)
69 byte_array
= QtCore
.QByteArray()
70 buf
= QtCore
.QBuffer(byte_array
)
71 buf
.open(QtCore
.QIODevice
.WriteOnly
)
72 pixmap
.save(buf
, 'PNG')
74 self
._default
_pixmap
_bytes
= byte_array
76 byte_array
= self
._default
_pixmap
_bytes
79 def network_finished(self
, reply
):
82 header
= QtCore
.QByteArray(bstr('Location'))
83 location
= ustr(reply
.rawHeader(header
)).strip()
85 request_location
= Gravatar
.url_for_email(self
.email
, self
.imgsize
)
86 relocated
= location
!= request_location
90 if reply
.error() == QtNetwork
.QNetworkReply
.NoError
:
92 # We could do get_url(parse.unquote(location)) to
93 # download the default image.
94 # Save bandwidth by using a pixmap.
95 self
.response
= self
.default_pixmap_as_bytes()
97 self
.response
= reply
.readAll()
100 self
.response
= self
.default_pixmap_as_bytes()
101 self
.timeout
= int(time
.time())
103 pixmap
= self
.set_pixmap_from_response()
105 # If the email has not changed (e.g. no other requests)
106 # then we know that this pixmap corresponds to this specific
107 # email address. We can't blindly trust self.email else
108 # we may add cache entries for thee wrong email address.
109 url
= Gravatar
.url_for_email(email
, self
.imgsize
)
110 if url
== reply
.url().toString():
111 self
.pixmaps
[email
] = pixmap
113 def set_pixmap_from_response(self
):
114 if self
.response
is None:
115 self
.response
= self
._default
_pixmap
_bytes
()
116 pixmap
= QtGui
.QPixmap()
117 pixmap
.loadFromData(self
.response
)
118 self
.setPixmap(pixmap
)