appmenu_select.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/python2
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2011 Marek Marczykowski <marmarek@mimuw.edu.pl>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public License along
  18. # with this program; if not, see <http://www.gnu.org/licenses/>.
  19. #
  20. #
  21. import subprocess
  22. from PyQt5 import QtWidgets, QtCore # pylint: disable=import-error
  23. # TODO description in tooltip
  24. # TODO icon
  25. # pylint: disable=too-few-public-methods
  26. class AppListWidgetItem(QtWidgets.QListWidgetItem):
  27. def __init__(self, name, ident, tooltip=None, parent=None):
  28. super(AppListWidgetItem, self).__init__(name, parent)
  29. if tooltip:
  30. self.setToolTip(tooltip)
  31. self.ident = ident
  32. @classmethod
  33. def from_line(cls, line):
  34. ident, name, comment = line.split('|', maxsplit=3)
  35. return cls(name=name, ident=ident, tooltip=comment)
  36. @classmethod
  37. def from_ident(cls, ident):
  38. name = 'Application missing in template! ({})'.format(ident)
  39. comment = 'The listed application was available at some point to ' \
  40. 'this qube, but not any more. The most likely cause is ' \
  41. 'template change. Install the application in the template ' \
  42. 'if you want to restore it.'
  43. return cls(name=name, ident=ident, tooltip=comment)
  44. class AppmenuSelectManager:
  45. def __init__(self, vm, apps_multiselect):
  46. self.vm = vm
  47. self.app_list = apps_multiselect # this is a multiselect wiget
  48. self.whitelisted = None
  49. self.has_missing = False
  50. self.fill_apps_list(template=None)
  51. def fill_apps_list(self, template=None):
  52. self.whitelisted = [line for line in subprocess.check_output(
  53. ['qvm-appmenus', '--get-whitelist', self.vm.name]
  54. ).decode().strip().split('\n') if line]
  55. currently_selected = [
  56. self.app_list.selected_list.item(i).ident
  57. for i in range(self.app_list.selected_list.count())]
  58. whitelist = set(self.whitelisted + currently_selected)
  59. # Check if appmenu entry is really installed
  60. # whitelisted = [a for a in whitelisted
  61. # if os.path.exists('%s/apps/%s-%s' %
  62. # (self.vm.dir_path, self.vm.name, a))]
  63. self.app_list.clear()
  64. command = ['qvm-appmenus', '--get-available',
  65. '--i-understand-format-is-unstable', '--file-field',
  66. 'Comment']
  67. if template:
  68. command.extend(['--template', template.name])
  69. command.append(self.vm.name)
  70. available_appmenus = [
  71. AppListWidgetItem.from_line(line)
  72. for line in subprocess.check_output(command).decode().splitlines()]
  73. for app in available_appmenus:
  74. if app.ident in whitelist:
  75. self.app_list.selected_list.addItem(app)
  76. whitelist.remove(app.ident)
  77. else:
  78. self.app_list.available_list.addItem(app)
  79. self.has_missing = bool(whitelist)
  80. for app in whitelist:
  81. item = AppListWidgetItem.from_ident(app)
  82. self.app_list.selected_list.addItem(item)
  83. self.app_list.available_list.sortItems()
  84. self.app_list.selected_list.sortItems()
  85. def save_appmenu_select_changes(self):
  86. new_whitelisted = [self.app_list.selected_list.item(i).ident
  87. for i in range(self.app_list.selected_list.count())]
  88. if set(new_whitelisted) == set(self.whitelisted):
  89. return False
  90. p = subprocess.Popen([
  91. 'qvm-appmenus', '--set-whitelist', '-', '--update', self.vm.name],
  92. stdin=subprocess.PIPE)
  93. p.communicate('\n'.join(new_whitelisted).encode())
  94. if p.returncode != 0:
  95. exception_text = QtCore.QCoreApplication.translate(
  96. "Command {command} failed", "exception").format(
  97. command='qvm-appmenus --set-whitelist')
  98. raise RuntimeError(exception_text)
  99. return True