1
# -*- coding: utf-8 -*-
5
# Copyright 2012 David Klasinc <bigwhale@lubica.net>
6
# Copyright 2010 Andrew <andrew@karmic-desktop>
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.
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.
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,
24
logger = logging.getLogger("Indicator")
26
from gettext import gettext as _
27
from gi.repository import Gtk, GObject, GLib
29
from kazam.backend.constants import *
31
class KazamSuperIndicator(GObject.GObject):
33
"indicator-pause-request" : (GObject.SIGNAL_RUN_LAST,
36
"indicator-unpause-request" : (GObject.SIGNAL_RUN_LAST,
39
"indicator-quit-request" : (GObject.SIGNAL_RUN_LAST,
42
"indicator-show-request" : (GObject.SIGNAL_RUN_LAST,
45
"indicator-stop-request" : (GObject.SIGNAL_RUN_LAST,
48
"indicator-start-request" : (GObject.SIGNAL_RUN_LAST,
52
"indicator-about-request" : (GObject.SIGNAL_RUN_LAST,
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
64
logger.debug("Indicatior silent: {0}".format(self.silent))
66
self.menu = Gtk.Menu()
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)
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)
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)
80
self.menuitem_separator = Gtk.SeparatorMenuItem()
82
self.menuitem_quit = Gtk.MenuItem(_("Quit"))
83
self.menuitem_quit.connect("activate", self.on_menuitem_quit_activate)
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)
94
# Setup keybindings - Hardcore way
97
from gi.repository import Keybinder
98
logger.debug("Trying to bind hotkeys.")
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
107
logger.info("Unable to import Keybinder, hotkeys not available.")
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)
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")
125
def on_menuitem_pause_activate(self, menuitem):
126
if self.menuitem_pause.get_active():
127
self.emit("indicator-pause-request")
129
self.emit("indicator-unpause-request")
131
def on_menuitem_start_activate(self, menuitem):
132
self.recording = True
133
self.emit("indicator-start-request")
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")
144
def on_menuitem_quit_activate(self, menuitem):
145
self.emit("indicator-quit-request")
148
from gi.repository import AppIndicator3
150
class KazamIndicator(KazamSuperIndicator):
151
def __init__(self, silent = False):
152
super(KazamIndicator, self).__init__(silent)
155
self.indicator = AppIndicator3.Indicator.new("kazam",
157
AppIndicator3.IndicatorCategory.APPLICATION_STATUS)
159
self.indicator.set_menu(self.menu)
160
self.indicator.set_attention_icon("kazam-recording")
161
self.indicator.set_icon("kazam-stopped")
164
self.indicator.set_status(AppIndicator3.IndicatorStatus.PASSIVE)
166
self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
169
self.indicator.set_status(AppIndicator3.IndicatorStatus.PASSIVE)
172
self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
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.")
179
self.indicator.set_attention_icon("kazam-recording")
180
logger.debug("Recording resumed.")
181
KazamSuperIndicator.on_menuitem_pause_activate(self, menuitem)
183
def on_menuitem_finish_activate(self, menuitem):
184
logger.debug("Recording stopped.")
186
self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
187
KazamSuperIndicator.on_menuitem_finish_activate(self, menuitem)
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
202
if self.blink_state != BLINK_STOP:
203
if self.blink_icon == BLINK_READY_ICON:
205
self.indicator.set_icon("kazam-stopped")
206
self.blink_icon = BLINK_STOP_ICON
209
self.indicator.set_icon("kazam-countdown")
210
self.blink_icon = BLINK_READY_ICON
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)
217
def start_recording(self):
218
logger.debug("Recording started.")
220
self.indicator.set_status(AppIndicator3.IndicatorStatus.ATTENTION)
224
# AppIndicator failed to import, not running Ubuntu?
225
# Fallback to Gtk.StatusIcon.
227
class KazamIndicator(KazamSuperIndicator):
229
def __init__(self, silent = False):
230
super(KazamIndicator, self).__init__()
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)
239
self.indicator.set_visible(False)
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())
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)
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)
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.")
261
self.indicator.set_from_icon_name("kazam-recording")
262
logger.debug("Recording resumed.")
263
KazamSuperIndicator.on_menuitem_pause_activate(self, menuitem)
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
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
283
self.indicator.set_from_icon_name("kazam-countdown")
284
self.blink_icon = BLINK_READY_ICON
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)
291
def start_recording(self):
292
logger.debug("Recording started.")
293
self.indicator.set_from_icon_name("kazam-recording")