def __init__(self): super(AvatarsBox, self).__init__(homogeneous=False, spacing=10) self.__committer_box = None self.__authors_box = None self._init_gui() # If more later you want to implement more avatar providers: # * Create a new class named AvatarProvider + provider_name that # inherit from the AvatarProvider class. # * Implement a method that return url to use in the request. # For example, with Gravatar, the method return the complete url # with MD5 hash of the email address and put the value in a # gravatar_id field. # Then create a new worker (manage them in a python dictionary). provider = AvatarProviderGravatar() self.__worker = AvatarDownloaderWorker( provider.gravatar_id_for_email ) # This callback method should be fired by all workers when a request # is done. self.__worker.set_callback_method(self._update_avatar_from_response) self.connect('destroy', self.on_destroy)
class AvatarsBox(Gtk.HBox): """GTK container for author and committer avatars.""" def __init__(self): super(AvatarsBox, self).__init__(homogeneous=False, spacing=10) self.__committer_box = None self.__authors_box = None self._init_gui() # If more later you want to implement more avatar providers: # * Create a new class named AvatarProvider + provider_name that # inherit from the AvatarProvider class. # * Implement a method that return url to use in the request. # For example, with Gravatar, the method return the complete url # with MD5 hash of the email address and put the value in a # gravatar_id field. # Then create a new worker (manage them in a python dictionary). provider = AvatarProviderGravatar() self.__worker = AvatarDownloaderWorker( provider.gravatar_id_for_email ) # This callback method should be fired by all workers when a request # is done. self.__worker.set_callback_method(self._update_avatar_from_response) self.connect('destroy', self.on_destroy) def on_destroy(self, widget): self.__worker.stop() if self.__worker.is_alive(): self.__worker.join() def add(self, username, role): """Add the given username in the role box and add in the worker queue. """ avatar = Avatar(username) is_shown = self._role_box_for("committer").showing(avatar) if (role == "author" and not is_shown) or role == "committer": if self._role_box_for(role).append_avatars_with(avatar): self.__worker.queue(avatar.email) def merge(self, usernames, role): """Add avatars from a list""" for username in usernames: self.add(username, role) def reset(self): """ Request a reset view for all boxes in order to show only avatars of the selected line in the revision view screen. """ for role in ("committer", "author"): self._role_box_for(role).reset_view() def _init_gui(self): """Create boxes where avatars will be displayed.""" # 2 Gtk.HBox: One for the committer and one for authors # Committer self.__committer_box = AvatarBox() self.__committer_box.set_size_request(80, 80) self.pack_end(self.__committer_box, False, True, 0) self.__committer_box.show() # Authors self.__authors_box = AvatarBox() self.pack_end(self.__authors_box, False, True, 0) self.__authors_box.set_spacing(10) self.__authors_box.show() def _update_avatar_from_response(self, response, email): """Callback method fired by avatar worker when finished. :param response: a urllib2.urlopen() return value. :param email: used to identify item from self.__avatars. """ if email: # Convert downloaded image from provider to Gtk.Image loader = GdkPixbuf.PixbufLoader() loader.write(response.read()) loader.close() for role in ["committer", "author"]: self._role_box_for(role).and_avatar_email( email).update_avatar_image_from_pixbuf_loader(loader) def _role_box_for(self, role): """ Return the Gtk.HBox for the given role """ if role == "committer": return self.__committer_box else: return self.__authors_box