qvm_dvm_nautilus.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import os
  2. from subprocess import Popen
  3. from gi.repository import Nautilus, GObject
  4. class OpenInDvmItemExtension(GObject.GObject, Nautilus.MenuProvider):
  5. '''Open File(s) in DisposableVM.
  6. Uses the nautilus-python api to provide a context menu within Nautilus which
  7. will enable the user to select file(s) to to open in a disposableVM
  8. '''
  9. def get_file_items(self, window, files):
  10. '''Attaches context menu in Nautilus
  11. '''
  12. if not files:
  13. return
  14. menu_item1 = Nautilus.MenuItem(name='QubesMenuProvider::OpenInDvm',
  15. label='Edit In DisposableVM',
  16. tip='',
  17. icon='')
  18. menu_item1.connect('activate', self.on_menu_item_clicked, files)
  19. menu_item2 = Nautilus.MenuItem(name='QubesMenuProvider::ViewInDvm',
  20. label='View In DisposableVM',
  21. tip='',
  22. icon='')
  23. menu_item2.connect('activate',
  24. self.on_menu_item_clicked,
  25. files, True)
  26. return menu_item1, menu_item2,
  27. def on_menu_item_clicked(self, menu, files, view_only=False):
  28. '''Called when user chooses files though Nautilus context menu.
  29. '''
  30. for file_obj in files:
  31. # Check if file still exists
  32. if file_obj.is_gone():
  33. return
  34. gio_file = file_obj.get_location()
  35. # Use subprocess.DEVNULL in python >= 3.3
  36. devnull = open(os.devnull, 'wb')
  37. command = ['nohup', '/usr/bin/qvm-open-in-dvm']
  38. if view_only:
  39. command.append('--view-only')
  40. command.append(gio_file.get_path())
  41. # Use Popen instead of subprocess.call to spawn the process
  42. Popen(command, stdout=devnull, stderr=devnull)
  43. devnull.close()