appmenu_select.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. if tooltip:
  30. self.setToolTip(tooltip)
  31. self.ident = ident
  32. @classmethod
  33. def from_line(cls, line):
  34. ident, name, comment = line.split('|', maxsplit=3)
  35. return cls(name=name, ident=ident, tooltip=comment)
  36. class AppmenuSelectManager:
  37. def __init__(self, vm, apps_multiselect):
  38. self.vm = vm
  39. self.app_list = apps_multiselect # this is a multiselect wiget
  40. self.whitelisted = None
  41. self.fill_apps_list()
  42. def fill_apps_list(self):
  43. self.whitelisted = [line for line in subprocess.check_output(
  44. ['qvm-appmenus', '--get-whitelist', self.vm.name]
  45. ).decode().strip().split('\n') if line]
  46. # Check if appmenu entry is really installed
  47. # whitelisted = [a for a in whitelisted
  48. # if os.path.exists('%s/apps/%s-%s' %
  49. # (self.vm.dir_path, self.vm.name, a))]
  50. self.app_list.clear()
  51. available_appmenus = [AppListWidgetItem.from_line(line)
  52. for line in subprocess.check_output(
  53. ['qvm-appmenus', '--get-available',
  54. '--i-understand-format-is-unstable', '--file-field',
  55. 'Comment', self.vm.name]).decode().splitlines()]
  56. for app in available_appmenus:
  57. if app.ident in self.whitelisted:
  58. self.app_list.selected_list.addItem(app)
  59. else:
  60. self.app_list.available_list.addItem(app)
  61. self.app_list.available_list.sortItems()
  62. self.app_list.selected_list.sortItems()
  63. def save_appmenu_select_changes(self):
  64. new_whitelisted = [self.app_list.selected_list.item(i).ident
  65. for i in range(self.app_list.selected_list.count())]
  66. if set(new_whitelisted) == set(self.whitelisted):
  67. return False
  68. p = subprocess.Popen([
  69. 'qvm-appmenus', '--set-whitelist', '-', '--update', self.vm.name],
  70. stdin=subprocess.PIPE)
  71. p.communicate('\n'.join(new_whitelisted).encode())
  72. if p.returncode != 0:
  73. exception_text = QtCore.QCoreApplication.translate(
  74. "Command {command} failed", "exception").format(
  75. command='qvm-appmenus --set-whitelist')
  76. raise RuntimeError(exception_text)
  77. return True