~kazam-team/kazam/unstable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# -*- coding: utf-8 -*-
#
#       indicator.py
#
#       Copyright 2012 David Klasinc <bigwhale@lubica.net>
#       Copyright 2010 Andrew <andrew@karmic-desktop>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 3 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import logging
logger = logging.getLogger("Indicator")

from gettext import gettext as _
from gi.repository import Gtk, GObject, GLib

from kazam.backend.prefs import *


class KazamSuperIndicator(GObject.GObject):
    __gsignals__ = {
        "indicator-pause-request": (GObject.SIGNAL_RUN_LAST,
                                    None,
                                    (), ),
        "indicator-unpause-request": (GObject.SIGNAL_RUN_LAST,
                                      None,
                                      (), ),
        "indicator-quit-request": (GObject.SIGNAL_RUN_LAST,
                                   None,
                                   (), ),
        "indicator-show-request": (GObject.SIGNAL_RUN_LAST,
                                   None,
                                   (), ),
        "indicator-stop-request": (GObject.SIGNAL_RUN_LAST,
                                   None,
                                   (), ),
        "indicator-start-request": (GObject.SIGNAL_RUN_LAST,
                                    None,
                                    (), ),

        "indicator-about-request": (GObject.SIGNAL_RUN_LAST,
                                    None,
                                    (), ),
    }

    def __init__(self, silent=False):
        super(KazamSuperIndicator, self).__init__()
        self.blink_icon = BLINK_STOP_ICON
        self.blink_state = False
        self.blink_mode = BLINK_SLOW
        self.recording = False
        self.silent = silent
        logger.debug("Indicatior silent: {0}".format(self.silent))

        self.menu = Gtk.Menu()

        self.menuitem_start = Gtk.MenuItem(_("Start recording"))
        self.menuitem_start.set_sensitive(True)
        self.menuitem_start.connect("activate", self.on_menuitem_start_activate)

        self.menuitem_pause = Gtk.CheckMenuItem(_("Pause recording"))
        self.menuitem_pause.set_sensitive(False)
        self.menuitem_pause.connect("toggled", self.on_menuitem_pause_activate)

        self.menuitem_finish = Gtk.MenuItem(_("Finish recording"))
        self.menuitem_finish.set_sensitive(False)
        self.menuitem_finish.connect("activate", self.on_menuitem_finish_activate)

        self.menuitem_separator = Gtk.SeparatorMenuItem()

        self.menuitem_quit = Gtk.MenuItem(_("Quit"))
        self.menuitem_quit.connect("activate", self.on_menuitem_quit_activate)

        self.menu.append(self.menuitem_start)
        self.menu.append(self.menuitem_pause)
        self.menu.append(self.menuitem_finish)
        self.menu.append(self.menuitem_separator)
        self.menu.append(self.menuitem_quit)

        self.menu.show_all()

        #
        # Setup keybindings - Hardcore way
        #
        try:
            from gi.repository import Keybinder
            logger.debug("Trying to bind hotkeys.")
            Keybinder.init()
            Keybinder.bind("<Super><Ctrl>R", self.cb_hotkeys, "start-request")
            Keybinder.bind("<Super><Ctrl>F", self.cb_hotkeys, "stop-request")
            Keybinder.bind("<Super><Ctrl>P", self.cb_hotkeys, "pause-request")
            Keybinder.bind("<Super><Ctrl>W", self.cb_hotkeys, "show-request")
            Keybinder.bind("<Super><Ctrl>Q", self.cb_hotkeys, "quit-request")
            self.recording = False
        except ImportError:
            logger.info("Unable to import Keybinder, hotkeys not available.")

    def cb_hotkeys(self, key, action):
        logger.debug("KEY {0}, ACTION {1}".format(key, action))
        if action == "start-request" and not self.recording:
            self.on_menuitem_start_activate(None)
        elif action == "stop-request" and self.recording:
            self.on_menuitem_finish_activate(None)
        elif action == "pause-request" and self.recording:
            if not self.menuitem_pause.get_active():
                self.menuitem_pause.set_active(True)
            else:
                self.menuitem_pause.set_active(False)
        elif action == "show-request" and not self.recording:
            self.emit("indicator-show-request")
        elif action == "quit-request" and not self.recording:
            self.emit("indicator-quit-request")

    def on_menuitem_pause_activate(self, menuitem):
        if self.menuitem_pause.get_active():
            self.emit("indicator-pause-request")
        else:
            self.emit("indicator-unpause-request")

    def on_menuitem_start_activate(self, menuitem):
        self.recording = True
        self.emit("indicator-start-request")

    def on_menuitem_finish_activate(self, menuitem):
        self.recording = False
        self.menuitem_start.set_sensitive(True)
        self.menuitem_pause.set_sensitive(False)
        self.menuitem_pause.set_active(False)
        self.menuitem_finish.set_sensitive(False)
        self.menuitem_quit.set_sensitive(True)
        self.emit("indicator-stop-request")

    def on_menuitem_quit_activate(self, menuitem):
        self.emit("indicator-quit-request")

try:
    from gi.repository import AppIndicator3

    class KazamIndicator(KazamSuperIndicator):

        def __init__(self, silent=False):
            super(KazamIndicator, self).__init__(silent)
            self.silent = silent

            self.indicator = AppIndicator3.Indicator.new("kazam",
                                                         "kazam-stopped",
                                                         AppIndicator3.IndicatorCategory.APPLICATION_STATUS)

            self.indicator.set_menu(self.menu)
            self.indicator.set_attention_icon("kazam-recording")
            self.indicator.set_icon("kazam-stopped")

            if self.silent:
                self.indicator.set_status(AppIndicator3.IndicatorStatus.PASSIVE)
            else:
                self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)

        def hide_it(self):
            self.indicator.set_status(AppIndicator3.IndicatorStatus.PASSIVE)

        def show_it(self):
            self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)

        def on_menuitem_pause_activate(self, menuitem):
            if menuitem.get_active():
                self.indicator.set_attention_icon("kazam-paused")
                logger.debug("Recording paused.")
            else:
                self.indicator.set_attention_icon("kazam-recording")
                logger.debug("Recording resumed.")
            KazamSuperIndicator.on_menuitem_pause_activate(self, menuitem)

        def on_menuitem_finish_activate(self, menuitem):
            logger.debug("Recording stopped.")
            if not self.silent:
                self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
            KazamSuperIndicator.on_menuitem_finish_activate(self, menuitem)

        def blink_set_state(self, state):
            if state == BLINK_STOP:
                self.blink_state = BLINK_STOP
                self.indicator.set_icon("kazam-stopped")
            elif state == BLINK_START:
                self.blink_state = BLINK_SLOW
                GLib.timeout_add(500, self.blink)
            elif state == BLINK_SLOW:
                self.blink_state = BLINK_SLOW
            elif state == BLINK_FAST:
                self.blink_state = BLINK_FAST

        def blink(self):
            if self.blink_state != BLINK_STOP:
                if self.blink_icon == BLINK_READY_ICON:
                    if not self.silent:
                        self.indicator.set_icon("kazam-stopped")
                    self.blink_icon = BLINK_STOP_ICON
                else:
                    if not self.silent:
                        self.indicator.set_icon("kazam-countdown")
                    self.blink_icon = BLINK_READY_ICON

                if self.blink_state == BLINK_SLOW:
                    GLib.timeout_add(500, self.blink)
                elif self.blink_state == BLINK_FAST:
                    GLib.timeout_add(200, self.blink)

        def start_recording(self):
            logger.debug("Recording started.")
            if not self.silent:
                self.indicator.set_status(AppIndicator3.IndicatorStatus.ATTENTION)

except ImportError:
    #
    # AppIndicator failed to import, not running Ubuntu?
    # Fallback to Gtk.StatusIcon.
    #
    class KazamIndicator(KazamSuperIndicator):

        def __init__(self, silent=False):
            super(KazamIndicator, self).__init__()
            self.silent = silent

            self.indicator = Gtk.StatusIcon()
            self.indicator.set_from_icon_name("kazam-stopped")
            self.indicator.connect("popup-menu", self.cb_indicator_popup_menu)
            self.indicator.connect("activate", self.cb_indicator_activate)

            if self.silent:
                self.indicator.set_visible(False)

        def cb_indicator_activate(self, widget):
            def position(menu, widget):
                return (Gtk.StatusIcon.position_menu(self.menu, widget))
            self.menu.popup(None, None, position, self.indicator, 0, Gtk.get_current_event_time())

        def cb_indicator_popup_menu(self, icon, button, time):
            def position(menu, icon):
                return (Gtk.StatusIcon.position_menu(self.menu, icon))
            self.menu.popup(None, None, position, self.indicator, button, time)

        def on_menuitem_finish_activate(self, menuitem):
            logger.debug("Recording stopped.")
            self.indicator.set_from_icon_name("kazam-stopped")
            KazamSuperIndicator.on_menuitem_finish_activate(self, menuitem)

        def on_menuitem_pause_activate(self, menuitem):
            if menuitem.get_active():
                self.indicator.set_from_icon_name("kazam-paused")
                logger.debug("Recording paused.")
            else:
                self.indicator.set_from_icon_name("kazam-recording")
                logger.debug("Recording resumed.")
            KazamSuperIndicator.on_menuitem_pause_activate(self, menuitem)

        def blink_set_state(self, state):
            if state == BLINK_STOP:
                self.blink_state = BLINK_STOP
                self.indicator.set_from_icon_name("kazam-stopped")
            elif state == BLINK_START:
                self.blink_state = BLINK_SLOW
                GLib.timeout_add(500, self.blink)
            elif state == BLINK_SLOW:
                self.blink_state = BLINK_SLOW
            elif state == BLINK_FAST:
                self.blink_state = BLINK_FAST

        def blink(self):
            if self.blink_state != BLINK_STOP:
                if self.blink_icon == BLINK_READY_ICON:
                    self.indicator.set_from_icon_name("kazam-stopped")
                    self.blink_icon = BLINK_STOP_ICON
                else:
                    self.indicator.set_from_icon_name("kazam-countdown")
                    self.blink_icon = BLINK_READY_ICON

                if self.blink_state == BLINK_SLOW:
                    GLib.timeout_add(500, self.blink)
                elif self.blink_state == BLINK_FAST:
                    GLib.timeout_add(200, self.blink)

        def start_recording(self):
            logger.debug("Recording started.")
            self.indicator.set_from_icon_name("kazam-recording")

        def hide_it(self):
            self.indicator.set_visible(False)

        def show_it(self):
            self.indicator.set_visible(True)