appmenu_select.py 3.2 KB

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