~ubuntu-branches/ubuntu/wily/blueman/wily-proposed

« back to all changes in this revision

Viewing changes to blueman/plugins/applet/StatusIcon.py

  • Committer: Package Import Robot
  • Author(s): Jackson Doak
  • Date: 2014-01-21 08:54:58 UTC
  • mfrom: (2.3.4 sid)
  • Revision ID: package-import@ubuntu.com-20140121085458-riy3j6wk9vfd599j
Tags: 1.23-git201312311147-1ubuntu1
* Merge from debian unstable. Remaining changes:
  - debian/patches/01_dont_autostart_lxde.patch:
    + Don't autostart the applet in LXDE
  - debian/patches/02_dont_crash_on_non-bluetooth_card.patch:
    + Avoid crashing when receiving event for cards blueman shouldn't handle
  - debian/control: Don't depend on python-appindicator
  - debian/patches/03_filemanager_fix.patch:
    + Add support for more filemanagers 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Valmantas Paliksa <walmis at balticum-tv dot lt>
2
 
# Copyright (C) 2008 Tadas Dailyda <tadas at dailyda dot com>
3
 
#
4
 
# Licensed under the GNU General Public License Version 3
5
 
#
6
 
# This program is free software: you can redistribute it and/or modify
7
 
# it under the terms of the GNU General Public License as published by
8
 
# the Free Software Foundation, either version 3 of the License, or
9
 
# (at your option) any later version.
10
 
#
11
 
# This program is distributed in the hope that it will be useful,
12
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
# GNU General Public License for more details.
15
 
#
16
 
# You should have received a copy of the GNU General Public License
17
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
 
1
from blueman.main.PluginManager import StopException
19
2
from blueman.Functions import *
20
3
from blueman.plugins.AppletPlugin import AppletPlugin
21
4
import gtk
22
5
import gobject
23
6
 
 
7
 
24
8
class StatusIcon(AppletPlugin, gtk.StatusIcon):
25
 
        __unloadable__ = False
26
 
        __icon__ = "blueman"
27
 
        
28
 
        def on_entry_changed(self, entry, ic, image):
29
 
                
30
 
                if ic.has_icon(self.get_option("icon")):
31
 
                        icon = gtk.STOCK_APPLY
32
 
                else:
33
 
                        icon = gtk.STOCK_CANCEL
34
 
                        
35
 
                image.set_from_stock(icon, gtk.ICON_SIZE_LARGE_TOOLBAR)
36
 
                
37
 
                if self.timeout:
38
 
                        gobject.source_remove(self.timeout)
39
 
                        
40
 
                self.timeout = gobject.timeout_add(1000, lambda: self.IconShouldChange())
41
 
        
42
 
        def widget_decorator(self, widget, name, options):
43
 
                entry = widget.get_children()[1]
44
 
                image = gtk.Image()
45
 
                
46
 
                completion = gtk.EntryCompletion()
47
 
                entry.set_completion(completion)
48
 
                
49
 
                liststore = gtk.ListStore(gobject.TYPE_STRING)
50
 
                
51
 
                completion.set_model(liststore)
52
 
                
53
 
                completion.set_text_column(0)
54
 
 
55
 
                ic = gtk.icon_theme_get_default()
56
 
                icons = ic.list_icons("Applications")
57
 
                for i in icons:
58
 
                        liststore.append([i])
59
 
                
60
 
                if ic.has_icon(self.get_option("icon")):
61
 
                        icon = gtk.STOCK_APPLY
62
 
                else:
63
 
                        icon = gtk.STOCK_CANCEL
64
 
                        
65
 
                image.set_from_stock(icon, gtk.ICON_SIZE_LARGE_TOOLBAR)
66
 
                image.show()
67
 
                widget.pack_start(image, 0, 0)
68
 
                entry.connect("changed", self.on_entry_changed, ic, image)
69
 
                
70
 
        __options__ = {"icon": {"type": str,
71
 
                                                        "default": "blueman-tray",
72
 
                                                        "name": _("Icon Name"),
73
 
                                                        "desc": _("Custom icon to use for the notification area"),
74
 
                                                        "decorator": widget_decorator
75
 
                                                   }
76
 
                                  }
77
 
        
78
 
        FORCE_SHOW = 2
79
 
        SHOW = 1
80
 
        FORCE_HIDE = 0
81
 
        
82
 
        def on_load(self, applet):
83
 
                gtk.StatusIcon.__init__(self)
84
 
                self.lines = {}
85
 
                self.pixbuf = None
86
 
                self.timeout = None
87
 
                
88
 
                #self.connect("size-changed", self.on_status_icon_resized)
89
 
                
90
 
                self.SetTextLine(0, _("Bluetooth Enabled"))
91
 
                
92
 
                AppletPlugin.add_method(self.on_query_status_icon_visibility)
93
 
                AppletPlugin.add_method(self.on_status_icon_query_icon)
94
 
                
95
 
                ic = gtk.icon_theme_get_default()
96
 
                ic.connect("changed", self.on_icon_theme_changed)
97
 
                
98
 
                self.on_status_icon_resized()
99
 
                
100
 
        def on_icon_theme_changed(self, icon_theme):
101
 
                self.IconShouldChange()
102
 
                
103
 
        def on_power_state_changed(self, manager, state):
104
 
                if state:
105
 
                        self.SetTextLine(0, _("Bluetooth Enabled"))
106
 
                else:
107
 
                        self.SetTextLine(0, _("Bluetooth Disabled"))
108
 
                        
109
 
                self.QueryVisibility()
110
 
        
111
 
        def QueryVisibility(self):
112
 
                        
113
 
                rets = self.Applet.Plugins.Run("on_query_status_icon_visibility")
114
 
                if not StatusIcon.FORCE_HIDE in rets:
115
 
                        if StatusIcon.FORCE_SHOW in rets:
116
 
                                self.set_visible(True)
117
 
                        else:
118
 
                                if not self.Applet.Manager:
119
 
                                        self.set_visible(False)
120
 
                                        return
121
 
                                        
122
 
                                try:
123
 
                                        if self.Applet.Manager.ListAdapters() == []:
124
 
                                                self.set_visible(False)
125
 
                                        else:
126
 
                                                self.set_visible(True)
127
 
                                except:
128
 
                                        self.set_visible(False)
129
 
                else:
130
 
                        self.set_visible(False)
131
 
                        
132
 
        def set_visible(self, visible):
133
 
                self.props.visible = visible
134
 
                        
135
 
        def SetTextLine(self, id, text):
136
 
                if text:
137
 
                        self.lines[id] = text
138
 
                else:
139
 
                        try:
140
 
                                del self.lines[id]
141
 
                        except:
142
 
                                pass
143
 
                                
144
 
                self.update_tooltip()
145
 
 
146
 
                        
147
 
        def update_tooltip(self):
148
 
                s = ""
149
 
                keys = self.lines.keys()
150
 
                keys.sort()
151
 
                for k in keys:
152
 
                        s += self.lines[k] + "\n"
153
 
                        
154
 
                self.props.tooltip_markup = s[:-1]
155
 
                        
156
 
        def IconShouldChange(self):
157
 
                self.on_status_icon_resized()
158
 
                
159
 
        def on_adapter_added(self, path):
160
 
                self.QueryVisibility()
161
 
                
162
 
        def on_adapter_removed(self, path):
163
 
                self.QueryVisibility()
164
 
        
165
 
        def on_manager_state_changed(self, state):
166
 
                self.QueryVisibility()
167
 
                
168
 
        def on_status_icon_resized(self):
169
 
                self.icon = "blueman-tray"
170
 
                
171
 
                #p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, 22, 22)
172
 
                #p.fill(0)
173
 
                
174
 
                
175
 
                #self.pixbuf.copy_area(0, 0, self.pixbuf.props.width, self.pixbuf.props.height, p, 5, 0)
176
 
                
177
 
                #self.pixbuf = p
178
 
                ic = gtk.icon_theme_get_default()
179
 
                
180
 
                def callback(inst, ret):
181
 
                        if ret != None:
182
 
                                for i in ret:
183
 
                                        if ic.has_icon(i):
184
 
                                                self.icon = i
185
 
                                                raise StopException
186
 
                                
187
 
                self.Applet.Plugins.RunEx("on_status_icon_query_icon", callback)
188
 
                self.props.icon_name = self.icon
189
 
                
190
 
                return True
191
 
                
192
 
        def on_query_status_icon_visibility(self):
193
 
                return StatusIcon.SHOW
194
 
                
195
 
        def on_status_icon_query_icon(self):
196
 
                return None
 
9
    __unloadable__ = False
 
10
    __icon__ = "blueman"
 
11
 
 
12
    def on_entry_changed(self, entry, ic, image):
 
13
 
 
14
        if ic.has_icon(self.get_option("icon")):
 
15
            icon = gtk.STOCK_APPLY
 
16
        else:
 
17
            icon = gtk.STOCK_CANCEL
 
18
 
 
19
        image.set_from_stock(icon, gtk.ICON_SIZE_LARGE_TOOLBAR)
 
20
 
 
21
        if self.timeout:
 
22
            gobject.source_remove(self.timeout)
 
23
 
 
24
        self.timeout = gobject.timeout_add(1000, lambda: self.IconShouldChange())
 
25
 
 
26
    def widget_decorator(self, widget, name, options):
 
27
        entry = widget.get_children()[1]
 
28
        image = gtk.Image()
 
29
 
 
30
        completion = gtk.EntryCompletion()
 
31
        entry.set_completion(completion)
 
32
 
 
33
        liststore = gtk.ListStore(gobject.TYPE_STRING)
 
34
 
 
35
        completion.set_model(liststore)
 
36
 
 
37
        completion.set_text_column(0)
 
38
 
 
39
        ic = gtk.icon_theme_get_default()
 
40
        icons = ic.list_icons("Applications")
 
41
        for i in icons:
 
42
            liststore.append([i])
 
43
 
 
44
        if ic.has_icon(self.get_option("icon")):
 
45
            icon = gtk.STOCK_APPLY
 
46
        else:
 
47
            icon = gtk.STOCK_CANCEL
 
48
 
 
49
        image.set_from_stock(icon, gtk.ICON_SIZE_LARGE_TOOLBAR)
 
50
        image.show()
 
51
        widget.pack_start(image, 0, 0)
 
52
        entry.connect("changed", self.on_entry_changed, ic, image)
 
53
 
 
54
    __options__ = {"icon": {"type": str,
 
55
                            "default": "blueman-tray",
 
56
                            "name": _("Icon Name"),
 
57
                            "desc": _("Custom icon to use for the notification area"),
 
58
                            "decorator": widget_decorator
 
59
    }
 
60
    }
 
61
 
 
62
    FORCE_SHOW = 2
 
63
    SHOW = 1
 
64
    FORCE_HIDE = 0
 
65
 
 
66
    def on_load(self, applet):
 
67
        gtk.StatusIcon.__init__(self)
 
68
        self.lines = {}
 
69
        self.pixbuf = None
 
70
        self.timeout = None
 
71
 
 
72
        #self.connect("size-changed", self.on_status_icon_resized)
 
73
 
 
74
        self.SetTextLine(0, _("Bluetooth Enabled"))
 
75
 
 
76
        AppletPlugin.add_method(self.on_query_status_icon_visibility)
 
77
        AppletPlugin.add_method(self.on_status_icon_query_icon)
 
78
 
 
79
        ic = gtk.icon_theme_get_default()
 
80
        ic.connect("changed", self.on_icon_theme_changed)
 
81
 
 
82
        self.on_status_icon_resized()
 
83
 
 
84
    def on_icon_theme_changed(self, icon_theme):
 
85
        self.IconShouldChange()
 
86
 
 
87
    def on_power_state_changed(self, manager, state):
 
88
        if state:
 
89
            self.SetTextLine(0, _("Bluetooth Enabled"))
 
90
        else:
 
91
            self.SetTextLine(0, _("Bluetooth Disabled"))
 
92
 
 
93
        self.QueryVisibility()
 
94
 
 
95
    def QueryVisibility(self):
 
96
 
 
97
        rets = self.Applet.Plugins.Run("on_query_status_icon_visibility")
 
98
        if not StatusIcon.FORCE_HIDE in rets:
 
99
            if StatusIcon.FORCE_SHOW in rets:
 
100
                self.set_visible(True)
 
101
            else:
 
102
                if not self.Applet.Manager:
 
103
                    self.set_visible(False)
 
104
                    return
 
105
 
 
106
                try:
 
107
                    if self.Applet.Manager.ListAdapters() == []:
 
108
                        self.set_visible(False)
 
109
                    else:
 
110
                        self.set_visible(True)
 
111
                except:
 
112
                    self.set_visible(False)
 
113
        else:
 
114
            self.set_visible(False)
 
115
 
 
116
    def set_visible(self, visible):
 
117
        self.props.visible = visible
 
118
 
 
119
    def SetTextLine(self, id, text):
 
120
        if text:
 
121
            self.lines[id] = text
 
122
        else:
 
123
            try:
 
124
                del self.lines[id]
 
125
            except:
 
126
                pass
 
127
 
 
128
        self.update_tooltip()
 
129
 
 
130
 
 
131
    def update_tooltip(self):
 
132
        s = ""
 
133
        keys = self.lines.keys()
 
134
        keys.sort()
 
135
        for k in keys:
 
136
            s += self.lines[k] + "\n"
 
137
 
 
138
        self.props.tooltip_markup = s[:-1]
 
139
 
 
140
    def IconShouldChange(self):
 
141
        self.on_status_icon_resized()
 
142
 
 
143
    def on_adapter_added(self, path):
 
144
        self.QueryVisibility()
 
145
 
 
146
    def on_adapter_removed(self, path):
 
147
        self.QueryVisibility()
 
148
 
 
149
    def on_manager_state_changed(self, state):
 
150
        self.QueryVisibility()
 
151
 
 
152
    def on_status_icon_resized(self):
 
153
        self.icon = "blueman-tray"
 
154
 
 
155
        #p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, 22, 22)
 
156
        #p.fill(0)
 
157
 
 
158
 
 
159
        #self.pixbuf.copy_area(0, 0, self.pixbuf.props.width, self.pixbuf.props.height, p, 5, 0)
 
160
 
 
161
        #self.pixbuf = p
 
162
        ic = gtk.icon_theme_get_default()
 
163
 
 
164
        def callback(inst, ret):
 
165
            if ret != None:
 
166
                for i in ret:
 
167
                    if ic.has_icon(i):
 
168
                        self.icon = i
 
169
                        raise StopException
 
170
 
 
171
        self.Applet.Plugins.RunEx("on_status_icon_query_icon", callback)
 
172
        self.props.icon_name = self.icon
 
173
 
 
174
        return True
 
175
 
 
176
    def on_query_status_icon_visibility(self):
 
177
        return StatusIcon.SHOW
 
178
 
 
179
    def on_status_icon_query_icon(self):
 
180
        return None
197
181