Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr/520'
Browse files Browse the repository at this point in the history
* origin/pr/520:
  Hide Copy/Move/Edit/View to qube for network items
  Allow recent:/// in Nautilus for Copy/Move/Open
  • Loading branch information
marmarek committed Sep 28, 2024
2 parents 468e15f + 91374a1 commit 702a825
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 23 deletions.
56 changes: 50 additions & 6 deletions qubes-rpc/nautilus/qvm_copy_nautilus.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from gi.repository import Nautilus, GObject, GLib

import os.path
from gi.repository import Nautilus, GObject, GLib, Gio

class CopyToAppvmItemExtension(GObject.GObject, Nautilus.MenuProvider):
'''Copy file(s) to AppVM.
Expand All @@ -17,6 +17,34 @@ def get_file_items(self, *args):
if not files:
return

# Do not attach context menu to anything other than local items
# - or recent items which point to actual local items
for file_obj in files:
file_uri_scheme = file_obj.get_uri_scheme()
if file_uri_scheme == 'file':
# Check if file is not gone in the meantime
if file_obj.is_gone():
return
else:
continue
elif file_uri_scheme == 'recent':
# Ensure recent item is actually a local item & it still exists
try:
file_location = file_obj.get_location()
file_info = file_location.query_info(
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI, 0, None)
target_uri = file_info.get_attribute_string(
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI)
if not target_uri.startswith('file://'):
# Maybe a network item in recents. Hide menu.
return
except GLib.GError:
# Item in recents points to a file which is gone. Hide menu.
return
else:
# Not a local file (e.g. smb://). Hide menu.
return

menu_item = Nautilus.MenuItem(name='QubesMenuProvider::CopyToAppvm',
label='Copy to other qube...',
tip='',
Expand All @@ -28,10 +56,26 @@ def get_file_items(self, *args):
def on_menu_item_clicked(self, menu, files):
'''Called when user chooses files though Nautilus context menu.
'''
cmd = [file_obj.get_location().get_path()
for file_obj in files
# Check if file is not gone
if not file_obj.is_gone()]
paths = []
for file_obj in files:
file_location = file_obj.get_location()
file_uri_scheme = file_obj.get_uri_scheme()
if file_uri_scheme == 'file':
if not file_obj.is_gone():
# Check yet another time if file is not gone
paths.append(file_location.get_path())
elif file_uri_scheme == 'recent':
try:
file_info = file_location.query_info(
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI, 0, None)
target_uri = file_info.get_attribute_string(
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI)
paths.append(target_uri[7:])
except GLib.GError:
pass

# Double-check if the file is not gone in the meantime
cmd = [path for path in paths if os.path.exists(path)]
cmd.insert(0, '/usr/lib/qubes/qvm-copy-to-vm.gnome')
pid = GLib.spawn_async(cmd)[0]
GLib.spawn_close_pid(pid)
61 changes: 50 additions & 11 deletions qubes-rpc/nautilus/qvm_dvm_nautilus.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from gi.repository import Nautilus, GObject, GLib
import os.path
from gi.repository import Nautilus, GObject, GLib, Gio


class OpenInDvmItemExtension(GObject.GObject, Nautilus.MenuProvider):
Expand All @@ -18,6 +19,34 @@ def get_file_items(self, *args):
if not files:
return

# Do not attach context menu to anything other than local items
# - or recent items which point to actual local items
for file_obj in files:
file_uri_scheme = file_obj.get_uri_scheme()
if file_uri_scheme == 'file':
# Check if file is not gone in the meantime
if file_obj.is_gone():
return
else:
continue
elif file_uri_scheme == 'recent':
# Ensure recent item is actually a local item & it still exists
try:
file_location = file_obj.get_location()
file_info = file_location.query_info(
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI, 0, None)
target_uri = file_info.get_attribute_string(
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI)
if not target_uri.startswith('file://'):
# Maybe a network item in recents. Hide menu.
return
except GLib.GError:
# Item in recents points to a file which is gone. Hide menu.
return
else:
# Not a local file (e.g. smb://). Hide menu.
return

menu_item1 = Nautilus.MenuItem(name='QubesMenuProvider::OpenInDvm',
label='Edit in disposable qube',
tip='',
Expand All @@ -30,26 +59,36 @@ def get_file_items(self, *args):
tip='',
icon='')

menu_item2.connect('activate',
self.on_menu_item_clicked,
files, True)
menu_item2.connect('activate', self.on_menu_item_clicked, files, True)
return menu_item1, menu_item2,

def on_menu_item_clicked(self, menu, files, view_only=False):
'''Called when user chooses files though Nautilus context menu.
'''
for file_obj in files:

# Check if file still exists
if file_obj.is_gone():
return

gio_file = file_obj.get_location()
file_location = file_obj.get_location()
file_uri = file_location.get_uri()
file_uri_scheme = file_obj.get_uri_scheme()
if file_uri_scheme == 'file':
if not file_obj.is_gone():
# Check yet another time if file is not gone
file_path = file_location.get_path()
else:
return
elif file_uri_scheme == 'recent':
try:
file_info = file_location.query_info(
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI, 0, None)
target_uri = file_info.get_attribute_string(
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI)
file_path = target_uri[7:]
except GLib.GError:
return

command = ['/usr/bin/qvm-open-in-dvm']
if view_only:
command.append('--view-only')
command.append(gio_file.get_path())
command.append(file_path)

pid = GLib.spawn_async(command)[0]
GLib.spawn_close_pid(pid)
55 changes: 49 additions & 6 deletions qubes-rpc/nautilus/qvm_move_nautilus.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from gi.repository import Nautilus, GObject, GLib

import os.path
from gi.repository import Nautilus, GObject, GLib, Gio

class MoveToAppvmItemExtension(GObject.GObject, Nautilus.MenuProvider):
'''Move file(s) to AppVM.
Expand All @@ -17,6 +17,34 @@ def get_file_items(self, *args):
if not files:
return

# Do not attach context menu to anything other than local items
# - or recent items which point to actual local items
for file_obj in files:
file_uri_scheme = file_obj.get_uri_scheme()
if file_uri_scheme == 'file':
# Check if file is not gone in the meantime
if file_obj.is_gone():
return
else:
continue
elif file_uri_scheme == 'recent':
# Ensure recent item is actually a local item & it still exists
try:
file_location = file_obj.get_location()
file_info = file_location.query_info(
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI, 0, None)
target_uri = file_info.get_attribute_string(
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI)
if not target_uri.startswith('file://'):
# Maybe a network item in recents. Hide menu.
return
except GLib.GError:
# Item in recents points to a file which is gone. Hide menu.
return
else:
# Not a local file (e.g. smb://). Hide menu.
return

menu_item = Nautilus.MenuItem(name='QubesMenuProvider::MoveToAppvm',
label='Move to other qube...',
tip='',
Expand All @@ -28,10 +56,25 @@ def get_file_items(self, *args):
def on_menu_item_clicked(self, menu, files):
'''Called when user chooses files though Nautilus context menu.
'''
cmd = [file_obj.get_location().get_path()
for file_obj in files
# Check if file is not gone
if not file_obj.is_gone()]
paths = []
for file_obj in files:
file_location = file_obj.get_location()
file_uri_scheme = file_obj.get_uri_scheme()
if file_uri_scheme == 'file':
if not file_obj.is_gone():
# Check yet another time if file is not gone
paths.append(file_location.get_path())
elif file_uri_scheme == 'recent':
try:
file_info = file_location.query_info(
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI, 0, None)
target_uri = file_info.get_attribute_string(
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI)
paths.append(target_uri[7:])
except GLib.GError:
pass
# Double-check if the file is not gone in the meantime
cmd = [path for path in paths if os.path.exists(path)]
cmd.insert(0, '/usr/lib/qubes/qvm-move-to-vm.gnome')
pid = GLib.spawn_async(cmd)[0]
GLib.spawn_close_pid(pid)

0 comments on commit 702a825

Please sign in to comment.