1
# Copyright (C) 2011 by Guillaume Hain (zedtux) <zedtux@zedroot.org>
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
from gi.repository import Gtk
18
from gi.repository import GdkPixbuf
20
from bzrlib.config import parse_username
22
from bzrlib.plugins.gtk.i18n import _i18n
23
from bzrlib.plugins.gtk.avatarproviders import (
24
AvatarProviderGravatar,
25
AvatarDownloaderWorker,
29
class Avatar(Gtk.HBox):
30
""" Author or committer avatar """
32
def __init__(self, apparent_username):
34
super(Avatar, self).__init__()
36
self.apparent_username = apparent_username
37
self.username, self.email = parse_username(apparent_username)
40
def __eq__(self, other):
41
return (self.apparent_username == other.apparent_username and
42
self.username == other.username and
43
self.email == other.email)
45
def show_spinner(self):
47
Replace the current content of the Avatar with a Gtk.Spinner
48
if an email address has been parsed. If not, show an Gtk.Label with
49
the translatable 'No email' text.
52
tooltip = _i18n("Retrieving avatar for %s...") % self.email
53
spinner = Gtk.Spinner()
55
self.pack_start(spinner, False, True, 0)
56
spinner.set_tooltip_text(tooltip)
57
spinner.set_size_request(20, 20)
60
no_email = Gtk.Label(label=_i18n("No email"))
61
self.pack_start(no_email, True, True, 0)
62
self.set_tooltip_text(self.apparent_username)
66
"""Replace the current content of the Avatar with the Gtk.Image """
67
if self.email and self.image:
68
children = self.get_children()
70
self.remove(children[0])
71
self.pack_start(self.image, True, True, 0)
72
self.image.set_tooltip_text(self.apparent_username)
76
class AvatarBox(Gtk.HBox):
77
"""HBox showing an avatar."""
79
def __init__(self, homogeneous=False, spacing=0):
80
super(AvatarBox, self).__init__(
81
homogeneous=homogeneous, spacing=spacing)
84
self.__displaying = None
87
"""Remove current avatars from the gtk box."""
88
for child in self.get_children():
90
self.__displaying = None
92
def have_avatar(self, avatar):
93
"""Return True if this box has the specified avatar.
95
return avatar.email in self.__avatars
97
def showing(self, avatar):
98
"""Return True if the displaying avatar matches the specified one.
100
return self.__displaying and self.__displaying == avatar
102
def append_avatars_with(self, avatar):
104
Append avatars collection with the given one if not already registed
105
otherwise render it back.
106
When an avatar is added this method True, otherwise, if the avatar
107
was in the collection, return False.
109
if avatar.email and not avatar.email in self.__avatars:
110
self.__avatars[avatar.email] = avatar
111
self._new_cell_for_avatar(avatar)
114
self.and_avatar_email(avatar.email).come_back_to_gui()
117
def and_avatar_email(self, email):
119
Select the avatar from avatars collection
120
in order to apply future actions
123
if email and email in self.__avatars:
124
self.avatar = self.__avatars[email]
129
def update_avatar_image_from_pixbuf_loader(self, loader):
130
"""Replace the Gtk.Spinner with the given loader."""
132
self.avatar.image = Gtk.Image()
133
self.avatar.image.set_from_pixbuf(loader.get_pixbuf())
134
self.avatar.show_image()
135
self.__displaying = self.avatar
137
def come_back_to_gui(self):
138
"""Render back avatar in the GUI."""
140
self.pack_start(self.avatar, True, True, 0)
141
self.__displaying = self.avatar
143
self._show_no_email()
145
def _new_cell_for_avatar(self, avatar):
146
"""Create a new cell in this box with a Gtk.Spinner."""
147
avatar.show_spinner()
148
self.pack_start(avatar, True, True, 0)
150
self.__displaying = avatar
152
def _show_no_email(self):
153
"""Show a Gtk.Label with text 'No email'."""
154
no_email = Gtk.Label(label=_i18n("No email"))
155
self.pack_start(no_email, True, True, 0)
159
class AvatarsBox(Gtk.HBox):
160
"""GTK container for author and committer avatars."""
163
super(AvatarsBox, self).__init__(homogeneous=False, spacing=10)
164
self.__committer_box = None
165
self.__authors_box = None
168
# If more later you want to implement more avatar providers:
169
# * Create a new class named AvatarProvider + provider_name that
170
# inherit from the AvatarProvider class.
171
# * Implement a method that return url to use in the request.
173
# For example, with Gravatar, the method return the complete url
174
# with MD5 hash of the email address and put the value in a
176
# Then create a new worker (manage them in a python dictionary).
177
provider = AvatarProviderGravatar()
178
self.__worker = AvatarDownloaderWorker(
179
provider.gravatar_id_for_email
181
# This callback method should be fired by all workers when a request
183
self.__worker.set_callback_method(self._update_avatar_from_response)
184
self.connect('destroy', self.on_destroy)
186
def on_destroy(self, widget):
188
if self.__worker.is_alive():
191
def add(self, username, role):
192
"""Add the given username in the role box and add in the worker queue.
194
avatar = Avatar(username)
195
is_shown = self._role_box_for("committer").showing(avatar)
196
if (role == "author" and not is_shown) or role == "committer":
197
if self._role_box_for(role).append_avatars_with(avatar):
198
self.__worker.queue(avatar.email)
200
def merge(self, usernames, role):
201
"""Add avatars from a list"""
202
for username in usernames:
203
self.add(username, role)
207
Request a reset view for all boxes in order to show only avatars
208
of the selected line in the revision view screen.
210
for role in ("committer", "author"):
211
self._role_box_for(role).reset_view()
214
"""Create boxes where avatars will be displayed."""
215
# 2 Gtk.HBox: One for the committer and one for authors
217
self.__committer_box = AvatarBox()
218
self.__committer_box.set_size_request(80, 80)
219
self.pack_end(self.__committer_box, False, True, 0)
220
self.__committer_box.show()
222
self.__authors_box = AvatarBox()
223
self.pack_end(self.__authors_box, False, True, 0)
224
self.__authors_box.set_spacing(10)
225
self.__authors_box.show()
227
def _update_avatar_from_response(self, response, email):
228
"""Callback method fired by avatar worker when finished.
230
:param response: a urllib2.urlopen() return value.
231
:param email: used to identify item from self.__avatars.
234
# Convert downloaded image from provider to Gtk.Image
235
loader = GdkPixbuf.PixbufLoader()
236
loader.write(response.read())
239
for role in ["committer", "author"]:
240
self._role_box_for(role).and_avatar_email(
241
email).update_avatar_image_from_pixbuf_loader(loader)
243
def _role_box_for(self, role):
244
""" Return the Gtk.HBox for the given role """
245
if role == "committer":
246
return self.__committer_box
248
return self.__authors_box