2013-01-23 15:55:14 +01:00
|
|
|
#!/usr/bin/python2
|
2011-05-25 02:26:41 +02:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2011 Marek Marczykowski <marmarek@mimuw.edu.pl>
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2017-11-06 21:06:30 +01:00
|
|
|
# You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
# with this program; if not, see <http://www.gnu.org/licenses/>.
|
2011-05-25 02:26:41 +02:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2017-07-12 14:09:07 +02:00
|
|
|
import subprocess
|
2019-10-23 18:18:39 +02:00
|
|
|
from PyQt5 import QtWidgets, QtCore # pylint: disable=import-error
|
2011-05-25 02:26:41 +02:00
|
|
|
|
2017-07-12 14:09:07 +02:00
|
|
|
# TODO description in tooltip
|
|
|
|
# TODO icon
|
2017-11-09 17:23:23 +01:00
|
|
|
# pylint: disable=too-few-public-methods
|
2019-10-23 18:18:39 +02:00
|
|
|
class AppListWidgetItem(QtWidgets.QListWidgetItem):
|
2019-06-05 16:19:38 +02:00
|
|
|
def __init__(self, name, ident, tooltip=None, parent=None):
|
2012-01-24 15:04:28 +01:00
|
|
|
super(AppListWidgetItem, self).__init__(name, parent)
|
2019-06-05 16:19:38 +02:00
|
|
|
if tooltip:
|
|
|
|
self.setToolTip(tooltip)
|
2017-07-12 14:09:07 +02:00
|
|
|
self.ident = ident
|
2011-05-25 02:26:41 +02:00
|
|
|
|
2017-07-12 14:09:07 +02:00
|
|
|
@classmethod
|
|
|
|
def from_line(cls, line):
|
2019-06-05 16:19:38 +02:00
|
|
|
ident, name, comment = line.split('|', maxsplit=3)
|
|
|
|
return cls(name=name, ident=ident, tooltip=comment)
|
2011-05-25 02:26:41 +02:00
|
|
|
|
2020-05-11 17:22:41 +02:00
|
|
|
@classmethod
|
|
|
|
def from_ident(cls, ident):
|
|
|
|
name = 'Application missing in template! ({})'.format(ident)
|
|
|
|
comment = 'The listed application was available at some point to ' \
|
|
|
|
'this qube, but not any more. The most likely cause is ' \
|
|
|
|
'template change. Install the application in the template ' \
|
|
|
|
'if you want to restore it.'
|
|
|
|
return cls(name=name, ident=ident, tooltip=comment)
|
|
|
|
|
2011-05-25 02:26:41 +02:00
|
|
|
|
2012-02-09 19:47:21 +01:00
|
|
|
class AppmenuSelectManager:
|
2017-11-09 16:08:16 +01:00
|
|
|
def __init__(self, vm, apps_multiselect):
|
2011-05-25 02:26:41 +02:00
|
|
|
self.vm = vm
|
2017-07-12 14:09:07 +02:00
|
|
|
self.app_list = apps_multiselect # this is a multiselect wiget
|
2017-07-30 18:55:32 +02:00
|
|
|
self.whitelisted = None
|
2020-05-11 17:22:41 +02:00
|
|
|
self.has_missing = False
|
|
|
|
self.fill_apps_list(template=None)
|
2011-05-25 02:26:41 +02:00
|
|
|
|
2020-05-11 17:22:41 +02:00
|
|
|
def fill_apps_list(self, template=None):
|
2017-07-30 18:55:32 +02:00
|
|
|
self.whitelisted = [line for line in subprocess.check_output(
|
2017-07-12 14:09:07 +02:00
|
|
|
['qvm-appmenus', '--get-whitelist', self.vm.name]
|
|
|
|
).decode().strip().split('\n') if line]
|
2012-01-24 15:04:28 +01:00
|
|
|
|
2020-05-11 17:22:41 +02:00
|
|
|
currently_selected = [
|
|
|
|
self.app_list.selected_list.item(i).ident
|
|
|
|
for i in range(self.app_list.selected_list.count())]
|
|
|
|
|
|
|
|
whitelist = set(self.whitelisted + currently_selected)
|
|
|
|
|
2012-11-27 02:35:39 +01:00
|
|
|
# Check if appmenu entry is really installed
|
2017-11-06 23:18:18 +01:00
|
|
|
# whitelisted = [a for a in whitelisted
|
|
|
|
# if os.path.exists('%s/apps/%s-%s' %
|
|
|
|
# (self.vm.dir_path, self.vm.name, a))]
|
2011-05-25 02:26:41 +02:00
|
|
|
|
2012-11-27 02:35:39 +01:00
|
|
|
self.app_list.clear()
|
2012-01-24 15:04:28 +01:00
|
|
|
|
2020-05-11 17:22:41 +02:00
|
|
|
command = ['qvm-appmenus', '--get-available',
|
|
|
|
'--i-understand-format-is-unstable', '--file-field',
|
|
|
|
'Comment']
|
|
|
|
if template:
|
|
|
|
command.extend(['--template', template.name])
|
|
|
|
command.append(self.vm.name)
|
|
|
|
|
|
|
|
available_appmenus = [
|
|
|
|
AppListWidgetItem.from_line(line)
|
|
|
|
for line in subprocess.check_output(command).decode().splitlines()]
|
2017-07-12 14:09:07 +02:00
|
|
|
|
2017-11-08 16:53:49 +01:00
|
|
|
for app in available_appmenus:
|
2020-05-11 17:22:41 +02:00
|
|
|
if app.ident in whitelist:
|
2017-11-08 16:53:49 +01:00
|
|
|
self.app_list.selected_list.addItem(app)
|
2020-05-11 17:22:41 +02:00
|
|
|
whitelist.remove(app.ident)
|
2017-07-12 14:09:07 +02:00
|
|
|
else:
|
2017-11-08 16:53:49 +01:00
|
|
|
self.app_list.available_list.addItem(app)
|
2012-01-24 15:04:28 +01:00
|
|
|
|
2020-05-11 17:22:41 +02:00
|
|
|
self.has_missing = bool(whitelist)
|
|
|
|
|
|
|
|
for app in whitelist:
|
|
|
|
item = AppListWidgetItem.from_ident(app)
|
|
|
|
self.app_list.selected_list.addItem(item)
|
|
|
|
|
2012-01-24 15:04:28 +01:00
|
|
|
self.app_list.available_list.sortItems()
|
|
|
|
self.app_list.selected_list.sortItems()
|
2011-05-25 02:26:41 +02:00
|
|
|
|
2017-07-30 18:55:32 +02:00
|
|
|
def save_appmenu_select_changes(self):
|
2017-07-12 14:09:07 +02:00
|
|
|
new_whitelisted = [self.app_list.selected_list.item(i).ident
|
2019-05-22 23:10:09 +02:00
|
|
|
for i in range(self.app_list.selected_list.count())]
|
2017-07-12 14:09:07 +02:00
|
|
|
|
2017-07-30 18:55:32 +02:00
|
|
|
if set(new_whitelisted) == set(self.whitelisted):
|
|
|
|
return False
|
2017-07-12 14:09:07 +02:00
|
|
|
|
2017-07-30 18:55:32 +02:00
|
|
|
p = subprocess.Popen([
|
|
|
|
'qvm-appmenus', '--set-whitelist', '-', '--update', self.vm.name],
|
|
|
|
stdin=subprocess.PIPE)
|
|
|
|
p.communicate('\n'.join(new_whitelisted).encode())
|
|
|
|
if p.returncode != 0:
|
2019-10-23 18:18:39 +02:00
|
|
|
exception_text = QtCore.QCoreApplication.translate(
|
2019-11-08 23:35:26 +01:00
|
|
|
"Command {command} failed", "exception").format(
|
|
|
|
command='qvm-appmenus --set-whitelist')
|
2019-10-23 18:18:39 +02:00
|
|
|
raise RuntimeError(exception_text)
|
2017-07-12 14:09:07 +02:00
|
|
|
|
2017-07-30 18:55:32 +02:00
|
|
|
return True
|