-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnautilus-open-with-menu.py
More file actions
128 lines (108 loc) · 4.54 KB
/
nautilus-open-with-menu.py
File metadata and controls
128 lines (108 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# nautilus-open-with-menu.py ---
#
# Filename: nautilus-open-with-menu.py
# Description:
# Author: Lord Yuuma
# Maintainer: Lord Yuuma
# Created: Mon Apr 18 17:50:46 2016 (+0200)
# Version:
# Package-Requires: ()
# Last-Updated: Sun May 15 01:11:05 2016 (+0200)
# By: Lord Yuuma
# URL:
# Doc URL:
# Keywords:
# Compatibility:
#
#
# Commentary:
#
#
#
#
# Change Log:
#
#
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
#
#
# Code:
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Nautilus', '3.0')
from gi.repository import Nautilus, GObject, Gio, Gtk
from functools import reduce
from operator import and_
class NautilusOpenWithMenu(Nautilus.MenuProvider, GObject.GObject):
def __init__(self):
pass
def get_file_items(self, window, files):
if not len(files):
return None
root_item = Nautilus.MenuItem(name="NautilusOpenWithMenu::root",
label="Open With...")
sub_menu = Nautilus.Menu()
root_item.set_submenu(sub_menu)
# for a large number of files, this is a good optimization, especially when they all have the same mimetype
# consider for instance opening thousand of music files with VLC or something similar and having to check all applications for each file
mimetypes = list(set([file.get_mime_type() for file in files]))
# here we search for ApplicationInfos, that are common for all
# because we want them to open ALL the files, they have to be a subset of the applications that can open the first
apps = [app_info for app_info in Gio.app_info_get_all_for_type(mimetypes[0])]
# here we build the list of name of applications that can be used for all files
names = [set([app_info.get_name() for app_info in Gio.app_info_get_all_for_type(type)])
for type in mimetypes]
names = list(reduce(and_, names))
# and then we reduce our application list to what is left
apps = [app for app in apps if app.get_name() in names]
if len(apps) < 2 and len(mimetypes) == 1 or len(apps) == 0:
return []
for app in apps:
item = Nautilus.MenuItem(name="NautilusOpenWithMenu::{}".format(app.get_name()),
label=app.get_name(),
icon=app.get_icon())
item.connect('activate', self.callback, app, files)
sub_menu.append_item(item)
if len(mimetypes) == 1:
item = Nautilus.MenuItem(name="NautilusOpenWithMenu::set_default",
label="Set default")
item.connect('activate', self.set_default, window, mimetypes[0], files)
sub_menu.append_item(item)
else:
item = Nautilus.MenuItem(name="NautilusOpenWithMenu::set_default_for",
label="Set default for")
set_default = Nautilus.Menu()
item.set_submenu(set_default)
sub_menu.append_item(item)
for mimetype in mimetypes:
item = Nautilus.MenuItem(name="NautilusOpenWithMenu::set_default_for_{}".format(mimetype),
label=mimetype)
item.connect('activate', self.set_default, window, mimetype, files)
set_default.append_item(item)
return [root_item]
def callback(self, menu, app, files):
fs = [Gio.File.new_for_uri(file.get_uri()) for file in files]
app.launch(fs)
def set_default(self, menu, window, mimetype, files):
dialog = Gtk.AppChooserDialog.new_for_content_type(window, 0, mimetype)
result = dialog.run()
if result == Gtk.ResponseType.OK:
app = dialog.get_app_info()
if app:
fs = [Gio.File.new_for_uri(file.get_uri()) for file in files if file.get_mime_type() == mimetype]
app.launch(fs)
app.set_as_default_for_type(mimetype)
dialog.destroy()