appmenu_select.py 3.3 KB

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