~ubuntu-branches/ubuntu/vivid/gpodder/vivid-proposed

« back to all changes in this revision

Viewing changes to share/gpodder/extensions/gtk_statusicon.py

  • Committer: Package Import Robot
  • Author(s): tony mancill
  • Date: 2014-03-20 21:41:43 UTC
  • mfrom: (5.2.30 sid)
  • Revision ID: package-import@ubuntu.com-20140320214143-f8ogn4hu7s9j7c43
Tags: 3.6.1-1
* New upstream release (Closes: #742191)
  - Fixes YouTube integration bug.
  - Use LC_ALL=C during manpage generation.
  - Add prefix to path in desktop file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 
7
7
import gpodder
8
8
 
 
9
import logging
 
10
logger = logging.getLogger(__name__)
 
11
 
9
12
_ = gpodder.gettext
10
13
 
11
14
__title__ = _('Gtk Status Icon')
17
20
import gtk
18
21
import os.path
19
22
 
 
23
from gpodder.gtkui import draw
 
24
 
 
25
DefaultConfig = {
 
26
    'download_progress_bar': False, # draw progress bar on icon while downloading?
 
27
}
 
28
 
20
29
class gPodderExtension:
21
30
    def __init__(self, container):
22
31
        self.container = container
 
32
        self.config = self.container.config
23
33
        self.status_icon = None
 
34
        self.icon_name = None
24
35
        self.gpodder = None
 
36
        self.last_progress = 1
25
37
 
26
 
    def on_load(self):
 
38
    def set_icon(self, use_pixbuf=False):
27
39
        path = os.path.join(os.path.dirname(__file__), '..', '..', 'icons')
28
40
        icon_path = os.path.abspath(path)
29
41
 
30
42
        theme = gtk.icon_theme_get_default()
31
43
        theme.append_search_path(icon_path)
32
44
 
33
 
        if theme.has_icon('gpodder'):
34
 
            self.status_icon = gtk.status_icon_new_from_icon_name('gpodder')
 
45
        if self.icon_name is None:
 
46
            if theme.has_icon('gpodder'):
 
47
                self.icon_name = 'gpodder'
 
48
            else:
 
49
                self.icon_name = 'stock_mic'
 
50
 
 
51
        if self.status_icon is None:
 
52
            self.status_icon = gtk.status_icon_new_from_icon_name(self.icon_name)
 
53
            return
 
54
 
 
55
        # If current mode matches desired mode, nothing to do.
 
56
        is_pixbuf = (self.status_icon.get_storage_type() == gtk.IMAGE_PIXBUF)
 
57
        if is_pixbuf == use_pixbuf:
 
58
            return
 
59
 
 
60
        if not use_pixbuf:
 
61
            self.status_icon.set_from_icon_name(self.icon_name)
35
62
        else:
36
 
            self.status_icon = gtk.status_icon_new_from_icon_name('stock_mic')
 
63
            # Currently icon is not a pixbuf => was loaded by name, at which
 
64
            # point size was automatically determined.
 
65
            icon_size = self.status_icon.get_size()
 
66
            icon_pixbuf = theme.load_icon(self.icon_name, icon_size, gtk.ICON_LOOKUP_USE_BUILTIN)
 
67
            self.status_icon.set_from_pixbuf(icon_pixbuf)
37
68
 
 
69
    def on_load(self):
 
70
        self.set_icon()
38
71
        self.status_icon.connect('activate', self.on_toggle_visible)
39
72
        self.status_icon.set_has_tooltip(True)
40
73
        self.status_icon.set_tooltip_text("gPodder")
50
83
        if self.status_icon is not None:
51
84
            self.status_icon.set_visible(False)
52
85
            self.status_icon = None
 
86
            self.icon_name = None
53
87
 
54
88
    def on_ui_object_available(self, name, ui_object):
55
89
        if name == 'gpodder-gtk':
56
90
            self.gpodder = ui_object
57
91
 
 
92
    def get_icon_pixbuf(self):
 
93
        assert self.status_icon is not None
 
94
        if self.status_icon.get_storage_type() != gtk.IMAGE_PIXBUF:
 
95
            self.set_icon(use_pixbuf=True)
 
96
        return self.status_icon.get_pixbuf()
 
97
 
 
98
    def on_download_progress(self, progress):
 
99
        logger.debug("download progress: %f", progress)
 
100
 
 
101
        if not self.config.download_progress_bar:
 
102
            # reset the icon in case option was turned off during download
 
103
            if self.last_progress < 1:
 
104
                self.last_progress = 1
 
105
                self.set_icon()
 
106
            # in any case, we're now done
 
107
            return
 
108
 
 
109
        if progress == 1:
 
110
            self.set_icon() # no progress bar
 
111
            self.last_progress = progress
 
112
            return
 
113
 
 
114
        # Only update in 3-percent-steps to save some resources
 
115
        if abs(progress-self.last_progress) < 0.03 and progress > self.last_progress:
 
116
            return
 
117
 
 
118
        icon = self.get_icon_pixbuf().copy()
 
119
        progressbar = draw.progressbar_pixbuf(icon.get_width(), icon.get_height(), progress)
 
120
        progressbar.composite(icon, 0, 0, icon.get_width(), icon.get_height(), 0, 0, 1, 1, gtk.gdk.INTERP_NEAREST, 255)
 
121
 
 
122
        self.status_icon.set_from_pixbuf(icon)
 
123
        self.last_progress = progress
58
124