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.plugins.gtk.avatar import Avatar
20
from bzrlib.plugins.gtk.avatarbox import AvatarBox
21
from bzrlib.plugins.gtk.avatarprovidergravatar import AvatarProviderGravatar
22
from bzrlib.plugins.gtk.avatardownloaderworker import AvatarDownloaderWorker
24
class AvatarsBox(gtk.HBox):
25
""" GTK container for authors and committers avatars """
29
gtk.HBox.__init__(self, False, 10)
31
self.__committer_box = None
32
self.__authors_box = None
35
# If more later you want to implement more avatar providers, to it like this:
36
# Create a new class named AvatarProvider + provider_name that inherit from
37
# the AvatarProvider class.
38
# Implement a method that return url to use in the request.
39
# For example, with Gravatar, the method return the complete url
40
# with MD5 hash of the email address and put the value in a gravatar_id field.
41
# Then create a new worker (manage them in a python dictionnary).
42
provider = AvatarProviderGravatar()
43
self.__worker = AvatarDownloaderWorker(
44
provider.gravatar_id_for_email
46
# This callback method should be fired bt all workers when a request is done.
47
self.__worker.set_callback_method(self._update_avatar_from_response)
51
# ~~~~~ Public methods ~~~~~
52
def add(self, username, role):
54
Add the given username in the right role box and add in the worker queue.
55
Here again: If you want to implement more providers, you should add the
56
avatar request in all workers queue.
58
avatar = Avatar(username)
59
if role is "author" and not self._role_box_for("committer").showing(avatar) or role is "committer":
60
if self._role_box_for(role).append_avatars_with(avatar):
61
self.__worker.queue(avatar.email)
63
def merge(self, usernames, role):
64
""" Add avatars from a list """
65
[self.add(username, role) for username in usernames]
69
Request a reset view for all boxes in order to show only avatars
70
of the selected line in the revision view screen.
72
[self._role_box_for(role).reset_view() for role in ["committer", "author"]]
75
# ~~~~~ Private methods ~~~~~
77
""" Create boxes where avatars will be displayed """
78
# 2 gtk.HBox: One for the committer and one for authors
80
self.__committer_box = AvatarBox()
81
self.__committer_box.set_size_request(80, 80)
82
self.pack_end(self.__committer_box, False)
83
self.__committer_box.show()
85
self.__authors_box = AvatarBox()
86
self.pack_end(self.__authors_box, False)
87
self.__authors_box.set_spacing(10)
88
self.__authors_box.show()
90
def _update_avatar_from_response(self, response, email):
92
Callback method fired by avatar worker when finished.
94
response is a urllib2.urlopen() return value.
95
email is used to identify item from self.__avatars.
98
# Convert downloaded image from provider to gtk.Image
99
loader = gtk.gdk.PixbufLoader()
100
loader.write(response.read())
103
for role in ["committer", "author"]:
104
self._role_box_for(role).and_avatar_email(email).update_avatar_image_from_pixbuf_loader(loader)
106
def _role_box_for(self, role):
107
""" Return the gtk.HBox for the given role """
108
return self.__committer_box if role is "committer" else self.__authors_box