appmenu_select.py 4.6 KB

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