appmenu_select.py 4.8 KB

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