/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-06-29 16:20:15 UTC
  • mto: This revision was merged to the branch mainline in revision 519.
  • Revision ID: jelmer@samba.org-20080629162015-amhe7xj4cdmup4id
Rename GtkProgressBarStack to GtkWindowProgressBarStack

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