/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
1
# Copyright (C) 2011 by Guillaume Hain (zedtux) <zedtux@zedroot.org>
2
#
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.
7
#
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.
12
#
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
16
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
17
from gi.repository import Gtk
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
18
723 by Jelmer Vernooij
Merge support for showing gravatars.
19
from bzrlib.config import parse_username
20
729.1.1 by Jelmer Vernooij
Move i18n support to a separate file, so gettext files aren't loaded unless bzr-gtk is used.
21
from bzrlib.plugins.gtk.i18n import _i18n
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
22
from bzrlib.plugins.gtk.avatarproviders import (
23
    AvatarProviderGravatar,
24
    AvatarDownloaderWorker,
25
    )
26
27
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
28
class Avatar(Gtk.HBox):
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
29
    """ Author or committer avatar """
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
30
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
31
    def __init__(self, apparent_username):
32
        """ Constructor """
734.1.6 by Curtis Hovey
Added a very rough fix to see gannontation.
33
        Gtk.HBox.__init__(self)
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
34
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
35
        self.apparent_username = apparent_username
36
        self.username, self.email = parse_username(apparent_username)
37
        self.image = None
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
38
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
39
    def __eq__(self, other):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
40
        return (self.apparent_username == other.apparent_username and
734.1.6 by Curtis Hovey
Added a very rough fix to see gannontation.
41
                self.username == other.username and
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
42
                self.email == other.email)
43
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
44
    def show_spinner(self):
45
        """
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
46
        Replace the current content of the Avatar with a Gtk.Spinner
47
        if an email address has been parsed. If not, show an Gtk.Label with
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
48
        the translatable 'No email' text.
49
        """
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
50
        if self.email:
729 by Jelmer Vernooij
Support use without gtk.Spinner, which is only available in pygtk >= 2.22.
51
            tooltip = _i18n("Retrieving avatar for %s...") % self.email
734.1.6 by Curtis Hovey
Added a very rough fix to see gannontation.
52
            spinner = Gtk.Spinner()
53
            spinner.start()
54
            self.pack_start(spinner, False, True, 0)
55
            spinner.set_tooltip_text(tooltip)
56
            spinner.set_size_request(20, 20)
57
            spinner.show()
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
58
        else:
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
59
            no_email = Gtk.Label(label=_i18n("No email"))
60
            self.pack_start(no_email, True, True, 0)
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
61
            self.set_tooltip_text(self.apparent_username)
62
            no_email.show()
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
63
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
64
    def show_image(self):
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
65
        """Replace the current content of the Avatar with the Gtk.Image """
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
66
        if self.email and self.image:
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
67
            self.remove(self.get_children()[0])
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
68
            self.pack_start(self.image, True, True, 0)
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
69
            self.image.set_tooltip_text(self.apparent_username)
70
            self.image.show()
71
72
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
73
class AvatarBox(Gtk.HBox):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
74
    """HBox showing an avatar."""
75
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
76
    def __init__(self, homogeneous=False, spacing=0):
734.1.6 by Curtis Hovey
Added a very rough fix to see gannontation.
77
        Gtk.HBox.__init__(self, homogeneous=homogeneous, spacing=spacing)
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
78
        self.__avatars = {}
79
        self.avatar = None
80
        self.__displaying = None
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
81
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
82
    def reset_view(self):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
83
        """Remove current avatars from the gtk box."""
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
84
        for child in self.get_children():
85
            self.remove(child)
86
        self.__displaying = None
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
87
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
88
    def have_avatar(self, avatar):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
89
        """Return True if this box has the specified avatar.
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
90
        """
91
        return avatar.email in self.__avatars
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
92
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
93
    def showing(self, avatar):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
94
        """Return True if the displaying avatar matches the specified one.
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
95
        """
96
        return self.__displaying and self.__displaying == avatar
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
97
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
98
    def append_avatars_with(self, avatar):
99
        """
100
        Append avatars collection with the given one if not already registed
101
        otherwise render it back.
102
        When an avatar is added this method True, otherwise, if the avatar
103
        was in the collection, return False.
104
        """
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
105
        if avatar.email and not avatar.email in self.__avatars:
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
106
            self.__avatars[avatar.email] = avatar
107
            self._new_cell_for_avatar(avatar)
108
            return True
109
        else:
110
            self.and_avatar_email(avatar.email).come_back_to_gui()
111
        return False
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
112
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
113
    def and_avatar_email(self, email):
114
        """
115
        Select the avatar from avatars collection
116
        in order to apply future actions
117
        """
118
        self.avatar = None
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
119
        if email and email in self.__avatars:
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
120
            self.avatar = self.__avatars[email]
121
        else:
122
            self.avatar = None
123
        return self
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
124
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
125
    def update_avatar_image_from_pixbuf_loader(self, loader):
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
126
        """Replace the Gtk.Spinner with the given loader."""
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
127
        if self.avatar:
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
128
            self.avatar.image = Gtk.Image()
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
129
            self.avatar.image.set_from_pixbuf(loader.get_pixbuf())
130
            self.avatar.show_image()
131
            self.__displaying = self.avatar
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
132
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
133
    def come_back_to_gui(self):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
134
        """Render back avatar in the GUI."""
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
135
        if self.avatar:
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
136
            self.pack_start(self.avatar, True, True, 0)
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
137
            self.__displaying = self.avatar
138
        else:
139
            self._show_no_email()
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
140
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
141
    def _new_cell_for_avatar(self, avatar):
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
142
        """Create a new cell in this box with a Gtk.Spinner."""
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
143
        avatar.show_spinner()
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
144
        self.pack_start(avatar, True, True, 0)
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
145
        avatar.show()
146
        self.__displaying = avatar
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
147
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
148
    def _show_no_email(self):
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
149
        """Show a Gtk.Label with test 'No email'."""
150
        no_email = Gtk.Label(label=_i18n("No email"))
151
        self.pack_start(no_email, True, True, 0)
719.2.4 by zedtux
Moved Avatar and AvatarBox classes into avatarsbox file
152
        no_email.show()
153
154
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
155
class AvatarsBox(Gtk.HBox):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
156
    """GTK container for author and committer avatars."""
157
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
158
    def __init__(self):
734.1.6 by Curtis Hovey
Added a very rough fix to see gannontation.
159
        Gtk.HBox.__init__(self, homogeneous=False, spacing=10)
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
160
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
161
        self.__committer_box = None
162
        self.__authors_box = None
163
        self._init_gui()
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
164
165
        # If more later you want to implement more avatar providers:
166
        # * Create a new class named AvatarProvider + provider_name that
167
        #   inherit from the AvatarProvider class.
168
        # * Implement a method that return url to use in the request.
169
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
170
        # For example, with Gravatar, the method return the complete url
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
171
        # with MD5 hash of the email address and put the value in a
172
        # gravatar_id field.
173
        # Then create a new worker (manage them in a python dictionary).
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
174
        provider = AvatarProviderGravatar()
175
        self.__worker = AvatarDownloaderWorker(
176
            provider.gravatar_id_for_email
177
        )
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
178
        # This callback method should be fired by all workers when a request
179
        # is done.
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
180
        self.__worker.set_callback_method(self._update_avatar_from_response)
181
        self.__worker.start()
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
182
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
183
    def add(self, username, role):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
184
        """Add the given username in the role box and add in the worker queue.
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
185
        """
186
        avatar = Avatar(username)
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
187
        if (role == "author" and not self._role_box_for("committer").showing(avatar)) or role == "committer":
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
188
            if self._role_box_for(role).append_avatars_with(avatar):
189
                self.__worker.queue(avatar.email)
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
190
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
191
    def merge(self, usernames, role):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
192
        """Add avatars from a list"""
719.2.2 by zedtux
Patched code following Jelmer's recommandations
193
        for username in usernames:
194
            self.add(username, role)
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
195
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
196
    def reset(self):
197
        """
198
        Request a reset view for all boxes in order to show only avatars
199
        of the selected line in the revision view screen.
200
        """
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
201
        for role in ("committer", "author"):
202
            self._role_box_for(role).reset_view()
203
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
204
    def _init_gui(self):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
205
        """Create boxes where avatars will be displayed."""
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
206
        # 2 Gtk.HBox: One for the committer and one for authors
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
207
        # Committer
208
        self.__committer_box = AvatarBox()
209
        self.__committer_box.set_size_request(80, 80)
734.1.6 by Curtis Hovey
Added a very rough fix to see gannontation.
210
        self.pack_end(self.__committer_box, False, True, 0)
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
211
        self.__committer_box.show()
212
        # Authors
213
        self.__authors_box = AvatarBox()
734.1.6 by Curtis Hovey
Added a very rough fix to see gannontation.
214
        self.pack_end(self.__authors_box, False, True, 0)
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
215
        self.__authors_box.set_spacing(10)
216
        self.__authors_box.show()
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
217
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
218
    def _update_avatar_from_response(self, response, email):
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
219
        """Callback method fired by avatar worker when finished.
220
221
        :param response: a urllib2.urlopen() return value.
222
        :param email: used to identify item from self.__avatars.
223
        """
734.1.6 by Curtis Hovey
Added a very rough fix to see gannontation.
224
        # XXX sinzui 2011-08-01: This must be implemented in cairo
225
#        if email:
226
#            # Convert downloaded image from provider to Gtk.Image
227
#            loader = Gdk.GdkPixbuf.PixbufLoader()
228
#            loader.write(response.read())
229
#            loader.close()
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
230
734.1.6 by Curtis Hovey
Added a very rough fix to see gannontation.
231
#            for role in ["committer", "author"]:
232
#                self._role_box_for(role).and_avatar_email(email).update_avatar_image_from_pixbuf_loader(loader)
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
233
719.2.1 by zedtux
Added committers and authors avatars from Gravatar (urllib2 version)
234
    def _role_box_for(self, role):
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
235
        """ Return the Gtk.HBox for the given role """
728 by Jelmer Vernooij
Cleanups, fix compatibility with older versions of pygtk.
236
        if role == "committer":
237
            return self.__committer_box
238
        else:
239
            return self.__authors_box