/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: zedtux
  • Date: 2011-03-11 14:59:04 UTC
  • mto: This revision was merged to the branch mainline in revision 723.
  • Revision ID: zedtux@zedroot.org-20110311145904-g0cq6drvweriy9hz
Added committers and authors avatars from Gravatar (urllib2 version)

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.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
 
23
 
 
24
class AvatarsBox(gtk.HBox):
 
25
    """ GTK container for authors and committers avatars """
 
26
    
 
27
    def __init__(self):
 
28
        """ Constructor """
 
29
        gtk.HBox.__init__(self, False, 10)
 
30
        
 
31
        self.__committer_box = None
 
32
        self.__authors_box = None
 
33
        self._init_gui()
 
34
        
 
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
 
45
        )
 
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)
 
48
        self.__worker.start()
 
49
    
 
50
    
 
51
    # ~~~~~ Public methods ~~~~~
 
52
    def add(self, username, role):
 
53
        """
 
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.
 
57
        """
 
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)
 
62
    
 
63
    def merge(self, usernames, role):
 
64
        """ Add avatars from a list """
 
65
        [self.add(username, role) for username in usernames]
 
66
    
 
67
    def reset(self):
 
68
        """
 
69
        Request a reset view for all boxes in order to show only avatars
 
70
        of the selected line in the revision view screen.
 
71
        """
 
72
        [self._role_box_for(role).reset_view() for role in ["committer", "author"]]
 
73
    
 
74
    
 
75
    # ~~~~~ Private methods ~~~~~
 
76
    def _init_gui(self):
 
77
        """ Create boxes where avatars will be displayed """
 
78
        # 2 gtk.HBox: One for the committer and one for authors
 
79
        # Committer
 
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()
 
84
        # Authors
 
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()
 
89
    
 
90
    def _update_avatar_from_response(self, response, email):
 
91
        """
 
92
        Callback method fired by avatar worker when finished.
 
93
        
 
94
        response is a urllib2.urlopen() return value.
 
95
        email is used to identify item from self.__avatars.
 
96
        """
 
97
        if not email is "":
 
98
            # Convert downloaded image from provider to gtk.Image
 
99
            loader = gtk.gdk.PixbufLoader()
 
100
            loader.write(response.read())
 
101
            loader.close()
 
102
            
 
103
            for role in ["committer", "author"]:
 
104
                self._role_box_for(role).and_avatar_email(email).update_avatar_image_from_pixbuf_loader(loader)
 
105
    
 
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