appmenu_select.py 4.8 KB

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