appmenu_select.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 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. # Check if appmenu entry is really installed
  63. whitelisted = [a for a in whitelisted if os.path.exists('%s/apps/%s-%s' % (self.vm.dir_path, self.vm.name, a))]
  64. self.app_list.clear()
  65. available_appmenus = []
  66. for template_file in template_file_list:
  67. desktop_template = open(template_dir + '/' + template_file, 'r')
  68. for line in desktop_template:
  69. if line.startswith("Name=%VMNAME%: "):
  70. desktop_name = line.partition('Name=%VMNAME%: ')[2].strip()
  71. available_appmenus.append( (template_file, desktop_name) )
  72. break
  73. desktop_template.close()
  74. self.whitelisted_appmenus = [a for a in available_appmenus if a[0] in whitelisted]
  75. available_appmenus = [a for a in available_appmenus if a[0] not in whitelisted]
  76. for a in available_appmenus:
  77. self.app_list.available_list.addItem( AppListWidgetItem(a[1], a[0]))
  78. for a in self.whitelisted_appmenus:
  79. self.app_list.selected_list.addItem( AppListWidgetItem(a[1], a[0]))
  80. self.app_list.available_list.sortItems()
  81. self.app_list.selected_list.sortItems()
  82. def save_list_of_selected(self):
  83. sth_changed = False
  84. added = []
  85. for i in range(self.app_list.selected_list.count()):
  86. item = self.app_list.selected_list.item(i)
  87. if item.filename not in [ w[0] for w in self.whitelisted_appmenus]:
  88. added.append(item)
  89. if self.app_list.selected_list.count() - len(added) < len(self.whitelisted_appmenus): #sth removed
  90. sth_changed = True;
  91. elif len(added) > 0:
  92. sth_changed = True;
  93. if sth_changed == True:
  94. whitelisted = open(self.vm.dir_path + '/' + whitelisted_filename, 'w')
  95. for i in range(self.app_list.selected_list.count()):
  96. item = self.app_list.selected_list.item(i)
  97. whitelisted.write(item.filename + '\n')
  98. whitelisted.close()
  99. return True
  100. else:
  101. return False
  102. def save_appmenu_select_changes(self):
  103. if self.save_list_of_selected():
  104. self.vm.remove_appmenus()
  105. self.vm.create_appmenus(verbose=False)