Skip to content

Commit

Permalink
Hide Copy/Move/Edit/View to qube for network items
Browse files Browse the repository at this point in the history
It does not make sense to show the 'Copy/Move to other qube'
or 'Edit/View in disposable qube' menu if it is a network item

(cherry picked from commit 91374a1)
  • Loading branch information
alimirjamali authored and marmarek committed Nov 5, 2024
1 parent 6cbf7c0 commit e06b4ef
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 37 deletions.
48 changes: 36 additions & 12 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 @@ -31,25 +59,21 @@ def on_menu_item_clicked(self, menu, files):
paths = []
for file_obj in files:
file_location = file_obj.get_location()
file_uri = file_location.get_uri()
if file_uri.startswith('file:///'):
file_uri_scheme = file_obj.get_uri_scheme()
if file_uri_scheme == 'file':
if not file_obj.is_gone():
# Check if file is not gone
# Check yet another time if file is not gone
paths.append(file_location.get_path())
elif file_uri.startswith('recent:///'):
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)
if target_uri.startswith('file://'):
paths.append(target_uri[7:])
paths.append(target_uri[7:])
except GLib.GError:
# TODO: Decide what to do if the recent item does not exist
pass
else:
# TODO: Decide what to do with other weird URIs (eg. smb:///)
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')
Expand Down
48 changes: 34 additions & 14 deletions qubes-rpc/nautilus/qvm_dvm_nautilus.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,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 @@ -31,9 +59,7 @@ 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):
Expand All @@ -42,28 +68,22 @@ def on_menu_item_clicked(self, menu, files, view_only=False):
for file_obj in files:
file_location = file_obj.get_location()
file_uri = file_location.get_uri()
if file_uri.startswith('file:///'):
file_uri_scheme = file_obj.get_uri_scheme()
if file_uri_scheme == 'file':
if not file_obj.is_gone():
# Check if file still exists
# Check yet another time if file is not gone
file_path = file_location.get_path()
else:
return
elif file_uri.startswith('recent:///'):
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)
if target_uri.startswith('file://'):
file_path = target_uri[7:]
if not os.path.exists(file_path):
return
file_path = target_uri[7:]
except GLib.GError:
#TODO: Decide what to do if the recent item does not exist
return
else:
# TODO: Decide what to do with other weird URIs (eg. smb:///)
return

command = ['/usr/bin/qvm-open-in-dvm']
if view_only:
Expand Down
44 changes: 33 additions & 11 deletions qubes-rpc/nautilus/qvm_move_nautilus.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -31,28 +59,22 @@ def on_menu_item_clicked(self, menu, files):
paths = []
for file_obj in files:
file_location = file_obj.get_location()
file_uri = file_location.get_uri()
if file_uri.startswith('file:///'):
file_uri_scheme = file_obj.get_uri_scheme()
if file_uri_scheme == 'file':
if not file_obj.is_gone():
# Check if file is not gone
# Check yet another time if file is not gone
paths.append(file_location.get_path())
elif file_uri.startswith('recent:///'):
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)
if target_uri.startswith('file://'):
paths.append(target_uri[7:])
paths.append(target_uri[7:])
except GLib.GError:
# TODO: Decide what to do if the recent item does not exist
pass
else:
# TODO: Decide what to do with other weird URIs (eg. smb:///)
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)
# TODO: Refresh Nautilus to remove moved files from recents list

0 comments on commit e06b4ef

Please sign in to comment.