xmleditor: make gtksourceview optional

The only thing that GtkSourceView gives us is syntax highlighting and
auto-indent. When this library is not available, we can still offer xml
editing with a plain textview with very little lost functionality.

Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
This commit is contained in:
Jonathon Jongsma 2024-05-16 11:05:23 -05:00 committed by Pavel Hrdina
parent a91e23c858
commit 6a85d79a31
2 changed files with 28 additions and 13 deletions

View File

@ -25,9 +25,6 @@ Requires: libvirt-glib >= 0.0.9
Requires: gtk-vnc2
Requires: spice-gtk3
# We can work with gtksourceview 3 or gtksourceview4, pick the latest one
Requires: gtksourceview4
# virt-manager is one of those apps that people will often install onto
# a headless machine for use over SSH. This means the virt-manager dep
# chain needs to provide everything we need to get a usable app experience.
@ -42,6 +39,10 @@ Requires: dconf
# no ambiguity.
Requires: vte291
# We can use GtkTextView, gtksourceview 3 or gtksourceview4, recommend
# the latest one but don't make it a hard requirement
Recommends: gtksourceview4
# Weak dependencies for the common virt-manager usecase
Recommends: (libvirt-daemon-kvm or libvirt-daemon-qemu)
Recommends: libvirt-daemon-config-network

View File

@ -7,13 +7,24 @@ import gi
from virtinst import log
# We can use either gtksourceview3 or gtksourceview4
have_gtksourceview = True
try:
gi.require_version("GtkSource", "4")
log.debug("Using GtkSource 4")
except ValueError: # pragma: no cover
gi.require_version("GtkSource", "3.0")
log.debug("Using GtkSource 3.0")
from gi.repository import GtkSource
try:
gi.require_version("GtkSource", "3.0")
log.debug("Using GtkSource 3.0")
except ValueError:
log.debug("Not using GtkSource")
have_gtksourceview = False
if have_gtksourceview:
from gi.repository import GtkSource
else:
# if GtkSourceView is not available, just use a plain TextView. This will
# only disable auto-indent and syntax highlighting.
from gi.repository import Gtk
from .lib import uiutil
from .baseclass import vmmGObjectUI
@ -66,17 +77,20 @@ class vmmXMLEditor(vmmGObjectUI):
not enabled)
def _init_ui(self):
self._srcview = GtkSource.View()
self._srcbuff = self._srcview.get_buffer()
lang = GtkSource.LanguageManager.get_default().get_language("xml")
self._srcbuff.set_language(lang)
if have_gtksourceview:
self._srcview = GtkSource.View()
self._srcbuff = self._srcview.get_buffer()
self._srcview.set_auto_indent(True)
lang = GtkSource.LanguageManager.get_default().get_language("xml")
self._srcbuff.set_language(lang)
self._srcbuff.set_highlight_syntax(True)
else:
self._srcview = Gtk.TextView()
self._srcbuff = self._srcview.get_buffer()
self._srcview.set_monospace(True)
self._srcview.set_auto_indent(True)
self._srcview.get_accessible().set_name("XML editor")
self._srcbuff.set_highlight_syntax(True)
self._srcbuff.connect("changed", self._buffer_changed_cb)
self.widget("xml-notebook").connect("switch-page",