appmenu_select.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. import PyQt5.QtWidgets # pylint: disable=import-error
  23. # TODO description in tooltip
  24. # TODO icon
  25. # pylint: disable=too-few-public-methods
  26. class AppListWidgetItem(PyQt5.QtWidgets.QListWidgetItem):
  27. def __init__(self, name, ident, parent=None):
  28. super(AppListWidgetItem, self).__init__(name, parent)
  29. # self.setToolTip(command)
  30. self.ident = ident
  31. @classmethod
  32. def from_line(cls, line):
  33. ident, _icon_name, name = line.strip().split(maxsplit=2)
  34. return cls(name=name, ident=ident)
  35. class AppmenuSelectManager:
  36. def __init__(self, vm, apps_multiselect):
  37. self.vm = vm
  38. self.app_list = apps_multiselect # this is a multiselect wiget
  39. self.whitelisted = None
  40. self.fill_apps_list()
  41. def fill_apps_list(self):
  42. self.whitelisted = [line for line in subprocess.check_output(
  43. ['qvm-appmenus', '--get-whitelist', self.vm.name]
  44. ).decode().strip().split('\n') if line]
  45. # Check if appmenu entry is really installed
  46. # whitelisted = [a for a in whitelisted
  47. # if os.path.exists('%s/apps/%s-%s' %
  48. # (self.vm.dir_path, self.vm.name, a))]
  49. self.app_list.clear()
  50. available_appmenus = [AppListWidgetItem.from_line(line)
  51. for line in subprocess.check_output(
  52. ['qvm-appmenus',
  53. '--get-available', '--i-understand-format-is-unstable',
  54. self.vm.name]).decode().splitlines()]
  55. for app in available_appmenus:
  56. if app.ident in self.whitelisted:
  57. self.app_list.selected_list.addItem(app)
  58. else:
  59. self.app_list.available_list.addItem(app)
  60. self.app_list.available_list.sortItems()
  61. self.app_list.selected_list.sortItems()
  62. def save_appmenu_select_changes(self):
  63. new_whitelisted = [self.app_list.selected_list.item(i).ident
  64. for i in range(self.app_list.selected_list.count())]
  65. if set(new_whitelisted) == set(self.whitelisted):
  66. return False
  67. p = subprocess.Popen([
  68. 'qvm-appmenus', '--set-whitelist', '-', '--update', self.vm.name],
  69. stdin=subprocess.PIPE)
  70. p.communicate('\n'.join(new_whitelisted).encode())
  71. if p.returncode != 0:
  72. raise RuntimeError('qvm-appmenus --set-whitelist failed')
  73. return True