appmenu_select.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 PyQt4.QtGui # pylint: disable=import-error
  23. # TODO description in tooltip
  24. # TODO icon
  25. # pylint: disable=too-few-public-methods
  26. class AppListWidgetItem(PyQt4.QtGui.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(['qvm-appmenus',
  52. '--get-available', '--i-understand-format-is-unstable',
  53. self.vm.name]).decode().splitlines()]
  54. for app in available_appmenus:
  55. if app.ident in self.whitelisted:
  56. self.app_list.selected_list.addItem(app)
  57. else:
  58. self.app_list.available_list.addItem(app)
  59. self.app_list.available_list.sortItems()
  60. self.app_list.selected_list.sortItems()
  61. def save_appmenu_select_changes(self):
  62. new_whitelisted = [self.app_list.selected_list.item(i).ident
  63. for i in range(self.app_list.selected_list.count())]
  64. if set(new_whitelisted) == set(self.whitelisted):
  65. return False
  66. p = subprocess.Popen([
  67. 'qvm-appmenus', '--set-whitelist', '-', '--update', self.vm.name],
  68. stdin=subprocess.PIPE)
  69. p.communicate('\n'.join(new_whitelisted).encode())
  70. if p.returncode != 0:
  71. raise RuntimeError('qvm-appmenus --set-whitelist failed')
  72. return True