appmenu_select.py 3.0 KB

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