2009-03-04 14:44:01 +01:00
|
|
|
## src/gtkspell.py
|
|
|
|
##
|
|
|
|
## (C) 2008 Thorsten P. 'dGhvcnN0ZW5wIEFUIHltYWlsIGNvbQ==\n'.decode("base64")
|
2015-08-16 14:22:04 +02:00
|
|
|
## (C) 2015 Yann Leboulanger <asterix AT lagaule.org>
|
2009-03-04 14:44:01 +01:00
|
|
|
##
|
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim 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; version 3 only.
|
|
|
|
##
|
|
|
|
## Gajim 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 Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-05-22 23:09:49 +02:00
|
|
|
from gi.repository import GObject
|
2012-12-27 00:02:51 +01:00
|
|
|
from gi.repository import Gtk
|
2017-10-19 11:26:22 +02:00
|
|
|
from gi.repository import GLib
|
2015-09-20 18:18:35 +02:00
|
|
|
import gi
|
2017-10-19 11:26:22 +02:00
|
|
|
try:
|
2017-10-27 10:35:06 +02:00
|
|
|
gi.require_version('GtkSpell', '3.0')
|
2017-10-19 11:26:22 +02:00
|
|
|
from gi.repository import GtkSpell
|
|
|
|
HAS_GTK_SPELL = True
|
2017-10-27 10:42:43 +02:00
|
|
|
except (ImportError, ValueError):
|
2017-10-19 11:26:22 +02:00
|
|
|
HAS_GTK_SPELL = False
|
|
|
|
|
2017-10-03 13:08:06 +02:00
|
|
|
|
2009-03-04 14:44:01 +01:00
|
|
|
def ensure_attached(func):
|
|
|
|
def f(self, *args, **kwargs):
|
|
|
|
if self.spell:
|
|
|
|
func(self, *args, **kwargs)
|
|
|
|
else:
|
|
|
|
raise RuntimeError("Spell object is already detached")
|
|
|
|
return f
|
|
|
|
|
|
|
|
|
2016-05-22 23:09:49 +02:00
|
|
|
class Spell(GObject.GObject):
|
|
|
|
__gsignals__ = {
|
|
|
|
'language_changed': (GObject.SignalFlags.RUN_FIRST, None, (str,))
|
|
|
|
}
|
2009-03-04 14:44:01 +01:00
|
|
|
|
2016-05-22 23:09:49 +02:00
|
|
|
def __init__(self, textview, language=None, create=True, jid=None,
|
|
|
|
per_type=None):
|
|
|
|
GObject.GObject.__init__(self)
|
2012-12-27 00:02:51 +01:00
|
|
|
if not isinstance(textview, Gtk.TextView):
|
|
|
|
raise TypeError("Textview must be derived from Gtk.TextView")
|
2015-08-16 14:22:04 +02:00
|
|
|
spell = GtkSpell.Checker.get_from_text_view(textview)
|
2009-03-04 14:44:01 +01:00
|
|
|
if create:
|
|
|
|
if spell:
|
|
|
|
raise RuntimeError("Textview has already a Spell obj attached")
|
2015-08-16 14:22:04 +02:00
|
|
|
self.spell = GtkSpell.Checker.new()
|
2017-10-19 11:26:22 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.spell.set_language(language)
|
|
|
|
except GLib.GError as error:
|
|
|
|
if error.domain == 'gtkspell-error-quark':
|
|
|
|
raise OSError("Unable to set language: '%s'" % language)
|
|
|
|
|
2016-05-22 23:09:49 +02:00
|
|
|
self.spell.connect('language-changed', self.on_language_changed)
|
2015-08-16 14:22:04 +02:00
|
|
|
|
2009-03-04 14:44:01 +01:00
|
|
|
else:
|
|
|
|
if spell:
|
|
|
|
self.spell = spell
|
|
|
|
else:
|
|
|
|
raise RuntimeError("Textview has no Spell object attached")
|
|
|
|
|
2016-05-22 23:09:49 +02:00
|
|
|
def on_language_changed(self, spell, lang):
|
|
|
|
self.emit('language_changed', lang)
|
|
|
|
|
2009-03-04 14:44:01 +01:00
|
|
|
@ensure_attached
|
|
|
|
def set_language(self, language):
|
2015-08-16 14:22:04 +02:00
|
|
|
if not self.spell.set_language(language):
|
|
|
|
raise OSError("Unable to set language: '%s'" % language)
|
2009-03-04 14:44:01 +01:00
|
|
|
|
|
|
|
@ensure_attached
|
|
|
|
def recheck_all(self):
|
2015-08-16 14:22:04 +02:00
|
|
|
self.spell.recheck_all()
|
2009-03-04 14:44:01 +01:00
|
|
|
|
|
|
|
def detach(self):
|
2017-10-19 11:26:22 +02:00
|
|
|
if self.spell is not None:
|
|
|
|
self.spell.detach()
|
|
|
|
|
|
|
|
def attach(self, textview):
|
|
|
|
spell = GtkSpell.Checker.get_from_text_view(textview)
|
|
|
|
if spell is None:
|
|
|
|
print('attached')
|
|
|
|
self.spell.attach(textview)
|
|
|
|
|
2009-03-04 14:44:01 +01:00
|
|
|
|
2016-05-22 23:09:49 +02:00
|
|
|
GObject.type_register(Spell)
|
2009-03-04 14:44:01 +01:00
|
|
|
|
2017-10-19 11:26:22 +02:00
|
|
|
|
|
|
|
def test_language(lang):
|
|
|
|
spell = GtkSpell.Checker.new()
|
|
|
|
try:
|
|
|
|
spell.set_language(lang)
|
|
|
|
except GLib.GError as error:
|
|
|
|
if error.domain == 'gtkspell-error-quark':
|
|
|
|
return False
|
|
|
|
return True
|