ls-dbus-backend 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/python3
  2. import dbus
  3. import dbus.mainloop.glib
  4. dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
  5. import dbus.service
  6. from gi.repository import GObject
  7. import logging
  8. import re
  9. import subprocess
  10. import sys
  11. import LanguageSelector.macros
  12. from LanguageSelector.utils import *
  13. DATADIR="/usr/share/language-selector/"
  14. class LanguageSelectorServer(dbus.service.Object):
  15. def __init__(self, bus=dbus.SystemBus(), datadir=DATADIR):
  16. bus_name = dbus.service.BusName('com.ubuntu.LanguageSelector',
  17. bus=bus)
  18. dbus.service.Object.__init__(self, bus_name, '/')
  19. self._datadir = "/usr/share/language-selector/"
  20. self._re_locale = re.compile(r'^[\w.@:-]+$')
  21. def _authWithPolicyKit(self, sender, connection, priv):
  22. logging.debug("_authWithPolicyKit")
  23. system_bus = dbus.SystemBus()
  24. obj = system_bus.get_object("org.freedesktop.PolicyKit1",
  25. "/org/freedesktop/PolicyKit1/Authority",
  26. "org.freedesktop.PolicyKit1.Authority")
  27. policykit = dbus.Interface(obj, "org.freedesktop.PolicyKit1.Authority")
  28. subject = ('system-bus-name',
  29. { 'name': dbus.String(sender, variant_level = 1) }
  30. )
  31. details = { '' : '' }
  32. flags = dbus.UInt32(1) # AllowUserInteraction = 0x00000001
  33. cancel_id = ''
  34. (ok, notused, details) = policykit.CheckAuthorization(
  35. subject, priv, details, flags, cancel_id)
  36. return ok
  37. @dbus.service.method(dbus_interface='com.ubuntu.LanguageSelector',
  38. in_signature="s",
  39. out_signature="b",
  40. connection_keyword='connection',
  41. sender_keyword='sender')
  42. def SetSystemDefaultLanguageEnv(self, sysLanguage, sender, connection):
  43. """ sysLanguage - the default system LANGUAGE and LANG """
  44. logging.debug("SetSystemDefaultLanguage")
  45. if not self._re_locale.search(sysLanguage):
  46. logging.error('SetSystemDefaultLanguage: Invalid locale "%s", rejecting', sysLanguage)
  47. return False
  48. if not self._authWithPolicyKit(sender, connection, "com.ubuntu.languageselector.setsystemdefaultlanguage"):
  49. return False
  50. conffiles = ["/etc/default/locale"]
  51. findString = "LANGUAGE="
  52. setString = "LANGUAGE=\"%s\"\n" % sysLanguage
  53. find_string_and_replace(findString, setString, conffiles)
  54. defaultLanguageLocale = language2locale(sysLanguage)
  55. findString = "LANG="
  56. setString = "LANG=\"%s\"\n" % defaultLanguageLocale
  57. find_string_and_replace(findString, setString, conffiles)
  58. return True
  59. @dbus.service.method(dbus_interface='com.ubuntu.LanguageSelector',
  60. in_signature="s",
  61. out_signature="b",
  62. connection_keyword='connection',
  63. sender_keyword='sender')
  64. def SetSystemDefaultFormatsEnv(self, sysFormats, sender, connection):
  65. """ sysFormats: various LC_* variables (de_DE.UTF-8) """
  66. logging.debug("SetSystemDefaultFormatsEnv")
  67. if not self._re_locale.search(sysFormats):
  68. logging.error('SetSystemDefaultFormatsEnv: Invalid locale "%s", rejecting', sysFormats)
  69. return False
  70. if not self._authWithPolicyKit(sender, connection, "com.ubuntu.languageselector.setsystemdefaultlanguage"):
  71. return False
  72. conffiles = ["/etc/default/locale"]
  73. macr = LanguageSelector.macros.LangpackMacros(self._datadir, sysFormats)
  74. defaultFormatsLocale = macr['SYSLOCALE']
  75. for var in 'LC_NUMERIC', 'LC_TIME', 'LC_MONETARY', 'LC_PAPER', 'LC_IDENTIFICATION', \
  76. 'LC_NAME', 'LC_ADDRESS', 'LC_TELEPHONE', 'LC_MEASUREMENT':
  77. findString = "%s=" % var
  78. setString = "%s=\"%s\"\n" % (var, defaultFormatsLocale)
  79. find_string_and_replace(findString, setString, conffiles)
  80. """ /etc/papersize ('a4' or 'letter') """
  81. papersize = subprocess.check_output(
  82. ['/usr/share/language-tools/locale2papersize', defaultFormatsLocale],
  83. universal_newlines=True)
  84. with open('/etc/papersize', 'w') as f:
  85. f.write(papersize)
  86. return True
  87. if __name__ == "__main__":
  88. # FIXME: use argparse
  89. if len(sys.argv) > 1 and sys.argv[1] == "--debug":
  90. logging.basicConfig(level=logging.DEBUG)
  91. server = LanguageSelectorServer()
  92. GObject.MainLoop().run()