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
19
from bzrlib.config import parse_username
21
from bzrlib.plugins.gtk import _i18n
22
from bzrlib.plugins.gtk.avatarproviders import AvatarProviderGravatar, AvatarDownloaderWorker
25
class Avatar(gtk.Box):
26
""" Author or committer avatar """
28
def __init__(self, apparent_username):
30
gtk.Box.__init__(self)
32
self.apparent_username = apparent_username
33
self.username, self.email = parse_username(apparent_username)
36
def __eq__(self, other):
38
Return True if attributes of the given avatar
39
match to current object attributes otherwise return False
41
return self.apparent_username == other.apparent_username and \
42
self.name == other.name and \
43
self.email == other.email
45
# ~~~~~ Public methods ~~~~~
46
def show_spinner(self):
48
Replace the current content of the Avatar with a gtk.Spinner
49
if an email address has been parsed. If not, show an gtk.Label with
50
the translatable 'No email' text.
52
if not self.email is "":
53
spinner = gtk.Spinner()
55
self.pack_start(spinner, False)
56
spinner.set_tooltip_text(_i18n("Retrieving avatar for %s...") % self.email)
57
spinner.set_size_request(20, 20)
60
no_email = gtk.Label(_i18n("No email"))
61
self.pack_start(no_email)
62
self.set_tooltip_text(self.apparent_username)
66
""" Replace the current content of the Avatar with the gtk.Image """
67
if not self.email is "" and self.image:
68
self.remove(self.get_children()[0])
69
self.pack_start(self.image)
70
self.image.set_tooltip_text(self.apparent_username)
74
class AvatarBox(gtk.HBox):
75
""" Improved gtk.HBox """
77
def __init__(self, homogeneous=False, spacing=0):
79
gtk.HBox.__init__(self, homogeneous, spacing)
82
self.__displaying = None
85
# ~~~~~ Public methods ~~~~~
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):
94
Return True if this box has registered given avatar,
95
otherwise return False
97
return avatar.email in self.__avatars
99
def showing(self, avatar):
101
Return True if the displaying avatar is the same
102
as the given one otherwise return False
104
return self.__displaying and self.__displaying == avatar
106
def append_avatars_with(self, avatar):
108
Append avatars collection with the given one if not already registed
109
otherwise render it back.
110
When an avatar is added this method True, otherwise, if the avatar
111
was in the collection, return False.
113
if not avatar.email is "" and not avatar.email in self.__avatars:
114
self.__avatars[avatar.email] = avatar
115
self._new_cell_for_avatar(avatar)
118
self.and_avatar_email(avatar.email).come_back_to_gui()
121
def and_avatar_email(self, email):
123
Select the avatar from avatars collection
124
in order to apply future actions
127
if not email is "" and email in self.__avatars:
128
self.avatar = self.__avatars[email]
133
def update_avatar_image_from_pixbuf_loader(self, loader):
134
""" Replace the gtk.Spinner with the given loader """
136
self.avatar.image = gtk.Image()
137
self.avatar.image.set_from_pixbuf(loader.get_pixbuf())
138
self.avatar.show_image()
139
self.__displaying = self.avatar
141
def come_back_to_gui(self):
142
""" Render back avatar in the GUI """
144
self.pack_start(self.avatar)
145
self.__displaying = self.avatar
147
self._show_no_email()
150
# ~~~~~ Private methods ~~~~~~
151
def _new_cell_for_avatar(self, avatar):
152
""" Create a new cell in this box with a gtk.Spinner """
153
avatar.show_spinner()
154
self.pack_start(avatar)
156
self.__displaying = avatar
158
def _show_no_email(self):
159
""" Show a gtk.Label with test 'No email' """
160
no_email = gtk.Label(_i18n("No email"))
161
self.pack_start(no_email)
165
class AvatarsBox(gtk.HBox):
166
""" GTK container for authors and committers avatars """
170
gtk.HBox.__init__(self, False, 10)
172
self.__committer_box = None
173
self.__authors_box = None
176
# If more later you want to implement more avatar providers, to it like this:
177
# Create a new class named AvatarProvider + provider_name that inherit from
178
# the AvatarProvider class.
179
# Implement a method that return url to use in the request.
180
# For example, with Gravatar, the method return the complete url
181
# with MD5 hash of the email address and put the value in a gravatar_id field.
182
# Then create a new worker (manage them in a python dictionnary).
183
provider = AvatarProviderGravatar()
184
self.__worker = AvatarDownloaderWorker(
185
provider.gravatar_id_for_email
187
# This callback method should be fired bt all workers when a request is done.
188
self.__worker.set_callback_method(self._update_avatar_from_response)
189
self.__worker.start()
192
# ~~~~~ Public methods ~~~~~
193
def add(self, username, role):
195
Add the given username in the right role box and add in the worker queue.
196
Here again: If you want to implement more providers, you should add the
197
avatar request in all workers queue.
199
avatar = Avatar(username)
200
if role is "author" and not self._role_box_for("committer").showing(avatar) or role is "committer":
201
if self._role_box_for(role).append_avatars_with(avatar):
202
self.__worker.queue(avatar.email)
204
def merge(self, usernames, role):
205
""" Add avatars from a list """
206
for username in usernames:
207
self.add(username, role)
211
Request a reset view for all boxes in order to show only avatars
212
of the selected line in the revision view screen.
214
[self._role_box_for(role).reset_view() for role in ["committer", "author"]]
217
# ~~~~~ Private methods ~~~~~
219
""" Create boxes where avatars will be displayed """
220
# 2 gtk.HBox: One for the committer and one for authors
222
self.__committer_box = AvatarBox()
223
self.__committer_box.set_size_request(80, 80)
224
self.pack_end(self.__committer_box, False)
225
self.__committer_box.show()
227
self.__authors_box = AvatarBox()
228
self.pack_end(self.__authors_box, False)
229
self.__authors_box.set_spacing(10)
230
self.__authors_box.show()
232
def _update_avatar_from_response(self, response, email):
234
Callback method fired by avatar worker when finished.
236
response is a urllib2.urlopen() return value.
237
email is used to identify item from self.__avatars.
240
# Convert downloaded image from provider to gtk.Image
241
loader = gtk.gdk.PixbufLoader()
242
loader.write(response.read())
245
for role in ["committer", "author"]:
246
self._role_box_for(role).and_avatar_email(email).update_avatar_image_from_pixbuf_loader(loader)
248
def _role_box_for(self, role):
249
""" Return the gtk.HBox for the given role """
250
return self.__committer_box if role is "committer" else self.__authors_box