appmenu_select.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/python2.6
  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 sys
  23. import os
  24. from PyQt4.QtCore import *
  25. from PyQt4.QtGui import *
  26. from qubes.qubes import QubesVmCollection
  27. from qubes.qubes import QubesException
  28. from qubes.qubes import qubes_appmenu_create_cmd
  29. from qubes.qubes import qubes_appmenu_remove_cmd
  30. from qubes.qubes import QubesDaemonPidfile
  31. from qubes.qubes import QubesHost
  32. from qubes.qubes import qrexec_client_path
  33. import qubesmanager.resources_rc
  34. from pyinotify import WatchManager, Notifier, ThreadedNotifier, EventsCodes, ProcessEvent
  35. import subprocess
  36. import time
  37. from operator import itemgetter
  38. from thread_monitor import *
  39. from multiselectwidget import *
  40. whitelisted_filename = 'whitelisted-appmenus.list'
  41. class AppListWidgetItem(QListWidgetItem):
  42. def __init__(self, name, filename, parent = None):
  43. super(AppListWidgetItem, self).__init__(name, parent)
  44. self.filename = filename
  45. class AppmenuSelectManager:
  46. def __init__(self, vm, apps_multiselect, parent=None):
  47. self.app_list = apps_multiselect # this is a multiselect wiget
  48. self.vm = vm
  49. if self.vm.template:
  50. self.source_vm = self.vm.template
  51. else:
  52. self.source_vm = self.vm
  53. self.fill_apps_list()
  54. def fill_apps_list(self):
  55. template_dir = self.source_vm.appmenus_templates_dir
  56. template_file_list = os.listdir(template_dir)
  57. whitelisted = []
  58. if os.path.exists(self.vm.dir_path + '/' + whitelisted_filename):
  59. f = open(self.vm.dir_path + '/' + whitelisted_filename, 'r')
  60. whitelisted = [item.strip() for item in f]
  61. f.close()
  62. self.app_list.clear()
  63. available_appmenus = []
  64. for template_file in template_file_list:
  65. desktop_template = open(template_dir + '/' + template_file, 'r')
  66. for line in desktop_template:
  67. if line.startswith("Name=%VMNAME%: "):
  68. desktop_name = line.partition('Name=%VMNAME%: ')[2].strip()
  69. available_appmenus.append( (template_file, desktop_name) )
  70. break
  71. desktop_template.close()
  72. self.whitelisted_appmenus = [a for a in available_appmenus if a[0] in whitelisted]
  73. available_appmenus = [a for a in available_appmenus if a[0] not in whitelisted]
  74. for a in available_appmenus:
  75. self.app_list.available_list.addItem( AppListWidgetItem(a[1], a[0]))
  76. for a in self.whitelisted_appmenus:
  77. self.app_list.selected_list.addItem( AppListWidgetItem(a[1], a[0]))
  78. self.app_list.available_list.sortItems()
  79. self.app_list.selected_list.sortItems()
  80. def save_list_of_selected(self):
  81. sth_changed = False
  82. added = []
  83. for i in range(self.app_list.selected_list.count()):
  84. item = self.app_list.selected_list.item(i)
  85. if item.filename not in [ w[0] for w in self.whitelisted_appmenus]:
  86. added.append(item)
  87. if self.app_list.selected_list.count() - len(added) < len(self.whitelisted_appmenus): #sth removed
  88. sth_changed = True;
  89. elif len(added) > 0:
  90. sth_changed = True;
  91. if sth_changed == True:
  92. whitelisted = open(self.vm.dir_path + '/' + whitelisted_filename, 'w')
  93. for i in range(self.app_list.selected_list.count()):
  94. item = self.app_list.selected_list.item(i)
  95. whitelisted.write(item.filename + '\n')
  96. whitelisted.close()
  97. return True
  98. else:
  99. return False
  100. def save_appmenu_select_changes(self):
  101. if self.save_list_of_selected():
  102. subprocess.check_call([qubes_appmenu_remove_cmd, self.vm.name])
  103. subprocess.check_call([qubes_appmenu_create_cmd, self.source_vm.appmenus_templates_dir, self.vm.name])