appmenu_select.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. from qubesadmin import exc
  24. # TODO description in tooltip
  25. # TODO icon
  26. # pylint: disable=too-few-public-methods
  27. class AppListWidgetItem(QtWidgets.QListWidgetItem):
  28. def __init__(self, name, ident, tooltip=None, parent=None):
  29. super(AppListWidgetItem, self).__init__(name, parent)
  30. additional_description = ".desktop filename: " + str(ident)
  31. if not tooltip:
  32. tooltip = additional_description
  33. else:
  34. tooltip += "\n" + additional_description
  35. self.setToolTip(tooltip)
  36. self.ident = ident
  37. @classmethod
  38. def from_line(cls, line):
  39. ident, name, comment = line.split('|', maxsplit=3)
  40. return cls(name=name, ident=ident, tooltip=comment)
  41. @classmethod
  42. def from_ident(cls, ident):
  43. name = 'Application missing in template! ({})'.format(ident)
  44. comment = 'The listed application was available at some point to ' \
  45. 'this qube, but not any more. The most likely cause is ' \
  46. 'template change. Install the application in the template ' \
  47. 'if you want to restore it.'
  48. return cls(name=name, ident=ident, tooltip=comment)
  49. class AppmenuSelectManager:
  50. def __init__(self, vm, apps_multiselect):
  51. self.vm = vm
  52. self.app_list = apps_multiselect # this is a multiselect wiget
  53. self.whitelisted = None
  54. self.has_missing = False
  55. self.fill_apps_list(template=None)
  56. def fill_apps_list(self, template=None):
  57. try:
  58. self.whitelisted = [line for line in subprocess.check_output(
  59. ['qvm-appmenus', '--get-whitelist', self.vm.name]
  60. ).decode().strip().split('\n') if line]
  61. except exc.QubesException:
  62. self.whitelisted = []
  63. currently_selected = [
  64. self.app_list.selected_list.item(i).ident
  65. for i in range(self.app_list.selected_list.count())]
  66. whitelist = set(self.whitelisted + currently_selected)
  67. # Check if appmenu entry is really installed
  68. # whitelisted = [a for a in whitelisted
  69. # if os.path.exists('%s/apps/%s-%s' %
  70. # (self.vm.dir_path, self.vm.name, a))]
  71. self.app_list.clear()
  72. command = ['qvm-appmenus', '--get-available',
  73. '--i-understand-format-is-unstable', '--file-field',
  74. 'Comment']
  75. if template:
  76. command.extend(['--template', template.name])
  77. command.append(self.vm.name)
  78. try:
  79. available_appmenus = [
  80. AppListWidgetItem.from_line(line)
  81. for line in subprocess.check_output(
  82. command).decode().splitlines()]
  83. except exc.QubesException:
  84. available_appmenus = []
  85. for app in available_appmenus:
  86. if app.ident in whitelist:
  87. self.app_list.selected_list.addItem(app)
  88. whitelist.remove(app.ident)
  89. else:
  90. self.app_list.available_list.addItem(app)
  91. self.has_missing = bool(whitelist)
  92. for app in whitelist:
  93. item = AppListWidgetItem.from_ident(app)
  94. self.app_list.selected_list.addItem(item)
  95. self.app_list.available_list.sortItems()
  96. self.app_list.selected_list.sortItems()
  97. def save_appmenu_select_changes(self):
  98. new_whitelisted = [self.app_list.selected_list.item(i).ident
  99. for i in range(self.app_list.selected_list.count())]
  100. if set(new_whitelisted) == set(self.whitelisted):
  101. return False
  102. p = subprocess.Popen([
  103. 'qvm-appmenus', '--set-whitelist', '-', '--update', self.vm.name],
  104. stdin=subprocess.PIPE)
  105. p.communicate('\n'.join(new_whitelisted).encode())
  106. if p.returncode != 0:
  107. exception_text = QtCore.QCoreApplication.translate(
  108. "Command {command} failed", "exception").format(
  109. command='qvm-appmenus --set-whitelist')
  110. raise RuntimeError(exception_text)
  111. return True