/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to avatarsbox.py

  • Committer: Jelmer Vernooij
  • Date: 2008-07-17 11:51:03 UTC
  • Revision ID: jelmer@samba.org-20080717115103-djh5sb0pvpse2zkb
Add note about glade.

Show diffs side-by-side

added added

removed removed

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