Skip to content

Commit

Permalink
nautilus: Add compatibility with Nautilus 43
Browse files Browse the repository at this point in the history
Nautilus 43 and nautilus-python 4 have made major changes to the API

Nautilus.LocationWidgetProvider has been dropped from the API
without a replacement

https://gnome.pages.gitlab.gnome.org/nautilus-python/nautilus-python-migrating-to-4.html
  • Loading branch information
jbicha authored and ximion committed Apr 20, 2023
1 parent 33c0538 commit f9ef151
Showing 1 changed file with 45 additions and 40 deletions.
85 changes: 45 additions & 40 deletions data/nautilus/open-tilix.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
from urllib.parse import unquote, urlparse


from gi.repository import Gio, GObject, Nautilus
from gi import require_version
require_version('Gtk', '3.0')
from gi.repository import Gio, GObject, Gtk, Nautilus
if hasattr(Nautilus, "LocationWidgetProvider"):
require_version('Gtk', '3.0')
from gi.repository import Gtk


TERMINAL = "tilix"
Expand All @@ -34,43 +36,44 @@ def open_terminal_in_file(filename):
else:
call("{0} &".format(TERMINAL), shell=True)

# Nautilus 43 doesn't offer the LocationWidgetProvider any more
if hasattr(Nautilus, "LocationWidgetProvider"):
class OpenTilixShortcutProvider(GObject.GObject,
Nautilus.LocationWidgetProvider):

def __init__(self):
source = Gio.SettingsSchemaSource.get_default()
if source.lookup(TILIX_KEYBINDINGS, True):
self._gsettings = Gio.Settings.new(TILIX_KEYBINDINGS)
self._gsettings.connect("changed", self._bind_shortcut)
self._create_accel_group()
self._window = None
self._uri = None

def _create_accel_group(self):
self._accel_group = Gtk.AccelGroup()
shortcut = self._gsettings.get_string(GSETTINGS_OPEN_TERMINAL)
key, mod = Gtk.accelerator_parse(shortcut)
self._accel_group.connect(key, mod, Gtk.AccelFlags.VISIBLE,
self._open_terminal)

def _bind_shortcut(self, gsettings, key):
if key == GSETTINGS_OPEN_TERMINAL:
self._accel_group.disconnect(self._open_terminal)
self._create_accel_group()

def _open_terminal(self, *args):
filename = unquote(self._uri[7:])
open_terminal_in_file(filename)

class OpenTilixShortcutProvider(GObject.GObject,
Nautilus.LocationWidgetProvider):

def __init__(self):
source = Gio.SettingsSchemaSource.get_default()
if source.lookup(TILIX_KEYBINDINGS, True):
self._gsettings = Gio.Settings.new(TILIX_KEYBINDINGS)
self._gsettings.connect("changed", self._bind_shortcut)
self._create_accel_group()
self._window = None
self._uri = None

def _create_accel_group(self):
self._accel_group = Gtk.AccelGroup()
shortcut = self._gsettings.get_string(GSETTINGS_OPEN_TERMINAL)
key, mod = Gtk.accelerator_parse(shortcut)
self._accel_group.connect(key, mod, Gtk.AccelFlags.VISIBLE,
self._open_terminal)

def _bind_shortcut(self, gsettings, key):
if key == GSETTINGS_OPEN_TERMINAL:
self._accel_group.disconnect(self._open_terminal)
self._create_accel_group()

def _open_terminal(self, *args):
filename = unquote(self._uri[7:])
open_terminal_in_file(filename)

def get_widget(self, uri, window):
self._uri = uri
if self._window:
self._window.remove_accel_group(self._accel_group)
if self._gsettings:
window.add_accel_group(self._accel_group)
self._window = window
return None
def get_widget(self, uri, window):
self._uri = uri
if self._window:
self._window.remove_accel_group(self._accel_group)
if self._gsettings:
window.add_accel_group(self._accel_group)
self._window = window
return None


class OpenTilixExtension(GObject.GObject, Nautilus.MenuProvider):
Expand Down Expand Up @@ -99,7 +102,8 @@ def _menu_activate_cb(self, menu, file_):
def _menu_background_activate_cb(self, menu, file_):
self._open_terminal(file_)

def get_file_items(self, window, files):
def get_file_items(self, *args):
files = args[-1]
if len(files) != 1:
return
items = []
Expand All @@ -124,7 +128,8 @@ def get_file_items(self, window, files):

return items

def get_background_items(self, window, file_):
def get_background_items(self, *args):
file_ = args[-1]
items = []
if file_.get_uri_scheme() in REMOTE_URI_SCHEME:
item = Nautilus.MenuItem(name='NautilusPython::open_bg_remote_item',
Expand Down

0 comments on commit f9ef151

Please sign in to comment.