1 from __future__
import division
, absolute_import
, unicode_literals
5 from cola
import sipcompat
8 from PyQt4
import QtGui
9 from PyQt4
import QtCore
10 from PyQt4
import QtNetwork
11 from PyQt4
.QtCore
import SIGNAL
14 from cola
import icons
15 from cola
.compat
import ustr
, parse
16 from cola
.widgets
import defs
19 class Gravatar(object):
22 def url_for_email(email
, imgsize
):
23 email_hash
= core
.decode(hashlib
.md5(core
.encode(email
)).hexdigest())
24 # Python2.6 requires byte strings for urllib2.quote() so we have
26 default_url
= 'https://git-cola.github.io/images/git-64x64.jpg'
27 encoded_url
= parse
.quote(core
.encode(default_url
), core
.encode(''))
28 query
= '?s=%d&d=%s' % (imgsize
, core
.decode(encoded_url
))
29 url
= 'https://gravatar.com/avatar/' + email_hash
+ query
33 class GravatarLabel(QtGui
.QLabel
):
35 def __init__(self
, parent
=None):
36 QtGui
.QLabel
.__init
__(self
, parent
)
41 self
.imgsize
= defs
.medium_icon
44 self
.network
= QtNetwork
.QNetworkAccessManager()
45 self
.connect(self
.network
,
46 SIGNAL('finished(QNetworkReply*)'),
47 self
.network_finished
)
49 def set_email(self
, email
):
50 if email
in self
.pixmaps
:
51 self
.setPixmap(self
.pixmaps
[email
])
53 if (self
.timeout
> 0 and
54 (int(time
.time()) - self
.timeout
) < (5 * 60)):
55 self
.set_pixmap_from_response()
57 if email
== self
.email
and self
.response
is not None:
58 self
.set_pixmap_from_response()
63 def request(self
, email
):
64 url
= Gravatar
.url_for_email(email
, self
.imgsize
)
65 self
.network
.get(QtNetwork
.QNetworkRequest(QtCore
.QUrl(url
)))
67 def default_pixmap_as_bytes(self
):
69 pixmap
= icons
.cola().pixmap(xres
)
70 byte_array
= QtCore
.QByteArray()
71 buf
= QtCore
.QBuffer(byte_array
)
72 buf
.open(QtCore
.QIODevice
.WriteOnly
)
73 pixmap
.save(buf
, 'PNG')
77 def network_finished(self
, reply
):
80 header
= QtCore
.QByteArray('Location')
81 location
= ustr(reply
.rawHeader(header
)).strip()
83 request_location
= Gravatar
.url_for_email(self
.email
, self
.imgsize
)
84 relocated
= location
!= request_location
88 if reply
.error() == QtNetwork
.QNetworkReply
.NoError
:
90 # We could do get_url(parse.unquote(location)) to
91 # download the default image.
92 # Save bandwidth by using a pixmap.
93 self
.response
= self
.default_pixmap_as_bytes()
95 self
.response
= reply
.readAll()
98 self
.response
= self
.default_pixmap_as_bytes()
99 self
.timeout
= int(time
.time())
101 pixmap
= self
.set_pixmap_from_response()
103 # If the email has not changed (e.g. no other requests)
104 # then we know that this pixmap corresponds to this specific
105 # email address. We can't blindly trust self.email else
106 # we may add cache entries for thee wrong email address.
107 url
= Gravatar
.url_for_email(email
, self
.imgsize
)
108 if url
== reply
.url().toString():
109 self
.pixmaps
[email
] = pixmap
111 def set_pixmap_from_response(self
):
112 pixmap
= QtGui
.QPixmap()
113 pixmap
.loadFromData(self
.response
)
114 self
.setPixmap(pixmap
)