2014-11-13 14:38:41 +01:00
|
|
|
#!/usr/bin/python2 -O
|
2015-01-19 18:03:23 +01:00
|
|
|
# vim: fileencoding=utf-8
|
|
|
|
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# 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 2 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.
|
|
|
|
#
|
|
|
|
|
|
|
|
'''Qubes extensions.
|
2014-11-13 18:10:27 +01:00
|
|
|
|
|
|
|
Extensions provide additional features (like application menus) found only on
|
|
|
|
some systems. They may be OS- or architecture-dependent or custom-developed for
|
|
|
|
particular customer.
|
|
|
|
'''
|
|
|
|
|
2014-11-13 14:38:41 +01:00
|
|
|
import inspect
|
|
|
|
|
|
|
|
import qubes.events
|
|
|
|
import qubes.plugins
|
|
|
|
|
|
|
|
class ExtensionPlugin(qubes.plugins.Plugin):
|
2014-11-13 18:10:27 +01:00
|
|
|
'''Metaclass for :py:class:`Extension`'''
|
2014-11-13 14:38:41 +01:00
|
|
|
def __init__(cls, name, bases, dict_):
|
|
|
|
super(ExtensionPlugin, cls).__init__(name, bases, dict_)
|
|
|
|
cls._instance = None
|
|
|
|
|
|
|
|
def __call__(cls, *args, **kwargs):
|
|
|
|
if cls._instance is None:
|
2015-01-19 17:06:30 +01:00
|
|
|
cls._instance = super(ExtensionPlugin, cls).__call__(
|
|
|
|
*args, **kwargs)
|
2014-11-13 14:38:41 +01:00
|
|
|
return cls._instance
|
|
|
|
|
|
|
|
class Extension(object):
|
2014-12-09 14:14:24 +01:00
|
|
|
'''Base class for all extensions
|
|
|
|
|
|
|
|
:param qubes.Qubes app: application object
|
2015-01-19 19:02:28 +01:00
|
|
|
''' # pylint: disable=too-few-public-methods
|
2014-12-09 14:14:24 +01:00
|
|
|
|
2014-11-13 14:38:41 +01:00
|
|
|
__metaclass__ = ExtensionPlugin
|
2014-12-09 14:14:24 +01:00
|
|
|
|
|
|
|
def __init__(self, app):
|
|
|
|
self.app = app
|
|
|
|
|
2014-11-13 14:38:41 +01:00
|
|
|
for name in dir(self):
|
|
|
|
attr = getattr(self, name)
|
2014-12-09 14:14:24 +01:00
|
|
|
if not qubes.events.ishandler(attr):
|
2014-11-13 14:38:41 +01:00
|
|
|
continue
|
|
|
|
|
2014-12-09 14:14:24 +01:00
|
|
|
if attr.ha_vm is not None:
|
2015-10-01 21:55:47 +02:00
|
|
|
for event in attr.ha_events:
|
|
|
|
attr.ha_vm.add_handler(event, attr)
|
2014-11-13 14:38:41 +01:00
|
|
|
else:
|
|
|
|
# global hook
|
2015-10-01 21:55:47 +02:00
|
|
|
for event in attr.ha_events:
|
|
|
|
self.app.add_handler(event, attr)
|
2014-12-09 14:14:24 +01:00
|
|
|
|
|
|
|
|
2015-01-12 14:57:24 +01:00
|
|
|
def handler(*events, **kwargs):
|
2014-12-09 14:14:24 +01:00
|
|
|
'''Event handler decorator factory.
|
|
|
|
|
|
|
|
To hook an event, decorate a method in your plugin class with this
|
|
|
|
decorator. You may hook both per-vm-class and global events.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
This decorator is intended only for extensions! For regular use in the
|
2015-01-19 18:03:23 +01:00
|
|
|
core, see :py:func:`qubes.events.handler`.
|
2014-12-09 14:14:24 +01:00
|
|
|
|
|
|
|
:param str event: event type
|
|
|
|
:param type vm: VM to hook (leave as None to hook all VMs)
|
2015-01-19 17:06:30 +01:00
|
|
|
:param bool system: when :py:obj:`True`, hook is system-wide (not attached \
|
|
|
|
to any VM)
|
2014-12-09 14:14:24 +01:00
|
|
|
'''
|
|
|
|
|
2015-01-20 14:41:19 +01:00
|
|
|
def decorator(func):
|
|
|
|
func.ha_events = events
|
2014-12-09 14:14:24 +01:00
|
|
|
|
2015-01-12 14:57:24 +01:00
|
|
|
if kwargs.get('system', False):
|
2015-01-20 14:41:19 +01:00
|
|
|
func.ha_vm = None
|
2015-01-12 14:57:24 +01:00
|
|
|
elif 'vm' in kwargs:
|
2015-01-20 14:41:19 +01:00
|
|
|
func.ha_vm = kwargs['vm']
|
2014-12-09 14:14:24 +01:00
|
|
|
else:
|
2015-01-20 14:41:19 +01:00
|
|
|
func.ha_vm = qubes.vm.BaseVM
|
2014-12-09 14:14:24 +01:00
|
|
|
|
2015-01-20 14:41:19 +01:00
|
|
|
return func
|
2014-12-09 14:14:24 +01:00
|
|
|
|
|
|
|
return decorator
|
2014-11-13 14:38:41 +01:00
|
|
|
|
|
|
|
|
2015-01-19 18:03:23 +01:00
|
|
|
__all__ = ['Extension', 'ExtensionPlugin', 'handler'] \
|
|
|
|
+ qubes.plugins.load(__file__)
|