bump product version to 6.3.0.0.beta1
[LibreOffice.git] / svl / source / config / asiancfg.cxx
blob27b5b5defc92687e7ca8400976f387524d5e0852
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <cassert>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/container/ElementExistException.hpp>
26 #include <com/sun/star/container/NoSuchElementException.hpp>
27 #include <com/sun/star/container/XNameAccess.hpp>
28 #include <com/sun/star/container/XNameContainer.hpp>
29 #include <com/sun/star/lang/Locale.hpp>
30 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
31 #include <com/sun/star/uno/Any.hxx>
32 #include <com/sun/star/uno/Reference.hxx>
33 #include <com/sun/star/uno/Sequence.hxx>
34 #include <comphelper/configuration.hxx>
35 #include <comphelper/processfactory.hxx>
36 #include <officecfg/Office/Common.hxx>
37 #include <rtl/ustrbuf.hxx>
38 #include <rtl/ustring.h>
39 #include <rtl/ustring.hxx>
40 #include <sal/log.hxx>
41 #include <sal/types.h>
42 #include <i18nlangtag/languagetag.hxx>
43 #include <svl/asiancfg.hxx>
45 namespace {
47 OUString toString(css::lang::Locale const & locale) {
48 SAL_WARN_IF( locale.Language.indexOf('-') != -1, "svl",
49 "Locale language \"" << locale.Language << "\" contains \"-\"");
50 SAL_WARN_IF( locale.Country.indexOf('-') != -1, "svl",
51 "Locale country \"" << locale.Country << "\" contains \"-\"");
52 return LanguageTag::convertToBcp47( locale, false);
57 struct SvxAsianConfig::Impl {
58 Impl():
59 context(comphelper::getProcessComponentContext()),
60 batch(comphelper::ConfigurationChanges::create(context))
63 Impl(const Impl&) = delete;
64 Impl& operator=(const Impl&) = delete;
66 css::uno::Reference< css::uno::XComponentContext > context;
68 std::shared_ptr< comphelper::ConfigurationChanges > batch;
71 SvxAsianConfig::SvxAsianConfig(): impl_(new Impl) {}
73 SvxAsianConfig::~SvxAsianConfig() {}
75 void SvxAsianConfig::Commit() {
76 impl_->batch->commit();
79 bool SvxAsianConfig::IsKerningWesternTextOnly() const {
80 return
81 officecfg::Office::Common::AsianLayout::IsKerningWesternTextOnly::get(
82 impl_->context);
85 void SvxAsianConfig::SetKerningWesternTextOnly(bool value) {
86 officecfg::Office::Common::AsianLayout::IsKerningWesternTextOnly::set(
87 value, impl_->batch);
90 CharCompressType SvxAsianConfig::GetCharDistanceCompression() const {
91 return static_cast<CharCompressType>(officecfg::Office::Common::AsianLayout::CompressCharacterDistance::get(
92 impl_->context));
95 void SvxAsianConfig::SetCharDistanceCompression(CharCompressType value) {
96 officecfg::Office::Common::AsianLayout::CompressCharacterDistance::set(
97 static_cast<sal_uInt16>(value), impl_->batch);
100 css::uno::Sequence< css::lang::Locale > SvxAsianConfig::GetStartEndCharLocales()
101 const
103 css::uno::Sequence< OUString > ns(
104 officecfg::Office::Common::AsianLayout::StartEndCharacters::get(
105 impl_->context)->
106 getElementNames());
107 css::uno::Sequence< css::lang::Locale > ls(ns.getLength());
108 for (sal_Int32 i = 0; i < ns.getLength(); ++i) {
109 ls[i] = LanguageTag::convertToLocale( ns[i], false);
111 return ls;
114 bool SvxAsianConfig::GetStartEndChars(
115 css::lang::Locale const & locale, OUString & startChars,
116 OUString & endChars) const
118 css::uno::Reference< css::container::XNameAccess > set(
119 officecfg::Office::Common::AsianLayout::StartEndCharacters::get(
120 impl_->context));
121 css::uno::Any v;
122 try {
123 v = set->getByName(toString(locale));
124 } catch (css::container::NoSuchElementException &) {
125 return false;
127 css::uno::Reference< css::beans::XPropertySet > el(
128 v.get< css::uno::Reference< css::beans::XPropertySet > >(),
129 css::uno::UNO_SET_THROW);
130 startChars = el->getPropertyValue("StartCharacters").get< OUString >();
131 endChars = el->getPropertyValue("EndCharacters").get< OUString >();
132 return true;
135 void SvxAsianConfig::SetStartEndChars(
136 css::lang::Locale const & locale, OUString const * startChars,
137 OUString const * endChars)
139 assert((startChars == nullptr) == (endChars == nullptr));
140 css::uno::Reference< css::container::XNameContainer > set(
141 officecfg::Office::Common::AsianLayout::StartEndCharacters::get(
142 impl_->batch));
143 OUString name(toString(locale));
144 if (startChars == nullptr) {
145 try {
146 set->removeByName(name);
147 } catch (css::container::NoSuchElementException &) {}
148 } else {
149 bool found;
150 css::uno::Any v;
151 try {
152 v = set->getByName(name);
153 found = true;
154 } catch (css::container::NoSuchElementException &) {
155 found = false;
157 if (found) {
158 css::uno::Reference< css::beans::XPropertySet > el(
159 v.get< css::uno::Reference< css::beans::XPropertySet > >(),
160 css::uno::UNO_SET_THROW);
161 el->setPropertyValue("StartCharacters", css::uno::makeAny(*startChars));
162 el->setPropertyValue("EndCharacters", css::uno::makeAny(*endChars));
163 } else {
164 css::uno::Reference< css::beans::XPropertySet > el(
165 (css::uno::Reference< css::lang::XSingleServiceFactory >(
166 set, css::uno::UNO_QUERY_THROW)->
167 createInstance()),
168 css::uno::UNO_QUERY_THROW);
169 el->setPropertyValue("StartCharacters", css::uno::makeAny(*startChars));
170 el->setPropertyValue("EndCharacters", css::uno::makeAny(*endChars));
171 css::uno::Any v2(css::uno::makeAny(el));
172 try {
173 set->insertByName(name, v2);
174 } catch (css::container::ElementExistException &) {
175 SAL_INFO("svl", "Concurrent update race for \"" << name << '"');
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */