/usr/lib/libreoffice/share/extensions/voikko/pythonpath/PropertyManager.py is in libreoffice-voikko 5.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | # Libreoffice-voikko: Linguistic extension for LibreOffice
# Copyright (C) 2015 Harri Pitkänen <hatapitk@iki.fi>
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.
#
# Alternatively, the contents of this file may be used under the terms of
# the GNU General Public License Version 3 or later (the "GPL"), in which
# case the provisions of the GPL are applicable instead of those above.
import logging
import unohelper
import os
import sys
import locale
import uno
from VoikkoHandlePool import VoikkoHandlePool
from com.sun.star.beans import XPropertyChangeListener, UnknownPropertyException, PropertyValue
from com.sun.star.linguistic2 import LinguServiceEvent
from com.sun.star.linguistic2.LinguServiceEventFlags import SPELL_CORRECT_WORDS_AGAIN, SPELL_WRONG_WORDS_AGAIN, HYPHENATE_AGAIN, PROOFREAD_AGAIN
class PropertyManager(unohelper.Base, XPropertyChangeListener):
def __init__(self):
self.__messageLanguage = "en_US"
VoikkoHandlePool.getInstance().setInstallationPath(self.__getInstallationPath())
logging.debug("PropertyManager.__init__")
self.__linguPropSet = None
self.__hyphMinLeading = 2
self.__hyphMinTrailing = 2
self.__hyphMinWordLength = 5
self.__hyphWordParts = False
self.__hyphUnknownWords = True
self.__linguEventListeners = {}
try:
dictVariant = self.readFromRegistry("/org.puimula.ooovoikko.Config/dictionary", "variant")
VoikkoHandlePool.getInstance().setPreferredGlobalVariant(dictVariant)
logging.debug("Initial dictionary variant '" + dictVariant + "'")
except UnknownPropertyException as e:
logging.debug("Setting initial dictionary variant to default")
VoikkoHandlePool.getInstance().setPreferredGlobalVariant("")
self.initialize()
def propertyChange(self, evt):
logging.debug("PropertyManager.propertyChange")
self.__setProperties(self.__linguPropSet)
event = LinguServiceEvent()
event.nEvent = SPELL_CORRECT_WORDS_AGAIN | SPELL_WRONG_WORDS_AGAIN | HYPHENATE_AGAIN | PROOFREAD_AGAIN
self.__sendLinguEvent(event);
def __setUiLanguage(self):
try:
lang = self.readFromRegistry("org.openoffice.Office.Linguistic/General", "UILocale")
logging.debug("Specified UI locale = '" + lang + "'")
if lang is not None and len(lang) > 0:
self.__messageLanguage = lang
else:
# Use system default language
lang = locale.getdefaultlocale()[0]
if lang is not None:
logging.debug("Locale language = '" + lang + "'")
self.__messageLanguage = lang
except UnknownPropertyException:
logging.error("PropertyManager.initialize caught UnknownPropertyException")
def initialize(self):
logging.debug("PropertyManager.initialize: starting")
self.__setUiLanguage()
VoikkoHandlePool.getInstance().setGlobalBooleanOption(PropertyManager.VOIKKO_OPT_IGNORE_DOT, True)
VoikkoHandlePool.getInstance().setGlobalBooleanOption(PropertyManager.VOIKKO_OPT_NO_UGLY_HYPHENATION, True)
# Set these options globally until OOo bug #97945 is resolved.
VoikkoHandlePool.getInstance().setGlobalBooleanOption(PropertyManager.VOIKKO_OPT_ACCEPT_TITLES_IN_GC, True)
VoikkoHandlePool.getInstance().setGlobalBooleanOption(PropertyManager.VOIKKO_OPT_ACCEPT_BULLETED_LISTS_IN_GC, True)
VoikkoHandlePool.getInstance().setGlobalBooleanOption(PropertyManager.VOIKKO_OPT_ACCEPT_UNFINISHED_PARAGRAPHS_IN_GC, True)
compContext = uno.getComponentContext()
servManager = compContext.ServiceManager
self.__linguPropSet = servManager.createInstanceWithContext("com.sun.star.linguistic2.LinguProperties", compContext)
self.__linguPropSet.addPropertyChangeListener("IsSpellWithDigits", self)
self.__linguPropSet.addPropertyChangeListener("IsSpellUpperCase", self)
logging.debug("PropertyManager.initialize: property manager initalized")
# synchronize the local settings from global preferences
self.__setProperties(self.__linguPropSet)
self.readVoikkoSettings()
# request that all users of linguistic services run the spellchecker and hyphenator
# again with updated settings
event = LinguServiceEvent()
event.nEvent = SPELL_CORRECT_WORDS_AGAIN | SPELL_WRONG_WORDS_AGAIN | HYPHENATE_AGAIN | PROOFREAD_AGAIN
self.__sendLinguEvent(event)
def getHyphMinLeading(self):
return self.__hyphMinLeading
def getHyphMinTrailing(self):
return self.__hyphMinTrailing
def getHyphMinWordLength(self):
return self.__hyphMinWordLength
def addLinguServiceEventListener(self, xLstnr):
logging.debug("PropertyManager.addLinguServiceEventListener")
if id(xLstnr) in self.__linguEventListeners:
return False
self.__linguEventListeners[id(xLstnr)] = xLstnr
return True
def removeLinguServiceEventListener(self, xLstnr):
logging.debug("PropertyManager.removeLinguServiceEventListener")
if id(xLstnr) in self.__linguEventListeners:
del self.__linguEventListeners[id(xLstnr)]
return True
return False
def readVoikkoSettings(self):
try:
self.__hyphWordParts = self.readFromRegistry("/org.puimula.ooovoikko.Config/hyphenator", "hyphWordParts")
self.__hyphUnknownWords = self.readFromRegistry("/org.puimula.ooovoikko.Config/hyphenator", "hyphUnknownWords")
except UnknownPropertyException as e:
logging.exception("PropertyManager.readVoikkoSettings", e)
self.__syncHyphenatorSettings()
def __getInstallationPath(self):
dname = os.path.dirname(sys.modules[__name__].__file__)
if dname.endswith("pythonpath"):
dname = dname[:-11]
logging.debug("PropertyManager.getInstallationPath: '" + dname + "'")
return dname
else:
logging.debug("PropertyManager.getInstallationPath: not using unexpect '" + dname + "'")
return None
def readFromRegistry(self, group, key):
rootView = PropertyManager.getRegistryProperties(group)
if rootView is None:
logging.error("PropertyManager.readFromRegistry: failed to obtain rootView " + group)
raise UnknownPropertyException()
return rootView.getHierarchicalPropertyValue(key)
def getMessageLanguage(self):
return self.__messageLanguage
def reloadVoikkoSettings(self):
voikko = VoikkoHandlePool.getInstance()
event = LinguServiceEvent()
event.nEvent = 0
try:
hyphWordParts = self.readFromRegistry("/org.puimula.ooovoikko.Config/hyphenator", "hyphWordParts")
if hyphWordParts != self.__hyphWordParts:
event.nEvent = event.nEvent | HYPHENATE_AGAIN
self.__hyphWordParts = hyphWordParts
hyphUnknownWords = self.readFromRegistry("/org.puimula.ooovoikko.Config/hyphenator", "hyphUnknownWords")
if hyphUnknownWords != self.__hyphUnknownWords:
event.nEvent = event.nEvent | HYPHENATE_AGAIN
self.__hyphUnknownWords = hyphUnknownWords
dictVariant = self.readFromRegistry("/org.puimula.ooovoikko.Config/dictionary", "variant")
if dictVariant is None or dictVariant == "":
dictVariant = voikko.getPreferredGlobalVariant()
if dictVariant != voikko.getPreferredGlobalVariant():
event.nEvent = event.nEvent | SPELL_CORRECT_WORDS_AGAIN | SPELL_WRONG_WORDS_AGAIN | PROOFREAD_AGAIN
voikko.setPreferredGlobalVariant(dictVariant)
except UnknownPropertyException as e:
logging.exception("PropertyManager.reloadVoikkoSettings", e)
self.__syncHyphenatorSettings()
self.__sendLinguEvent(event)
def __setProperties(self, properties):
for p in ["IsSpellWithDigits", "IsSpellUpperCase", "HyphMinLeading", "HyphMinTrailing", "HyphMinWordLength"]:
pValue = PropertyValue()
pValue.Name = p
pValue.Value = properties.getPropertyValue(p)
self.setValue(pValue)
def setValues(self, values):
for v in values:
self.setValue(v)
def resetValues(self, values):
for v in values:
globalV = PropertyValue()
globalV.Name = v.Name
globalV.Value = self.__linguPropSet.getPropertyValue(v.Name)
self.setValue(globalV)
def setValue(self, value):
if value.Name == "IsSpellWithDigits":
VoikkoHandlePool.getInstance().setGlobalBooleanOption(PropertyManager.VOIKKO_OPT_IGNORE_NUMBERS, not value.Value)
elif value.Name == "IsSpellUpperCase":
VoikkoHandlePool.getInstance().setGlobalBooleanOption(PropertyManager.VOIKKO_OPT_IGNORE_UPPERCASE, not value.Value)
elif value.Name == "HyphMinLeading":
if value.Value is not None:
self.__hyphMinLeading = value.Value
self.__syncHyphenatorSettings()
elif value.Name == "HyphMinTrailing":
if value.Value is not None:
self.__hyphMinTrailing = value.Value
self.__syncHyphenatorSettings()
elif value.Name == "HyphMinWordLength":
if value.Value is not None:
self.__hyphMinWordLength = value.Value
self.__syncHyphenatorSettings()
def __syncHyphenatorSettings(self):
if self.__hyphWordParts:
VoikkoHandlePool.getInstance().setGlobalIntegerOption(PropertyManager.VOIKKO_MIN_HYPHENATED_WORD_LENGTH, self.__hyphMinWordLength)
else:
VoikkoHandlePool.getInstance().setGlobalIntegerOption(PropertyManager.VOIKKO_MIN_HYPHENATED_WORD_LENGTH, 2)
VoikkoHandlePool.getInstance().setGlobalBooleanOption(PropertyManager.VOIKKO_OPT_HYPHENATE_UNKNOWN_WORDS, self.__hyphUnknownWords)
def __sendLinguEvent(self, event):
logging.debug("PropertyManager.sendLinguEvent")
for key, lstnr in self.__linguEventListeners.items():
logging.debug("PropertyManager.sendLinguEvent sending event")
lstnr.processLinguServiceEvent(event)
def getRegistryProperties(group):
logging.debug("PropertyManager.getRegistryProperties: " + group)
compContext = uno.getComponentContext()
servManager = compContext.ServiceManager
provider = servManager.createInstanceWithContext("com.sun.star.configuration.ConfigurationProvider", compContext)
pathArgument = PropertyValue()
pathArgument.Name = "nodepath"
pathArgument.Value = group
aArguments = (pathArgument,)
rootView = provider.createInstanceWithArguments("com.sun.star.configuration.ConfigurationUpdateAccess", aArguments)
return rootView;
getRegistryProperties = staticmethod(getRegistryProperties)
def getInstance():
if PropertyManager.instance is None:
PropertyManager.instance = PropertyManager()
return PropertyManager.instance
getInstance = staticmethod(getInstance)
PropertyManager.instance = None
PropertyManager.VOIKKO_OPT_IGNORE_NUMBERS = 1
PropertyManager.VOIKKO_OPT_IGNORE_UPPERCASE = 3
PropertyManager.VOIKKO_MIN_HYPHENATED_WORD_LENGTH = 9
PropertyManager.VOIKKO_OPT_HYPHENATE_UNKNOWN_WORDS = 15
PropertyManager.VOIKKO_OPT_IGNORE_DOT = 0
PropertyManager.VOIKKO_OPT_NO_UGLY_HYPHENATION = 4
PropertyManager.VOIKKO_OPT_ACCEPT_TITLES_IN_GC = 13
PropertyManager.VOIKKO_OPT_ACCEPT_BULLETED_LISTS_IN_GC = 16
PropertyManager.VOIKKO_OPT_ACCEPT_UNFINISHED_PARAGRAPHS_IN_GC = 14
|