appmenus fix

This commit is contained in:
Wojtek Porczyk 2017-07-12 14:09:07 +02:00
parent 6462ae88b7
commit eefa6c31c2
2 changed files with 41 additions and 79 deletions

View File

@ -20,113 +20,74 @@
# #
# #
import sys
import os import os
from PyQt4.QtCore import * import subprocess
from PyQt4.QtGui import * import sys
import qubesmanager.resources_rc
from pyinotify import WatchManager, Notifier, ThreadedNotifier, EventsCodes, ProcessEvent
import time import time
from operator import itemgetter from operator import itemgetter
from .thread_monitor import * from PyQt4.QtCore import *
from .multiselectwidget import * from PyQt4.QtGui import *
from pyinotify import WatchManager, Notifier, ThreadedNotifier, EventsCodes, ProcessEvent
whitelisted_filename = 'whitelisted-appmenus.list' import qubesmanager.resources_rc
# TODO description in tooltip
# TODO icon
class AppListWidgetItem(QListWidgetItem): class AppListWidgetItem(QListWidgetItem):
def __init__(self, name, filename, command, parent = None): def __init__(self, name, ident, parent=None):
super(AppListWidgetItem, self).__init__(name, parent) super(AppListWidgetItem, self).__init__(name, parent)
self.setToolTip(command) # self.setToolTip(command)
self.filename = filename self.ident = ident
@classmethod
def from_line(cls, line):
ident, name = line.strip().split(maxsplit=2)
return cls(name=name, ident=ident)
class AppmenuSelectManager: class AppmenuSelectManager:
def __init__(self, vm, apps_multiselect, parent=None): def __init__(self, vm, apps_multiselect, parent=None):
self.app_list = apps_multiselect # this is a multiselect wiget
self.vm = vm self.vm = vm
if self.vm.template: self.app_list = apps_multiselect # this is a multiselect wiget
self.source_vm = self.vm.template
else:
self.source_vm = self.vm
self.fill_apps_list() self.fill_apps_list()
def fill_apps_list(self): def fill_apps_list(self):
whitelisted = [line for line in subprocess.check_output(
template_dir = self.source_vm.appmenus_templates_dir ['qvm-appmenus', '--get-whitelist', self.vm.name]
).decode().strip().split('\n') if line]
template_file_list = os.listdir(template_dir)
whitelisted = []
if os.path.exists(self.vm.dir_path + '/' + whitelisted_filename):
f = open(self.vm.dir_path + '/' + whitelisted_filename, 'r')
whitelisted = [item.strip() for item in f]
f.close()
# Check if appmenu entry is really installed # Check if appmenu entry is really installed
whitelisted = [a for a in whitelisted if os.path.exists('%s/apps/%s-%s' % (self.vm.dir_path, self.vm.name, a))] # whitelisted = [a for a in whitelisted if os.path.exists('%s/apps/%s-%s' % (self.vm.dir_path, self.vm.name, a))]
self.app_list.clear() self.app_list.clear()
available_appmenus = [] available_appmenus = [AppListWidgetItem.from_line(line)
for template_file in template_file_list: for line in subprocess.check_output(['qvm-appmenus',
desktop_template = open(template_dir + '/' + template_file, 'r') '--get-available', '--i-understand-format-is-unstable',
desktop_name = None self.vm.name]).decode().split()]
desktop_command = None
for line in desktop_template:
if line.startswith("Name=%VMNAME%: "):
desktop_name = line.partition('Name=%VMNAME%: ')[2].strip()
if line.startswith("Exec=qvm-run"):
desktop_command = line[line.find("'"):].strip("'\n")
if not desktop_command:
desktop_command = ""
if desktop_name:
available_appmenus.append( (template_file, desktop_name, desktop_command) )
desktop_template.close()
self.whitelisted_appmenus = [a for a in available_appmenus if a[0] in whitelisted]
available_appmenus = [a for a in available_appmenus if a[0] not in whitelisted]
for a in available_appmenus: for a in available_appmenus:
self.app_list.available_list.addItem( AppListWidgetItem(a[1], a[0], a[2])) if a.ident in whitelisted:
self.app_list.selected_list.addItem(a)
for a in self.whitelisted_appmenus: else:
self.app_list.selected_list.addItem( AppListWidgetItem(a[1], a[0], a[2])) self.app_list.available_list.addItem(a)
self.app_list.available_list.sortItems() self.app_list.available_list.sortItems()
self.app_list.selected_list.sortItems() self.app_list.selected_list.sortItems()
def save_list_of_selected(self): def save_list_of_selected(self):
sth_changed = False
added = [] added = []
for i in range(self.app_list.selected_list.count()): new_whitelisted = [self.app_list.selected_list.item(i).ident
item = self.app_list.selected_list.item(i) for i in range(self.app_list.selected_list.count())]
if item.filename not in [ w[0] for w in self.whitelisted_appmenus]:
added.append(item)
if self.app_list.selected_list.count() - len(added) < len(self.whitelisted_appmenus): #sth removed subprocess.check_call(
sth_changed = True 'qvm-appmenus', '--set-whitelist', '-', self.vm.name,
elif len(added) > 0: input='\n'.join(new_whitelisted))
sth_changed = True
if sth_changed == True:
whitelisted = open(self.vm.dir_path + '/' + whitelisted_filename, 'w')
for i in range(self.app_list.selected_list.count()):
item = self.app_list.selected_list.item(i)
whitelisted.write(item.filename + '\n')
whitelisted.close()
return True return True
else:
return False
def save_appmenu_select_changes(self): def save_appmenu_select_changes(self):

View File

@ -33,10 +33,11 @@ import qubesadmin
import qubesadmin.tools import qubesadmin.tools
from . import utils from . import utils
from . import multiselectwidget
from .ui_settingsdlg import * from .ui_settingsdlg import *
from .appmenu_select import *
from .firewall import * from .firewall import *
from .appmenu_select import AppmenuSelectManager
from .backup_utils import get_path_for_vm from .backup_utils import get_path_for_vm
class VMSettingsWindow(Ui_SettingsDialog, QDialog): class VMSettingsWindow(Ui_SettingsDialog, QDialog):
@ -107,7 +108,7 @@ class VMSettingsWindow(Ui_SettingsDialog, QDialog):
####### apps tab ####### apps tab
if self.tabWidget.isTabEnabled(self.tabs_indices["applications"]): if self.tabWidget.isTabEnabled(self.tabs_indices["applications"]):
self.app_list = MultiSelectWidget(self) self.app_list = multiselectwidget.MultiSelectWidget(self)
self.apps_layout.addWidget(self.app_list) self.apps_layout.addWidget(self.app_list)
self.AppListManager = AppmenuSelectManager(self.vm, self.app_list) self.AppListManager = AppmenuSelectManager(self.vm, self.app_list)