Skip to content

Commit 702a825

Browse files
committed
Merge remote-tracking branch 'origin/pr/520'
* origin/pr/520: Hide Copy/Move/Edit/View to qube for network items Allow recent:/// in Nautilus for Copy/Move/Open
2 parents 468e15f + 91374a1 commit 702a825

File tree

3 files changed

+149
-23
lines changed

3 files changed

+149
-23
lines changed

qubes-rpc/nautilus/qvm_copy_nautilus.py

100755100644
+50-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from gi.repository import Nautilus, GObject, GLib
2-
1+
import os.path
2+
from gi.repository import Nautilus, GObject, GLib, Gio
33

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

20+
# Do not attach context menu to anything other than local items
21+
# - or recent items which point to actual local items
22+
for file_obj in files:
23+
file_uri_scheme = file_obj.get_uri_scheme()
24+
if file_uri_scheme == 'file':
25+
# Check if file is not gone in the meantime
26+
if file_obj.is_gone():
27+
return
28+
else:
29+
continue
30+
elif file_uri_scheme == 'recent':
31+
# Ensure recent item is actually a local item & it still exists
32+
try:
33+
file_location = file_obj.get_location()
34+
file_info = file_location.query_info(
35+
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI, 0, None)
36+
target_uri = file_info.get_attribute_string(
37+
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI)
38+
if not target_uri.startswith('file://'):
39+
# Maybe a network item in recents. Hide menu.
40+
return
41+
except GLib.GError:
42+
# Item in recents points to a file which is gone. Hide menu.
43+
return
44+
else:
45+
# Not a local file (e.g. smb://). Hide menu.
46+
return
47+
2048
menu_item = Nautilus.MenuItem(name='QubesMenuProvider::CopyToAppvm',
2149
label='Copy to other qube...',
2250
tip='',
@@ -28,10 +56,26 @@ def get_file_items(self, *args):
2856
def on_menu_item_clicked(self, menu, files):
2957
'''Called when user chooses files though Nautilus context menu.
3058
'''
31-
cmd = [file_obj.get_location().get_path()
32-
for file_obj in files
33-
# Check if file is not gone
34-
if not file_obj.is_gone()]
59+
paths = []
60+
for file_obj in files:
61+
file_location = file_obj.get_location()
62+
file_uri_scheme = file_obj.get_uri_scheme()
63+
if file_uri_scheme == 'file':
64+
if not file_obj.is_gone():
65+
# Check yet another time if file is not gone
66+
paths.append(file_location.get_path())
67+
elif file_uri_scheme == 'recent':
68+
try:
69+
file_info = file_location.query_info(
70+
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI, 0, None)
71+
target_uri = file_info.get_attribute_string(
72+
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI)
73+
paths.append(target_uri[7:])
74+
except GLib.GError:
75+
pass
76+
77+
# Double-check if the file is not gone in the meantime
78+
cmd = [path for path in paths if os.path.exists(path)]
3579
cmd.insert(0, '/usr/lib/qubes/qvm-copy-to-vm.gnome')
3680
pid = GLib.spawn_async(cmd)[0]
3781
GLib.spawn_close_pid(pid)

qubes-rpc/nautilus/qvm_dvm_nautilus.py

100755100644
+50-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from gi.repository import Nautilus, GObject, GLib
1+
import os.path
2+
from gi.repository import Nautilus, GObject, GLib, Gio
23

34

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

22+
# Do not attach context menu to anything other than local items
23+
# - or recent items which point to actual local items
24+
for file_obj in files:
25+
file_uri_scheme = file_obj.get_uri_scheme()
26+
if file_uri_scheme == 'file':
27+
# Check if file is not gone in the meantime
28+
if file_obj.is_gone():
29+
return
30+
else:
31+
continue
32+
elif file_uri_scheme == 'recent':
33+
# Ensure recent item is actually a local item & it still exists
34+
try:
35+
file_location = file_obj.get_location()
36+
file_info = file_location.query_info(
37+
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI, 0, None)
38+
target_uri = file_info.get_attribute_string(
39+
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI)
40+
if not target_uri.startswith('file://'):
41+
# Maybe a network item in recents. Hide menu.
42+
return
43+
except GLib.GError:
44+
# Item in recents points to a file which is gone. Hide menu.
45+
return
46+
else:
47+
# Not a local file (e.g. smb://). Hide menu.
48+
return
49+
2150
menu_item1 = Nautilus.MenuItem(name='QubesMenuProvider::OpenInDvm',
2251
label='Edit in disposable qube',
2352
tip='',
@@ -30,26 +59,36 @@ def get_file_items(self, *args):
3059
tip='',
3160
icon='')
3261

33-
menu_item2.connect('activate',
34-
self.on_menu_item_clicked,
35-
files, True)
62+
menu_item2.connect('activate', self.on_menu_item_clicked, files, True)
3663
return menu_item1, menu_item2,
3764

3865
def on_menu_item_clicked(self, menu, files, view_only=False):
3966
'''Called when user chooses files though Nautilus context menu.
4067
'''
4168
for file_obj in files:
42-
43-
# Check if file still exists
44-
if file_obj.is_gone():
45-
return
46-
47-
gio_file = file_obj.get_location()
69+
file_location = file_obj.get_location()
70+
file_uri = file_location.get_uri()
71+
file_uri_scheme = file_obj.get_uri_scheme()
72+
if file_uri_scheme == 'file':
73+
if not file_obj.is_gone():
74+
# Check yet another time if file is not gone
75+
file_path = file_location.get_path()
76+
else:
77+
return
78+
elif file_uri_scheme == 'recent':
79+
try:
80+
file_info = file_location.query_info(
81+
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI, 0, None)
82+
target_uri = file_info.get_attribute_string(
83+
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI)
84+
file_path = target_uri[7:]
85+
except GLib.GError:
86+
return
4887

4988
command = ['/usr/bin/qvm-open-in-dvm']
5089
if view_only:
5190
command.append('--view-only')
52-
command.append(gio_file.get_path())
91+
command.append(file_path)
5392

5493
pid = GLib.spawn_async(command)[0]
5594
GLib.spawn_close_pid(pid)

qubes-rpc/nautilus/qvm_move_nautilus.py

100755100644
+49-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from gi.repository import Nautilus, GObject, GLib
2-
1+
import os.path
2+
from gi.repository import Nautilus, GObject, GLib, Gio
33

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

20+
# Do not attach context menu to anything other than local items
21+
# - or recent items which point to actual local items
22+
for file_obj in files:
23+
file_uri_scheme = file_obj.get_uri_scheme()
24+
if file_uri_scheme == 'file':
25+
# Check if file is not gone in the meantime
26+
if file_obj.is_gone():
27+
return
28+
else:
29+
continue
30+
elif file_uri_scheme == 'recent':
31+
# Ensure recent item is actually a local item & it still exists
32+
try:
33+
file_location = file_obj.get_location()
34+
file_info = file_location.query_info(
35+
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI, 0, None)
36+
target_uri = file_info.get_attribute_string(
37+
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI)
38+
if not target_uri.startswith('file://'):
39+
# Maybe a network item in recents. Hide menu.
40+
return
41+
except GLib.GError:
42+
# Item in recents points to a file which is gone. Hide menu.
43+
return
44+
else:
45+
# Not a local file (e.g. smb://). Hide menu.
46+
return
47+
2048
menu_item = Nautilus.MenuItem(name='QubesMenuProvider::MoveToAppvm',
2149
label='Move to other qube...',
2250
tip='',
@@ -28,10 +56,25 @@ def get_file_items(self, *args):
2856
def on_menu_item_clicked(self, menu, files):
2957
'''Called when user chooses files though Nautilus context menu.
3058
'''
31-
cmd = [file_obj.get_location().get_path()
32-
for file_obj in files
33-
# Check if file is not gone
34-
if not file_obj.is_gone()]
59+
paths = []
60+
for file_obj in files:
61+
file_location = file_obj.get_location()
62+
file_uri_scheme = file_obj.get_uri_scheme()
63+
if file_uri_scheme == 'file':
64+
if not file_obj.is_gone():
65+
# Check yet another time if file is not gone
66+
paths.append(file_location.get_path())
67+
elif file_uri_scheme == 'recent':
68+
try:
69+
file_info = file_location.query_info(
70+
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI, 0, None)
71+
target_uri = file_info.get_attribute_string(
72+
Gio.FILE_ATTRIBUTE_STANDARD_TARGET_URI)
73+
paths.append(target_uri[7:])
74+
except GLib.GError:
75+
pass
76+
# Double-check if the file is not gone in the meantime
77+
cmd = [path for path in paths if os.path.exists(path)]
3578
cmd.insert(0, '/usr/lib/qubes/qvm-move-to-vm.gnome')
3679
pid = GLib.spawn_async(cmd)[0]
3780
GLib.spawn_close_pid(pid)

0 commit comments

Comments
 (0)