~ubuntu-branches/ubuntu/wily/kazam/wily-proposed

« back to all changes in this revision

Viewing changes to .pc/Gtk.StatusIconFix.patch/kazam/frontend/indicator.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2013-06-24 17:21:15 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130624172115-s10n31szr3x7th5m
Tags: 1.4.3-1
* New upstream release.
* Gtk.StatusIconFix.patch: Fix taking screenshots while using
  the Gtk.StatusIcon fallback by providing indicator.hide_it()
  and indicator.show_it()

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
#       indicator.py
 
4
#
 
5
#       Copyright 2012 David Klasinc <bigwhale@lubica.net>
 
6
#       Copyright 2010 Andrew <andrew@karmic-desktop>
 
7
#
 
8
#       This program is free software; you can redistribute it and/or modify
 
9
#       it under the terms of the GNU General Public License as published by
 
10
#       the Free Software Foundation; either version 3 of the License, or
 
11
#       (at your option) any later version.
 
12
#
 
13
#       This program is distributed in the hope that it will be useful,
 
14
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#       GNU General Public License for more details.
 
17
#
 
18
#       You should have received a copy of the GNU General Public License
 
19
#       along with this program; if not, write to the Free Software
 
20
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
21
#       MA 02110-1301, USA.
 
22
 
 
23
import logging
 
24
logger = logging.getLogger("Indicator")
 
25
 
 
26
from gettext import gettext as _
 
27
from gi.repository import Gtk, GObject, GLib
 
28
 
 
29
from kazam.backend.constants import *
 
30
 
 
31
class KazamSuperIndicator(GObject.GObject):
 
32
    __gsignals__ = {
 
33
        "indicator-pause-request" : (GObject.SIGNAL_RUN_LAST,
 
34
                                     None,
 
35
                                     (), ),
 
36
        "indicator-unpause-request" : (GObject.SIGNAL_RUN_LAST,
 
37
                                       None,
 
38
                                       (), ),
 
39
        "indicator-quit-request" : (GObject.SIGNAL_RUN_LAST,
 
40
                                    None,
 
41
                                    (), ),
 
42
        "indicator-show-request" : (GObject.SIGNAL_RUN_LAST,
 
43
                                    None,
 
44
                                    (), ),
 
45
        "indicator-stop-request" : (GObject.SIGNAL_RUN_LAST,
 
46
                                    None,
 
47
                                    (), ),
 
48
        "indicator-start-request" : (GObject.SIGNAL_RUN_LAST,
 
49
                                     None,
 
50
                                     (), ),
 
51
 
 
52
        "indicator-about-request" : (GObject.SIGNAL_RUN_LAST,
 
53
                                     None,
 
54
                                     (), ),
 
55
        }
 
56
 
 
57
    def __init__(self, silent = False):
 
58
        super(KazamSuperIndicator, self).__init__()
 
59
        self.blink_icon = BLINK_STOP_ICON
 
60
        self.blink_state = False
 
61
        self.blink_mode = BLINK_SLOW
 
62
        self.recording = False
 
63
        self.silent = silent
 
64
        logger.debug("Indicatior silent: {0}".format(self.silent))
 
65
 
 
66
        self.menu = Gtk.Menu()
 
67
 
 
68
        self.menuitem_start = Gtk.MenuItem(_("Start recording"))
 
69
        self.menuitem_start.set_sensitive(True)
 
70
        self.menuitem_start.connect("activate", self.on_menuitem_start_activate)
 
71
 
 
72
        self.menuitem_pause = Gtk.CheckMenuItem(_("Pause recording"))
 
73
        self.menuitem_pause.set_sensitive(False)
 
74
        self.menuitem_pause.connect("toggled", self.on_menuitem_pause_activate)
 
75
 
 
76
        self.menuitem_finish = Gtk.MenuItem(_("Finish recording"))
 
77
        self.menuitem_finish.set_sensitive(False)
 
78
        self.menuitem_finish.connect("activate", self.on_menuitem_finish_activate)
 
79
 
 
80
        self.menuitem_separator = Gtk.SeparatorMenuItem()
 
81
 
 
82
        self.menuitem_quit = Gtk.MenuItem(_("Quit"))
 
83
        self.menuitem_quit.connect("activate", self.on_menuitem_quit_activate)
 
84
 
 
85
        self.menu.append(self.menuitem_start)
 
86
        self.menu.append(self.menuitem_pause)
 
87
        self.menu.append(self.menuitem_finish)
 
88
        self.menu.append(self.menuitem_separator)
 
89
        self.menu.append(self.menuitem_quit)
 
90
 
 
91
        self.menu.show_all()
 
92
 
 
93
        #
 
94
        # Setup keybindings - Hardcore way
 
95
        #
 
96
        try:
 
97
            from gi.repository import Keybinder
 
98
            logger.debug("Trying to bind hotkeys.")
 
99
            Keybinder.init()
 
100
            Keybinder.bind("<Super><Ctrl>R", self.cb_hotkeys, "start-request")
 
101
            Keybinder.bind("<Super><Ctrl>F", self.cb_hotkeys, "stop-request")
 
102
            Keybinder.bind("<Super><Ctrl>P", self.cb_hotkeys, "pause-request")
 
103
            Keybinder.bind("<Super><Ctrl>W", self.cb_hotkeys, "show-request")
 
104
            Keybinder.bind("<Super><Ctrl>Q", self.cb_hotkeys, "quit-request")
 
105
            self.recording = False
 
106
        except ImportError:
 
107
            logger.info("Unable to import Keybinder, hotkeys not available.")
 
108
 
 
109
    def cb_hotkeys(self, key, action):
 
110
        logger.debug("KEY {0}, ACTION {1}".format(key, action))
 
111
        if action == "start-request" and not self.recording:
 
112
            self.on_menuitem_start_activate(None)
 
113
        elif action == "stop-request" and self.recording:
 
114
            self.on_menuitem_finish_activate(None)
 
115
        elif action == "pause-request" and self.recording:
 
116
            if not self.menuitem_pause.get_active():
 
117
                self.menuitem_pause.set_active(True)
 
118
            else:
 
119
                self.menuitem_pause.set_active(False)
 
120
        elif action == "show-request" and not self.recording:
 
121
            self.emit("indicator-show-request")
 
122
        elif action == "quit-request" and not self.recording:
 
123
            self.emit("indicator-quit-request")
 
124
 
 
125
    def on_menuitem_pause_activate(self, menuitem):
 
126
        if self.menuitem_pause.get_active():
 
127
            self.emit("indicator-pause-request")
 
128
        else:
 
129
            self.emit("indicator-unpause-request")
 
130
 
 
131
    def on_menuitem_start_activate(self, menuitem):
 
132
        self.recording = True
 
133
        self.emit("indicator-start-request")
 
134
 
 
135
    def on_menuitem_finish_activate(self, menuitem):
 
136
        self.recording = False
 
137
        self.menuitem_start.set_sensitive(True)
 
138
        self.menuitem_pause.set_sensitive(False)
 
139
        self.menuitem_pause.set_active(False)
 
140
        self.menuitem_finish.set_sensitive(False)
 
141
        self.menuitem_quit.set_sensitive(True)
 
142
        self.emit("indicator-stop-request")
 
143
 
 
144
    def on_menuitem_quit_activate(self, menuitem):
 
145
        self.emit("indicator-quit-request")
 
146
 
 
147
try:
 
148
    from gi.repository import AppIndicator3
 
149
 
 
150
    class KazamIndicator(KazamSuperIndicator):
 
151
        def __init__(self, silent = False):
 
152
            super(KazamIndicator, self).__init__(silent)
 
153
            self.silent = silent
 
154
 
 
155
            self.indicator = AppIndicator3.Indicator.new("kazam",
 
156
                             "kazam-stopped",
 
157
                             AppIndicator3.IndicatorCategory.APPLICATION_STATUS)
 
158
 
 
159
            self.indicator.set_menu(self.menu)
 
160
            self.indicator.set_attention_icon("kazam-recording")
 
161
            self.indicator.set_icon("kazam-stopped")
 
162
 
 
163
            if self.silent:
 
164
                self.indicator.set_status(AppIndicator3.IndicatorStatus.PASSIVE)
 
165
            else:
 
166
                self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
 
167
 
 
168
        def hide_it(self):
 
169
            self.indicator.set_status(AppIndicator3.IndicatorStatus.PASSIVE)
 
170
 
 
171
        def show_it(self):
 
172
            self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
 
173
 
 
174
        def on_menuitem_pause_activate(self, menuitem):
 
175
            if menuitem.get_active():
 
176
                self.indicator.set_attention_icon("kazam-paused")
 
177
                logger.debug("Recording paused.")
 
178
            else:
 
179
                self.indicator.set_attention_icon("kazam-recording")
 
180
                logger.debug("Recording resumed.")
 
181
            KazamSuperIndicator.on_menuitem_pause_activate(self, menuitem)
 
182
 
 
183
        def on_menuitem_finish_activate(self, menuitem):
 
184
            logger.debug("Recording stopped.")
 
185
            if not self.silent:
 
186
                self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
 
187
            KazamSuperIndicator.on_menuitem_finish_activate(self, menuitem)
 
188
 
 
189
        def blink_set_state(self, state):
 
190
            if state == BLINK_STOP:
 
191
                self.blink_state = BLINK_STOP
 
192
                self.indicator.set_icon("kazam-stopped")
 
193
            elif state == BLINK_START:
 
194
                self.blink_state = BLINK_SLOW
 
195
                GLib.timeout_add(500, self.blink)
 
196
            elif state == BLINK_SLOW:
 
197
                self.blink_state = BLINK_SLOW
 
198
            elif state == BLINK_FAST:
 
199
                self.blink_state = BLINK_FAST
 
200
 
 
201
        def blink(self):
 
202
            if self.blink_state != BLINK_STOP:
 
203
                if self.blink_icon == BLINK_READY_ICON:
 
204
                    if not self.silent:
 
205
                        self.indicator.set_icon("kazam-stopped")
 
206
                    self.blink_icon = BLINK_STOP_ICON
 
207
                else:
 
208
                    if not self.silent:
 
209
                        self.indicator.set_icon("kazam-countdown")
 
210
                    self.blink_icon = BLINK_READY_ICON
 
211
 
 
212
                if self.blink_state == BLINK_SLOW:
 
213
                    GLib.timeout_add(500, self.blink)
 
214
                elif self.blink_state == BLINK_FAST:
 
215
                    GLib.timeout_add(200, self.blink)
 
216
 
 
217
        def start_recording(self):
 
218
            logger.debug("Recording started.")
 
219
            if not self.silent:
 
220
                self.indicator.set_status(AppIndicator3.IndicatorStatus.ATTENTION)
 
221
 
 
222
except ImportError:
 
223
    #
 
224
    # AppIndicator failed to import, not running Ubuntu?
 
225
    # Fallback to Gtk.StatusIcon.
 
226
    #
 
227
    class KazamIndicator(KazamSuperIndicator):
 
228
 
 
229
        def __init__(self, silent = False):
 
230
            super(KazamIndicator, self).__init__()
 
231
            self.silent = silent
 
232
 
 
233
            self.indicator = Gtk.StatusIcon()
 
234
            self.indicator.set_from_icon_name("kazam-stopped")
 
235
            self.indicator.connect("popup-menu", self.cb_indicator_popup_menu)
 
236
            self.indicator.connect("activate", self.cb_indicator_activate)
 
237
 
 
238
            if self.silent:
 
239
                self.indicator.set_visible(False)
 
240
 
 
241
        def cb_indicator_activate(self, widget):
 
242
            def position(menu, widget):
 
243
                return (Gtk.StatusIcon.position_menu(self.menu, widget))
 
244
            self.menu.popup(None, None, position, self.indicator, 0, Gtk.get_current_event_time())
 
245
 
 
246
        def cb_indicator_popup_menu(self, icon, button, time):
 
247
            def position(menu, icon):
 
248
                return (Gtk.StatusIcon.position_menu(self.menu, icon))
 
249
            self.menu.popup(None, None, position, self.indicator, button, time)
 
250
 
 
251
        def on_menuitem_finish_activate(self, menuitem):
 
252
            logger.debug("Recording stopped.")
 
253
            self.indicator.set_from_icon_name("kazam-stopped")
 
254
            KazamSuperIndicator.on_menuitem_finish_activate(self, menuitem)
 
255
 
 
256
        def on_menuitem_pause_activate(self, menuitem):
 
257
            if menuitem.get_active():
 
258
                self.indicator.set_from_icon_name("kazam-paused")
 
259
                logger.debug("Recording paused.")
 
260
            else:
 
261
                self.indicator.set_from_icon_name("kazam-recording")
 
262
                logger.debug("Recording resumed.")
 
263
            KazamSuperIndicator.on_menuitem_pause_activate(self, menuitem)
 
264
 
 
265
        def blink_set_state(self, state):
 
266
            if state == BLINK_STOP:
 
267
                self.blink_state = BLINK_STOP
 
268
                self.indicator.set_from_icon_name("kazam-stopped")
 
269
            elif state == BLINK_START:
 
270
                self.blink_state = BLINK_SLOW
 
271
                GLib.timeout_add(500, self.blink)
 
272
            elif state == BLINK_SLOW:
 
273
                self.blink_state = BLINK_SLOW
 
274
            elif state == BLINK_FAST:
 
275
                self.blink_state = BLINK_FAST
 
276
 
 
277
        def blink(self):
 
278
            if self.blink_state != BLINK_STOP:
 
279
                if self.blink_icon == BLINK_READY_ICON:
 
280
                    self.indicator.set_from_icon_name("kazam-stopped")
 
281
                    self.blink_icon = BLINK_STOP_ICON
 
282
                else:
 
283
                    self.indicator.set_from_icon_name("kazam-countdown")
 
284
                    self.blink_icon = BLINK_READY_ICON
 
285
 
 
286
                if self.blink_state == BLINK_SLOW:
 
287
                    GLib.timeout_add(500, self.blink)
 
288
                elif self.blink_state == BLINK_FAST:
 
289
                    GLib.timeout_add(200, self.blink)
 
290
 
 
291
        def start_recording(self):
 
292
            logger.debug("Recording started.")
 
293
            self.indicator.set_from_icon_name("kazam-recording")