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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
# -*- coding: utf-8 -*-
# Copyright © 2010 Chris Jones <tortoise@tortuga>
# Copyright © 2010 Francesco Fumanti <francesco.fumanti@gmx.net>
# Copyright © 2011-2017 marmuta <marmvta@gmail.com>
#
# This file is part of Onboard.
#
# Onboard 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.
#
# Onboard 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, see <http://www.gnu.org/licenses/>.
from __future__ import division, print_function, unicode_literals
import subprocess
try:
import dbus
except ImportError:
pass
from Onboard.Version import require_gi_versions
require_gi_versions()
from gi.repository import GObject, Gtk
from Onboard.definitions import StatusIconProviderEnum
from Onboard.utils import unicode_str, run_script
import logging
_logger = logging.getLogger("Indicator")
from Onboard.Config import Config
config = Config()
class ContextMenu(GObject.GObject):
__gsignals__ = {
str('quit-onboard') : (GObject.SignalFlags.RUN_LAST,
GObject.TYPE_NONE, ())
}
def __init__(self, keyboard=None):
GObject.GObject.__init__(self)
self._keyboard = keyboard
self._show_label = _("_Show Onboard")
self._hide_label = _("_Hide Onboard")
self._menu = self.create_menu()
def set_keyboard(self, keyboard):
self._keyboard = keyboard
def get_gtk_menu(self):
return self._menu
def create_menu(self):
menu = Gtk.Menu()
# This updates the menu in gnome-shell and gnome-classic,
# but not in unity or unity2D.
menu.connect_object("show", ContextMenu.update_items, self)
show_item = Gtk.MenuItem.new_with_label(self._show_label)
show_item.set_use_underline(True)
show_item.connect_object("activate",
ContextMenu.on_show_keyboard_toggle, self)
menu.append(show_item)
if not config.lockdown.disable_preferences:
# Translators: label of a menu item. It used to be stock item
# STOCK_PREFERENCES until Gtk 3.10 deprecated those.
settings_item = Gtk.MenuItem.new_with_label(_("_Preferences"))
settings_item.set_use_underline(True)
settings_item.connect("activate", self._on_settings_clicked)
menu.append(settings_item)
item = Gtk.SeparatorMenuItem.new()
menu.append(item)
help_item = Gtk.MenuItem.new_with_label(_("_Help"))
help_item.set_use_underline(True)
help_item.connect("activate", self._on_help)
menu.append(help_item)
if not config.lockdown.disable_quit:
item = Gtk.SeparatorMenuItem.new()
menu.append(item)
# Translators: label of a menu item. It used to be stock item
# STOCK_QUIT until Gtk 3.10 deprecated those.
quit_item = Gtk.MenuItem.new_with_label(_("_Quit"))
quit_item.set_use_underline(True)
quit_item.connect("activate", self._on_quit)
menu.append(quit_item)
menu.show_all()
return menu
def popup(self, button, activate_time,
data=None, menu_position_func=None):
"""
Callback called when status icon right clicked. Produces menu.
"""
self._menu.popup(None, None,
menu_position_func, data,
button, activate_time)
def update_items(self):
if self._keyboard:
if self._keyboard.is_visible():
self._menu.get_children()[0].set_label(self._hide_label)
else:
self._menu.get_children()[0].set_label(self._show_label)
def _on_settings_clicked(self, widget):
run_script("sokSettings")
def on_show_keyboard_toggle(self):
# The menu item might have been activated by keyboard hotkeys. Delay
# request until keys have been released.
self._keyboard.request_visibility_toggle()
def _on_help(self, data=None):
subprocess.Popen(["/usr/bin/yelp", "help:onboard"])
def _on_quit(self, data=None):
_logger.debug("Entered _on_quit")
self.emit("quit-onboard")
class Indicator():
"Keyboard window managed by this indicator"
_keyboard = None
"Menu attached to backend"
_menu = None
def __new__(cls, *args, **kwargs):
"""
Singleton magic.
"""
if not hasattr(cls, "self"):
cls.self = object.__new__(cls, *args, **kwargs)
cls.self.init()
return cls.self
def __init__(self):
"""
This constructor is still called multiple times.
Do nothing here and use the singleton constructor "init()" instead.
"""
pass
def init(self):
self._menu = ContextMenu()
sip = config.status_icon_provider
if sip == StatusIconProviderEnum.auto:
# auto-detection
sip = config.get_preferred_statusicon_provider()
if sip == StatusIconProviderEnum.GtkStatusIcon:
backends = [BackendGtkStatusIcon]
elif sip == StatusIconProviderEnum.AppIndicator:
backends = [BackendAppIndicator, BackendGtkStatusIcon]
elif sip is None:
backends = []
else: # sip == StatusIconProviderEnum.auto
backends = [BackendAppIndicator,
BackendGtkStatusIcon]
self._backend = None
for backend in backends:
try:
self._backend = backend(self._menu)
break
except RuntimeError as ex:
_logger.info("Status icon provider: '{}' unavailable: {}"
.format(backend.__name__, unicode_str(ex)))
_logger.info("Status icon provider: '{}' selected"
.format(self._backend and
type(self._backend).__name__))
if self._backend is not None:
self._backend.set_visible(False)
def cleanup(self):
if self._backend:
self._backend.cleanup()
self.set_keyboard(None)
def set_keyboard(self, keyboard):
self._keyboard = keyboard
self._menu.set_keyboard(keyboard)
def get_menu(self):
return self._menu
def update_menu_items(self):
self._menu.update_items()
def set_visible(self, visible):
if self._backend is not None:
self._backend.set_visible(visible)
class BackendBase():
_menu = None
category = "ApplicationStatus"
icon_desc = _("Onboard on-screen keyboard")
icon_name = "onboard"
id = "Onboard"
title = _("Onboard on-screen keyboard")
def __init__(self, menu):
self._menu = menu
def cleanup(self):
pass
def get_menu(self):
return self._menu
class BackendGtkStatusIcon(BackendBase):
_status_icon = None
def __init__(self, menu):
BackendBase.__init__(self, menu)
self._status_icon = Gtk.StatusIcon(icon_name=self.icon_name)
self._status_icon.connect("activate",
lambda x:
self._menu.on_show_keyboard_toggle())
self._status_icon.connect("popup-menu",
self._on_status_icon_popup_menu)
def set_visible(self, visible):
self._status_icon.set_visible(visible)
def _on_status_icon_popup_menu(self, status_icon, button, activate_time):
"""
Callback called when status icon right clicked. Produces menu.
"""
self._menu.popup(button, activate_time,
status_icon, self._menu_position_func)
def _menu_position_func(self, menu, *args):
gtk_menu = self._menu.get_gtk_menu()
# Work around gi annotation bug in gtk-3.0:
# gtk_status_icon_position_menu() doesn't mark 'push_in' as inout
# which is required for any (*GtkMenuPositionFunc)
# Precise: args = (status_icon,)
if len(args) == 1: # in Precise
status_icon, = args
return Gtk.StatusIcon.position_menu(gtk_menu, status_icon)
elif len(args) == 2: # in <=Oneiric?
push_in, status_icon = args
return Gtk.StatusIcon.position_menu(gtk_menu, status_icon)
elif len(args) == 3: # in <=Xenial?
x, y, status_icon = args
return Gtk.StatusIcon.position_menu(gtk_menu, x, y, status_icon)
class BackendAppIndicator(BackendBase):
_indicator = None
STATUSNOTIFIER_OBJECT = "/org/ayatana/NotificationItem/Onboard"
STATUSNOTIFIER_IFACE = "org.kde.StatusNotifierItem"
ACTIVATE_METHOD = "Activate"
def __init__(self, menu):
BackendBase.__init__(self, menu)
try:
from gi.repository import AyatanaAppIndicator3 as AppIndicator
except ImportError as ex:
raise RuntimeError(ex)
self._indicator = AppIndicator.Indicator.new(
self.id,
self.icon_name,
AppIndicator.IndicatorCategory.APPLICATION_STATUS)
self._indicator.set_icon_full(self.icon_name,
self.icon_desc)
self._indicator.set_menu(menu._menu)
self._indicator.set_secondary_activate_target(
menu._menu.get_children()[0])
if "dbus" in globals():
# Watch left-click Activate() calls on desktops that send them
# (KDE Plasma). There is still "No such method 'Activate'" in
# AppIndicator.
try:
self._bus = dbus.SessionBus()
except dbus.exceptions.DBusException as ex:
_logger.warning("D-Bus session bus unavailable, "
"no left-click Activate() for AppIndicator: " +
unicode_str(ex))
else:
try:
self._bus.add_match_string(
"type='method_call',"
"eavesdrop=true,"
"path='{}',"
"interface='{}',"
"member='{}'"
.format(self.STATUSNOTIFIER_OBJECT,
self.STATUSNOTIFIER_IFACE,
self.ACTIVATE_METHOD))
self._bus.add_message_filter(self._on_activate_method)
except dbus.exceptions.DBusException as ex:
_logger.warning("Failed to setup D-Bus match rule, "
"no left-click Activate() for AppIndicator: " +
unicode_str(ex))
def cleanup(self):
pass
def _on_activate_method(self, bus, message):
if message.get_path() == self.STATUSNOTIFIER_OBJECT and \
message.get_member() == self.ACTIVATE_METHOD:
self._menu.on_show_keyboard_toggle()
return dbus.connection.HANDLER_RESULT_NOT_YET_HANDLED
def set_visible(self, visible):
self._set_indicator_active(visible)
def _set_indicator_active(self, active):
try:
from gi.repository import AyatanaAppIndicator3 as AppIndicator
except ImportError:
pass
else:
if active:
self._indicator.set_status(
AppIndicator.IndicatorStatus.ACTIVE)
else:
self._indicator.set_status(
AppIndicator.IndicatorStatus.PASSIVE)
|