diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
commit | 940b4d1848e8c70ab7642901a68594e8016caffc (patch) | |
tree | eb72f344ee6c3d9b80a7ecc079ea79e9fba8676d /unotools | |
parent | Initial commit. (diff) | |
download | libreoffice-940b4d1848e8c70ab7642901a68594e8016caffc.tar.xz libreoffice-940b4d1848e8c70ab7642901a68594e8016caffc.zip |
Adding upstream version 1:7.0.4.upstream/1%7.0.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
88 files changed, 31517 insertions, 0 deletions
diff --git a/unotools/CppunitTest_unotools_fontdefs.mk b/unotools/CppunitTest_unotools_fontdefs.mk new file mode 100644 index 000000000..5eada2084 --- /dev/null +++ b/unotools/CppunitTest_unotools_fontdefs.mk @@ -0,0 +1,30 @@ +# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*- +# +# This file is part of the LibreOffice project. +# +# 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/. +# + +$(eval $(call gb_CppunitTest_CppunitTest,unotools_fontdefs)) + +$(eval $(call gb_CppunitTest_use_external,unotools_fontdefs,boost_headers)) + +$(eval $(call gb_CppunitTest_use_sdk_api,unotools_fontdefs)) + +$(eval $(call gb_CppunitTest_add_exception_objects,unotools_fontdefs, \ + unotools/qa/unit/testGetEnglishSearchName \ +)) + +$(eval $(call gb_CppunitTest_use_libraries,unotools_fontdefs, \ + comphelper \ + cppu \ + cppuhelper \ + tl \ + sal \ + svt \ + utl \ +)) + +# vim: set noet sw=4 ts=4: diff --git a/unotools/IwyuFilter_unotools.yaml b/unotools/IwyuFilter_unotools.yaml new file mode 100644 index 000000000..74d2033fa --- /dev/null +++ b/unotools/IwyuFilter_unotools.yaml @@ -0,0 +1,44 @@ +--- +assumeFilename: unotools/source/i18n/resmgr.cxx +blacklist: + unotools/inc/unotoolsservices.hxx: + # Needed for extern declaration + - namespace comphelper::service_decl { class ServiceDecl; } } + unotools/source/config/itemholder1.hxx: + # Base class needs complete type + - com/sun/star/lang/XEventListener.hpp + unotools/source/ucbhelper/XTempFile.hxx: + # Base class needs complete type + - com/sun/star/io/XInputStream.hpp + - com/sun/star/io/XOutputStream.hpp + - com/sun/star/io/XTempFile.hpp + - com/sun/star/io/XTruncate.hpp + - com/sun/star/beans/XFastPropertySet.hpp + - com/sun/star/beans/XPropertyAccess.hpp + - com/sun/star/beans/XPropertySet.hpp + unotools/source/config/configpaths.cxx: + # Needed for linker visibility + - unotools/configpaths.hxx + unotools/source/config/lingucfg.cxx: + # Needed for direct member access + - com/sun/star/util/XChangesBatch.hpp + unotools/source/config/useroptions.cxx: + # Needed for direct member access + - com/sun/star/container/XNameAccess.hpp + unotools/source/i18n/resmgr.cxx: + # Boost wrappers, used only here + - boost/locale.hpp + - boost/locale/gnu_gettext.hpp + unotools/source/ucbhelper/tempfile.cxx: + # Needed for non-DBG_UTIL build + - unistd.h + unotools/source/ucbhelper/ucbhelper.cxx: + # Needed for namespaced function declarations + - unotools/ucbhelper.hxx + unotools/source/ucbhelper/ucblockbytes.cxx: + # Needed for direct member access + - com/sun/star/beans/PropertyValue.hpp + unotools/source/ucbhelper/ucbstreamhelper.cxx: + # Needed for direct member access + - com/sun/star/beans/PropertyValue.hpp + diff --git a/unotools/JunitTest_unotools_complex.mk b/unotools/JunitTest_unotools_complex.mk new file mode 100644 index 000000000..a67340493 --- /dev/null +++ b/unotools/JunitTest_unotools_complex.mk @@ -0,0 +1,33 @@ +# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*- +# +# This file is part of the LibreOffice project. +# +# 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/. +# + +$(eval $(call gb_JunitTest_JunitTest,unotools_complex)) + +$(eval $(call gb_JunitTest_use_unoapi_jars,unotools_complex)) + +$(eval $(call gb_JunitTest_use_jars,unotools_complex,\ + test-tools \ +)) + +$(eval $(call gb_JunitTest_set_defs,unotools_complex,\ + $$(DEFS) \ +)) + +$(eval $(call gb_JunitTest_add_classes,unotools_complex,\ + complex.tempfile.TempFileUnitTest \ +)) + +$(eval $(call gb_JunitTest_add_sourcefiles,unotools_complex,\ + unotools/qa/complex/tempfile/TempFileUnitTest \ + unotools/qa/complex/tempfile/Test01 \ + unotools/qa/complex/tempfile/Test02 \ + unotools/qa/complex/tempfile/TestHelper \ +)) + +# vim: set noet sw=4 ts=4: diff --git a/unotools/Library_utl.mk b/unotools/Library_utl.mk new file mode 100644 index 000000000..b7c06cbf4 --- /dev/null +++ b/unotools/Library_utl.mk @@ -0,0 +1,132 @@ +# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*- +# +# This file is part of the LibreOffice project. +# +# 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/. +# + +# declare a library +# utl is the name of the library as it is found in Repository.mk +$(eval $(call gb_Library_Library,utl)) + +$(eval $(call gb_Library_use_externals,utl,\ + boost_headers \ + boost_locale \ +)) + +$(eval $(call gb_Library_use_custom_headers,utl,\ + officecfg/registry \ +)) + +# in case UNO services are exported: declare location of component file +$(eval $(call gb_Library_set_componentfile,utl,unotools/util/utl)) + +$(eval $(call gb_Library_set_include,utl, \ + $$(INCLUDE) \ + -I$(SRCDIR)/unotools/inc \ +)) + +# add any additional definitions to be set for compilation here +# (e.g. -DLIB_DLLIMPLEMENTATION) +$(eval $(call gb_Library_add_defs,utl,\ + -DUNOTOOLS_DLLIMPLEMENTATION \ +)) + +$(eval $(call gb_Library_set_precompiled_header,utl,unotools/inc/pch/precompiled_utl)) + +$(eval $(call gb_Library_use_sdk_api,utl)) + +# add libraries to be linked to utl; again these names need to be given as +# specified in Repository.mk +$(eval $(call gb_Library_use_libraries,utl,\ + comphelper \ + cppu \ + cppuhelper \ + i18nlangtag \ + sal \ + salhelper \ + tl \ + ucbhelper \ +)) + +# add all source files that shall be compiled with exceptions enabled +# the name is relative to $(SRCROOT) and must not contain an extension +$(eval $(call gb_Library_add_exception_objects,utl,\ + unotools/source/accessibility/accessiblerelationsethelper \ + unotools/source/accessibility/accessiblestatesethelper \ + unotools/source/config/bootstrap \ + unotools/source/config/cmdoptions \ + unotools/source/config/compatibility \ + unotools/source/config/compatibilityviewoptions \ + unotools/source/config/configitem \ + unotools/source/config/configmgr \ + unotools/source/config/confignode \ + unotools/source/config/configpaths \ + unotools/source/config/configvaluecontainer \ + unotools/source/config/defaultoptions \ + unotools/source/config/docinfohelper \ + unotools/source/config/dynamicmenuoptions \ + unotools/source/config/eventcfg \ + unotools/source/config/extendedsecurityoptions \ + unotools/source/config/fltrcfg \ + unotools/source/config/fontcfg \ + unotools/source/config/fontoptions \ + unotools/source/config/historyoptions \ + unotools/source/config/itemholder1 \ + unotools/source/config/lingucfg \ + unotools/source/config/misccfg \ + unotools/source/config/moduleoptions \ + unotools/source/config/options \ + unotools/source/config/optionsdlg \ + unotools/source/config/pathoptions \ + unotools/source/config/printwarningoptions \ + unotools/source/config/saveopt \ + unotools/source/config/searchopt \ + unotools/source/config/securityoptions \ + unotools/source/config/syslocaleoptions \ + unotools/source/config/useroptions \ + unotools/source/config/viewoptions \ + unotools/source/i18n/calendarwrapper \ + unotools/source/i18n/caserotate \ + unotools/source/i18n/charclass \ + unotools/source/i18n/collatorwrapper \ + unotools/source/i18n/intlwrapper \ + unotools/source/i18n/localedatawrapper \ + unotools/source/i18n/nativenumberwrapper \ + unotools/source/i18n/readwritemutexguard \ + unotools/source/i18n/textsearch \ + unotools/source/i18n/transliterationwrapper \ + unotools/source/misc/closeveto \ + unotools/source/misc/datetime \ + unotools/source/misc/defaultencoding \ + unotools/source/misc/desktopterminationobserver \ + unotools/source/misc/eventlisteneradapter \ + unotools/source/misc/fontcvt \ + unotools/source/misc/fontdefs \ + unotools/source/misc/mediadescriptor \ + unotools/source/misc/sharedunocomponent \ + unotools/source/misc/syslocale \ + unotools/source/misc/unotoolsservices \ + unotools/source/misc/wincodepage \ + unotools/source/misc/ServiceDocumenter \ + unotools/source/misc/ZipPackageHelper \ + unotools/source/streaming/streamhelper \ + unotools/source/streaming/streamwrap \ + unotools/source/ucbhelper/localfilehelper \ + unotools/source/ucbhelper/progresshandlerwrap \ + unotools/source/ucbhelper/tempfile \ + unotools/source/ucbhelper/ucbhelper \ + unotools/source/ucbhelper/ucblockbytes \ + unotools/source/ucbhelper/ucbstreamhelper \ + unotools/source/ucbhelper/xtempfile \ +)) + +# see the source file +$(eval $(call gb_Library_add_exception_objects,utl,\ + unotools/source/i18n/resmgr, \ + -D_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR= -D_HAS_AUTO_PTR_ETC=1 \ +)) + +# vim: set noet sw=4 ts=4: diff --git a/unotools/Makefile b/unotools/Makefile new file mode 100644 index 000000000..0997e6284 --- /dev/null +++ b/unotools/Makefile @@ -0,0 +1,14 @@ +# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*- +# +# This file is part of the LibreOffice project. +# +# 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/. +# + +module_directory:=$(dir $(realpath $(firstword $(MAKEFILE_LIST)))) + +include $(module_directory)/../solenv/gbuild/partial_build.mk + +# vim: set noet sw=4 ts=4: diff --git a/unotools/Module_unotools.mk b/unotools/Module_unotools.mk new file mode 100644 index 000000000..c8a46e33e --- /dev/null +++ b/unotools/Module_unotools.mk @@ -0,0 +1,23 @@ +# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*- +# +# This file is part of the LibreOffice project. +# +# 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/. +# + +$(eval $(call gb_Module_Module,unotools)) + +$(eval $(call gb_Module_add_targets,unotools,\ + Library_utl \ +)) + +$(eval $(call gb_Module_add_subsequentcheck_targets,unotools,\ + JunitTest_unotools_complex \ +)) +$(eval $(call gb_Module_add_check_targets,unotools,\ + CppunitTest_unotools_fontdefs \ +)) + +# vim: set noet sw=4 ts=4: diff --git a/unotools/README b/unotools/README new file mode 100644 index 000000000..bac492a83 --- /dev/null +++ b/unotools/README @@ -0,0 +1 @@ +Helpers for C++ use of UNO. diff --git a/unotools/inc/pch/precompiled_utl.cxx b/unotools/inc/pch/precompiled_utl.cxx new file mode 100644 index 000000000..41a7f40d9 --- /dev/null +++ b/unotools/inc/pch/precompiled_utl.cxx @@ -0,0 +1,12 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + */ + +#include "precompiled_utl.hxx" + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/inc/pch/precompiled_utl.hxx b/unotools/inc/pch/precompiled_utl.hxx new file mode 100644 index 000000000..56d6decc7 --- /dev/null +++ b/unotools/inc/pch/precompiled_utl.hxx @@ -0,0 +1,148 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + */ + +/* + This file has been autogenerated by update_pch.sh. It is possible to edit it + manually (such as when an include file has been moved/renamed/removed). All such + manual changes will be rewritten by the next run of update_pch.sh (which presumably + also fixes all possible problems, so it's usually better to use it). + + Generated on 2019-10-17 15:17:31 using: + ./bin/update_pch unotools utl --cutoff=3 --exclude:system --exclude:module --include:local + + If after updating build fails, use the following command to locate conflicting headers: + ./bin/update_pch_bisect ./unotools/inc/pch/precompiled_utl.hxx "make unotools.build" --find-conflicts +*/ + +#if PCH_LEVEL >= 1 +#include <algorithm> +#include <cassert> +#include <cstddef> +#include <memory> +#include <ostream> +#include <stddef.h> +#include <type_traits> +#include <unordered_map> +#include <vector> +#include <boost/locale.hpp> +#include <boost/locale/gnu_gettext.hpp> +#include <optional> +#endif // PCH_LEVEL >= 1 +#if PCH_LEVEL >= 2 +#include <osl/detail/file.h> +#include <osl/diagnose.h> +#include <osl/endian.h> +#include <osl/file.hxx> +#include <osl/mutex.hxx> +#include <osl/nlsupport.h> +#include <osl/process.h> +#include <osl/thread.h> +#include <osl/thread.hxx> +#include <osl/time.h> +#include <rtl/alloc.h> +#include <rtl/bootstrap.hxx> +#include <rtl/character.hxx> +#include <rtl/crc.h> +#include <rtl/instance.hxx> +#include <rtl/locale.h> +#include <rtl/math.hxx> +#include <rtl/ref.hxx> +#include <rtl/string.h> +#include <rtl/string.hxx> +#include <rtl/tencinfo.h> +#include <rtl/textenc.h> +#include <rtl/uri.hxx> +#include <rtl/ustrbuf.hxx> +#include <rtl/ustring.h> +#include <rtl/ustring.hxx> +#include <sal/config.h> +#include <sal/log.hxx> +#include <sal/macros.h> +#include <sal/saldllapi.h> +#include <sal/types.h> +#include <sal/typesizes.h> +#include <vcl/errcode.hxx> +#endif // PCH_LEVEL >= 2 +#if PCH_LEVEL >= 3 +#include <com/sun/star/beans/NamedValue.hpp> +#include <com/sun/star/beans/PropertyAttribute.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/configuration/theDefaultProvider.hpp> +#include <com/sun/star/container/XHierarchicalNameAccess.hpp> +#include <com/sun/star/container/XNameAccess.hpp> +#include <com/sun/star/container/XNameContainer.hpp> +#include <com/sun/star/io/BufferSizeExceededException.hpp> +#include <com/sun/star/io/NotConnectedException.hpp> +#include <com/sun/star/io/XActiveDataSink.hpp> +#include <com/sun/star/lang/Locale.hpp> +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/lang/XSingleServiceFactory.hpp> +#include <com/sun/star/task/XInteractionHandler.hpp> +#include <com/sun/star/ucb/CommandAbortedException.hpp> +#include <com/sun/star/ucb/ContentCreationException.hpp> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Reference.h> +#include <com/sun/star/uno/Reference.hxx> +#include <com/sun/star/uno/RuntimeException.hpp> +#include <com/sun/star/uno/Sequence.h> +#include <com/sun/star/uno/Sequence.hxx> +#include <com/sun/star/uno/XInterface.hpp> +#include <com/sun/star/util/Date.hpp> +#include <com/sun/star/util/Time.hpp> +#include <com/sun/star/util/XChangesBatch.hpp> +#include <comphelper/comphelperdllapi.h> +#include <comphelper/configurationhelper.hxx> +#include <comphelper/processfactory.hxx> +#include <comphelper/sequence.hxx> +#include <comphelper/servicedecl.hxx> +#include <cppu/cppudllapi.h> +#include <cppuhelper/cppuhelperdllapi.h> +#include <cppuhelper/implbase.hxx> +#include <i18nlangtag/lang.h> +#include <i18nlangtag/languagetag.hxx> +#include <i18nlangtag/mslangid.hxx> +#include <i18nutil/transliteration.hxx> +#include <o3tl/any.hxx> +#include <o3tl/enumarray.hxx> +#include <o3tl/typed_flags_set.hxx> +#include <o3tl/underlyingenumvalue.hxx> +#include <salhelper/condition.hxx> +#include <tools/date.hxx> +#include <tools/debug.hxx> +#include <tools/diagnose_ex.h> +#include <tools/lineend.hxx> +#include <tools/ref.hxx> +#include <tools/stream.hxx> +#include <tools/time.hxx> +#include <tools/toolsdllapi.h> +#include <tools/urlobj.hxx> +#include <ucbhelper/content.hxx> +#include <ucbhelper/ucbhelperdllapi.h> +#endif // PCH_LEVEL >= 3 +#if PCH_LEVEL >= 4 +#include <unotools/charclass.hxx> +#include <unotools/configitem.hxx> +#include <unotools/configmgr.hxx> +#include <unotools/configpaths.hxx> +#include <unotools/fontdefs.hxx> +#include <unotools/localedatawrapper.hxx> +#include <unotools/options.hxx> +#include <unotools/pathoptions.hxx> +#include <unotools/securityoptions.hxx> +#include <unotools/streamwrap.hxx> +#include <unotools/syslocale.hxx> +#include <unotools/syslocaleoptions.hxx> +#include <unotools/ucbhelper.hxx> +#include <unotools/unotoolsdllapi.h> +#include <unotoolsservices.hxx> +#endif // PCH_LEVEL >= 4 + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/inc/unotoolsservices.hxx b/unotools/inc/unotoolsservices.hxx new file mode 100644 index 000000000..8a99f09e9 --- /dev/null +++ b/unotools/inc/unotoolsservices.hxx @@ -0,0 +1,22 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + */ + +#ifndef INCLUDED_UNOTOOLS_INC_UNOTOOLSSERVICES_HXX +#define INCLUDED_UNOTOOLS_INC_UNOTOOLSSERVICES_HXX + +#include <sal/config.h> + +namespace comphelper::service_decl { class ServiceDecl; } + +extern comphelper::service_decl::ServiceDecl const OTempFileServiceDecl; +extern comphelper::service_decl::ServiceDecl const ServiceDocumenterDecl; + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/unotools/qa/complex/tempfile/TempFileUnitTest.java b/unotools/qa/complex/tempfile/TempFileUnitTest.java new file mode 100644 index 000000000..2d23db0db --- /dev/null +++ b/unotools/qa/complex/tempfile/TempFileUnitTest.java @@ -0,0 +1,91 @@ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +package complex.tempfile; + +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.ucb.XSimpleFileAccess; +import com.sun.star.uno.UnoRuntime; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.openoffice.test.OfficeConnection; +import static org.junit.Assert.*; + +public class TempFileUnitTest +{ + private XMultiServiceFactory m_xMSF = null; + private XSimpleFileAccess m_xSFA = null; + + @Before public void before() { + m_xMSF = getMSF(); + if ( m_xMSF == null ) { + fail ( "Cannot create service factory!" ); + } + try + { + Object oSFA = m_xMSF.createInstance( "com.sun.star.ucb.SimpleFileAccess" ); + m_xSFA = UnoRuntime.queryInterface( XSimpleFileAccess.class, oSFA ); + } + catch ( Exception e ) + { + fail ( "Cannot get simple file access! Exception: " + e); + } + if ( m_xSFA == null ) { + fail ( "Cannot get simple file access!" ); + } + } + + @After public void after() { + m_xMSF = null; + m_xSFA = null; + } + + @Test public void ExecuteTest01() { + Test01 aTest = new Test01( m_xMSF, m_xSFA ); + assertTrue( "Test01 failed!", aTest.test() ); + } + + @Test public void ExecuteTest02() { + Test02 aTest = new Test02( m_xMSF, m_xSFA ); + assertTrue( "Test02 failed!", aTest.test() ); + } + + private XMultiServiceFactory getMSF() + { + return UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); + } + + // setup and close connections + @BeforeClass public static void setUpConnection() throws Exception { + System.out.println("setUpConnection()"); + connection.setUp(); + } + + @AfterClass public static void tearDownConnection() + throws InterruptedException, com.sun.star.uno.Exception + { + System.out.println("tearDownConnection()"); + connection.tearDown(); + } + + private static final OfficeConnection connection = new OfficeConnection(); +} + diff --git a/unotools/qa/complex/tempfile/Test01.java b/unotools/qa/complex/tempfile/Test01.java new file mode 100644 index 000000000..ed8e72985 --- /dev/null +++ b/unotools/qa/complex/tempfile/Test01.java @@ -0,0 +1,103 @@ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +package complex.tempfile; + +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.ucb.XSimpleFileAccess; +import com.sun.star.io.*; +import com.sun.star.uno.UnoRuntime; +import java.util.Random; + +public class Test01 { + private XMultiServiceFactory m_xMSF = null; + private XSimpleFileAccess m_xSFA = null; + private TestHelper m_aTestHelper = null; + + public Test01(XMultiServiceFactory xMSF, XSimpleFileAccess xSFA) { + m_xMSF = xMSF; + m_xSFA = xSFA; + m_aTestHelper = new TestHelper( "Test01: "); + } + + public boolean test() { + XTempFile xTempFile = null; + String sFileURL = null; + String sFileName = null; + //create a temporary file. + try { + Object oTempFile = m_xMSF.createInstance( "com.sun.star.io.TempFile" ); + xTempFile = UnoRuntime.queryInterface(XTempFile.class, oTempFile); + m_aTestHelper.Message( "Tempfile created." ); + UnoRuntime.queryInterface(XTruncate.class, oTempFile); + } catch( Exception e ) { + m_aTestHelper.Error( "Cannot create TempFile. exception: " + e ); + return false; + } + + //retrieve the tempfile URL + if ( xTempFile == null ) { + m_aTestHelper.Error( "Cannot get XTempFile interface." ); + return false; + } + + try { + //compare the file name with the name in the URL. + sFileURL = m_aTestHelper.GetTempFileURL( xTempFile ); + sFileName = m_aTestHelper.GetTempFileName( xTempFile ); + m_aTestHelper.CompareFileNameAndURL( sFileName, sFileURL ); + + //write to the stream using the service. + byte pBytesIn[] = new byte[9]; + byte pBytesOut1[][] = new byte [1][9]; + byte pBytesOut2[][] = new byte [1][9]; + Random oRandom = new Random(); + oRandom.nextBytes( pBytesIn ); + m_aTestHelper.WriteBytesWithStream( pBytesIn, xTempFile ); + + //check the result by reading from the service. + xTempFile.seek(0); + m_aTestHelper.ReadBytesWithStream( pBytesOut1, pBytesIn.length + 1, xTempFile ); + for ( int i = 0; i < pBytesIn.length ; i++ ) { + if ( pBytesOut1[0][i] != pBytesIn[i] ) { + m_aTestHelper.Error( "Tempfile outputs false data!" ); + } + } + + //check the result by reading from the file directly. + m_aTestHelper.ReadDirectlyFromTempFile( pBytesOut2, pBytesIn.length + 1, m_xSFA, sFileURL ); + for ( int i = 0; i < pBytesIn.length; i++ ) { + if ( pBytesOut2[0][i] != pBytesIn[i] ) { + m_aTestHelper.Error( "Tempfile contains false data!" ); + } + } + + //close the object(by closing input and output), check that the file was removed. + xTempFile.setRemoveFile( false ); + m_aTestHelper.CloseTempFile( xTempFile ); + if( !m_aTestHelper.IfTempFileExists( m_xSFA, sFileURL ) ) { + m_aTestHelper.Error( "TempFile mistakenly removed. " ); + } else { + m_aTestHelper.KillTempFile( sFileURL, m_xSFA ); + } + } catch ( Exception e ) { + m_aTestHelper.Error( "Exception: " + e ); + return false; + } + return true; + } +} diff --git a/unotools/qa/complex/tempfile/Test02.java b/unotools/qa/complex/tempfile/Test02.java new file mode 100644 index 000000000..afef403fb --- /dev/null +++ b/unotools/qa/complex/tempfile/Test02.java @@ -0,0 +1,84 @@ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +package complex.tempfile; + + +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.ucb.XSimpleFileAccess; +import com.sun.star.io.*; + +import com.sun.star.uno.UnoRuntime; +import java.util.Random; + +public class Test02 { + + private XMultiServiceFactory m_xMSF; + private XSimpleFileAccess m_xSFA; + private TestHelper m_aTestHelper; + + public Test02(XMultiServiceFactory xMSF, XSimpleFileAccess xSFA) { + m_xMSF = xMSF; + m_xSFA = xSFA; + m_aTestHelper = new TestHelper( "Test02: "); + } + + public boolean test() { + Object oTempFile = null; + XTempFile xTempFile = null; + String sFileURL = null; + //create a temporary file. + try { + oTempFile = m_xMSF.createInstance( "com.sun.star.io.TempFile" ); + xTempFile = UnoRuntime.queryInterface(XTempFile.class, oTempFile); + m_aTestHelper.Message( "Tempfile created." ); + UnoRuntime.queryInterface(XTruncate.class, oTempFile); + } catch(Exception e) { + m_aTestHelper.Error( "Cannot create TempFile. exception: " + e ); + return false; + } + try { + //write something. + byte pBytesIn[] = new byte[9]; + byte pBytesOut[][] = new byte[1][9]; + Random oRandom = new Random(); + oRandom.nextBytes( pBytesIn ); + m_aTestHelper.WriteBytesWithStream( pBytesIn, xTempFile ); + + //get the URL. + sFileURL = m_aTestHelper.GetTempFileURL( xTempFile ); + + //let the service not to remove the URL. + m_aTestHelper.SetTempFileRemove( xTempFile, false ); + + //close the tempfile by closing input and output. + m_aTestHelper.CloseTempFile( xTempFile ); + + //check that the file is still available. + m_aTestHelper.ReadDirectlyFromTempFile( pBytesOut, pBytesIn.length + 1, m_xSFA, sFileURL ); + for ( int i = 0; i < pBytesIn.length; i++ ) { + if ( pBytesOut[0][i] != pBytesIn[i] ) { + m_aTestHelper.Error( "Tempfile contains false data!" ); + } + } + } catch ( Exception e) { + m_aTestHelper.Error("Exception: " + e); + return false; + } + return true; + } +} diff --git a/unotools/qa/complex/tempfile/TestHelper.java b/unotools/qa/complex/tempfile/TestHelper.java new file mode 100644 index 000000000..237c95b9e --- /dev/null +++ b/unotools/qa/complex/tempfile/TestHelper.java @@ -0,0 +1,200 @@ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +package complex.tempfile; + + + +import com.sun.star.io.*; + +import com.sun.star.uno.AnyConverter; +import com.sun.star.ucb.XSimpleFileAccess; + + +public class TestHelper { + + private String m_sTestPrefix; + + public TestHelper( String sTestPrefix ) { + + m_sTestPrefix = sTestPrefix; + } + public void SetTempFileRemove( XTempFile xTempFile, boolean b ) { + try { + xTempFile.setRemoveFile( b ); + } catch( Exception e ) { + Error( "Cannot set TempFileRemove. exception: " + e ); + } + } + + public String GetTempFileURL ( XTempFile xTempFile ) { + String sTempFileURL = null; + try { + sTempFileURL = AnyConverter.toString( xTempFile.getUri() ); + } catch (Exception e) { + Error ( "Cannot get TempFileURL. exception: " + e ); + } + if ( sTempFileURL == null || sTempFileURL.equals("") ) { + Error ( "Temporary file not valid." ); + } + return sTempFileURL; + } + + public String GetTempFileName( XTempFile xTempFile ) { + String sTempFileName = null; + try { + sTempFileName = AnyConverter.toString( xTempFile.getResourceName() ); + } catch ( Exception e ) { + Error( "Cannot get TempFileName. exception: " + e ); + } + if ( sTempFileName == null || sTempFileName.equals("") ) { + Error( "Temporary file not valid." ); + } + return sTempFileName; + } + + public boolean CompareFileNameAndURL ( String sTempFileName, String sTempFileURL ) { + boolean bRet = false; + try { + bRet = sTempFileURL.endsWith( sTempFileName.replaceAll( "\\\\" , "/" ) ); + Message ( "Compare file name and URL: " + + ( bRet ? "OK." : "ERROR: FILE NAME AND URL DO NOT MATCH." ) ); + } + catch ( Exception e ) { + Error ( "exception: " + e); + } + return bRet; + } + + public void WriteBytesWithStream( byte [] pBytes, XTempFile xTempFile ) { + try { + XOutputStream xOutTemp = xTempFile.getOutputStream(); + if ( xOutTemp == null ) { + Error( "Cannot get output stream." ); + } else { + xOutTemp.writeBytes( pBytes ); + Message ( "Write to tempfile successfully." ); + } + } catch ( Exception e ) { + Error( "Cannot write to stream. exception: " + e ); + } + } + + public void ReadBytesWithStream( byte [][] pBytes, int nBytes, XTempFile xTempFile ) { + try { + XInputStream xInTemp = xTempFile.getInputStream(); + if ( xInTemp == null ) { + Error( "Cannot get input stream from tempfile." ); + } else { + xInTemp.readBytes( pBytes, nBytes ); + Message ( "Read from tempfile successfully." ); + } + } catch ( Exception e ) { + Error( "Cannot read from stream. exception: " + e ); + } + } + public void ReadDirectlyFromTempFile( byte [][] pBytes, int nBytes, XSimpleFileAccess xSFA, String sTempFileURL ) + { + try + { + if ( xSFA != null ) { + XInputStream xInTemp = xSFA.openFileRead( sTempFileURL ); + if ( xInTemp != null ) + { + xInTemp.readBytes( pBytes, nBytes ); + xInTemp.closeInput(); + Message ( "Read directly from tempfile successfully." ); + } else { + Error ( "Cannot create input stream from URL." ); + } + } + } + catch ( Exception e) + { + Error( "Exception caught in TestHelper." + + "ReadDirectlyFromTempFile(). exception: " + e ); + } + } + + public void CloseTempFile( XTempFile xTempFile ) { + XOutputStream xOutTemp = null; + XInputStream xInTemp = null; + try { + xOutTemp = xTempFile.getOutputStream(); + if ( xOutTemp == null ) { + Error( "Cannot get output stream." ); + } + } catch ( Exception e ) { + Error( "Cannot get output stream. exception:" + e ); + } + try { + xOutTemp.closeOutput(); + } catch( Exception e ) { + Error( "Cannot close output stream. exception:" + e ); + } + try { + xInTemp = xTempFile.getInputStream(); + if ( xInTemp == null ) { + Error( "Cannot get input stream." ); + } + } catch ( Exception e ) { + Error( "Cannot get input stream. exception:" + e ); + } + try { + xInTemp.closeInput(); + Message ( "Tempfile closed successfully." ); + } catch( Exception e ) { + Error( "Cannot close input stream. exception:" + e ); + } + } + + public void KillTempFile ( String sTempFileURL, XSimpleFileAccess xSFA ) { + try { + if ( sTempFileURL != null && xSFA != null ) { + xSFA.kill( sTempFileURL ); + Message ( "Tempfile killed successfully." ); + } + } + catch ( Exception e ) { + Error ( "Exception caught in TestHelper." + + "KillTempFile(): " + e); + } + } + + public boolean IfTempFileExists( XSimpleFileAccess xSFA, String sTempFileURL ) { + boolean bRet = false; + try { + if ( sTempFileURL != null && xSFA != null ) { + bRet = xSFA.exists( sTempFileURL ); + Message ( "Tempfile " + ( bRet ? "still " : "no longer " ) + "exists." ); + } + } + catch( Exception e ) { + Error( "Exception caught in TestHelper." + + "IfTempFileExists(): " + e ); + } + return bRet; + } + + public void Error( String sError ) { + System.out.println( m_sTestPrefix + "Error: " + sError ); + } + + public void Message( String sMessage ) { + System.out.println( m_sTestPrefix + sMessage ); + } +} diff --git a/unotools/qa/unit/testGetEnglishSearchName.cxx b/unotools/qa/unit/testGetEnglishSearchName.cxx new file mode 100644 index 000000000..453418705 --- /dev/null +++ b/unotools/qa/unit/testGetEnglishSearchName.cxx @@ -0,0 +1,72 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + */ + +#include <cppunit/TestFixture.h> +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/plugin/TestPlugIn.h> +#include <unotools/fontdefs.hxx> + +class Test: public CppUnit::TestFixture +{ +public: + void testSingleElement(); + + CPPUNIT_TEST_SUITE(Test); + CPPUNIT_TEST(testSingleElement); + + CPPUNIT_TEST_SUITE_END(); +}; + +void Test::testSingleElement() +{ + // lowercase + OUString test1 = GetEnglishSearchFontName( "SYMBOL" ); + CPPUNIT_ASSERT_EQUAL( OUString("symbol"),test1); + //trailing whitespaces + test1 = GetEnglishSearchFontName( "Symbol " ); + CPPUNIT_ASSERT_EQUAL(OUString("symbol"),test1); + //no longer remove script suffixes + test1 = GetEnglishSearchFontName( "Symbol(SIP)" ); + CPPUNIT_ASSERT_EQUAL(OUString("symbol(sip)"),test1); + test1 = GetEnglishSearchFontName( "CM Roman CE" ); + CPPUNIT_ASSERT_EQUAL( OUString("cmromance"),test1); + //remove special characters; leave semicolon, numbers + test1 = GetEnglishSearchFontName( "sy;mb?=ol129" ); + CPPUNIT_ASSERT_EQUAL( OUString("sy;mbol129"),test1); + + //transformation + + //for Japanese fontname + // IPAMincho + sal_Unicode const aIPAMincho[]={'i','p','a', 0x660e, 0x671d,0}; + OUString test_ja_JP1 = GetEnglishSearchFontName(aIPAMincho); + CPPUNIT_ASSERT_EQUAL( OUString("ipamincho"),test_ja_JP1); + // IPAGothic + sal_Unicode const aIPAGothic[]={'i','p','a', 0x30b4, 0x30b7, 0x30c3, 0x30af,0}; + OUString test_ja_JP2 = GetEnglishSearchFontName(aIPAGothic); + CPPUNIT_ASSERT_EQUAL( OUString("ipagothic"),test_ja_JP2); + // HiraginoKakuGothic + sal_Unicode const aHiraginoKakuGothic[]={0x30D2, 0x30E9, 0x30AE, 0x30CE, 0x89D2, 0x30B4, 0x30B7, 0x30C3, 0x30AF,0}; + OUString test_ja_JP3 = GetEnglishSearchFontName(aHiraginoKakuGothic); + CPPUNIT_ASSERT_EQUAL( OUString("hiraginosans"),test_ja_JP3); + // HiraginoMincho Pro N + sal_Unicode const aHiraginoMinchoProN[]={0x30D2, 0x30E9, 0x30AE, 0x30CE, 0x660E, 0x671D, 'p','r','o','n',0}; + OUString test_ja_JP4 = GetEnglishSearchFontName(aHiraginoMinchoProN); + CPPUNIT_ASSERT_EQUAL( OUString("hiraginominchopron"),test_ja_JP4); + // HiraginoMaruGothic Pro N + sal_Unicode const aHiraginoMaruGothicProN[]={ 0x30D2, 0x30E9, 0x30AE, 0x30CE, 0x4E38, 0x30B4, 'p','r','o','n',0}; + OUString test_ja_JP5 = GetEnglishSearchFontName(aHiraginoMaruGothicProN); + CPPUNIT_ASSERT_EQUAL( OUString("hiraginomarugothicpron"),test_ja_JP5); +} + +CPPUNIT_TEST_SUITE_REGISTRATION(Test); +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/accessibility/accessiblerelationsethelper.cxx b/unotools/source/accessibility/accessiblerelationsethelper.cxx new file mode 100644 index 000000000..8f7062343 --- /dev/null +++ b/unotools/source/accessibility/accessiblerelationsethelper.cxx @@ -0,0 +1,226 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <com/sun/star/lang/IndexOutOfBoundsException.hpp> +#include <o3tl/safeint.hxx> +#include <unotools/accessiblerelationsethelper.hxx> +#include <vector> +#include <comphelper/sequence.hxx> + +using namespace ::utl; +using namespace ::com::sun::star; +using namespace ::com::sun::star::accessibility; + +class AccessibleRelationSetHelperImpl +{ +public: + AccessibleRelationSetHelperImpl(); + AccessibleRelationSetHelperImpl(const AccessibleRelationSetHelperImpl& rImpl); + + /// @throws uno::RuntimeException + sal_Int32 getRelationCount() const; + /// @throws lang::IndexOutOfBoundsException + /// @throws uno::RuntimeException + AccessibleRelation const & getRelation( sal_Int32 nIndex ) const; + /// @throws uno::RuntimeException + bool containsRelation( sal_Int16 aRelationType ) const; + /// @throws uno::RuntimeException + AccessibleRelation getRelationByType( sal_Int16 aRelationType ) const; + /// @throws uno::RuntimeException + void AddRelation(const AccessibleRelation& rRelation); + +private: + std::vector<AccessibleRelation> maRelations; +}; + +AccessibleRelationSetHelperImpl::AccessibleRelationSetHelperImpl() +{ +} + +AccessibleRelationSetHelperImpl::AccessibleRelationSetHelperImpl(const AccessibleRelationSetHelperImpl& rImpl) + : maRelations(rImpl.maRelations) +{ +} + +sal_Int32 AccessibleRelationSetHelperImpl::getRelationCount() const +{ + return maRelations.size(); +} + +AccessibleRelation const & AccessibleRelationSetHelperImpl::getRelation( sal_Int32 nIndex ) const +{ + if ((nIndex < 0) || (o3tl::make_unsigned(nIndex) >= maRelations.size())) + throw lang::IndexOutOfBoundsException(); + return maRelations[nIndex]; +} + +bool AccessibleRelationSetHelperImpl::containsRelation( sal_Int16 aRelationType ) const +{ + AccessibleRelation defaultRelation; // default is INVALID + AccessibleRelation relationByType = getRelationByType(aRelationType); + return relationByType.RelationType != defaultRelation.RelationType; +} + +AccessibleRelation AccessibleRelationSetHelperImpl::getRelationByType( sal_Int16 aRelationType ) const +{ + sal_Int32 nCount(getRelationCount()); + sal_Int32 i(0); + while (i < nCount) + { + if (maRelations[i].RelationType == aRelationType) + return maRelations[i]; + i++; + } + return AccessibleRelation(); +} + +void AccessibleRelationSetHelperImpl::AddRelation(const AccessibleRelation& rRelation) +{ + sal_Int32 nCount(getRelationCount()); + sal_Int32 i(0); + bool bFound(false); + while ((i < nCount) && !bFound) + { + if (maRelations[i].RelationType == rRelation.RelationType) + bFound = true; + else + i++; + } + if (bFound) + maRelations[i].TargetSet = comphelper::concatSequences(maRelations[i].TargetSet, rRelation.TargetSet); + else + maRelations.push_back(rRelation); +} + +//===== internal ============================================================ + +AccessibleRelationSetHelper::AccessibleRelationSetHelper () + : mpHelperImpl(new AccessibleRelationSetHelperImpl) +{ +} + +AccessibleRelationSetHelper::AccessibleRelationSetHelper (const AccessibleRelationSetHelper& rHelper) + : cppu::WeakImplHelper<XAccessibleRelationSet>(rHelper) +{ + if (rHelper.mpHelperImpl) + mpHelperImpl.reset(new AccessibleRelationSetHelperImpl(*rHelper.mpHelperImpl)); + else + mpHelperImpl.reset(new AccessibleRelationSetHelperImpl()); +} + +AccessibleRelationSetHelper::~AccessibleRelationSetHelper() +{ +} + +//===== XAccessibleRelationSet ============================================== + + /** Returns the number of relations in this relation set. + + @return + Returns the number of relations or zero if there are none. + */ +sal_Int32 SAL_CALL + AccessibleRelationSetHelper::getRelationCount( ) +{ + osl::MutexGuard aGuard (maMutex); + return mpHelperImpl->getRelationCount(); +} + + /** Returns the relation of this relation set that is specified by + the given index. + + @param nIndex + This index specifies the relatio to return. + + @return + For a valid index, i.e. inside the range 0 to the number of + relations minus one, the returned value is the requested + relation. If the index is invalid then the returned relation + has the type INVALID. + + */ + AccessibleRelation SAL_CALL + AccessibleRelationSetHelper::getRelation( sal_Int32 nIndex ) +{ + osl::MutexGuard aGuard (maMutex); + return mpHelperImpl->getRelation(nIndex); +} + + /** Tests whether the relation set contains a relation matching the + specified key. + + @param aRelationType + The type of relation to look for in this set of relations. This + has to be one of the constants of + <type>AccessibleRelationType</type>. + + @return + Returns <TRUE/> if there is a (at least one) relation of the + given type and <FALSE/> if there is no such relation in the set. + */ +sal_Bool SAL_CALL + AccessibleRelationSetHelper::containsRelation( sal_Int16 aRelationType ) +{ + osl::MutexGuard aGuard (maMutex); + return mpHelperImpl->containsRelation(aRelationType); +} + + /** Retrieve and return the relation with the given relation type. + + @param aRelationType + The type of the relation to return. This has to be one of the + constants of <type>AccessibleRelationType</type>. + + @return + If a relation with the given type could be found than (a copy + of) this relation is returned. Otherwise a relation with the + type INVALID is returned. + */ +AccessibleRelation SAL_CALL + AccessibleRelationSetHelper::getRelationByType( sal_Int16 aRelationType ) +{ + osl::MutexGuard aGuard (maMutex); + return mpHelperImpl->getRelationByType(aRelationType); +} + +void AccessibleRelationSetHelper::AddRelation(const AccessibleRelation& rRelation) +{ + osl::MutexGuard aGuard (maMutex); + mpHelperImpl->AddRelation(rRelation); +} + +//===== XTypeProvider ======================================================= + +uno::Sequence< css::uno::Type> AccessibleRelationSetHelper::getTypes() +{ + static const uno::Sequence< css::uno::Type> aTypes { + cppu::UnoType<XAccessibleRelationSet>::get(), + cppu::UnoType<lang::XTypeProvider>::get() + }; + return aTypes; +} + +uno::Sequence<sal_Int8> SAL_CALL AccessibleRelationSetHelper::getImplementationId() +{ + return css::uno::Sequence<sal_Int8>(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/accessibility/accessiblestatesethelper.cxx b/unotools/source/accessibility/accessiblestatesethelper.cxx new file mode 100644 index 000000000..9a180a467 --- /dev/null +++ b/unotools/source/accessibility/accessiblestatesethelper.cxx @@ -0,0 +1,229 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unotools/accessiblestatesethelper.hxx> +#include <tools/debug.hxx> + +// defines how many states the bitfield can contain +// it has the size of 64 because I use a uInt64 +#define BITFIELDSIZE 64 + +using namespace ::utl; +using namespace ::com::sun::star; +using namespace ::com::sun::star::accessibility; + +class AccessibleStateSetHelperImpl +{ +public: + AccessibleStateSetHelperImpl(); + AccessibleStateSetHelperImpl(const AccessibleStateSetHelperImpl& rImpl); + + /// @throws uno::RuntimeException + bool IsEmpty () const; + /// @throws uno::RuntimeException + bool Contains (sal_Int16 aState) const; + /// @throws uno::RuntimeException + uno::Sequence<sal_Int16> GetStates() const; + /// @throws uno::RuntimeException + void AddState(sal_Int16 aState); + /// @throws uno::RuntimeException + void RemoveState(sal_Int16 aState); + + inline void AddStates( const sal_Int64 _nStates ); + +private: + sal_uInt64 maStates; +}; + +AccessibleStateSetHelperImpl::AccessibleStateSetHelperImpl() + : maStates(0) +{ +} + +AccessibleStateSetHelperImpl::AccessibleStateSetHelperImpl(const AccessibleStateSetHelperImpl& rImpl) + : maStates(rImpl.maStates) +{ +} + +inline bool AccessibleStateSetHelperImpl::IsEmpty () const +{ + return maStates == 0; +} + +inline bool AccessibleStateSetHelperImpl::Contains (sal_Int16 aState) const +{ + DBG_ASSERT(aState < BITFIELDSIZE, "the statesset is too small"); + sal_uInt64 aTempBitSet(1); + aTempBitSet <<= aState; + return ((aTempBitSet & maStates) != 0); +} + +inline uno::Sequence<sal_Int16> AccessibleStateSetHelperImpl::GetStates() const +{ + uno::Sequence<sal_Int16> aRet(BITFIELDSIZE); + sal_Int16* pSeq = aRet.getArray(); + sal_Int16 nStateCount(0); + for (sal_Int16 i = 0; i < BITFIELDSIZE; ++i) + if (Contains(i)) + { + *pSeq = i; + ++pSeq; + ++nStateCount; + } + aRet.realloc(nStateCount); + return aRet; +} + +inline void AccessibleStateSetHelperImpl::AddStates( const sal_Int64 _nStates ) +{ + maStates |= _nStates; +} + +inline void AccessibleStateSetHelperImpl::AddState(sal_Int16 aState) +{ + DBG_ASSERT(aState < BITFIELDSIZE, "the statesset is too small"); + sal_uInt64 aTempBitSet(1); + aTempBitSet <<= aState; + maStates |= aTempBitSet; +} + +inline void AccessibleStateSetHelperImpl::RemoveState(sal_Int16 aState) +{ + DBG_ASSERT(aState < BITFIELDSIZE, "the statesset is too small"); + sal_uInt64 aTempBitSet(1); + aTempBitSet <<= aState; + aTempBitSet = ~aTempBitSet; + maStates &= aTempBitSet; +} + +//===== internal ============================================================ + +AccessibleStateSetHelper::AccessibleStateSetHelper () + : mpHelperImpl(new AccessibleStateSetHelperImpl) +{ +} + +AccessibleStateSetHelper::AccessibleStateSetHelper ( const sal_Int64 _nInitialStates ) + : mpHelperImpl(new AccessibleStateSetHelperImpl) +{ + mpHelperImpl->AddStates( _nInitialStates ); +} + +AccessibleStateSetHelper::AccessibleStateSetHelper (const AccessibleStateSetHelper& rHelper) + : cppu::WeakImplHelper<XAccessibleStateSet>(rHelper) +{ + if (rHelper.mpHelperImpl) + mpHelperImpl.reset(new AccessibleStateSetHelperImpl(*rHelper.mpHelperImpl)); + else + mpHelperImpl.reset(new AccessibleStateSetHelperImpl()); +} + +AccessibleStateSetHelper::~AccessibleStateSetHelper() +{ +} + +//===== XAccessibleStateSet ============================================== + + /** Checks whether the current state set is empty. + + @return + Returns <TRUE/> if there is no state in this state set and + <FALSE/> if there is at least one state set in it. + */ +sal_Bool SAL_CALL AccessibleStateSetHelper::isEmpty () +{ + osl::MutexGuard aGuard (maMutex); + return mpHelperImpl->IsEmpty(); +} + + /** Checks if the given state is a member of the state set of this + object. + + @param aState + The state for which to check membership. This has to be one of + the constants of <type>AccessibleStateType</type>. + + @return + Returns <TRUE/> if the given state is a member of this object's + state set and <FALSE/> otherwise. + */ +sal_Bool SAL_CALL AccessibleStateSetHelper::contains (sal_Int16 aState) +{ + osl::MutexGuard aGuard (maMutex); + return mpHelperImpl->Contains(aState); +} + + /** Checks if all of the given states are in this object's state + set. + + @param aStateSet + This sequence of states is interpreted as set and every of its + members, duplicates are ignored, is checked for membership in + this object's state set. Each state has to be one of the + constants of <type>AccessibleStateType</type>. + + @return + Returns <TRUE/> if all states of the given state set are members + of this object's state set. <FALSE/> is returned if at least + one of the states in the given state is not a member of this + object's state set. + */ +sal_Bool SAL_CALL AccessibleStateSetHelper::containsAll + (const uno::Sequence<sal_Int16>& rStateSet) +{ + osl::MutexGuard aGuard (maMutex); + return std::all_of(rStateSet.begin(), rStateSet.end(), + [this](const sal_Int16 nState) { return mpHelperImpl->Contains(nState); }); +} + +uno::Sequence<sal_Int16> SAL_CALL AccessibleStateSetHelper::getStates() +{ + osl::MutexGuard aGuard(maMutex); + return mpHelperImpl->GetStates(); +} + +void AccessibleStateSetHelper::AddState(sal_Int16 aState) +{ + osl::MutexGuard aGuard (maMutex); + mpHelperImpl->AddState(aState); +} + +void AccessibleStateSetHelper::RemoveState(sal_Int16 aState) +{ + osl::MutexGuard aGuard (maMutex); + mpHelperImpl->RemoveState(aState); +} + +//===== XTypeProvider ======================================================= + +uno::Sequence< css::uno::Type> AccessibleStateSetHelper::getTypes() +{ + css::uno::Sequence< css::uno::Type> aTypeSequence { + cppu::UnoType<XAccessibleStateSet>::get(), + cppu::UnoType<lang::XTypeProvider>::get() + }; + return aTypeSequence; +} + +uno::Sequence<sal_Int8> SAL_CALL AccessibleStateSetHelper::getImplementationId() +{ + return css::uno::Sequence<sal_Int8>(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/bootstrap.cxx b/unotools/source/config/bootstrap.cxx new file mode 100644 index 000000000..a6c8eb228 --- /dev/null +++ b/unotools/source/config/bootstrap.cxx @@ -0,0 +1,742 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <config_folders.h> + +#include <unotools/bootstrap.hxx> + +#include <rtl/ustring.hxx> +#include <rtl/ustrbuf.hxx> +#include <sal/log.hxx> +#include <osl/file.hxx> +#include <osl/diagnose.h> + +#include <rtl/bootstrap.hxx> +#include <rtl/instance.hxx> +#include <osl/process.h> + +// #define this to true, if remembering defaults is not supported properly +#define RTL_BOOTSTRAP_DEFAULTS_BROKEN true + +#define BOOTSTRAP_ITEM_PRODUCT_KEY "ProductKey" +#define BOOTSTRAP_ITEM_VERSIONFILE "Location" +#define BOOTSTRAP_ITEM_BUILDID "buildid" + +#define BOOTSTRAP_ITEM_BASEINSTALLATION "BRAND_BASE_DIR" +#define BOOTSTRAP_ITEM_USERINSTALLATION "UserInstallation" + +#define BOOTSTRAP_ITEM_USERDIR "UserDataDir" + +#define BOOTSTRAP_DEFAULT_BASEINSTALL "$SYSBINDIR/.." + +#define BOOTSTRAP_DIRNAME_USERDIR "user" + +typedef char const * AsciiString; + +namespace utl +{ + +// Implementation class: Bootstrap::Impl + +static OUString makeImplName() +{ + OUString uri; + rtl::Bootstrap::get( "BRAND_BASE_DIR", uri); + return uri + "/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("bootstrap"); +} + +class Bootstrap::Impl +{ + const OUString m_aImplName; +public: // struct to cache the result of a path lookup + struct PathData + { + OUString path; + PathStatus status; + + PathData() + : path() + , status(DATA_UNKNOWN) + {} + }; +public: // data members + // base install data + PathData aBaseInstall_; + + // user install data + PathData aUserInstall_; + + // INI files + PathData aBootstrapINI_; + PathData aVersionINI_; + + // overall status + Status status_; + +public: // construction and initialization + Impl() : m_aImplName(makeImplName()) + { + initialize(); + } + + void initialize(); + + // access helper + OUString getBootstrapValue(OUString const& _sName, OUString const& _sDefault) const; + static bool getVersionValue(OUString const& _sName, OUString& _rValue, OUString const& _sDefault); + + const OUString& getImplName() const { return m_aImplName; } + +private: // implementation + bool initBaseInstallationData(rtl::Bootstrap const & _rData); + bool initUserInstallationData(rtl::Bootstrap const & _rData); +}; + +namespace +{ + class theImpl : public rtl::Static<Bootstrap::Impl, theImpl> {}; +} + +const Bootstrap::Impl& Bootstrap::data() +{ + return theImpl::get(); +} + +bool Bootstrap::getProcessWorkingDir(OUString &rUrl) +{ + rUrl.clear(); + OUString s("$OOO_CWD"); + rtl::Bootstrap::expandMacros(s); + if (s.isEmpty()) + { + if (osl_getProcessWorkingDir(&rUrl.pData) == osl_Process_E_None) + return true; + } + else if (s[0] == '1') + { + rUrl = s.copy(1); + return true; + } + else if (s[0] == '2' && + (osl::FileBase::getFileURLFromSystemPath(s.copy(1), rUrl) == + osl::FileBase::E_None)) + { + return true; + } + return false; +} + +void Bootstrap::reloadData() +{ + theImpl::get().initialize(); +} + +// helper + +typedef Bootstrap::PathStatus PathStatus; + +sal_Unicode const cURLSeparator = '/'; + +// path status utility function +static PathStatus implCheckStatusOfURL(OUString const& _sURL, osl::DirectoryItem& aDirItem) +{ + using namespace osl; + + PathStatus eStatus = Bootstrap::DATA_UNKNOWN; + + if (!_sURL.isEmpty()) + { + switch( DirectoryItem::get(_sURL, aDirItem) ) + { + case DirectoryItem::E_None: // Success + eStatus = Bootstrap::PATH_EXISTS; + break; + + case DirectoryItem::E_NOENT: // No such file or directory + eStatus = Bootstrap::PATH_VALID; + break; + + case DirectoryItem::E_INVAL: // the format of the parameters was not valid + case DirectoryItem::E_NAMETOOLONG: // File name too long + case DirectoryItem::E_NOTDIR: // A component of the path prefix of path is not a directory + eStatus = Bootstrap::DATA_INVALID; + break; + + // how to handle these ? + case DirectoryItem::E_LOOP: // Too many symbolic links encountered + case DirectoryItem::E_ACCES: // permission denied + // any other error - what to do ? + default: + eStatus = Bootstrap::DATA_UNKNOWN; + break; + } + } + else + { + eStatus = Bootstrap::DATA_MISSING; + } + + return eStatus; +} + +static bool implNormalizeURL(OUString & _sURL, osl::DirectoryItem& aDirItem) +{ + using namespace osl; + + OSL_PRECOND(aDirItem.is(), "Opened DirItem required"); + + static const sal_uInt32 cosl_FileStatus_Mask = osl_FileStatus_Mask_FileURL; + + FileStatus aFileStatus(cosl_FileStatus_Mask); + + if (aDirItem.getFileStatus(aFileStatus) != DirectoryItem::E_None) + return false; + + OUString aNormalizedURL = aFileStatus.getFileURL(); + + if (aNormalizedURL.isEmpty()) + return false; + + // #109863# sal/osl returns final slash for file URLs contradicting + // the URL/URI RFCs. + if ( !aNormalizedURL.endsWith(OUStringChar(cURLSeparator)) ) + _sURL = aNormalizedURL; + else + _sURL = aNormalizedURL.copy( 0, aNormalizedURL.getLength()-1 ); + + return true; +} + +static bool implEnsureAbsolute(OUString & _rsURL) // also strips embedded dots !! +{ + using osl::File; + + OUString sBasePath; + OSL_VERIFY(Bootstrap::getProcessWorkingDir(sBasePath)); + + OUString sAbsolute; + if ( File::E_None == File::getAbsoluteFileURL(sBasePath, _rsURL, sAbsolute)) + { + _rsURL = sAbsolute; + return true; + } + else + { + OSL_FAIL("Could not get absolute file URL for URL"); + return false; + } +} + +static bool implMakeAbsoluteURL(OUString & _rsPathOrURL) +{ + using namespace osl; + + bool bURL; + + OUString sOther; + // check if it already was normalized + if ( File::E_None == File::getSystemPathFromFileURL(_rsPathOrURL, sOther) ) + { + bURL = true; + } + + else if ( File::E_None == File::getFileURLFromSystemPath(_rsPathOrURL, sOther) ) + { + _rsPathOrURL = sOther; + bURL = true; + } + else + bURL = false; + + return bURL && implEnsureAbsolute(_rsPathOrURL); +} + +static PathStatus dbgCheckStatusOfURL(OUString const& _sURL) +{ + using namespace osl; + + DirectoryItem aDirItem; + + return implCheckStatusOfURL(_sURL,aDirItem); +} + +static PathStatus checkStatusAndNormalizeURL(OUString & _sURL) +{ + using namespace osl; + + PathStatus eStatus = Bootstrap::DATA_UNKNOWN; + + if (_sURL.isEmpty()) + eStatus = Bootstrap::DATA_MISSING; + + else if ( !implMakeAbsoluteURL(_sURL) ) + eStatus = Bootstrap::DATA_INVALID; + + else + { + DirectoryItem aDirItem; + + eStatus = implCheckStatusOfURL(_sURL,aDirItem); + + if (eStatus == Bootstrap::PATH_EXISTS && !implNormalizeURL(_sURL,aDirItem)) + OSL_FAIL("Unexpected failure getting actual URL for existing object"); + } + return eStatus; +} + +// helpers to build and check a nested URL +static PathStatus getDerivedPath( + OUString& _rURL, + OUString const& _aBaseURL, PathStatus _aBaseStatus, + OUString const& _sRelativeURL, + rtl::Bootstrap const & _rData, OUString const& _sBootstrapParameter + ) +{ + OUString sDerivedURL; + OSL_PRECOND(!_rData.getFrom(_sBootstrapParameter,sDerivedURL),"Setting for derived path is already defined"); + OSL_PRECOND(!_sRelativeURL.isEmpty() && _sRelativeURL[0] != cURLSeparator,"Invalid Relative URL"); + + PathStatus aStatus = _aBaseStatus; + + // do we have a base path ? + if (!_aBaseURL.isEmpty()) + { + OSL_PRECOND(!_aBaseURL.endsWith(OUStringChar(cURLSeparator)), "Unexpected: base URL ends in slash"); + + sDerivedURL = _aBaseURL + OUStringChar(cURLSeparator) + _sRelativeURL; + + // a derived (nested) URL can only exist or have a lesser status, if the parent exists + if (aStatus == Bootstrap::PATH_EXISTS) + aStatus = checkStatusAndNormalizeURL(sDerivedURL); + + else // the relative appendix must be valid + OSL_ASSERT(aStatus != Bootstrap::PATH_VALID || dbgCheckStatusOfURL(sDerivedURL) == Bootstrap::PATH_VALID); + + _rData.getFrom(_sBootstrapParameter, _rURL, sDerivedURL); + + OSL_ENSURE(sDerivedURL == _rURL,"Could not set derived URL via Bootstrap default parameter"); + SAL_WARN_IF( !(RTL_BOOTSTRAP_DEFAULTS_BROKEN || (_rData.getFrom(_sBootstrapParameter,sDerivedURL) && sDerivedURL==_rURL)), + "unotools.config", + "Use of default did not affect bootstrap value"); + } + else + { + // clear the result + _rURL = _aBaseURL; + + // if we have no data it can't be a valid path + OSL_ASSERT( aStatus > Bootstrap::PATH_VALID ); + } + + return aStatus; +} + +static PathStatus getDerivedPath( + OUString& _rURL, + Bootstrap::Impl::PathData const& _aBaseData, + OUString const& _sRelativeURL, + rtl::Bootstrap const & _rData, OUString const& _sBootstrapParameter + ) +{ + return getDerivedPath(_rURL,_aBaseData.path,_aBaseData.status,_sRelativeURL,_rData,_sBootstrapParameter); +} + +static OUString getExecutableBaseName() +{ + OUString sExecutable; + + if (osl_Process_E_None == osl_getExecutableFile(&sExecutable.pData)) + { + // split the executable name + sal_Int32 nSepIndex = sExecutable.lastIndexOf(cURLSeparator); + + sExecutable = sExecutable.copy(nSepIndex + 1); + + // ... and get the basename (strip the extension) + sal_Unicode const cExtensionSep = '.'; + + sal_Int32 const nExtIndex = sExecutable.lastIndexOf(cExtensionSep); + sal_Int32 const nExtLength = sExecutable.getLength() - nExtIndex - 1; + if (0 < nExtIndex && nExtLength < 4) + sExecutable = sExecutable.copy(0,nExtIndex); + } + else + SAL_WARN("unotools.config", "Cannot get executable name: osl_getExecutableFile failed"); + + return sExecutable; +} + +static Bootstrap::PathStatus updateStatus(Bootstrap::Impl::PathData & _rResult) +{ + _rResult.status = checkStatusAndNormalizeURL(_rResult.path); + return _rResult.status; +} + +static Bootstrap::PathStatus implGetBootstrapFile(rtl::Bootstrap const & _rData, Bootstrap::Impl::PathData & _rBootstrapFile) +{ + _rData.getIniName(_rBootstrapFile.path); + + return updateStatus(_rBootstrapFile); +} + +static Bootstrap::PathStatus implGetVersionFile(rtl::Bootstrap const & _rData, Bootstrap::Impl::PathData & _rVersionFile) +{ + _rData.getFrom(BOOTSTRAP_ITEM_VERSIONFILE, _rVersionFile.path); + + return updateStatus(_rVersionFile); +} + +// Error reporting + +static char const IS_MISSING[] = "is missing"; +static char const IS_INVALID[] = "is corrupt"; +static char const PERIOD[] = ". "; + +static void addFileError(OUStringBuffer& _rBuf, OUString const& _aPath, AsciiString _sWhat) +{ + OUString sSimpleFileName = _aPath.copy(1 +_aPath.lastIndexOf(cURLSeparator)); + + _rBuf.append("The configuration file"); + _rBuf.append(" '").append(sSimpleFileName).append("' "); + _rBuf.appendAscii(_sWhat).append(PERIOD); +} + +static void addMissingDirectoryError(OUStringBuffer& _rBuf, OUString const& _aPath) +{ + _rBuf.append("The configuration directory"); + _rBuf.append(" '").append(_aPath).append("' "); + _rBuf.append(IS_MISSING).append(PERIOD); +} + +static void addUnexpectedError(OUStringBuffer& _rBuf, AsciiString _sExtraInfo = nullptr) +{ + if (nullptr == _sExtraInfo) + _sExtraInfo = "An internal failure occurred"; + + _rBuf.appendAscii(_sExtraInfo).append(PERIOD); +} + +static Bootstrap::FailureCode describeError(OUStringBuffer& _rBuf, Bootstrap::Impl const& _rData) +{ + Bootstrap::FailureCode eErrCode = Bootstrap::INVALID_BOOTSTRAP_DATA; + + _rBuf.append("The program cannot be started. "); + + switch (_rData.aUserInstall_.status) + { + case Bootstrap::PATH_EXISTS: + switch (_rData.aBaseInstall_.status) + { + case Bootstrap::PATH_VALID: + addMissingDirectoryError(_rBuf, _rData.aBaseInstall_.path); + eErrCode = Bootstrap::MISSING_INSTALL_DIRECTORY; + break; + + case Bootstrap::DATA_INVALID: + addUnexpectedError(_rBuf,"The installation path is invalid"); + break; + + case Bootstrap::DATA_MISSING: + addUnexpectedError(_rBuf,"The installation path is not available"); + break; + + case Bootstrap::PATH_EXISTS: // seems to be all fine (?) + addUnexpectedError(_rBuf,""); + break; + + default: OSL_ASSERT(false); + addUnexpectedError(_rBuf); + break; + } + break; + + case Bootstrap::PATH_VALID: + addMissingDirectoryError(_rBuf, _rData.aUserInstall_.path); + eErrCode = Bootstrap::MISSING_USER_DIRECTORY; + break; + + // else fall through + case Bootstrap::DATA_INVALID: + if (_rData.aVersionINI_.status == Bootstrap::PATH_EXISTS) + { + addFileError(_rBuf, _rData.aVersionINI_.path, IS_INVALID); + eErrCode = Bootstrap::INVALID_VERSION_FILE_ENTRY; + break; + } + [[fallthrough]]; + + case Bootstrap::DATA_MISSING: + switch (_rData.aVersionINI_.status) + { + case Bootstrap::PATH_EXISTS: + addFileError(_rBuf, _rData.aVersionINI_.path, "does not support the current version"); + eErrCode = Bootstrap::MISSING_VERSION_FILE_ENTRY; + break; + + case Bootstrap::PATH_VALID: + addFileError(_rBuf, _rData.aVersionINI_.path, IS_MISSING); + eErrCode = Bootstrap::MISSING_VERSION_FILE; + break; + + default: + switch (_rData.aBootstrapINI_.status) + { + case Bootstrap::PATH_EXISTS: + addFileError(_rBuf, _rData.aBootstrapINI_.path, IS_INVALID); + + if (_rData.aVersionINI_.status == Bootstrap::DATA_MISSING) + eErrCode = Bootstrap::MISSING_BOOTSTRAP_FILE_ENTRY; + else + eErrCode = Bootstrap::INVALID_BOOTSTRAP_FILE_ENTRY; + break; + + case Bootstrap::DATA_INVALID: OSL_ASSERT(false); [[fallthrough]]; + case Bootstrap::PATH_VALID: + addFileError(_rBuf, _rData.aBootstrapINI_.path, IS_MISSING); + eErrCode = Bootstrap::MISSING_BOOTSTRAP_FILE; + break; + + default: + addUnexpectedError(_rBuf); + break; + } + break; + } + break; + + default: OSL_ASSERT(false); + addUnexpectedError(_rBuf); + break; + } + + return eErrCode; +} + + +OUString Bootstrap::getProductKey() +{ + OUString const csProductKeyItem(BOOTSTRAP_ITEM_PRODUCT_KEY); + + OUString const sDefaultProductKey = getExecutableBaseName(); + + return data().getBootstrapValue( csProductKeyItem, sDefaultProductKey ); +} + +OUString Bootstrap::getProductKey(OUString const& _sDefault) +{ + OUString const csProductKeyItem(BOOTSTRAP_ITEM_PRODUCT_KEY); + + return data().getBootstrapValue( csProductKeyItem, _sDefault ); +} + +OUString Bootstrap::getBuildIdData(OUString const& _sDefault) +{ + OUString const csBuildIdItem(BOOTSTRAP_ITEM_BUILDID); + + OUString sBuildId; + // read buildid from version.ini (versionrc), if it doesn't exist or buildid is empty + if ( !utl::Bootstrap::Impl::getVersionValue( csBuildIdItem, sBuildId, _sDefault ) + || sBuildId.isEmpty() ) + { + // read buildid from bootstrap.ini (bootstraprc) + sBuildId = data().getBootstrapValue( csBuildIdItem, _sDefault ); + } + return sBuildId; +} + +Bootstrap::PathStatus Bootstrap::locateBaseInstallation(OUString& _rURL) +{ + Impl::PathData const& aPathData = data().aBaseInstall_; + + _rURL = aPathData.path; + return aPathData.status; +} + +Bootstrap::PathStatus Bootstrap::locateUserInstallation(OUString& _rURL) +{ + Impl::PathData const& aPathData = data().aUserInstall_; + + _rURL = aPathData.path; + return aPathData.status; +} + +Bootstrap::PathStatus Bootstrap::locateUserData(OUString& _rURL) +{ + OUString const csUserDirItem(BOOTSTRAP_ITEM_USERDIR); + + rtl::Bootstrap aData( data().getImplName() ); + + if ( aData.getFrom(csUserDirItem, _rURL) ) + { + return checkStatusAndNormalizeURL(_rURL); + } + else + { + OUString const csUserDir(BOOTSTRAP_DIRNAME_USERDIR); + return getDerivedPath(_rURL, data().aUserInstall_ ,csUserDir, aData, csUserDirItem); + } +} + +Bootstrap::PathStatus Bootstrap::locateBootstrapFile(OUString& _rURL) +{ + Impl::PathData const& aPathData = data().aBootstrapINI_; + + _rURL = aPathData.path; + return aPathData.status; +} + +Bootstrap::PathStatus Bootstrap::locateVersionFile(OUString& _rURL) +{ + Impl::PathData const& aPathData = data().aVersionINI_; + + _rURL = aPathData.path; + return aPathData.status; +} + +Bootstrap::Status Bootstrap::checkBootstrapStatus(OUString& _rDiagnosticMessage, FailureCode& _rErrCode) +{ + Impl const& aData = data(); + + Status result = aData.status_; + + // maybe do further checks here + + OUStringBuffer sErrorBuffer; + if (result != DATA_OK) + _rErrCode = describeError(sErrorBuffer,aData); + + else + _rErrCode = NO_FAILURE; + + _rDiagnosticMessage = sErrorBuffer.makeStringAndClear(); + + return result; +} + + +bool Bootstrap::Impl::initBaseInstallationData(rtl::Bootstrap const & _rData) +{ + OUString const csBaseInstallItem( BOOTSTRAP_ITEM_BASEINSTALLATION ); + OUString const csBaseInstallDefault( BOOTSTRAP_DEFAULT_BASEINSTALL ); + + _rData.getFrom(csBaseInstallItem, aBaseInstall_.path, csBaseInstallDefault); + + bool bResult = (PATH_EXISTS == updateStatus(aBaseInstall_)); + + implGetBootstrapFile(_rData, aBootstrapINI_); + + return bResult; +} + +bool Bootstrap::Impl::initUserInstallationData(rtl::Bootstrap const & _rData) +{ + OUString const csUserInstallItem( BOOTSTRAP_ITEM_USERINSTALLATION ); + + if (_rData.getFrom(csUserInstallItem, aUserInstall_.path)) + { + updateStatus(aUserInstall_); + } + else + { + // should we do just this + aUserInstall_.status = DATA_MISSING; + + // ... or this - look for a single-user user directory ? + OUString const csUserDirItem(BOOTSTRAP_ITEM_USERDIR); + OUString sDummy; + // look for $BASEINSTALLATION/user only if default UserDir setting is used + if (! _rData.getFrom(csUserDirItem, sDummy)) + { + OUString const csUserDir(BOOTSTRAP_DIRNAME_USERDIR); + + if ( PATH_EXISTS == getDerivedPath(sDummy, aBaseInstall_, csUserDir, _rData, csUserDirItem) ) + aUserInstall_ = aBaseInstall_; + } + } + + bool bResult = (PATH_EXISTS == aUserInstall_.status); + + implGetVersionFile(_rData, aVersionINI_); + + return bResult; +} + +void Bootstrap::Impl::initialize() +{ + rtl::Bootstrap aData( m_aImplName ); + + if (!initBaseInstallationData(aData)) + { + status_ = INVALID_BASE_INSTALL; + } + else if (!initUserInstallationData(aData)) + { + status_ = INVALID_USER_INSTALL; + + if (aUserInstall_.status >= DATA_MISSING) + { + switch (aVersionINI_.status) + { + case PATH_EXISTS: + case PATH_VALID: + status_ = MISSING_USER_INSTALL; + break; + + case DATA_INVALID: + case DATA_MISSING: + status_ = INVALID_BASE_INSTALL; + break; + default: + break; + } + } + } + else + { + status_ = DATA_OK; + } +} + +OUString Bootstrap::Impl::getBootstrapValue(OUString const& _sName, OUString const& _sDefault) const +{ + rtl::Bootstrap aData( m_aImplName ); + + OUString sResult; + aData.getFrom(_sName,sResult,_sDefault); + return sResult; +} + +bool Bootstrap::Impl::getVersionValue(OUString const& _sName, OUString& _rValue, OUString const& _sDefault) +{ + // try to open version.ini (versionrc) + OUString uri; + rtl::Bootstrap::get( "BRAND_BASE_DIR", uri); + rtl::Bootstrap aData( uri + "/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("version") ); + if ( aData.getHandle() == nullptr ) + // version.ini (versionrc) doesn't exist + return false; + + // read value + aData.getFrom(_sName,_rValue,_sDefault); + return true; +} + +} // namespace utl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/cmdoptions.cxx b/unotools/source/config/cmdoptions.cxx new file mode 100644 index 000000000..5826eb0ac --- /dev/null +++ b/unotools/source/config/cmdoptions.cxx @@ -0,0 +1,356 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <sal/log.hxx> +#include <unotools/cmdoptions.hxx> +#include <unotools/configitem.hxx> +#include <tools/debug.hxx> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Sequence.hxx> +#include <com/sun/star/frame/XFrame.hpp> +#include <cppuhelper/weakref.hxx> +#include <rtl/instance.hxx> + +#include "itemholder1.hxx" + +#include <algorithm> +#include <unordered_map> + +using namespace ::std; +using namespace ::utl; +using namespace ::osl; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::beans; + +#define ROOTNODE_CMDOPTIONS "Office.Commands/Execute" +#define PATHDELIMITER "/" + +#define SETNODE_DISABLED "Disabled" + +#define PROPERTYNAME_CMD "Command" + +namespace { + +/*-**************************************************************************************************************** + @descr support simple command option structures and operations on it +****************************************************************************************************************-*/ +class SvtCmdOptions +{ + public: + + // the only way to free memory! + void Clear() + { + m_aCommandHashMap.clear(); + } + + bool HasEntries() const + { + return ( !m_aCommandHashMap.empty() ); + } + + bool Lookup( const OUString& aCmd ) const + { + CommandHashMap::const_iterator pEntry = m_aCommandHashMap.find( aCmd ); + return ( pEntry != m_aCommandHashMap.end() ); + } + + void AddCommand( const OUString& aCmd ) + { + m_aCommandHashMap.emplace( aCmd, 0 ); + } + + private: + typedef std::unordered_map<OUString, sal_Int32> + CommandHashMap; + + CommandHashMap m_aCommandHashMap; +}; + +} + +typedef ::std::vector< css::uno::WeakReference< css::frame::XFrame > > SvtFrameVector; + +class SvtCommandOptions_Impl : public ConfigItem +{ + public: + + SvtCommandOptions_Impl(); + virtual ~SvtCommandOptions_Impl() override; + + /*-**************************************************************************************************** + @short called for notify of configmanager + @descr This method is called from the ConfigManager before the application ends or from the + PropertyChangeListener if the sub tree broadcasts changes. You must update your + internal values. + + @seealso baseclass ConfigItem + + @param "lPropertyNames" is the list of properties which should be updated. + *//*-*****************************************************************************************************/ + + virtual void Notify( const Sequence< OUString >& lPropertyNames ) override; + + /*-**************************************************************************************************** + @short base implementation of public interface for "SvtDynamicMenuOptions"! + @descr These class is used as static member of "SvtDynamicMenuOptions" ... + => The code exist only for one time and isn't duplicated for every instance! + *//*-*****************************************************************************************************/ + + bool HasEntries ( SvtCommandOptions::CmdOption eOption ) const; + bool Lookup ( SvtCommandOptions::CmdOption eCmdOption, const OUString& ) const; + void EstablishFrameCallback(const css::uno::Reference< css::frame::XFrame >& xFrame); + + private: + + virtual void ImplCommit() override; + + /*-**************************************************************************************************** + @short return list of key names of our configuration management which represent our module tree + @descr This method returns the current list of key names! We need it to get needed values from our + configuration management and support dynamical menu item lists! + @param "nDisabledCount", returns count of menu entries for "new" + @return A list of configuration key names is returned. + *//*-*****************************************************************************************************/ + + Sequence< OUString > impl_GetPropertyNames(); + + private: + SvtCmdOptions m_aDisabledCommands; + SvtFrameVector m_lFrames; +}; + +// constructor + +SvtCommandOptions_Impl::SvtCommandOptions_Impl() + // Init baseclasses first + : ConfigItem( ROOTNODE_CMDOPTIONS ) + // Init member then... +{ + // Get names and values of all accessible menu entries and fill internal structures. + // See impl_GetPropertyNames() for further information. + Sequence< OUString > lNames = impl_GetPropertyNames (); + Sequence< Any > lValues = GetProperties ( lNames ); + + // Safe impossible cases. + // We need values from ALL configuration keys. + // Follow assignment use order of values in relation to our list of key names! + DBG_ASSERT( !(lNames.getLength()!=lValues.getLength()), "SvtCommandOptions_Impl::SvtCommandOptions_Impl()\nI miss some values of configuration keys!\n" ); + + // Copy values from list in right order to our internal member. + // Attention: List for names and values have an internal construction pattern! + sal_Int32 nItem = 0; + OUString sCmd; + + // Get names/values for disabled commands. + for( nItem=0; nItem < lNames.getLength(); ++nItem ) + { + // Currently only one value + lValues[nItem] >>= sCmd; + m_aDisabledCommands.AddCommand( sCmd ); + } + +/*TODO: Not used in the moment! see Notify() ... + // Enable notification mechanism of our baseclass. + // We need it to get information about changes outside these class on our used configuration keys! */ + Sequence<OUString> aNotifySeq { "Disabled" }; + EnableNotification( aNotifySeq, true ); +} + +// destructor + +SvtCommandOptions_Impl::~SvtCommandOptions_Impl() +{ + assert(!IsModified()); // should have been committed +} + +// public method + +void SvtCommandOptions_Impl::Notify( const Sequence< OUString >& ) +{ + MutexGuard aGuard( SvtCommandOptions::GetOwnStaticMutex() ); + + Sequence< OUString > lNames = impl_GetPropertyNames (); + Sequence< Any > lValues = GetProperties ( lNames ); + + // Safe impossible cases. + // We need values from ALL configuration keys. + // Follow assignment use order of values in relation to our list of key names! + DBG_ASSERT( !(lNames.getLength()!=lValues.getLength()), "SvtCommandOptions_Impl::Notify()\nI miss some values of configuration keys!\n" ); + + // Copy values from list in right order to our internal member. + // Attention: List for names and values have an internal construction pattern! + sal_Int32 nItem = 0; + OUString sCmd; + + m_aDisabledCommands.Clear(); + + // Get names/values for disabled commands. + for( nItem=0; nItem < lNames.getLength(); ++nItem ) + { + // Currently only one value + lValues[nItem] >>= sCmd; + m_aDisabledCommands.AddCommand( sCmd ); + } + + // don't forget to update all existing frames and her might cached dispatch objects! + // But look for already killed frames. We hold weak references instead of hard ones ... + for (SvtFrameVector::iterator pIt = m_lFrames.begin(); pIt != m_lFrames.end(); ) + { + css::uno::Reference< css::frame::XFrame > xFrame(pIt->get(), css::uno::UNO_QUERY); + if (xFrame.is()) + { + xFrame->contextChanged(); + ++pIt; + } + else + pIt = m_lFrames.erase(pIt); + } +} + +// public method + +void SvtCommandOptions_Impl::ImplCommit() +{ + SAL_WARN("unotools.config","SvtCommandOptions_Impl::ImplCommit(): Not implemented yet!"); +} + +// public method + +bool SvtCommandOptions_Impl::HasEntries( SvtCommandOptions::CmdOption eOption ) const +{ + if ( eOption == SvtCommandOptions::CMDOPTION_DISABLED ) + return m_aDisabledCommands.HasEntries(); + else + return false; +} + +// public method + +bool SvtCommandOptions_Impl::Lookup( SvtCommandOptions::CmdOption eCmdOption, const OUString& aCommand ) const +{ + switch( eCmdOption ) + { + case SvtCommandOptions::CMDOPTION_DISABLED: + { + return m_aDisabledCommands.Lookup( aCommand ); + } + default: + SAL_WARN( "unotools.config", "SvtCommandOptions_Impl::Lookup() Unknown option type given!" ); + } + + return false; +} + +// public method + +void SvtCommandOptions_Impl::EstablishFrameCallback(const css::uno::Reference< css::frame::XFrame >& xFrame) +{ + // check if frame already exists inside list + // ignore double registrations + // every frame must be notified one times only! + css::uno::WeakReference< css::frame::XFrame > xWeak(xFrame); + SvtFrameVector::const_iterator pIt = ::std::find(m_lFrames.begin(), m_lFrames.end(), xWeak); + if (pIt == m_lFrames.end()) + m_lFrames.push_back(xWeak); +} + +// private method + +Sequence< OUString > SvtCommandOptions_Impl::impl_GetPropertyNames() +{ + // First get ALL names of current existing list items in configuration! + Sequence< OUString > lDisabledItems = GetNodeNames( SETNODE_DISABLED, utl::ConfigNameFormat::LocalPath ); + + // Expand all keys + std::transform(lDisabledItems.begin(), lDisabledItems.end(), lDisabledItems.begin(), + [](const OUString& rItem) -> OUString { + return SETNODE_DISABLED PATHDELIMITER + rItem + PATHDELIMITER PROPERTYNAME_CMD; }); + + // Return result. + return lDisabledItems; +} + +namespace { + +std::weak_ptr<SvtCommandOptions_Impl> g_pCommandOptions; + +} + +SvtCommandOptions::SvtCommandOptions() +{ + // Global access, must be guarded (multithreading!). + MutexGuard aGuard( GetOwnStaticMutex() ); + + m_pImpl = g_pCommandOptions.lock(); + if( !m_pImpl ) + { + m_pImpl = std::make_shared<SvtCommandOptions_Impl>(); + g_pCommandOptions = m_pImpl; + ItemHolder1::holdConfigItem(EItem::CmdOptions); + } +} + +SvtCommandOptions::~SvtCommandOptions() +{ + // Global access, must be guarded (multithreading!) + MutexGuard aGuard( GetOwnStaticMutex() ); + + m_pImpl.reset(); +} + +// public method + +bool SvtCommandOptions::HasEntries( CmdOption eOption ) const +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + return m_pImpl->HasEntries( eOption ); +} + +// public method + +bool SvtCommandOptions::Lookup( CmdOption eCmdOption, const OUString& aCommandURL ) const +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + return m_pImpl->Lookup( eCmdOption, aCommandURL ); +} + +// public method + +void SvtCommandOptions::EstablishFrameCallback(const css::uno::Reference< css::frame::XFrame >& xFrame) +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + m_pImpl->EstablishFrameCallback(xFrame); +} + +namespace +{ + class theCommandOptionsMutex : public rtl::Static<osl::Mutex, theCommandOptionsMutex>{}; +} + +// private method + +Mutex& SvtCommandOptions::GetOwnStaticMutex() +{ + return theCommandOptionsMutex::get(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/compatibility.cxx b/unotools/source/config/compatibility.cxx new file mode 100644 index 000000000..5556a0a23 --- /dev/null +++ b/unotools/source/config/compatibility.cxx @@ -0,0 +1,378 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unotools/compatibility.hxx> +#include <unotools/configitem.hxx> +#include <unotools/syslocale.hxx> +#include <tools/debug.hxx> +#include <sal/log.hxx> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Sequence.hxx> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/lang/Locale.hpp> +#include <i18nlangtag/languagetag.hxx> + +#include "itemholder1.hxx" + +#include <algorithm> +#include <vector> + +using namespace ::std; +using namespace ::utl; +using namespace ::osl; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::beans; + +#define ROOTNODE_OPTIONS "Office.Compatibility" +#define PATHDELIMITER "/" +#define SETNODE_ALLFILEFORMATS "AllFileFormats" + +SvtCompatibilityEntry::SvtCompatibilityEntry() + : m_aPropertyValue( SvtCompatibilityEntry::getElementCount() ) +{ + /* Should be in the start. Do not remove it. */ + setValue<OUString>( Index::Name, OUString() ); + setValue<OUString>( Index::Module, OUString() ); + + /* Editable list of default values. Sync it with the SvtCompatibilityEntry::Index enum class. */ + setValue<bool>( Index::UsePrtMetrics, false ); + setValue<bool>( Index::AddSpacing, false ); + setValue<bool>( Index::AddSpacingAtPages, false ); + setValue<bool>( Index::UseOurTabStops, false ); + setValue<bool>( Index::NoExtLeading, false ); + setValue<bool>( Index::UseLineSpacing, false ); + setValue<bool>( Index::AddTableSpacing, false ); + setValue<bool>( Index::UseObjectPositioning, false ); + setValue<bool>( Index::UseOurTextWrapping, false ); + setValue<bool>( Index::ConsiderWrappingStyle, false ); + setValue<bool>( Index::ExpandWordSpace, true ); + setValue<bool>( Index::ProtectForm, false ); + setValue<bool>( Index::MsWordTrailingBlanks, false ); + setValue<bool>( Index::SubtractFlysAnchoredAtFlys, false ); + setValue<bool>( Index::EmptyDbFieldHidesPara, true ); + setValue<bool>( Index::AddTableLineSpacing, false ); + + setDefaultEntry( false ); +} + +OUString SvtCompatibilityEntry::getName( const Index rIdx ) +{ + static const char* sPropertyName[] = + { + /* Should be in the start. Do not remove it. */ + "Name", + "Module", + + /* Editable list of compatibility option names. Sync it with the SvtCompatibilityEntry::Index enum class. */ + "UsePrinterMetrics", + "AddSpacing", + "AddSpacingAtPages", + "UseOurTabStopFormat", + "NoExternalLeading", + "UseLineSpacing", + "AddTableSpacing", + "UseObjectPositioning", + "UseOurTextWrapping", + "ConsiderWrappingStyle", + "ExpandWordSpace", + "ProtectForm", + "MsWordCompTrailingBlanks", + "SubtractFlysAnchoredAtFlys", + "EmptyDbFieldHidesPara", + "AddTableLineSpacing", + }; + + /* Size of sPropertyName array not equal size of the SvtCompatibilityEntry::Index enum class */ + assert( SAL_N_ELEMENTS(sPropertyName) == static_cast<int>( SvtCompatibilityEntry::getElementCount() ) ); + + return OUString::createFromAscii( sPropertyName[ static_cast<int>(rIdx) ] ); +} + +/*-**************************************************************************************************************** + @descr support simple menu structures and operations on it +****************************************************************************************************************-*/ + +/*-**************************************************************************************************** + @short base implementation of public interface for "SvtCompatibilityOptions"! + @descr These class is used as static member of "SvtCompatibilityOptions" ... + => The code exist only for one time and isn't duplicated for every instance! +*//*-*****************************************************************************************************/ +class SvtCompatibilityOptions_Impl : public ConfigItem +{ + public: + SvtCompatibilityOptions_Impl(); + virtual ~SvtCompatibilityOptions_Impl() override; + + void AppendItem( const SvtCompatibilityEntry& aItem ); + void Clear(); + + void SetDefault( SvtCompatibilityEntry::Index rIdx, bool rValue ); + bool GetDefault( SvtCompatibilityEntry::Index rIdx ) const; + + Sequence< Sequence< PropertyValue > > GetList() const; + + /*-**************************************************************************************************** + @short called for notify of configmanager + @descr This method is called from the ConfigManager before the application ends or from the + PropertyChangeListener if the sub tree broadcasts changes. You must update your + internal values. + + @seealso baseclass ConfigItem + + @param "lPropertyNames" is the list of properties which should be updated. + *//*-*****************************************************************************************************/ + virtual void Notify( const Sequence< OUString >& lPropertyNames ) override; + + private: + virtual void ImplCommit() override; + + /*-**************************************************************************************************** + @short return list of key names of our configuration management which represent one module tree + @descr These methods return the current list of key names! We need it to get needed values from our + configuration management and support dynamical menu item lists! + @return A list of configuration key names is returned. + *//*-*****************************************************************************************************/ + Sequence< OUString > impl_GetPropertyNames( Sequence< OUString >& rItems ); + + private: + std::vector< SvtCompatibilityEntry > m_aOptions; + SvtCompatibilityEntry m_aDefOptions; +}; + +SvtCompatibilityOptions_Impl::SvtCompatibilityOptions_Impl() : ConfigItem( ROOTNODE_OPTIONS ) +{ + // Get names and values of all accessible menu entries and fill internal structures. + // See impl_GetPropertyNames() for further information. + Sequence< OUString > lNodes; + Sequence< OUString > lNames = impl_GetPropertyNames( lNodes ); + Sequence< Any > lValues = GetProperties( lNames ); + + // Safe impossible cases. + // We need values from ALL configuration keys. + // Follow assignment use order of values in relation to our list of key names! + DBG_ASSERT( !( lNames.getLength()!=lValues.getLength() ), "SvtCompatibilityOptions_Impl::SvtCompatibilityOptions_Impl()\nI miss some values of configuration keys!\n" ); + + // Get names/values for new menu. + // 4 subkeys for every item! + bool bDefaultFound = false; + sal_Int32 nDestStep = 0; + for ( const auto& rNode : std::as_const(lNodes) ) + { + SvtCompatibilityEntry aItem; + + aItem.setValue<OUString>( SvtCompatibilityEntry::Index::Name, rNode ); + + for ( int i = static_cast<int>(SvtCompatibilityEntry::Index::Module); i < static_cast<int>(SvtCompatibilityEntry::Index::INVALID); ++i ) + { + aItem.setValue( SvtCompatibilityEntry::Index(i), lValues[ nDestStep ] ); + nDestStep++; + } + + m_aOptions.push_back( aItem ); + + if ( !bDefaultFound && aItem.getValue<OUString>( SvtCompatibilityEntry::Index::Name ) == SvtCompatibilityEntry::getDefaultEntryName() ) + { + SvtSysLocale aSysLocale; + css::lang::Locale aLocale = aSysLocale.GetLanguageTag().getLocale(); + if ( aLocale.Language == "zh" || aLocale.Language == "ja" || aLocale.Language == "ko" ) + aItem.setValue<bool>( SvtCompatibilityEntry::Index::ExpandWordSpace, false ); + + m_aDefOptions = aItem; + bDefaultFound = true; + } + } +} + +SvtCompatibilityOptions_Impl::~SvtCompatibilityOptions_Impl() +{ + assert( !IsModified() ); // should have been committed +} + +void SvtCompatibilityOptions_Impl::AppendItem( const SvtCompatibilityEntry& aItem ) +{ + m_aOptions.push_back( aItem ); + + // default item reset? + if ( aItem.getValue<OUString>( SvtCompatibilityEntry::Index::Name ) == SvtCompatibilityEntry::getDefaultEntryName() ) + m_aDefOptions = aItem; + + SetModified(); +} + +void SvtCompatibilityOptions_Impl::Clear() +{ + m_aOptions.clear(); + + SetModified(); +} + +void SvtCompatibilityOptions_Impl::SetDefault( SvtCompatibilityEntry::Index rIdx, bool rValue ) +{ + /* Are not set Name and Module */ + assert( rIdx != SvtCompatibilityEntry::Index::Name && rIdx != SvtCompatibilityEntry::Index::Module ); + + m_aDefOptions.setValue<bool>( rIdx, rValue ); +} + +bool SvtCompatibilityOptions_Impl::GetDefault( SvtCompatibilityEntry::Index rIdx ) const +{ + /* Are not set Name and Module */ + assert( rIdx != SvtCompatibilityEntry::Index::Name && rIdx != SvtCompatibilityEntry::Index::Module ); + + return m_aDefOptions.getValue<bool>( rIdx ); +} + +Sequence< Sequence< PropertyValue > > SvtCompatibilityOptions_Impl::GetList() const +{ + Sequence< PropertyValue > lProperties( SvtCompatibilityEntry::getElementCount() ); + Sequence< Sequence< PropertyValue > > lResult( m_aOptions.size() ); + + for ( int i = static_cast<int>(SvtCompatibilityEntry::Index::Name); i < static_cast<int>(SvtCompatibilityEntry::Index::INVALID); ++i ) + lProperties[i].Name = SvtCompatibilityEntry::getName( SvtCompatibilityEntry::Index(i) ); + + sal_Int32 j = 0; + for ( const auto& rItem : m_aOptions ) + { + for ( int i = static_cast<int>(SvtCompatibilityEntry::Index::Name); i < static_cast<int>(SvtCompatibilityEntry::Index::INVALID); ++i ) + lProperties[i].Value = rItem.getValue( SvtCompatibilityEntry::Index(i) ); + lResult[ j++ ] = lProperties; + } + + return lResult; +} + +void SvtCompatibilityOptions_Impl::Notify( const Sequence< OUString >& ) +{ + SAL_WARN( "unotools.config", "SvtCompatibilityOptions_Impl::Notify() Not implemented yet! I don't know how I can handle a dynamical list of unknown properties ..." ); +} + +void SvtCompatibilityOptions_Impl::ImplCommit() +{ + // Write all properties! + // Delete complete set first. + ClearNodeSet( SETNODE_ALLFILEFORMATS ); + + Sequence< PropertyValue > lPropertyValues( SvtCompatibilityEntry::getElementCount() - 1 ); + sal_uInt32 nNewCount = m_aOptions.size(); + for ( sal_uInt32 nItem = 0; nItem < nNewCount; ++nItem ) + { + SvtCompatibilityEntry aItem = m_aOptions[ nItem ]; + OUString sNode = SETNODE_ALLFILEFORMATS PATHDELIMITER + aItem.getValue<OUString>( SvtCompatibilityEntry::Index::Name ) + PATHDELIMITER; + + for ( int i = static_cast<int>(SvtCompatibilityEntry::Index::Module); i < static_cast<int>(SvtCompatibilityEntry::Index::INVALID); ++i ) + { + lPropertyValues[ i - 1 ].Name = sNode + SvtCompatibilityEntry::getName( SvtCompatibilityEntry::Index(i) ); + lPropertyValues[ i - 1 ].Value = aItem.getValue( SvtCompatibilityEntry::Index(i) ); + } + + SetSetProperties( SETNODE_ALLFILEFORMATS, lPropertyValues ); + } +} + +Sequence< OUString > SvtCompatibilityOptions_Impl::impl_GetPropertyNames( Sequence< OUString >& rItems ) +{ + // First get ALL names of current existing list items in configuration! + rItems = GetNodeNames( SETNODE_ALLFILEFORMATS ); + + // expand list to result list ... + Sequence< OUString > lProperties( rItems.getLength() * ( SvtCompatibilityEntry::getElementCount() - 1 ) ); + + sal_Int32 nDestStep = 0; + // Copy entries to destination and expand every item with 2 supported sub properties. + for ( const auto& rItem : std::as_const(rItems) ) + { + OUString sFixPath = SETNODE_ALLFILEFORMATS PATHDELIMITER + rItem + PATHDELIMITER; + for ( int i = static_cast<int>(SvtCompatibilityEntry::Index::Module); i < static_cast<int>(SvtCompatibilityEntry::Index::INVALID); ++i ) + { + lProperties[ nDestStep ] = sFixPath + SvtCompatibilityEntry::getName( SvtCompatibilityEntry::Index(i) ); + nDestStep++; + } + } + + // Return result. + return lProperties; +} + +namespace +{ + std::weak_ptr<SvtCompatibilityOptions_Impl> theOptions; +} + +SvtCompatibilityOptions::SvtCompatibilityOptions() +{ + // Global access, must be guarded (multithreading!). + MutexGuard aGuard( GetOwnStaticMutex() ); + + m_pImpl = theOptions.lock(); + if ( !m_pImpl ) + { + m_pImpl = std::make_shared<SvtCompatibilityOptions_Impl>(); + theOptions = m_pImpl; + ItemHolder1::holdConfigItem( EItem::Compatibility ); + } +} + +SvtCompatibilityOptions::~SvtCompatibilityOptions() +{ + // Global access, must be guarded (multithreading!) + MutexGuard aGuard( GetOwnStaticMutex() ); + m_pImpl.reset(); +} + +void SvtCompatibilityOptions::AppendItem( const SvtCompatibilityEntry& aItem ) +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + m_pImpl->AppendItem( aItem ); +} + +void SvtCompatibilityOptions::Clear() +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + m_pImpl->Clear(); +} + +void SvtCompatibilityOptions::SetDefault( SvtCompatibilityEntry::Index rIdx, bool rValue ) +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + m_pImpl->SetDefault( rIdx, rValue ); +} + +bool SvtCompatibilityOptions::GetDefault( SvtCompatibilityEntry::Index rIdx ) const +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + return m_pImpl->GetDefault( rIdx ); +} + +Sequence< Sequence< PropertyValue > > SvtCompatibilityOptions::GetList() const +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + return m_pImpl->GetList(); +} + +namespace +{ + class theCompatibilityOptionsMutex : public rtl::Static<osl::Mutex, theCompatibilityOptionsMutex>{}; +} + +Mutex& SvtCompatibilityOptions::GetOwnStaticMutex() +{ + return theCompatibilityOptionsMutex::get(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/compatibilityviewoptions.cxx b/unotools/source/config/compatibilityviewoptions.cxx new file mode 100644 index 000000000..b7a64f43f --- /dev/null +++ b/unotools/source/config/compatibilityviewoptions.cxx @@ -0,0 +1,170 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + */ + +#include <unotools/compatibilityviewoptions.hxx> +#include <unotools/configitem.hxx> +#include "itemholder1.hxx" + +#define ROOTNODE_COMPATIBILITY_VIEW "Office.Compatibility/View" + +#define PROPERTYNAME_MSCOMPATIBLEFORMSMENU "MSCompatibleFormsMenu" + +#define PROPERTYHANDLE_MSCOMPATIBLEFORMSMENU 0 + +class SvtCompatibilityViewOptions_Impl : public utl::ConfigItem +{ +private: + bool m_bShowMSCompatibleFormsMenu; + +public: + SvtCompatibilityViewOptions_Impl(); + virtual ~SvtCompatibilityViewOptions_Impl() override; + + /** + @short Called for notify of configmanager. + + This method is called from the ConfigManager before application ends or from the + PropertyChangeListener if the sub tree broadcasts changes. You must update your + internal values. + + @see baseclass ConfigItem + + @param "seqPropertyNames" is the list of properties which should be updated. + */ + virtual void Notify(const css::uno::Sequence<OUString>& seqPropertyNames) override; + + bool HasMSOCompatibleFormsMenu() const { return m_bShowMSCompatibleFormsMenu; } + void SetMSOCompatibleFormsMenu(bool bSet) + { + bool bModified = (m_bShowMSCompatibleFormsMenu != bSet); + if (bModified) + { + m_bShowMSCompatibleFormsMenu = bSet; + SetModified(); + Commit(); + } + } + +private: + virtual void ImplCommit() override; + + /** + @short Return list of fix key names of our configuration management which represent our module tree. + + This method returns a static const list of key names. We need it to get needed values from our + configuration management. + + @return A list of needed configuration keys is returned. + */ + static css::uno::Sequence<OUString> const& impl_GetPropertyNames(); +}; + +SvtCompatibilityViewOptions_Impl::SvtCompatibilityViewOptions_Impl() + : ConfigItem(ROOTNODE_COMPATIBILITY_VIEW) + , m_bShowMSCompatibleFormsMenu(false) +{ + // Use our static list of configuration keys to get his values. + css::uno::Sequence<OUString> seqNames = impl_GetPropertyNames(); + css::uno::Sequence<css::uno::Any> seqValues = GetProperties(seqNames); + assert(seqNames.getLength() == seqValues.getLength()); + + if (seqValues[PROPERTYHANDLE_MSCOMPATIBLEFORMSMENU].hasValue()) + { + assert(seqValues[PROPERTYHANDLE_MSCOMPATIBLEFORMSMENU].getValueTypeClass() + == css::uno::TypeClass_BOOLEAN); + seqValues[PROPERTYHANDLE_MSCOMPATIBLEFORMSMENU] >>= m_bShowMSCompatibleFormsMenu; + } + + EnableNotification(seqNames); +} + +SvtCompatibilityViewOptions_Impl::~SvtCompatibilityViewOptions_Impl() +{ + assert(!IsModified()); // should have been committed +} + +void SvtCompatibilityViewOptions_Impl::Notify(const css::uno::Sequence<OUString>& seqPropertyNames) +{ + // Use given list of updated properties to get his values from configuration directly! + css::uno::Sequence<css::uno::Any> seqValues = GetProperties(seqPropertyNames); + assert(seqPropertyNames.getLength() == seqValues.getLength()); + + for (sal_Int32 nProperty = 0; nProperty < seqPropertyNames.getLength(); ++nProperty) + { + if (seqPropertyNames[nProperty] == PROPERTYNAME_MSCOMPATIBLEFORMSMENU) + { + assert(seqValues[nProperty].getValueTypeClass() == css::uno::TypeClass_BOOLEAN); + seqValues[nProperty] >>= m_bShowMSCompatibleFormsMenu; + } + } +} + +void SvtCompatibilityViewOptions_Impl::ImplCommit() +{ + // Get names of supported properties, create a list for values and copy current values to it. + css::uno::Sequence<OUString> seqNames = impl_GetPropertyNames(); + css::uno::Sequence<css::uno::Any> seqValues(seqNames.getLength()); + + seqValues[PROPERTYHANDLE_MSCOMPATIBLEFORMSMENU] <<= m_bShowMSCompatibleFormsMenu; + + // Set properties in configuration. + PutProperties(seqNames, seqValues); +} + +css::uno::Sequence<OUString> const& SvtCompatibilityViewOptions_Impl::impl_GetPropertyNames() +{ + static const css::uno::Sequence<OUString> seqPropertyNames{ OUString( + PROPERTYNAME_MSCOMPATIBLEFORMSMENU) }; + return seqPropertyNames; +} + +namespace +{ +std::weak_ptr<SvtCompatibilityViewOptions_Impl> theOptions; +} + +SvtCompatibilityViewOptions::SvtCompatibilityViewOptions() +{ + // Global access, must be guarded (multithreading!). + osl::MutexGuard aGuard(GetOwnStaticMutex()); + + m_pImpl = theOptions.lock(); + if (!m_pImpl) + { + m_pImpl = std::make_shared<SvtCompatibilityViewOptions_Impl>(); + theOptions = m_pImpl; + ItemHolder1::holdConfigItem(EItem::CompatibilityView); + } +} + +SvtCompatibilityViewOptions::~SvtCompatibilityViewOptions() +{ + // Global access, must be guarded (multithreading!) + osl::MutexGuard aGuard(GetOwnStaticMutex()); + m_pImpl.reset(); +} + +bool SvtCompatibilityViewOptions::HasMSOCompatibleFormsMenu() const +{ + return m_pImpl->HasMSOCompatibleFormsMenu(); +} + +void SvtCompatibilityViewOptions::SetMSOCompatibleFormsMenu(bool bSet) +{ + m_pImpl->SetMSOCompatibleFormsMenu(bSet); +} + +osl::Mutex& SvtCompatibilityViewOptions::GetOwnStaticMutex() +{ + static osl::Mutex ourMutex; + + return ourMutex; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/unotools/source/config/configitem.cxx b/unotools/source/config/configitem.cxx new file mode 100644 index 000000000..a67850797 --- /dev/null +++ b/unotools/source/config/configitem.cxx @@ -0,0 +1,1066 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <sal/log.hxx> +#include <unotools/configitem.hxx> +#include <unotools/configmgr.hxx> +#include <unotools/configpaths.hxx> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/util/XChangesListener.hpp> +#include <com/sun/star/util/XChangesNotifier.hpp> +#include <com/sun/star/container/XHierarchicalNameAccess.hpp> +#include <com/sun/star/configuration/XTemplateContainer.hpp> +#include <com/sun/star/container/XNameContainer.hpp> +#include <com/sun/star/lang/XSingleServiceFactory.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/beans/PropertyAttribute.hpp> +#include <com/sun/star/util/XChangesBatch.hpp> +#include <osl/diagnose.h> +#include <comphelper/sequence.hxx> +#include <comphelper/solarmutex.hxx> +#include <rtl/ref.hxx> +#include <tools/diagnose_ex.h> + +using namespace utl; +using namespace com::sun::star::uno; +using namespace com::sun::star::util; +using namespace com::sun::star::lang; +using namespace com::sun::star::beans; +using namespace com::sun::star::container; +using namespace com::sun::star::configuration; + +#include <cppuhelper/implbase.hxx> + +/* + The ConfigChangeListener_Impl receives notifications from the configuration about changes that + have happened. It forwards this notification to the ConfigItem it knows a pParent by calling its + "CallNotify" method. As ConfigItems are most probably not thread safe, the SolarMutex is acquired + before doing so. +*/ + +namespace utl{ + class ConfigChangeListener_Impl : public cppu::WeakImplHelper + < + css::util::XChangesListener + > + { + public: + ConfigItem* pParent; + const Sequence< OUString > aPropertyNames; + ConfigChangeListener_Impl(ConfigItem& rItem, const Sequence< OUString >& rNames); + + //XChangesListener + virtual void SAL_CALL changesOccurred( const ChangesEvent& Event ) override; + + //XEventListener + virtual void SAL_CALL disposing( const EventObject& Source ) override; + }; +} + +namespace { + +class ValueCounter_Impl +{ + sal_Int16& rCnt; +public: + explicit ValueCounter_Impl(sal_Int16& rCounter) + : rCnt(rCounter) + { + rCnt++; + } + ~ValueCounter_Impl() + { + OSL_ENSURE(rCnt>0, "RefCount < 0 ??"); + rCnt--; + } +}; + +} + +ConfigChangeListener_Impl::ConfigChangeListener_Impl( + ConfigItem& rItem, const Sequence< OUString >& rNames) : + pParent(&rItem), + aPropertyNames(rNames) +{ +} + +void ConfigChangeListener_Impl::changesOccurred( const ChangesEvent& rEvent ) +{ + Sequence<OUString> aChangedNames(rEvent.Changes.getLength()); + OUString* pNames = aChangedNames.getArray(); + + sal_Int32 nNotify = 0; + for(const auto& rElementChange : rEvent.Changes) + { + OUString sTemp; + rElementChange.Accessor >>= sTemp; + //true if the path is completely correct or if it is longer + //i.e ...Print/Content/Graphic and .../Print + bool bFound = std::any_of(aPropertyNames.begin(), aPropertyNames.end(), + [&sTemp](const OUString& rCheckPropertyName) { return isPrefixOfConfigurationPath(sTemp, rCheckPropertyName); }); + if(bFound) + pNames[nNotify++] = sTemp; + } + if( nNotify ) + { + ::comphelper::SolarMutex *pMutex = ::comphelper::SolarMutex::get(); + if ( pMutex ) + { + osl::Guard<comphelper::SolarMutex> aMutexGuard( pMutex ); + aChangedNames.realloc(nNotify); + pParent->CallNotify(aChangedNames); + } + } +} + +void ConfigChangeListener_Impl::disposing( const EventObject& /*rSource*/ ) +{ + pParent->RemoveChangesListener(); +} + +ConfigItem::ConfigItem(const OUString &rSubTree, ConfigItemMode nSetMode ) : + sSubTree(rSubTree), + m_nMode(nSetMode), + m_bIsModified(false), + m_bEnableInternalNotification(false), + m_nInValueChange(0) +{ + if (utl::ConfigManager::IsFuzzing()) + return; + + if (nSetMode & ConfigItemMode::ReleaseTree) + ConfigManager::getConfigManager().addConfigItem(*this); + else + m_xHierarchyAccess = ConfigManager::getConfigManager().addConfigItem(*this); +} + +ConfigItem::~ConfigItem() +{ + RemoveChangesListener(); + ConfigManager::getConfigManager().removeConfigItem(*this); +} + +void ConfigItem::CallNotify( const css::uno::Sequence<OUString>& rPropertyNames ) +{ + // the call is forwarded to the virtual Notify() method + // it is pure virtual, so all classes deriving from ConfigItem have to decide how they + // want to notify listeners + if(m_nInValueChange <= 0 || m_bEnableInternalNotification) + Notify(rPropertyNames); +} + +void ConfigItem::impl_packLocalizedProperties( const Sequence< OUString >& lInNames , + const Sequence< Any >& lInValues , + Sequence< Any >& lOutValues ) +{ + // Safe impossible cases. + // This method should be called for special ConfigItem-mode only! + OSL_ENSURE( ((m_nMode & ConfigItemMode::AllLocales ) == ConfigItemMode::AllLocales), "ConfigItem::impl_packLocalizedProperties() Wrong call of this method detected!" ); + + sal_Int32 nSourceCounter; // used to step during input lists + sal_Int32 nSourceSize; // marks end of loop over input lists + sal_Int32 nDestinationCounter; // actual position in output lists + sal_Int32 nPropertyCounter; // counter of inner loop for Sequence< PropertyValue > + sal_Int32 nPropertiesSize; // marks end of inner loop + Sequence< OUString > lPropertyNames; // list of all locales for localized entry + Sequence< PropertyValue > lProperties; // localized values of a configuration entry packed for return + Reference< XInterface > xLocalizedNode; // if cfg entry is localized ... lInValues contains an XInterface! + + // Optimise follow algorithm ... A LITTLE BIT :-) + // There exist two different possibilities: + // i ) There exist no localized entries ... => size of lOutValues will be the same like lInNames/lInValues! + // ii) There exist some (mostly one or two) localized entries ... => size of lOutValues will be the same like lInNames/lInValues! + // ... Why? If a localized value exist - the any is filled with an XInterface object (is a SetNode-service). + // We read all his child nodes and pack it into Sequence< PropertyValue >. + // The result list we pack into the return any. We never change size of lists! + nSourceSize = lInNames.getLength(); + lOutValues.realloc( nSourceSize ); + + // Algorithm: + // Copy all names and values from in to out lists. + // Look for special localized entries ... You can detect it as "XInterface" packed into an Any. + // Use this XInterface-object to read all localized values and pack it into Sequence< PropertValue >. + // Add this list to out lists then. + + nDestinationCounter = 0; + for( nSourceCounter=0; nSourceCounter<nSourceSize; ++nSourceCounter ) + { + // If item a special localized one ... convert and pack it ... + if( lInValues[nSourceCounter].getValueTypeName() == "com.sun.star.uno.XInterface" ) + { + lInValues[nSourceCounter] >>= xLocalizedNode; + Reference< XNameContainer > xSetAccess( xLocalizedNode, UNO_QUERY ); + if( xSetAccess.is() ) + { + lPropertyNames = xSetAccess->getElementNames(); + nPropertiesSize = lPropertyNames.getLength(); + lProperties.realloc( nPropertiesSize ); + + for( nPropertyCounter=0; nPropertyCounter<nPropertiesSize; ++nPropertyCounter ) + { + lProperties[nPropertyCounter].Name = lPropertyNames[nPropertyCounter]; + OUString sLocaleValue; + xSetAccess->getByName( lPropertyNames[nPropertyCounter] ) >>= sLocaleValue; + lProperties[nPropertyCounter].Value <<= sLocaleValue; + } + + lOutValues[nDestinationCounter] <<= lProperties; + } + } + // ... or copy normal items to return lists directly. + else + { + lOutValues[nDestinationCounter] = lInValues[nSourceCounter]; + } + ++nDestinationCounter; + } +} + +void ConfigItem::impl_unpackLocalizedProperties( const Sequence< OUString >& lInNames , + const Sequence< Any >& lInValues , + Sequence< OUString >& lOutNames , + Sequence< Any >& lOutValues ) +{ + // Safe impossible cases. + // This method should be called for special ConfigItem-mode only! + OSL_ENSURE( ((m_nMode & ConfigItemMode::AllLocales ) == ConfigItemMode::AllLocales), "ConfigItem::impl_unpackLocalizedProperties() Wrong call of this method detected!" ); + + sal_Int32 nSourceSize; // marks end of loop over input lists + sal_Int32 nDestinationCounter; // actual position in output lists + sal_Int32 nPropertiesSize; // marks end of inner loop + OUString sNodeName; // base name of node ( e.g. "UIName/" ) ... expand to locale ( e.g. "UIName/de" ) + Sequence< PropertyValue > lProperties; // localized values of a configuration entry gotten from lInValues-Any + + // Optimise follow algorithm ... A LITTLE BIT :-) + // There exist two different possibilities: + // i ) There exist no localized entries ... => size of lOutNames/lOutValues will be the same like lInNames/lInValues! + // ii) There exist some (mostly one or two) localized entries ... => size of lOutNames/lOutValues will be some bytes greater then lInNames/lInValues. + // => I think we should make it fast for i). ii) is a special case and mustn't be SOOOO... fast. + // We should reserve same space for output list like input ones first. + // Follow algorithm looks for these borders and change it for ii) only! + // It will be faster then a "realloc()" call in every loop ... + nSourceSize = lInNames.getLength(); + + lOutNames.realloc ( nSourceSize ); + lOutValues.realloc ( nSourceSize ); + + // Algorithm: + // Copy all names and values from const to return lists. + // Look for special localized entries ... You can detect it as Sequence< PropertyValue > packed into an Any. + // Split it ... insert PropertyValue.Name to lOutNames and PropertyValue.Value to lOutValues. + + nDestinationCounter = 0; + for( sal_Int32 nSourceCounter=0; nSourceCounter<nSourceSize; ++nSourceCounter ) + { + // If item a special localized one ... split it and insert his parts to output lists ... + if( lInValues[nSourceCounter].getValueType() == cppu::UnoType<Sequence<PropertyValue>>::get() ) + { + lInValues[nSourceCounter] >>= lProperties; + nPropertiesSize = lProperties.getLength(); + + sNodeName = lInNames[nSourceCounter] + "/"; + + if( (nDestinationCounter+nPropertiesSize) > lOutNames.getLength() ) + { + lOutNames.realloc ( nDestinationCounter+nPropertiesSize ); + lOutValues.realloc ( nDestinationCounter+nPropertiesSize ); + } + + for( const auto& rProperty : std::as_const(lProperties) ) + { + lOutNames [nDestinationCounter] = sNodeName + rProperty.Name; + lOutValues[nDestinationCounter] = rProperty.Value; + ++nDestinationCounter; + } + } + // ... or copy normal items to return lists directly. + else + { + if( (nDestinationCounter+1) > lOutNames.getLength() ) + { + lOutNames.realloc ( nDestinationCounter+1 ); + lOutValues.realloc ( nDestinationCounter+1 ); + } + + lOutNames [nDestinationCounter] = lInNames [nSourceCounter]; + lOutValues[nDestinationCounter] = lInValues[nSourceCounter]; + ++nDestinationCounter; + } + } +} + +Sequence< sal_Bool > ConfigItem::GetReadOnlyStates(const css::uno::Sequence< OUString >& rNames) +{ + sal_Int32 i; + + // size of return list is fix! + // Every item must match to length of incoming name list. + sal_Int32 nCount = rNames.getLength(); + Sequence< sal_Bool > lStates(nCount); + + // We must be sure to return a valid information every time! + // Set default to non readonly... similar to the configuration handling of this property. + std::fill(lStates.begin(), lStates.end(), false); + + // no access - no information... + Reference< XHierarchicalNameAccess > xHierarchyAccess = GetTree(); + if (!xHierarchyAccess.is()) + return lStates; + + for (i=0; i<nCount; ++i) + { + try + { + OUString sName = rNames[i]; + OUString sPath; + OUString sProperty; + + (void)::utl::splitLastFromConfigurationPath(sName,sPath,sProperty); + if (sPath.isEmpty() && sProperty.isEmpty()) + { + OSL_FAIL("ConfigItem::IsReadonly() split failed"); + continue; + } + + Reference< XInterface > xNode; + Reference< XPropertySet > xSet; + Reference< XPropertySetInfo > xInfo; + if (!sPath.isEmpty()) + { + Any aNode = xHierarchyAccess->getByHierarchicalName(sPath); + if (!(aNode >>= xNode) || !xNode.is()) + { + OSL_FAIL("ConfigItem::IsReadonly() no set available"); + continue; + } + } + else + { + xNode = xHierarchyAccess; + } + + xSet.set(xNode, UNO_QUERY); + if (xSet.is()) + { + xInfo = xSet->getPropertySetInfo(); + OSL_ENSURE(xInfo.is(), "ConfigItem::IsReadonly() getPropertySetInfo failed ..."); + } + else + { + xInfo.set(xNode, UNO_QUERY); + OSL_ENSURE(xInfo.is(), "ConfigItem::IsReadonly() UNO_QUERY failed ..."); + } + + if (!xInfo.is()) + { + OSL_FAIL("ConfigItem::IsReadonly() no prop info available"); + continue; + } + + Property aProp = xInfo->getPropertyByName(sProperty); + lStates[i] = ((aProp.Attributes & PropertyAttribute::READONLY) == PropertyAttribute::READONLY); + } + catch (const Exception&) + { + } + } + + return lStates; +} + +Sequence< Any > ConfigItem::GetProperties(const Sequence< OUString >& rNames) +{ + Sequence< Any > aRet(rNames.getLength()); + const OUString* pNames = rNames.getConstArray(); + Any* pRet = aRet.getArray(); + Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree(); + if(xHierarchyAccess.is()) + { + for(int i = 0; i < rNames.getLength(); i++) + { + try + { + pRet[i] = xHierarchyAccess->getByHierarchicalName(pNames[i]); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( + "unotools.config", + "ignoring XHierarchicalNameAccess to /org.openoffice." + << sSubTree << "/" << pNames[i]); + } + } + + // In special mode "ALL_LOCALES" we must convert localized values to Sequence< PropertyValue >. + if((m_nMode & ConfigItemMode::AllLocales ) == ConfigItemMode::AllLocales) + { + Sequence< Any > lValues; + impl_packLocalizedProperties( rNames, aRet, lValues ); + aRet = lValues; + } + } + return aRet; +} + +bool ConfigItem::PutProperties( const Sequence< OUString >& rNames, + const Sequence< Any>& rValues) +{ + ValueCounter_Impl aCounter(m_nInValueChange); + Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree(); + Reference<XNameReplace> xTopNodeReplace(xHierarchyAccess, UNO_QUERY); + bool bRet = xHierarchyAccess.is() && xTopNodeReplace.is(); + if(bRet) + { + Sequence< OUString > lNames; + Sequence< Any > lValues; + const OUString* pNames = nullptr; + const Any* pValues = nullptr; + sal_Int32 nNameCount; + if(( m_nMode & ConfigItemMode::AllLocales ) == ConfigItemMode::AllLocales ) + { + // If ConfigItem works in "ALL_LOCALES"-mode ... we must support a Sequence< PropertyValue > + // as value of a localized configuration entry! + // How we can do that? + // We must split all PropertyValues to "Sequence< OUString >" AND "Sequence< Any >"! + impl_unpackLocalizedProperties( rNames, rValues, lNames, lValues ); + pNames = lNames.getConstArray (); + pValues = lValues.getConstArray (); + nNameCount = lNames.getLength (); + } + else + { + // This is the normal mode ... + // Use given input lists directly. + pNames = rNames.getConstArray (); + pValues = rValues.getConstArray (); + nNameCount = rNames.getLength (); + } + for(int i = 0; i < nNameCount; i++) + { + try + { + OUString sNode, sProperty; + if (splitLastFromConfigurationPath(pNames[i],sNode, sProperty)) + { + Any aNode = xHierarchyAccess->getByHierarchicalName(sNode); + + Reference<XNameAccess> xNodeAcc; + aNode >>= xNodeAcc; + Reference<XNameReplace> xNodeReplace(xNodeAcc, UNO_QUERY); + Reference<XNameContainer> xNodeCont (xNodeAcc, UNO_QUERY); + + bool bExist = (xNodeAcc.is() && xNodeAcc->hasByName(sProperty)); + if (bExist && xNodeReplace.is()) + xNodeReplace->replaceByName(sProperty, pValues[i]); + else + if (!bExist && xNodeCont.is()) + xNodeCont->insertByName(sProperty, pValues[i]); + else + bRet = false; + } + else //direct value + { + xTopNodeReplace->replaceByName(sProperty, pValues[i]); + } + } + catch (css::uno::Exception &) + { + TOOLS_WARN_EXCEPTION("unotools.config", "Exception from PutProperties"); + } + } + try + { + Reference<XChangesBatch> xBatch(xHierarchyAccess, UNO_QUERY); + xBatch->commitChanges(); + } + catch (css::uno::Exception &) + { + TOOLS_WARN_EXCEPTION("unotools.config", "Exception from commitChanges"); + } + } + + return bRet; +} + +void ConfigItem::DisableNotification() +{ + OSL_ENSURE( xChangeLstnr.is(), "ConfigItem::DisableNotification: notifications not enabled currently!" ); + RemoveChangesListener(); +} + +bool ConfigItem::EnableNotification(const Sequence< OUString >& rNames, + bool bEnableInternalNotification ) +{ + OSL_ENSURE(!(m_nMode & ConfigItemMode::ReleaseTree), "notification in ConfigItemMode::ReleaseTree mode not possible"); + m_bEnableInternalNotification = bEnableInternalNotification; + Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree(); + Reference<XChangesNotifier> xChgNot(xHierarchyAccess, UNO_QUERY); + if(!xChgNot.is()) + return false; + + OSL_ENSURE(!xChangeLstnr.is(), "EnableNotification already called"); + if(xChangeLstnr.is()) + xChgNot->removeChangesListener( xChangeLstnr ); + bool bRet = true; + + try + { + xChangeLstnr = new ConfigChangeListener_Impl(*this, rNames); + xChgNot->addChangesListener( xChangeLstnr ); + } + catch (const RuntimeException&) + { + bRet = false; + } + return bRet; +} + +void ConfigItem::RemoveChangesListener() +{ + Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree(); + if(xHierarchyAccess.is()) + { + Reference<XChangesNotifier> xChgNot(xHierarchyAccess, UNO_QUERY); + if(xChgNot.is() && xChangeLstnr.is()) + { + try + { + xChgNot->removeChangesListener( xChangeLstnr ); + xChangeLstnr = nullptr; + } + catch (const Exception&) + { + } + } + } +} + +static void lcl_normalizeLocalNames(Sequence< OUString >& _rNames, ConfigNameFormat _eFormat, Reference<XInterface> const& _xParentNode) +{ + switch (_eFormat) + { + case ConfigNameFormat::LocalNode: + // unaltered - this is our input format + break; + + case ConfigNameFormat::LocalPath: + { + Reference<XTemplateContainer> xTypeContainer(_xParentNode, UNO_QUERY); + if (xTypeContainer.is()) + { + OUString sTypeName = xTypeContainer->getElementTemplateName(); + sTypeName = sTypeName.copy(sTypeName.lastIndexOf('/')+1); + + std::transform(_rNames.begin(), _rNames.end(), _rNames.begin(), + [&sTypeName](const OUString& rName) -> OUString { return wrapConfigurationElementName(rName,sTypeName); }); + } + else + { + Reference<XServiceInfo> xSVI(_xParentNode, UNO_QUERY); + if (xSVI.is() && xSVI->supportsService("com.sun.star.configuration.SetAccess")) + { + std::transform(_rNames.begin(), _rNames.end(), _rNames.begin(), + [](const OUString& rName) -> OUString { return wrapConfigurationElementName(rName); }); + } + } + } + break; + + } +} + +Sequence< OUString > ConfigItem::GetNodeNames(const OUString& rNode) +{ + ConfigNameFormat const eDefaultFormat = ConfigNameFormat::LocalNode; // CONFIG_NAME_DEFAULT; + + return GetNodeNames(rNode, eDefaultFormat); +} + +Sequence< OUString > ConfigItem::GetNodeNames(const OUString& rNode, ConfigNameFormat eFormat) +{ + Sequence< OUString > aRet; + Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree(); + if(xHierarchyAccess.is()) + { + try + { + Reference<XNameAccess> xCont; + if(!rNode.isEmpty()) + { + Any aNode = xHierarchyAccess->getByHierarchicalName(rNode); + aNode >>= xCont; + } + else + xCont.set(xHierarchyAccess, UNO_QUERY); + if(xCont.is()) + { + aRet = xCont->getElementNames(); + lcl_normalizeLocalNames(aRet,eFormat,xCont); + } + + } + catch (css::uno::Exception &) + { + TOOLS_WARN_EXCEPTION("unotools.config", "Exception from GetNodeNames"); + } + } + return aRet; +} + +bool ConfigItem::ClearNodeSet(const OUString& rNode) +{ + ValueCounter_Impl aCounter(m_nInValueChange); + bool bRet = false; + Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree(); + if(xHierarchyAccess.is()) + { + try + { + Reference<XNameContainer> xCont; + if(!rNode.isEmpty()) + { + Any aNode = xHierarchyAccess->getByHierarchicalName(rNode); + aNode >>= xCont; + } + else + xCont.set(xHierarchyAccess, UNO_QUERY); + if(!xCont.is()) + return false; + const Sequence< OUString > aNames = xCont->getElementNames(); + Reference<XChangesBatch> xBatch(xHierarchyAccess, UNO_QUERY); + for(const OUString& rName : aNames) + { + try + { + xCont->removeByName(rName); + } + catch (css::uno::Exception &) + { + TOOLS_WARN_EXCEPTION("unotools.config", "Exception from removeByName"); + } + } + xBatch->commitChanges(); + bRet = true; + } + catch (css::uno::Exception &) + { + TOOLS_WARN_EXCEPTION("unotools.config", "Exception from ClearNodeSet"); + } + } + return bRet; +} + +bool ConfigItem::ClearNodeElements(const OUString& rNode, Sequence< OUString > const & rElements) +{ + ValueCounter_Impl aCounter(m_nInValueChange); + bool bRet = false; + Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree(); + if(xHierarchyAccess.is()) + { + try + { + Reference<XNameContainer> xCont; + if(!rNode.isEmpty()) + { + Any aNode = xHierarchyAccess->getByHierarchicalName(rNode); + aNode >>= xCont; + } + else + xCont.set(xHierarchyAccess, UNO_QUERY); + if(!xCont.is()) + return false; + try + { + for(const OUString& rElement : rElements) + { + xCont->removeByName(rElement); + } + Reference<XChangesBatch> xBatch(xHierarchyAccess, UNO_QUERY); + xBatch->commitChanges(); + } + catch (css::uno::Exception &) + { + TOOLS_WARN_EXCEPTION("unotools.config", "Exception from commitChanges()"); + } + bRet = true; + } + catch (css::uno::Exception &) + { + TOOLS_WARN_EXCEPTION("unotools.config", "Exception from GetNodeNames()"); + } + } + return bRet; +} + +static OUString lcl_extractSetPropertyName( const OUString& rInPath, const OUString& rPrefix ) +{ + OUString const sSubPath = dropPrefixFromConfigurationPath( rInPath, rPrefix); + return extractFirstFromConfigurationPath( sSubPath ); +} + +static +Sequence< OUString > lcl_extractSetPropertyNames( const Sequence< PropertyValue >& rValues, const OUString& rPrefix ) +{ + Sequence< OUString > aSubNodeNames(rValues.getLength()); + OUString* pSubNodeNames = aSubNodeNames.getArray(); + + OUString sLastSubNode; + sal_Int32 nSubIndex = 0; + + for(const PropertyValue& rProperty : rValues) + { + OUString const sSubPath = dropPrefixFromConfigurationPath( rProperty.Name, rPrefix); + OUString const sSubNode = extractFirstFromConfigurationPath( sSubPath ); + + if(sLastSubNode != sSubNode) + { + pSubNodeNames[nSubIndex++] = sSubNode; + } + + sLastSubNode = sSubNode; + } + aSubNodeNames.realloc(nSubIndex); + + return aSubNodeNames; +} + +// Add or change properties +bool ConfigItem::SetSetProperties( + const OUString& rNode, const Sequence< PropertyValue >& rValues) +{ + ValueCounter_Impl aCounter(m_nInValueChange); + bool bRet = true; + Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree(); + if(xHierarchyAccess.is()) + { + Reference<XChangesBatch> xBatch(xHierarchyAccess, UNO_QUERY); + try + { + Reference<XNameContainer> xCont; + if(!rNode.isEmpty()) + { + Any aNode = xHierarchyAccess->getByHierarchicalName(rNode); + aNode >>= xCont; + } + else + xCont.set(xHierarchyAccess, UNO_QUERY); + if(!xCont.is()) + return false; + + Reference<XSingleServiceFactory> xFac(xCont, UNO_QUERY); + + if(xFac.is()) + { + const Sequence< OUString > aSubNodeNames = lcl_extractSetPropertyNames(rValues, rNode); + + for(const auto& rSubNodeName : aSubNodeNames) + { + if(!xCont->hasByName(rSubNodeName)) + { + Reference<XInterface> xInst = xFac->createInstance(); + Any aVal; aVal <<= xInst; + xCont->insertByName(rSubNodeName, aVal); + } + //set values + } + try + { + xBatch->commitChanges(); + } + catch (css::uno::Exception &) + { + TOOLS_WARN_EXCEPTION("unotools.config", "Exception from commitChanges()"); + } + + const PropertyValue* pProperties = rValues.getConstArray(); + + Sequence< OUString > aSetNames(rValues.getLength()); + OUString* pSetNames = aSetNames.getArray(); + + Sequence< Any> aSetValues(rValues.getLength()); + Any* pSetValues = aSetValues.getArray(); + + bool bEmptyNode = rNode.isEmpty(); + for(sal_Int32 k = 0; k < rValues.getLength(); k++) + { + pSetNames[k] = pProperties[k].Name.copy( bEmptyNode ? 1 : 0); + pSetValues[k] = pProperties[k].Value; + } + bRet = PutProperties(aSetNames, aSetValues); + } + else + { + //if no factory is available then the node contains basic data elements + for(const PropertyValue& rValue : rValues) + { + try + { + OUString sSubNode = lcl_extractSetPropertyName( rValue.Name, rNode ); + + if(xCont->hasByName(sSubNode)) + xCont->replaceByName(sSubNode, rValue.Value); + else + xCont->insertByName(sSubNode, rValue.Value); + + OSL_ENSURE( xHierarchyAccess->hasByHierarchicalName(rValue.Name), + "Invalid config path" ); + } + catch (css::uno::Exception &) + { + TOOLS_WARN_EXCEPTION("unotools.config", "Exception from insert/replaceByName()"); + } + } + xBatch->commitChanges(); + } + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION("unotools.config", "Exception from SetSetProperties"); + bRet = false; + } + } + return bRet; +} + +bool ConfigItem::ReplaceSetProperties( + const OUString& rNode, const Sequence< PropertyValue >& rValues) +{ + ValueCounter_Impl aCounter(m_nInValueChange); + bool bRet = true; + Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree(); + if(xHierarchyAccess.is()) + { + Reference<XChangesBatch> xBatch(xHierarchyAccess, UNO_QUERY); + try + { + Reference<XNameContainer> xCont; + if(!rNode.isEmpty()) + { + Any aNode = xHierarchyAccess->getByHierarchicalName(rNode); + aNode >>= xCont; + } + else + xCont.set(xHierarchyAccess, UNO_QUERY); + if(!xCont.is()) + return false; + + // JB: Change: now the same name handling for sets of simple values + const Sequence< OUString > aSubNodeNames = lcl_extractSetPropertyNames(rValues, rNode); + + Reference<XSingleServiceFactory> xFac(xCont, UNO_QUERY); + const bool isSimpleValueSet = !xFac.is(); + + //remove unknown members first + { + const Sequence<OUString> aContainerSubNodes = xCont->getElementNames(); + + for(const OUString& rContainerSubNode : aContainerSubNodes) + { + bool bFound = comphelper::findValue(aSubNodeNames, rContainerSubNode) != -1; + if(!bFound) + try + { + xCont->removeByName(rContainerSubNode); + } + catch (const Exception&) + { + if (isSimpleValueSet) + { + try + { + // #i37322#: fallback action: replace with <void/> + xCont->replaceByName(rContainerSubNode, Any()); + // fallback successful: continue looping + continue; + } + catch (Exception &) + {} // propagate original exception, if fallback fails + } + throw; + } + } + try { xBatch->commitChanges(); } + catch (css::uno::Exception &) + { + TOOLS_WARN_EXCEPTION("unotools.config", "Exception from commitChanges"); + } + } + + if(xFac.is()) // !isSimpleValueSet + { + for(const OUString& rSubNodeName : aSubNodeNames) + { + if(!xCont->hasByName(rSubNodeName)) + { + //create if not available + Reference<XInterface> xInst = xFac->createInstance(); + Any aVal; aVal <<= xInst; + xCont->insertByName(rSubNodeName, aVal); + } + } + try { xBatch->commitChanges(); } + catch (css::uno::Exception &) + { + TOOLS_WARN_EXCEPTION("unotools.config", "Exception from commitChanges"); + } + + const PropertyValue* pProperties = rValues.getConstArray(); + + Sequence< OUString > aSetNames(rValues.getLength()); + OUString* pSetNames = aSetNames.getArray(); + + Sequence< Any> aSetValues(rValues.getLength()); + Any* pSetValues = aSetValues.getArray(); + + bool bEmptyNode = rNode.isEmpty(); + for(sal_Int32 k = 0; k < rValues.getLength(); k++) + { + pSetNames[k] = pProperties[k].Name.copy( bEmptyNode ? 1 : 0); + pSetValues[k] = pProperties[k].Value; + } + bRet = PutProperties(aSetNames, aSetValues); + } + else + { + //if no factory is available then the node contains basic data elements + for(const PropertyValue& rValue : rValues) + { + try + { + OUString sSubNode = lcl_extractSetPropertyName( rValue.Name, rNode ); + + if(xCont->hasByName(sSubNode)) + xCont->replaceByName(sSubNode, rValue.Value); + else + xCont->insertByName(sSubNode, rValue.Value); + } + catch (css::uno::Exception &) + { + TOOLS_WARN_EXCEPTION("unotools.config", "Exception from insert/replaceByName"); + } + } + xBatch->commitChanges(); + } + } + catch (const Exception& ) + { + TOOLS_WARN_EXCEPTION("unotools.config", "Exception from ReplaceSetProperties"); + bRet = false; + } + } + return bRet; +} + +bool ConfigItem::AddNode(const OUString& rNode, const OUString& rNewNode) +{ + ValueCounter_Impl aCounter(m_nInValueChange); + bool bRet = true; + Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree(); + if(xHierarchyAccess.is()) + { + Reference<XChangesBatch> xBatch(xHierarchyAccess, UNO_QUERY); + try + { + Reference<XNameContainer> xCont; + if(!rNode.isEmpty()) + { + Any aNode = xHierarchyAccess->getByHierarchicalName(rNode); + aNode >>= xCont; + } + else + xCont.set(xHierarchyAccess, UNO_QUERY); + if(!xCont.is()) + return false; + + Reference<XSingleServiceFactory> xFac(xCont, UNO_QUERY); + + if(xFac.is()) + { + if(!xCont->hasByName(rNewNode)) + { + Reference<XInterface> xInst = xFac->createInstance(); + Any aVal; aVal <<= xInst; + xCont->insertByName(rNewNode, aVal); + } + try + { + xBatch->commitChanges(); + } + catch (css::uno::Exception &) + { + TOOLS_WARN_EXCEPTION("unotools.config", "Exception from commitChanges"); + } + } + else + { + //if no factory is available then the node contains basic data elements + try + { + if(!xCont->hasByName(rNewNode)) + xCont->insertByName(rNewNode, Any()); + } + catch (css::uno::Exception &) + { + TOOLS_WARN_EXCEPTION("unotools.config", "Exception from AddNode"); + } + } + xBatch->commitChanges(); + } + catch (const Exception&) + { + DBG_UNHANDLED_EXCEPTION("unotools.config"); + bRet = false; + } + } + return bRet; +} + + +void ConfigItem::SetModified() +{ + m_bIsModified = true; +} + +void ConfigItem::ClearModified() +{ + m_bIsModified = false; +} + +Reference< XHierarchicalNameAccess> ConfigItem::GetTree() +{ + Reference< XHierarchicalNameAccess> xRet; + if (utl::ConfigManager::IsFuzzing()) + return xRet; + if(!m_xHierarchyAccess.is()) + xRet = ConfigManager::acquireTree(*this); + else + xRet = m_xHierarchyAccess; + return xRet; +} + +void ConfigItem::Commit() +{ + ImplCommit(); + ClearModified(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/configmgr.cxx b/unotools/source/config/configmgr.cxx new file mode 100644 index 000000000..610246295 --- /dev/null +++ b/unotools/source/config/configmgr.cxx @@ -0,0 +1,186 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <com/sun/star/beans/NamedValue.hpp> +#include <com/sun/star/container/XHierarchicalNameAccess.hpp> +#include <com/sun/star/configuration/theDefaultProvider.hpp> +#include <com/sun/star/lang/XMultiServiceFactory.hpp> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Reference.hxx> +#include <com/sun/star/uno/Sequence.hxx> +#include <i18nlangtag/languagetag.hxx> +#include <officecfg/Setup.hxx> +#include <rtl/instance.hxx> +#include <rtl/ustring.hxx> +#include <sal/log.hxx> +#include <unotools/configitem.hxx> +#include <unotools/configmgr.hxx> +#include <comphelper/processfactory.hxx> + +namespace { + +class RegisterConfigItemHelper { +public: + RegisterConfigItemHelper( + utl::ConfigManager & manager, utl::ConfigItem & item): + manager_(manager), item_(&item) + { + manager.registerConfigItem(item_); + } + + ~RegisterConfigItemHelper() { + if (item_ != nullptr) { + manager_.removeConfigItem(*item_); + } + } + + void keep() { item_ = nullptr; } + +private: + utl::ConfigManager & manager_; + utl::ConfigItem * item_; + + RegisterConfigItemHelper(const RegisterConfigItemHelper&) = delete; + RegisterConfigItemHelper& operator=(const RegisterConfigItemHelper&) = delete; +}; + +css::uno::Reference< css::lang::XMultiServiceFactory > +getConfigurationProvider() { + return css::configuration::theDefaultProvider::get( comphelper::getProcessComponentContext() ); +} + +struct theConfigManager: + public rtl::Static< utl::ConfigManager, theConfigManager > +{}; + +} + +OUString utl::ConfigManager::getAboutBoxProductVersion() { + return officecfg::Setup::Product::ooSetupVersionAboutBox::get(); +} + +OUString utl::ConfigManager::getAboutBoxProductVersionSuffix() { + return officecfg::Setup::Product::ooSetupVersionAboutBoxSuffix::get(); +} + +OUString utl::ConfigManager::getDefaultCurrency() { + return officecfg::Setup::L10N::ooSetupCurrency::get(); +} + +OUString utl::ConfigManager::getUILocale() { + return officecfg::Setup::L10N::ooLocale::get(); +} + +OUString utl::ConfigManager::getWorkLocale() { + return officecfg::Setup::L10N::ooSetupSystemLocale::get(); +} + +OUString utl::ConfigManager::getProductExtension() { + return officecfg::Setup::Product::ooSetupExtension::get(); +} + +OUString utl::ConfigManager::getProductName() { + return officecfg::Setup::Product::ooName::get(); +} + +OUString utl::ConfigManager::getProductVersion() { + return officecfg::Setup::Product::ooSetupVersion::get(); +} + +OUString utl::ConfigManager::getVendor() { + return officecfg::Setup::Product::ooVendor::get(); +} + +void utl::ConfigManager::storeConfigItems() { + getConfigManager().doStoreConfigItems(); +} + +utl::ConfigManager & utl::ConfigManager::getConfigManager() { + return theConfigManager::get(); +} + +css::uno::Reference< css::container::XHierarchicalNameAccess > +utl::ConfigManager::acquireTree(utl::ConfigItem const & item) { + css::uno::Sequence< css::uno::Any > args(1); + args[0] <<= css::beans::NamedValue( + "nodepath", + css::uno::makeAny("/org.openoffice." + item.GetSubTreeName())); + if (item.GetMode() & ConfigItemMode::AllLocales) { + args.realloc(2); + args[1] <<= css::beans::NamedValue("locale", css::uno::makeAny(OUString("*"))); + } + return css::uno::Reference< css::container::XHierarchicalNameAccess >( + getConfigurationProvider()->createInstanceWithArguments( + "com.sun.star.configuration.ConfigurationUpdateAccess", + args), + css::uno::UNO_QUERY_THROW); +} + +utl::ConfigManager::ConfigManager() {} + +utl::ConfigManager::~ConfigManager() { + SAL_WARN_IF(!items_.empty(), "unotools.config", "ConfigManager not empty"); +} + +css::uno::Reference< css::container::XHierarchicalNameAccess > +utl::ConfigManager::addConfigItem(utl::ConfigItem & item) { + RegisterConfigItemHelper reg(*this, item); + css::uno::Reference< css::container::XHierarchicalNameAccess > tree( + acquireTree(item)); + reg.keep(); + return tree; +} + +void utl::ConfigManager::removeConfigItem(utl::ConfigItem & item) { + items_.erase(std::remove(items_.begin(), items_.end(), &item), items_.end()); +} + +void utl::ConfigManager::registerConfigItem(utl::ConfigItem * item) { + assert(item != nullptr); + items_.push_back(item); +} + +void utl::ConfigManager::doStoreConfigItems() { + for (auto const& item : items_) + { + if (item->IsModified()) { + item->Commit(); + item->ClearModified(); + } + } +} + +static bool bIsFuzzing = false; + +#if !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) +bool utl::ConfigManager::IsFuzzing() +{ + return bIsFuzzing; +} +#endif + +void utl::ConfigManager::EnableFuzzing() +{ + bIsFuzzing = true; + LanguageTag::disable_lt_tag_parse(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/confignode.cxx b/unotools/source/config/confignode.cxx new file mode 100644 index 000000000..59c5fbba8 --- /dev/null +++ b/unotools/source/config/confignode.cxx @@ -0,0 +1,578 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unotools/confignode.hxx> +#include <unotools/configpaths.hxx> +#include <tools/diagnose_ex.h> +#include <osl/diagnose.h> +#include <sal/log.hxx> +#include <com/sun/star/configuration/theDefaultProvider.hpp> +#include <com/sun/star/lang/XSingleServiceFactory.hpp> +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/util/XChangesBatch.hpp> +#include <com/sun/star/util/XStringEscape.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/container/XNamed.hpp> +#include <com/sun/star/container/XNameContainer.hpp> +#include <com/sun/star/container/XHierarchicalNameAccess.hpp> +#include <comphelper/namedvaluecollection.hxx> + +namespace utl +{ + + using namespace ::com::sun::star::uno; + using namespace ::com::sun::star::lang; + using namespace ::com::sun::star::util; + using namespace ::com::sun::star::beans; + using namespace ::com::sun::star::container; + using namespace ::com::sun::star::configuration; + + //= OConfigurationNode + + OConfigurationNode::OConfigurationNode(const Reference< XInterface >& _rxNode ) + :m_bEscapeNames(false) + { + OSL_ENSURE(_rxNode.is(), "OConfigurationNode::OConfigurationNode: invalid node interface!"); + if (_rxNode.is()) + { + // collect all interfaces necessary + m_xHierarchyAccess.set(_rxNode, UNO_QUERY); + m_xDirectAccess.set(_rxNode, UNO_QUERY); + + // reset _all_ interfaces if _one_ of them is not supported + if (!m_xHierarchyAccess.is() || !m_xDirectAccess.is()) + { + m_xHierarchyAccess = nullptr; + m_xDirectAccess = nullptr; + } + + // now for the non-critical interfaces + m_xReplaceAccess.set(_rxNode, UNO_QUERY); + m_xContainerAccess.set(_rxNode, UNO_QUERY); + } + + Reference< XComponent > xConfigNodeComp(m_xDirectAccess, UNO_QUERY); + if (xConfigNodeComp.is()) + startComponentListening(xConfigNodeComp); + + if (isValid()) + m_bEscapeNames = isSetNode() && Reference< XStringEscape >::query(m_xDirectAccess).is(); + } + + OConfigurationNode::OConfigurationNode(const OConfigurationNode& _rSource) + : OEventListenerAdapter() + , m_xHierarchyAccess(_rSource.m_xHierarchyAccess) + , m_xDirectAccess(_rSource.m_xDirectAccess) + , m_xReplaceAccess(_rSource.m_xReplaceAccess) + , m_xContainerAccess(_rSource.m_xContainerAccess) + , m_bEscapeNames(_rSource.m_bEscapeNames) + { + Reference< XComponent > xConfigNodeComp(m_xDirectAccess, UNO_QUERY); + if (xConfigNodeComp.is()) + startComponentListening(xConfigNodeComp); + } + + OConfigurationNode::OConfigurationNode(OConfigurationNode&& _rSource) + : OEventListenerAdapter() + , m_xHierarchyAccess(std::move(_rSource.m_xHierarchyAccess)) + , m_xDirectAccess(std::move(_rSource.m_xDirectAccess)) + , m_xReplaceAccess(std::move(_rSource.m_xReplaceAccess)) + , m_xContainerAccess(std::move(_rSource.m_xContainerAccess)) + , m_bEscapeNames(std::move(_rSource.m_bEscapeNames)) + { + Reference< XComponent > xConfigNodeComp(m_xDirectAccess, UNO_QUERY); + if (xConfigNodeComp.is()) + startComponentListening(xConfigNodeComp); + } + + OConfigurationNode& OConfigurationNode::operator=(const OConfigurationNode& _rSource) + { + stopAllComponentListening(); + + m_xHierarchyAccess = _rSource.m_xHierarchyAccess; + m_xDirectAccess = _rSource.m_xDirectAccess; + m_xContainerAccess = _rSource.m_xContainerAccess; + m_xReplaceAccess = _rSource.m_xReplaceAccess; + m_bEscapeNames = _rSource.m_bEscapeNames; + + Reference< XComponent > xConfigNodeComp(m_xDirectAccess, UNO_QUERY); + if (xConfigNodeComp.is()) + startComponentListening(xConfigNodeComp); + + return *this; + } + + OConfigurationNode& OConfigurationNode::operator=(OConfigurationNode&& _rSource) + { + stopAllComponentListening(); + + m_xHierarchyAccess = std::move(_rSource.m_xHierarchyAccess); + m_xDirectAccess = std::move(_rSource.m_xDirectAccess); + m_xContainerAccess = std::move(_rSource.m_xContainerAccess); + m_xReplaceAccess = std::move(_rSource.m_xReplaceAccess); + m_bEscapeNames = std::move(_rSource.m_bEscapeNames); + + Reference< XComponent > xConfigNodeComp(m_xDirectAccess, UNO_QUERY); + if (xConfigNodeComp.is()) + startComponentListening(xConfigNodeComp); + + return *this; + } + + void OConfigurationNode::_disposing( const EventObject& _rSource ) + { + Reference< XComponent > xDisposingSource(_rSource.Source, UNO_QUERY); + Reference< XComponent > xConfigNodeComp(m_xDirectAccess, UNO_QUERY); + if (xDisposingSource.get() == xConfigNodeComp.get()) + clear(); + } + + OUString OConfigurationNode::getLocalName() const + { + OUString sLocalName; + try + { + Reference< XNamed > xNamed( m_xDirectAccess, UNO_QUERY_THROW ); + sLocalName = xNamed->getName(); + } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION("unotools"); + } + return sLocalName; + } + + OUString OConfigurationNode::normalizeName(const OUString& _rName, NAMEORIGIN _eOrigin) const + { + OUString sName(_rName); + if (m_bEscapeNames) + { + Reference< XStringEscape > xEscaper(m_xDirectAccess, UNO_QUERY); + if (xEscaper.is() && !sName.isEmpty()) + { + try + { + if (NO_CALLER == _eOrigin) + sName = xEscaper->escapeString(sName); + else + sName = xEscaper->unescapeString(sName); + } + catch(Exception&) + { + DBG_UNHANDLED_EXCEPTION("unotools"); + } + } + } + return sName; + } + + Sequence< OUString > OConfigurationNode::getNodeNames() const throw() + { + OSL_ENSURE(m_xDirectAccess.is(), "OConfigurationNode::getNodeNames: object is invalid!"); + Sequence< OUString > aReturn; + if (m_xDirectAccess.is()) + { + try + { + aReturn = m_xDirectAccess->getElementNames(); + // normalize the names + std::transform(aReturn.begin(), aReturn.end(), aReturn.begin(), + [this](const OUString& rName) -> OUString { return normalizeName(rName, NO_CONFIGURATION); }); + } + catch(Exception&) + { + OSL_FAIL("OConfigurationNode::getNodeNames: caught a generic exception!"); + } + } + + return aReturn; + } + + bool OConfigurationNode::removeNode(const OUString& _rName) const throw() + { + OSL_ENSURE(m_xContainerAccess.is(), "OConfigurationNode::removeNode: object is invalid!"); + if (m_xContainerAccess.is()) + { + try + { + OUString sName = normalizeName(_rName, NO_CALLER); + m_xContainerAccess->removeByName(sName); + return true; + } + catch (NoSuchElementException&) + { + SAL_WARN( "unotools", "OConfigurationNode::removeNode: there is no element named: " << _rName ); + } + catch (WrappedTargetException&) + { + OSL_FAIL("OConfigurationNode::removeNode: caught a WrappedTargetException!"); + } + catch(Exception&) + { + OSL_FAIL("OConfigurationNode::removeNode: caught a generic exception!"); + } + } + return false; + } + + OConfigurationNode OConfigurationNode::insertNode(const OUString& _rName,const Reference< XInterface >& _xNode) const throw() + { + if(_xNode.is()) + { + try + { + OUString sName = normalizeName(_rName, NO_CALLER); + m_xContainerAccess->insertByName(sName, makeAny(_xNode)); + // if we're here, all was ok ... + return OConfigurationNode( _xNode ); + } + catch(const Exception&) + { + DBG_UNHANDLED_EXCEPTION("unotools"); + } + + // dispose the child if it has already been created, but could not be inserted + Reference< XComponent > xChildComp(_xNode, UNO_QUERY); + if (xChildComp.is()) + try { xChildComp->dispose(); } catch(Exception&) { } + } + + return OConfigurationNode(); + } + + OConfigurationNode OConfigurationNode::createNode(const OUString& _rName) const throw() + { + Reference< XSingleServiceFactory > xChildFactory(m_xContainerAccess, UNO_QUERY); + OSL_ENSURE(xChildFactory.is(), "OConfigurationNode::createNode: object is invalid or read-only!"); + + if (xChildFactory.is()) // implies m_xContainerAccess.is() + { + Reference< XInterface > xNewChild; + try + { + xNewChild = xChildFactory->createInstance(); + } + catch(const Exception&) + { + DBG_UNHANDLED_EXCEPTION("unotools"); + } + return insertNode(_rName,xNewChild); + } + + return OConfigurationNode(); + } + + OConfigurationNode OConfigurationNode::openNode(const OUString& _rPath) const throw() + { + OSL_ENSURE(m_xDirectAccess.is(), "OConfigurationNode::openNode: object is invalid!"); + OSL_ENSURE(m_xHierarchyAccess.is(), "OConfigurationNode::openNode: object is invalid!"); + try + { + OUString sNormalized = normalizeName(_rPath, NO_CALLER); + + Reference< XInterface > xNode; + if (m_xDirectAccess.is() && m_xDirectAccess->hasByName(sNormalized)) + { + xNode.set( + m_xDirectAccess->getByName(sNormalized), css::uno::UNO_QUERY); + if (!xNode.is()) + OSL_FAIL("OConfigurationNode::openNode: could not open the node!"); + } + else if (m_xHierarchyAccess.is()) + { + xNode.set( + m_xHierarchyAccess->getByHierarchicalName(_rPath), + css::uno::UNO_QUERY); + if (!xNode.is()) + OSL_FAIL("OConfigurationNode::openNode: could not open the node!"); + } + if (xNode.is()) + return OConfigurationNode( xNode ); + } + catch(const NoSuchElementException&) + { + SAL_WARN( "unotools", "OConfigurationNode::openNode: there is no element named " << _rPath ); + } + catch(Exception&) + { + OSL_FAIL("OConfigurationNode::openNode: caught an exception while retrieving the node!"); + } + return OConfigurationNode(); + } + + bool OConfigurationNode::isSetNode() const + { + bool bIsSet = false; + Reference< XServiceInfo > xSI(m_xHierarchyAccess, UNO_QUERY); + if (xSI.is()) + { + try { bIsSet = xSI->supportsService("com.sun.star.configuration.SetAccess"); } + catch(Exception&) { } + } + return bIsSet; + } + + bool OConfigurationNode::hasByHierarchicalName( const OUString& _rName ) const throw() + { + OSL_ENSURE( m_xHierarchyAccess.is(), "OConfigurationNode::hasByHierarchicalName: no hierarchy access!" ); + try + { + if ( m_xHierarchyAccess.is() ) + { + OUString sName = normalizeName( _rName, NO_CALLER ); + return m_xHierarchyAccess->hasByHierarchicalName( sName ); + } + } + catch(Exception&) + { + } + return false; + } + + bool OConfigurationNode::hasByName(const OUString& _rName) const throw() + { + OSL_ENSURE(m_xDirectAccess.is(), "OConfigurationNode::hasByName: object is invalid!"); + try + { + OUString sName = normalizeName(_rName, NO_CALLER); + if (m_xDirectAccess.is()) + return m_xDirectAccess->hasByName(sName); + } + catch(Exception&) + { + } + return false; + } + + bool OConfigurationNode::setNodeValue(const OUString& _rPath, const Any& _rValue) const throw() + { + bool bResult = false; + + OSL_ENSURE(m_xReplaceAccess.is(), "OConfigurationNode::setNodeValue: object is invalid!"); + if (m_xReplaceAccess.is()) + { + try + { + // check if _rPath is a level-1 path + OUString sNormalizedName = normalizeName(_rPath, NO_CALLER); + if (m_xReplaceAccess->hasByName(sNormalizedName)) + { + m_xReplaceAccess->replaceByName(sNormalizedName, _rValue); + bResult = true; + } + + // check if the name refers to an indirect descendant + else if (m_xHierarchyAccess.is() && m_xHierarchyAccess->hasByHierarchicalName(_rPath)) + { + OSL_ASSERT(!_rPath.isEmpty()); + + OUString sParentPath, sLocalName; + + if ( splitLastFromConfigurationPath(_rPath, sParentPath, sLocalName) ) + { + OConfigurationNode aParentAccess = openNode(sParentPath); + if (aParentAccess.isValid()) + bResult = aParentAccess.setNodeValue(sLocalName, _rValue); + } + else + { + m_xReplaceAccess->replaceByName(sLocalName, _rValue); + bResult = true; + } + } + + } + catch(IllegalArgumentException&) + { + OSL_FAIL("OConfigurationNode::setNodeValue: could not replace the value: caught an IllegalArgumentException!"); + } + catch(NoSuchElementException&) + { + OSL_FAIL("OConfigurationNode::setNodeValue: could not replace the value: caught a NoSuchElementException!"); + } + catch(WrappedTargetException&) + { + OSL_FAIL("OConfigurationNode::setNodeValue: could not replace the value: caught a WrappedTargetException!"); + } + catch(Exception&) + { + OSL_FAIL("OConfigurationNode::setNodeValue: could not replace the value: caught a generic Exception!"); + } + + } + return bResult; + } + + Any OConfigurationNode::getNodeValue(const OUString& _rPath) const throw() + { + OSL_ENSURE(m_xDirectAccess.is(), "OConfigurationNode::hasByName: object is invalid!"); + OSL_ENSURE(m_xHierarchyAccess.is(), "OConfigurationNode::hasByName: object is invalid!"); + Any aReturn; + try + { + OUString sNormalizedPath = normalizeName(_rPath, NO_CALLER); + if (m_xDirectAccess.is() && m_xDirectAccess->hasByName(sNormalizedPath) ) + { + aReturn = m_xDirectAccess->getByName(sNormalizedPath); + } + else if (m_xHierarchyAccess.is()) + { + aReturn = m_xHierarchyAccess->getByHierarchicalName(_rPath); + } + } + catch(const NoSuchElementException&) + { + DBG_UNHANDLED_EXCEPTION("unotools"); + } + return aReturn; + } + + void OConfigurationNode::clear() throw() + { + m_xHierarchyAccess.clear(); + m_xDirectAccess.clear(); + m_xReplaceAccess.clear(); + m_xContainerAccess.clear(); + } + + //= helper + + namespace + { + + Reference< XMultiServiceFactory > lcl_getConfigProvider( const Reference<XComponentContext> & i_rContext ) + { + try + { + Reference< XMultiServiceFactory > xProvider = theDefaultProvider::get( i_rContext ); + return xProvider; + } + catch ( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION("unotools"); + } + return nullptr; + } + + Reference< XInterface > lcl_createConfigurationRoot( const Reference< XMultiServiceFactory >& i_rxConfigProvider, + const OUString& i_rNodePath, const bool i_bUpdatable, const sal_Int32 i_nDepth ) + { + ENSURE_OR_RETURN( i_rxConfigProvider.is(), "invalid provider", nullptr ); + try + { + ::comphelper::NamedValueCollection aArgs; + aArgs.put( "nodepath", i_rNodePath ); + aArgs.put( "depth", i_nDepth ); + + OUString sAccessService( i_bUpdatable ? + OUString( "com.sun.star.configuration.ConfigurationUpdateAccess" ) : + OUString( "com.sun.star.configuration.ConfigurationAccess" )); + + Reference< XInterface > xRoot( + i_rxConfigProvider->createInstanceWithArguments( sAccessService, aArgs.getWrappedPropertyValues() ), + UNO_SET_THROW + ); + return xRoot; + } + catch ( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION("unotools"); + } + return nullptr; + } + } + + OConfigurationTreeRoot::OConfigurationTreeRoot( const Reference< XInterface >& _rxRootNode ) + :OConfigurationNode( _rxRootNode ) + ,m_xCommitter( _rxRootNode, UNO_QUERY ) + { + } + + OConfigurationTreeRoot::OConfigurationTreeRoot( const Reference<XComponentContext> & i_rContext, const OUString& i_rNodePath, const bool i_bUpdatable ) + :OConfigurationNode( lcl_createConfigurationRoot( lcl_getConfigProvider( i_rContext ), + i_rNodePath, i_bUpdatable, -1 ).get() ) + ,m_xCommitter() + { + if ( i_bUpdatable ) + { + m_xCommitter.set( getUNONode(), UNO_QUERY ); + OSL_ENSURE( m_xCommitter.is(), "OConfigurationTreeRoot::OConfigurationTreeRoot: could not create an updatable node!" ); + } + } + + void OConfigurationTreeRoot::clear() throw() + { + OConfigurationNode::clear(); + m_xCommitter.clear(); + } + + bool OConfigurationTreeRoot::commit() const throw() + { + OSL_ENSURE(isValid(), "OConfigurationTreeRoot::commit: object is invalid!"); + if (!isValid()) + return false; + OSL_ENSURE(m_xCommitter.is(), "OConfigurationTreeRoot::commit: I'm a readonly node!"); + if (!m_xCommitter.is()) + return false; + + try + { + m_xCommitter->commitChanges(); + return true; + } + catch(const Exception&) + { + DBG_UNHANDLED_EXCEPTION("unotools"); + } + return false; + } + + OConfigurationTreeRoot OConfigurationTreeRoot::createWithProvider(const Reference< XMultiServiceFactory >& _rxConfProvider, const OUString& _rPath, sal_Int32 _nDepth, CREATION_MODE _eMode) + { + Reference< XInterface > xRoot( lcl_createConfigurationRoot( + _rxConfProvider, _rPath, _eMode != CM_READONLY, _nDepth ) ); + if ( xRoot.is() ) + return OConfigurationTreeRoot( xRoot ); + return OConfigurationTreeRoot(); + } + + OConfigurationTreeRoot OConfigurationTreeRoot::createWithComponentContext( const Reference< XComponentContext >& _rxContext, const OUString& _rPath, sal_Int32 _nDepth, CREATION_MODE _eMode ) + { + return createWithProvider( lcl_getConfigProvider( _rxContext ), _rPath, _nDepth, _eMode ); + } + + OConfigurationTreeRoot OConfigurationTreeRoot::tryCreateWithComponentContext( const Reference< XComponentContext >& rxContext, + const OUString& _rPath, sal_Int32 _nDepth , CREATION_MODE _eMode ) + { + OSL_ENSURE( rxContext.is(), "OConfigurationTreeRoot::tryCreateWithComponentContext: invalid XComponentContext!" ); + try + { + Reference< XMultiServiceFactory > xConfigFactory = theDefaultProvider::get( rxContext ); + return createWithProvider( xConfigFactory, _rPath, _nDepth, _eMode ); + } + catch(const Exception&) + { + // silence this, 'cause the contract of this method states "no assertions" + } + return OConfigurationTreeRoot(); + } + +} // namespace utl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/configpaths.cxx b/unotools/source/config/configpaths.cxx new file mode 100644 index 000000000..81d8cb828 --- /dev/null +++ b/unotools/source/config/configpaths.cxx @@ -0,0 +1,284 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <string_view> + +#include <unotools/configpaths.hxx> +#include <rtl/ustring.hxx> +#include <rtl/ustrbuf.hxx> +#include <osl/diagnose.h> + +namespace utl +{ + +static +void lcl_resolveCharEntities(OUString & aLocalString) +{ + sal_Int32 nEscapePos=aLocalString.indexOf('&'); + if (nEscapePos < 0) return; + + OUStringBuffer aResult; + sal_Int32 nStart = 0; + + do + { + sal_Unicode ch = 0; + if (aLocalString.match("&",nEscapePos)) + ch = '&'; + + else if (aLocalString.match("'",nEscapePos)) + ch = '\''; + + else if (aLocalString.match(""",nEscapePos)) + ch = '"'; + + OSL_ENSURE(ch,"Configuration path contains '&' that is not part of a valid character escape"); + if (ch) + { + aResult.append(std::u16string_view(aLocalString).substr(nStart,nEscapePos-nStart)).append(ch); + + sal_Int32 nEscapeEnd=aLocalString.indexOf(';',nEscapePos); + nStart = nEscapeEnd+1; + nEscapePos=aLocalString.indexOf('&',nStart); + } + else + { + nEscapePos=aLocalString.indexOf('&',nEscapePos+1); + } + } + while ( nEscapePos > 0); + + aResult.append(std::u16string_view(aLocalString).substr(nStart)); + + aLocalString = aResult.makeStringAndClear(); +} + +bool splitLastFromConfigurationPath(OUString const& _sInPath, + OUString& _rsOutPath, + OUString& _rsLocalName) +{ + sal_Int32 nStart,nEnd; + + sal_Int32 nPos = _sInPath.getLength()-1; + + // strip trailing slash + if (nPos > 0 && _sInPath[ nPos ] == '/') + { + OSL_FAIL("Invalid config path: trailing '/' is not allowed"); + --nPos; + } + + // check for predicate ['xxx'] or ["yyy"] + if (nPos > 0 && _sInPath[ nPos ] == ']') + { + sal_Unicode chQuote = _sInPath[--nPos]; + + if (chQuote == '\'' || chQuote == '\"') + { + nEnd = nPos; + nPos = _sInPath.lastIndexOf(chQuote,nEnd); + nStart = nPos + 1; + --nPos; // nPos = rInPath.lastIndexOf('[',nPos); + } + else // allow [xxx] + { + nEnd = nPos + 1; + nPos = _sInPath.lastIndexOf('[',nEnd); + nStart = nPos + 1; + } + + OSL_ENSURE(nPos >= 0 && _sInPath[nPos] == '[', "Invalid config path: unmatched quotes or brackets"); + if (nPos >= 0 && _sInPath[nPos] == '[') + { + nPos = _sInPath.lastIndexOf('/',nPos); + } + else // defined behavior for invalid paths + { + nStart = 0; + nEnd = _sInPath.getLength(); + nPos = -1; + } + + } + else + { + nEnd = nPos+1; + nPos = _sInPath.lastIndexOf('/',nEnd); + nStart = nPos + 1; + } + OSL_ASSERT( -1 <= nPos && + nPos < nStart && + nStart < nEnd && + nEnd <= _sInPath.getLength() ); + + OSL_ASSERT(nPos == -1 || _sInPath[nPos] == '/'); + OSL_ENSURE(nPos != 0 , "Invalid config child path: immediate child of root"); + + _rsLocalName = _sInPath.copy(nStart, nEnd-nStart); + _rsOutPath = (nPos > 0) ? _sInPath.copy(0,nPos) : OUString(); + lcl_resolveCharEntities(_rsLocalName); + + return nPos >= 0; +} + +OUString extractFirstFromConfigurationPath(OUString const& _sInPath, OUString* _sOutPath) +{ + sal_Int32 nSep = _sInPath.indexOf('/'); + sal_Int32 nBracket = _sInPath.indexOf('['); + + sal_Int32 nStart = nBracket + 1; + sal_Int32 nEnd = nSep; + + if (0 <= nBracket) // found a bracket-quoted relative path + { + if (nSep < 0 || nBracket < nSep) // and the separator comes after it + { + sal_Unicode chQuote = _sInPath[nStart]; + if (chQuote == '\'' || chQuote == '\"') + { + ++nStart; + nEnd = _sInPath.indexOf(chQuote, nStart+1); + nBracket = nEnd+1; + } + else + { + nEnd = _sInPath.indexOf(']',nStart); + nBracket = nEnd; + } + OSL_ENSURE(nEnd > nStart && _sInPath[nBracket] == ']', "Invalid config path: improper mismatch of quote or bracket"); + OSL_ENSURE((nBracket+1 == _sInPath.getLength() && nSep == -1) || (_sInPath[nBracket+1] == '/' && nSep == nBracket+1), "Invalid config path: brackets not followed by slash"); + } + else // ... but our initial element name is in simple form + nStart = 0; + } + + OUString sResult = (nEnd >= 0) ? _sInPath.copy(nStart, nEnd-nStart) : _sInPath; + lcl_resolveCharEntities(sResult); + + if (_sOutPath != nullptr) + { + *_sOutPath = (nSep >= 0) ? _sInPath.copy(nSep + 1) : OUString(); + } + + return sResult; +} + +// find the position after the prefix in the nested path +static sal_Int32 lcl_findPrefixEnd(OUString const& _sNestedPath, OUString const& _sPrefixPath) +{ + // TODO: currently handles only exact prefix matches + sal_Int32 nPrefixLength = _sPrefixPath.getLength(); + + OSL_ENSURE(nPrefixLength == 0 || _sPrefixPath[nPrefixLength-1] != '/', + "Cannot handle slash-terminated prefix paths"); + + bool bIsPrefix; + if (_sNestedPath.getLength() > nPrefixLength) + { + bIsPrefix = _sNestedPath[nPrefixLength] == '/' && + _sNestedPath.startsWith(_sPrefixPath); + ++nPrefixLength; + } + else if (_sNestedPath.getLength() == nPrefixLength) + { + bIsPrefix = _sNestedPath == _sPrefixPath; + } + else + { + bIsPrefix = false; + } + + return bIsPrefix ? nPrefixLength : 0; +} + +bool isPrefixOfConfigurationPath(OUString const& _sNestedPath, + OUString const& _sPrefixPath) +{ + return _sPrefixPath.isEmpty() || lcl_findPrefixEnd(_sNestedPath,_sPrefixPath) != 0; +} + +OUString dropPrefixFromConfigurationPath(OUString const& _sNestedPath, + OUString const& _sPrefixPath) +{ + if ( sal_Int32 nPrefixEnd = lcl_findPrefixEnd(_sNestedPath,_sPrefixPath) ) + { + return _sNestedPath.copy(nPrefixEnd); + } + else + { + OSL_ENSURE(_sPrefixPath.isEmpty(), "Path does not start with expected prefix"); + + return _sNestedPath; + } +} + +static +OUString lcl_wrapName(const OUString& _sContent, const OUString& _sType) +{ + const sal_Unicode * const pBeginContent = _sContent.getStr(); + const sal_Unicode * const pEndContent = pBeginContent + _sContent.getLength(); + + OSL_PRECOND(!_sType.isEmpty(), "Unexpected config type name: empty"); + OSL_PRECOND(pBeginContent <= pEndContent, "Invalid config name: empty"); + + if (pBeginContent == pEndContent) + return _sType; + + OUStringBuffer aNormalized(_sType.getLength() + _sContent.getLength() + 4); // reserve approximate size initially + + // prefix: type, opening bracket and quote + aNormalized.append( _sType ).append( "['" ); + + // content: copy over each char and handle escaping + for(const sal_Unicode* pCur = pBeginContent; pCur != pEndContent; ++pCur) + { + // append (escape if needed) + switch(*pCur) + { + case u'&' : aNormalized.append( "&" ); break; + case u'\'': aNormalized.append( "'" ); break; + case u'\"': aNormalized.append( """ ); break; + + default: aNormalized.append( *pCur ); + } + } + + // suffix: closing quote and bracket + aNormalized.append( "']" ); + + return aNormalized.makeStringAndClear(); +} + +OUString wrapConfigurationElementName(OUString const& _sElementName) +{ + return lcl_wrapName(_sElementName, "*" ); +} + +OUString wrapConfigurationElementName(OUString const& _sElementName, + OUString const& _sTypeName) +{ + // todo: check that _sTypeName is valid + return lcl_wrapName(_sElementName, _sTypeName); +} + +} // namespace utl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/configvaluecontainer.cxx b/unotools/source/config/configvaluecontainer.cxx new file mode 100644 index 000000000..6fc60d753 --- /dev/null +++ b/unotools/source/config/configvaluecontainer.cxx @@ -0,0 +1,303 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <sal/log.hxx> +#include <unotools/configvaluecontainer.hxx> +#include <unotools/confignode.hxx> +#include <uno/data.h> +#include <algorithm> +#include <vector> + +namespace utl +{ + + using namespace ::com::sun::star::uno; + using namespace ::com::sun::star::lang; + + //= NodeValueAccessor + + namespace { + + enum class LocationType + { + SimplyObjectInstance, + Unbound + }; + + } + + struct NodeValueAccessor + { + private: + OUString sRelativePath; // the relative path of the node + LocationType eLocationType; // the type of location where the value is stored + void* pLocation; // the pointer to the location + Type aDataType; // the type object pointed to by pLocation + + public: + explicit NodeValueAccessor( const OUString& _rNodePath ); + + void bind( void* _pLocation, const Type& _rType ); + + bool isBound( ) const { return ( LocationType::Unbound != eLocationType ) && ( nullptr != pLocation ); } + const OUString& getPath( ) const { return sRelativePath; } + LocationType getLocType( ) const { return eLocationType; } + void* getLocation( ) const { return pLocation; } + const Type& getDataType( ) const { return aDataType; } + + bool operator == ( const NodeValueAccessor& rhs ) const; + }; + + NodeValueAccessor::NodeValueAccessor( const OUString& _rNodePath ) + :sRelativePath( _rNodePath ) + ,eLocationType( LocationType::Unbound ) + ,pLocation( nullptr ) + { + } + + bool NodeValueAccessor::operator == ( const NodeValueAccessor& rhs ) const + { + return ( sRelativePath == rhs.sRelativePath ) + && ( eLocationType == rhs.eLocationType ) + && ( pLocation == rhs.pLocation ); + } + + void NodeValueAccessor::bind( void* _pLocation, const Type& _rType ) + { + SAL_WARN_IF(isBound(), "unotools.config", "NodeValueAccessor::bind: already bound!"); + + eLocationType = LocationType::SimplyObjectInstance; + pLocation = _pLocation; + aDataType = _rType; + } + + static + void lcl_copyData( const NodeValueAccessor& _rAccessor, const Any& _rData, ::osl::Mutex& _rMutex ) + { + ::osl::MutexGuard aGuard( _rMutex ); + + SAL_WARN_IF(!_rAccessor.isBound(), "unotools.config", "::utl::lcl_copyData: invalid accessor!"); + switch ( _rAccessor.getLocType() ) + { + case LocationType::SimplyObjectInstance: + { + if ( _rData.hasValue() ) + { + // assign the value + bool bSuccess = uno_type_assignData( + _rAccessor.getLocation(), _rAccessor.getDataType().getTypeLibType(), + const_cast< void* >( _rData.getValue() ), _rData.getValueType().getTypeLibType(), + cpp_queryInterface, cpp_acquire, cpp_release + ); + SAL_WARN_IF(!bSuccess, "unotools.config", + "::utl::lcl_copyData( Accessor, Any ): could not assign the data (node path: \"" << _rAccessor.getPath() << "\""); + } + else { + SAL_INFO("unotools.config", "::utl::lcl_copyData: NULL value lost!"); + } + } + break; + default: + break; + } + } + + static + void lcl_copyData( Any& _rData, const NodeValueAccessor& _rAccessor, ::osl::Mutex& _rMutex ) + { + ::osl::MutexGuard aGuard( _rMutex ); + + SAL_WARN_IF(!_rAccessor.isBound(), "unotools.config", "::utl::lcl_copyData: invalid accessor!" ); + switch ( _rAccessor.getLocType() ) + { + case LocationType::SimplyObjectInstance: + // a simple setValue... + _rData.setValue( _rAccessor.getLocation(), _rAccessor.getDataType() ); + break; + + default: + break; + } + } + + //= functors on NodeValueAccessor instances + + namespace { + + /// base class for functors synchronizing between exchange locations and config sub nodes + struct SubNodeAccess + { + protected: + const OConfigurationNode& m_rRootNode; + ::osl::Mutex& m_rMutex; + + public: + SubNodeAccess( const OConfigurationNode& _rRootNode, ::osl::Mutex& _rMutex ) + :m_rRootNode( _rRootNode ) + ,m_rMutex( _rMutex ) + { + } + }; + + struct UpdateFromConfig : public SubNodeAccess + { + public: + UpdateFromConfig( const OConfigurationNode& _rRootNode, ::osl::Mutex& _rMutex ) : SubNodeAccess( _rRootNode, _rMutex ) { } + + void operator() ( NodeValueAccessor const & _rAccessor ) + { + ::utl::lcl_copyData( _rAccessor, m_rRootNode.getNodeValue( _rAccessor.getPath( ) ), m_rMutex ); + } + }; + + struct UpdateToConfig : public SubNodeAccess + { + public: + UpdateToConfig( const OConfigurationNode& _rRootNode, ::osl::Mutex& _rMutex ) : SubNodeAccess( _rRootNode, _rMutex ) { } + + void operator() ( NodeValueAccessor const & _rAccessor ) + { + Any aNewValue; + lcl_copyData( aNewValue, _rAccessor, m_rMutex ); + m_rRootNode.setNodeValue( _rAccessor.getPath( ), aNewValue ); + } + }; + + } + + //= OConfigurationValueContainerImpl + + struct OConfigurationValueContainerImpl + { + Reference< XComponentContext > xORB; // the service factory + ::osl::Mutex& rMutex; // the mutex for accessing the data containers + OConfigurationTreeRoot aConfigRoot; // the configuration node we're accessing + + std::vector<NodeValueAccessor> aAccessors; // the accessors to the node values + + OConfigurationValueContainerImpl( const Reference< XComponentContext >& _rxORB, ::osl::Mutex& _rMutex ) + :xORB( _rxORB ) + ,rMutex( _rMutex ) + { + } + }; + + //= OConfigurationValueContainer + + OConfigurationValueContainer::OConfigurationValueContainer( + const Reference< XComponentContext >& _rxORB, ::osl::Mutex& _rAccessSafety, + const char* _pConfigLocation, const sal_Int32 _nLevels ) + :m_pImpl( new OConfigurationValueContainerImpl( _rxORB, _rAccessSafety ) ) + { + implConstruct( OUString::createFromAscii( _pConfigLocation ), _nLevels ); + } + + OConfigurationValueContainer::~OConfigurationValueContainer() + { + } + + void OConfigurationValueContainer::implConstruct( const OUString& _rConfigLocation, + const sal_Int32 _nLevels ) + { + SAL_WARN_IF(m_pImpl->aConfigRoot.isValid(), "unotools.config", "OConfigurationValueContainer::implConstruct: already initialized!"); + + // create the configuration node we're about to work with + m_pImpl->aConfigRoot = OConfigurationTreeRoot::createWithComponentContext( + m_pImpl->xORB, + _rConfigLocation, + _nLevels + ); + SAL_WARN_IF(!m_pImpl->aConfigRoot.isValid(), "unotools.config", + "Could not access the configuration node located at " << _rConfigLocation); + } + + void OConfigurationValueContainer::registerExchangeLocation( const char* _pRelativePath, + void* _pContainer, const Type& _rValueType ) + { + // checks... + SAL_WARN_IF(!_pContainer, "unotools.config", + "OConfigurationValueContainer::registerExchangeLocation: invalid container location!"); + SAL_WARN_IF(!( (TypeClass_CHAR == _rValueType.getTypeClass( ) ) + || ( TypeClass_BOOLEAN == _rValueType.getTypeClass( ) ) + || ( TypeClass_BYTE == _rValueType.getTypeClass( ) ) + || ( TypeClass_SHORT == _rValueType.getTypeClass( ) ) + || ( TypeClass_LONG == _rValueType.getTypeClass( ) ) + || ( TypeClass_DOUBLE == _rValueType.getTypeClass( ) ) + || ( TypeClass_STRING == _rValueType.getTypeClass( ) ) + || ( TypeClass_SEQUENCE == _rValueType.getTypeClass( ) )), + "unotools.config", + "OConfigurationValueContainer::registerExchangeLocation: invalid type!" ); + + // build an accessor for this container + NodeValueAccessor aNewAccessor( OUString::createFromAscii( _pRelativePath ) ); + aNewAccessor.bind( _pContainer, _rValueType ); + + // insert it into our structure + implRegisterExchangeLocation( aNewAccessor ); + } + + void OConfigurationValueContainer::read( ) + { + std::for_each( + m_pImpl->aAccessors.begin(), + m_pImpl->aAccessors.end(), + UpdateFromConfig( m_pImpl->aConfigRoot, m_pImpl->rMutex ) + ); + } + + void OConfigurationValueContainer::commit() + { + // write the current values in the exchange locations + std::for_each( + m_pImpl->aAccessors.begin(), + m_pImpl->aAccessors.end(), + UpdateToConfig( m_pImpl->aConfigRoot, m_pImpl->rMutex ) + ); + + // commit the changes done + m_pImpl->aConfigRoot.commit( ); + } + + void OConfigurationValueContainer::implRegisterExchangeLocation( const NodeValueAccessor& _rAccessor ) + { + // some checks + SAL_WARN_IF(m_pImpl->aConfigRoot.isValid() && !m_pImpl->aConfigRoot.hasByHierarchicalName(_rAccessor.getPath()), + "unotools.config", + "OConfigurationValueContainer::implRegisterExchangeLocation: invalid relative path!" ); + + // another check (should be the first container for this node) + SAL_WARN_IF(!(m_pImpl->aAccessors.end() == ::std::find( + m_pImpl->aAccessors.begin(), + m_pImpl->aAccessors.end(), + _rAccessor)), + "unotools.config", + "OConfigurationValueContainer::implRegisterExchangeLocation: already registered a container for this subnode!" ); + + // remember the accessor + m_pImpl->aAccessors.push_back( _rAccessor ); + + // and initially fill the value + lcl_copyData( _rAccessor, m_pImpl->aConfigRoot.getNodeValue( _rAccessor.getPath() ), m_pImpl->rMutex ); + } + +} // namespace utl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/defaultoptions.cxx b/unotools/source/config/defaultoptions.cxx new file mode 100644 index 000000000..80a597abc --- /dev/null +++ b/unotools/source/config/defaultoptions.cxx @@ -0,0 +1,357 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <osl/file.hxx> +#include <sal/log.hxx> +#include <unotools/defaultoptions.hxx> +#include <unotools/pathoptions.hxx> +#include <unotools/configitem.hxx> +#include <tools/debug.hxx> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Sequence.hxx> +#include <osl/mutex.hxx> + +#include <rtl/instance.hxx> +#include <rtl/ustrbuf.hxx> + +#include "itemholder1.hxx" + +using namespace osl; +using namespace utl; +using namespace com::sun::star::uno; + +#define DEFAULTPATH_ADDIN 0 +#define DEFAULTPATH_AUTOCORRECT 1 +#define DEFAULTPATH_AUTOTEXT 2 +#define DEFAULTPATH_BACKUP 3 +#define DEFAULTPATH_BASIC 4 +#define DEFAULTPATH_BITMAP 5 +#define DEFAULTPATH_CONFIG 6 +#define DEFAULTPATH_DICTIONARY 7 +#define DEFAULTPATH_FAVORITES 8 +#define DEFAULTPATH_FILTER 9 +#define DEFAULTPATH_GALLERY 10 +#define DEFAULTPATH_GRAPHIC 11 +#define DEFAULTPATH_HELP 12 +#define DEFAULTPATH_LINGUISTIC 13 +#define DEFAULTPATH_MODULE 14 +#define DEFAULTPATH_PALETTE 15 +#define DEFAULTPATH_PLUGIN 16 +#define DEFAULTPATH_TEMP 17 +#define DEFAULTPATH_TEMPLATE 18 +#define DEFAULTPATH_USERCONFIG 19 +#define DEFAULTPATH_WORK 20 +#define DEFAULTPATH_CLASSIFICATION 21 +#define DEFAULTPATH_USERDICTIONARY 22 + +// class SvtDefaultOptions_Impl ------------------------------------------ + +class SvtDefaultOptions_Impl : public utl::ConfigItem +{ +public: + OUString m_aAddinPath; + OUString m_aAutoCorrectPath; + OUString m_aAutoTextPath; + OUString m_aBackupPath; + OUString m_aBasicPath; + OUString m_aBitmapPath; + OUString m_aConfigPath; + OUString m_aDictionaryPath; + OUString m_aFavoritesPath; + OUString m_aFilterPath; + OUString m_aGalleryPath; + OUString m_aGraphicPath; + OUString m_aHelpPath; + OUString m_aLinguisticPath; + OUString m_aModulePath; + OUString m_aPalettePath; + OUString m_aPluginPath; + OUString m_aTempPath; + OUString m_aTemplatePath; + OUString m_aUserConfigPath; + OUString m_aWorkPath; + OUString m_aClassificationPath; + + SvtDefaultOptions_Impl(); + virtual ~SvtDefaultOptions_Impl() override; + + OUString GetDefaultPath( sal_uInt16 nId ) const; + virtual void Notify( const css::uno::Sequence<OUString>& aPropertyNames) override; + +private: + virtual void ImplCommit() final override; +}; + +// global ---------------------------------------------------------------- + +namespace { + +std::weak_ptr<SvtDefaultOptions_Impl> g_pOptions; + +} + +typedef OUString SvtDefaultOptions_Impl:: *PathStrPtr; + +namespace { + +struct PathToDefaultMapping_Impl +{ + SvtPathOptions::Paths _ePath; + PathStrPtr _pDefaultPath; +}; + +} + +static PathToDefaultMapping_Impl const PathMap_Impl[] = +{ + { SvtPathOptions::PATH_ADDIN, &SvtDefaultOptions_Impl::m_aAddinPath }, + { SvtPathOptions::PATH_AUTOCORRECT, &SvtDefaultOptions_Impl::m_aAutoCorrectPath }, + { SvtPathOptions::PATH_AUTOTEXT, &SvtDefaultOptions_Impl::m_aAutoTextPath }, + { SvtPathOptions::PATH_BACKUP, &SvtDefaultOptions_Impl::m_aBackupPath }, + { SvtPathOptions::PATH_BASIC, &SvtDefaultOptions_Impl::m_aBasicPath }, + { SvtPathOptions::PATH_BITMAP, &SvtDefaultOptions_Impl::m_aBitmapPath }, + { SvtPathOptions::PATH_CONFIG, &SvtDefaultOptions_Impl::m_aConfigPath }, + { SvtPathOptions::PATH_DICTIONARY, &SvtDefaultOptions_Impl::m_aDictionaryPath }, + { SvtPathOptions::PATH_FAVORITES, &SvtDefaultOptions_Impl::m_aFavoritesPath }, + { SvtPathOptions::PATH_FILTER, &SvtDefaultOptions_Impl::m_aFilterPath }, + { SvtPathOptions::PATH_GALLERY, &SvtDefaultOptions_Impl::m_aGalleryPath }, + { SvtPathOptions::PATH_GRAPHIC, &SvtDefaultOptions_Impl::m_aGraphicPath }, + { SvtPathOptions::PATH_HELP, &SvtDefaultOptions_Impl::m_aHelpPath }, + { SvtPathOptions::PATH_LINGUISTIC, &SvtDefaultOptions_Impl::m_aLinguisticPath }, + { SvtPathOptions::PATH_MODULE, &SvtDefaultOptions_Impl::m_aModulePath }, + { SvtPathOptions::PATH_PALETTE, &SvtDefaultOptions_Impl::m_aPalettePath }, + { SvtPathOptions::PATH_PLUGIN, &SvtDefaultOptions_Impl::m_aPluginPath }, + { SvtPathOptions::PATH_TEMP, &SvtDefaultOptions_Impl::m_aTempPath }, + { SvtPathOptions::PATH_TEMPLATE, &SvtDefaultOptions_Impl::m_aTemplatePath }, + { SvtPathOptions::PATH_USERCONFIG, &SvtDefaultOptions_Impl::m_aUserConfigPath }, + { SvtPathOptions::PATH_WORK, &SvtDefaultOptions_Impl::m_aWorkPath }, + { SvtPathOptions::PATH_CLASSIFICATION, &SvtDefaultOptions_Impl::m_aClassificationPath } +}; + +// functions ------------------------------------------------------------- + +static Sequence< OUString > GetDefaultPropertyNames() +{ + static const char* aPropNames[] = + { + "Addin", // PATH_ADDIN + "AutoCorrect", // PATH_AUTOCORRECT + "AutoText", // PATH_AUTOTEXT + "Backup", // PATH_BACKUP + "Basic", // PATH_BASIC + "Bitmap", // PATH_BITMAP + "Config", // PATH_CONFIG + "Dictionary", // PATH_DICTIONARY + "Favorite", // PATH_FAVORITES + "Filter", // PATH_FILTER + "Gallery", // PATH_GALLERY + "Graphic", // PATH_GRAPHIC + "Help", // PATH_HELP + "Linguistic", // PATH_LINGUISTIC + "Module", // PATH_MODULE + "Palette", // PATH_PALETTE + "Plugin", // PATH_PLUGIN + "Temp", // PATH_TEMP + "Template", // PATH_TEMPLATE + "UserConfig", // PATH_USERCONFIG + "Work", // PATH_WORK + "Classification" // PATH_CLASSIFICATION + }; + + const int nCount = SAL_N_ELEMENTS( aPropNames ); + Sequence< OUString > aNames( nCount ); + OUString* pNames = aNames.getArray(); + for ( int i = 0; i < nCount; i++ ) + pNames[i] = OUString::createFromAscii( aPropNames[i] ); + + return aNames; +} + +void SvtDefaultOptions_Impl::Notify( const Sequence< OUString >& ) +{ + // no notification, will never be changed +} + +void SvtDefaultOptions_Impl::ImplCommit() +{ + // will never be changed +} + +// class SvtDefaultOptions_Impl ------------------------------------------ + +OUString SvtDefaultOptions_Impl::GetDefaultPath( sal_uInt16 nId ) const +{ + OUString aRet; + sal_uInt16 nIdx = 0; + + while ( PathMap_Impl[nIdx]._ePath <= SvtPathOptions::PATH_CLASSIFICATION ) + { + if ( nId == PathMap_Impl[nIdx]._ePath && PathMap_Impl[nIdx]._pDefaultPath ) + { + aRet = this->*(PathMap_Impl[nIdx]._pDefaultPath); + if ( nId == SvtPathOptions::PATH_ADDIN || + nId == SvtPathOptions::PATH_FILTER || + nId == SvtPathOptions::PATH_HELP || + nId == SvtPathOptions::PATH_MODULE || + nId == SvtPathOptions::PATH_PLUGIN ) + { + OUString aTmp; + osl::FileBase::getFileURLFromSystemPath( aRet, aTmp ); + aRet = aTmp; + } + + break; + } + ++nIdx; + } + + return aRet; +} + +SvtDefaultOptions_Impl::SvtDefaultOptions_Impl() : ConfigItem( "Office.Common/Path/Default" ) +{ + Sequence< OUString > aNames = GetDefaultPropertyNames(); + Sequence< Any > aValues = GetProperties( aNames ); + EnableNotification( aNames ); + const Any* pValues = aValues.getConstArray(); + DBG_ASSERT( aValues.getLength() == aNames.getLength(), "GetProperties failed" ); + if ( aValues.getLength() == aNames.getLength() ) + { + SvtPathOptions aPathOpt; + OUString aTempStr; + OUStringBuffer aFullPathBuf; + + for ( int nProp = 0; nProp < aNames.getLength(); nProp++ ) + { + if ( pValues[nProp].hasValue() ) + { + switch ( pValues[nProp].getValueTypeClass() ) + { + case css::uno::TypeClass_STRING : + { + // multi paths + if ( pValues[nProp] >>= aTempStr ) + aFullPathBuf = aPathOpt.SubstituteVariable( aTempStr ); + else + { + SAL_WARN( "unotools.config", "any operator >>= failed" ); + } + break; + } + + case css::uno::TypeClass_SEQUENCE : + { + // single paths + aFullPathBuf.setLength(0); + Sequence < OUString > aList; + if ( pValues[nProp] >>= aList ) + { + sal_Int32 nCount = aList.getLength(); + for ( sal_Int32 nPosition = 0; nPosition < nCount; ++nPosition ) + { + aFullPathBuf.append(aPathOpt.SubstituteVariable( aList[ nPosition ] )); + if ( nPosition < nCount-1 ) + aFullPathBuf.append(";"); + } + } + else + { + SAL_WARN( "unotools.config", "any operator >>= failed" ); + } + break; + } + + default: + { + SAL_WARN( "unotools.config", "Wrong any type" ); + } + } + + auto aFullPath = aFullPathBuf.makeStringAndClear(); + switch ( nProp ) + { + case DEFAULTPATH_ADDIN: m_aAddinPath = aFullPath; break; + case DEFAULTPATH_AUTOCORRECT: m_aAutoCorrectPath = aFullPath; break; + case DEFAULTPATH_AUTOTEXT: m_aAutoTextPath = aFullPath; break; + case DEFAULTPATH_BACKUP: m_aBackupPath = aFullPath; break; + case DEFAULTPATH_BASIC: m_aBasicPath = aFullPath; break; + case DEFAULTPATH_BITMAP: m_aBitmapPath = aFullPath; break; + case DEFAULTPATH_CONFIG: m_aConfigPath = aFullPath; break; + case DEFAULTPATH_DICTIONARY: m_aDictionaryPath = aFullPath; break; + case DEFAULTPATH_FAVORITES: m_aFavoritesPath = aFullPath; break; + case DEFAULTPATH_FILTER: m_aFilterPath = aFullPath; break; + case DEFAULTPATH_GALLERY: m_aGalleryPath = aFullPath; break; + case DEFAULTPATH_GRAPHIC: m_aGraphicPath = aFullPath; break; + case DEFAULTPATH_HELP: m_aHelpPath = aFullPath; break; + case DEFAULTPATH_LINGUISTIC: m_aLinguisticPath = aFullPath; break; + case DEFAULTPATH_MODULE: m_aModulePath = aFullPath; break; + case DEFAULTPATH_PALETTE: m_aPalettePath = aFullPath; break; + case DEFAULTPATH_PLUGIN: m_aPluginPath = aFullPath; break; + case DEFAULTPATH_TEMP: m_aTempPath = aFullPath; break; + case DEFAULTPATH_TEMPLATE: m_aTemplatePath = aFullPath; break; + case DEFAULTPATH_USERCONFIG: m_aUserConfigPath = aFullPath; break; + case DEFAULTPATH_WORK: m_aWorkPath = aFullPath; break; + case DEFAULTPATH_CLASSIFICATION: m_aClassificationPath = aFullPath;break; + case DEFAULTPATH_USERDICTIONARY: break; + + default: + SAL_WARN( "unotools.config", "invalid index to load a default path" ); + } + } + } + } +} + +SvtDefaultOptions_Impl::~SvtDefaultOptions_Impl() +{ + if ( IsModified() ) + Commit(); +} + +// class SvtDefaultOptions ----------------------------------------------- +namespace { struct lclMutex : public rtl::Static< ::osl::Mutex, lclMutex > {}; } + +SvtDefaultOptions::SvtDefaultOptions() +{ + // Global access, must be guarded (multithreading) + ::osl::MutexGuard aGuard( lclMutex::get() ); + pImpl = g_pOptions.lock(); + if ( !pImpl ) + { + pImpl = std::make_shared<SvtDefaultOptions_Impl>(); + g_pOptions = pImpl; + ItemHolder1::holdConfigItem(EItem::DefaultOptions); + } +} + +SvtDefaultOptions::~SvtDefaultOptions() +{ + // Global access, must be guarded (multithreading) + ::osl::MutexGuard aGuard( lclMutex::get() ); + pImpl.reset(); +} + +OUString SvtDefaultOptions::GetDefaultPath( sal_uInt16 nId ) const +{ + return pImpl->GetDefaultPath( nId ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/docinfohelper.cxx b/unotools/source/config/docinfohelper.cxx new file mode 100644 index 000000000..7e605d187 --- /dev/null +++ b/unotools/source/config/docinfohelper.cxx @@ -0,0 +1,101 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <rtl/ustrbuf.hxx> +#include <unotools/configmgr.hxx> +#include <unotools/bootstrap.hxx> +#include <unotools/docinfohelper.hxx> +#include <rtl/bootstrap.hxx> + +using namespace ::com::sun::star; + +namespace utl +{ + +OUString DocInfoHelper::GetGeneratorString() +{ + OUStringBuffer aResult(128); + + // First product: branded name + version + // version is <product_versions>_<product_extension>$<platform> + + // plain product name + OUString aValue( utl::ConfigManager::getProductName() ); + if ( !aValue.isEmpty() ) + { + aResult.append( aValue.replace( ' ', '_' ) ); + aResult.append( '/' ); + + aValue = utl::ConfigManager::getProductVersion(); + if ( !aValue.isEmpty() ) + { + aResult.append( aValue.replace( ' ', '_' ) ); + + aValue = utl::ConfigManager::getProductExtension(); + if ( !aValue.isEmpty() ) + { + aResult.append( aValue.replace( ' ', '_' ) ); + } + } + + OUString os( "$_OS" ); + OUString arch( "$_ARCH" ); + ::rtl::Bootstrap::expandMacros(os); + ::rtl::Bootstrap::expandMacros(arch); + aResult.append( '$' ); + aResult.append( os ); + aResult.append( '_' ); + aResult.append( arch ); + aResult.append( ' ' ); + } + + // second product: LibreOffice_project/<build_information> + // build_information has '(' and '[' encoded as '$', ')' and ']' ignored + // and ':' replaced by '-' + { + aResult.append( "LibreOffice_project/" ); + OUString aBuildId( Bootstrap::getBuildIdData( OUString() ) ); + for( sal_Int32 i=0; i < aBuildId.getLength(); i++ ) + { + sal_Unicode c = aBuildId[i]; + switch( c ) + { + case '(': + case '[': + aResult.append( '$' ); + break; + case ')': + case ']': + break; + case ':': + aResult.append( '-' ); + break; + default: + aResult.append( c ); + break; + } + } + } + + return aResult.makeStringAndClear(); +} + +} // end of namespace utl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/dynamicmenuoptions.cxx b/unotools/source/config/dynamicmenuoptions.cxx new file mode 100644 index 000000000..019ca1b86 --- /dev/null +++ b/unotools/source/config/dynamicmenuoptions.cxx @@ -0,0 +1,558 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <sal/log.hxx> +#include <unotools/dynamicmenuoptions.hxx> +#include <unotools/configitem.hxx> +#include <tools/debug.hxx> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Sequence.hxx> +#include <com/sun/star/beans/PropertyValue.hpp> + +#include <vector> + +#include "itemholder1.hxx" + +#include <algorithm> + +using namespace ::std; +using namespace ::utl; +using namespace ::osl; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::beans; + +#define ROOTNODE_MENUS "Office.Common/Menus/" +#define PATHDELIMITER "/" + +#define SETNODE_NEWMENU "New" +#define SETNODE_WIZARDMENU "Wizard" + +#define PROPERTYNAME_URL DYNAMICMENU_PROPERTYNAME_URL +#define PROPERTYNAME_TITLE DYNAMICMENU_PROPERTYNAME_TITLE +#define PROPERTYNAME_IMAGEIDENTIFIER DYNAMICMENU_PROPERTYNAME_IMAGEIDENTIFIER +#define PROPERTYNAME_TARGETNAME DYNAMICMENU_PROPERTYNAME_TARGETNAME + +#define PROPERTYCOUNT 4 + +#define OFFSET_URL 0 +#define OFFSET_TITLE 1 +#define OFFSET_IMAGEIDENTIFIER 2 +#define OFFSET_TARGETNAME 3 + +#define PATHPREFIX_SETUP "m" + +namespace { + +/*-**************************************************************************************************************** + @descr struct to hold information about one menu entry. +****************************************************************************************************************-*/ +struct SvtDynMenuEntry +{ + OUString sURL; + OUString sTitle; + OUString sImageIdentifier; + OUString sTargetName; +}; + +/*-**************************************************************************************************************** + @descr support simple menu structures and operations on it +****************************************************************************************************************-*/ +class SvtDynMenu +{ + public: + // append setup written menu entry + // Don't touch name of entry. It was defined by setup and must be the same every time! + // Look for double menu entries here too... may be some separator items are superfluous... + void AppendSetupEntry( const SvtDynMenuEntry& rEntry ) + { + if( + ( lSetupEntries.empty() ) || + ( lSetupEntries.rbegin()->sURL != rEntry.sURL ) + ) + { + lSetupEntries.push_back( rEntry ); + } + } + + // convert internal list to external format + // for using it on right menus really + // Notice: We build a property list with 4 entries and set it on result list then. + // Separator entries will be packed in another way then normal entries! We define + // special string "sSeparator" to perform too ... + Sequence< Sequence< PropertyValue > > GetList() const + { + sal_Int32 nSetupCount = static_cast<sal_Int32>(lSetupEntries.size()); + sal_Int32 nUserCount = static_cast<sal_Int32>(lUserEntries.size()); + sal_Int32 nStep = 0; + Sequence< PropertyValue > lProperties ( PROPERTYCOUNT ); + Sequence< Sequence< PropertyValue > > lResult ( nSetupCount+nUserCount ); + OUString sSeparator ( "private:separator" ); + + lProperties[OFFSET_URL ].Name = PROPERTYNAME_URL; + lProperties[OFFSET_TITLE ].Name = PROPERTYNAME_TITLE; + lProperties[OFFSET_IMAGEIDENTIFIER].Name = PROPERTYNAME_IMAGEIDENTIFIER; + lProperties[OFFSET_TARGETNAME ].Name = PROPERTYNAME_TARGETNAME; + + for( const auto& pList : {&lSetupEntries, &lUserEntries} ) + { + for( const auto& rItem : *pList ) + { + if( rItem.sURL == sSeparator ) + { + lProperties[OFFSET_URL ].Value <<= sSeparator; + lProperties[OFFSET_TITLE ].Value <<= OUString(); + lProperties[OFFSET_IMAGEIDENTIFIER ].Value <<= OUString(); + lProperties[OFFSET_TARGETNAME ].Value <<= OUString(); + } + else + { + lProperties[OFFSET_URL ].Value <<= rItem.sURL; + lProperties[OFFSET_TITLE ].Value <<= rItem.sTitle; + lProperties[OFFSET_IMAGEIDENTIFIER ].Value <<= rItem.sImageIdentifier; + lProperties[OFFSET_TARGETNAME ].Value <<= rItem.sTargetName; + } + lResult[nStep] = lProperties; + ++nStep; + } + } + return lResult; + } + + private: + vector< SvtDynMenuEntry > lSetupEntries; + vector< SvtDynMenuEntry > lUserEntries; +}; + +} + +class SvtDynamicMenuOptions_Impl : public ConfigItem +{ + public: + + SvtDynamicMenuOptions_Impl(); + virtual ~SvtDynamicMenuOptions_Impl() override; + + /*-**************************************************************************************************** + @short called for notify of configmanager + @descr This method is called from the ConfigManager before the application ends or from the + PropertyChangeListener if the sub tree broadcasts changes. You must update your + internal values. + + @seealso baseclass ConfigItem + + @param "lPropertyNames" is the list of properties which should be updated. + *//*-*****************************************************************************************************/ + + virtual void Notify( const Sequence< OUString >& lPropertyNames ) override; + + /*-**************************************************************************************************** + @short base implementation of public interface for "SvtDynamicMenuOptions"! + @descr These class is used as static member of "SvtDynamicMenuOptions" ... + => The code exist only for one time and isn't duplicated for every instance! + *//*-*****************************************************************************************************/ + + Sequence< Sequence< PropertyValue > > GetMenu ( EDynamicMenuType eMenu ) const; + + private: + + virtual void ImplCommit() override; + + /*-**************************************************************************************************** + @short return list of key names of our configuration management which represent our module tree + @descr This method returns the current list of key names! We need it to get needed values from our + configuration management and support dynamical menu item lists! + @param "nNewCount" , returns count of menu entries for "new" + @param "nWizardCount" , returns count of menu entries for "wizard" + @return A list of configuration key names is returned. + *//*-*****************************************************************************************************/ + + Sequence< OUString > impl_GetPropertyNames( sal_uInt32& nNewCount, sal_uInt32& nWizardCount ); + + /*-**************************************************************************************************** + @short sort given source list and expand it for all well known properties to destination + @descr We must support sets of entries with count inside the name .. but some of them could be missing! + e.g. s1-s2-s3-s0-u1-s6-u5-u7 + Then we must sort it by name and expand it to the follow one: + sSetNode/s0/URL + sSetNode/s0/Title + sSetNode/s0/... + sSetNode/s1/URL + sSetNode/s1/Title + sSetNode/s1/... + ... + sSetNode/s6/URL + sSetNode/s6/Title + sSetNode/s6/... + sSetNode/u1/URL + sSetNode/u1/Title + sSetNode/u1/... + ... + sSetNode/u7/URL + sSetNode/u7/Title + sSetNode/u7/... + Rules: We start with all setup written entries names "sx" and x=[0..n]. + Then we handle all "ux" items. Inside these blocks we sort it ascending by number. + + @attention We add these expanded list to the end of given "lDestination" list! + So we must start on "lDestination.getLength()". + Reallocation of memory of destination list is done by us! + + @seealso method impl_GetPropertyNames() + + @param "lSource" , original list (e.g. [m1-m2-m3-m6-m0] ) + @param "lDestination" , destination of operation + @param "sSetNode" , name of configuration set to build complete path + @return A list of configuration key names is returned. + *//*-*****************************************************************************************************/ + + static void impl_SortAndExpandPropertyNames( const Sequence< OUString >& lSource , + Sequence< OUString >& lDestination , + const OUString& sSetNode ); + + // private member + + private: + + SvtDynMenu m_aNewMenu; + SvtDynMenu m_aWizardMenu; +}; + +// constructor + +SvtDynamicMenuOptions_Impl::SvtDynamicMenuOptions_Impl() + // Init baseclasses first + : ConfigItem( ROOTNODE_MENUS ) + // Init member then... +{ + // Get names and values of all accessible menu entries and fill internal structures. + // See impl_GetPropertyNames() for further information. + sal_uInt32 nNewCount = 0; + sal_uInt32 nWizardCount = 0; + Sequence< OUString > lNames = impl_GetPropertyNames ( nNewCount , + nWizardCount ); + Sequence< Any > lValues = GetProperties ( lNames ); + + // Safe impossible cases. + // We need values from ALL configuration keys. + // Follow assignment use order of values in relation to our list of key names! + DBG_ASSERT( !(lNames.getLength()!=lValues.getLength()), "SvtDynamicMenuOptions_Impl::SvtDynamicMenuOptions_Impl()\nI miss some values of configuration keys!\n" ); + + // Copy values from list in right order to our internal member. + // Attention: List for names and values have an internal construction pattern! + + // first "New" menu ... + // Name Value + // /New/1/URL "private:factory/swriter" + // /New/1/Title "New Writer Document" + // /New/1/ImageIdentifier "icon_writer" + // /New/1/TargetName "_blank" + + // /New/2/URL "private:factory/scalc" + // /New/2/Title "New Calc Document" + // /New/2/ImageIdentifier "icon_calc" + // /New/2/TargetName "_blank" + + // second "Wizard" menu ... + // /Wizard/1/URL "file://b" + // /Wizard/1/Title "PaintSomething" + // /Wizard/1/ImageIdentifier "icon_?" + // /Wizard/1/TargetName "_self" + + // ... and so on ... + + sal_uInt32 nItem = 0; + sal_uInt32 nPosition = 0; + + // Get names/values for new menu. + // 4 subkeys for every item! + for( nItem=0; nItem<nNewCount; ++nItem ) + { + SvtDynMenuEntry aItem; + lValues[nPosition] >>= aItem.sURL; + ++nPosition; + lValues[nPosition] >>= aItem.sTitle; + ++nPosition; + lValues[nPosition] >>= aItem.sImageIdentifier; + ++nPosition; + lValues[nPosition] >>= aItem.sTargetName; + ++nPosition; + m_aNewMenu.AppendSetupEntry( aItem ); + } + + // Attention: Don't reset nPosition here! + + // Get names/values for wizard menu. + // 4 subkeys for every item! + for( nItem=0; nItem<nWizardCount; ++nItem ) + { + SvtDynMenuEntry aItem; + lValues[nPosition] >>= aItem.sURL; + ++nPosition; + lValues[nPosition] >>= aItem.sTitle; + ++nPosition; + lValues[nPosition] >>= aItem.sImageIdentifier; + ++nPosition; + lValues[nPosition] >>= aItem.sTargetName; + ++nPosition; + m_aWizardMenu.AppendSetupEntry( aItem ); + } + + // Attention: Don't reset nPosition here! + +/*TODO: Not used in the moment! see Notify() ... + // Enable notification mechanism of our baseclass. + // We need it to get information about changes outside these class on our used configuration keys! + EnableNotification( lNames ); +*/ +} + +// destructor + +SvtDynamicMenuOptions_Impl::~SvtDynamicMenuOptions_Impl() +{ + assert(!IsModified()); // should have been committed +} + +// public method + +void SvtDynamicMenuOptions_Impl::Notify( const Sequence< OUString >& ) +{ + SAL_WARN( "unotools.config", "SvtDynamicMenuOptions_Impl::Notify() Not implemented yet! I don't know how I can handle a dynamical list of unknown properties ..." ); +} + +// public method + +void SvtDynamicMenuOptions_Impl::ImplCommit() +{ + SAL_WARN("unotools.config", "SvtDynamicMenuOptions_Impl::ImplCommit(): Not implemented yet!"); + /* + // Write all properties! + // Delete complete sets first. + ClearNodeSet( SETNODE_NEWMENU ); + ClearNodeSet( SETNODE_WIZARDMENU ); + + MenuEntry aItem; + OUString sNode; + Sequence< PropertyValue > lPropertyValues( PROPERTYCOUNT ); + sal_uInt32 nItem = 0; + + // Copy "new" menu entries to save-list! + sal_uInt32 nNewCount = m_aNewMenu.size(); + for( nItem=0; nItem<nNewCount; ++nItem ) + { + aItem = m_aNewMenu[nItem]; + // Format: "New/1/URL" + // "New/1/Title" + // ... + sNode = SETNODE_NEWMENU + PATHDELIMITER + PATHPREFIX + OUString::valueOf( (sal_Int32)nItem ) + PATHDELIMITER; + + lPropertyValues[OFFSET_URL ].Name = sNode + PROPERTYNAME_URL; + lPropertyValues[OFFSET_TITLE ].Name = sNode + PROPERTYNAME_TITLE; + lPropertyValues[OFFSET_IMAGEIDENTIFIER ].Name = sNode + PROPERTYNAME_IMAGEIDENTIFIER; + lPropertyValues[OFFSET_TARGETNAME ].Name = sNode + PROPERTYNAME_TARGETNAME; + + lPropertyValues[OFFSET_URL ].Value <<= aItem.sURL; + lPropertyValues[OFFSET_TITLE ].Value <<= aItem.sTitle; + lPropertyValues[OFFSET_IMAGEIDENTIFIER ].Value <<= aItem.sImageIdentifier; + lPropertyValues[OFFSET_TARGETNAME ].Value <<= aItem.sTargetName; + + SetSetProperties( SETNODE_NEWMENU, lPropertyValues ); + } + + // Copy "wizard" menu entries to save-list! + sal_uInt32 nWizardCount = m_aWizardMenu.size(); + for( nItem=0; nItem<nWizardCount; ++nItem ) + { + aItem = m_aWizardMenu[nItem]; + // Format: "Wizard/1/URL" + // "Wizard/1/Title" + // ... + sNode = SETNODE_WIZARDMENU + PATHDELIMITER + PATHPREFIX + OUString::valueOf( (sal_Int32)nItem ) + PATHDELIMITER; + + lPropertyValues[OFFSET_URL ].Name = sNode + PROPERTYNAME_URL; + lPropertyValues[OFFSET_TITLE ].Name = sNode + PROPERTYNAME_TITLE; + lPropertyValues[OFFSET_IMAGEIDENTIFIER ].Name = sNode + PROPERTYNAME_IMAGEIDENTIFIER; + lPropertyValues[OFFSET_TARGETNAME ].Name = sNode + PROPERTYNAME_TARGETNAME; + + lPropertyValues[OFFSET_URL ].Value <<= aItem.sURL; + lPropertyValues[OFFSET_TITLE ].Value <<= aItem.sTitle; + lPropertyValues[OFFSET_IMAGEIDENTIFIER ].Value <<= aItem.sImageIdentifier; + lPropertyValues[OFFSET_TARGETNAME ].Value <<= aItem.sTargetName; + + SetSetProperties( SETNODE_WIZARDMENU, lPropertyValues ); + } + + */ +} + +// public method + +Sequence< Sequence< PropertyValue > > SvtDynamicMenuOptions_Impl::GetMenu( EDynamicMenuType eMenu ) const +{ + Sequence< Sequence< PropertyValue > > lReturn; + switch( eMenu ) + { + case EDynamicMenuType::NewMenu : + lReturn = m_aNewMenu.GetList(); + break; + + case EDynamicMenuType::WizardMenu : + lReturn = m_aWizardMenu.GetList(); + break; + } + return lReturn; +} + +// private method + +Sequence< OUString > SvtDynamicMenuOptions_Impl::impl_GetPropertyNames( sal_uInt32& nNewCount, sal_uInt32& nWizardCount ) +{ + // First get ALL names of current existing list items in configuration! + Sequence< OUString > lNewItems = GetNodeNames( SETNODE_NEWMENU ); + Sequence< OUString > lWizardItems = GetNodeNames( SETNODE_WIZARDMENU ); + + // Get information about list counts ... + nNewCount = lNewItems.getLength (); + nWizardCount = lWizardItems.getLength (); + + // Sort and expand all three list to result list ... + Sequence< OUString > lProperties; + impl_SortAndExpandPropertyNames( lNewItems , lProperties, SETNODE_NEWMENU ); + impl_SortAndExpandPropertyNames( lWizardItems , lProperties, SETNODE_WIZARDMENU ); + + // Return result. + return lProperties; +} + +// private helper + +namespace { + +class CountWithPrefixSort +{ + public: + bool operator() ( const OUString& s1 , + const OUString& s2 ) const + { + // Get order numbers from entry name without prefix. + // e.g. "m10" => 10 + // "m5" => 5 + sal_Int32 n1 = s1.copy( 1 ).toInt32(); + sal_Int32 n2 = s2.copy( 1 ).toInt32(); + // MUST be in [0,1] ... because it's a difference between + // insert-positions of given entries in sorted list! + return( n1<n2 ); + } +}; + +class SelectByPrefix +{ + public: + bool operator() ( const OUString& s ) const + { + // Prefer setup written entries by check first letter of given string. It must be a "s". + return s.startsWith( PATHPREFIX_SETUP ); + } +}; + +} + +// private method + +void SvtDynamicMenuOptions_Impl::impl_SortAndExpandPropertyNames( const Sequence< OUString >& lSource , + Sequence< OUString >& lDestination , + const OUString& sSetNode ) +{ + vector< OUString > lTemp; + sal_Int32 nSourceCount = lSource.getLength(); + sal_Int32 nDestinationStep = lDestination.getLength(); // start on end of current list ...! + + lDestination.realloc( (nSourceCount*PROPERTYCOUNT)+nDestinationStep ); // get enough memory for copy operations after nDestination ... + + // Copy all items to temp. vector to use fast sort operations :-) + lTemp.reserve(nSourceCount); + std::copy(lSource.begin(), lSource.end(), std::back_inserter(lTemp)); + + // Sort all entries by number ... + stable_sort( lTemp.begin(), lTemp.end(), CountWithPrefixSort() ); + // and split into setup & user written entries! + stable_partition( lTemp.begin(), lTemp.end(), SelectByPrefix() ); + + // Copy sorted entries to destination and expand every item with + // 4 supported sub properties. + for( const auto& rItem : lTemp ) + { + OUString sFixPath(sSetNode + PATHDELIMITER + rItem + PATHDELIMITER); + lDestination[nDestinationStep++] = sFixPath + PROPERTYNAME_URL; + lDestination[nDestinationStep++] = sFixPath + PROPERTYNAME_TITLE; + lDestination[nDestinationStep++] = sFixPath + PROPERTYNAME_IMAGEIDENTIFIER; + lDestination[nDestinationStep++] = sFixPath + PROPERTYNAME_TARGETNAME; + } +} + +namespace { + // global + std::weak_ptr<SvtDynamicMenuOptions_Impl> g_pDynamicMenuOptions; +} + +SvtDynamicMenuOptions::SvtDynamicMenuOptions() +{ + // Global access, must be guarded (multithreading!). + MutexGuard aGuard( GetOwnStaticMutex() ); + + m_pImpl = g_pDynamicMenuOptions.lock(); + if( !m_pImpl ) + { + m_pImpl = std::make_shared<SvtDynamicMenuOptions_Impl>(); + g_pDynamicMenuOptions = m_pImpl; + ItemHolder1::holdConfigItem(EItem::DynamicMenuOptions); + } +} + +SvtDynamicMenuOptions::~SvtDynamicMenuOptions() +{ + // Global access, must be guarded (multithreading!) + MutexGuard aGuard( GetOwnStaticMutex() ); + + m_pImpl.reset(); +} + +// public method + +Sequence< Sequence< PropertyValue > > SvtDynamicMenuOptions::GetMenu( EDynamicMenuType eMenu ) const +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + return m_pImpl->GetMenu( eMenu ); +} + +namespace +{ + class theDynamicMenuOptionsMutex : public rtl::Static<osl::Mutex, theDynamicMenuOptionsMutex>{}; +} + +// private method + +Mutex& SvtDynamicMenuOptions::GetOwnStaticMutex() +{ + return theDynamicMenuOptionsMutex::get(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/eventcfg.cxx b/unotools/source/config/eventcfg.cxx new file mode 100644 index 000000000..b0e36fc62 --- /dev/null +++ b/unotools/source/config/eventcfg.cxx @@ -0,0 +1,378 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unotools/eventcfg.hxx> +#include <unotools/configmgr.hxx> +#include <unotools/configitem.hxx> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Sequence.hxx> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <o3tl/enumarray.hxx> +#include <o3tl/enumrange.hxx> +#include <rtl/ref.hxx> +#include <sal/log.hxx> + +#include "itemholder1.hxx" + +#include <algorithm> +#include <unordered_map> + +using namespace ::std; +using namespace ::utl; +using namespace ::osl; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star; + +#define PATHDELIMITER "/" +#define SETNODE_BINDINGS "Bindings" +#define PROPERTYNAME_BINDINGURL "BindingURL" + +static o3tl::enumarray<GlobalEventId, const char*> pEventAsciiNames = +{ +"OnStartApp", +"OnCloseApp", +"OnCreate", +"OnNew", +"OnLoadFinished", +"OnLoad", +"OnPrepareUnload", +"OnUnload", +"OnSave", +"OnSaveDone", +"OnSaveFailed", +"OnSaveAs", +"OnSaveAsDone", +"OnSaveAsFailed", +"OnCopyTo", +"OnCopyToDone", +"OnCopyToFailed", +"OnFocus", +"OnUnfocus", +"OnPrint", +"OnViewCreated", +"OnPrepareViewClosing", +"OnViewClosed", +"OnModifyChanged", +"OnTitleChanged", +"OnVisAreaChanged", +"OnModeChanged", +"OnStorageChanged" +}; + +typedef std::unordered_map< OUString, OUString > EventBindingHash; +typedef o3tl::enumarray< GlobalEventId, OUString > SupportedEventsVector; + +class GlobalEventConfig_Impl : public utl::ConfigItem +{ +private: + EventBindingHash m_eventBindingHash; + SupportedEventsVector m_supportedEvents; + + void initBindingInfo(); + + virtual void ImplCommit() override; + +public: + GlobalEventConfig_Impl( ); + virtual ~GlobalEventConfig_Impl( ) override; + + void Notify( const css::uno::Sequence<OUString>& aPropertyNames) override; + + /// @throws css::lang::IllegalArgumentException + /// @throws css::container::NoSuchElementException + /// @throws css::lang::WrappedTargetException + /// @throws css::uno::RuntimeException + void replaceByName( const OUString& aName, const css::uno::Any& aElement ); + /// @throws css::container::NoSuchElementException + /// @throws css::lang::WrappedTargetException + /// @throws css::uno::RuntimeException + css::uno::Any getByName( const OUString& aName ); + /// @throws css::uno::RuntimeException + css::uno::Sequence< OUString > getElementNames( ); + /// @throws css::uno::RuntimeException + bool hasByName( const OUString& aName ); + /// @throws css::uno::RuntimeException + static css::uno::Type const & getElementType( ); + /// @throws css::uno::RuntimeException + bool hasElements() const; + OUString const & GetEventName( GlobalEventId nID ) const; +}; + + +GlobalEventConfig_Impl::GlobalEventConfig_Impl() + : ConfigItem( "Office.Events/ApplicationEvents", ConfigItemMode::NONE ) +{ + // the supported event names + for (const GlobalEventId id : o3tl::enumrange<GlobalEventId>()) + m_supportedEvents[id] = OUString::createFromAscii( pEventAsciiNames[id] ); + + initBindingInfo(); + +/*TODO: Not used in the moment! see Notify() ... + // Enable notification mechanism of our baseclass. + // We need it to get information about changes outside these class on our used configuration keys! */ + Sequence<OUString> aNotifySeq { "Events" }; + EnableNotification( aNotifySeq, true ); +} + +// destructor + +GlobalEventConfig_Impl::~GlobalEventConfig_Impl() +{ + assert(!IsModified()); // should have been committed +} + +OUString const & GlobalEventConfig_Impl::GetEventName( GlobalEventId nIndex ) const +{ + return m_supportedEvents[nIndex]; +} + +// public method + +void GlobalEventConfig_Impl::Notify( const Sequence< OUString >& ) +{ + MutexGuard aGuard( GlobalEventConfig::GetOwnStaticMutex() ); + + initBindingInfo(); +} + +// public method + +void GlobalEventConfig_Impl::ImplCommit() +{ + //DF need to check it this is correct?? + SAL_INFO("unotools", "In GlobalEventConfig_Impl::ImplCommit"); + // clear the existing nodes + ClearNodeSet( SETNODE_BINDINGS ); + Sequence< beans::PropertyValue > seqValues( 1 ); + OUString sNode; + //step through the list of events + for(const auto& rEntry : m_eventBindingHash) + { + //no point in writing out empty bindings! + if(rEntry.second.isEmpty() ) + continue; + sNode = SETNODE_BINDINGS PATHDELIMITER "BindingType['" + + rEntry.first + + "']" PATHDELIMITER PROPERTYNAME_BINDINGURL; + SAL_INFO("unotools", "writing binding for: " << sNode); + seqValues[ 0 ].Name = sNode; + seqValues[ 0 ].Value <<= rEntry.second; + //write the data to the registry + SetSetProperties(SETNODE_BINDINGS,seqValues); + } +} + +// private method + +void GlobalEventConfig_Impl::initBindingInfo() +{ + // Get ALL names of current existing list items in configuration! + const Sequence< OUString > lEventNames = GetNodeNames( SETNODE_BINDINGS, utl::ConfigNameFormat::LocalPath ); + + OUString aSetNode = SETNODE_BINDINGS PATHDELIMITER; + OUString aCommandKey = PATHDELIMITER PROPERTYNAME_BINDINGURL; + + // Expand all keys + Sequence< OUString > lMacros(1); + for (const auto& rEventName : lEventNames ) + { + lMacros[0] = aSetNode + rEventName + aCommandKey; + SAL_INFO("unotools", "reading binding for: " << lMacros[0]); + Sequence< Any > lValues = GetProperties( lMacros ); + OUString sMacroURL; + if( lValues.hasElements() ) + { + lValues[0] >>= sMacroURL; + sal_Int32 startIndex = rEventName.indexOf('\''); + sal_Int32 endIndex = rEventName.lastIndexOf('\''); + if( startIndex >=0 && endIndex > 0 ) + { + startIndex++; + OUString eventName = rEventName.copy(startIndex,endIndex-startIndex); + m_eventBindingHash[ eventName ] = sMacroURL; + } + } + } +} + +void GlobalEventConfig_Impl::replaceByName( const OUString& aName, const Any& aElement ) +{ + Sequence< beans::PropertyValue > props; + //DF should we prepopulate the hash with a list of valid event Names? + if( !( aElement >>= props ) ) + { + throw lang::IllegalArgumentException( OUString(), + Reference< XInterface > (), 2); + } + OUString macroURL; + for( const auto& rProp : std::as_const(props) ) + { + if ( rProp.Name == "Script" ) + rProp.Value >>= macroURL; + } + m_eventBindingHash[ aName ] = macroURL; + SetModified(); +} + +Any GlobalEventConfig_Impl::getByName( const OUString& aName ) +{ + Any aRet; + Sequence< beans::PropertyValue > props(2); + props[0].Name = "EventType"; + props[0].Value <<= OUString("Script"); + props[1].Name = "Script"; + EventBindingHash::const_iterator it = m_eventBindingHash.find( aName ); + if( it != m_eventBindingHash.end() ) + { + props[1].Value <<= it->second; + } + else + { + // not yet accessed - is it a supported name? + SupportedEventsVector::iterator pos = ::std::find( + m_supportedEvents.begin(), m_supportedEvents.end(), aName ); + if ( pos == m_supportedEvents.end() ) + throw container::NoSuchElementException( aName ); + + props[1].Value <<= OUString(); + } + aRet <<= props; + return aRet; +} + +Sequence< OUString > GlobalEventConfig_Impl::getElementNames( ) +{ + return uno::Sequence< OUString >(m_supportedEvents.data(), SupportedEventsVector::size()); +} + +bool GlobalEventConfig_Impl::hasByName( const OUString& aName ) +{ + if ( m_eventBindingHash.find( aName ) != m_eventBindingHash.end() ) + return true; + + // never accessed before - is it supported in general? + SupportedEventsVector::iterator pos = ::std::find( + m_supportedEvents.begin(), m_supportedEvents.end(), aName ); + return pos != m_supportedEvents.end(); +} + +Type const & GlobalEventConfig_Impl::getElementType( ) +{ + //DF definitely not sure about this?? + return cppu::UnoType<Sequence<beans::PropertyValue>>::get(); +} + +bool GlobalEventConfig_Impl::hasElements() const +{ + return !m_eventBindingHash.empty(); +} + +// and now the wrapper + +//initialize static member +GlobalEventConfig_Impl* GlobalEventConfig::m_pImpl = nullptr; +sal_Int32 GlobalEventConfig::m_nRefCount = 0; + +GlobalEventConfig::GlobalEventConfig() +{ + // Global access, must be guarded (multithreading!). + MutexGuard aGuard( GetOwnStaticMutex() ); + // Increase our refcount ... + ++m_nRefCount; + // ... and initialize our data container only if it not already exist! + if( m_pImpl == nullptr ) + { + m_pImpl = new GlobalEventConfig_Impl; + ItemHolder1::holdConfigItem(EItem::EventConfig); + } +} + +GlobalEventConfig::~GlobalEventConfig() +{ + // Global access, must be guarded (multithreading!) + MutexGuard aGuard( GetOwnStaticMutex() ); + // Decrease our refcount. + --m_nRefCount; + // If last instance was deleted ... + // we must destroy our static data container! + if( m_nRefCount <= 0 ) + { + delete m_pImpl; + m_pImpl = nullptr; + } +} + +Reference< container::XNameReplace > SAL_CALL GlobalEventConfig::getEvents() +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + Reference< container::XNameReplace > ret(this); + return ret; +} + +void SAL_CALL GlobalEventConfig::replaceByName( const OUString& aName, const Any& aElement ) +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + m_pImpl->replaceByName( aName, aElement ); +} +Any SAL_CALL GlobalEventConfig::getByName( const OUString& aName ) +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + return m_pImpl->getByName( aName ); +} +Sequence< OUString > SAL_CALL GlobalEventConfig::getElementNames( ) +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + return m_pImpl->getElementNames( ); +} +sal_Bool SAL_CALL GlobalEventConfig::hasByName( const OUString& aName ) +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + return m_pImpl->hasByName( aName ); +} +Type SAL_CALL GlobalEventConfig::getElementType( ) +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + return GlobalEventConfig_Impl::getElementType( ); +} +sal_Bool SAL_CALL GlobalEventConfig::hasElements( ) +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + return m_pImpl->hasElements( ); +} + +namespace +{ + class theGlobalEventConfigMutex : public rtl::Static<osl::Mutex, theGlobalEventConfigMutex>{}; +} + +Mutex& GlobalEventConfig::GetOwnStaticMutex() +{ + return theGlobalEventConfigMutex::get(); +} + +OUString GlobalEventConfig::GetEventName( GlobalEventId nIndex ) +{ + if (utl::ConfigManager::IsFuzzing()) + return OUString(); + rtl::Reference<GlobalEventConfig> createImpl(new GlobalEventConfig); + return GlobalEventConfig::m_pImpl->GetEventName( nIndex ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/extendedsecurityoptions.cxx b/unotools/source/config/extendedsecurityoptions.cxx new file mode 100644 index 000000000..2d57ab423 --- /dev/null +++ b/unotools/source/config/extendedsecurityoptions.cxx @@ -0,0 +1,226 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <unotools/extendedsecurityoptions.hxx> +#include <unotools/configitem.hxx> +#include <tools/debug.hxx> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Sequence.hxx> +#include <osl/diagnose.h> + +#include "itemholder1.hxx" + +#include <unordered_map> + +using namespace ::utl; +using namespace ::osl; +using namespace ::com::sun::star::uno; + +#define ROOTNODE_SECURITY "Office.Security" + +#define SECURE_EXTENSIONS_SET OUString("SecureExtensions") + +#define PROPERTYNAME_HYPERLINKS_OPEN OUString("Hyperlinks/Open") + +#define PROPERTYHANDLE_HYPERLINKS_OPEN 0 + +#define PROPERTYCOUNT 1 + +typedef std::unordered_map<OUString, sal_Int32> + ExtensionHashMap; + +class SvtExtendedSecurityOptions_Impl : public ConfigItem +{ + public: + SvtExtendedSecurityOptions_Impl(); + virtual ~SvtExtendedSecurityOptions_Impl() override; + + /*-**************************************************************************************************** + @short called for notify of configmanager + @descr This method is called from the ConfigManager before the application ends or from the + PropertyChangeListener if the sub tree broadcasts changes. You must update your + internal values. + + @seealso baseclass ConfigItem + + @param "seqPropertyNames" is the list of properties which should be updated. + *//*-*****************************************************************************************************/ + + virtual void Notify( const Sequence< OUString >& seqPropertyNames ) override; + + SvtExtendedSecurityOptions::OpenHyperlinkMode GetOpenHyperlinkMode() const { return m_eOpenHyperlinkMode;} + + private: + virtual void ImplCommit() override; + + /*-**************************************************************************************************** + @short return list of key names of our configuration management which represent our module tree + @descr This method returns a static const list of key names. We need it to get needed values from our + configuration management. + @return A list of needed configuration keys is returned. + *//*-*****************************************************************************************************/ + + static Sequence< OUString > GetPropertyNames(); + + SvtExtendedSecurityOptions::OpenHyperlinkMode m_eOpenHyperlinkMode; +}; + +// constructor + +SvtExtendedSecurityOptions_Impl::SvtExtendedSecurityOptions_Impl() + // Init baseclasses first + : ConfigItem ( ROOTNODE_SECURITY ) + , m_eOpenHyperlinkMode(SvtExtendedSecurityOptions::OPEN_NEVER) + // Init member then. +{ + Sequence< OUString > seqNames = GetPropertyNames(); + Sequence< Any > seqValues = GetProperties( seqNames ); + + sal_Int32 nPropertyCount = seqValues.getLength(); + for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty ) + { + // Safe impossible cases. + // Check any for valid value. + DBG_ASSERT( seqValues[nProperty].hasValue(), "SvtExtendedSecurityOptions_Impl::SvtExtendedSecurityOptions_Impl()\nInvalid property value detected!\n" ); + switch( nProperty ) + { + case PROPERTYHANDLE_HYPERLINKS_OPEN: + { + DBG_ASSERT( ( seqValues[nProperty].getValueTypeClass() == TypeClass_LONG ), "SvtExtendedSecurityOptions_Impl::SvtExtendedSecurityOptions_Impl()\nWho has changed the value type of 'Hyperlink/Open'?" ); + + sal_Int32 nMode = SvtExtendedSecurityOptions::OPEN_WITHSECURITYCHECK; + if ( seqValues[nProperty] >>= nMode ) + m_eOpenHyperlinkMode = static_cast<SvtExtendedSecurityOptions::OpenHyperlinkMode>(nMode); + else { + OSL_FAIL("Wrong type for Open mode!"); + } + } + break; + } + } + + // Enable notification mechanism of our baseclass. + // We need it to get information about changes outside these class on our used configuration keys! + Sequence<OUString> seqNotifyNames { SECURE_EXTENSIONS_SET }; + EnableNotification( seqNotifyNames ); +} + +// destructor + +SvtExtendedSecurityOptions_Impl::~SvtExtendedSecurityOptions_Impl() +{ + assert(!IsModified()); // should have been committed +} + +// public method + +void SvtExtendedSecurityOptions_Impl::Notify( const Sequence< OUString >& ) +{ + // Not implemented +} + +// public method + +void SvtExtendedSecurityOptions_Impl::ImplCommit() +{ + // Get names of supported properties, create a list for values and copy current values to it. + Sequence< OUString > seqNames = GetPropertyNames (); + sal_Int32 nCount = seqNames.getLength(); + Sequence< Any > seqValues ( nCount ); + for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty ) + { + switch( nProperty ) + { + case PROPERTYHANDLE_HYPERLINKS_OPEN: { + seqValues[nProperty] <<= static_cast<sal_Int32>(m_eOpenHyperlinkMode); + } + break; + } + } + + // Set properties in configuration. + PutProperties( seqNames, seqValues ); +} + +// private method (currently not used) + +Sequence< OUString > SvtExtendedSecurityOptions_Impl::GetPropertyNames() +{ + // Build list of configuration key names. + const OUString pProperties[] = + { + PROPERTYNAME_HYPERLINKS_OPEN + }; + // Initialize return sequence with these list ... + const Sequence< OUString > seqPropertyNames( pProperties, PROPERTYCOUNT ); + // ... and return it. + return seqPropertyNames; +} + +namespace { + +std::weak_ptr<SvtExtendedSecurityOptions_Impl> g_pExtendedSecurityOptions; + +} + +SvtExtendedSecurityOptions::SvtExtendedSecurityOptions() +{ + // Global access, must be guarded (multithreading!). + MutexGuard aGuard( GetInitMutex() ); + + m_pImpl = g_pExtendedSecurityOptions.lock(); + if( !m_pImpl ) + { + m_pImpl = std::make_shared<SvtExtendedSecurityOptions_Impl>(); + g_pExtendedSecurityOptions = m_pImpl; + ItemHolder1::holdConfigItem(EItem::ExtendedSecurityOptions); + } +} + +SvtExtendedSecurityOptions::~SvtExtendedSecurityOptions() +{ + // Global access, must be guarded (multithreading!) + MutexGuard aGuard( GetInitMutex() ); + + m_pImpl.reset(); +} + +// public method + +SvtExtendedSecurityOptions::OpenHyperlinkMode SvtExtendedSecurityOptions::GetOpenHyperlinkMode() const +{ + MutexGuard aGuard( GetInitMutex() ); + return m_pImpl->GetOpenHyperlinkMode(); +} + +namespace +{ + class theExtendedSecurityOptionsMutex : public rtl::Static<osl::Mutex, theExtendedSecurityOptionsMutex>{}; +} + +// private method + +Mutex& SvtExtendedSecurityOptions::GetInitMutex() +{ + return theExtendedSecurityOptionsMutex::get(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/fltrcfg.cxx b/unotools/source/config/fltrcfg.cxx new file mode 100644 index 000000000..c68ac827e --- /dev/null +++ b/unotools/source/config/fltrcfg.cxx @@ -0,0 +1,690 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <o3tl/any.hxx> +#include <o3tl/typed_flags_set.hxx> +#include <unotools/fltrcfg.hxx> +#include <tools/debug.hxx> +#include <osl/diagnose.h> + +#include <com/sun/star/uno/Sequence.hxx> + +using namespace utl; +using namespace com::sun::star::uno; + +namespace { + +enum class ConfigFlags { + NONE = 0x0000000, + WordCode = 0x0000001, + WordStorage = 0x0000002, + ExcelCode = 0x0000004, + ExcelStorage = 0x0000008, + PowerPointCode = 0x0000010, + PowerPointStorage = 0x0000020, + MathLoad = 0x0000100, + MathSave = 0x0000200, + WriterLoad = 0x0000400, + WriterSave = 0x0000800, + CalcLoad = 0x0001000, + CalcSave = 0x0002000, + ImpressLoad = 0x0004000, + ImpressSave = 0x0008000, + ExcelExecTbl = 0x0010000, + EnablePowerPointPreview = 0x0020000, + EnableExcelPreview = 0x0040000, + EnableWordPreview = 0x0080000, + UseEnhancedFields = 0x0100000, + WordWbctbl = 0x0200000, + SmartArtShapeLoad = 0x0400000, + CharBackgroundToHighlighting = 0x8000000, + CreateMSOLockFiles = 0x2000000, + VisioLoad = 0x4000000, +}; + +} + +namespace o3tl { + template<> struct typed_flags<ConfigFlags> : is_typed_flags<ConfigFlags, 0xe7fff3f> {}; +} + +namespace { + +class SvtAppFilterOptions_Impl : public utl::ConfigItem +{ +private: + bool bLoadVBA; + bool bSaveVBA; + +protected: + virtual void ImplCommit() override; + +public: + explicit SvtAppFilterOptions_Impl(const OUString& rRoot) : + utl::ConfigItem(rRoot), + bLoadVBA(false), + bSaveVBA(false) {} + virtual ~SvtAppFilterOptions_Impl() override; + virtual void Notify( const css::uno::Sequence<OUString>& aPropertyNames) override; + void Load(); + + bool IsLoad() const {return bLoadVBA;} + void SetLoad(bool bSet) + { + if(bSet != bLoadVBA) + SetModified(); + bLoadVBA = bSet; + } + bool IsSave() const {return bSaveVBA;} + void SetSave(bool bSet) + { + if(bSet != bSaveVBA) + SetModified(); + bSaveVBA = bSet; + } +}; + +} + +SvtAppFilterOptions_Impl::~SvtAppFilterOptions_Impl() +{ + assert(!IsModified()); // should have been committed +} + +void SvtAppFilterOptions_Impl::ImplCommit() +{ + PutProperties( + {"Load", "Save"}, {css::uno::Any(bLoadVBA), css::uno::Any(bSaveVBA)}); +} + +void SvtAppFilterOptions_Impl::Notify( const Sequence< OUString >& ) +{ + // no listeners supported yet +} + +void SvtAppFilterOptions_Impl::Load() +{ + Sequence<OUString> aNames(2); + OUString* pNames = aNames.getArray(); + pNames[0] = "Load"; + pNames[1] = "Save"; + + Sequence<Any> aValues = GetProperties(aNames); + const Any* pValues = aValues.getConstArray(); + + if(pValues[0].hasValue()) + bLoadVBA = *o3tl::doAccess<bool>(pValues[0]); + if(pValues[1].hasValue()) + bSaveVBA = *o3tl::doAccess<bool>(pValues[1]); +} + +namespace { + +class SvtWriterFilterOptions_Impl : public SvtAppFilterOptions_Impl +{ +private: + bool bLoadExecutable; + + virtual void ImplCommit() override; + +public: + explicit SvtWriterFilterOptions_Impl(const OUString& rRoot) : + SvtAppFilterOptions_Impl(rRoot), + bLoadExecutable(false) + {} + void Load(); + + bool IsLoadExecutable() const {return bLoadExecutable;} + void SetLoadExecutable(bool bSet) + { + if(bSet != bLoadExecutable) + SetModified(); + bLoadExecutable = bSet; + } +}; + +} + +void SvtWriterFilterOptions_Impl::ImplCommit() +{ + SvtAppFilterOptions_Impl::ImplCommit(); + + Sequence<OUString> aNames { "Executable" }; + Sequence<Any> aValues(1); + aValues[0] <<= bLoadExecutable; + + PutProperties(aNames, aValues); +} + +void SvtWriterFilterOptions_Impl::Load() +{ + SvtAppFilterOptions_Impl::Load(); + + Sequence<OUString> aNames { "Executable" }; + + Sequence<Any> aValues = GetProperties(aNames); + const Any* pValues = aValues.getConstArray(); + if(pValues[0].hasValue()) + bLoadExecutable = *o3tl::doAccess<bool>(pValues[0]); +} + +namespace { + +class SvtCalcFilterOptions_Impl : public SvtAppFilterOptions_Impl +{ +private: + bool bLoadExecutable; + + virtual void ImplCommit() override; + +public: + explicit SvtCalcFilterOptions_Impl(const OUString& rRoot) : + SvtAppFilterOptions_Impl(rRoot), + bLoadExecutable(false) + {} + void Load(); + + bool IsLoadExecutable() const {return bLoadExecutable;} + void SetLoadExecutable(bool bSet) + { + if(bSet != bLoadExecutable) + SetModified(); + bLoadExecutable = bSet; + } +}; + +} + +void SvtCalcFilterOptions_Impl::ImplCommit() +{ + SvtAppFilterOptions_Impl::ImplCommit(); + + Sequence<OUString> aNames { "Executable" }; + Sequence<Any> aValues(1); + aValues[0] <<= bLoadExecutable; + + PutProperties(aNames, aValues); +} + +void SvtCalcFilterOptions_Impl::Load() +{ + SvtAppFilterOptions_Impl::Load(); + + Sequence<OUString> aNames { "Executable" }; + + Sequence<Any> aValues = GetProperties(aNames); + const Any* pValues = aValues.getConstArray(); + if(pValues[0].hasValue()) + bLoadExecutable = *o3tl::doAccess<bool>(pValues[0]); +} + +struct SvtFilterOptions_Impl +{ + ConfigFlags nFlags; + SvtWriterFilterOptions_Impl aWriterCfg; + SvtCalcFilterOptions_Impl aCalcCfg; + SvtAppFilterOptions_Impl aImpressCfg; + + SvtFilterOptions_Impl() : + aWriterCfg("Office.Writer/Filter/Import/VBA"), + aCalcCfg("Office.Calc/Filter/Import/VBA"), + aImpressCfg("Office.Impress/Filter/Import/VBA") + { + nFlags = ConfigFlags::WordCode | + ConfigFlags::WordStorage | + ConfigFlags::ExcelCode | + ConfigFlags::ExcelStorage | + ConfigFlags::PowerPointCode | + ConfigFlags::PowerPointStorage | + ConfigFlags::MathLoad | + ConfigFlags::MathSave | + ConfigFlags::WriterLoad | + ConfigFlags::WriterSave | + ConfigFlags::CalcLoad | + ConfigFlags::CalcSave | + ConfigFlags::ImpressLoad | + ConfigFlags::ImpressSave | + ConfigFlags::UseEnhancedFields | + ConfigFlags::SmartArtShapeLoad | + ConfigFlags::CharBackgroundToHighlighting| + ConfigFlags::CreateMSOLockFiles; + Load(); + } + + void SetFlag( ConfigFlags nFlag, bool bSet ); + bool IsFlag( ConfigFlags nFlag ) const; + void Load() + { + aWriterCfg.Load(); + aCalcCfg.Load(); + aImpressCfg.Load(); + } +}; + +void SvtFilterOptions_Impl::SetFlag( ConfigFlags nFlag, bool bSet ) +{ + switch(nFlag) + { + case ConfigFlags::WordCode: aWriterCfg.SetLoad(bSet);break; + case ConfigFlags::WordStorage: aWriterCfg.SetSave(bSet);break; + case ConfigFlags::WordWbctbl: aWriterCfg.SetLoadExecutable(bSet);break; + case ConfigFlags::ExcelCode: aCalcCfg.SetLoad(bSet);break; + case ConfigFlags::ExcelStorage: aCalcCfg.SetSave(bSet);break; + case ConfigFlags::ExcelExecTbl: aCalcCfg.SetLoadExecutable(bSet);break; + case ConfigFlags::PowerPointCode: aImpressCfg.SetLoad(bSet);break; + case ConfigFlags::PowerPointStorage: aImpressCfg.SetSave(bSet);break; + default: + if( bSet ) + nFlags |= nFlag; + else + nFlags &= ~nFlag; + } +} + +bool SvtFilterOptions_Impl::IsFlag( ConfigFlags nFlag ) const +{ + bool bRet; + switch(nFlag) + { + case ConfigFlags::WordCode : bRet = aWriterCfg.IsLoad();break; + case ConfigFlags::WordStorage : bRet = aWriterCfg.IsSave();break; + case ConfigFlags::WordWbctbl : bRet = aWriterCfg.IsLoadExecutable();break; + case ConfigFlags::ExcelCode : bRet = aCalcCfg.IsLoad();break; + case ConfigFlags::ExcelStorage : bRet = aCalcCfg.IsSave();break; + case ConfigFlags::ExcelExecTbl : bRet = aCalcCfg.IsLoadExecutable();break; + case ConfigFlags::PowerPointCode : bRet = aImpressCfg.IsLoad();break; + case ConfigFlags::PowerPointStorage : bRet = aImpressCfg.IsSave();break; + default: + bRet = bool(nFlags & nFlag ); + } + return bRet; +} + +namespace { + +const Sequence<OUString>& GetPropertyNames() +{ + static Sequence<OUString> const aNames + { + "Import/MathTypeToMath", // 0 + "Import/WinWordToWriter", // 1 + "Import/PowerPointToImpress", // 2 + "Import/ExcelToCalc", // 3 + "Export/MathToMathType", // 4 + "Export/WriterToWinWord", // 5 + "Export/ImpressToPowerPoint", // 6 + "Export/CalcToExcel", // 7 + "Export/EnablePowerPointPreview", // 8 + "Export/EnableExcelPreview", // 9 + "Export/EnableWordPreview", // 10 + "Import/ImportWWFieldsAsEnhancedFields", // 11 + "Import/SmartArtToShapes", // 12 + "Export/CharBackgroundToHighlighting", // 13 + "Import/CreateMSOLockFiles", // 14 + "Import/VisioToDraw" // 15 + }; + return aNames; +} + +} + +SvtFilterOptions::SvtFilterOptions() : + ConfigItem( "Office.Common/Filter/Microsoft" ), + pImpl(new SvtFilterOptions_Impl) +{ + EnableNotification(GetPropertyNames()); + Load(); +} + +SvtFilterOptions::~SvtFilterOptions() +{ +} + +static ConfigFlags lcl_GetFlag(sal_Int32 nProp) +{ + ConfigFlags nFlag = ConfigFlags::NONE; + switch(nProp) + { + case 0: nFlag = ConfigFlags::MathLoad; break; + case 1: nFlag = ConfigFlags::WriterLoad; break; + case 2: nFlag = ConfigFlags::ImpressLoad; break; + case 3: nFlag = ConfigFlags::CalcLoad; break; + case 4: nFlag = ConfigFlags::MathSave; break; + case 5: nFlag = ConfigFlags::WriterSave; break; + case 6: nFlag = ConfigFlags::ImpressSave; break; + case 7: nFlag = ConfigFlags::CalcSave; break; + case 8: nFlag = ConfigFlags::EnablePowerPointPreview; break; + case 9: nFlag = ConfigFlags::EnableExcelPreview; break; + case 10: nFlag = ConfigFlags::EnableWordPreview; break; + case 11: nFlag = ConfigFlags::UseEnhancedFields; break; + case 12: nFlag = ConfigFlags::SmartArtShapeLoad; break; + case 13: nFlag = ConfigFlags::CharBackgroundToHighlighting; break; + case 14: nFlag = ConfigFlags::CreateMSOLockFiles; break; + case 15: + nFlag = ConfigFlags::VisioLoad; + break; + + default: OSL_FAIL("illegal value"); + } + return nFlag; +} + +void SvtFilterOptions::Notify( const Sequence<OUString>& ) +{ + Load(); +} + +void SvtFilterOptions::ImplCommit() +{ + const Sequence<OUString>& aNames = GetPropertyNames(); + Sequence<Any> aValues(aNames.getLength()); + Any* pValues = aValues.getArray(); + + for(int nProp = 0; nProp < aNames.getLength(); nProp++) + { + ConfigFlags nFlag = lcl_GetFlag(nProp); + pValues[nProp] <<= pImpl->IsFlag(nFlag); + + } + PutProperties(aNames, aValues); +} + +void SvtFilterOptions::Load() +{ + pImpl->Load(); + const Sequence<OUString>& rNames = GetPropertyNames(); + Sequence<Any> aValues = GetProperties(rNames); + const Any* pValues = aValues.getConstArray(); + DBG_ASSERT(aValues.getLength() == rNames.getLength(), "GetProperties failed"); + if(aValues.getLength() == rNames.getLength()) + { + for(int nProp = 0; nProp < rNames.getLength(); nProp++) + { + if(pValues[nProp].hasValue()) + { + bool bVal = *o3tl::doAccess<bool>(pValues[nProp]); + ConfigFlags nFlag = lcl_GetFlag(nProp); + pImpl->SetFlag( nFlag, bVal); + } + } + } +} + +void SvtFilterOptions::SetLoadWordBasicCode( bool bFlag ) +{ + pImpl->SetFlag( ConfigFlags::WordCode, bFlag ); + SetModified(); +} + +bool SvtFilterOptions::IsLoadWordBasicCode() const +{ + return pImpl->IsFlag( ConfigFlags::WordCode ); +} + +void SvtFilterOptions::SetLoadWordBasicExecutable( bool bFlag ) +{ + pImpl->SetFlag( ConfigFlags::WordWbctbl, bFlag ); + SetModified(); +} + +bool SvtFilterOptions::IsLoadWordBasicExecutable() const +{ + return pImpl->IsFlag( ConfigFlags::WordWbctbl ); +} + +void SvtFilterOptions::SetLoadWordBasicStorage( bool bFlag ) +{ + pImpl->SetFlag( ConfigFlags::WordStorage, bFlag ); + SetModified(); +} + +bool SvtFilterOptions::IsLoadWordBasicStorage() const +{ + return pImpl->IsFlag( ConfigFlags::WordStorage ); +} + +void SvtFilterOptions::SetLoadExcelBasicCode( bool bFlag ) +{ + pImpl->SetFlag( ConfigFlags::ExcelCode, bFlag ); + SetModified(); +} + +bool SvtFilterOptions::IsLoadExcelBasicCode() const +{ + return pImpl->IsFlag( ConfigFlags::ExcelCode ); +} + +void SvtFilterOptions::SetLoadExcelBasicExecutable( bool bFlag ) +{ + pImpl->SetFlag( ConfigFlags::ExcelExecTbl, bFlag ); + SetModified(); +} + +bool SvtFilterOptions::IsLoadExcelBasicExecutable() const +{ + return pImpl->IsFlag( ConfigFlags::ExcelExecTbl ); +} + +void SvtFilterOptions::SetLoadExcelBasicStorage( bool bFlag ) +{ + pImpl->SetFlag( ConfigFlags::ExcelStorage, bFlag ); + SetModified(); +} + +bool SvtFilterOptions::IsLoadExcelBasicStorage() const +{ + return pImpl->IsFlag( ConfigFlags::ExcelStorage ); +} + +void SvtFilterOptions::SetLoadPPointBasicCode( bool bFlag ) +{ + pImpl->SetFlag( ConfigFlags::PowerPointCode, bFlag ); + SetModified(); +} + +bool SvtFilterOptions::IsLoadPPointBasicCode() const +{ + return pImpl->IsFlag( ConfigFlags::PowerPointCode ); +} + +void SvtFilterOptions::SetLoadPPointBasicStorage( bool bFlag ) +{ + pImpl->SetFlag( ConfigFlags::PowerPointStorage, bFlag ); + SetModified(); +} + +bool SvtFilterOptions::IsLoadPPointBasicStorage() const +{ + return pImpl->IsFlag( ConfigFlags::PowerPointStorage ); +} + +bool SvtFilterOptions::IsMathType2Math() const +{ + return pImpl->IsFlag( ConfigFlags::MathLoad ); +} + +void SvtFilterOptions::SetMathType2Math( bool bFlag ) +{ + pImpl->SetFlag( ConfigFlags::MathLoad, bFlag ); + SetModified(); +} + +bool SvtFilterOptions::IsMath2MathType() const +{ + return pImpl->IsFlag( ConfigFlags::MathSave ); +} + +void SvtFilterOptions::SetMath2MathType( bool bFlag ) +{ + pImpl->SetFlag( ConfigFlags::MathSave, bFlag ); + SetModified(); +} + +bool SvtFilterOptions::IsWinWord2Writer() const +{ + return pImpl->IsFlag( ConfigFlags::WriterLoad ); +} + +void SvtFilterOptions::SetWinWord2Writer( bool bFlag ) +{ + pImpl->SetFlag( ConfigFlags::WriterLoad, bFlag ); + SetModified(); +} + +bool SvtFilterOptions::IsWriter2WinWord() const +{ + return pImpl->IsFlag( ConfigFlags::WriterSave ); +} + +void SvtFilterOptions::SetWriter2WinWord( bool bFlag ) +{ + pImpl->SetFlag( ConfigFlags::WriterSave, bFlag ); + SetModified(); +} + +bool SvtFilterOptions::IsUseEnhancedFields() const +{ + return pImpl->IsFlag( ConfigFlags::UseEnhancedFields ); +} + +bool SvtFilterOptions::IsExcel2Calc() const +{ + return pImpl->IsFlag( ConfigFlags::CalcLoad ); +} + +void SvtFilterOptions::SetExcel2Calc( bool bFlag ) +{ + pImpl->SetFlag( ConfigFlags::CalcLoad, bFlag ); + SetModified(); +} + +bool SvtFilterOptions::IsCalc2Excel() const +{ + return pImpl->IsFlag( ConfigFlags::CalcSave ); +} + +void SvtFilterOptions::SetCalc2Excel( bool bFlag ) +{ + pImpl->SetFlag( ConfigFlags::CalcSave, bFlag ); + SetModified(); +} + +bool SvtFilterOptions::IsPowerPoint2Impress() const +{ + return pImpl->IsFlag( ConfigFlags::ImpressLoad ); +} + +void SvtFilterOptions::SetPowerPoint2Impress( bool bFlag ) +{ + pImpl->SetFlag( ConfigFlags::ImpressLoad, bFlag ); + SetModified(); +} + +bool SvtFilterOptions::IsImpress2PowerPoint() const +{ + return pImpl->IsFlag( ConfigFlags::ImpressSave ); +} + +void SvtFilterOptions::SetImpress2PowerPoint( bool bFlag ) +{ + pImpl->SetFlag( ConfigFlags::ImpressSave, bFlag ); + SetModified(); +} + +bool SvtFilterOptions::IsSmartArt2Shape() const +{ + return pImpl->IsFlag( ConfigFlags::SmartArtShapeLoad ); +} + +void SvtFilterOptions::SetSmartArt2Shape( bool bFlag ) +{ + pImpl->SetFlag( ConfigFlags::SmartArtShapeLoad, bFlag ); + SetModified(); +} + +bool SvtFilterOptions::IsVisio2Draw() const { return pImpl->IsFlag(ConfigFlags::VisioLoad); } + +void SvtFilterOptions::SetVisio2Draw(bool bFlag) +{ + pImpl->SetFlag(ConfigFlags::VisioLoad, bFlag); + SetModified(); +} + +namespace +{ + class theFilterOptions + : public rtl::Static<SvtFilterOptions, theFilterOptions> + { + }; +} + +SvtFilterOptions& SvtFilterOptions::Get() +{ + return theFilterOptions::get(); +} + +bool SvtFilterOptions::IsEnablePPTPreview() const +{ + return pImpl->IsFlag( ConfigFlags::EnablePowerPointPreview ); +} + +bool SvtFilterOptions::IsEnableCalcPreview() const +{ + return pImpl->IsFlag( ConfigFlags::EnableExcelPreview ); +} + +bool SvtFilterOptions::IsEnableWordPreview() const +{ + return pImpl->IsFlag( ConfigFlags::EnableWordPreview ); +} + +bool SvtFilterOptions::IsCharBackground2Highlighting() const +{ + return pImpl->IsFlag( ConfigFlags::CharBackgroundToHighlighting ); +} + +bool SvtFilterOptions::IsCharBackground2Shading() const +{ + return !pImpl->IsFlag( ConfigFlags::CharBackgroundToHighlighting ); +} + +void SvtFilterOptions::SetCharBackground2Highlighting() +{ + pImpl->SetFlag( ConfigFlags::CharBackgroundToHighlighting, true ); + SetModified(); +} + +void SvtFilterOptions::SetCharBackground2Shading() +{ + pImpl->SetFlag( ConfigFlags::CharBackgroundToHighlighting, false ); + SetModified(); +} + +bool SvtFilterOptions::IsMSOLockFileCreationIsEnabled() const +{ + return pImpl->IsFlag( ConfigFlags::CreateMSOLockFiles ); +} + +void SvtFilterOptions::EnableMSOLockFileCreation(bool bEnable) +{ + pImpl->SetFlag( ConfigFlags::CreateMSOLockFiles, bEnable ); + SetModified(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/fontcfg.cxx b/unotools/source/config/fontcfg.cxx new file mode 100644 index 000000000..8cc694bbb --- /dev/null +++ b/unotools/source/config/fontcfg.cxx @@ -0,0 +1,1089 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <i18nlangtag/mslangid.hxx> +#include <i18nlangtag/languagetag.hxx> +#include <o3tl/any.hxx> +#include <unotools/configmgr.hxx> +#include <unotools/fontcfg.hxx> +#include <unotools/fontdefs.hxx> +#include <comphelper/processfactory.hxx> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Sequence.hxx> +#include <com/sun/star/configuration/theDefaultProvider.hpp> +#include <com/sun/star/container/XNameAccess.hpp> +#include <comphelper/propertysequence.hxx> +#include <unotools/syslocale.hxx> +#include <rtl/ustrbuf.hxx> +#include <rtl/instance.hxx> +#include <osl/diagnose.h> +#include <sal/macros.h> +#include <sal/log.hxx> + +#include <string.h> +#include <algorithm> + +using namespace utl; +using namespace com::sun::star::uno; +using namespace com::sun::star::lang; +using namespace com::sun::star::beans; +using namespace com::sun::star::container; +using namespace com::sun::star::configuration; + +/* + * DefaultFontConfiguration + */ + +static const char* getKeyType( DefaultFontType nKeyType ) +{ + switch( nKeyType ) + { + case DefaultFontType::CJK_DISPLAY: return "CJK_DISPLAY"; + case DefaultFontType::CJK_HEADING: return "CJK_HEADING"; + case DefaultFontType::CJK_PRESENTATION: return "CJK_PRESENTATION"; + case DefaultFontType::CJK_SPREADSHEET: return "CJK_SPREADSHEET"; + case DefaultFontType::CJK_TEXT: return "CJK_TEXT"; + case DefaultFontType::CTL_DISPLAY: return "CTL_DISPLAY"; + case DefaultFontType::CTL_HEADING: return "CTL_HEADING"; + case DefaultFontType::CTL_PRESENTATION: return "CTL_PRESENTATION"; + case DefaultFontType::CTL_SPREADSHEET: return "CTL_SPREADSHEET"; + case DefaultFontType::CTL_TEXT: return "CTL_TEXT"; + case DefaultFontType::FIXED: return "FIXED"; + case DefaultFontType::LATIN_DISPLAY: return "LATIN_DISPLAY"; + case DefaultFontType::LATIN_FIXED: return "LATIN_FIXED"; + case DefaultFontType::LATIN_HEADING: return "LATIN_HEADING"; + case DefaultFontType::LATIN_PRESENTATION: return "LATIN_PRESENTATION"; + case DefaultFontType::LATIN_SPREADSHEET: return "LATIN_SPREADSHEET"; + case DefaultFontType::LATIN_TEXT: return "LATIN_TEXT"; + case DefaultFontType::SANS: return "SANS"; + case DefaultFontType::SANS_UNICODE: return "SANS_UNICODE"; + case DefaultFontType::SERIF: return "SERIF"; + case DefaultFontType::SYMBOL: return "SYMBOL"; + case DefaultFontType::UI_FIXED: return "UI_FIXED"; + case DefaultFontType::UI_SANS: return "UI_SANS"; + default: + OSL_FAIL( "unmatched type" ); + return ""; + } +} + +namespace +{ + class theDefaultFontConfiguration + : public rtl::Static<DefaultFontConfiguration, + theDefaultFontConfiguration> + { + }; +} + +DefaultFontConfiguration& DefaultFontConfiguration::get() +{ + return theDefaultFontConfiguration::get(); +} + +DefaultFontConfiguration::DefaultFontConfiguration() +{ + if (utl::ConfigManager::IsFuzzing()) + return; + // create configuration hierarchical access name + try + { + // get service provider + m_xConfigProvider = theDefaultProvider::get(comphelper::getProcessComponentContext()); + Sequence<Any> aArgs(comphelper::InitAnyPropertySequence( + { + {"nodepath", Any(OUString( "/org.openoffice.VCL/DefaultFonts" ))} + })); + m_xConfigAccess = + Reference< XNameAccess >( + m_xConfigProvider->createInstanceWithArguments( "com.sun.star.configuration.ConfigurationAccess", + aArgs ), + UNO_QUERY ); + if( m_xConfigAccess.is() ) + { + const Sequence< OUString > aLocales = m_xConfigAccess->getElementNames(); + // fill config hash with empty interfaces + for( const OUString& rLocaleString : aLocales ) + { + // Feed through LanguageTag for casing. + OUString aLoc( LanguageTag( rLocaleString, true).getBcp47( false)); + m_aConfig[ aLoc ] = LocaleAccess(); + m_aConfig[ aLoc ].aConfigLocaleString = rLocaleString; + } + } + } + catch (const Exception&) + { + // configuration is awry + m_xConfigProvider.clear(); + m_xConfigAccess.clear(); + } + SAL_INFO("unotools.config", "config provider: " << m_xConfigProvider.is() + << ", config access: " << m_xConfigAccess.is()); +} + +DefaultFontConfiguration::~DefaultFontConfiguration() +{ + // release all nodes + m_aConfig.clear(); + // release top node + m_xConfigAccess.clear(); + // release config provider + m_xConfigProvider.clear(); +} + +OUString DefaultFontConfiguration::tryLocale( const OUString& rBcp47, const OUString& rType ) const +{ + OUString aRet; + + std::unordered_map< OUString, LocaleAccess >::const_iterator it = m_aConfig.find( rBcp47 ); + if( it != m_aConfig.end() ) + { + if( !it->second.xAccess.is() ) + { + try + { + Reference< XNameAccess > xNode; + if ( m_xConfigAccess->hasByName( it->second.aConfigLocaleString ) ) + { + Any aAny = m_xConfigAccess->getByName( it->second.aConfigLocaleString ); + if( aAny >>= xNode ) + it->second.xAccess = xNode; + } + } + catch (const NoSuchElementException&) + { + } + catch (const WrappedTargetException&) + { + } + } + if( it->second.xAccess.is() ) + { + try + { + if ( it->second.xAccess->hasByName( rType ) ) + { + Any aAny = it->second.xAccess->getByName( rType ); + aAny >>= aRet; + } + } + catch (const NoSuchElementException&) + { + } + catch (const WrappedTargetException&) + { + } + } + } + + return aRet; +} + +OUString DefaultFontConfiguration::getDefaultFont( const LanguageTag& rLanguageTag, DefaultFontType nType ) const +{ + OUString aType = OUString::createFromAscii( getKeyType( nType ) ); + // Try the simple cases first without constructing fallbacks. + OUString aRet = tryLocale( rLanguageTag.getBcp47(), aType ); + if (aRet.isEmpty()) + { + if (rLanguageTag.isIsoLocale()) + { + if (!rLanguageTag.getCountry().isEmpty()) + { + aRet = tryLocale( rLanguageTag.getLanguage(), aType ); + } + } + else + { + ::std::vector< OUString > aFallbacks( rLanguageTag.getFallbackStrings( false)); + for (const auto& rFallback : aFallbacks) + { + aRet = tryLocale( rFallback, aType ); + if (!aRet.isEmpty()) + break; + } + } + } + if( aRet.isEmpty() ) + { + aRet = tryLocale( "en", aType ); + } + return aRet; +} + +OUString DefaultFontConfiguration::getUserInterfaceFont( const LanguageTag& rLanguageTag ) const +{ + LanguageTag aLanguageTag( rLanguageTag); + if( aLanguageTag.isSystemLocale() ) + aLanguageTag = SvtSysLocale().GetUILanguageTag(); + + OUString aUIFont = getDefaultFont( aLanguageTag, DefaultFontType::UI_SANS ); + + if( !aUIFont.isEmpty() ) + return aUIFont; + + // fallback mechanism (either no configuration or no entry in configuration + + #define FALLBACKFONT_UI_SANS "Andale Sans UI;Albany;Albany AMT;Tahoma;Arial Unicode MS;Arial;Nimbus Sans L;Bitstream Vera Sans;gnu-unifont;Interface User;Geneva;WarpSans;Dialog;Swiss;Lucida;Helvetica;Charcoal;Chicago;MS Sans Serif;Helv;Times;Times New Roman;Interface System" + #define FALLBACKFONT_UI_SANS_LATIN2 "Andale Sans UI;Albany;Albany AMT;Tahoma;Arial Unicode MS;Arial;Nimbus Sans L;Luxi Sans;Bitstream Vera Sans;Interface User;Geneva;WarpSans;Dialog;Swiss;Lucida;Helvetica;Charcoal;Chicago;MS Sans Serif;Helv;Times;Times New Roman;Interface System" + #define FALLBACKFONT_UI_SANS_ARABIC "Tahoma;Traditional Arabic;Simplified Arabic;Lucidasans;Lucida Sans;Supplement;Andale Sans UI;clearlyU;Interface User;Arial Unicode MS;Lucida Sans Unicode;WarpSans;Geneva;MS Sans Serif;Helv;Dialog;Albany;Lucida;Helvetica;Charcoal;Chicago;Arial;Helmet;Interface System;Sans Serif" + #define FALLBACKFONT_UI_SANS_THAI "OONaksit;Tahoma;Lucidasans;Arial Unicode MS" + #define FALLBACKFONT_UI_SANS_KOREAN "Noto Sans CJK KR;Noto Sans KR;Source Han Sans KR;NanumGothic;NanumBarunGothic;NanumBarunGothic YetHangul;KoPubWorld Dotum;Malgun Gothic;Apple SD Gothic Neo;Dotum;Gulim;Apple Gothic;UnDotum;Baekmuk Gulim;Arial Unicode MS;Lucida Sans Unicode;gnu-unifont;Andale Sans UI" + #define FALLBACKFONT_UI_SANS_CHINSIM "Andale Sans UI;Arial Unicode MS;ZYSong18030;AR PL SungtiL GB;AR PL KaitiM GB;SimSun;Lucida Sans Unicode;Fangsong;Hei;Song;Kai;Ming;gnu-unifont;Interface User;" + #define FALLBACKFONT_UI_SANS_CHINTRD "Andale Sans UI;Arial Unicode MS;AR PL Mingti2L Big5;AR PL KaitiM Big5;Kai;PMingLiU;MingLiU;Ming;Lucida Sans Unicode;gnu-unifont;Interface User;" + + const OUString aLanguage( aLanguageTag.getLanguage()); + + // optimize font list for some locales, as long as Andale Sans UI does not support them + if( aLanguage == "ar" || aLanguage == "he" || aLanguage == "iw" ) + { + return FALLBACKFONT_UI_SANS_ARABIC; + } + else if ( aLanguage == "th" ) + { + return FALLBACKFONT_UI_SANS_THAI; + } + else if ( aLanguage == "ko" ) + { + return FALLBACKFONT_UI_SANS_KOREAN; + } + else if( aLanguage == "cs" || + aLanguage == "hu" || + aLanguage == "pl" || + aLanguage == "ro" || + aLanguage == "rm" || + aLanguage == "hr" || + aLanguage == "sk" || + aLanguage == "sl" || + aLanguage == "sb") + { + return FALLBACKFONT_UI_SANS_LATIN2; + } + else + { + const Locale& aLocale( aLanguageTag.getLocale()); + if (MsLangId::isTraditionalChinese(aLocale)) + return FALLBACKFONT_UI_SANS_CHINTRD; + else if (MsLangId::isSimplifiedChinese(aLocale)) + return FALLBACKFONT_UI_SANS_CHINSIM; + } + + return FALLBACKFONT_UI_SANS; +} + +/* + * FontSubstConfigItem::get + */ + +namespace +{ + class theFontSubstConfiguration + : public rtl::Static<FontSubstConfiguration, theFontSubstConfiguration> + { + }; +} + +FontSubstConfiguration& FontSubstConfiguration::get() +{ + return theFontSubstConfiguration::get(); +} + +/* + * FontSubstConfigItem::FontSubstConfigItem + */ + +FontSubstConfiguration::FontSubstConfiguration() : + maSubstHash( 300 ) +{ + if (utl::ConfigManager::IsFuzzing()) + return; + try + { + // get service provider + Reference< XComponentContext > xContext( comphelper::getProcessComponentContext() ); + // create configuration hierarchical access name + m_xConfigProvider = theDefaultProvider::get( xContext ); + Sequence<Any> aArgs(comphelper::InitAnyPropertySequence( + { + {"nodepath", Any(OUString( "/org.openoffice.VCL/FontSubstitutions" ))} + })); + m_xConfigAccess = + Reference< XNameAccess >( + m_xConfigProvider->createInstanceWithArguments( "com.sun.star.configuration.ConfigurationAccess", + aArgs ), + UNO_QUERY ); + if( m_xConfigAccess.is() ) + { + const Sequence< OUString > aLocales = m_xConfigAccess->getElementNames(); + // fill config hash with empty interfaces + for( const OUString& rLocaleString : aLocales ) + { + // Feed through LanguageTag for casing. + OUString aLoc( LanguageTag( rLocaleString, true).getBcp47( false)); + m_aSubst[ aLoc ] = LocaleSubst(); + m_aSubst[ aLoc ].aConfigLocaleString = rLocaleString; + } + } + } + catch (const Exception&) + { + // configuration is awry + m_xConfigProvider.clear(); + m_xConfigAccess.clear(); + } + SAL_INFO("unotools.config", "config provider: " << m_xConfigProvider.is() + << ", config access: " << m_xConfigAccess.is()); +} + +/* + * FontSubstConfigItem::~FontSubstConfigItem + */ + +FontSubstConfiguration::~FontSubstConfiguration() +{ + // release config access + m_xConfigAccess.clear(); + // release config provider + m_xConfigProvider.clear(); +} + +/* + * FontSubstConfigItem::getMapName + */ + +static const char* const aImplKillLeadingList[] = +{ + "microsoft", + "monotype", + "linotype", + "baekmuk", + "adobe", + "nimbus", + "zycjk", + "itc", + "sun", + "amt", + "ms", + "mt", + "cg", + "hg", + "fz", + "ipa", + "sazanami", + "kochi", + nullptr +}; + +static const char* const aImplKillTrailingList[] = +{ + "microsoft", + "monotype", + "linotype", + "adobe", + "nimbus", + "itc", + "sun", + "amt", + "ms", + "mt", + "clm", + // Scripts, for compatibility with older versions + "we", + "cyr", + "tur", + "wt", + "greek", + "wl", + // CJK extensions + "gb", + "big5", + "pro", + "z01", + "z02", + "z03", + "z13", + "b01", + "w3x12", + // Old Printer Fontnames + "5cpi", + "6cpi", + "7cpi", + "8cpi", + "9cpi", + "10cpi", + "11cpi", + "12cpi", + "13cpi", + "14cpi", + "15cpi", + "16cpi", + "18cpi", + "24cpi", + "scale", + "pc", + nullptr +}; + +static const char* const aImplKillTrailingWithExceptionsList[] = +{ + "ce", "monospace", "oldface", nullptr, + "ps", "caps", nullptr, + nullptr +}; + +namespace { + +struct ImplFontAttrWeightSearchData +{ + const char* mpStr; + FontWeight meWeight; +}; + +} + +static ImplFontAttrWeightSearchData const aImplWeightAttrSearchList[] = +{ +// the attribute names are ordered by "first match wins" +// e.g. "semilight" should wins over "semi" +{ "extrablack", WEIGHT_BLACK }, +{ "ultrablack", WEIGHT_BLACK }, +{ "ultrabold", WEIGHT_ULTRABOLD }, +{ "semibold", WEIGHT_SEMIBOLD }, +{ "semilight", WEIGHT_SEMILIGHT }, +{ "semi", WEIGHT_SEMIBOLD }, +{ "demi", WEIGHT_SEMIBOLD }, +{ "black", WEIGHT_BLACK }, +{ "bold", WEIGHT_BOLD }, +{ "heavy", WEIGHT_BLACK }, +{ "ultralight", WEIGHT_ULTRALIGHT }, +{ "light", WEIGHT_LIGHT }, +{ "medium", WEIGHT_MEDIUM }, +{ nullptr, WEIGHT_DONTKNOW }, +}; + +namespace { + +struct ImplFontAttrWidthSearchData +{ + const char* mpStr; + FontWidth meWidth; +}; + +} + +static ImplFontAttrWidthSearchData const aImplWidthAttrSearchList[] = +{ +{ "narrow", WIDTH_CONDENSED }, +{ "semicondensed", WIDTH_SEMI_CONDENSED }, +{ "ultracondensed", WIDTH_ULTRA_CONDENSED }, +{ "semiexpanded", WIDTH_SEMI_EXPANDED }, +{ "ultraexpanded", WIDTH_ULTRA_EXPANDED }, +{ "expanded", WIDTH_EXPANDED }, +{ "wide", WIDTH_ULTRA_EXPANDED }, +{ "condensed", WIDTH_CONDENSED }, +{ "cond", WIDTH_CONDENSED }, +{ "cn", WIDTH_CONDENSED }, +{ nullptr, WIDTH_DONTKNOW }, +}; + +namespace { + +struct ImplFontAttrTypeSearchData +{ + const char* mpStr; + ImplFontAttrs mnType; +}; + +} + +static ImplFontAttrTypeSearchData const aImplTypeAttrSearchList[] = +{ +{ "monotype", ImplFontAttrs::None }, +{ "linotype", ImplFontAttrs::None }, +{ "titling", ImplFontAttrs::Titling }, +{ "captitals", ImplFontAttrs::Capitals }, +{ "captital", ImplFontAttrs::Capitals }, +{ "caps", ImplFontAttrs::Capitals }, +{ "italic", ImplFontAttrs::Italic }, +{ "oblique", ImplFontAttrs::Italic }, +{ "rounded", ImplFontAttrs::Rounded }, +{ "outline", ImplFontAttrs::Outline }, +{ "shadow", ImplFontAttrs::Shadow }, +{ "handwriting", ImplFontAttrs::Handwriting | ImplFontAttrs::Script }, +{ "hand", ImplFontAttrs::Handwriting | ImplFontAttrs::Script }, +{ "signet", ImplFontAttrs::Handwriting | ImplFontAttrs::Script }, +{ "script", ImplFontAttrs::BrushScript | ImplFontAttrs::Script }, +{ "calligraphy", ImplFontAttrs::Chancery | ImplFontAttrs::Script }, +{ "chancery", ImplFontAttrs::Chancery | ImplFontAttrs::Script }, +{ "corsiva", ImplFontAttrs::Chancery | ImplFontAttrs::Script }, +{ "gothic", ImplFontAttrs::SansSerif | ImplFontAttrs::Gothic }, +{ "schoolbook", ImplFontAttrs::Serif | ImplFontAttrs::Schoolbook }, +{ "schlbk", ImplFontAttrs::Serif | ImplFontAttrs::Schoolbook }, +{ "typewriter", ImplFontAttrs::Typewriter | ImplFontAttrs::Fixed }, +{ "lineprinter", ImplFontAttrs::Typewriter | ImplFontAttrs::Fixed }, +{ "monospaced", ImplFontAttrs::Fixed }, +{ "monospace", ImplFontAttrs::Fixed }, +{ "mono", ImplFontAttrs::Fixed }, +{ "fixed", ImplFontAttrs::Fixed }, +{ "sansserif", ImplFontAttrs::SansSerif }, +{ "sans", ImplFontAttrs::SansSerif }, +{ "swiss", ImplFontAttrs::SansSerif }, +{ "serif", ImplFontAttrs::Serif }, +{ "bright", ImplFontAttrs::Serif }, +{ "symbols", ImplFontAttrs::Symbol }, +{ "symbol", ImplFontAttrs::Symbol }, +{ "dingbats", ImplFontAttrs::Symbol }, +{ "dings", ImplFontAttrs::Symbol }, +{ "ding", ImplFontAttrs::Symbol }, +{ "bats", ImplFontAttrs::Symbol }, +{ "math", ImplFontAttrs::Symbol }, +{ "oldstyle", ImplFontAttrs::OtherStyle }, +{ "oldface", ImplFontAttrs::OtherStyle }, +{ "old", ImplFontAttrs::OtherStyle }, +{ "new", ImplFontAttrs::None }, +{ "modern", ImplFontAttrs::None }, +{ "lucida", ImplFontAttrs::None }, +{ "regular", ImplFontAttrs::None }, +{ "extended", ImplFontAttrs::None }, +{ "extra", ImplFontAttrs::OtherStyle }, +{ "ext", ImplFontAttrs::None }, +{ "scalable", ImplFontAttrs::None }, +{ "scale", ImplFontAttrs::None }, +{ "nimbus", ImplFontAttrs::None }, +{ "adobe", ImplFontAttrs::None }, +{ "itc", ImplFontAttrs::None }, +{ "amt", ImplFontAttrs::None }, +{ "mt", ImplFontAttrs::None }, +{ "ms", ImplFontAttrs::None }, +{ "cpi", ImplFontAttrs::None }, +{ "no", ImplFontAttrs::None }, +{ nullptr, ImplFontAttrs::None }, +}; + +static bool ImplKillLeading( OUString& rName, const char* const* ppStr ) +{ + for(; *ppStr; ++ppStr ) + { + const char* pStr = *ppStr; + const sal_Unicode* pNameStr = rName.getStr(); + while ( (*pNameStr == static_cast<sal_Unicode>(static_cast<unsigned char>(*pStr))) && *pStr ) + { + pNameStr++; + pStr++; + } + if ( !*pStr ) + { + sal_Int32 nLen = static_cast<sal_Int32>(pNameStr - rName.getStr()); + rName = rName.copy(nLen); + return true; + } + } + + // special case for Baekmuk + // TODO: allow non-ASCII KillLeading list + const sal_Unicode* pNameStr = rName.getStr(); + if( (pNameStr[0]==0xBC31) && (pNameStr[1]==0xBC35) ) + { + sal_Int32 nLen = (pNameStr[2]==0x0020) ? 3 : 2; + rName = rName.copy(nLen); + return true; + } + + return false; +} + +static sal_Int32 ImplIsTrailing( const OUString& rName, const char* pStr ) +{ + sal_Int32 nStrLen = static_cast<sal_Int32>(strlen( pStr )); + if( nStrLen >= rName.getLength() ) + return 0; + + const sal_Unicode* pEndName = rName.getStr() + rName.getLength(); + const sal_Unicode* pNameStr = pEndName - nStrLen; + do if( *(pNameStr++) != *(pStr++) ) + return 0; + while( *pStr ); + + return nStrLen; +} + +static bool ImplKillTrailing( OUString& rName, const char* const* ppStr ) +{ + for(; *ppStr; ++ppStr ) + { + sal_Int32 nTrailLen = ImplIsTrailing( rName, *ppStr ); + if( nTrailLen ) + { + rName = rName.copy(0, rName.getLength() - nTrailLen ); + return true; + } + } + + return false; +} + +static bool ImplKillTrailingWithExceptions( OUString& rName, const char* const* ppStr ) +{ + for(; *ppStr; ++ppStr ) + { + sal_Int32 nTrailLen = ImplIsTrailing( rName, *ppStr ); + if( nTrailLen ) + { + // check string match against string exceptions + while( *++ppStr ) + if( ImplIsTrailing( rName, *ppStr ) ) + return false; + + rName = rName.copy(0, rName.getLength() - nTrailLen ); + return true; + } + else + { + // skip exception strings + while( *++ppStr ) {} + } + } + + return false; +} + +static bool ImplFindAndErase( OUString& rName, const char* pStr ) +{ + sal_Int32 nLen = static_cast<sal_Int32>(strlen(pStr)); + sal_Int32 nPos = rName.indexOfAsciiL(pStr, nLen ); + if ( nPos < 0 ) + return false; + + OUStringBuffer sBuff(rName); + sBuff.remove(nPos, nLen); + rName = sBuff.makeStringAndClear(); + return true; +} + +void FontSubstConfiguration::getMapName( const OUString& rOrgName, OUString& rShortName, + OUString& rFamilyName, FontWeight& rWeight, + FontWidth& rWidth, ImplFontAttrs& rType ) +{ + rShortName = rOrgName; + + // TODO: get rid of the crazy O(N*strlen) searches below + // they should be possible in O(strlen) + + // Kill leading vendor names and other unimportant data + ImplKillLeading( rShortName, aImplKillLeadingList ); + + // Kill trailing vendor names and other unimportant data + ImplKillTrailing( rShortName, aImplKillTrailingList ); + ImplKillTrailingWithExceptions( rShortName, aImplKillTrailingWithExceptionsList ); + + rFamilyName = rShortName; + + // Kill attributes from the name and update the data + // Weight + const ImplFontAttrWeightSearchData* pWeightList = aImplWeightAttrSearchList; + while ( pWeightList->mpStr ) + { + if ( ImplFindAndErase( rFamilyName, pWeightList->mpStr ) ) + { + if ( (rWeight == WEIGHT_DONTKNOW) || (rWeight == WEIGHT_NORMAL) ) + rWeight = pWeightList->meWeight; + break; + } + pWeightList++; + } + + // Width + const ImplFontAttrWidthSearchData* pWidthList = aImplWidthAttrSearchList; + while ( pWidthList->mpStr ) + { + if ( ImplFindAndErase( rFamilyName, pWidthList->mpStr ) ) + { + if ( (rWidth == WIDTH_DONTKNOW) || (rWidth == WIDTH_NORMAL) ) + rWidth = pWidthList->meWidth; + break; + } + pWidthList++; + } + + // Type + rType = ImplFontAttrs::None; + const ImplFontAttrTypeSearchData* pTypeList = aImplTypeAttrSearchList; + while ( pTypeList->mpStr ) + { + if ( ImplFindAndErase( rFamilyName, pTypeList->mpStr ) ) + rType |= pTypeList->mnType; + pTypeList++; + } + + // Remove numbers + // TODO: also remove localized and fullwidth digits + sal_Int32 i = 0; + OUStringBuffer sBuff(rFamilyName); + while ( i < sBuff.getLength() ) + { + sal_Unicode c = sBuff[ i ]; + if ( (c >= 0x0030) && (c <= 0x0039) ) + sBuff.remove(i, 1); + else + i++; + } +} + +namespace { + +struct StrictStringSort +{ + bool operator()( const FontNameAttr& rLeft, const FontNameAttr& rRight ) + { return rLeft.Name.compareTo( rRight.Name ) < 0; } +}; + +} + +// The entries in this table must match the bits in the ImplFontAttrs enum. + +static const char* const pAttribNames[] = +{ + "default", + "standard", + "normal", + "symbol", + "fixed", + "sansserif", + "serif", + "decorative", + "special", + "italic", + "title", + "capitals", + "cjk", + "cjk_jp", + "cjk_sc", + "cjk_tc", + "cjk_kr", + "ctl", + "nonelatin", + "full", + "outline", + "shadow", + "rounded", + "typewriter", + "script", + "handwriting", + "chancery", + "comic", + "brushscript", + "gothic", + "schoolbook", + "other" +}; + +namespace { + +struct enum_convert +{ + const char* pName; + int nEnum; +}; + +} + +static const enum_convert pWeightNames[] = +{ + { "normal", WEIGHT_NORMAL }, + { "medium", WEIGHT_MEDIUM }, + { "bold", WEIGHT_BOLD }, + { "black", WEIGHT_BLACK }, + { "semibold", WEIGHT_SEMIBOLD }, + { "light", WEIGHT_LIGHT }, + { "semilight", WEIGHT_SEMILIGHT }, + { "ultrabold", WEIGHT_ULTRABOLD }, + { "semi", WEIGHT_SEMIBOLD }, + { "demi", WEIGHT_SEMIBOLD }, + { "heavy", WEIGHT_BLACK }, + { "unknown", WEIGHT_DONTKNOW }, + { "thin", WEIGHT_THIN }, + { "ultralight", WEIGHT_ULTRALIGHT } +}; + +static const enum_convert pWidthNames[] = +{ + { "normal", WIDTH_NORMAL }, + { "condensed", WIDTH_CONDENSED }, + { "expanded", WIDTH_EXPANDED }, + { "unknown", WIDTH_DONTKNOW }, + { "ultracondensed", WIDTH_ULTRA_CONDENSED }, + { "extracondensed", WIDTH_EXTRA_CONDENSED }, + { "semicondensed", WIDTH_SEMI_CONDENSED }, + { "semiexpanded", WIDTH_SEMI_EXPANDED }, + { "extraexpanded", WIDTH_EXTRA_EXPANDED }, + { "ultraexpanded", WIDTH_ULTRA_EXPANDED } +}; + +void FontSubstConfiguration::fillSubstVector( const css::uno::Reference< XNameAccess >& rFont, + const OUString& rType, + std::vector< OUString >& rSubstVector ) const +{ + try + { + Any aAny = rFont->getByName( rType ); + if( auto pLine = o3tl::tryAccess<OUString>(aAny) ) + { + sal_Int32 nLength = pLine->getLength(); + if( nLength ) + { + const sal_Unicode* pStr = pLine->getStr(); + sal_Int32 nTokens = 0; + // count tokens + while( nLength-- ) + { + if( *pStr++ == ';' ) + nTokens++; + } + rSubstVector.clear(); + // optimize performance, heap fragmentation + rSubstVector.reserve( nTokens ); + sal_Int32 nIndex = 0; + while( nIndex != -1 ) + { + OUString aSubst( pLine->getToken( 0, ';', nIndex ) ); + if( !aSubst.isEmpty() ) + { + auto itPair = maSubstHash.insert( aSubst ); + if (!itPair.second) + aSubst = *itPair.first; + rSubstVector.push_back( aSubst ); + } + } + } + } + } + catch (const NoSuchElementException&) + { + } + catch (const WrappedTargetException&) + { + } +} + +FontWeight FontSubstConfiguration::getSubstWeight( const css::uno::Reference< XNameAccess >& rFont, + const OUString& rType ) const +{ + int weight = -1; + try + { + Any aAny = rFont->getByName( rType ); + if( auto pLine = o3tl::tryAccess<OUString>(aAny) ) + { + if( !pLine->isEmpty() ) + { + for( weight=SAL_N_ELEMENTS(pWeightNames)-1; weight >= 0; weight-- ) + if( pLine->equalsIgnoreAsciiCaseAscii( pWeightNames[weight].pName ) ) + break; + } + SAL_WARN_IF(weight < 0, "unotools.config", "Error: invalid weight " << *pLine); + } + } + catch (const NoSuchElementException&) + { + } + catch (const WrappedTargetException&) + { + } + return static_cast<FontWeight>( weight >= 0 ? pWeightNames[weight].nEnum : WEIGHT_DONTKNOW ); +} + +FontWidth FontSubstConfiguration::getSubstWidth( const css::uno::Reference< XNameAccess >& rFont, + const OUString& rType ) const +{ + int width = -1; + try + { + Any aAny = rFont->getByName( rType ); + if( auto pLine = o3tl::tryAccess<OUString>(aAny) ) + { + if( !pLine->isEmpty() ) + { + for( width=SAL_N_ELEMENTS(pWidthNames)-1; width >= 0; width-- ) + if( pLine->equalsIgnoreAsciiCaseAscii( pWidthNames[width].pName ) ) + break; + } + SAL_WARN_IF( width < 0, "unotools.config", "Error: invalid width " << *pLine); + } + } + catch (const NoSuchElementException&) + { + } + catch (const WrappedTargetException&) + { + } + return static_cast<FontWidth>( width >= 0 ? pWidthNames[width].nEnum : WIDTH_DONTKNOW ); +} + +ImplFontAttrs FontSubstConfiguration::getSubstType( const css::uno::Reference< XNameAccess >& rFont, + const OUString& rType ) const +{ + sal_uLong type = 0; + try + { + Any aAny = rFont->getByName( rType ); + auto pLine = o3tl::tryAccess<OUString>(aAny); + if( !pLine ) + return ImplFontAttrs::None; + if( pLine->isEmpty() ) + return ImplFontAttrs::None; + sal_Int32 nIndex = 0; + while( nIndex != -1 ) + { + OUString aToken( pLine->getToken( 0, ',', nIndex ) ); + for( int k = 0; k < 32; k++ ) + if( aToken.equalsIgnoreAsciiCaseAscii( pAttribNames[k] ) ) + { + type |= sal_uLong(1) << k; + break; + } + } + assert(((type & ~o3tl::typed_flags<ImplFontAttrs>::mask) == 0) && "invalid font attributes"); + } + catch (const NoSuchElementException&) + { + } + catch (const WrappedTargetException&) + { + } + + return static_cast<ImplFontAttrs>(type); +} + +void FontSubstConfiguration::readLocaleSubst( const OUString& rBcp47 ) const +{ + std::unordered_map< OUString, LocaleSubst >::const_iterator it = m_aSubst.find( rBcp47 ); + if( it != m_aSubst.end() ) + { + if( ! it->second.bConfigRead ) + { + it->second.bConfigRead = true; + Reference< XNameAccess > xNode; + try + { + Any aAny = m_xConfigAccess->getByName( it->second.aConfigLocaleString ); + aAny >>= xNode; + } + catch (const NoSuchElementException&) + { + } + catch (const WrappedTargetException&) + { + } + if( xNode.is() ) + { + const Sequence< OUString > aFonts = xNode->getElementNames(); + int nFonts = aFonts.getLength(); + // improve performance, heap fragmentation + it->second.aSubstAttributes.reserve( nFonts ); + + // strings for subst retrieval, construct only once + OUString const aSubstFontsStr ( "SubstFonts" ); + OUString const aSubstFontsMSStr ( "SubstFontsMS" ); + OUString const aSubstWeightStr ( "FontWeight" ); + OUString const aSubstWidthStr ( "FontWidth" ); + OUString const aSubstTypeStr ( "FontType" ); + for( const OUString& rFontName : aFonts ) + { + Reference< XNameAccess > xFont; + try + { + Any aAny = xNode->getByName( rFontName ); + aAny >>= xFont; + } + catch (const NoSuchElementException&) + { + } + catch (const WrappedTargetException&) + { + } + if( ! xFont.is() ) + { + SAL_WARN("unotools.config", "did not get font attributes for " << rFontName); + continue; + } + + FontNameAttr aAttr; + // read subst attributes from config + aAttr.Name = rFontName; + fillSubstVector( xFont, aSubstFontsStr, aAttr.Substitutions ); + fillSubstVector( xFont, aSubstFontsMSStr, aAttr.MSSubstitutions ); + aAttr.Weight = getSubstWeight( xFont, aSubstWeightStr ); + aAttr.Width = getSubstWidth( xFont, aSubstWidthStr ); + aAttr.Type = getSubstType( xFont, aSubstTypeStr ); + + // finally insert this entry + it->second.aSubstAttributes.push_back( aAttr ); + } + std::sort( it->second.aSubstAttributes.begin(), it->second.aSubstAttributes.end(), StrictStringSort() ); + } + } + } +} + +const FontNameAttr* FontSubstConfiguration::getSubstInfo( const OUString& rFontName ) const +{ + if( rFontName.isEmpty() ) + return nullptr; + + // search if a (language dep.) replacement table for the given font exists + // fallback is english + OUString aSearchFont( rFontName.toAsciiLowerCase() ); + FontNameAttr aSearchAttr; + aSearchAttr.Name = aSearchFont; + + LanguageTag aLanguageTag("en"); + + if( aLanguageTag.isSystemLocale() ) + aLanguageTag = SvtSysLocale().GetUILanguageTag(); + + ::std::vector< OUString > aFallbacks( aLanguageTag.getFallbackStrings( true)); + if (aLanguageTag.getLanguage() != "en") + aFallbacks.emplace_back("en"); + + for (const auto& rFallback : aFallbacks) + { + std::unordered_map< OUString, LocaleSubst >::const_iterator lang = m_aSubst.find( rFallback ); + if( lang != m_aSubst.end() ) + { + if( ! lang->second.bConfigRead ) + readLocaleSubst( rFallback ); + // try to find an exact match + // because the list is sorted this will also find fontnames of the form searchfontname* + std::vector< FontNameAttr >::const_iterator it = ::std::lower_bound( lang->second.aSubstAttributes.begin(), lang->second.aSubstAttributes.end(), aSearchAttr, StrictStringSort() ); + if( it != lang->second.aSubstAttributes.end()) + { + const FontNameAttr& rFoundAttr = *it; + // a search for "abcblack" may match with an entry for "abc" + // the reverse is not a good idea (e.g. #i112731# alba->albani) + if( rFoundAttr.Name.getLength() <= aSearchFont.getLength() ) + if( aSearchFont.startsWith( rFoundAttr.Name)) + return &rFoundAttr; + } + } + } + return nullptr; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/fontoptions.cxx b/unotools/source/config/fontoptions.cxx new file mode 100644 index 000000000..c9fa6ec48 --- /dev/null +++ b/unotools/source/config/fontoptions.cxx @@ -0,0 +1,302 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unotools/fontoptions.hxx> +#include <unotools/configitem.hxx> +#include <tools/debug.hxx> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Sequence.hxx> + +#include <rtl/instance.hxx> +#include "itemholder1.hxx" + +using namespace ::utl; +using namespace ::osl; +using namespace ::com::sun::star::uno; + +#define ROOTNODE_FONT "Office.Common/Font" + +#define PROPERTYNAME_REPLACEMENTTABLE "Substitution/Replacement" +#define PROPERTYNAME_FONTHISTORY "View/History" +#define PROPERTYNAME_FONTWYSIWYG "View/ShowFontBoxWYSIWYG" + +#define PROPERTYHANDLE_REPLACEMENTTABLE 0 +#define PROPERTYHANDLE_FONTHISTORY 1 +#define PROPERTYHANDLE_FONTWYSIWYG 2 + +class SvtFontOptions_Impl : public ConfigItem +{ + public: + + SvtFontOptions_Impl(); + virtual ~SvtFontOptions_Impl() override; + + /*-**************************************************************************************************** + @short called for notify of configmanager + @descr This method is called from the ConfigManager before the application ends or from the + PropertyChangeListener if the sub tree broadcasts changes. You must update your + internal values. + + @seealso baseclass ConfigItem + + @param "seqPropertyNames" is the list of properties which should be updated. + *//*-*****************************************************************************************************/ + + virtual void Notify( const Sequence< OUString >& seqPropertyNames ) override; + + /*-**************************************************************************************************** + @short access method to get internal values + @descr These method give us a chance to regulate access to our internal values. + It's not used in the moment - but it's possible for the feature! + *//*-*****************************************************************************************************/ + + bool IsFontHistoryEnabled ( ) const { return m_bFontHistory;} + + bool IsFontWYSIWYGEnabled ( ) const { return m_bFontWYSIWYG;} + void EnableFontWYSIWYG ( bool bState ); + + private: + + virtual void ImplCommit() override; + + /*-**************************************************************************************************** + @short return list of key names of our configuration management which represent our module tree + @descr This method returns a static const list of key names. We need it to get needed values from our + configuration management. + @return A list of needed configuration keys is returned. + *//*-*****************************************************************************************************/ + + static Sequence< OUString > impl_GetPropertyNames(); + + private: + + bool m_bReplacementTable; + bool m_bFontHistory; + bool m_bFontWYSIWYG; +}; + +// constructor + +SvtFontOptions_Impl::SvtFontOptions_Impl() + // Init baseclasses first + : ConfigItem ( ROOTNODE_FONT ) + // Init member then. + , m_bReplacementTable ( false ) + , m_bFontHistory ( false ) + , m_bFontWYSIWYG ( false ) +{ + // Use our static list of configuration keys to get his values. + Sequence< OUString > seqNames = impl_GetPropertyNames ( ); + Sequence< Any > seqValues = GetProperties ( seqNames ); + + // Safe impossible cases. + // We need values from ALL configuration keys. + // Follow assignment use order of values in relation to our list of key names! + DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nI miss some values of configuration keys!\n" ); + + // Copy values from list in right order to our internal member. + sal_Int32 nPropertyCount = seqValues.getLength(); + for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty ) + { + // Safe impossible cases. + // Check any for valid value. + DBG_ASSERT( seqValues[nProperty].hasValue(), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nInvalid property value detected!\n" ); + switch( nProperty ) + { + case PROPERTYHANDLE_REPLACEMENTTABLE : { + DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\Substitution\\Replacement\"?" ); + seqValues[nProperty] >>= m_bReplacementTable; + } + break; + case PROPERTYHANDLE_FONTHISTORY : { + DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\View\\History\"?" ); + seqValues[nProperty] >>= m_bFontHistory; + } + break; + case PROPERTYHANDLE_FONTWYSIWYG : { + DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::SvtFontOptions_Impl()\nWho has changed the value type of \"Office.Common\\Font\\View\\ShowFontBoxWYSIWYG\"?" ); + seqValues[nProperty] >>= m_bFontWYSIWYG; + } + break; + } + } + + // Enable notification mechanism of our baseclass. + // We need it to get information about changes outside these class on our used configuration keys! + EnableNotification( seqNames ); +} + +// destructor + +SvtFontOptions_Impl::~SvtFontOptions_Impl() +{ + assert(!IsModified()); // should have been committed +} + +// public method + +void SvtFontOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames ) +{ + // Use given list of updated properties to get his values from configuration directly! + Sequence< Any > seqValues = GetProperties( seqPropertyNames ); + // Safe impossible cases. + // We need values from ALL notified configuration keys. + DBG_ASSERT( !(seqPropertyNames.getLength()!=seqValues.getLength()), "SvtFontOptions_Impl::Notify()\nI miss some values of configuration keys!\n" ); + // Step over list of property names and get right value from coreesponding value list to set it on internal members! + sal_Int32 nCount = seqPropertyNames.getLength(); + for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty ) + { + if( seqPropertyNames[nProperty] == PROPERTYNAME_REPLACEMENTTABLE ) + { + DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\Substitution\\Replacement\"?" ); + seqValues[nProperty] >>= m_bReplacementTable; + } + else + if( seqPropertyNames[nProperty] == PROPERTYNAME_FONTHISTORY ) + { + DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\View\\History\"?" ); + seqValues[nProperty] >>= m_bFontHistory; + } + else + if( seqPropertyNames[nProperty] == PROPERTYNAME_FONTWYSIWYG ) + { + DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "SvtFontOptions_Impl::Notify()\nWho has changed the value type of \"Office.Common\\Font\\View\\ShowFontBoxWYSIWYG\"?" ); + seqValues[nProperty] >>= m_bFontWYSIWYG; + } +#if OSL_DEBUG_LEVEL > 0 + else assert(false && "Unknown property detected ... I can't handle these!"); +#endif + } +} + +// public method + +void SvtFontOptions_Impl::ImplCommit() +{ + // Get names of supported properties, create a list for values and copy current values to it. + Sequence< OUString > seqNames = impl_GetPropertyNames(); + sal_Int32 nCount = seqNames.getLength(); + Sequence< Any > seqValues ( nCount ); + for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty ) + { + switch( nProperty ) + { + case PROPERTYHANDLE_REPLACEMENTTABLE : { + seqValues[nProperty] <<= m_bReplacementTable; + } + break; + case PROPERTYHANDLE_FONTHISTORY : { + seqValues[nProperty] <<= m_bFontHistory; + } + break; + case PROPERTYHANDLE_FONTWYSIWYG : { + seqValues[nProperty] <<= m_bFontWYSIWYG; + } + break; + } + } + // Set properties in configuration. + PutProperties( seqNames, seqValues ); +} + +// public method + +void SvtFontOptions_Impl::EnableFontWYSIWYG( bool bState ) +{ + m_bFontWYSIWYG = bState; + SetModified(); +} + +// private method + +Sequence< OUString > SvtFontOptions_Impl::impl_GetPropertyNames() +{ + return Sequence< OUString > + { + PROPERTYNAME_REPLACEMENTTABLE , + PROPERTYNAME_FONTHISTORY , + PROPERTYNAME_FONTWYSIWYG , + }; +} + +namespace { + +std::weak_ptr<SvtFontOptions_Impl> g_pFontOptions; + +} + +SvtFontOptions::SvtFontOptions() +{ + // Global access, must be guarded (multithreading!). + MutexGuard aGuard( impl_GetOwnStaticMutex() ); + + m_pImpl = g_pFontOptions.lock(); + if( !m_pImpl ) + { + m_pImpl = std::make_shared<SvtFontOptions_Impl>(); + g_pFontOptions = m_pImpl; + ItemHolder1::holdConfigItem(EItem::FontOptions); + } +} + +SvtFontOptions::~SvtFontOptions() +{ + // Global access, must be guarded (multithreading!) + MutexGuard aGuard( impl_GetOwnStaticMutex() ); + + m_pImpl.reset(); +} + +// public method + +bool SvtFontOptions::IsFontHistoryEnabled() const +{ + MutexGuard aGuard( impl_GetOwnStaticMutex() ); + return m_pImpl->IsFontHistoryEnabled(); +} + +// public method + +bool SvtFontOptions::IsFontWYSIWYGEnabled() const +{ + MutexGuard aGuard( impl_GetOwnStaticMutex() ); + return m_pImpl->IsFontWYSIWYGEnabled(); +} + +// public method + +void SvtFontOptions::EnableFontWYSIWYG( bool bState ) +{ + MutexGuard aGuard( impl_GetOwnStaticMutex() ); + m_pImpl->EnableFontWYSIWYG( bState ); +} + +namespace +{ + class theFontOptionsMutex : public rtl::Static<osl::Mutex, theFontOptionsMutex> {}; +} + +// private method + +Mutex& SvtFontOptions::impl_GetOwnStaticMutex() +{ + return theFontOptionsMutex::get(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/historyoptions.cxx b/unotools/source/config/historyoptions.cxx new file mode 100644 index 000000000..986e39898 --- /dev/null +++ b/unotools/source/config/historyoptions.cxx @@ -0,0 +1,568 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unotools/historyoptions.hxx> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Sequence.hxx> + +#include <cassert> +#include <algorithm> + +#include "itemholder1.hxx" + +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/container/XNameAccess.hpp> +#include <com/sun/star/container/XNameContainer.hpp> +#include <com/sun/star/lang/XSingleServiceFactory.hpp> +#include <comphelper/configurationhelper.hxx> +#include <comphelper/processfactory.hxx> +#include <tools/diagnose_ex.h> +#include <optional> + +using namespace ::std; +using namespace ::utl; +using namespace ::osl; +using namespace ::com::sun::star; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::beans; + +namespace { + static const ::sal_Int32 s_nOffsetURL = 0; + static const ::sal_Int32 s_nOffsetFilter = 1; + static const ::sal_Int32 s_nOffsetTitle = 2; + static const ::sal_Int32 s_nOffsetPassword = 3; + static const ::sal_Int32 s_nOffsetThumbnail = 4; + + const char s_sItemList[] = "ItemList"; + const char s_sOrderList[] = "OrderList"; + const char s_sHistoryItemRef[] = "HistoryItemRef"; + const char s_sFilter[] = "Filter"; + const char s_sTitle[] = "Title"; + const char s_sPassword[] = "Password"; + const char s_sThumbnail[] = "Thumbnail"; + + class theHistoryOptionsMutex : public rtl::Static<osl::Mutex, theHistoryOptionsMutex>{}; +} + +/// Internal implementation of the SvtHistoryOptions. +class SvtHistoryOptions_Impl +{ +public: + SvtHistoryOptions_Impl(); + + /// Returns the maximum size of the internal lists, ie. the capacity not the size. + sal_uInt32 GetCapacity(EHistoryType eHistory) const; + + /// Clear the specified history list. + void Clear(EHistoryType eHistory); + + /// Get a sequence list from the items. + Sequence< Sequence<PropertyValue> > GetList(EHistoryType eHistory); + + void AppendItem(EHistoryType eHistory, + const OUString& sURL, const OUString& sFilter, const OUString& sTitle, + const std::optional<OUString>& sThumbnail); + + void DeleteItem(EHistoryType eHistory, const OUString& sURL); + +private: + /// Return the appropriate list of recent documents (based on eHistory). + uno::Reference<container::XNameAccess> GetListAccess(EHistoryType eHistory) const; + + void impl_truncateList(EHistoryType eHistory, sal_uInt32 nSize); + +private: + uno::Reference<container::XNameAccess> m_xCfg; + uno::Reference<container::XNameAccess> m_xCommonXCU; +}; + +SvtHistoryOptions_Impl::SvtHistoryOptions_Impl() +{ + try + { + m_xCfg.set( + ::comphelper::ConfigurationHelper::openConfig( + ::comphelper::getProcessComponentContext(), + "org.openoffice.Office.Histories/Histories", + ::comphelper::EConfigurationModes::Standard), + uno::UNO_QUERY); + + m_xCommonXCU.set( + ::comphelper::ConfigurationHelper::openConfig( + ::comphelper::getProcessComponentContext(), + "org.openoffice.Office.Common/History", + ::comphelper::EConfigurationModes::Standard), + uno::UNO_QUERY); + } + catch(const uno::Exception&) + { + DBG_UNHANDLED_EXCEPTION("unotools.config"); + m_xCfg.clear(); + m_xCommonXCU.clear(); + } +} + +sal_uInt32 SvtHistoryOptions_Impl::GetCapacity(EHistoryType eHistory) const +{ + uno::Reference<beans::XPropertySet> xListAccess(m_xCommonXCU, uno::UNO_QUERY); + + if (!xListAccess.is()) + return 0; + + sal_uInt32 nSize = 0; + + try + { + switch (eHistory) + { + case ePICKLIST: + xListAccess->getPropertyValue("PickListSize") >>= nSize; + break; + + case eHELPBOOKMARKS: + xListAccess->getPropertyValue("HelpBookmarkSize") >>= nSize; + break; + + default: + break; + } + } + catch (const uno::Exception&) + { + DBG_UNHANDLED_EXCEPTION("unotools.config"); + } + + return nSize; +} + +uno::Reference<container::XNameAccess> SvtHistoryOptions_Impl::GetListAccess(EHistoryType eHistory) const +{ + uno::Reference<container::XNameAccess> xListAccess; + + try + { + switch (eHistory) + { + case ePICKLIST: + m_xCfg->getByName("PickList") >>= xListAccess; + break; + + case eHELPBOOKMARKS: + m_xCfg->getByName("HelpBookmarks") >>= xListAccess; + break; + + default: + break; + } + } + catch (const uno::Exception&) + { + DBG_UNHANDLED_EXCEPTION("unotools.config"); + } + + return xListAccess; +} + +void SvtHistoryOptions_Impl::impl_truncateList(EHistoryType eHistory, sal_uInt32 nSize) +{ + uno::Reference<container::XNameAccess> xList(GetListAccess(eHistory)); + if (!xList.is()) + return; + + uno::Reference<container::XNameContainer> xItemList; + uno::Reference<container::XNameContainer> xOrderList; + uno::Reference<beans::XPropertySet> xSet; + + try + { + xList->getByName(s_sOrderList) >>= xOrderList; + xList->getByName(s_sItemList) >>= xItemList; + + const sal_uInt32 nLength = xOrderList->getElementNames().getLength(); + if (nSize < nLength) + { + for (sal_uInt32 i=nLength-1; i>=nSize; --i) + { + OUString sTmp; + const OUString sRemove = OUString::number(i); + xOrderList->getByName(sRemove) >>= xSet; + xSet->getPropertyValue(s_sHistoryItemRef) >>= sTmp; + xItemList->removeByName(sTmp); + xOrderList->removeByName(sRemove); + } + + ::comphelper::ConfigurationHelper::flush(m_xCfg); + } + } + catch(const uno::Exception&) + { + DBG_UNHANDLED_EXCEPTION("unotools.config"); + } +} + +void SvtHistoryOptions_Impl::Clear( EHistoryType eHistory ) +{ + uno::Reference<container::XNameAccess> xListAccess(GetListAccess(eHistory)); + if (!xListAccess.is()) + return; + + uno::Reference<container::XNameContainer> xNode; + + try + { + // clear ItemList + xListAccess->getByName(s_sItemList) >>= xNode; + Sequence<OUString> aStrings(xNode->getElementNames()); + + for (const auto& rString : std::as_const(aStrings)) + xNode->removeByName(rString); + + // clear OrderList + xListAccess->getByName(s_sOrderList) >>= xNode; + aStrings = xNode->getElementNames(); + + for (const auto& rString : std::as_const(aStrings)) + xNode->removeByName(rString); + + ::comphelper::ConfigurationHelper::flush(m_xCfg); + } + catch(const uno::Exception&) + { + DBG_UNHANDLED_EXCEPTION("unotools.config"); + } +} + +Sequence< Sequence<PropertyValue> > SvtHistoryOptions_Impl::GetList(EHistoryType eHistory) +{ + uno::Reference<container::XNameAccess> xListAccess(GetListAccess(eHistory)); + if (!xListAccess.is()) + return Sequence< Sequence<PropertyValue> >(); + + impl_truncateList(eHistory, GetCapacity(eHistory)); + + Sequence<PropertyValue> seqProperties(5); + seqProperties[s_nOffsetURL ].Name = HISTORY_PROPERTYNAME_URL; + seqProperties[s_nOffsetFilter ].Name = HISTORY_PROPERTYNAME_FILTER; + seqProperties[s_nOffsetTitle ].Name = HISTORY_PROPERTYNAME_TITLE; + seqProperties[s_nOffsetPassword ].Name = HISTORY_PROPERTYNAME_PASSWORD; + seqProperties[s_nOffsetThumbnail ].Name = HISTORY_PROPERTYNAME_THUMBNAIL; + + uno::Reference<container::XNameAccess> xItemList; + uno::Reference<container::XNameAccess> xOrderList; + try + { + xListAccess->getByName(s_sItemList) >>= xItemList; + xListAccess->getByName(s_sOrderList) >>= xOrderList; + } + catch(const uno::Exception&) + { + DBG_UNHANDLED_EXCEPTION("unotools.config"); + } + + const sal_Int32 nLength = xOrderList->getElementNames().getLength(); + Sequence< Sequence<PropertyValue> > aRet(nLength); + sal_Int32 nCount = 0; + + for (sal_Int32 nItem = 0; nItem < nLength; ++nItem) + { + try + { + OUString sUrl; + uno::Reference<beans::XPropertySet> xSet; + xOrderList->getByName(OUString::number(nItem)) >>= xSet; + xSet->getPropertyValue(s_sHistoryItemRef) >>= sUrl; + + xItemList->getByName(sUrl) >>= xSet; + seqProperties[s_nOffsetURL ].Value <<= sUrl; + + seqProperties[s_nOffsetFilter ].Value = xSet->getPropertyValue(s_sFilter); + seqProperties[s_nOffsetTitle ].Value = xSet->getPropertyValue(s_sTitle); + seqProperties[s_nOffsetPassword ].Value = xSet->getPropertyValue(s_sPassword); + seqProperties[s_nOffsetThumbnail].Value = xSet->getPropertyValue(s_sThumbnail); + aRet[nCount++] = seqProperties; + } + catch(const uno::Exception&) + { + // <https://bugs.libreoffice.org/show_bug.cgi?id=46074> + // "FILEOPEN: No Recent Documents..." discusses a problem + // with corrupted /org.openoffice.Office/Histories/Histories + // configuration items; to work around that problem, simply + // ignore such corrupted individual items here, so that at + // least newly added items are successfully reported back + // from this function: + DBG_UNHANDLED_EXCEPTION("unotools.config"); + } + } + assert(nCount <= nLength); + aRet.realloc(nCount); + return aRet; +} + +void SvtHistoryOptions_Impl::AppendItem(EHistoryType eHistory, + const OUString& sURL, const OUString& sFilter, const OUString& sTitle, + const std::optional<OUString>& sThumbnail) +{ + uno::Reference<container::XNameAccess> xListAccess(GetListAccess(eHistory)); + if (!xListAccess.is()) + return; + + impl_truncateList(eHistory, GetCapacity(eHistory)); + + sal_Int32 nMaxSize = GetCapacity(eHistory); + if (nMaxSize == 0) + return; + + uno::Reference<container::XNameContainer> xItemList; + uno::Reference<container::XNameContainer> xOrderList; + uno::Reference<beans::XPropertySet> xSet; + + try + { + xListAccess->getByName(s_sItemList) >>= xItemList; + xListAccess->getByName(s_sOrderList) >>= xOrderList; + sal_Int32 nLength = xOrderList->getElementNames().getLength(); + + // The item to be appended already exists + if (xItemList->hasByName(sURL)) + { + if (sThumbnail) + { + // update the thumbnail + xItemList->getByName(sURL) >>= xSet; + xSet->setPropertyValue(s_sThumbnail, uno::makeAny(*sThumbnail)); + } + + for (sal_Int32 i=0; i<nLength; ++i) + { + OUString aItem; + xOrderList->getByName(OUString::number(i)) >>= xSet; + xSet->getPropertyValue(s_sHistoryItemRef) >>= aItem; + + if (aItem == sURL) + { + for (sal_Int32 j = i - 1; j >= 0; --j) + { + uno::Reference<beans::XPropertySet> xPrevSet; + uno::Reference<beans::XPropertySet> xNextSet; + xOrderList->getByName(OUString::number(j+1)) >>= xPrevSet; + xOrderList->getByName(OUString::number(j)) >>= xNextSet; + + OUString sTemp; + xNextSet->getPropertyValue(s_sHistoryItemRef) >>= sTemp; + xPrevSet->setPropertyValue(s_sHistoryItemRef, uno::makeAny(sTemp)); + } + xOrderList->getByName(OUString::number(0)) >>= xSet; + xSet->setPropertyValue(s_sHistoryItemRef, uno::makeAny(aItem)); + break; + } + } + + ::comphelper::ConfigurationHelper::flush(m_xCfg); + } + else // The item to be appended does not exist yet + { + uno::Reference<lang::XSingleServiceFactory> xFac; + uno::Reference<uno::XInterface> xInst; + uno::Reference<beans::XPropertySet> xPrevSet; + uno::Reference<beans::XPropertySet> xNextSet; + + // Append new item to OrderList. + if ( nLength == nMaxSize ) + { + OUString sRemove; + xOrderList->getByName(OUString::number(nLength-1)) >>= xSet; + xSet->getPropertyValue(s_sHistoryItemRef) >>= sRemove; + try + { + xItemList->removeByName(sRemove); + } + catch (container::NoSuchElementException &) + { + // <https://bugs.libreoffice.org/show_bug.cgi?id=46074> + // "FILEOPEN: No Recent Documents..." discusses a problem + // with corrupted /org.openoffice.Office/Histories/Histories + // configuration items; to work around that problem, simply + // ignore such corrupted individual items here, so that at + // least newly added items are successfully added: + if (!sRemove.isEmpty()) + { + throw; + } + } + } + if (nLength != nMaxSize) + { + xFac.set(xOrderList, uno::UNO_QUERY); + xInst = xFac->createInstance(); + OUString sPush = OUString::number(nLength++); + xOrderList->insertByName(sPush, uno::makeAny(xInst)); + } + for (sal_Int32 j=nLength-1; j>0; --j) + { + xOrderList->getByName( OUString::number(j) ) >>= xPrevSet; + xOrderList->getByName( OUString::number(j-1) ) >>= xNextSet; + OUString sTemp; + xNextSet->getPropertyValue(s_sHistoryItemRef) >>= sTemp; + xPrevSet->setPropertyValue(s_sHistoryItemRef, uno::makeAny(sTemp)); + } + xOrderList->getByName( OUString::number(0) ) >>= xSet; + xSet->setPropertyValue(s_sHistoryItemRef, uno::makeAny(sURL)); + + // Append the item to ItemList. + xFac.set(xItemList, uno::UNO_QUERY); + xInst = xFac->createInstance(); + xItemList->insertByName(sURL, uno::makeAny(xInst)); + + xSet.set(xInst, uno::UNO_QUERY); + xSet->setPropertyValue(s_sFilter, uno::makeAny(sFilter)); + xSet->setPropertyValue(s_sTitle, uno::makeAny(sTitle)); + xSet->setPropertyValue(s_sPassword, uno::makeAny(OUString())); + xSet->setPropertyValue(s_sThumbnail, uno::makeAny(sThumbnail.value_or(OUString()))); + + ::comphelper::ConfigurationHelper::flush(m_xCfg); + } + } + catch(const uno::Exception&) + { + DBG_UNHANDLED_EXCEPTION("unotools.config"); + } +} + +void SvtHistoryOptions_Impl::DeleteItem(EHistoryType eHistory, const OUString& sURL) +{ + uno::Reference<container::XNameAccess> xListAccess(GetListAccess(eHistory)); + if (!xListAccess.is()) + return; + + uno::Reference<container::XNameContainer> xItemList; + uno::Reference<container::XNameContainer> xOrderList; + uno::Reference<beans::XPropertySet> xSet; + + try + { + xListAccess->getByName(s_sItemList) >>= xItemList; + xListAccess->getByName(s_sOrderList) >>= xOrderList; + sal_Int32 nLength = xOrderList->getElementNames().getLength(); + + // if it does not exist, nothing to do + if (!xItemList->hasByName(sURL)) + return; + + // it's the last one, just clear the lists + if (nLength == 1) + { + Clear(eHistory); + return; + } + + // find it in the OrderList + sal_Int32 nFromWhere = 0; + for (; nFromWhere < nLength - 1; ++nFromWhere) + { + OUString aItem; + xOrderList->getByName(OUString::number(nFromWhere)) >>= xSet; + xSet->getPropertyValue(s_sHistoryItemRef) >>= aItem; + + if (aItem == sURL) + break; + } + + // and shift the rest of the items in OrderList accordingly + for (sal_Int32 i = nFromWhere; i < nLength - 1; ++i) + { + uno::Reference<beans::XPropertySet> xPrevSet; + uno::Reference<beans::XPropertySet> xNextSet; + xOrderList->getByName(OUString::number(i)) >>= xPrevSet; + xOrderList->getByName(OUString::number(i + 1)) >>= xNextSet; + + OUString sTemp; + xNextSet->getPropertyValue(s_sHistoryItemRef) >>= sTemp; + xPrevSet->setPropertyValue(s_sHistoryItemRef, uno::makeAny(sTemp)); + } + xOrderList->removeByName(OUString::number(nLength - 1)); + + // and finally remove it from the ItemList + xItemList->removeByName(sURL); + + ::comphelper::ConfigurationHelper::flush(m_xCfg); + } + catch (const uno::Exception&) + { + DBG_UNHANDLED_EXCEPTION("unotools.config"); + } +} + +namespace { + +std::weak_ptr<SvtHistoryOptions_Impl> g_pHistoryOptions; + +} + +SvtHistoryOptions::SvtHistoryOptions() +{ + MutexGuard aGuard(theHistoryOptionsMutex::get()); + + m_pImpl = g_pHistoryOptions.lock(); + if( !m_pImpl ) + { + m_pImpl = std::make_shared<SvtHistoryOptions_Impl>(); + g_pHistoryOptions = m_pImpl; + ItemHolder1::holdConfigItem(EItem::HistoryOptions); + } +} + +SvtHistoryOptions::~SvtHistoryOptions() +{ + MutexGuard aGuard(theHistoryOptionsMutex::get()); + + m_pImpl.reset(); +} + +void SvtHistoryOptions::Clear( EHistoryType eHistory ) +{ + MutexGuard aGuard(theHistoryOptionsMutex::get()); + + m_pImpl->Clear( eHistory ); +} + +Sequence< Sequence< PropertyValue > > SvtHistoryOptions::GetList( EHistoryType eHistory ) const +{ + MutexGuard aGuard(theHistoryOptionsMutex::get()); + + return m_pImpl->GetList( eHistory ); +} + +void SvtHistoryOptions::AppendItem(EHistoryType eHistory, + const OUString& sURL, const OUString& sFilter, const OUString& sTitle, + const std::optional<OUString>& sThumbnail) +{ + MutexGuard aGuard(theHistoryOptionsMutex::get()); + + m_pImpl->AppendItem(eHistory, sURL, sFilter, sTitle, sThumbnail); +} + +void SvtHistoryOptions::DeleteItem(EHistoryType eHistory, const OUString& sURL) +{ + MutexGuard aGuard(theHistoryOptionsMutex::get()); + + m_pImpl->DeleteItem(eHistory, sURL); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/itemholder1.cxx b/unotools/source/config/itemholder1.cxx new file mode 100644 index 000000000..81c0137cd --- /dev/null +++ b/unotools/source/config/itemholder1.cxx @@ -0,0 +1,219 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include "itemholder1.hxx" + +#include <comphelper/processfactory.hxx> +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/configuration/theDefaultProvider.hpp> + +#include <unotools/misccfg.hxx> +#include <unotools/useroptions.hxx> +#include <unotools/cmdoptions.hxx> +#include <unotools/compatibility.hxx> +#include <unotools/compatibilityviewoptions.hxx> +#include <unotools/defaultoptions.hxx> +#include <unotools/dynamicmenuoptions.hxx> +#include <unotools/extendedsecurityoptions.hxx> +#include <unotools/fontoptions.hxx> +#include <unotools/historyoptions.hxx> +#include <unotools/lingucfg.hxx> +#include <unotools/moduleoptions.hxx> +#include <unotools/pathoptions.hxx> +#include <unotools/printwarningoptions.hxx> +#include <unotools/optionsdlg.hxx> +#include <unotools/securityoptions.hxx> +#include <unotools/viewoptions.hxx> +#include <unotools/options.hxx> +#include <unotools/syslocaleoptions.hxx> +#include <sal/log.hxx> +#include <osl/diagnose.h> +#include <tools/diagnose_ex.h> + +ItemHolder1::ItemHolder1() + : ItemHolderMutexBase() +{ + try + { + css::uno::Reference< css::uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext(); + css::uno::Reference< css::lang::XComponent > xCfg( + css::configuration::theDefaultProvider::get( xContext ), + css::uno::UNO_QUERY_THROW ); + xCfg->addEventListener(static_cast< css::lang::XEventListener* >(this)); + } +#ifdef DBG_UTIL + catch(const css::uno::Exception&) + { + static bool bMessage = true; + if(bMessage) + { + bMessage = false; + css::uno::Any ex( cppu::getCaughtException() ); + SAL_WARN( "unotools", "CreateInstance with arguments exception: " << exceptionToString(ex)); + } + } +#else + catch(css::uno::Exception&){} +#endif +} + +ItemHolder1::~ItemHolder1() +{ + impl_releaseAllItems(); +} + +void ItemHolder1::holdConfigItem(EItem eItem) +{ + static ItemHolder1* pHolder = new ItemHolder1(); + pHolder->impl_addItem(eItem); +} + +void SAL_CALL ItemHolder1::disposing(const css::lang::EventObject&) +{ + css::uno::Reference< css::uno::XInterface > xSelfHold(static_cast< css::lang::XEventListener* >(this), css::uno::UNO_QUERY); + impl_releaseAllItems(); +} + +void ItemHolder1::impl_addItem(EItem eItem) +{ + osl::MutexGuard aLock(m_aLock); + + for ( auto const & rInfo : m_lItems ) + { + if (rInfo.eItem == eItem) + return; + } + + TItemInfo aNewItem; + aNewItem.eItem = eItem; + impl_newItem(aNewItem); + if (aNewItem.pItem) + m_lItems.emplace_back(std::move(aNewItem)); +} + +void ItemHolder1::impl_releaseAllItems() +{ + std::vector< TItemInfo > items; + { + ::osl::MutexGuard aLock(m_aLock); + items.swap(m_lItems); + } + + // items will be freed when the block exits +} + +void ItemHolder1::impl_newItem(TItemInfo& rItem) +{ + switch(rItem.eItem) + { + case EItem::CmdOptions : + rItem.pItem.reset( new SvtCommandOptions() ); + break; + + case EItem::Compatibility : + rItem.pItem.reset( new SvtCompatibilityOptions() ); + break; + + case EItem::CompatibilityView : + rItem.pItem.reset( new SvtCompatibilityViewOptions() ); + break; + + case EItem::DefaultOptions : + rItem.pItem.reset( new SvtDefaultOptions() ); + break; + + case EItem::DynamicMenuOptions : + rItem.pItem.reset( new SvtDynamicMenuOptions() ); + break; + + case EItem::EventConfig : + //rItem.pItem.reset( new GlobalEventConfig() ); + break; + + case EItem::ExtendedSecurityOptions : + rItem.pItem.reset( new SvtExtendedSecurityOptions() ); + break; + + case EItem::FontOptions : + rItem.pItem.reset( new SvtFontOptions() ); + break; + + case EItem::HistoryOptions : + rItem.pItem.reset( new SvtHistoryOptions() ); + break; + + case EItem::LinguConfig : + rItem.pItem.reset( new SvtLinguConfig() ); + break; + + case EItem::ModuleOptions : + rItem.pItem.reset( new SvtModuleOptions() ); + break; + + case EItem::OptionsDialogOptions : + rItem.pItem.reset( new SvtOptionsDialogOptions() ); + break; + + case EItem::PathOptions : + rItem.pItem.reset( new SvtPathOptions() ); + break; + + case EItem::PrintWarningOptions : + rItem.pItem.reset( new SvtPrintWarningOptions() ); + break; + + case EItem::MiscConfig : + rItem.pItem.reset( new ::utl::MiscCfg() ); + break; + + case EItem::SecurityOptions : + rItem.pItem.reset( new SvtSecurityOptions() ); + break; + + case EItem::ViewOptionsDialog : + rItem.pItem.reset( new SvtViewOptions(EViewType::Dialog, OUString()) ); + break; + + case EItem::ViewOptionsTabDialog : + rItem.pItem.reset( new SvtViewOptions(EViewType::TabDialog, OUString()) ); + break; + + case EItem::ViewOptionsTabPage : + rItem.pItem.reset( new SvtViewOptions(EViewType::TabPage, OUString()) ); + break; + + case EItem::ViewOptionsWindow : + rItem.pItem.reset( new SvtViewOptions(EViewType::Window, OUString()) ); + break; + + case EItem::UserOptions : + rItem.pItem.reset( new SvtUserOptions() ); + break; + + case EItem::SysLocaleOptions : + rItem.pItem.reset( new SvtSysLocaleOptions() ); + break; + + default: + OSL_FAIL( "unknown item type" ); + break; + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/itemholder1.hxx b/unotools/source/config/itemholder1.hxx new file mode 100644 index 000000000..c632eedc7 --- /dev/null +++ b/unotools/source/config/itemholder1.hxx @@ -0,0 +1,62 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_UNOTOOLS_SOURCE_CONFIG_ITEMHOLDER1_HXX +#define INCLUDED_UNOTOOLS_SOURCE_CONFIG_ITEMHOLDER1_HXX + +#include <unotools/itemholderbase.hxx> +#include <cppuhelper/implbase.hxx> +#include <com/sun/star/lang/XEventListener.hpp> + +class ItemHolder1 : private ItemHolderMutexBase + , public ::cppu::WeakImplHelper< css::lang::XEventListener > +{ + + // member + private: + + std::vector<TItemInfo> m_lItems; + + // c++ interface + public: + + ItemHolder1(); + virtual ~ItemHolder1() override; + static void holdConfigItem(EItem eItem); + + // uno interface + public: + + virtual void SAL_CALL disposing(const css::lang::EventObject& aEvent) override; + + // helper + private: + + void impl_addItem(EItem eItem); + void impl_releaseAllItems(); + static void impl_newItem(TItemInfo& rItem); +}; + +// namespaces + +#undef css + +#endif // INCLUDED_UNOTOOLS_SOURCE_CONFIG_ITEMHOLDER1_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/lingucfg.cxx b/unotools/source/config/lingucfg.cxx new file mode 100644 index 000000000..3bd595388 --- /dev/null +++ b/unotools/source/config/lingucfg.cxx @@ -0,0 +1,1182 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <com/sun/star/lang/Locale.hpp> +#include <com/sun/star/lang/XMultiServiceFactory.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/configuration/theDefaultProvider.hpp> +#include <com/sun/star/container/XNameAccess.hpp> +#include <com/sun/star/util/XChangesBatch.hpp> +#include <rtl/instance.hxx> +#include <sal/log.hxx> +#include <osl/mutex.hxx> +#include <tools/diagnose_ex.h> +#include <i18nlangtag/mslangid.hxx> +#include <i18nlangtag/languagetag.hxx> +#include <tools/debug.hxx> +#include <unotools/configitem.hxx> +#include <unotools/lingucfg.hxx> +#include <unotools/linguprops.hxx> +#include <sal/macros.h> +#include <comphelper/getexpandeduri.hxx> +#include <comphelper/processfactory.hxx> + +#include "itemholder1.hxx" + +using namespace com::sun::star; + +#define FILE_PROTOCOL "file:///" + +namespace +{ + class theSvtLinguConfigItemMutex : + public rtl::Static< osl::Mutex, theSvtLinguConfigItemMutex > {}; +} + +static bool lcl_SetLocale( LanguageType &rLanguage, const uno::Any &rVal ) +{ + bool bSucc = false; + + lang::Locale aNew; + if (rVal >>= aNew) // conversion successful? + { + LanguageType nNew = LanguageTag::convertToLanguageType( aNew, false); + if (nNew != rLanguage) + { + rLanguage = nNew; + bSucc = true; + } + } + return bSucc; +} + +static OUString lcl_LanguageToCfgLocaleStr( LanguageType nLanguage ) +{ + OUString aRes; + if (LANGUAGE_SYSTEM != nLanguage) + aRes = LanguageTag::convertToBcp47( nLanguage ); + return aRes; +} + +static LanguageType lcl_CfgAnyToLanguage( const uno::Any &rVal ) +{ + OUString aTmp; + rVal >>= aTmp; + return (aTmp.isEmpty()) ? LANGUAGE_SYSTEM : LanguageTag::convertToLanguageTypeWithFallback( aTmp ); +} + +SvtLinguOptions::SvtLinguOptions() + : bROActiveDics(false) + , bROActiveConvDics(false) + , nHyphMinLeading(2) + , nHyphMinTrailing(2) + , nHyphMinWordLength(0) + , bROHyphMinLeading(false) + , bROHyphMinTrailing(false) + , bROHyphMinWordLength(false) + , nDefaultLanguage(LANGUAGE_NONE) + , nDefaultLanguage_CJK(LANGUAGE_NONE) + , nDefaultLanguage_CTL(LANGUAGE_NONE) + , bRODefaultLanguage(false) + , bRODefaultLanguage_CJK(false) + , bRODefaultLanguage_CTL(false) + , bIsSpellSpecial(true) + , bIsSpellAuto(false) + , bIsSpellReverse(false) + , bROIsSpellSpecial(false) + , bROIsSpellAuto(false) + , bROIsSpellReverse(false) + , bIsHyphSpecial(true) + , bIsHyphAuto(false) + , bROIsHyphSpecial(false) + , bROIsHyphAuto(false) + , bIsUseDictionaryList(true) + , bIsIgnoreControlCharacters(true) + , bROIsUseDictionaryList(false) + , bROIsIgnoreControlCharacters(false) + , bIsSpellWithDigits(false) + , bIsSpellUpperCase(false) + , bIsSpellCapitalization(true) + , bROIsSpellWithDigits(false) + , bROIsSpellUpperCase(false) + , bROIsSpellCapitalization(false) + , bIsIgnorePostPositionalWord(true) + , bIsAutoCloseDialog(false) + , bIsShowEntriesRecentlyUsedFirst(false) + , bIsAutoReplaceUniqueEntries(false) + , bIsDirectionToSimplified(true) + , bIsUseCharacterVariants(false) + , bIsTranslateCommonTerms(false) + , bIsReverseMapping(false) + , bROIsIgnorePostPositionalWord(false) + , bROIsAutoCloseDialog(false) + , bROIsShowEntriesRecentlyUsedFirst(false) + , bROIsAutoReplaceUniqueEntries(false) + , bROIsDirectionToSimplified(false) + , bROIsUseCharacterVariants(false) + , bROIsTranslateCommonTerms(false) + , bROIsReverseMapping(false) + , nDataFilesChangedCheckValue(0) + , bRODataFilesChangedCheckValue(false) + , bIsGrammarAuto(false) + , bIsGrammarInteractive(false) + , bROIsGrammarAuto(false) + , bROIsGrammarInteractive(false) +{ +} + +class SvtLinguConfigItem : public utl::ConfigItem +{ + SvtLinguOptions aOpt; + + static bool GetHdlByName( sal_Int32 &rnHdl, const OUString &rPropertyName, bool bFullPropName = false ); + static uno::Sequence< OUString > GetPropertyNames(); + void LoadOptions( const uno::Sequence< OUString > &rProperyNames ); + bool SaveOptions( const uno::Sequence< OUString > &rProperyNames ); + + SvtLinguConfigItem(const SvtLinguConfigItem&) = delete; + SvtLinguConfigItem& operator=(const SvtLinguConfigItem&) = delete; + virtual void ImplCommit() override; + +public: + SvtLinguConfigItem(); + + // utl::ConfigItem + virtual void Notify( const css::uno::Sequence< OUString > &rPropertyNames ) override; + + // make some protected functions of utl::ConfigItem public + using utl::ConfigItem::GetNodeNames; + using utl::ConfigItem::GetProperties; + //using utl::ConfigItem::PutProperties; + //using utl::ConfigItem::SetSetProperties; + using utl::ConfigItem::ReplaceSetProperties; + //using utl::ConfigItem::GetReadOnlyStates; + + css::uno::Any + GetProperty( const OUString &rPropertyName ) const; + css::uno::Any + GetProperty( sal_Int32 nPropertyHandle ) const; + + bool SetProperty( const OUString &rPropertyName, + const css::uno::Any &rValue ); + bool SetProperty( sal_Int32 nPropertyHandle, + const css::uno::Any &rValue ); + + const SvtLinguOptions& GetOptions() const; + + bool IsReadOnly( const OUString &rPropertyName ) const; + bool IsReadOnly( sal_Int32 nPropertyHandle ) const; +}; + +SvtLinguConfigItem::SvtLinguConfigItem() : + utl::ConfigItem( "Office.Linguistic" ) +{ + const uno::Sequence< OUString > &rPropertyNames = GetPropertyNames(); + LoadOptions( rPropertyNames ); + ClearModified(); + + // request notify events when properties change + EnableNotification( rPropertyNames ); +} + +void SvtLinguConfigItem::Notify( const uno::Sequence< OUString > &rPropertyNames ) +{ + LoadOptions( rPropertyNames ); + NotifyListeners(ConfigurationHints::NONE); +} + +void SvtLinguConfigItem::ImplCommit() +{ + SaveOptions( GetPropertyNames() ); +} + +namespace { + +struct NamesToHdl +{ + const char *pFullPropName; // full qualified name as used in configuration + const char *pPropName; // property name only (atom) of above + sal_Int32 nHdl; // numeric handle representing the property +}; + +} + +static NamesToHdl const aNamesToHdl[] = +{ +{/* 0 */ "General/DefaultLocale", UPN_DEFAULT_LOCALE, UPH_DEFAULT_LOCALE}, +{/* 1 */ "General/DictionaryList/ActiveDictionaries", UPN_ACTIVE_DICTIONARIES, UPH_ACTIVE_DICTIONARIES}, +{/* 2 */ "General/DictionaryList/IsUseDictionaryList", UPN_IS_USE_DICTIONARY_LIST, UPH_IS_USE_DICTIONARY_LIST}, +{/* 3 */ "General/IsIgnoreControlCharacters", UPN_IS_IGNORE_CONTROL_CHARACTERS, UPH_IS_IGNORE_CONTROL_CHARACTERS}, +{/* 5 */ "General/DefaultLocale_CJK", UPN_DEFAULT_LOCALE_CJK, UPH_DEFAULT_LOCALE_CJK}, +{/* 6 */ "General/DefaultLocale_CTL", UPN_DEFAULT_LOCALE_CTL, UPH_DEFAULT_LOCALE_CTL}, + +{/* 7 */ "SpellChecking/IsSpellUpperCase", UPN_IS_SPELL_UPPER_CASE, UPH_IS_SPELL_UPPER_CASE}, +{/* 8 */ "SpellChecking/IsSpellWithDigits", UPN_IS_SPELL_WITH_DIGITS, UPH_IS_SPELL_WITH_DIGITS}, +{/* 9 */ "SpellChecking/IsSpellCapitalization", UPN_IS_SPELL_CAPITALIZATION, UPH_IS_SPELL_CAPITALIZATION}, +{/* 10 */ "SpellChecking/IsSpellAuto", UPN_IS_SPELL_AUTO, UPH_IS_SPELL_AUTO}, +{/* 11 */ "SpellChecking/IsSpellSpecial", UPN_IS_SPELL_SPECIAL, UPH_IS_SPELL_SPECIAL}, +{/* 14 */ "SpellChecking/IsReverseDirection", UPN_IS_WRAP_REVERSE, UPH_IS_WRAP_REVERSE}, + +{/* 15 */ "Hyphenation/MinLeading", UPN_HYPH_MIN_LEADING, UPH_HYPH_MIN_LEADING}, +{/* 16 */ "Hyphenation/MinTrailing", UPN_HYPH_MIN_TRAILING, UPH_HYPH_MIN_TRAILING}, +{/* 17 */ "Hyphenation/MinWordLength", UPN_HYPH_MIN_WORD_LENGTH, UPH_HYPH_MIN_WORD_LENGTH}, +{/* 18 */ "Hyphenation/IsHyphSpecial", UPN_IS_HYPH_SPECIAL, UPH_IS_HYPH_SPECIAL}, +{/* 19 */ "Hyphenation/IsHyphAuto", UPN_IS_HYPH_AUTO, UPH_IS_HYPH_AUTO}, + +{/* 20 */ "TextConversion/ActiveConversionDictionaries", UPN_ACTIVE_CONVERSION_DICTIONARIES, UPH_ACTIVE_CONVERSION_DICTIONARIES}, +{/* 21 */ "TextConversion/IsIgnorePostPositionalWord", UPN_IS_IGNORE_POST_POSITIONAL_WORD, UPH_IS_IGNORE_POST_POSITIONAL_WORD}, +{/* 22 */ "TextConversion/IsAutoCloseDialog", UPN_IS_AUTO_CLOSE_DIALOG, UPH_IS_AUTO_CLOSE_DIALOG}, +{/* 23 */ "TextConversion/IsShowEntriesRecentlyUsedFirst", UPN_IS_SHOW_ENTRIES_RECENTLY_USED_FIRST, UPH_IS_SHOW_ENTRIES_RECENTLY_USED_FIRST}, +{/* 24 */ "TextConversion/IsAutoReplaceUniqueEntries", UPN_IS_AUTO_REPLACE_UNIQUE_ENTRIES, UPH_IS_AUTO_REPLACE_UNIQUE_ENTRIES}, +{/* 25 */ "TextConversion/IsDirectionToSimplified", UPN_IS_DIRECTION_TO_SIMPLIFIED, UPH_IS_DIRECTION_TO_SIMPLIFIED}, +{/* 26 */ "TextConversion/IsUseCharacterVariants", UPN_IS_USE_CHARACTER_VARIANTS, UPH_IS_USE_CHARACTER_VARIANTS}, +{/* 27 */ "TextConversion/IsTranslateCommonTerms", UPN_IS_TRANSLATE_COMMON_TERMS, UPH_IS_TRANSLATE_COMMON_TERMS}, +{/* 28 */ "TextConversion/IsReverseMapping", UPN_IS_REVERSE_MAPPING, UPH_IS_REVERSE_MAPPING}, + +{/* 29 */ "ServiceManager/DataFilesChangedCheckValue", UPN_DATA_FILES_CHANGED_CHECK_VALUE, UPH_DATA_FILES_CHANGED_CHECK_VALUE}, + +{/* 30 */ "GrammarChecking/IsAutoCheck", UPN_IS_GRAMMAR_AUTO, UPH_IS_GRAMMAR_AUTO}, +{/* 31 */ "GrammarChecking/IsInteractiveCheck", UPN_IS_GRAMMAR_INTERACTIVE, UPH_IS_GRAMMAR_INTERACTIVE}, + + /* similar to entry 0 (thus no own configuration entry) but with different property name and type */ +{ nullptr, UPN_DEFAULT_LANGUAGE, UPH_DEFAULT_LANGUAGE}, + +{ nullptr, nullptr, -1} +}; + +uno::Sequence< OUString > SvtLinguConfigItem::GetPropertyNames() +{ + uno::Sequence< OUString > aNames; + + sal_Int32 nMax = SAL_N_ELEMENTS(aNamesToHdl); + + aNames.realloc( nMax ); + OUString *pNames = aNames.getArray(); + sal_Int32 nIdx = 0; + for (sal_Int32 i = 0; i < nMax; ++i) + { + const char *pFullPropName = aNamesToHdl[i].pFullPropName; + if (pFullPropName) + pNames[ nIdx++ ] = OUString::createFromAscii( pFullPropName ); + } + aNames.realloc( nIdx ); + + return aNames; +} + +bool SvtLinguConfigItem::GetHdlByName( + sal_Int32 &rnHdl, + const OUString &rPropertyName, + bool bFullPropName ) +{ + NamesToHdl const *pEntry = &aNamesToHdl[0]; + + if (bFullPropName) + { + while (pEntry && pEntry->pFullPropName != nullptr) + { + if (rPropertyName.equalsAscii( pEntry->pFullPropName )) + { + rnHdl = pEntry->nHdl; + break; + } + ++pEntry; + } + return pEntry && pEntry->pFullPropName != nullptr; + } + else + { + while (pEntry && pEntry->pPropName != nullptr) + { + if (rPropertyName.equalsAscii( pEntry->pPropName )) + { + rnHdl = pEntry->nHdl; + break; + } + ++pEntry; + } + return pEntry && pEntry->pPropName != nullptr; + } +} + +uno::Any SvtLinguConfigItem::GetProperty( const OUString &rPropertyName ) const +{ + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + + sal_Int32 nHdl; + return GetHdlByName( nHdl, rPropertyName ) ? GetProperty( nHdl ) : uno::Any(); +} + +uno::Any SvtLinguConfigItem::GetProperty( sal_Int32 nPropertyHandle ) const +{ + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + + uno::Any aRes; + + const sal_Int16 *pnVal = nullptr; + const LanguageType *plVal = nullptr; + const bool *pbVal = nullptr; + const sal_Int32 *pnInt32Val = nullptr; + + const SvtLinguOptions &rOpt = const_cast< SvtLinguConfigItem * >(this)->aOpt; + switch (nPropertyHandle) + { + case UPH_IS_USE_DICTIONARY_LIST : pbVal = &rOpt.bIsUseDictionaryList; break; + case UPH_IS_IGNORE_CONTROL_CHARACTERS : pbVal = &rOpt.bIsIgnoreControlCharacters; break; + case UPH_IS_HYPH_AUTO : pbVal = &rOpt.bIsHyphAuto; break; + case UPH_IS_HYPH_SPECIAL : pbVal = &rOpt.bIsHyphSpecial; break; + case UPH_IS_SPELL_AUTO : pbVal = &rOpt.bIsSpellAuto; break; + case UPH_IS_SPELL_SPECIAL : pbVal = &rOpt.bIsSpellSpecial; break; + case UPH_IS_WRAP_REVERSE : pbVal = &rOpt.bIsSpellReverse; break; + case UPH_DEFAULT_LANGUAGE : plVal = &rOpt.nDefaultLanguage; break; + case UPH_IS_SPELL_CAPITALIZATION : pbVal = &rOpt.bIsSpellCapitalization; break; + case UPH_IS_SPELL_WITH_DIGITS : pbVal = &rOpt.bIsSpellWithDigits; break; + case UPH_IS_SPELL_UPPER_CASE : pbVal = &rOpt.bIsSpellUpperCase; break; + case UPH_HYPH_MIN_LEADING : pnVal = &rOpt.nHyphMinLeading; break; + case UPH_HYPH_MIN_TRAILING : pnVal = &rOpt.nHyphMinTrailing; break; + case UPH_HYPH_MIN_WORD_LENGTH : pnVal = &rOpt.nHyphMinWordLength; break; + case UPH_ACTIVE_DICTIONARIES : + { + aRes <<= rOpt.aActiveDics; + break; + } + case UPH_ACTIVE_CONVERSION_DICTIONARIES : + { + aRes <<= rOpt.aActiveConvDics; + break; + } + case UPH_DEFAULT_LOCALE : + { + aRes <<= LanguageTag::convertToLocale( rOpt.nDefaultLanguage, false); + break; + } + case UPH_DEFAULT_LOCALE_CJK : + { + aRes <<= LanguageTag::convertToLocale( rOpt.nDefaultLanguage_CJK, false); + break; + } + case UPH_DEFAULT_LOCALE_CTL : + { + aRes <<= LanguageTag::convertToLocale( rOpt.nDefaultLanguage_CTL, false); + break; + } + case UPH_IS_IGNORE_POST_POSITIONAL_WORD : pbVal = &rOpt.bIsIgnorePostPositionalWord; break; + case UPH_IS_AUTO_CLOSE_DIALOG : pbVal = &rOpt.bIsAutoCloseDialog; break; + case UPH_IS_SHOW_ENTRIES_RECENTLY_USED_FIRST : pbVal = &rOpt.bIsShowEntriesRecentlyUsedFirst; break; + case UPH_IS_AUTO_REPLACE_UNIQUE_ENTRIES : pbVal = &rOpt.bIsAutoReplaceUniqueEntries; break; + + case UPH_IS_DIRECTION_TO_SIMPLIFIED: pbVal = &rOpt.bIsDirectionToSimplified; break; + case UPH_IS_USE_CHARACTER_VARIANTS : pbVal = &rOpt.bIsUseCharacterVariants; break; + case UPH_IS_TRANSLATE_COMMON_TERMS : pbVal = &rOpt.bIsTranslateCommonTerms; break; + case UPH_IS_REVERSE_MAPPING : pbVal = &rOpt.bIsReverseMapping; break; + + case UPH_DATA_FILES_CHANGED_CHECK_VALUE : pnInt32Val = &rOpt.nDataFilesChangedCheckValue; break; + case UPH_IS_GRAMMAR_AUTO: pbVal = &rOpt.bIsGrammarAuto; break; + case UPH_IS_GRAMMAR_INTERACTIVE: pbVal = &rOpt.bIsGrammarInteractive; break; + default : + SAL_WARN( "unotools.config", "unexpected property handle" ); + } + + if (pbVal) + aRes <<= *pbVal; + else if (pnVal) + aRes <<= *pnVal; + else if (plVal) + aRes <<= static_cast<sal_Int16>(static_cast<sal_uInt16>(*plVal)); + else if (pnInt32Val) + aRes <<= *pnInt32Val; + + return aRes; +} + +bool SvtLinguConfigItem::SetProperty( const OUString &rPropertyName, const uno::Any &rValue ) +{ + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + + bool bSucc = false; + sal_Int32 nHdl; + if (GetHdlByName( nHdl, rPropertyName )) + bSucc = SetProperty( nHdl, rValue ); + return bSucc; +} + +bool SvtLinguConfigItem::SetProperty( sal_Int32 nPropertyHandle, const uno::Any &rValue ) +{ + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + + bool bSucc = false; + if (!rValue.hasValue()) + return bSucc; + + bool bMod = false; + + sal_Int16 *pnVal = nullptr; + LanguageType *plVal = nullptr; + bool *pbVal = nullptr; + sal_Int32 *pnInt32Val = nullptr; + + SvtLinguOptions &rOpt = aOpt; + switch (nPropertyHandle) + { + case UPH_IS_USE_DICTIONARY_LIST : pbVal = &rOpt.bIsUseDictionaryList; break; + case UPH_IS_IGNORE_CONTROL_CHARACTERS : pbVal = &rOpt.bIsIgnoreControlCharacters; break; + case UPH_IS_HYPH_AUTO : pbVal = &rOpt.bIsHyphAuto; break; + case UPH_IS_HYPH_SPECIAL : pbVal = &rOpt.bIsHyphSpecial; break; + case UPH_IS_SPELL_AUTO : pbVal = &rOpt.bIsSpellAuto; break; + case UPH_IS_SPELL_SPECIAL : pbVal = &rOpt.bIsSpellSpecial; break; + case UPH_IS_WRAP_REVERSE : pbVal = &rOpt.bIsSpellReverse; break; + case UPH_DEFAULT_LANGUAGE : plVal = &rOpt.nDefaultLanguage; break; + case UPH_IS_SPELL_CAPITALIZATION : pbVal = &rOpt.bIsSpellCapitalization; break; + case UPH_IS_SPELL_WITH_DIGITS : pbVal = &rOpt.bIsSpellWithDigits; break; + case UPH_IS_SPELL_UPPER_CASE : pbVal = &rOpt.bIsSpellUpperCase; break; + case UPH_HYPH_MIN_LEADING : pnVal = &rOpt.nHyphMinLeading; break; + case UPH_HYPH_MIN_TRAILING : pnVal = &rOpt.nHyphMinTrailing; break; + case UPH_HYPH_MIN_WORD_LENGTH : pnVal = &rOpt.nHyphMinWordLength; break; + case UPH_ACTIVE_DICTIONARIES : + { + rValue >>= rOpt.aActiveDics; + bMod = true; + break; + } + case UPH_ACTIVE_CONVERSION_DICTIONARIES : + { + rValue >>= rOpt.aActiveConvDics; + bMod = true; + break; + } + case UPH_DEFAULT_LOCALE : + { + bSucc = lcl_SetLocale( rOpt.nDefaultLanguage, rValue ); + bMod = bSucc; + break; + } + case UPH_DEFAULT_LOCALE_CJK : + { + bSucc = lcl_SetLocale( rOpt.nDefaultLanguage_CJK, rValue ); + bMod = bSucc; + break; + } + case UPH_DEFAULT_LOCALE_CTL : + { + bSucc = lcl_SetLocale( rOpt.nDefaultLanguage_CTL, rValue ); + bMod = bSucc; + break; + } + case UPH_IS_IGNORE_POST_POSITIONAL_WORD : pbVal = &rOpt.bIsIgnorePostPositionalWord; break; + case UPH_IS_AUTO_CLOSE_DIALOG : pbVal = &rOpt.bIsAutoCloseDialog; break; + case UPH_IS_SHOW_ENTRIES_RECENTLY_USED_FIRST : pbVal = &rOpt.bIsShowEntriesRecentlyUsedFirst; break; + case UPH_IS_AUTO_REPLACE_UNIQUE_ENTRIES : pbVal = &rOpt.bIsAutoReplaceUniqueEntries; break; + + case UPH_IS_DIRECTION_TO_SIMPLIFIED : pbVal = &rOpt.bIsDirectionToSimplified; break; + case UPH_IS_USE_CHARACTER_VARIANTS : pbVal = &rOpt.bIsUseCharacterVariants; break; + case UPH_IS_TRANSLATE_COMMON_TERMS : pbVal = &rOpt.bIsTranslateCommonTerms; break; + case UPH_IS_REVERSE_MAPPING : pbVal = &rOpt.bIsReverseMapping; break; + + case UPH_DATA_FILES_CHANGED_CHECK_VALUE : pnInt32Val = &rOpt.nDataFilesChangedCheckValue; break; + case UPH_IS_GRAMMAR_AUTO: pbVal = &rOpt.bIsGrammarAuto; break; + case UPH_IS_GRAMMAR_INTERACTIVE: pbVal = &rOpt.bIsGrammarInteractive; break; + default : + SAL_WARN( "unotools.config", "unexpected property handle" ); + } + + if (pbVal) + { + bool bNew = bool(); + if (rValue >>= bNew) + { + if (bNew != *pbVal) + { + *pbVal = bNew; + bMod = true; + } + bSucc = true; + } + } + else if (pnVal) + { + sal_Int16 nNew = sal_Int16(); + if (rValue >>= nNew) + { + if (nNew != *pnVal) + { + *pnVal = nNew; + bMod = true; + } + bSucc = true; + } + } + else if (plVal) + { + sal_Int16 nNew = sal_Int16(); + if (rValue >>= nNew) + { + if (nNew != static_cast<sal_uInt16>(*plVal)) + { + *plVal = LanguageType(static_cast<sal_uInt16>(nNew)); + bMod = true; + } + bSucc = true; + } + } + else if (pnInt32Val) + { + sal_Int32 nNew = sal_Int32(); + if (rValue >>= nNew) + { + if (nNew != *pnInt32Val) + { + *pnInt32Val = nNew; + bMod = true; + } + bSucc = true; + } + } + + if (bMod) + SetModified(); + + NotifyListeners(ConfigurationHints::NONE); + return bSucc; +} + +const SvtLinguOptions& SvtLinguConfigItem::GetOptions() const +{ + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + return aOpt; +} + +void SvtLinguConfigItem::LoadOptions( const uno::Sequence< OUString > &rProperyNames ) +{ + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + + bool bRes = false; + + const OUString *pProperyNames = rProperyNames.getConstArray(); + sal_Int32 nProps = rProperyNames.getLength(); + + const uno::Sequence< uno::Any > aValues = GetProperties( rProperyNames ); + const uno::Sequence< sal_Bool > aROStates = GetReadOnlyStates( rProperyNames ); + + if (nProps && aValues.getLength() == nProps && aROStates.getLength() == nProps) + { + SvtLinguOptions &rOpt = aOpt; + + const uno::Any *pValue = aValues.getConstArray(); + const sal_Bool *pROStates = aROStates.getConstArray(); + for (sal_Int32 i = 0; i < nProps; ++i) + { + const uno::Any &rVal = pValue[i]; + sal_Int32 nPropertyHandle(0); + GetHdlByName( nPropertyHandle, pProperyNames[i], true ); + switch ( nPropertyHandle ) + { + case UPH_DEFAULT_LOCALE : + { rOpt.bRODefaultLanguage = pROStates[i]; rOpt.nDefaultLanguage = lcl_CfgAnyToLanguage( rVal ); } break; + case UPH_ACTIVE_DICTIONARIES : + { rOpt.bROActiveDics = pROStates[i]; rVal >>= rOpt.aActiveDics; } break; + case UPH_IS_USE_DICTIONARY_LIST : + { rOpt.bROIsUseDictionaryList = pROStates[i]; rVal >>= rOpt.bIsUseDictionaryList; } break; + case UPH_IS_IGNORE_CONTROL_CHARACTERS : + { rOpt.bROIsIgnoreControlCharacters = pROStates[i]; rVal >>= rOpt.bIsIgnoreControlCharacters; } break; + case UPH_DEFAULT_LOCALE_CJK : + { rOpt.bRODefaultLanguage_CJK = pROStates[i]; rOpt.nDefaultLanguage_CJK = lcl_CfgAnyToLanguage( rVal ); } break; + case UPH_DEFAULT_LOCALE_CTL : + { rOpt.bRODefaultLanguage_CTL = pROStates[i]; rOpt.nDefaultLanguage_CTL = lcl_CfgAnyToLanguage( rVal ); } break; + + case UPH_IS_SPELL_UPPER_CASE : + { rOpt.bROIsSpellUpperCase = pROStates[i]; rVal >>= rOpt.bIsSpellUpperCase; } break; + case UPH_IS_SPELL_WITH_DIGITS : + { rOpt.bROIsSpellWithDigits = pROStates[i]; rVal >>= rOpt.bIsSpellWithDigits; } break; + case UPH_IS_SPELL_CAPITALIZATION : + { rOpt.bROIsSpellCapitalization = pROStates[i]; rVal >>= rOpt.bIsSpellCapitalization; } break; + case UPH_IS_SPELL_AUTO : + { rOpt.bROIsSpellAuto = pROStates[i]; rVal >>= rOpt.bIsSpellAuto; } break; + case UPH_IS_SPELL_SPECIAL : + { rOpt.bROIsSpellSpecial = pROStates[i]; rVal >>= rOpt.bIsSpellSpecial; } break; + case UPH_IS_WRAP_REVERSE : + { rOpt.bROIsSpellReverse = pROStates[i]; rVal >>= rOpt.bIsSpellReverse; } break; + + case UPH_HYPH_MIN_LEADING : + { rOpt.bROHyphMinLeading = pROStates[i]; rVal >>= rOpt.nHyphMinLeading; } break; + case UPH_HYPH_MIN_TRAILING : + { rOpt.bROHyphMinTrailing = pROStates[i]; rVal >>= rOpt.nHyphMinTrailing; } break; + case UPH_HYPH_MIN_WORD_LENGTH : + { rOpt.bROHyphMinWordLength = pROStates[i]; rVal >>= rOpt.nHyphMinWordLength; } break; + case UPH_IS_HYPH_SPECIAL : + { rOpt.bROIsHyphSpecial = pROStates[i]; rVal >>= rOpt.bIsHyphSpecial; } break; + case UPH_IS_HYPH_AUTO : + { rOpt.bROIsHyphAuto = pROStates[i]; rVal >>= rOpt.bIsHyphAuto; } break; + + case UPH_ACTIVE_CONVERSION_DICTIONARIES : { rOpt.bROActiveConvDics = pROStates[i]; rVal >>= rOpt.aActiveConvDics; } break; + + case UPH_IS_IGNORE_POST_POSITIONAL_WORD : + { rOpt.bROIsIgnorePostPositionalWord = pROStates[i]; rVal >>= rOpt.bIsIgnorePostPositionalWord; } break; + case UPH_IS_AUTO_CLOSE_DIALOG : + { rOpt.bROIsAutoCloseDialog = pROStates[i]; rVal >>= rOpt.bIsAutoCloseDialog; } break; + case UPH_IS_SHOW_ENTRIES_RECENTLY_USED_FIRST : + { rOpt.bROIsShowEntriesRecentlyUsedFirst = pROStates[i]; rVal >>= rOpt.bIsShowEntriesRecentlyUsedFirst; } break; + case UPH_IS_AUTO_REPLACE_UNIQUE_ENTRIES : + { rOpt.bROIsAutoReplaceUniqueEntries = pROStates[i]; rVal >>= rOpt.bIsAutoReplaceUniqueEntries; } break; + + case UPH_IS_DIRECTION_TO_SIMPLIFIED : + { + rOpt.bROIsDirectionToSimplified = pROStates[i]; + if( ! (rVal >>= rOpt.bIsDirectionToSimplified) ) + { + //default is locale dependent: + if (MsLangId::isTraditionalChinese(rOpt.nDefaultLanguage_CJK)) + { + rOpt.bIsDirectionToSimplified = false; + } + else + { + rOpt.bIsDirectionToSimplified = true; + } + } + } break; + case UPH_IS_USE_CHARACTER_VARIANTS : + { rOpt.bROIsUseCharacterVariants = pROStates[i]; rVal >>= rOpt.bIsUseCharacterVariants; } break; + case UPH_IS_TRANSLATE_COMMON_TERMS : + { rOpt.bROIsTranslateCommonTerms = pROStates[i]; rVal >>= rOpt.bIsTranslateCommonTerms; } break; + case UPH_IS_REVERSE_MAPPING : + { rOpt.bROIsReverseMapping = pROStates[i]; rVal >>= rOpt.bIsReverseMapping; } break; + + case UPH_DATA_FILES_CHANGED_CHECK_VALUE : + { rOpt.bRODataFilesChangedCheckValue = pROStates[i]; rVal >>= rOpt.nDataFilesChangedCheckValue; } break; + + case UPH_IS_GRAMMAR_AUTO: + { rOpt.bROIsGrammarAuto = pROStates[i]; rVal >>= rOpt.bIsGrammarAuto; } + break; + case UPH_IS_GRAMMAR_INTERACTIVE: + { rOpt.bROIsGrammarInteractive = pROStates[i]; rVal >>= rOpt.bIsGrammarInteractive; } + break; + + default: + SAL_WARN( "unotools.config", "unexpected case" ); + } + } + + bRes = true; + } + DBG_ASSERT( bRes, "LoadOptions failed" ); +} + +bool SvtLinguConfigItem::SaveOptions( const uno::Sequence< OUString > &rProperyNames ) +{ + if (!IsModified()) + return true; + + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + + bool bRet = false; + + sal_Int32 nProps = rProperyNames.getLength(); + uno::Sequence< uno::Any > aValues( nProps ); + uno::Any *pValue = aValues.getArray(); + + if (nProps && aValues.getLength() == nProps) + { + const SvtLinguOptions &rOpt = aOpt; + + OUString aTmp( lcl_LanguageToCfgLocaleStr( rOpt.nDefaultLanguage ) ); + *pValue++ <<= aTmp; // 0 + *pValue++ <<= rOpt.aActiveDics; // 1 + *pValue++ <<= rOpt.bIsUseDictionaryList; // 2 + *pValue++ <<= rOpt.bIsIgnoreControlCharacters; // 3 + aTmp = lcl_LanguageToCfgLocaleStr( rOpt.nDefaultLanguage_CJK ); + *pValue++ <<= aTmp; // 5 + aTmp = lcl_LanguageToCfgLocaleStr( rOpt.nDefaultLanguage_CTL ); + *pValue++ <<= aTmp; // 6 + + *pValue++ <<= rOpt.bIsSpellUpperCase; // 7 + *pValue++ <<= rOpt.bIsSpellWithDigits; // 8 + *pValue++ <<= rOpt.bIsSpellCapitalization; // 9 + *pValue++ <<= rOpt.bIsSpellAuto; // 10 + *pValue++ <<= rOpt.bIsSpellSpecial; // 11 + *pValue++ <<= rOpt.bIsSpellReverse; // 14 + + *pValue++ <<= rOpt.nHyphMinLeading; // 15 + *pValue++ <<= rOpt.nHyphMinTrailing; // 16 + *pValue++ <<= rOpt.nHyphMinWordLength; // 17 + *pValue++ <<= rOpt.bIsHyphSpecial; // 18 + *pValue++ <<= rOpt.bIsHyphAuto; // 19 + + *pValue++ <<= rOpt.aActiveConvDics; // 20 + + *pValue++ <<= rOpt.bIsIgnorePostPositionalWord; // 21 + *pValue++ <<= rOpt.bIsAutoCloseDialog; // 22 + *pValue++ <<= rOpt.bIsShowEntriesRecentlyUsedFirst; // 23 + *pValue++ <<= rOpt.bIsAutoReplaceUniqueEntries; // 24 + + *pValue++ <<= rOpt.bIsDirectionToSimplified; // 25 + *pValue++ <<= rOpt.bIsUseCharacterVariants; // 26 + *pValue++ <<= rOpt.bIsTranslateCommonTerms; // 27 + *pValue++ <<= rOpt.bIsReverseMapping; // 28 + + *pValue++ <<= rOpt.nDataFilesChangedCheckValue; // 29 + *pValue++ <<= rOpt.bIsGrammarAuto; // 30 + *pValue++ <<= rOpt.bIsGrammarInteractive; // 31 + + bRet |= PutProperties( rProperyNames, aValues ); + } + + if (bRet) + ClearModified(); + + return bRet; +} + +bool SvtLinguConfigItem::IsReadOnly( const OUString &rPropertyName ) const +{ + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + + bool bReadOnly = false; + sal_Int32 nHdl; + if (GetHdlByName( nHdl, rPropertyName )) + bReadOnly = IsReadOnly( nHdl ); + return bReadOnly; +} + +bool SvtLinguConfigItem::IsReadOnly( sal_Int32 nPropertyHandle ) const +{ + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + + bool bReadOnly = false; + + const SvtLinguOptions &rOpt = const_cast< SvtLinguConfigItem * >(this)->aOpt; + switch(nPropertyHandle) + { + case UPH_IS_USE_DICTIONARY_LIST : bReadOnly = rOpt.bROIsUseDictionaryList; break; + case UPH_IS_IGNORE_CONTROL_CHARACTERS : bReadOnly = rOpt.bROIsIgnoreControlCharacters; break; + case UPH_IS_HYPH_AUTO : bReadOnly = rOpt.bROIsHyphAuto; break; + case UPH_IS_HYPH_SPECIAL : bReadOnly = rOpt.bROIsHyphSpecial; break; + case UPH_IS_SPELL_AUTO : bReadOnly = rOpt.bROIsSpellAuto; break; + case UPH_IS_SPELL_SPECIAL : bReadOnly = rOpt.bROIsSpellSpecial; break; + case UPH_IS_WRAP_REVERSE : bReadOnly = rOpt.bROIsSpellReverse; break; + case UPH_DEFAULT_LANGUAGE : bReadOnly = rOpt.bRODefaultLanguage; break; + case UPH_IS_SPELL_CAPITALIZATION : bReadOnly = rOpt.bROIsSpellCapitalization; break; + case UPH_IS_SPELL_WITH_DIGITS : bReadOnly = rOpt.bROIsSpellWithDigits; break; + case UPH_IS_SPELL_UPPER_CASE : bReadOnly = rOpt.bROIsSpellUpperCase; break; + case UPH_HYPH_MIN_LEADING : bReadOnly = rOpt.bROHyphMinLeading; break; + case UPH_HYPH_MIN_TRAILING : bReadOnly = rOpt.bROHyphMinTrailing; break; + case UPH_HYPH_MIN_WORD_LENGTH : bReadOnly = rOpt.bROHyphMinWordLength; break; + case UPH_ACTIVE_DICTIONARIES : bReadOnly = rOpt.bROActiveDics; break; + case UPH_ACTIVE_CONVERSION_DICTIONARIES : bReadOnly = rOpt.bROActiveConvDics; break; + case UPH_DEFAULT_LOCALE : bReadOnly = rOpt.bRODefaultLanguage; break; + case UPH_DEFAULT_LOCALE_CJK : bReadOnly = rOpt.bRODefaultLanguage_CJK; break; + case UPH_DEFAULT_LOCALE_CTL : bReadOnly = rOpt.bRODefaultLanguage_CTL; break; + case UPH_IS_IGNORE_POST_POSITIONAL_WORD : bReadOnly = rOpt.bROIsIgnorePostPositionalWord; break; + case UPH_IS_AUTO_CLOSE_DIALOG : bReadOnly = rOpt.bROIsAutoCloseDialog; break; + case UPH_IS_SHOW_ENTRIES_RECENTLY_USED_FIRST : bReadOnly = rOpt.bROIsShowEntriesRecentlyUsedFirst; break; + case UPH_IS_AUTO_REPLACE_UNIQUE_ENTRIES : bReadOnly = rOpt.bROIsAutoReplaceUniqueEntries; break; + case UPH_IS_DIRECTION_TO_SIMPLIFIED : bReadOnly = rOpt.bROIsDirectionToSimplified; break; + case UPH_IS_USE_CHARACTER_VARIANTS : bReadOnly = rOpt.bROIsUseCharacterVariants; break; + case UPH_IS_TRANSLATE_COMMON_TERMS : bReadOnly = rOpt.bROIsTranslateCommonTerms; break; + case UPH_IS_REVERSE_MAPPING : bReadOnly = rOpt.bROIsReverseMapping; break; + case UPH_DATA_FILES_CHANGED_CHECK_VALUE : bReadOnly = rOpt.bRODataFilesChangedCheckValue; break; + case UPH_IS_GRAMMAR_AUTO: bReadOnly = rOpt.bROIsGrammarAuto; break; + case UPH_IS_GRAMMAR_INTERACTIVE: bReadOnly = rOpt.bROIsGrammarInteractive; break; + default : + SAL_WARN( "unotools.config", "unexpected property handle" ); + } + return bReadOnly; +} + +static SvtLinguConfigItem *pCfgItem = nullptr; +static sal_Int32 nCfgItemRefCount = 0; + +static const char aG_Dictionaries[] = "Dictionaries"; + +SvtLinguConfig::SvtLinguConfig() +{ + // Global access, must be guarded (multithreading) + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + ++nCfgItemRefCount; +} + +SvtLinguConfig::~SvtLinguConfig() +{ + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + + if (pCfgItem && pCfgItem->IsModified()) + pCfgItem->Commit(); + + if (--nCfgItemRefCount <= 0) + { + delete pCfgItem; + pCfgItem = nullptr; + } +} + +SvtLinguConfigItem & SvtLinguConfig::GetConfigItem() +{ + // Global access, must be guarded (multithreading) + osl::MutexGuard aGuard(theSvtLinguConfigItemMutex::get()); + if (!pCfgItem) + { + pCfgItem = new SvtLinguConfigItem; + ItemHolder1::holdConfigItem(EItem::LinguConfig); + } + return *pCfgItem; +} + +uno::Sequence< OUString > SvtLinguConfig::GetNodeNames( const OUString &rNode ) const +{ + return GetConfigItem().GetNodeNames( rNode ); +} + +uno::Sequence< uno::Any > SvtLinguConfig::GetProperties( const uno::Sequence< OUString > &rNames ) const +{ + return GetConfigItem().GetProperties(rNames); +} + +bool SvtLinguConfig::ReplaceSetProperties( + const OUString &rNode, const uno::Sequence< beans::PropertyValue >& rValues ) +{ + return GetConfigItem().ReplaceSetProperties( rNode, rValues ); +} + +uno::Any SvtLinguConfig::GetProperty( const OUString &rPropertyName ) const +{ + return GetConfigItem().GetProperty( rPropertyName ); +} + +uno::Any SvtLinguConfig::GetProperty( sal_Int32 nPropertyHandle ) const +{ + return GetConfigItem().GetProperty( nPropertyHandle ); +} + +bool SvtLinguConfig::SetProperty( const OUString &rPropertyName, const uno::Any &rValue ) +{ + return GetConfigItem().SetProperty( rPropertyName, rValue ); +} + +bool SvtLinguConfig::SetProperty( sal_Int32 nPropertyHandle, const uno::Any &rValue ) +{ + return GetConfigItem().SetProperty( nPropertyHandle, rValue ); +} + +void SvtLinguConfig::GetOptions( SvtLinguOptions &rOptions ) const +{ + rOptions = GetConfigItem().GetOptions(); +} + +bool SvtLinguConfig::IsReadOnly( const OUString &rPropertyName ) const +{ + return GetConfigItem().IsReadOnly( rPropertyName ); +} + +bool SvtLinguConfig::GetElementNamesFor( + const OUString &rNodeName, + uno::Sequence< OUString > &rElementNames ) const +{ + bool bSuccess = false; + try + { + uno::Reference< container::XNameAccess > xNA( GetMainUpdateAccess(), uno::UNO_QUERY_THROW ); + xNA.set( xNA->getByName("ServiceManager"), uno::UNO_QUERY_THROW ); + xNA.set( xNA->getByName( rNodeName ), uno::UNO_QUERY_THROW ); + rElementNames = xNA->getElementNames(); + bSuccess = true; + } + catch (uno::Exception &) + { + } + return bSuccess; +} + +bool SvtLinguConfig::GetSupportedDictionaryFormatsFor( + const OUString &rSetName, + const OUString &rSetEntry, + uno::Sequence< OUString > &rFormatList ) const +{ + if (rSetName.isEmpty() || rSetEntry.isEmpty()) + return false; + bool bSuccess = false; + try + { + uno::Reference< container::XNameAccess > xNA( GetMainUpdateAccess(), uno::UNO_QUERY_THROW ); + xNA.set( xNA->getByName("ServiceManager"), uno::UNO_QUERY_THROW ); + xNA.set( xNA->getByName( rSetName ), uno::UNO_QUERY_THROW ); + xNA.set( xNA->getByName( rSetEntry ), uno::UNO_QUERY_THROW ); + if (xNA->getByName( "SupportedDictionaryFormats" ) >>= rFormatList) + bSuccess = true; + DBG_ASSERT( rFormatList.hasElements(), "supported dictionary format list is empty" ); + } + catch (uno::Exception &) + { + } + return bSuccess; +} + +static bool lcl_GetFileUrlFromOrigin( + OUString /*out*/ &rFileUrl, + const OUString &rOrigin ) +{ + OUString aURL( + comphelper::getExpandedUri( + comphelper::getProcessComponentContext(), rOrigin)); + if (aURL.startsWith( FILE_PROTOCOL )) + { + rFileUrl = aURL; + return true; + } + else + { + SAL_WARN( + "unotools.config", "not a file URL, <" << aURL << ">" ); + return false; + } +} + +bool SvtLinguConfig::GetDictionaryEntry( + const OUString &rNodeName, + SvtLinguConfigDictionaryEntry &rDicEntry ) const +{ + if (rNodeName.isEmpty()) + return false; + bool bSuccess = false; + try + { + uno::Reference< container::XNameAccess > xNA( GetMainUpdateAccess(), uno::UNO_QUERY_THROW ); + xNA.set( xNA->getByName("ServiceManager"), uno::UNO_QUERY_THROW ); + xNA.set( xNA->getByName( aG_Dictionaries ), uno::UNO_QUERY_THROW ); + xNA.set( xNA->getByName( rNodeName ), uno::UNO_QUERY_THROW ); + + // read group data... + uno::Sequence< OUString > aLocations; + OUString aFormatName; + uno::Sequence< OUString > aLocaleNames; + bSuccess = (xNA->getByName( "Locations" ) >>= aLocations) && + (xNA->getByName( "Format" ) >>= aFormatName) && + (xNA->getByName( "Locales" ) >>= aLocaleNames); + DBG_ASSERT( aLocations.hasElements(), "Dictionary locations not set" ); + DBG_ASSERT( !aFormatName.isEmpty(), "Dictionary format name not set" ); + DBG_ASSERT( aLocaleNames.hasElements(), "No locales set for the dictionary" ); + + // if successful continue + if (bSuccess) + { + // get file URL's for the locations + for (OUString& rLocation : aLocations) + { + if (!lcl_GetFileUrlFromOrigin( rLocation, rLocation )) + bSuccess = false; + } + + // if everything was fine return the result + if (bSuccess) + { + rDicEntry.aLocations = aLocations; + rDicEntry.aFormatName = aFormatName; + rDicEntry.aLocaleNames = aLocaleNames; + } + } + } + catch (uno::Exception &) + { + } + return bSuccess; +} + +uno::Sequence< OUString > SvtLinguConfig::GetDisabledDictionaries() const +{ + uno::Sequence< OUString > aResult; + try + { + uno::Reference< container::XNameAccess > xNA( GetMainUpdateAccess(), uno::UNO_QUERY_THROW ); + xNA.set( xNA->getByName("ServiceManager"), uno::UNO_QUERY_THROW ); + xNA->getByName( "DisabledDictionaries" ) >>= aResult; + } + catch (uno::Exception &) + { + } + return aResult; +} + +std::vector< SvtLinguConfigDictionaryEntry > SvtLinguConfig::GetActiveDictionariesByFormat( + const OUString &rFormatName ) const +{ + std::vector< SvtLinguConfigDictionaryEntry > aRes; + if (rFormatName.isEmpty()) + return aRes; + + try + { + uno::Sequence< OUString > aElementNames; + GetElementNamesFor( aG_Dictionaries, aElementNames ); + + const uno::Sequence< OUString > aDisabledDics( GetDisabledDictionaries() ); + + SvtLinguConfigDictionaryEntry aDicEntry; + for (const OUString& rElementName : std::as_const(aElementNames)) + { + // does dictionary match the format we are looking for? + if (GetDictionaryEntry( rElementName, aDicEntry ) && + aDicEntry.aFormatName == rFormatName) + { + // check if it is active or not + bool bDicIsActive = std::none_of(aDisabledDics.begin(), aDisabledDics.end(), + [&rElementName](const OUString& rDic) { return rDic == rElementName; }); + + if (bDicIsActive) + { + DBG_ASSERT( !aDicEntry.aFormatName.isEmpty(), + "FormatName not set" ); + DBG_ASSERT( aDicEntry.aLocations.hasElements(), + "Locations not set" ); + DBG_ASSERT( aDicEntry.aLocaleNames.hasElements(), + "Locales not set" ); + aRes.push_back( aDicEntry ); + } + } + } + } + catch (uno::Exception &) + { + } + + return aRes; +} + +uno::Reference< util::XChangesBatch > const & SvtLinguConfig::GetMainUpdateAccess() const +{ + if (!m_xMainUpdateAccess.is()) + { + try + { + // get configuration provider + uno::Reference< uno::XComponentContext > xContext = comphelper::getProcessComponentContext(); + uno::Reference< lang::XMultiServiceFactory > xConfigurationProvider = + configuration::theDefaultProvider::get( xContext ); + + // get configuration update access + beans::PropertyValue aValue; + aValue.Name = "nodepath"; + aValue.Value <<= OUString("org.openoffice.Office.Linguistic"); + uno::Sequence< uno::Any > aProps(1); + aProps[0] <<= aValue; + m_xMainUpdateAccess.set( + xConfigurationProvider->createInstanceWithArguments( + "com.sun.star.configuration.ConfigurationUpdateAccess", aProps), + uno::UNO_QUERY_THROW ); + } + catch (uno::Exception &) + { + } + } + + return m_xMainUpdateAccess; +} + +OUString SvtLinguConfig::GetVendorImageUrl_Impl( + const OUString &rServiceImplName, + const OUString &rImageName ) const +{ + OUString aRes; + try + { + uno::Reference< container::XNameAccess > xImagesNA( GetMainUpdateAccess(), uno::UNO_QUERY_THROW ); + xImagesNA.set( xImagesNA->getByName("Images"), uno::UNO_QUERY_THROW ); + + uno::Reference< container::XNameAccess > xNA( xImagesNA->getByName("ServiceNameEntries"), uno::UNO_QUERY_THROW ); + xNA.set( xNA->getByName( rServiceImplName ), uno::UNO_QUERY_THROW ); + uno::Any aAny(xNA->getByName("VendorImagesNode")); + OUString aVendorImagesNode; + if (aAny >>= aVendorImagesNode) + { + xNA = xImagesNA; + xNA.set( xNA->getByName("VendorImages"), uno::UNO_QUERY_THROW ); + xNA.set( xNA->getByName( aVendorImagesNode ), uno::UNO_QUERY_THROW ); + aAny = xNA->getByName( rImageName ); + OUString aTmp; + if (aAny >>= aTmp) + { + if (lcl_GetFileUrlFromOrigin( aTmp, aTmp )) + aRes = aTmp; + } + } + } + catch (uno::Exception &) + { + DBG_UNHANDLED_EXCEPTION("unotools"); + } + return aRes; +} + +OUString SvtLinguConfig::GetSpellAndGrammarContextSuggestionImage( + const OUString &rServiceImplName +) const +{ + OUString aRes; + if (!rServiceImplName.isEmpty()) + { + aRes = GetVendorImageUrl_Impl( rServiceImplName, "SpellAndGrammarContextMenuSuggestionImage" ); + } + return aRes; +} + +OUString SvtLinguConfig::GetSpellAndGrammarContextDictionaryImage( + const OUString &rServiceImplName +) const +{ + OUString aRes; + if (!rServiceImplName.isEmpty()) + { + aRes = GetVendorImageUrl_Impl( rServiceImplName, "SpellAndGrammarContextMenuDictionaryImage" ); + } + return aRes; +} + +OUString SvtLinguConfig::GetSynonymsContextImage( + const OUString &rServiceImplName +) const +{ + OUString aRes; + if (!rServiceImplName.isEmpty()) + { + OUString aPath( GetVendorImageUrl_Impl( rServiceImplName, "SynonymsContextMenuImage" ) ); + aRes = aPath; + } + return aRes; +} + +bool SvtLinguConfig::HasGrammarChecker() const +{ + bool bRes = false; + + try + { + uno::Reference< container::XNameAccess > xNA( GetMainUpdateAccess(), uno::UNO_QUERY_THROW ); + xNA.set( xNA->getByName("ServiceManager"), uno::UNO_QUERY_THROW ); + xNA.set( xNA->getByName("GrammarCheckerList"), uno::UNO_QUERY_THROW ); + + uno::Sequence< OUString > aElementNames( xNA->getElementNames() ); + bRes = aElementNames.hasElements(); + } + catch (const uno::Exception&) + { + } + + return bRes; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/misccfg.cxx b/unotools/source/config/misccfg.cxx new file mode 100644 index 000000000..4941773af --- /dev/null +++ b/unotools/source/config/misccfg.cxx @@ -0,0 +1,249 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <o3tl/any.hxx> +#include <unotools/misccfg.hxx> +#include <rtl/instance.hxx> +#include <unotools/configitem.hxx> +#include <tools/debug.hxx> +#include <com/sun/star/uno/Sequence.hxx> +#include <osl/mutex.hxx> +#include "itemholder1.hxx" + +using namespace com::sun::star::uno; + +namespace utl +{ +class SfxMiscCfg; + +static std::weak_ptr<SfxMiscCfg> g_pOptions; + +class SfxMiscCfg : public utl::ConfigItem +{ +private: + bool bPaperSize; // printer warnings + bool bPaperOrientation; + bool bNotFound; + sal_Int32 nYear2000; // two digit year representation + + static css::uno::Sequence<OUString> GetPropertyNames(); + void Load(); + + virtual void ImplCommit() final override; + +public: + SfxMiscCfg( ); + virtual ~SfxMiscCfg( ) override; + + virtual void Notify( const css::uno::Sequence<OUString>& aPropertyNames) override; + + bool IsNotFoundWarning() const {return bNotFound;} + void SetNotFoundWarning( bool bSet); + + bool IsPaperSizeWarning() const {return bPaperSize;} + void SetPaperSizeWarning(bool bSet); + + bool IsPaperOrientationWarning() const {return bPaperOrientation;} + void SetPaperOrientationWarning( bool bSet); + + // 0 ... 99 + sal_Int32 GetYear2000() const { return nYear2000; } + void SetYear2000( sal_Int32 nSet ); + +}; + +SfxMiscCfg::SfxMiscCfg() : + ConfigItem( "Office.Common" ), + bPaperSize(false), + bPaperOrientation (false), + bNotFound (false), + nYear2000( 1930 ) +{ + Load(); +} + +SfxMiscCfg::~SfxMiscCfg() +{ + if ( IsModified() ) + Commit(); +} + +void SfxMiscCfg::SetNotFoundWarning( bool bSet) +{ + if(bNotFound != bSet) + SetModified(); + bNotFound = bSet; +} + +void SfxMiscCfg::SetPaperSizeWarning( bool bSet) +{ + if(bPaperSize != bSet) + SetModified(); + bPaperSize = bSet; +} + +void SfxMiscCfg::SetPaperOrientationWarning( bool bSet) +{ + if(bPaperOrientation != bSet) + SetModified(); + bPaperOrientation = bSet; +} + +void SfxMiscCfg::SetYear2000( sal_Int32 nSet ) +{ + if(nYear2000 != nSet) + SetModified(); + nYear2000 = nSet; +} + +Sequence<OUString> SfxMiscCfg::GetPropertyNames() +{ + return + { + OUString("Print/Warning/PaperSize"), + OUString("Print/Warning/PaperOrientation"), + OUString("Print/Warning/NotFound"), + OUString("DateFormat/TwoDigitYear") + }; +} + +void SfxMiscCfg::Load() +{ + const Sequence<OUString>& rNames = GetPropertyNames(); + Sequence<Any> aValues = GetProperties(rNames); + EnableNotification(rNames); + const Any* pValues = aValues.getConstArray(); + DBG_ASSERT(aValues.getLength() == rNames.getLength(), "GetProperties failed"); + if(aValues.getLength() == rNames.getLength()) + { + for(int nProp = 0; nProp < rNames.getLength(); nProp++) + { + if(pValues[nProp].hasValue()) + { + switch(nProp) + { + case 0: bPaperSize = *o3tl::doAccess<bool>(pValues[nProp]); break; //"Print/Warning/PaperSize", + case 1: bPaperOrientation = *o3tl::doAccess<bool>(pValues[nProp]); break; //"Print/Warning/PaperOrientation", + case 2: bNotFound = *o3tl::doAccess<bool>(pValues[nProp]); break; //"Print/Warning/NotFound", + case 3: pValues[nProp] >>= nYear2000;break; //"DateFormat/TwoDigitYear", + } + } + } + } +} + +void SfxMiscCfg::Notify( const css::uno::Sequence<OUString>& ) +{ + Load(); +} + +void SfxMiscCfg::ImplCommit() +{ + const Sequence<OUString>& rNames = GetPropertyNames(); + Sequence<Any> aValues(rNames.getLength()); + Any* pValues = aValues.getArray(); + + for(int nProp = 0; nProp < rNames.getLength(); nProp++) + { + switch(nProp) + { + case 0: pValues[nProp] <<= bPaperSize;break; //"Print/Warning/PaperSize", + case 1: pValues[nProp] <<= bPaperOrientation;break; //"Print/Warning/PaperOrientation", + case 2: pValues[nProp] <<= bNotFound;break; //"Print/Warning/NotFound", + case 3: pValues[nProp] <<= nYear2000;break; //"DateFormat/TwoDigitYear", + } + } + PutProperties(rNames, aValues); +} + +namespace +{ + class LocalSingleton : public rtl::Static< osl::Mutex, LocalSingleton > + { + }; +} + +MiscCfg::MiscCfg( ) +{ + // Global access, must be guarded (multithreading) + ::osl::MutexGuard aGuard( LocalSingleton::get() ); + m_pImpl = g_pOptions.lock(); + if ( !m_pImpl ) + { + m_pImpl = std::make_shared<SfxMiscCfg>(); + g_pOptions = m_pImpl; + ItemHolder1::holdConfigItem(EItem::MiscConfig); + } + + m_pImpl->AddListener(this); +} + +MiscCfg::~MiscCfg( ) +{ + // Global access, must be guarded (multithreading) + ::osl::MutexGuard aGuard( LocalSingleton::get() ); + m_pImpl->RemoveListener(this); + m_pImpl.reset(); +} + +bool MiscCfg::IsNotFoundWarning() const +{ + return m_pImpl->IsNotFoundWarning(); +} + +void MiscCfg::SetNotFoundWarning( bool bSet) +{ + m_pImpl->SetNotFoundWarning( bSet ); +} + +bool MiscCfg::IsPaperSizeWarning() const +{ + return m_pImpl->IsPaperSizeWarning(); +} + +void MiscCfg::SetPaperSizeWarning(bool bSet) +{ + m_pImpl->SetPaperSizeWarning( bSet ); +} + +bool MiscCfg::IsPaperOrientationWarning() const +{ + return m_pImpl->IsPaperOrientationWarning(); +} + +void MiscCfg::SetPaperOrientationWarning( bool bSet) +{ + m_pImpl->SetPaperOrientationWarning( bSet ); +} + +sal_Int32 MiscCfg::GetYear2000() const +{ + return m_pImpl->GetYear2000(); +} + +void MiscCfg::SetYear2000( sal_Int32 nSet ) +{ + m_pImpl->SetYear2000( nSet ); +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/moduleoptions.cxx b/unotools/source/config/moduleoptions.cxx new file mode 100644 index 000000000..8082749e9 --- /dev/null +++ b/unotools/source/config/moduleoptions.cxx @@ -0,0 +1,1118 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unotools/moduleoptions.hxx> +#include <comphelper/sequenceashashmap.hxx> +#include <unotools/configitem.hxx> +#include <comphelper/processfactory.hxx> +#include <comphelper/sequence.hxx> +#include <osl/diagnose.h> +#include <o3tl/enumarray.hxx> +#include <rtl/instance.hxx> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Sequence.hxx> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/container/XNameAccess.hpp> +#include <com/sun/star/frame/XModel.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/document/XTypeDetection.hpp> +#include <com/sun/star/util/PathSubstitution.hpp> +#include <com/sun/star/util/XStringSubstitution.hpp> + +#include "itemholder1.hxx" + +/*-************************************************************************************************************ + @descr These values are used to define necessary keys from our configuration management to support + all functionality of these implementation. + It's a fast way to make changes if some keys change its name or location! + + Property handle are necessary to specify right position in return list of configuration + for asked values. We ask it with a list of properties to get its values. The returned list + has the same order like our given name list! + e.g.: + NAMELIST[ PROPERTYHANDLE_xxx ] => VALUELIST[ PROPERTYHANDLE_xxx ] +*//*-*************************************************************************************************************/ +#define ROOTNODE_FACTORIES "Setup/Office/Factories" +#define PATHSEPARATOR "/" + +// Attention: The property "ooSetupFactoryEmptyDocumentURL" is read from configuration but not used! There is +// special code that uses hard coded strings to return them. +#define PROPERTYNAME_SHORTNAME "ooSetupFactoryShortName" +#define PROPERTYNAME_TEMPLATEFILE "ooSetupFactoryTemplateFile" +#define PROPERTYNAME_WINDOWATTRIBUTES "ooSetupFactoryWindowAttributes" +#define PROPERTYNAME_EMPTYDOCUMENTURL "ooSetupFactoryEmptyDocumentURL" +#define PROPERTYNAME_DEFAULTFILTER "ooSetupFactoryDefaultFilter" +#define PROPERTYNAME_ICON "ooSetupFactoryIcon" + +#define PROPERTYHANDLE_SHORTNAME 0 +#define PROPERTYHANDLE_TEMPLATEFILE 1 +#define PROPERTYHANDLE_WINDOWATTRIBUTES 2 +#define PROPERTYHANDLE_EMPTYDOCUMENTURL 3 +#define PROPERTYHANDLE_DEFAULTFILTER 4 +#define PROPERTYHANDLE_ICON 5 + +#define PROPERTYCOUNT 6 + +#define FACTORYNAME_WRITER "com.sun.star.text.TextDocument" +#define FACTORYNAME_WRITERWEB "com.sun.star.text.WebDocument" +#define FACTORYNAME_WRITERGLOBAL "com.sun.star.text.GlobalDocument" +#define FACTORYNAME_CALC "com.sun.star.sheet.SpreadsheetDocument" +#define FACTORYNAME_DRAW "com.sun.star.drawing.DrawingDocument" +#define FACTORYNAME_IMPRESS "com.sun.star.presentation.PresentationDocument" +#define FACTORYNAME_MATH "com.sun.star.formula.FormulaProperties" +#define FACTORYNAME_CHART "com.sun.star.chart2.ChartDocument" +#define FACTORYNAME_DATABASE "com.sun.star.sdb.OfficeDatabaseDocument" +#define FACTORYNAME_STARTMODULE "com.sun.star.frame.StartModule" +#define FACTORYNAME_BASIC "com.sun.star.script.BasicIDE" + +#define FACTORYCOUNT 11 + +namespace { + +/*-************************************************************************************************************ + @descr This struct hold information about one factory. We declare a complete array which can hold infos + for all well known factories. Values of enum "EFactory" (see header!) are directly used as index! + So we can support a fast access on this information. +*//*-*************************************************************************************************************/ +struct FactoryInfo +{ + public: + + // initialize empty struct + FactoryInfo() + { + free(); + } + + // easy way to reset struct member! + void free() + { + bInstalled = false; + sFactory.clear(); + sTemplateFile.clear(); + sDefaultFilter.clear(); + nIcon = 0; + bChangedTemplateFile = false; + bChangedDefaultFilter = false; + bDefaultFilterReadonly = false; + } + + // returns list of properties, which has changed only! + // We use given value of sNodeBase to build full qualified paths ... + // Last sign of it must be "/". because we use it directly, without any additional things! + css::uno::Sequence< css::beans::PropertyValue > getChangedProperties( const OUString& sNodeBase ) + { + // a) reserve memory for max. count of changed properties + // b) add names and values of changed ones only and count it + // c) resize return list by using count + css::uno::Sequence< css::beans::PropertyValue > lProperties ( 4 ); + sal_Int8 nRealyChanged = 0; + + if( bChangedTemplateFile ) + { + lProperties[nRealyChanged].Name = sNodeBase + PROPERTYNAME_TEMPLATEFILE; + + if ( !sTemplateFile.isEmpty() ) + { + lProperties[nRealyChanged].Value + <<= getStringSubstitution() + ->reSubstituteVariables( sTemplateFile ); + } + else + { + lProperties[nRealyChanged].Value <<= sTemplateFile; + } + + ++nRealyChanged; + } + if( bChangedDefaultFilter ) + { + lProperties[nRealyChanged].Name = sNodeBase + PROPERTYNAME_DEFAULTFILTER; + lProperties[nRealyChanged].Value <<= sDefaultFilter; + ++nRealyChanged; + } + + // Don't forget to reset changed flags! Otherwise we save it again and again and ... + bChangedTemplateFile = false; + bChangedDefaultFilter = false; + + lProperties.realloc( nRealyChanged ); + return lProperties; + } + + // We must support setting AND marking of changed values. + // That's why we can't make our member public. We must use get/set/init methods + // to control access on it! + bool getInstalled () const { return bInstalled; }; + const OUString& getFactory () const { return sFactory; }; + const OUString& getTemplateFile () const { return sTemplateFile; }; + const OUString& getDefaultFilter () const { return sDefaultFilter; }; + bool isDefaultFilterReadonly() const { return bDefaultFilterReadonly; } + sal_Int32 getIcon () const { return nIcon; }; + + // If you call set-methods - we check for changes of values and mark it. + // But if you wish to set it without that... you must initialize it! + void initInstalled () { bInstalled = true; } + void initFactory ( const OUString& sNewFactory ) { sFactory = sNewFactory; } + void initDefaultFilter ( const OUString& sNewDefaultFilter ) { sDefaultFilter = sNewDefaultFilter; } + void setDefaultFilterReadonly( const bool bVal){bDefaultFilterReadonly = bVal;} + void initIcon ( sal_Int32 nNewIcon ) { nIcon = nNewIcon; } + + void initTemplateFile( const OUString& sNewTemplateFile ) + { + if ( !sNewTemplateFile.isEmpty() ) + { + sTemplateFile= getStringSubstitution()->substituteVariables( sNewTemplateFile, false ); + } + else + { + sTemplateFile = sNewTemplateFile; + } + } + + void setTemplateFile( const OUString& sNewTemplateFile ) + { + if( sTemplateFile != sNewTemplateFile ) + { + sTemplateFile = sNewTemplateFile; + bChangedTemplateFile = true; + } + }; + + void setDefaultFilter( const OUString& sNewDefaultFilter ) + { + if( sDefaultFilter != sNewDefaultFilter ) + { + sDefaultFilter = sNewDefaultFilter; + bChangedDefaultFilter = true; + } + }; + + private: + css::uno::Reference< css::util::XStringSubstitution > const & getStringSubstitution() + { + if ( !xSubstVars.is() ) + { + xSubstVars.set( css::util::PathSubstitution::create(::comphelper::getProcessComponentContext()) ); + } + return xSubstVars; + } + + bool bInstalled; + OUString sFactory; + OUString sTemplateFile; + OUString sDefaultFilter; + sal_Int32 nIcon; + + bool bChangedTemplateFile :1; + bool bChangedDefaultFilter :1; + bool bDefaultFilterReadonly :1; + + css::uno::Reference< css::util::XStringSubstitution > xSubstVars; +}; + +} + +class SvtModuleOptions_Impl : public ::utl::ConfigItem +{ + + // public methods + + public: + + // constructor / destructor + + SvtModuleOptions_Impl(); + virtual ~SvtModuleOptions_Impl() override; + + // override methods of baseclass + + virtual void Notify( const css::uno::Sequence< OUString >& lPropertyNames ) override; + + // public interface + + bool IsModuleInstalled ( SvtModuleOptions::EModule eModule ) const; + css::uno::Sequence < OUString > GetAllServiceNames(); + OUString const & GetFactoryName ( SvtModuleOptions::EFactory eFactory ) const; + OUString const & GetFactoryStandardTemplate( SvtModuleOptions::EFactory eFactory ) const; + static OUString GetFactoryEmptyDocumentURL( SvtModuleOptions::EFactory eFactory ); + OUString const & GetFactoryDefaultFilter ( SvtModuleOptions::EFactory eFactory ) const; + bool IsDefaultFilterReadonly( SvtModuleOptions::EFactory eFactory ) const; + sal_Int32 GetFactoryIcon ( SvtModuleOptions::EFactory eFactory ) const; + static bool ClassifyFactoryByName ( const OUString& sName , + SvtModuleOptions::EFactory& eFactory ); + void SetFactoryStandardTemplate( SvtModuleOptions::EFactory eFactory , + const OUString& sTemplate ); + void SetFactoryDefaultFilter ( SvtModuleOptions::EFactory eFactory , + const OUString& sFilter ); + void MakeReadonlyStatesAvailable(); + + // private methods + + private: + static css::uno::Sequence< OUString > impl_ExpandSetNames ( const css::uno::Sequence< OUString >& lSetNames ); + void impl_Read ( const css::uno::Sequence< OUString >& lSetNames ); + + virtual void ImplCommit() override; + + // private member + + private: + o3tl::enumarray<SvtModuleOptions::EFactory, FactoryInfo> m_lFactories; + bool m_bReadOnlyStatesWellKnown; +}; + +/*-************************************************************************************************************ + @short default ctor + @descr We open our configuration here and read all necessary values from it. + These values are cached till everyone call Commit(). Then we write changed ones back to cfg. + + @seealso baseclass ConfigItem + @seealso method impl_Read() + @threadsafe no +*//*-*************************************************************************************************************/ +SvtModuleOptions_Impl::SvtModuleOptions_Impl() + : ::utl::ConfigItem( ROOTNODE_FACTORIES ) + , m_bReadOnlyStatesWellKnown( false ) +{ + // First initialize list of factory infos! Otherwise we couldn't guarantee right working of these class. + for( auto & rFactory : m_lFactories ) + rFactory.free(); + + // Get name list of all existing set node names in configuration to read her properties in impl_Read(). + // These list is a list of long names of our factories. + const css::uno::Sequence< OUString > lFactories = GetNodeNames( OUString() ); + impl_Read( lFactories ); + + // Enable notification for changes by using configuration directly. + // So we can update our internal values immediately. + EnableNotification( lFactories ); +} + +SvtModuleOptions_Impl::~SvtModuleOptions_Impl() +{ + assert(!IsModified()); // should have been committed +} + +/*-************************************************************************************************************ + @short called for notify of configmanager + @descr This method is called from the ConfigManager before application ends or from the + PropertyChangeListener if the sub tree broadcasts changes. You must update our + internal values. + + @attention We are registered for pure set node names only. So we can use our internal method "impl_Read()" to + update our info list. Because - this method expand given name list to full qualified property list + and use it to read the values. These values are filled into our internal member list m_lFactories + at right position. + + @seealso method impl_Read() + + @param "lNames" is the list of set node entries which should be updated. + @threadsafe no +*//*-*************************************************************************************************************/ +void SvtModuleOptions_Impl::Notify( const css::uno::Sequence< OUString >& ) +{ + OSL_FAIL( "SvtModuleOptions_Impl::Notify() Not implemented yet!" ); +} + +/*-**************************************************************************************************** + @short write changes to configuration + @descr This method writes the changed values into the sub tree + and should always called in our destructor to guarantee consistency of config data. + + @attention We clear complete set in configuration first and write it completely new! So we don't must + distinguish between existing, added or removed elements. Our internal cached values + are the only and right ones. + + @seealso baseclass ConfigItem + @threadsafe no +*//*-*****************************************************************************************************/ +void SvtModuleOptions_Impl::ImplCommit() +{ + // Reserve memory for ALL possible factory properties! + // Step over all factories and get her really changed values only. + // Build list of these ones and use it for commit. + css::uno::Sequence< css::beans::PropertyValue > lCommitProperties( FACTORYCOUNT*PROPERTYCOUNT ); + sal_Int32 nRealCount = 0; + OUString sBasePath; + for( FactoryInfo & rInfo : m_lFactories ) + { + // These path is used to build full qualified property names... + // See pInfo->getChangedProperties() for further information + sBasePath = PATHSEPARATOR + rInfo.getFactory() + PATHSEPARATOR; + + const css::uno::Sequence< css::beans::PropertyValue > lChangedProperties = rInfo.getChangedProperties ( sBasePath ); + std::copy(lChangedProperties.begin(), lChangedProperties.end(), std::next(lCommitProperties.begin(), nRealCount)); + nRealCount += lChangedProperties.getLength(); + } + // Resize commit list to real size. + // If nothing to do - suppress calling of configuration... + // It could be too expensive :-) + if( nRealCount > 0 ) + { + lCommitProperties.realloc( nRealCount ); + SetSetProperties( OUString(), lCommitProperties ); + } +} + +/*-**************************************************************************************************** + @short access method to get internal values + @descr These methods implement easy access to our internal values. + You give us right enum value to specify which module interest you ... we return right information. + + @attention Some people use any value as enum ... but we support in header specified values only! + We use it directly as index in our internal list. If enum value isn't right - we crash with an + "index out of range"!!! Please use me right - otherwise there is no guarantee. + @param "eModule" , index in list - specify module + @return Queried information. + + @onerror We return default values. (mostly "not installed"!) + @threadsafe no +*//*-*****************************************************************************************************/ +bool SvtModuleOptions_Impl::IsModuleInstalled( SvtModuleOptions::EModule eModule ) const +{ + switch( eModule ) + { + case SvtModuleOptions::EModule::WRITER: + return m_lFactories[SvtModuleOptions::EFactory::WRITER].getInstalled(); + case SvtModuleOptions::EModule::WEB: + return m_lFactories[SvtModuleOptions::EFactory::WRITERWEB].getInstalled(); + case SvtModuleOptions::EModule::GLOBAL: + return m_lFactories[SvtModuleOptions::EFactory::WRITERGLOBAL].getInstalled(); + case SvtModuleOptions::EModule::CALC: + return m_lFactories[SvtModuleOptions::EFactory::CALC].getInstalled(); + case SvtModuleOptions::EModule::DRAW: + return m_lFactories[SvtModuleOptions::EFactory::DRAW].getInstalled(); + case SvtModuleOptions::EModule::IMPRESS: + return m_lFactories[SvtModuleOptions::EFactory::IMPRESS].getInstalled(); + case SvtModuleOptions::EModule::MATH: + return m_lFactories[SvtModuleOptions::EFactory::MATH].getInstalled(); + case SvtModuleOptions::EModule::CHART: + return m_lFactories[SvtModuleOptions::EFactory::CHART].getInstalled(); + case SvtModuleOptions::EModule::STARTMODULE: + return m_lFactories[SvtModuleOptions::EFactory::STARTMODULE].getInstalled(); + case SvtModuleOptions::EModule::BASIC: + return true; // Couldn't be deselected by setup yet! + case SvtModuleOptions::EModule::DATABASE: + return m_lFactories[SvtModuleOptions::EFactory::DATABASE].getInstalled(); + } + + return false; +} + +css::uno::Sequence < OUString > SvtModuleOptions_Impl::GetAllServiceNames() +{ + std::vector<OUString> aVec; + + for( const auto & rFactory : m_lFactories ) + if( rFactory.getInstalled() ) + aVec.push_back( rFactory.getFactory() ); + + return comphelper::containerToSequence(aVec); +} + +OUString const & SvtModuleOptions_Impl::GetFactoryName( SvtModuleOptions::EFactory eFactory ) const +{ + return m_lFactories[eFactory].getFactory(); +} + +OUString SvtModuleOptions::GetFactoryShortName(SvtModuleOptions::EFactory eFactory) +{ + // Attention: Hard configured yet ... because it's not fine to make changes possible by xml file yet. + // But it's good to plan further possibilities! + + //return m_lFactories[eFactory].sShortName; + + OUString sShortName; + switch( eFactory ) + { + case SvtModuleOptions::EFactory::WRITER : sShortName = "swriter"; + break; + case SvtModuleOptions::EFactory::WRITERWEB: sShortName = "swriter/web"; + break; + case SvtModuleOptions::EFactory::WRITERGLOBAL: sShortName = "swriter/GlobalDocument"; + break; + case SvtModuleOptions::EFactory::CALC : sShortName = "scalc"; + break; + case SvtModuleOptions::EFactory::DRAW : sShortName = "sdraw"; + break; + case SvtModuleOptions::EFactory::IMPRESS : sShortName = "simpress"; + break; + case SvtModuleOptions::EFactory::MATH : sShortName = "smath"; + break; + case SvtModuleOptions::EFactory::CHART : sShortName = "schart"; + break; + case SvtModuleOptions::EFactory::BASIC : sShortName = "sbasic"; + break; + case SvtModuleOptions::EFactory::DATABASE : sShortName = "sdatabase"; + break; + default: + OSL_FAIL( "unknown factory" ); + break; + } + + return sShortName; +} + +OUString const & SvtModuleOptions_Impl::GetFactoryStandardTemplate( SvtModuleOptions::EFactory eFactory ) const +{ + return m_lFactories[eFactory].getTemplateFile(); +} + +OUString SvtModuleOptions_Impl::GetFactoryEmptyDocumentURL( SvtModuleOptions::EFactory eFactory ) +{ + // Attention: Hard configured yet ... because it's not fine to make changes possible by xml file yet. + // But it's good to plan further possibilities! + + //return m_lFactories[eFactory].getEmptyDocumentURL(); + + OUString sURL; + switch( eFactory ) + { + case SvtModuleOptions::EFactory::WRITER : sURL = "private:factory/swriter"; + break; + case SvtModuleOptions::EFactory::WRITERWEB : sURL = "private:factory/swriter/web"; + break; + case SvtModuleOptions::EFactory::WRITERGLOBAL : sURL = "private:factory/swriter/GlobalDocument"; + break; + case SvtModuleOptions::EFactory::CALC : sURL = "private:factory/scalc"; + break; + case SvtModuleOptions::EFactory::DRAW : sURL = "private:factory/sdraw"; + break; + case SvtModuleOptions::EFactory::IMPRESS : sURL = "private:factory/simpress?slot=6686"; + break; + case SvtModuleOptions::EFactory::MATH : sURL = "private:factory/smath"; + break; + case SvtModuleOptions::EFactory::CHART : sURL = "private:factory/schart"; + break; + case SvtModuleOptions::EFactory::BASIC : sURL = "private:factory/sbasic"; + break; + case SvtModuleOptions::EFactory::DATABASE : sURL = "private:factory/sdatabase?Interactive"; + break; + default: + OSL_FAIL( "unknown factory" ); + break; + } + return sURL; +} + +OUString const & SvtModuleOptions_Impl::GetFactoryDefaultFilter( SvtModuleOptions::EFactory eFactory ) const +{ + return m_lFactories[eFactory].getDefaultFilter(); +} + +bool SvtModuleOptions_Impl::IsDefaultFilterReadonly( SvtModuleOptions::EFactory eFactory ) const +{ + return m_lFactories[eFactory].isDefaultFilterReadonly(); +} + +sal_Int32 SvtModuleOptions_Impl::GetFactoryIcon( SvtModuleOptions::EFactory eFactory ) const +{ + return m_lFactories[eFactory].getIcon(); +} + +void SvtModuleOptions_Impl::SetFactoryStandardTemplate( SvtModuleOptions::EFactory eFactory , + const OUString& sTemplate ) +{ + m_lFactories[eFactory].setTemplateFile( sTemplate ); + SetModified(); +} + +void SvtModuleOptions_Impl::SetFactoryDefaultFilter( SvtModuleOptions::EFactory eFactory, + const OUString& sFilter ) +{ + m_lFactories[eFactory].setDefaultFilter( sFilter ); + SetModified(); +} + +/*-************************************************************************************************************ + @short return list of key names of our configuration management which represent our module tree + @descr You give use a list of current existing set node names .. and we expand it for all + well known properties which are necessary for this implementation. + These full expanded list should be used to get values of this properties. + + @seealso ctor + @return List of all relative addressed properties of given set entry names. + + @onerror List will be empty. + @threadsafe no +*//*-*************************************************************************************************************/ +css::uno::Sequence< OUString > SvtModuleOptions_Impl::impl_ExpandSetNames( const css::uno::Sequence< OUString >& lSetNames ) +{ + sal_Int32 nCount = lSetNames.getLength(); + css::uno::Sequence< OUString > lPropNames ( nCount*PROPERTYCOUNT ); + OUString* pPropNames = lPropNames.getArray(); + sal_Int32 nPropStart = 0; + + for( const auto& rSetName : lSetNames ) + { + pPropNames[nPropStart+PROPERTYHANDLE_SHORTNAME ] = rSetName + PATHSEPARATOR PROPERTYNAME_SHORTNAME; + pPropNames[nPropStart+PROPERTYHANDLE_TEMPLATEFILE ] = rSetName + PATHSEPARATOR PROPERTYNAME_TEMPLATEFILE; + pPropNames[nPropStart+PROPERTYHANDLE_WINDOWATTRIBUTES] = rSetName + PATHSEPARATOR PROPERTYNAME_WINDOWATTRIBUTES; + pPropNames[nPropStart+PROPERTYHANDLE_EMPTYDOCUMENTURL] = rSetName + PATHSEPARATOR PROPERTYNAME_EMPTYDOCUMENTURL; + pPropNames[nPropStart+PROPERTYHANDLE_DEFAULTFILTER ] = rSetName + PATHSEPARATOR PROPERTYNAME_DEFAULTFILTER; + pPropNames[nPropStart+PROPERTYHANDLE_ICON ] = rSetName + PATHSEPARATOR PROPERTYNAME_ICON; + nPropStart += PROPERTYCOUNT; + } + + return lPropNames; +} + +/*-************************************************************************************************************ + @short helper to classify given factory by name + @descr Every factory has its own long and short name. So we can match right enum value for internal using. + + @attention We change in/out parameter "eFactory" in every case! But you should use it only, if return value is sal_True! + Algorithm: Set out-parameter to probably value ... and check the longname. + If it matches with these factory - break operation and return true AND right set parameter. + Otherwise try next one and so on. If no factory was found return false. Out parameter eFactory + is set to last tried value but shouldn't be used! Because our return value is false! + @param "sLongName" , long name of factory, which should be classified + @return "eFactory" , right enum value, which match given long name + and true for successfully classification, false otherwise + + @onerror We return false. + @threadsafe no +*//*-*************************************************************************************************************/ +bool SvtModuleOptions_Impl::ClassifyFactoryByName( const OUString& sName, SvtModuleOptions::EFactory& eFactory ) +{ + bool bState; + + eFactory = SvtModuleOptions::EFactory::WRITER; + bState = ( sName == FACTORYNAME_WRITER ); + + if( !bState ) + { + eFactory = SvtModuleOptions::EFactory::WRITERWEB; + bState = ( sName == FACTORYNAME_WRITERWEB ); + } + // no else! + if( !bState ) + { + eFactory = SvtModuleOptions::EFactory::WRITERGLOBAL; + bState = ( sName == FACTORYNAME_WRITERGLOBAL ); + } + // no else! + if( !bState ) + { + eFactory = SvtModuleOptions::EFactory::CALC; + bState = ( sName == FACTORYNAME_CALC ); + } + // no else! + if( !bState ) + { + eFactory = SvtModuleOptions::EFactory::DRAW; + bState = ( sName == FACTORYNAME_DRAW ); + } + // no else! + if( !bState ) + { + eFactory = SvtModuleOptions::EFactory::IMPRESS; + bState = ( sName == FACTORYNAME_IMPRESS ); + } + // no else! + if( !bState ) + { + eFactory = SvtModuleOptions::EFactory::MATH; + bState = ( sName == FACTORYNAME_MATH ); + } + // no else! + if( !bState ) + { + eFactory = SvtModuleOptions::EFactory::CHART; + bState = ( sName == FACTORYNAME_CHART ); + } + // no else! + if( !bState ) + { + eFactory = SvtModuleOptions::EFactory::DATABASE; + bState = ( sName == FACTORYNAME_DATABASE ); + } + // no else! + if( !bState ) + { + eFactory = SvtModuleOptions::EFactory::STARTMODULE; + bState = ( sName == FACTORYNAME_STARTMODULE); + } + // no else! + if( !bState ) + { + eFactory = SvtModuleOptions::EFactory::BASIC; + bState = ( sName == FACTORYNAME_BASIC); + } + + return bState; +} + +/*-************************************************************************************************************ + @short read factory configuration + @descr Give us a list of pure factory names (long names!) which can be used as + direct set node names... and we read her property values and fill internal list. + These method can be used by initial reading at ctor and later updating by "Notify()". + + @seealso ctor + @seealso method Notify() + + @param "lFactories" is the list of set node entries which should be read. + @onerror We do nothing. + @threadsafe no +*//*-*************************************************************************************************************/ +void SvtModuleOptions_Impl::impl_Read( const css::uno::Sequence< OUString >& lFactories ) +{ + // Expand every set node name in lFactories to full qualified paths to its properties + // and get right values from configuration. + const css::uno::Sequence< OUString > lProperties = impl_ExpandSetNames( lFactories ); + const css::uno::Sequence< css::uno::Any > lValues = GetProperties( lProperties ); + + // Safe impossible cases. + // We need values from ALL configuration keys. + // Follow assignment use order of values in relation to our list of key names! + OSL_ENSURE( !(lProperties.getLength()!=lValues.getLength()), "SvtModuleOptions_Impl::impl_Read()\nI miss some values of configuration keys!" ); + + // Algorithm: We step over all given factory names and classify it. These enum value can be used as direct index + // in our member list m_lFactories! VAriable nPropertyStart marks start position of every factory + // and her properties in expanded property/value list. The defines PROPERTHANDLE_xxx are used as offset values + // added to nPropertyStart. So we can address every property relative in these lists. + // If we found any valid values ... we reset all existing information for corresponding m_lFactories-entry and + // use a pointer to these struct in memory directly to set new values. + // But we set it only, if bInstalled is true. Otherwise all other values of a factory can be undeclared .. They + // shouldn't be used then. + // Attention: If a propertyset of a factory will be ignored we must step to next start position of next factory infos! + // see "nPropertyStart += PROPERTYCOUNT" ... + + sal_Int32 nPropertyStart = 0; + FactoryInfo* pInfo = nullptr; + SvtModuleOptions::EFactory eFactory; + + for( const OUString& sFactoryName : lFactories ) + { + if( ClassifyFactoryByName( sFactoryName, eFactory ) ) + { + OUString sTemp; + sal_Int32 nTemp = 0; + + pInfo = &(m_lFactories[eFactory]); + pInfo->free(); + + pInfo->initInstalled(); + pInfo->initFactory ( sFactoryName ); + + if (lValues[nPropertyStart+PROPERTYHANDLE_TEMPLATEFILE] >>= sTemp) + pInfo->initTemplateFile( sTemp ); + if (lValues[nPropertyStart+PROPERTYHANDLE_DEFAULTFILTER ] >>= sTemp) + pInfo->initDefaultFilter( sTemp ); + if (lValues[nPropertyStart+PROPERTYHANDLE_ICON] >>= nTemp) + pInfo->initIcon( nTemp ); + } + nPropertyStart += PROPERTYCOUNT; + } +} + +void SvtModuleOptions_Impl::MakeReadonlyStatesAvailable() +{ + if (m_bReadOnlyStatesWellKnown) + return; + + css::uno::Sequence< OUString > lFactories = GetNodeNames(OUString()); + std::transform(lFactories.begin(), lFactories.end(), lFactories.begin(), + [](const OUString& rFactory) -> OUString { + return rFactory + PATHSEPARATOR PROPERTYNAME_DEFAULTFILTER; + }); + + css::uno::Sequence< sal_Bool > lReadonlyStates = GetReadOnlyStates(lFactories); + sal_Int32 c = lFactories.getLength(); + for (sal_Int32 i=0; i<c; ++i) + { + OUString& rFactoryName = lFactories[i]; + SvtModuleOptions::EFactory eFactory; + + if (!ClassifyFactoryByName(rFactoryName, eFactory)) + continue; + + FactoryInfo& rInfo = m_lFactories[eFactory]; + rInfo.setDefaultFilterReadonly(lReadonlyStates[i]); + } + + m_bReadOnlyStatesWellKnown = true; +} + +namespace { + //global + std::weak_ptr<SvtModuleOptions_Impl> g_pModuleOptions; +} + +/*-************************************************************************************************************ + @short standard constructor and destructor + @descr This will initialize an instance with default values. We initialize/deinitialize our static data + container and create a static mutex, which is used for threadsafe code in further time of this object. + + @seealso method impl_GetOwnStaticMutex() + @threadsafe yes +*//*-*************************************************************************************************************/ +SvtModuleOptions::SvtModuleOptions() +{ + // Global access, must be guarded (multithreading!) + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + + m_pImpl = g_pModuleOptions.lock(); + if( !m_pImpl ) + { + m_pImpl = std::make_shared<SvtModuleOptions_Impl>(); + g_pModuleOptions = m_pImpl; + ItemHolder1::holdConfigItem(EItem::ModuleOptions); + } +} + +SvtModuleOptions::~SvtModuleOptions() +{ + // Global access, must be guarded (multithreading!) + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + + m_pImpl.reset(); +} + +/*-************************************************************************************************************ + @short access to configuration data + @descr This methods allow read/write access to configuration values. + They are threadsafe. All calls are forwarded to impl-data-container. See there for further information! + + @seealso method impl_GetOwnStaticMutex() + @threadsafe yes +*//*-*************************************************************************************************************/ +bool SvtModuleOptions::IsModuleInstalled( EModule eModule ) const +{ + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + return m_pImpl->IsModuleInstalled( eModule ); +} + +OUString SvtModuleOptions::GetFactoryName( EFactory eFactory ) const +{ + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + return m_pImpl->GetFactoryName( eFactory ); +} + +OUString SvtModuleOptions::GetFactoryStandardTemplate( EFactory eFactory ) const +{ + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + return m_pImpl->GetFactoryStandardTemplate( eFactory ); +} + +OUString SvtModuleOptions::GetFactoryEmptyDocumentURL( EFactory eFactory ) const +{ + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + return SvtModuleOptions_Impl::GetFactoryEmptyDocumentURL( eFactory ); +} + +OUString SvtModuleOptions::GetFactoryDefaultFilter( EFactory eFactory ) const +{ + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + return m_pImpl->GetFactoryDefaultFilter( eFactory ); +} + +bool SvtModuleOptions::IsDefaultFilterReadonly( EFactory eFactory ) const +{ + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + m_pImpl->MakeReadonlyStatesAvailable(); + return m_pImpl->IsDefaultFilterReadonly( eFactory ); +} + +sal_Int32 SvtModuleOptions::GetFactoryIcon( EFactory eFactory ) const +{ + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + return m_pImpl->GetFactoryIcon( eFactory ); +} + +bool SvtModuleOptions::ClassifyFactoryByName( const OUString& sName , + EFactory& eFactory ) +{ + // We don't need any mutex here ... because we don't use any member here! + return SvtModuleOptions_Impl::ClassifyFactoryByName( sName, eFactory ); +} + +void SvtModuleOptions::SetFactoryStandardTemplate( EFactory eFactory , + const OUString& sTemplate ) +{ + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + m_pImpl->SetFactoryStandardTemplate( eFactory, sTemplate ); +} + +void SvtModuleOptions::SetFactoryDefaultFilter( EFactory eFactory, + const OUString& sFilter ) +{ + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + m_pImpl->SetFactoryDefaultFilter( eFactory, sFilter ); +} + +bool SvtModuleOptions::IsMath() const +{ + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + return m_pImpl->IsModuleInstalled( EModule::MATH ); +} + +bool SvtModuleOptions::IsChart() const +{ + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + return m_pImpl->IsModuleInstalled( EModule::CHART ); +} + +bool SvtModuleOptions::IsCalc() const +{ + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + return m_pImpl->IsModuleInstalled( EModule::CALC ); +} + +bool SvtModuleOptions::IsDraw() const +{ + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + return m_pImpl->IsModuleInstalled( EModule::DRAW ); +} + +bool SvtModuleOptions::IsWriter() const +{ + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + return m_pImpl->IsModuleInstalled( EModule::WRITER ); +} + +bool SvtModuleOptions::IsImpress() const +{ + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + return m_pImpl->IsModuleInstalled( EModule::IMPRESS ); +} + +bool SvtModuleOptions::IsDataBase() const +{ + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + return m_pImpl->IsModuleInstalled( EModule::DATABASE ); +} + +namespace +{ + class theModuleOptionsMutex : public rtl::Static<osl::Mutex, theModuleOptionsMutex> {}; +} +/*-**************************************************************************************************** + @short return a reference to a static mutex + @descr These class is threadsafe. + We create a static mutex only for one time and use it to protect our refcount and container + member! + @return A reference to a static mutex member. + @threadsafe yes +*//*-*****************************************************************************************************/ +::osl::Mutex& SvtModuleOptions::impl_GetOwnStaticMutex() +{ + return theModuleOptionsMutex::get(); +} + +OUString SvtModuleOptions::GetModuleName( EModule eModule ) const +{ + switch( eModule ) + { + case SvtModuleOptions::EModule::WRITER : { return "Writer"; } + case SvtModuleOptions::EModule::WEB : { return "Web"; } + case SvtModuleOptions::EModule::GLOBAL : { return "Global"; } + case SvtModuleOptions::EModule::CALC : { return "Calc"; } + case SvtModuleOptions::EModule::DRAW : { return "Draw"; } + case SvtModuleOptions::EModule::IMPRESS : { return "Impress"; } + case SvtModuleOptions::EModule::MATH : { return "Math"; } + case SvtModuleOptions::EModule::CHART : { return "Chart"; } + case SvtModuleOptions::EModule::BASIC : { return "Basic"; } + case SvtModuleOptions::EModule::DATABASE : { return "Database"; } + default: + OSL_FAIL( "unknown module" ); + break; + } + + return OUString(); +} + +SvtModuleOptions::EFactory SvtModuleOptions::ClassifyFactoryByShortName(const OUString& sName) +{ + if ( sName == "swriter" ) + return EFactory::WRITER; + if (sName.equalsIgnoreAsciiCase("swriter/Web")) // sometimes they are registered for swriter/web :-( + return EFactory::WRITERWEB; + if (sName.equalsIgnoreAsciiCase("swriter/GlobalDocument")) // sometimes they are registered for swriter/globaldocument :-( + return EFactory::WRITERGLOBAL; + if ( sName == "scalc" ) + return EFactory::CALC; + if ( sName == "sdraw" ) + return EFactory::DRAW; + if ( sName == "simpress" ) + return EFactory::IMPRESS; + if ( sName == "schart" ) + return EFactory::CHART; + if ( sName == "smath" ) + return EFactory::MATH; + if ( sName == "sbasic" ) + return EFactory::BASIC; + if ( sName == "sdatabase" ) + return EFactory::DATABASE; + + return EFactory::UNKNOWN_FACTORY; +} + +SvtModuleOptions::EFactory SvtModuleOptions::ClassifyFactoryByServiceName(const OUString& sName) +{ + if (sName == FACTORYNAME_WRITERGLOBAL) + return EFactory::WRITERGLOBAL; + if (sName == FACTORYNAME_WRITERWEB) + return EFactory::WRITERWEB; + if (sName == FACTORYNAME_WRITER) + return EFactory::WRITER; + if (sName == FACTORYNAME_CALC) + return EFactory::CALC; + if (sName == FACTORYNAME_DRAW) + return EFactory::DRAW; + if (sName == FACTORYNAME_IMPRESS) + return EFactory::IMPRESS; + if (sName == FACTORYNAME_MATH) + return EFactory::MATH; + if (sName == FACTORYNAME_CHART) + return EFactory::CHART; + if (sName == FACTORYNAME_DATABASE) + return EFactory::DATABASE; + if (sName == FACTORYNAME_STARTMODULE) + return EFactory::STARTMODULE; + if (sName == FACTORYNAME_BASIC) + return EFactory::BASIC; + + return EFactory::UNKNOWN_FACTORY; +} + +SvtModuleOptions::EFactory SvtModuleOptions::ClassifyFactoryByURL(const OUString& sURL , + const css::uno::Sequence< css::beans::PropertyValue >& lMediaDescriptor) +{ + css::uno::Reference< css::uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext(); + + css::uno::Reference< css::container::XNameAccess > xFilterCfg; + css::uno::Reference< css::container::XNameAccess > xTypeCfg; + try + { + xFilterCfg.set( + xContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.FilterFactory", xContext), css::uno::UNO_QUERY); + xTypeCfg.set( + xContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", xContext), css::uno::UNO_QUERY); + } + catch(const css::uno::RuntimeException&) + { throw; } + catch(const css::uno::Exception&) + { return EFactory::UNKNOWN_FACTORY; } + + ::comphelper::SequenceAsHashMap stlDesc(lMediaDescriptor); + + // is there already a filter inside the descriptor? + OUString sFilterName = stlDesc.getUnpackedValueOrDefault("FilterName", OUString()); + if (!sFilterName.isEmpty()) + { + try + { + ::comphelper::SequenceAsHashMap stlFilterProps (xFilterCfg->getByName(sFilterName)); + OUString sDocumentService = stlFilterProps.getUnpackedValueOrDefault("DocumentService", OUString()); + SvtModuleOptions::EFactory eApp = SvtModuleOptions::ClassifyFactoryByServiceName(sDocumentService); + + if (eApp != EFactory::UNKNOWN_FACTORY) + return eApp; + } + catch(const css::uno::RuntimeException&) + { throw; } + catch(const css::uno::Exception&) + { /* do nothing here ... may the following code can help!*/ } + } + + // is there already a type inside the descriptor? + OUString sTypeName = stlDesc.getUnpackedValueOrDefault("TypeName", OUString()); + if (sTypeName.isEmpty()) + { + // no :-( + // start flat detection of URL + css::uno::Reference< css::document::XTypeDetection > xDetect(xTypeCfg, css::uno::UNO_QUERY); + sTypeName = xDetect->queryTypeByURL(sURL); + } + + if (sTypeName.isEmpty()) + return EFactory::UNKNOWN_FACTORY; + + // yes - there is a type info + // Try to find the preferred filter. + try + { + ::comphelper::SequenceAsHashMap stlTypeProps (xTypeCfg->getByName(sTypeName)); + OUString sPreferredFilter = stlTypeProps.getUnpackedValueOrDefault("PreferredFilter", OUString()); + ::comphelper::SequenceAsHashMap stlFilterProps (xFilterCfg->getByName(sPreferredFilter)); + OUString sDocumentService = stlFilterProps.getUnpackedValueOrDefault("DocumentService", OUString()); + SvtModuleOptions::EFactory eApp = SvtModuleOptions::ClassifyFactoryByServiceName(sDocumentService); + + if (eApp != EFactory::UNKNOWN_FACTORY) + return eApp; + } + catch(const css::uno::RuntimeException&) + { throw; } + catch(const css::uno::Exception&) + { /* do nothing here ... may the following code can help!*/ } + + // no filter/no type/no detection result => no fun :-) + return EFactory::UNKNOWN_FACTORY; +} + +SvtModuleOptions::EFactory SvtModuleOptions::ClassifyFactoryByModel(const css::uno::Reference< css::frame::XModel >& xModel) +{ + css::uno::Reference< css::lang::XServiceInfo > xInfo(xModel, css::uno::UNO_QUERY); + if (!xInfo.is()) + return EFactory::UNKNOWN_FACTORY; + + const css::uno::Sequence< OUString > lServices = xInfo->getSupportedServiceNames(); + + for (const OUString& rService : lServices) + { + SvtModuleOptions::EFactory eApp = SvtModuleOptions::ClassifyFactoryByServiceName(rService); + if (eApp != EFactory::UNKNOWN_FACTORY) + return eApp; + } + + return EFactory::UNKNOWN_FACTORY; +} + +css::uno::Sequence < OUString > SvtModuleOptions::GetAllServiceNames() +{ + ::osl::MutexGuard aGuard( impl_GetOwnStaticMutex() ); + return m_pImpl->GetAllServiceNames(); +} + +OUString SvtModuleOptions::GetDefaultModuleName() const +{ + OUString aModule; + if (m_pImpl->IsModuleInstalled(SvtModuleOptions::EModule::WRITER)) + aModule = GetFactoryShortName(SvtModuleOptions::EFactory::WRITER); + else if (m_pImpl->IsModuleInstalled(SvtModuleOptions::EModule::CALC)) + aModule = GetFactoryShortName(SvtModuleOptions::EFactory::CALC); + else if (m_pImpl->IsModuleInstalled(SvtModuleOptions::EModule::IMPRESS)) + aModule = GetFactoryShortName(SvtModuleOptions::EFactory::IMPRESS); + else if (m_pImpl->IsModuleInstalled(SvtModuleOptions::EModule::DATABASE)) + aModule = GetFactoryShortName(SvtModuleOptions::EFactory::DATABASE); + else if (m_pImpl->IsModuleInstalled(SvtModuleOptions::EModule::DRAW)) + aModule = GetFactoryShortName(SvtModuleOptions::EFactory::DRAW); + else if (m_pImpl->IsModuleInstalled(SvtModuleOptions::EModule::WEB)) + aModule = GetFactoryShortName(SvtModuleOptions::EFactory::WRITERWEB); + else if (m_pImpl->IsModuleInstalled(SvtModuleOptions::EModule::GLOBAL)) + aModule = GetFactoryShortName(SvtModuleOptions::EFactory::WRITERGLOBAL); + else if (m_pImpl->IsModuleInstalled(SvtModuleOptions::EModule::MATH)) + aModule = GetFactoryShortName(SvtModuleOptions::EFactory::MATH); + return aModule; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/options.cxx b/unotools/source/config/options.cxx new file mode 100644 index 000000000..4da44fa27 --- /dev/null +++ b/unotools/source/config/options.cxx @@ -0,0 +1,114 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> +#include <unotools/options.hxx> + +#include <algorithm> + +using utl::detail::Options; +using utl::ConfigurationBroadcaster; + +utl::ConfigurationListener::~ConfigurationListener() {} + +ConfigurationBroadcaster::ConfigurationBroadcaster() +: m_nBroadcastBlocked( 0 ) +, m_nBlockedHint( ConfigurationHints::NONE ) +{ +} + +ConfigurationBroadcaster::ConfigurationBroadcaster(ConfigurationBroadcaster const & rSource) +: mpList( rSource.mpList ? new IMPL_ConfigurationListenerList(*rSource.mpList) : nullptr ) +, m_nBroadcastBlocked( rSource.m_nBroadcastBlocked ) +, m_nBlockedHint( rSource.m_nBlockedHint ) +{ +} + +ConfigurationBroadcaster::~ConfigurationBroadcaster() +{ +} + +ConfigurationBroadcaster & ConfigurationBroadcaster::operator =( + ConfigurationBroadcaster const & other) +{ + if (&other != this) { + mpList.reset( + other.mpList == nullptr ? nullptr : new IMPL_ConfigurationListenerList(*other.mpList)); + m_nBroadcastBlocked = other.m_nBroadcastBlocked; + m_nBlockedHint = other.m_nBlockedHint; + } + return *this; +} + +void ConfigurationBroadcaster::AddListener( utl::ConfigurationListener* pListener ) +{ + if ( !mpList ) + mpList.reset(new IMPL_ConfigurationListenerList); + mpList->push_back( pListener ); +} + +void ConfigurationBroadcaster::RemoveListener( utl::ConfigurationListener const * pListener ) +{ + if ( mpList ) { + auto it = std::find(mpList->begin(), mpList->end(), pListener); + if ( it != mpList->end() ) + mpList->erase( it ); + } +} + +void ConfigurationBroadcaster::NotifyListeners( ConfigurationHints nHint ) +{ + if ( m_nBroadcastBlocked ) + m_nBlockedHint |= nHint; + else + { + nHint |= m_nBlockedHint; + m_nBlockedHint = ConfigurationHints::NONE; + if ( mpList ) { + for ( size_t n = 0; n < mpList->size(); n++ ) + (*mpList)[ n ]->ConfigurationChanged( this, nHint ); + } + } +} + +void ConfigurationBroadcaster::BlockBroadcasts( bool bBlock ) +{ + if ( bBlock ) + ++m_nBroadcastBlocked; + else if ( m_nBroadcastBlocked ) + { + if ( --m_nBroadcastBlocked == 0 ) + NotifyListeners( ConfigurationHints::NONE ); + } +} + +Options::Options() +{ +} + +Options::~Options() +{ +} + +void Options::ConfigurationChanged( ConfigurationBroadcaster*, ConfigurationHints nHint ) +{ + NotifyListeners( nHint ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/optionsdlg.cxx b/unotools/source/config/optionsdlg.cxx new file mode 100644 index 000000000..18cd92386 --- /dev/null +++ b/unotools/source/config/optionsdlg.cxx @@ -0,0 +1,236 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unotools/optionsdlg.hxx> +#include <unotools/configitem.hxx> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Sequence.hxx> +#include <osl/mutex.hxx> + +#include "itemholder1.hxx" + +#include <unordered_map> + +using namespace utl; +using namespace com::sun::star::beans; +using namespace com::sun::star::uno; + +#define CFG_FILENAME "Office.OptionsDialog" +#define ROOT_NODE "OptionsDialogGroups" +#define PAGES_NODE "Pages" +#define OPTIONS_NODE "Options" + +static SvtOptionsDlgOptions_Impl* pOptions = nullptr; +static sal_Int32 nRefCount = 0; + +class SvtOptionsDlgOptions_Impl : public utl::ConfigItem +{ +private: + typedef std::unordered_map< OUString, bool > OptionNodeList; + + static constexpr OUStringLiteral g_sPathDelimiter = "/"; + OptionNodeList m_aOptionNodeList; + + enum NodeType{ NT_Group, NT_Page, NT_Option }; + void ReadNode( const OUString& _rNode, NodeType _eType ); + bool IsHidden( const OUString& _rPath ) const; + + virtual void ImplCommit() override; + +public: + SvtOptionsDlgOptions_Impl(); + + virtual void Notify( const css::uno::Sequence< OUString >& aPropertyNames ) override; + + static ::osl::Mutex & getInitMutex(); + + bool IsGroupHidden ( const OUString& _rGroup ) const; + bool IsPageHidden ( const OUString& _rPage, + const OUString& _rGroup ) const; + bool IsOptionHidden ( const OUString& _rOption, + const OUString& _rPage, + const OUString& _rGroup ) const; +}; + +namespace +{ + class theOptionsDlgOptions_ImplMutex : public rtl::Static<osl::Mutex, theOptionsDlgOptions_ImplMutex>{}; +} + +::osl::Mutex & SvtOptionsDlgOptions_Impl::getInitMutex() +{ + return theOptionsDlgOptions_ImplMutex::get(); +} + +SvtOptionsDlgOptions_Impl::SvtOptionsDlgOptions_Impl() + : ConfigItem( CFG_FILENAME ), + m_aOptionNodeList( OptionNodeList() ) +{ + OUString sRootNode( ROOT_NODE ); + const Sequence< OUString > aNodeSeq = GetNodeNames( sRootNode ); + OUString sNode( sRootNode + g_sPathDelimiter ); + for ( const auto& rNode : aNodeSeq ) + { + OUString sSubNode( sNode + rNode ); + ReadNode( sSubNode, NT_Group ); + } +} + +void SvtOptionsDlgOptions_Impl::ImplCommit() +{ + // nothing to commit +} + +void SvtOptionsDlgOptions_Impl::Notify( const Sequence< OUString >& ) +{ + // nothing to notify +} + +void SvtOptionsDlgOptions_Impl::ReadNode( const OUString& _rNode, NodeType _eType ) +{ + OUString sNode( _rNode + g_sPathDelimiter ); + OUString sSet; + sal_Int32 nLen = 0; + switch ( _eType ) + { + case NT_Group : + { + sSet = PAGES_NODE; + nLen = 2; + break; + } + + case NT_Page : + { + sSet = OPTIONS_NODE; + nLen = 2; + break; + } + + case NT_Option : + { + nLen = 1; + break; + } + } + + Sequence< OUString > lResult( nLen ); + lResult[0] = sNode + "Hide"; + if ( _eType != NT_Option ) + lResult[1] = sNode + sSet; + + Sequence< Any > aValues = GetProperties( lResult ); + bool bHide = false; + if ( aValues[0] >>= bHide ) + m_aOptionNodeList.emplace( sNode, bHide ); + + if ( _eType != NT_Option ) + { + OUString sNodes( sNode + sSet ); + const Sequence< OUString > aNodes = GetNodeNames( sNodes ); + for ( const auto& rNode : aNodes ) + { + OUString sSubNodeName( sNodes + g_sPathDelimiter + rNode ); + ReadNode( sSubNodeName, _eType == NT_Group ? NT_Page : NT_Option ); + } + } +} + +static OUString getGroupPath( const OUString& _rGroup ) +{ + return OUString( ROOT_NODE "/" + _rGroup + "/" ); +} +static OUString getPagePath( const OUString& _rPage ) +{ + return OUString( PAGES_NODE "/" + _rPage + "/" ); +} +static OUString getOptionPath( const OUString& _rOption ) +{ + return OUString( OPTIONS_NODE "/" + _rOption + "/" ); +} + +bool SvtOptionsDlgOptions_Impl::IsHidden( const OUString& _rPath ) const +{ + bool bRet = false; + OptionNodeList::const_iterator pIter = m_aOptionNodeList.find( _rPath ); + if ( pIter != m_aOptionNodeList.end() ) + bRet = pIter->second; + return bRet; +} + +bool SvtOptionsDlgOptions_Impl::IsGroupHidden( const OUString& _rGroup ) const +{ + return IsHidden( getGroupPath( _rGroup ) ); +} + +bool SvtOptionsDlgOptions_Impl::IsPageHidden( const OUString& _rPage, const OUString& _rGroup ) const +{ + return IsHidden( getGroupPath( _rGroup ) + getPagePath( _rPage ) ); +} + +bool SvtOptionsDlgOptions_Impl::IsOptionHidden( + const OUString& _rOption, const OUString& _rPage, const OUString& _rGroup ) const +{ + return IsHidden( getGroupPath( _rGroup ) + getPagePath( _rPage ) + getOptionPath( _rOption ) ); +} + +SvtOptionsDialogOptions::SvtOptionsDialogOptions() +{ + // Global access, must be guarded (multithreading) + ::osl::MutexGuard aGuard( SvtOptionsDlgOptions_Impl::getInitMutex() ); + ++nRefCount; + if ( !pOptions ) + { + pOptions = new SvtOptionsDlgOptions_Impl; + + ItemHolder1::holdConfigItem( EItem::OptionsDialogOptions ); + } + m_pImp = pOptions; +} + +SvtOptionsDialogOptions::~SvtOptionsDialogOptions() +{ + // Global access, must be guarded (multithreading) + ::osl::MutexGuard aGuard( SvtOptionsDlgOptions_Impl::getInitMutex() ); + if ( !--nRefCount ) + { + if ( pOptions->IsModified() ) + pOptions->Commit(); + delete pOptions; + pOptions = nullptr; + } +} + +bool SvtOptionsDialogOptions::IsGroupHidden( const OUString& _rGroup ) const +{ + return m_pImp->IsGroupHidden( _rGroup ); +} + +bool SvtOptionsDialogOptions::IsPageHidden( const OUString& _rPage, const OUString& _rGroup ) const +{ + return m_pImp->IsPageHidden( _rPage, _rGroup ); +} + +bool SvtOptionsDialogOptions::IsOptionHidden( + const OUString& _rOption, const OUString& _rPage, const OUString& _rGroup ) const +{ + return m_pImp->IsOptionHidden( _rOption, _rPage, _rGroup ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/pathoptions.cxx b/unotools/source/config/pathoptions.cxx new file mode 100644 index 000000000..9d1927042 --- /dev/null +++ b/unotools/source/config/pathoptions.cxx @@ -0,0 +1,850 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <sal/log.hxx> +#include <unotools/pathoptions.hxx> +#include <tools/diagnose_ex.h> +#include <tools/urlobj.hxx> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Sequence.hxx> +#include <osl/mutex.hxx> +#include <osl/file.hxx> + +#include <unotools/ucbhelper.hxx> +#include <comphelper/getexpandeduri.hxx> +#include <comphelper/processfactory.hxx> +#include <com/sun/star/beans/XFastPropertySet.hpp> +#include <com/sun/star/beans/XPropertySetInfo.hpp> +#include <com/sun/star/util/thePathSettings.hpp> +#include <com/sun/star/util/PathSubstitution.hpp> +#include <com/sun/star/util/XStringSubstitution.hpp> +#include <com/sun/star/util/theMacroExpander.hpp> +#include <rtl/instance.hxx> + +#include "itemholder1.hxx" + +#include <set> +#include <unordered_map> +#include <vector> + +using namespace osl; +using namespace utl; +using namespace com::sun::star::uno; +using namespace com::sun::star::beans; +using namespace com::sun::star::util; +using namespace com::sun::star::lang; + +#define SEARCHPATH_DELIMITER ';' +#define SIGN_STARTVARIABLE "$(" +#define SIGN_ENDVARIABLE ")" + +// Supported variables by the old SvtPathOptions implementation +#define SUBSTITUTE_INSTPATH "$(instpath)" +#define SUBSTITUTE_PROGPATH "$(progpath)" +#define SUBSTITUTE_USERPATH "$(userpath)" +#define SUBSTITUTE_PATH "$(path)" + +#define STRPOS_NOTFOUND -1 + +typedef std::unordered_map<OUString, sal_Int32> NameToHandleMap; + +typedef std::set<OUString> VarNameSet; + +// class SvtPathOptions_Impl --------------------------------------------- +class SvtPathOptions_Impl +{ + private: + // Local variables to return const references + std::vector< OUString > m_aPathArray; + Reference< XFastPropertySet > m_xPathSettings; + Reference< XStringSubstitution > m_xSubstVariables; + Reference< XMacroExpander > m_xMacroExpander; + mutable std::unordered_map<sal_Int32, sal_Int32> + m_aMapEnumToPropHandle; + VarNameSet m_aSystemPathVarNames; + + OUString m_aEmptyString; + mutable ::osl::Mutex m_aMutex; + + public: + SvtPathOptions_Impl(); + + // get the paths, not const because of using a mutex + const OUString& GetPath( SvtPathOptions::Paths ); + const OUString& GetAddinPath() { return GetPath( SvtPathOptions::PATH_ADDIN ); } + const OUString& GetAutoCorrectPath() { return GetPath( SvtPathOptions::PATH_AUTOCORRECT ); } + const OUString& GetAutoTextPath() { return GetPath( SvtPathOptions::PATH_AUTOTEXT ); } + const OUString& GetBackupPath() { return GetPath( SvtPathOptions::PATH_BACKUP ); } + const OUString& GetBasicPath() { return GetPath( SvtPathOptions::PATH_BASIC ); } + const OUString& GetBitmapPath() { return GetPath( SvtPathOptions::PATH_BITMAP ); } + const OUString& GetConfigPath() { return GetPath( SvtPathOptions::PATH_CONFIG ); } + const OUString& GetDictionaryPath() { return GetPath( SvtPathOptions::PATH_DICTIONARY ); } + const OUString& GetFavoritesPath() { return GetPath( SvtPathOptions::PATH_FAVORITES ); } + const OUString& GetFilterPath() { return GetPath( SvtPathOptions::PATH_FILTER ); } + const OUString& GetGalleryPath() { return GetPath( SvtPathOptions::PATH_GALLERY ); } + const OUString& GetGraphicPath() { return GetPath( SvtPathOptions::PATH_GRAPHIC ); } + const OUString& GetHelpPath() { return GetPath( SvtPathOptions::PATH_HELP ); } + const OUString& GetLinguisticPath() { return GetPath( SvtPathOptions::PATH_LINGUISTIC ); } + const OUString& GetModulePath() { return GetPath( SvtPathOptions::PATH_MODULE ); } + const OUString& GetPalettePath() { return GetPath( SvtPathOptions::PATH_PALETTE ); } + const OUString& GetIconsetPath() { return GetPath( SvtPathOptions::PATH_ICONSET); } + const OUString& GetPluginPath() { return GetPath( SvtPathOptions::PATH_PLUGIN ); } + const OUString& GetStoragePath() { return GetPath( SvtPathOptions::PATH_STORAGE ); } + const OUString& GetTempPath() { return GetPath( SvtPathOptions::PATH_TEMP ); } + const OUString& GetTemplatePath() { return GetPath( SvtPathOptions::PATH_TEMPLATE ); } + const OUString& GetUserConfigPath() { return GetPath( SvtPathOptions::PATH_USERCONFIG ); } + const OUString& GetWorkPath() { return GetPath( SvtPathOptions::PATH_WORK ); } + const OUString& GetUIConfigPath() { return GetPath( SvtPathOptions::PATH_UICONFIG ); } + const OUString& GetFingerprintPath() { return GetPath( SvtPathOptions::PATH_FINGERPRINT ); } + const OUString& GetNumbertextPath() { return GetPath( SvtPathOptions::PATH_NUMBERTEXT ); } + const OUString& GetClassificationPath() { return GetPath( SvtPathOptions::PATH_CLASSIFICATION ); } + + // set the paths + void SetPath( SvtPathOptions::Paths, const OUString& rNewPath ); + void SetAddinPath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_ADDIN, rPath ); } + void SetAutoCorrectPath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_AUTOCORRECT, rPath ); } + void SetAutoTextPath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_AUTOTEXT, rPath ); } + void SetBackupPath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_BACKUP, rPath ); } + void SetBasicPath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_BASIC, rPath ); } + void SetBitmapPath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_BITMAP, rPath ); } + void SetConfigPath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_CONFIG, rPath ); } + void SetDictionaryPath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_DICTIONARY, rPath ); } + void SetFavoritesPath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_FAVORITES, rPath ); } + void SetFilterPath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_FILTER, rPath ); } + void SetGalleryPath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_GALLERY, rPath ); } + void SetGraphicPath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_GRAPHIC, rPath ); } + void SetHelpPath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_HELP, rPath ); } + void SetLinguisticPath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_LINGUISTIC, rPath ); } + void SetModulePath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_MODULE, rPath ); } + void SetPalettePath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_PALETTE, rPath ); } + void SetPluginPath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_PLUGIN, rPath ); } + void SetStoragePath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_STORAGE, rPath ); } + void SetTempPath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_TEMP, rPath ); } + void SetTemplatePath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_TEMPLATE, rPath ); } + void SetUserConfigPath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_USERCONFIG, rPath ); } + void SetWorkPath( const OUString& rPath ) { SetPath( SvtPathOptions::PATH_WORK, rPath ); } + + OUString SubstVar( const OUString& rVar ) const; + OUString ExpandMacros( const OUString& rPath ) const; + OUString UsePathVariables( const OUString& rPath ) const; +}; + +// global ---------------------------------------------------------------- + +static std::weak_ptr<SvtPathOptions_Impl> g_pOptions; + +namespace { + +// functions ------------------------------------------------------------- +struct PropertyStruct +{ + const char* pPropName; // The ascii name of the Office path + SvtPathOptions::Paths ePath; // The enum value used by SvtPathOptions +}; + +struct VarNameAttribute +{ + const char* pVarName; // The name of the path variable +}; + +} + +static const PropertyStruct aPropNames[] = +{ + { "Addin", SvtPathOptions::PATH_ADDIN }, + { "AutoCorrect", SvtPathOptions::PATH_AUTOCORRECT }, + { "AutoText", SvtPathOptions::PATH_AUTOTEXT }, + { "Backup", SvtPathOptions::PATH_BACKUP }, + { "Basic", SvtPathOptions::PATH_BASIC }, + { "Bitmap", SvtPathOptions::PATH_BITMAP }, + { "Config", SvtPathOptions::PATH_CONFIG }, + { "Dictionary", SvtPathOptions::PATH_DICTIONARY }, + { "Favorite", SvtPathOptions::PATH_FAVORITES }, + { "Filter", SvtPathOptions::PATH_FILTER }, + { "Gallery", SvtPathOptions::PATH_GALLERY }, + { "Graphic", SvtPathOptions::PATH_GRAPHIC }, + { "Help", SvtPathOptions::PATH_HELP }, + { "Iconset", SvtPathOptions::PATH_ICONSET }, + { "Linguistic", SvtPathOptions::PATH_LINGUISTIC }, + { "Module", SvtPathOptions::PATH_MODULE }, + { "Palette", SvtPathOptions::PATH_PALETTE }, + { "Plugin", SvtPathOptions::PATH_PLUGIN }, + { "Storage", SvtPathOptions::PATH_STORAGE }, + { "Temp", SvtPathOptions::PATH_TEMP }, + { "Template", SvtPathOptions::PATH_TEMPLATE }, + { "UserConfig", SvtPathOptions::PATH_USERCONFIG }, + { "Work", SvtPathOptions::PATH_WORK }, + { "UIConfig", SvtPathOptions::PATH_UICONFIG }, + { "Fingerprint", SvtPathOptions::PATH_FINGERPRINT }, + { "Numbertext", SvtPathOptions::PATH_NUMBERTEXT }, + { "Classification", SvtPathOptions::PATH_CLASSIFICATION } +}; + +static const VarNameAttribute aVarNameAttribute[] = +{ + { SUBSTITUTE_INSTPATH }, // $(instpath) + { SUBSTITUTE_PROGPATH }, // $(progpath) + { SUBSTITUTE_USERPATH }, // $(userpath) + { SUBSTITUTE_PATH }, // $(path) +}; + +// class SvtPathOptions_Impl --------------------------------------------- + +const OUString& SvtPathOptions_Impl::GetPath( SvtPathOptions::Paths ePath ) +{ + if ( ePath >= SvtPathOptions::PATH_COUNT ) + return m_aEmptyString; + + ::osl::MutexGuard aGuard( m_aMutex ); + + try + { + OUString aPathValue; + OUString aResult; + sal_Int32 nHandle = m_aMapEnumToPropHandle[ static_cast<sal_Int32>(ePath) ]; + + // Substitution is done by the service itself using the substitution service + Any a = m_xPathSettings->getFastPropertyValue( nHandle ); + a >>= aPathValue; + if( ePath == SvtPathOptions::PATH_ADDIN || + ePath == SvtPathOptions::PATH_FILTER || + ePath == SvtPathOptions::PATH_HELP || + ePath == SvtPathOptions::PATH_MODULE || + ePath == SvtPathOptions::PATH_PLUGIN || + ePath == SvtPathOptions::PATH_STORAGE + ) + { + // These office paths have to be converted to system pates + osl::FileBase::getSystemPathFromFileURL( aPathValue, aResult ); + aPathValue = aResult; + } + else if (ePath == SvtPathOptions::PATH_PALETTE || + ePath == SvtPathOptions::PATH_ICONSET) + { + auto ctx = comphelper::getProcessComponentContext(); + OUStringBuffer buf(aPathValue.getLength()*2); + for (sal_Int32 i = 0;;) + { + buf.append( + comphelper::getExpandedUri( + ctx, aPathValue.getToken(0, ';', i))); + if (i == -1) { + break; + } + buf.append(';'); + } + aPathValue = buf.makeStringAndClear(); + } + + m_aPathArray[ ePath ] = aPathValue; + return m_aPathArray[ ePath ]; + } + catch (UnknownPropertyException &) + { + } + + return m_aEmptyString; +} + +void SvtPathOptions_Impl::SetPath( SvtPathOptions::Paths ePath, const OUString& rNewPath ) +{ + ::osl::MutexGuard aGuard( m_aMutex ); + + if ( ePath < SvtPathOptions::PATH_COUNT ) + { + OUString aResult; + OUString aNewValue; + Any a; + + switch ( ePath ) + { + case SvtPathOptions::PATH_ADDIN: + case SvtPathOptions::PATH_FILTER: + case SvtPathOptions::PATH_HELP: + case SvtPathOptions::PATH_MODULE: + case SvtPathOptions::PATH_PLUGIN: + case SvtPathOptions::PATH_STORAGE: + { + // These office paths have to be convert back to UCB-URL's + osl::FileBase::getFileURLFromSystemPath( rNewPath, aResult ); + aNewValue = aResult; + } + break; + + default: + aNewValue = rNewPath; + } + + // Resubstitution is done by the service itself using the substitution service + a <<= aNewValue; + try + { + m_xPathSettings->setFastPropertyValue( m_aMapEnumToPropHandle[ static_cast<sal_Int32>(ePath)], a ); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION("unotools.config", "SetPath"); + } + } +} + +OUString SvtPathOptions_Impl::ExpandMacros( const OUString& rPath ) const +{ + OUString sExpanded( rPath ); + + const INetURLObject aParser( rPath ); + if ( aParser.GetProtocol() == INetProtocol::VndSunStarExpand ) + sExpanded = m_xMacroExpander->expandMacros( aParser.GetURLPath( INetURLObject::DecodeMechanism::WithCharset ) ); + + return sExpanded; +} + +OUString SvtPathOptions_Impl::UsePathVariables( const OUString& rPath ) const +{ + return m_xSubstVariables->reSubstituteVariables( rPath ); +} + +OUString SvtPathOptions_Impl::SubstVar( const OUString& rVar ) const +{ + // Don't work at parameter-string directly. Copy it. + OUString aWorkText = rVar; + + // Convert the returned path to system path! + bool bConvertLocal = false; + + // Search for first occurrence of "$(...". + sal_Int32 nPosition = aWorkText.indexOf( SIGN_STARTVARIABLE ); // = first position of "$(" in string + sal_Int32 nLength = 0; // = count of letters from "$(" to ")" in string + + // Have we found any variable like "$(...)"? + if ( nPosition != STRPOS_NOTFOUND ) + { + // Yes; Get length of found variable. + // If no ")" was found - nLength is set to 0 by default! see before. + sal_Int32 nEndPosition = aWorkText.indexOf( SIGN_ENDVARIABLE, nPosition ); + if ( nEndPosition != STRPOS_NOTFOUND ) + nLength = nEndPosition - nPosition + 1; + } + + // Is there another path variable? + while ( ( nPosition != STRPOS_NOTFOUND ) && ( nLength > 0 ) ) + { + // YES; Get the next variable for replace. + OUString aSubString = aWorkText.copy( nPosition, nLength ); + aSubString = aSubString.toAsciiLowerCase(); + + // Look for special variable that needs a system path. + VarNameSet::const_iterator pIter = m_aSystemPathVarNames.find( aSubString ); + if ( pIter != m_aSystemPathVarNames.end() ) + bConvertLocal = true; + + nPosition += nLength; + + // We must control index in string before call something at OUString! + // The OUString-implementation don't do it for us :-( but the result is not defined otherwise. + if ( nPosition + 1 > aWorkText.getLength() ) + { + // Position is out of range. Break loop! + nPosition = STRPOS_NOTFOUND; + nLength = 0; + } + else + { + // Else; Position is valid. Search for next variable. + nPosition = aWorkText.indexOf( SIGN_STARTVARIABLE, nPosition ); + // Have we found any variable like "$(...)"? + if ( nPosition != STRPOS_NOTFOUND ) + { + // Yes; Get length of found variable. If no ")" was found - nLength must set to 0! + nLength = 0; + sal_Int32 nEndPosition = aWorkText.indexOf( SIGN_ENDVARIABLE, nPosition ); + if ( nEndPosition != STRPOS_NOTFOUND ) + nLength = nEndPosition - nPosition + 1; + } + } + } + + aWorkText = m_xSubstVariables->substituteVariables( rVar, false ); + + if ( bConvertLocal ) + { + // Convert the URL to a system path for special path variables + OUString aReturn; + osl::FileBase::getSystemPathFromFileURL( aWorkText, aReturn ); + return aReturn; + } + + return aWorkText; +} + +SvtPathOptions_Impl::SvtPathOptions_Impl() : + m_aPathArray( sal_Int32(SvtPathOptions::PATH_COUNT) ) +{ + Reference< XComponentContext > xContext = comphelper::getProcessComponentContext(); + + // Create necessary services + Reference< XPathSettings > xPathSettings = thePathSettings::get(xContext); + m_xPathSettings.set( xPathSettings, UNO_QUERY_THROW ); + m_xSubstVariables.set( PathSubstitution::create(xContext) ); + m_xMacroExpander = theMacroExpander::get(xContext); + + // Create temporary hash map to have a mapping between property names and property handles + Reference< XPropertySetInfo > xPropSetInfo = xPathSettings->getPropertySetInfo(); + const Sequence< Property > aPathPropSeq = xPropSetInfo->getProperties(); + + NameToHandleMap aTempHashMap; + for ( const css::beans::Property& aProperty : aPathPropSeq ) + { + aTempHashMap.emplace(aProperty.Name, aProperty.Handle); + } + + // Create mapping between internal enum (SvtPathOptions::Paths) and property handle + for ( auto const & p : aPropNames ) + { + NameToHandleMap::const_iterator pIter = + aTempHashMap.find( OUString::createFromAscii( p.pPropName )); + + if ( pIter != aTempHashMap.end() ) + { + sal_Int32 nHandle = pIter->second; + sal_Int32 nEnum = p.ePath; + m_aMapEnumToPropHandle.emplace( nEnum, nHandle ); + } + } + + // Create hash map for path variables that need a system path as a return value! + for ( auto const & i : aVarNameAttribute ) + { + m_aSystemPathVarNames.insert( OUString::createFromAscii( i.pVarName ) ); + } +} + +// class SvtPathOptions -------------------------------------------------- + +namespace { struct lclMutex : public rtl::Static< ::osl::Mutex, lclMutex > {}; } + +SvtPathOptions::SvtPathOptions() +{ + // Global access, must be guarded (multithreading) + ::osl::MutexGuard aGuard( lclMutex::get() ); + pImpl = g_pOptions.lock(); + if ( !pImpl ) + { + pImpl = std::make_shared<SvtPathOptions_Impl>(); + g_pOptions = pImpl; + ItemHolder1::holdConfigItem(EItem::PathOptions); + } +} + +SvtPathOptions::~SvtPathOptions() +{ + // Global access, must be guarded (multithreading) + ::osl::MutexGuard aGuard( lclMutex::get() ); + + pImpl.reset(); +} + +const OUString& SvtPathOptions::GetAddinPath() const +{ + return pImpl->GetAddinPath(); +} + +const OUString& SvtPathOptions::GetAutoCorrectPath() const +{ + return pImpl->GetAutoCorrectPath(); +} + +const OUString& SvtPathOptions::GetAutoTextPath() const +{ + return pImpl->GetAutoTextPath(); +} + +const OUString& SvtPathOptions::GetBackupPath() const +{ + return pImpl->GetBackupPath(); +} + +const OUString& SvtPathOptions::GetBasicPath() const +{ + return pImpl->GetBasicPath(); +} + +const OUString& SvtPathOptions::GetBitmapPath() const +{ + return pImpl->GetBitmapPath(); +} + +const OUString& SvtPathOptions::GetConfigPath() const +{ + return pImpl->GetConfigPath(); +} + +const OUString& SvtPathOptions::GetDictionaryPath() const +{ + return pImpl->GetDictionaryPath(); +} + +const OUString& SvtPathOptions::GetFavoritesPath() const +{ + return pImpl->GetFavoritesPath(); +} + +const OUString& SvtPathOptions::GetFilterPath() const +{ + return pImpl->GetFilterPath(); +} + +const OUString& SvtPathOptions::GetGalleryPath() const +{ + return pImpl->GetGalleryPath(); +} + +const OUString& SvtPathOptions::GetGraphicPath() const +{ + return pImpl->GetGraphicPath(); +} + +const OUString& SvtPathOptions::GetHelpPath() const +{ + return pImpl->GetHelpPath(); +} + +const OUString& SvtPathOptions::GetLinguisticPath() const +{ + return pImpl->GetLinguisticPath(); +} + +const OUString& SvtPathOptions::GetFingerprintPath() const +{ + return pImpl->GetFingerprintPath(); +} + +const OUString& SvtPathOptions::GetNumbertextPath() const +{ + return pImpl->GetNumbertextPath(); +} + +const OUString& SvtPathOptions::GetModulePath() const +{ + return pImpl->GetModulePath(); +} + +const OUString& SvtPathOptions::GetPalettePath() const +{ + return pImpl->GetPalettePath(); +} + +const OUString& SvtPathOptions::GetIconsetPath() const +{ + return pImpl->GetIconsetPath(); +} + +const OUString& SvtPathOptions::GetPluginPath() const +{ + return pImpl->GetPluginPath(); +} + +const OUString& SvtPathOptions::GetStoragePath() const +{ + return pImpl->GetStoragePath(); +} + +const OUString& SvtPathOptions::GetTempPath() const +{ + return pImpl->GetTempPath(); +} + +const OUString& SvtPathOptions::GetTemplatePath() const +{ + return pImpl->GetTemplatePath(); +} + +const OUString& SvtPathOptions::GetUserConfigPath() const +{ + return pImpl->GetUserConfigPath(); +} + +const OUString& SvtPathOptions::GetWorkPath() const +{ + return pImpl->GetWorkPath(); +} + +const OUString& SvtPathOptions::GetClassificationPath() const +{ + return pImpl->GetClassificationPath(); +} + +void SvtPathOptions::SetAddinPath( const OUString& rPath ) +{ + pImpl->SetAddinPath( rPath ); +} + +void SvtPathOptions::SetAutoCorrectPath( const OUString& rPath ) +{ + pImpl->SetAutoCorrectPath( rPath ); +} + +void SvtPathOptions::SetAutoTextPath( const OUString& rPath ) +{ + pImpl->SetAutoTextPath( rPath ); +} + +void SvtPathOptions::SetBackupPath( const OUString& rPath ) +{ + pImpl->SetBackupPath( rPath ); +} + +void SvtPathOptions::SetBasicPath( const OUString& rPath ) +{ + pImpl->SetBasicPath( rPath ); +} + +void SvtPathOptions::SetBitmapPath( const OUString& rPath ) +{ + pImpl->SetBitmapPath( rPath ); +} + +void SvtPathOptions::SetConfigPath( const OUString& rPath ) +{ + pImpl->SetConfigPath( rPath ); +} + +void SvtPathOptions::SetDictionaryPath( const OUString& rPath ) +{ + pImpl->SetDictionaryPath( rPath ); +} + +void SvtPathOptions::SetFavoritesPath( const OUString& rPath ) +{ + pImpl->SetFavoritesPath( rPath ); +} + +void SvtPathOptions::SetFilterPath( const OUString& rPath ) +{ + pImpl->SetFilterPath( rPath ); +} + +void SvtPathOptions::SetGalleryPath( const OUString& rPath ) +{ + pImpl->SetGalleryPath( rPath ); +} + +void SvtPathOptions::SetGraphicPath( const OUString& rPath ) +{ + pImpl->SetGraphicPath( rPath ); +} + +void SvtPathOptions::SetHelpPath( const OUString& rPath ) +{ + pImpl->SetHelpPath( rPath ); +} + +void SvtPathOptions::SetLinguisticPath( const OUString& rPath ) +{ + pImpl->SetLinguisticPath( rPath ); +} + +void SvtPathOptions::SetModulePath( const OUString& rPath ) +{ + pImpl->SetModulePath( rPath ); +} + +void SvtPathOptions::SetPalettePath( const OUString& rPath ) +{ + pImpl->SetPalettePath( rPath ); +} + +void SvtPathOptions::SetPluginPath( const OUString& rPath ) +{ + pImpl->SetPluginPath( rPath ); +} + +void SvtPathOptions::SetStoragePath( const OUString& rPath ) +{ + pImpl->SetStoragePath( rPath ); +} + +void SvtPathOptions::SetTempPath( const OUString& rPath ) +{ + pImpl->SetTempPath( rPath ); +} + +void SvtPathOptions::SetTemplatePath( const OUString& rPath ) +{ + pImpl->SetTemplatePath( rPath ); +} + +void SvtPathOptions::SetUserConfigPath( const OUString& rPath ) +{ + pImpl->SetUserConfigPath( rPath ); +} + +void SvtPathOptions::SetWorkPath( const OUString& rPath ) +{ + pImpl->SetWorkPath( rPath ); +} + +OUString SvtPathOptions::SubstituteVariable( const OUString& rVar ) const +{ + return pImpl->SubstVar( rVar ); +} + +OUString SvtPathOptions::ExpandMacros( const OUString& rPath ) const +{ + return pImpl->ExpandMacros( rPath ); +} + +OUString SvtPathOptions::UseVariable( const OUString& rPath ) const +{ + return pImpl->UsePathVariables( rPath ); +} + +bool SvtPathOptions::SearchFile( OUString& rIniFile, Paths ePath ) +{ + // check parameter: empty inifile name? + if ( rIniFile.isEmpty() ) + { + SAL_WARN( "unotools.config", "SvtPathOptions::SearchFile(): invalid parameter" ); + return false; + } + + OUString aIniFile = pImpl->SubstVar( rIniFile ); + bool bRet = false; + + switch ( ePath ) + { + case PATH_USERCONFIG: + { + // path is a URL + bRet = true; + INetURLObject aObj( GetUserConfigPath() ); + + sal_Int32 nIniIndex = 0; + do + { + OUString aToken = aIniFile.getToken( 0, '/', nIniIndex ); + aObj.insertName(aToken); + } + while ( nIniIndex >= 0 ); + + if ( !::utl::UCBContentHelper::Exists( aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ) ) ) + { + aObj.SetSmartURL( GetConfigPath() ); + aObj.insertName( aIniFile ); + bRet = ::utl::UCBContentHelper::Exists( aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ) ); + } + + if ( bRet ) + rIniFile = aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ); + + break; + } + + default: + { + OUString aPath; + switch ( ePath ) + { + case PATH_ADDIN: aPath = GetAddinPath(); break; + case PATH_AUTOCORRECT: aPath = GetAutoCorrectPath(); break; + case PATH_AUTOTEXT: aPath = GetAutoTextPath(); break; + case PATH_BACKUP: aPath = GetBackupPath(); break; + case PATH_BASIC: aPath = GetBasicPath(); break; + case PATH_BITMAP: aPath = GetBitmapPath(); break; + case PATH_CONFIG: aPath = GetConfigPath(); break; + case PATH_DICTIONARY: aPath = GetDictionaryPath(); break; + case PATH_FAVORITES: aPath = GetFavoritesPath(); break; + case PATH_FILTER: aPath = GetFilterPath(); break; + case PATH_GALLERY: aPath = GetGalleryPath(); break; + case PATH_GRAPHIC: aPath = GetGraphicPath(); break; + case PATH_HELP: aPath = GetHelpPath(); break; + case PATH_LINGUISTIC: aPath = GetLinguisticPath(); break; + case PATH_MODULE: aPath = GetModulePath(); break; + case PATH_PALETTE: aPath = GetPalettePath(); break; + case PATH_ICONSET: aPath = GetIconsetPath(); break; + case PATH_PLUGIN: aPath = GetPluginPath(); break; + case PATH_STORAGE: aPath = GetStoragePath(); break; + case PATH_TEMP: aPath = GetTempPath(); break; + case PATH_TEMPLATE: aPath = GetTemplatePath(); break; + case PATH_WORK: aPath = GetWorkPath(); break; + case PATH_UICONFIG: aPath = pImpl->GetUIConfigPath(); break; + case PATH_FINGERPRINT: aPath = GetFingerprintPath(); break; + case PATH_NUMBERTEXT: aPath = GetNumbertextPath(); break; + case PATH_CLASSIFICATION: aPath = GetClassificationPath(); break; + // coverity[dead_error_begin] - following conditions exist to avoid compiler warning + case PATH_USERCONFIG: + case PATH_COUNT: + break; + } + + sal_Int32 nPathIndex = 0; + do + { + bool bIsURL = true; + OUString aPathToken = aPath.getToken( 0, SEARCHPATH_DELIMITER, nPathIndex ); + INetURLObject aObj( aPathToken ); + if ( aObj.HasError() ) + { + bIsURL = false; + OUString aURL; + if ( osl::FileBase::getFileURLFromSystemPath( aPathToken, aURL ) + == osl::FileBase::E_None ) + aObj.SetURL( aURL ); + } + if ( aObj.GetProtocol() == INetProtocol::VndSunStarExpand ) + { + Reference< XMacroExpander > xMacroExpander = theMacroExpander::get( ::comphelper::getProcessComponentContext() ); + const OUString sExpandedPath = xMacroExpander->expandMacros( aObj.GetURLPath( INetURLObject::DecodeMechanism::WithCharset ) ); + aObj.SetURL( sExpandedPath ); + } + + sal_Int32 nIniIndex = 0; + do + { + OUString aToken = aIniFile.getToken( 0, '/', nIniIndex ); + aObj.insertName(aToken); + } + while ( nIniIndex >= 0 ); + + bRet = ::utl::UCBContentHelper::Exists( aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ) ); + + if ( bRet ) + { + if ( !bIsURL ) + { + OUString sTmp; + osl::FileBase::getSystemPathFromFileURL( + aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), sTmp ); + rIniFile = sTmp; + } + else + rIniFile = aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ); + break; + } + } + while ( nPathIndex >= 0 ); + } + } + + return bRet; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/printwarningoptions.cxx b/unotools/source/config/printwarningoptions.cxx new file mode 100644 index 000000000..c2409dab3 --- /dev/null +++ b/unotools/source/config/printwarningoptions.cxx @@ -0,0 +1,319 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unotools/printwarningoptions.hxx> +#include <unotools/configitem.hxx> +#include <tools/debug.hxx> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Sequence.hxx> + +#include "itemholder1.hxx" + +// namespaces + +using namespace ::utl; +using namespace ::osl; +using namespace ::com::sun::star::uno; + +#define ROOTNODE_START "Office.Common/Print" + +#define PROPERTYNAME_PAPERSIZE OUString("Warning/PaperSize") +#define PROPERTYNAME_PAPERORIENTATION OUString("Warning/PaperOrientation") +#define PROPERTYNAME_NOTFOUND OUString("Warning/NotFound") +#define PROPERTYNAME_TRANSPARENCY OUString("Warning/Transparency") +#define PROPERTYNAME_PRINTINGMODIFIESDOCUMENT OUString("PrintingModifiesDocument") + +#define PROPERTYHANDLE_PAPERSIZE 0 +#define PROPERTYHANDLE_PAPERORIENTATION 1 +#define PROPERTYHANDLE_NOTFOUND 2 +#define PROPERTYHANDLE_TRANSPARENCY 3 +#define PROPERTYHDL_PRINTINGMODIFIESDOCUMENT 4 + +#define PROPERTYCOUNT 5 + +class SvtPrintWarningOptions_Impl : public ConfigItem +{ +public: + +// constructor / destructor + + SvtPrintWarningOptions_Impl(); + virtual ~SvtPrintWarningOptions_Impl() override; + +// override methods of baseclass + + virtual void Notify( const css::uno::Sequence< OUString >& aPropertyNames ) override; + +// public interface + + bool IsPaperSize() const { return m_bPaperSize; } + bool IsPaperOrientation() const { return m_bPaperOrientation; } + bool IsTransparency() const { return m_bTransparency; } + bool IsModifyDocumentOnPrintingAllowed() const { return m_bModifyDocumentOnPrintingAllowed; } + + void SetPaperSize( bool bState ) { m_bPaperSize = bState; SetModified(); } + void SetPaperOrientation( bool bState ) { m_bPaperOrientation = bState; SetModified(); } + void SetTransparency( bool bState ) { m_bTransparency = bState; SetModified(); } + void SetModifyDocumentOnPrintingAllowed( bool bState ) { m_bModifyDocumentOnPrintingAllowed = bState; SetModified(); } + +// private methods + +private: + + virtual void ImplCommit() override; + + static Sequence< OUString > impl_GetPropertyNames(); + +// private member + +private: + + bool m_bPaperSize; + bool m_bPaperOrientation; + bool m_bNotFound; + bool m_bTransparency; + bool m_bModifyDocumentOnPrintingAllowed; +}; + +// constructor + +SvtPrintWarningOptions_Impl::SvtPrintWarningOptions_Impl() : + ConfigItem( ROOTNODE_START ), + m_bPaperSize( false ), + m_bPaperOrientation( false ), + m_bNotFound( false ), + m_bTransparency( true ), + m_bModifyDocumentOnPrintingAllowed( true ) +{ + Sequence< OUString > seqNames( impl_GetPropertyNames() ); + Sequence< Any > seqValues( GetProperties( seqNames ) ); + + DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtPrintWarningOptions_Impl::SvtPrintWarningOptions_Impl()\nI miss some values of configuration keys!\n" ); + + // Copy values from list in right order to our internal member. + sal_Int32 nPropertyCount = seqValues.getLength(); + sal_Int32 nProperty = 0; + + for( nProperty=0; nProperty<nPropertyCount; ++nProperty ) + { + DBG_ASSERT( seqValues[nProperty].hasValue(), "SvtPrintWarningOptions_Impl::SvtPrintWarningOptions_Impl()\nInvalid property value for property detected!\n" ); + + switch( nProperty ) + { + case PROPERTYHANDLE_PAPERSIZE: + { + DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "Invalid type" ); + seqValues[nProperty] >>= m_bPaperSize; + } + break; + + case PROPERTYHANDLE_PAPERORIENTATION: + { + DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "Invalid type" ); + seqValues[nProperty] >>= m_bPaperOrientation; + } + break; + + case PROPERTYHANDLE_NOTFOUND: + { + DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "Invalid type" ); + seqValues[nProperty] >>= m_bNotFound; + } + break; + + case PROPERTYHANDLE_TRANSPARENCY: + { + DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "Invalid type" ); + seqValues[nProperty] >>= m_bTransparency; + } + break; + case PROPERTYHDL_PRINTINGMODIFIESDOCUMENT: + { + DBG_ASSERT(!(seqValues[nProperty].getValueTypeClass()!=TypeClass_BOOLEAN), "Invalid type" ); + seqValues[nProperty] >>= m_bModifyDocumentOnPrintingAllowed; + } + break; + + } + } +} + +// destructor + +SvtPrintWarningOptions_Impl::~SvtPrintWarningOptions_Impl() +{ + assert(!IsModified()); // should have been committed +} + +// Commit + +void SvtPrintWarningOptions_Impl::ImplCommit() +{ + Sequence< OUString > aSeqNames( impl_GetPropertyNames() ); + Sequence< Any > aSeqValues( aSeqNames.getLength() ); + + for( sal_Int32 nProperty = 0, nCount = aSeqNames.getLength(); nProperty < nCount; ++nProperty ) + { + switch( nProperty ) + { + case PROPERTYHANDLE_PAPERSIZE: + aSeqValues[nProperty] <<= m_bPaperSize; + break; + + case PROPERTYHANDLE_PAPERORIENTATION: + aSeqValues[nProperty] <<= m_bPaperOrientation; + break; + + case PROPERTYHANDLE_NOTFOUND: + aSeqValues[nProperty] <<= m_bNotFound; + break; + + case PROPERTYHANDLE_TRANSPARENCY: + aSeqValues[nProperty] <<= m_bTransparency; + break; + case PROPERTYHDL_PRINTINGMODIFIESDOCUMENT: + aSeqValues[nProperty] <<= m_bModifyDocumentOnPrintingAllowed; + break; + } + } + + PutProperties( aSeqNames, aSeqValues ); +} + +void SvtPrintWarningOptions_Impl::Notify( const Sequence< OUString >& ) +{ +} + +// private method + +Sequence< OUString > SvtPrintWarningOptions_Impl::impl_GetPropertyNames() +{ + // Build list of configuration key names. + const OUString pProperties[] = + { + PROPERTYNAME_PAPERSIZE, + PROPERTYNAME_PAPERORIENTATION, + PROPERTYNAME_NOTFOUND, + PROPERTYNAME_TRANSPARENCY, + PROPERTYNAME_PRINTINGMODIFIESDOCUMENT + }; + + // Initialize return sequence with these list ... + const Sequence< OUString > seqPropertyNames( pProperties, PROPERTYCOUNT ); + + return seqPropertyNames; +} + +static std::weak_ptr<SvtPrintWarningOptions_Impl> g_pPrintWarningOptions; + +SvtPrintWarningOptions::SvtPrintWarningOptions() +{ + // Global access, must be guarded (multithreading!). + MutexGuard aGuard( GetOwnStaticMutex() ); + + m_pImpl = g_pPrintWarningOptions.lock(); + if( !m_pImpl ) + { + m_pImpl = std::make_shared<SvtPrintWarningOptions_Impl>(); + g_pPrintWarningOptions = m_pImpl; + ItemHolder1::holdConfigItem(EItem::PrintWarningOptions); + } +} + +SvtPrintWarningOptions::~SvtPrintWarningOptions() +{ + // Global access, must be guarded (multithreading!) + MutexGuard aGuard( GetOwnStaticMutex() ); + + m_pImpl.reset(); +} + +// public method + +bool SvtPrintWarningOptions::IsPaperSize() const +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + return m_pImpl->IsPaperSize(); +} + +// public method + +bool SvtPrintWarningOptions::IsPaperOrientation() const +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + return m_pImpl->IsPaperOrientation(); +} + +// public method + +bool SvtPrintWarningOptions::IsTransparency() const +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + return m_pImpl->IsTransparency(); +} + +// public method + +void SvtPrintWarningOptions::SetPaperSize( bool bState ) +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + m_pImpl->SetPaperSize( bState ); +} + +// public method + +void SvtPrintWarningOptions::SetPaperOrientation( bool bState ) +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + m_pImpl->SetPaperOrientation( bState ); +} + +// public method + +void SvtPrintWarningOptions::SetTransparency( bool bState ) +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + m_pImpl->SetTransparency( bState ); +} + +bool SvtPrintWarningOptions::IsModifyDocumentOnPrintingAllowed() const +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + return m_pImpl->IsModifyDocumentOnPrintingAllowed(); +} + +void SvtPrintWarningOptions::SetModifyDocumentOnPrintingAllowed( bool bState ) +{ + MutexGuard aGuard( GetOwnStaticMutex() ); + m_pImpl->SetModifyDocumentOnPrintingAllowed( bState ); +} + +namespace +{ + class thePrintWarningOptionsMutex : public rtl::Static<osl::Mutex, thePrintWarningOptionsMutex>{}; +} + +// private method + +Mutex& SvtPrintWarningOptions::GetOwnStaticMutex() +{ + return thePrintWarningOptionsMutex::get(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/saveopt.cxx b/unotools/source/config/saveopt.cxx new file mode 100644 index 000000000..d8f4d37dd --- /dev/null +++ b/unotools/source/config/saveopt.cxx @@ -0,0 +1,971 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <sal/log.hxx> +#include <unotools/saveopt.hxx> +#include <rtl/instance.hxx> +#include <unotools/configmgr.hxx> +#include <unotools/configitem.hxx> +#include <tools/debug.hxx> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Sequence.hxx> + +#include <osl/diagnose.h> +#include <osl/mutex.hxx> +#include <officecfg/Office/Recovery.hxx> + +using namespace utl; +using namespace com::sun::star::uno; + +namespace { + +class SvtSaveOptions_Impl; +class SvtLoadOptions_Impl; + +} + +#define CFG_READONLY_DEFAULT false + +struct SvtLoadSaveOptions_Impl +{ + std::unique_ptr<SvtSaveOptions_Impl> pSaveOpt; + std::unique_ptr<SvtLoadOptions_Impl> pLoadOpt; +}; + +static std::unique_ptr<SvtLoadSaveOptions_Impl> pOptions; +static sal_Int32 nRefCount = 0; + +namespace { + +class SvtSaveOptions_Impl : public utl::ConfigItem +{ + sal_Int32 nAutoSaveTime; + bool bUseUserData, + bBackup, + bAutoSave, + bAutoSavePrompt, + bUserAutoSave, + bDocInfSave, + bSaveWorkingSet, + bSaveDocView, + bSaveRelINet, + bSaveRelFSys, + bDoPrettyPrinting, + bWarnAlienFormat, + bLoadDocPrinter; + + SvtSaveOptions::ODFDefaultVersion eODFDefaultVersion; + + bool bROAutoSaveTime, + bROUseUserData, + bROBackup, + bROAutoSave, + bROAutoSavePrompt, + bROUserAutoSave, + bRODocInfSave, + bROSaveWorkingSet, + bROSaveDocView, + bROSaveRelINet, + bROSaveRelFSys, + bROWarnAlienFormat, + bRODoPrettyPrinting, + bROLoadDocPrinter, + bROODFDefaultVersion; + + virtual void ImplCommit() override; + +public: + SvtSaveOptions_Impl(); + + virtual void Notify( const css::uno::Sequence< OUString >& aPropertyNames ) override; + + sal_Int32 GetAutoSaveTime() const { return nAutoSaveTime; } + bool IsUseUserData() const { return bUseUserData; } + bool IsBackup() const { return bBackup; } + bool IsAutoSave() const { return bAutoSave; } + bool IsAutoSavePrompt() const { return bAutoSavePrompt; } + bool IsUserAutoSave() const { return bUserAutoSave; } + bool IsDocInfoSave() const { return bDocInfSave; } + bool IsSaveWorkingSet() const { return bSaveWorkingSet; } + bool IsSaveDocView() const { return bSaveDocView; } + bool IsSaveRelINet() const { return bSaveRelINet; } + bool IsSaveRelFSys() const { return bSaveRelFSys; } + bool IsPrettyPrintingEnabled( ) const { return bDoPrettyPrinting; } + bool IsWarnAlienFormat() const { return bWarnAlienFormat; } + bool IsLoadDocPrinter() const { return bLoadDocPrinter; } + + SvtSaveOptions::ODFDefaultVersion + GetODFDefaultVersion() const { return eODFDefaultVersion; } + + void SetAutoSaveTime( sal_Int32 n ); + void SetUseUserData( bool b ); + void SetBackup( bool b ); + void SetAutoSave( bool b ); + void SetAutoSavePrompt( bool b ); + void SetUserAutoSave( bool b ); + void SetDocInfoSave( bool b ); + void SetSaveWorkingSet( bool b ); + void SetSaveDocView( bool b ); + void SetSaveRelINet( bool b ); + void SetSaveRelFSys( bool b ); + void EnablePrettyPrinting( bool _bDoPP ); + void SetWarnAlienFormat( bool _bDoPP ); + void SetLoadDocPrinter( bool bNew ); + void SetODFDefaultVersion( SvtSaveOptions::ODFDefaultVersion eNew ); + + bool IsReadOnly( SvtSaveOptions::EOption eOption ) const; +}; + +} + +void SvtSaveOptions_Impl::SetAutoSaveTime( sal_Int32 n ) +{ + if (!bROAutoSaveTime && nAutoSaveTime!=n) + { + nAutoSaveTime = n; + SetModified(); + Commit(); + } +} + +void SvtSaveOptions_Impl::SetUseUserData( bool b ) +{ + if (!bROUseUserData && bUseUserData!=b) + { + bUseUserData = b; + SetModified(); + } +} + +void SvtSaveOptions_Impl::SetBackup( bool b ) +{ + if (!bROBackup && bBackup!=b) + { + bBackup = b; + SetModified(); + } +} + +void SvtSaveOptions_Impl::SetAutoSave( bool b ) +{ + if (!bROAutoSave && bAutoSave!=b) + { + bAutoSave = b; + SetModified(); + Commit(); + } +} + +void SvtSaveOptions_Impl::SetAutoSavePrompt( bool b ) +{ + if (!bROAutoSavePrompt && bAutoSavePrompt!=b) + { + bAutoSavePrompt = b; + SetModified(); + } +} + +void SvtSaveOptions_Impl::SetUserAutoSave( bool b ) +{ + if (!bROUserAutoSave && bUserAutoSave!=b) + { + bUserAutoSave = b; + SetModified(); + Commit(); + } +} + +void SvtSaveOptions_Impl::SetDocInfoSave(bool b) +{ + if (!bRODocInfSave && bDocInfSave!=b) + { + bDocInfSave = b; + SetModified(); + } +} + +void SvtSaveOptions_Impl::SetSaveWorkingSet( bool b ) +{ + if (!bROSaveWorkingSet && bSaveWorkingSet!=b) + { + bSaveWorkingSet = b; + SetModified(); + } +} + +void SvtSaveOptions_Impl::SetSaveDocView( bool b ) +{ + if (!bROSaveDocView && bSaveDocView!=b) + { + bSaveDocView = b; + SetModified(); + } +} + +void SvtSaveOptions_Impl::SetSaveRelINet( bool b ) +{ + if (!bROSaveRelINet && bSaveRelINet!=b) + { + bSaveRelINet = b; + SetModified(); + } +} + +void SvtSaveOptions_Impl::SetSaveRelFSys( bool b ) +{ + if (!bROSaveRelFSys && bSaveRelFSys!=b) + { + bSaveRelFSys = b; + SetModified(); + } +} + +void SvtSaveOptions_Impl::EnablePrettyPrinting( bool _bDoPP ) +{ + if (!bRODoPrettyPrinting && bDoPrettyPrinting!=_bDoPP) + { + bDoPrettyPrinting = _bDoPP; + SetModified(); + } +} + +void SvtSaveOptions_Impl::SetWarnAlienFormat( bool _bDoPP ) +{ + if (!bROWarnAlienFormat && bWarnAlienFormat!=_bDoPP) + { + bWarnAlienFormat = _bDoPP; + SetModified(); + } +} + +void SvtSaveOptions_Impl::SetLoadDocPrinter( bool bNew ) +{ + if ( !bROLoadDocPrinter && bLoadDocPrinter != bNew ) + { + bLoadDocPrinter = bNew; + SetModified(); + } +} + +void SvtSaveOptions_Impl::SetODFDefaultVersion( SvtSaveOptions::ODFDefaultVersion eNew ) +{ + if ( !bROODFDefaultVersion && eODFDefaultVersion != eNew ) + { + eODFDefaultVersion = eNew; + SetModified(); + } +} + +bool SvtSaveOptions_Impl::IsReadOnly( SvtSaveOptions::EOption eOption ) const +{ + bool bReadOnly = CFG_READONLY_DEFAULT; + switch(eOption) + { + case SvtSaveOptions::EOption::AutoSaveTime : + bReadOnly = bROAutoSaveTime; + break; + case SvtSaveOptions::EOption::UseUserData : + bReadOnly = bROUseUserData; + break; + case SvtSaveOptions::EOption::Backup : + bReadOnly = bROBackup; + break; + case SvtSaveOptions::EOption::AutoSave : + bReadOnly = bROAutoSave; + break; + case SvtSaveOptions::EOption::AutoSavePrompt : + bReadOnly = bROAutoSavePrompt; + break; + case SvtSaveOptions::EOption::UserAutoSave : + bReadOnly = bROUserAutoSave; + break; + case SvtSaveOptions::EOption::DocInfSave : + bReadOnly = bRODocInfSave; + break; + case SvtSaveOptions::EOption::SaveWorkingSet : + bReadOnly = bROSaveWorkingSet; + break; + case SvtSaveOptions::EOption::SaveDocView : + bReadOnly = bROSaveDocView; + break; + case SvtSaveOptions::EOption::SaveRelInet : + bReadOnly = bROSaveRelINet; + break; + case SvtSaveOptions::EOption::SaveRelFsys : + bReadOnly = bROSaveRelFSys; + break; + case SvtSaveOptions::EOption::DoPrettyPrinting : + bReadOnly = bRODoPrettyPrinting; + break; + case SvtSaveOptions::EOption::WarnAlienFormat : + bReadOnly = bROWarnAlienFormat; + break; + case SvtSaveOptions::EOption::LoadDocPrinter : + bReadOnly = bROLoadDocPrinter; + break; + case SvtSaveOptions::EOption::OdfDefaultVersion : + bReadOnly = bROODFDefaultVersion; + break; + } + return bReadOnly; +} + +#define FORMAT 0 +#define TIMEINTERVALL 1 +#define USEUSERDATA 2 +#define CREATEBACKUP 3 +#define AUTOSAVE 4 +#define PROMPT 5 +#define EDITPROPERTY 6 +#define SAVEVIEWINFO 7 +#define PRETTYPRINTING 8 +#define WARNALIENFORMAT 9 +#define LOADDOCPRINTER 10 +#define FILESYSTEM 11 +#define INTERNET 12 +#define SAVEWORKINGSET 13 +#define ODFDEFAULTVERSION 14 + +static Sequence< OUString > GetPropertyNames() +{ + static const char* aPropNames[] = + { + "Graphic/Format", + "Document/AutoSaveTimeIntervall", + "Document/UseUserData", + "Document/CreateBackup", + "Document/AutoSave", + "Document/AutoSavePrompt", + "Document/EditProperty", + "Document/ViewInfo", + "Document/PrettyPrinting", + "Document/WarnAlienFormat", + "Document/LoadPrinter", + "URL/FileSystem", + "URL/Internet", + "WorkingSet", + "ODF/DefaultVersion" + }; + + const int nCount = SAL_N_ELEMENTS( aPropNames ); + Sequence< OUString > aNames( nCount ); + OUString* pNames = aNames.getArray(); + for ( int i = 0; i < nCount; i++ ) + pNames[i] = OUString::createFromAscii( aPropNames[i] ); + + return aNames; +} + +SvtSaveOptions_Impl::SvtSaveOptions_Impl() + : ConfigItem( "Office.Common/Save" ) + , nAutoSaveTime( 0 ) + , bUseUserData( false ) + , bBackup( false ) + , bAutoSave( false ) + , bAutoSavePrompt( false ) + , bUserAutoSave( false ) + , bDocInfSave( false ) + , bSaveWorkingSet( false ) + , bSaveDocView( false ) + , bSaveRelINet( false ) + , bSaveRelFSys( false ) + , bDoPrettyPrinting( false ) + , bWarnAlienFormat( true ) + , bLoadDocPrinter( true ) + , eODFDefaultVersion( SvtSaveOptions::ODFVER_LATEST ) + , bROAutoSaveTime( CFG_READONLY_DEFAULT ) + , bROUseUserData( CFG_READONLY_DEFAULT ) + , bROBackup( CFG_READONLY_DEFAULT ) + , bROAutoSave( CFG_READONLY_DEFAULT ) + , bROAutoSavePrompt( CFG_READONLY_DEFAULT ) + , bROUserAutoSave( CFG_READONLY_DEFAULT ) + , bRODocInfSave( CFG_READONLY_DEFAULT ) + , bROSaveWorkingSet( CFG_READONLY_DEFAULT ) + , bROSaveDocView( CFG_READONLY_DEFAULT ) + , bROSaveRelINet( CFG_READONLY_DEFAULT ) + , bROSaveRelFSys( CFG_READONLY_DEFAULT ) + , bROWarnAlienFormat( CFG_READONLY_DEFAULT ) + , bRODoPrettyPrinting( CFG_READONLY_DEFAULT ) + , bROLoadDocPrinter( CFG_READONLY_DEFAULT ) + , bROODFDefaultVersion( CFG_READONLY_DEFAULT ) +{ + Sequence< OUString > aNames = GetPropertyNames(); + Sequence< Any > aValues = GetProperties( aNames ); + Sequence< sal_Bool > aROStates = GetReadOnlyStates( aNames ); + EnableNotification( aNames ); + const Any* pValues = aValues.getConstArray(); + const sal_Bool* pROStates = aROStates.getConstArray(); + DBG_ASSERT( aValues.getLength() == aNames.getLength(), "GetProperties failed" ); + DBG_ASSERT( aROStates.getLength() == aNames.getLength(), "GetReadOnlyStates failed" ); + if ( aValues.getLength() == aNames.getLength() && aROStates.getLength() == aNames.getLength() ) + { + for ( int nProp = 0; nProp < aNames.getLength(); nProp++ ) + { + if ( pValues[nProp].hasValue() ) + { + sal_Int32 nTemp = 0; + switch ( nProp ) + { + case FORMAT: + // not supported anymore + break; + + case TIMEINTERVALL : + if ( pValues[nProp] >>= nTemp ) + nAutoSaveTime = nTemp; + else { + OSL_FAIL( "Wrong Type!" ); + }; + bROAutoSaveTime = pROStates[nProp]; + break; + + case ODFDEFAULTVERSION : + { + sal_Int16 nTmp = 0; + if ( pValues[nProp] >>= nTmp ) + { + if( nTmp == 3 ) + eODFDefaultVersion = SvtSaveOptions::ODFVER_LATEST; + else + eODFDefaultVersion = SvtSaveOptions::ODFDefaultVersion( nTmp ); + } + else { + SAL_WARN( "unotools.config", "SvtSaveOptions_Impl::SvtSaveOptions_Impl(): Wrong Type!" ); + }; + bROODFDefaultVersion = pROStates[nProp]; + break; + } + + default: + { + bool bTemp = bool(); + if ( pValues[nProp] >>= bTemp ) + { + switch ( nProp ) + { + case USEUSERDATA : + bUseUserData = bTemp; + bROUseUserData = pROStates[nProp]; + break; + case CREATEBACKUP : + bBackup = bTemp; + bROBackup = pROStates[nProp]; + break; + case AUTOSAVE : + bAutoSave = bTemp; + bROAutoSave = pROStates[nProp]; + break; + case PROMPT : + bAutoSavePrompt = bTemp; + bROAutoSavePrompt = pROStates[nProp]; + break; + case EDITPROPERTY : + bDocInfSave = bTemp; + bRODocInfSave = pROStates[nProp]; + break; + case SAVEWORKINGSET : + bSaveWorkingSet = bTemp; + bROSaveWorkingSet = pROStates[nProp]; + break; + case SAVEVIEWINFO : + bSaveDocView = bTemp; + bROSaveDocView = pROStates[nProp]; + break; + case FILESYSTEM : + bSaveRelFSys = bTemp; + bROSaveRelFSys = pROStates[nProp]; + break; + case INTERNET : + bSaveRelINet = bTemp; + bROSaveRelINet = pROStates[nProp]; + break; + + case PRETTYPRINTING: + bDoPrettyPrinting = bTemp; + bRODoPrettyPrinting = pROStates[nProp]; + break; + + case WARNALIENFORMAT: + bWarnAlienFormat = bTemp; + bROWarnAlienFormat = pROStates[nProp]; + break; + + case LOADDOCPRINTER: + bLoadDocPrinter = bTemp; + bROLoadDocPrinter = pROStates[nProp]; + break; + + default : + SAL_WARN( "unotools.config", "invalid index to load a path" ); + } + } + else + { + OSL_FAIL( "Wrong Type!" ); + } + } + } + } + } + } + + if (!utl::ConfigManager::IsFuzzing()) + { + bAutoSave = officecfg::Office::Recovery::AutoSave::Enabled::get(); + nAutoSaveTime = officecfg::Office::Recovery::AutoSave::TimeIntervall::get(); + bUserAutoSave = officecfg::Office::Recovery::AutoSave::UserAutoSaveEnabled::get(); + } + else + { + bAutoSave = false; + nAutoSaveTime = 0; + bUserAutoSave = false; + } +} + +void SvtSaveOptions_Impl::ImplCommit() +{ + Sequence< OUString > aOrgNames = GetPropertyNames(); + OUString* pOrgNames = aOrgNames.getArray(); + sal_Int32 nOrgCount = aOrgNames.getLength(); + + Sequence< OUString > aNames( nOrgCount ); + Sequence< Any > aValues( nOrgCount ); + OUString* pNames = aNames.getArray(); + Any* pValues = aValues.getArray(); + sal_Int32 nRealCount = 0; + + for (sal_Int32 i=0; i<nOrgCount; ++i) + { + switch (i) + { + case FORMAT: + // not supported anymore + break; + case TIMEINTERVALL : + if (!bROAutoSaveTime) + { + pValues[nRealCount] <<= nAutoSaveTime; + pNames[nRealCount] = pOrgNames[i]; + ++nRealCount; + } + break; + case USEUSERDATA : + if (!bROUseUserData) + { + pValues[nRealCount] <<= bUseUserData; + pNames[nRealCount] = pOrgNames[i]; + ++nRealCount; + } + break; + case CREATEBACKUP : + if (!bROBackup) + { + pValues[nRealCount] <<= bBackup; + pNames[nRealCount] = pOrgNames[i]; + ++nRealCount; + } + break; + case AUTOSAVE : + if (!bROAutoSave) + { + pValues[nRealCount] <<= bAutoSave; + pNames[nRealCount] = pOrgNames[i]; + ++nRealCount; + } + break; + case PROMPT : + if (!bROAutoSavePrompt) + { + pValues[nRealCount] <<= bAutoSavePrompt; + pNames[nRealCount] = pOrgNames[i]; + ++nRealCount; + } + break; + case EDITPROPERTY : + if (!bRODocInfSave) + { + pValues[nRealCount] <<= bDocInfSave; + pNames[nRealCount] = pOrgNames[i]; + ++nRealCount; + } + break; + case SAVEWORKINGSET : + if (!bROSaveWorkingSet) + { + pValues[nRealCount] <<= bSaveWorkingSet; + pNames[nRealCount] = pOrgNames[i]; + ++nRealCount; + } + break; + case SAVEVIEWINFO : + if (!bROSaveDocView) + { + pValues[nRealCount] <<= bSaveDocView; + pNames[nRealCount] = pOrgNames[i]; + ++nRealCount; + } + break; + case FILESYSTEM : + if (!bROSaveRelFSys) + { + pValues[nRealCount] <<= bSaveRelFSys; + pNames[nRealCount] = pOrgNames[i]; + ++nRealCount; + } + break; + case INTERNET : + if (!bROSaveRelINet) + { + pValues[nRealCount] <<= bSaveRelINet; + pNames[nRealCount] = pOrgNames[i]; + ++nRealCount; + } + break; + case PRETTYPRINTING: + if (!bRODoPrettyPrinting) + { + pValues[nRealCount] <<= bDoPrettyPrinting; + pNames[nRealCount] = pOrgNames[i]; + ++nRealCount; + } + break; + case WARNALIENFORMAT: + if (!bROWarnAlienFormat) + { + pValues[nRealCount] <<= bWarnAlienFormat; + pNames[nRealCount] = pOrgNames[i]; + ++nRealCount; + } + break; + case LOADDOCPRINTER: + if (!bROLoadDocPrinter) + { + pValues[nRealCount] <<= bLoadDocPrinter; + pNames[nRealCount] = pOrgNames[i]; + ++nRealCount; + } + break; + case ODFDEFAULTVERSION: + if (!bROODFDefaultVersion) + { + pValues[nRealCount] <<= (eODFDefaultVersion == SvtSaveOptions::ODFVER_LATEST) ? sal_Int16( 3 ) : sal_Int16( eODFDefaultVersion ); + pNames[nRealCount] = pOrgNames[i]; + ++nRealCount; + } + break; + + default: + SAL_WARN( "unotools.config", "invalid index to save a path" ); + } + } + + aNames.realloc(nRealCount); + aValues.realloc(nRealCount); + PutProperties( aNames, aValues ); + + std::shared_ptr< comphelper::ConfigurationChanges > batch( + comphelper::ConfigurationChanges::create()); + officecfg::Office::Recovery::AutoSave::TimeIntervall::set(nAutoSaveTime, batch); + officecfg::Office::Recovery::AutoSave::Enabled::set(bAutoSave, batch); + officecfg::Office::Recovery::AutoSave::UserAutoSaveEnabled::set(bUserAutoSave, batch); + batch->commit(); +} + +void SvtSaveOptions_Impl::Notify( const Sequence<OUString>& ) +{ +} + +namespace { + +class SvtLoadOptions_Impl : public utl::ConfigItem +{ +private: + bool bLoadUserDefinedSettings; + + virtual void ImplCommit() override; + +public: + SvtLoadOptions_Impl(); + + virtual void Notify( const css::uno::Sequence< OUString >& aPropertyNames ) override; + + void SetLoadUserSettings(bool b){bLoadUserDefinedSettings = b; SetModified();} + bool IsLoadUserSettings() const {return bLoadUserDefinedSettings;} +}; + +} + +const char cUserDefinedSettings[] = "UserDefinedSettings"; + +SvtLoadOptions_Impl::SvtLoadOptions_Impl() + : ConfigItem( "Office.Common/Load" ) + , bLoadUserDefinedSettings( false ) +{ + Sequence< OUString > aNames { cUserDefinedSettings }; + Sequence< Any > aValues = GetProperties( aNames ); + EnableNotification( aNames ); + const Any* pValues = aValues.getConstArray(); + DBG_ASSERT( aValues.getLength() == aNames.getLength(), "GetProperties failed" ); + pValues[0] >>= bLoadUserDefinedSettings; +} + +void SvtLoadOptions_Impl::ImplCommit() +{ + PutProperties( + {cUserDefinedSettings}, {css::uno::Any(bLoadUserDefinedSettings)}); +} + +void SvtLoadOptions_Impl::Notify( const Sequence<OUString>& ) +{ + SAL_WARN( "unotools.config", "properties have been changed" ); +} + +namespace +{ + class LocalSingleton : public rtl::Static< osl::Mutex, LocalSingleton > + { + }; +} + +SvtSaveOptions::SvtSaveOptions() +{ + // Global access, must be guarded (multithreading) + ::osl::MutexGuard aGuard( LocalSingleton::get() ); + if ( !pOptions ) + { + pOptions.reset(new SvtLoadSaveOptions_Impl); + pOptions->pSaveOpt.reset(new SvtSaveOptions_Impl); + pOptions->pLoadOpt.reset( new SvtLoadOptions_Impl); + } + ++nRefCount; + pImp = pOptions.get(); +} + +SvtSaveOptions::~SvtSaveOptions() +{ + // Global access, must be guarded (multithreading) + ::osl::MutexGuard aGuard( LocalSingleton::get() ); + if ( !--nRefCount ) + { + if ( pOptions->pSaveOpt->IsModified() ) + pOptions->pSaveOpt->Commit(); + if ( pOptions->pLoadOpt->IsModified() ) + pOptions->pLoadOpt->Commit(); + + pOptions.reset(); + } +} + +void SvtSaveOptions::SetAutoSaveTime( sal_Int32 n ) +{ + pImp->pSaveOpt->SetAutoSaveTime( n ); +} + +sal_Int32 SvtSaveOptions::GetAutoSaveTime() const +{ + return pImp->pSaveOpt->GetAutoSaveTime(); +} + +void SvtSaveOptions::SetUseUserData( bool b ) +{ + pImp->pSaveOpt->SetUseUserData( b ); +} + +bool SvtSaveOptions::IsUseUserData() const +{ + return pImp->pSaveOpt->IsUseUserData(); +} + +void SvtSaveOptions::SetBackup( bool b ) +{ + pImp->pSaveOpt->SetBackup( b ); +} + +bool SvtSaveOptions::IsBackup() const +{ + return pImp->pSaveOpt->IsBackup(); +} + +void SvtSaveOptions::SetAutoSave( bool b ) +{ + pImp->pSaveOpt->SetAutoSave( b ); +} + +bool SvtSaveOptions::IsAutoSave() const +{ + return pImp->pSaveOpt->IsAutoSave(); +} + +void SvtSaveOptions::SetAutoSavePrompt( bool b ) +{ + pImp->pSaveOpt->SetAutoSavePrompt( b ); +} + +bool SvtSaveOptions::IsAutoSavePrompt() const +{ + return pImp->pSaveOpt->IsAutoSavePrompt(); +} + +void SvtSaveOptions::SetUserAutoSave( bool b ) +{ + pImp->pSaveOpt->SetUserAutoSave( b ); +} + +bool SvtSaveOptions::IsUserAutoSave() const +{ + return pImp->pSaveOpt->IsUserAutoSave(); +} + +void SvtSaveOptions::SetDocInfoSave(bool b) +{ + pImp->pSaveOpt->SetDocInfoSave( b ); +} + +bool SvtSaveOptions::IsDocInfoSave() const +{ + return pImp->pSaveOpt->IsDocInfoSave(); +} + +void SvtSaveOptions::SetSaveWorkingSet( bool b ) +{ + pImp->pSaveOpt->SetSaveWorkingSet( b ); +} + +bool SvtSaveOptions::IsSaveWorkingSet() const +{ + return pImp->pSaveOpt->IsSaveWorkingSet(); +} + +void SvtSaveOptions::SetSaveDocView( bool b ) +{ + pImp->pSaveOpt->SetSaveDocView( b ); +} + +bool SvtSaveOptions::IsSaveDocView() const +{ + return pImp->pSaveOpt->IsSaveDocView(); +} + +void SvtSaveOptions::SetSaveRelINet( bool b ) +{ + pImp->pSaveOpt->SetSaveRelINet( b ); +} + +bool SvtSaveOptions::IsSaveRelINet() const +{ + return pImp->pSaveOpt->IsSaveRelINet(); +} + +void SvtSaveOptions::SetSaveRelFSys( bool b ) +{ + pImp->pSaveOpt->SetSaveRelFSys( b ); +} + +bool SvtSaveOptions::IsSaveRelFSys() const +{ + return pImp->pSaveOpt->IsSaveRelFSys(); +} + +void SvtSaveOptions::SetLoadUserSettings(bool b) +{ + pImp->pLoadOpt->SetLoadUserSettings(b); +} + +bool SvtSaveOptions::IsLoadUserSettings() const +{ + return pImp->pLoadOpt->IsLoadUserSettings(); +} + +void SvtSaveOptions::SetPrettyPrinting( bool _bEnable ) +{ + pImp->pSaveOpt->EnablePrettyPrinting( _bEnable ); +} + +bool SvtSaveOptions::IsPrettyPrinting() const +{ + return pImp->pSaveOpt->IsPrettyPrintingEnabled(); +} + +void SvtSaveOptions::SetWarnAlienFormat( bool _bEnable ) +{ + pImp->pSaveOpt->SetWarnAlienFormat( _bEnable ); +} + +bool SvtSaveOptions::IsWarnAlienFormat() const +{ + return pImp->pSaveOpt->IsWarnAlienFormat(); +} + +void SvtSaveOptions::SetLoadDocumentPrinter( bool _bEnable ) +{ + pImp->pSaveOpt->SetLoadDocPrinter( _bEnable ); +} + +bool SvtSaveOptions::IsLoadDocumentPrinter() const +{ + return pImp->pSaveOpt->IsLoadDocPrinter(); +} + +void SvtSaveOptions::SetODFDefaultVersion( SvtSaveOptions::ODFDefaultVersion eVersion ) +{ + pImp->pSaveOpt->SetODFDefaultVersion( eVersion ); +} + +SvtSaveOptions::ODFDefaultVersion SvtSaveOptions::GetODFDefaultVersion() const +{ + auto const nRet = pImp->pSaveOpt->GetODFDefaultVersion(); + SAL_WARN_IF(nRet == ODFVER_UNKNOWN, "unotools.config", "DefaultVersion is ODFVER_UNKNOWN?"); + return (nRet == ODFVER_UNKNOWN) ? ODFVER_LATEST : nRet; +} + +SvtSaveOptions::ODFSaneDefaultVersion SvtSaveOptions::GetODFSaneDefaultVersion() const +{ + switch (pImp->pSaveOpt->GetODFDefaultVersion()) + { + default: + assert(!"map new ODFDefaultVersion to ODFSaneDefaultVersion"); + break; + case ODFVER_UNKNOWN: + case ODFVER_LATEST: + return ODFSVER_LATEST_EXTENDED; + case ODFVER_010: + return ODFSVER_010; + case ODFVER_011: + return ODFSVER_011; + case ODFVER_012: + return ODFSVER_012; + case ODFVER_012_EXT_COMPAT: + return ODFSVER_012_EXT_COMPAT; + case ODFVER_012_EXTENDED: + return ODFSVER_012_EXTENDED; + case ODFVER_013: + return ODFSVER_013; + } + return ODFSVER_LATEST_EXTENDED; +} + +bool SvtSaveOptions::IsReadOnly( SvtSaveOptions::EOption eOption ) const +{ + return pImp->pSaveOpt->IsReadOnly(eOption); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/searchopt.cxx b/unotools/source/config/searchopt.cxx new file mode 100644 index 000000000..23a2ae933 --- /dev/null +++ b/unotools/source/config/searchopt.cxx @@ -0,0 +1,614 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <unotools/searchopt.hxx> +#include <tools/debug.hxx> +#include <unotools/configitem.hxx> +#include <com/sun/star/uno/Sequence.hxx> +#include <com/sun/star/uno/Any.h> +#include <sal/macros.h> +#include <osl/diagnose.h> +#include <i18nutil/transliteration.hxx> + +using namespace utl; +using namespace com::sun::star::uno; + +#define MAX_FLAGS_OFFSET 29 + +class SvtSearchOptions_Impl : public ConfigItem +{ + sal_Int32 nFlags; + bool bModified; + + SvtSearchOptions_Impl(const SvtSearchOptions_Impl&) = delete; + SvtSearchOptions_Impl& operator=(const SvtSearchOptions_Impl&) = delete; + + // ConfigItem + virtual void ImplCommit() override; + +protected: + bool IsModified() const { return bModified; } + using ConfigItem::SetModified; + void SetModified( bool bVal ); + void Load(); + bool Save(); + + static Sequence< OUString > GetPropertyNames(); + +public: + SvtSearchOptions_Impl(); + virtual ~SvtSearchOptions_Impl() override; + + virtual void Notify( const css::uno::Sequence< OUString >& aPropertyNames ) override; + + bool GetFlag( sal_uInt16 nOffset ) const; + void SetFlag( sal_uInt16 nOffset, bool bVal ); + void SetSearchAlgorithm( sal_uInt16 nOffset, bool bVal ); +}; + +SvtSearchOptions_Impl::SvtSearchOptions_Impl() : + ConfigItem( "Office.Common/SearchOptions" ), + nFlags(0x0003FFFF) // set all options values to 'true' + +{ + Load(); + SetModified( false ); +} + +SvtSearchOptions_Impl::~SvtSearchOptions_Impl() +{ + assert(!IsModified()); // should have been committed +} + +void SvtSearchOptions_Impl::ImplCommit() +{ + if (IsModified()) + Save(); +} + +void SvtSearchOptions_Impl::Notify( const Sequence< OUString >& ) +{ +} + +bool SvtSearchOptions_Impl::GetFlag( sal_uInt16 nOffset ) const +{ + DBG_ASSERT( nOffset <= MAX_FLAGS_OFFSET, "offset out of range"); + return ((nFlags >> nOffset) & 0x01) != 0; +} + +void SvtSearchOptions_Impl::SetFlag( sal_uInt16 nOffset, bool bVal ) +{ + DBG_ASSERT( nOffset <= MAX_FLAGS_OFFSET, "offset out of range"); + sal_Int32 nOldFlags = nFlags; + sal_Int32 nMask = (sal_Int32(1)) << nOffset; + if (bVal) + nFlags |= nMask; + else + nFlags &= ~nMask; + if (nFlags != nOldFlags) + SetModified( true ); +} + +void SvtSearchOptions_Impl::SetModified( bool bVal ) +{ + bModified = bVal; + if (bModified) + { + ConfigItem::SetModified(); + } +} + +Sequence< OUString > SvtSearchOptions_Impl::GetPropertyNames() +{ + static const char* aPropNames[ MAX_FLAGS_OFFSET + 1 ] = + { + "IsWholeWordsOnly", // 0 + "IsBackwards", // 1 + "IsUseRegularExpression", // 2 + //"IsCurrentSelectionOnly", // interactively set or not... + "IsSearchForStyles", // 3 + "IsSimilaritySearch", // 4 + "IsUseAsianOptions", // 5 + "IsMatchCase", // 6 + "Japanese/IsMatchFullHalfWidthForms", // 7 + "Japanese/IsMatchHiraganaKatakana", // 8 + "Japanese/IsMatchContractions", // 9 + "Japanese/IsMatchMinusDashCho-on", // 10 + "Japanese/IsMatchRepeatCharMarks", // 11 + "Japanese/IsMatchVariantFormKanji", // 12 + "Japanese/IsMatchOldKanaForms", // 13 + "Japanese/IsMatch_DiZi_DuZu", // 14 + "Japanese/IsMatch_BaVa_HaFa", // 15 + "Japanese/IsMatch_TsiThiChi_DhiZi", // 16 + "Japanese/IsMatch_HyuIyu_ByuVyu", // 17 + "Japanese/IsMatch_SeShe_ZeJe", // 18 + "Japanese/IsMatch_IaIya", // 19 + "Japanese/IsMatch_KiKu", // 20 + "Japanese/IsIgnorePunctuation", // 21 + "Japanese/IsIgnoreWhitespace", // 22 + "Japanese/IsIgnoreProlongedSoundMark", // 23 + "Japanese/IsIgnoreMiddleDot", // 24 + "IsNotes", // 25 + "IsIgnoreDiacritics_CTL", // 26 + "IsIgnoreKashida_CTL", // 27 + "IsSearchFormatted", // 28 + "IsUseWildcard" // 29 + }; + + const int nCount = SAL_N_ELEMENTS( aPropNames ); + Sequence< OUString > aNames( nCount ); + OUString* pNames = aNames.getArray(); + for (sal_Int32 i = 0; i < nCount; ++i) + pNames[i] = OUString::createFromAscii( aPropNames[i] ); + + return aNames; +} + +void SvtSearchOptions_Impl::SetSearchAlgorithm( sal_uInt16 nOffset, bool bVal ) +{ + if (bVal) + { + // Search algorithms are mutually exclusive. + if (nOffset != 2 && GetFlag(2)) + SetFlag( 2, false ); + if (nOffset != 4 && GetFlag(4)) + SetFlag( 4, false ); + if (nOffset != 29 && GetFlag(29)) + SetFlag( 29, false ); + } + SetFlag( nOffset, bVal ); +} + +void SvtSearchOptions_Impl::Load() +{ + bool bSucc = false; + + Sequence< OUString > aNames = GetPropertyNames(); + sal_Int32 nProps = aNames.getLength(); + + const Sequence< Any > aValues = GetProperties( aNames ); + DBG_ASSERT( aValues.getLength() == aNames.getLength(), + "GetProperties failed" ); + //EnableNotification( aNames ); + + if (nProps && aValues.getLength() == nProps) + { + bSucc = true; + + const Any* pValues = aValues.getConstArray(); + for (sal_Int32 i = 0; i < nProps; ++i) + { + const Any &rVal = pValues[i]; + DBG_ASSERT( rVal.hasValue(), "property value missing" ); + if (rVal.hasValue()) + { + bool bVal = bool(); + if (rVal >>= bVal) + { + if (i <= MAX_FLAGS_OFFSET) + { + // use index in sequence as flag index + SetFlag( i, bVal ); + } + else { + OSL_FAIL( "unexpected index" ); + } + } + else + { + OSL_FAIL( "unexpected type" ); + bSucc = false; + } + } + else + { + OSL_FAIL( "value missing" ); + bSucc = false; + } + } + } + DBG_ASSERT( bSucc, "LoadConfig failed" ); +} + +bool SvtSearchOptions_Impl::Save() +{ + bool bSucc = false; + + const Sequence< OUString > aNames = GetPropertyNames(); + sal_Int32 nProps = aNames.getLength(); + + Sequence< Any > aValues( nProps ); + Any *pValue = aValues.getArray(); + + DBG_ASSERT( nProps == MAX_FLAGS_OFFSET + 1, + "unexpected size of index" ); + if (nProps == MAX_FLAGS_OFFSET + 1) + { + for (sal_Int32 i = 0; i < nProps; ++i) + pValue[i] <<= GetFlag(i); + bSucc |= PutProperties( aNames, aValues ); + } + + if (bSucc) + SetModified( false ); + + return bSucc; +} + +SvtSearchOptions::SvtSearchOptions() + : pImpl( new SvtSearchOptions_Impl ) +{ +} + +SvtSearchOptions::~SvtSearchOptions() +{ +} + +void SvtSearchOptions::Commit() +{ + pImpl->Commit(); +} + +TransliterationFlags SvtSearchOptions::GetTransliterationFlags() const +{ + TransliterationFlags nRes = TransliterationFlags::NONE; + + if (!IsMatchCase()) // 'IsMatchCase' means act case sensitive + nRes |= TransliterationFlags::IGNORE_CASE; + if ( IsMatchFullHalfWidthForms()) + nRes |= TransliterationFlags::IGNORE_WIDTH; + if ( IsMatchHiraganaKatakana()) + nRes |= TransliterationFlags::IGNORE_KANA; + if ( IsMatchContractions()) + nRes |= TransliterationFlags::ignoreSize_ja_JP; + if ( IsMatchMinusDashChoon()) + nRes |= TransliterationFlags::ignoreMinusSign_ja_JP; + if ( IsMatchRepeatCharMarks()) + nRes |= TransliterationFlags::ignoreIterationMark_ja_JP; + if ( IsMatchVariantFormKanji()) + nRes |= TransliterationFlags::ignoreTraditionalKanji_ja_JP; + if ( IsMatchOldKanaForms()) + nRes |= TransliterationFlags::ignoreTraditionalKana_ja_JP; + if ( IsMatchDiziDuzu()) + nRes |= TransliterationFlags::ignoreZiZu_ja_JP; + if ( IsMatchBavaHafa()) + nRes |= TransliterationFlags::ignoreBaFa_ja_JP; + if ( IsMatchTsithichiDhizi()) + nRes |= TransliterationFlags::ignoreTiJi_ja_JP; + if ( IsMatchHyuiyuByuvyu()) + nRes |= TransliterationFlags::ignoreHyuByu_ja_JP; + if ( IsMatchSesheZeje()) + nRes |= TransliterationFlags::ignoreSeZe_ja_JP; + if ( IsMatchIaiya()) + nRes |= TransliterationFlags::ignoreIandEfollowedByYa_ja_JP; + if ( IsMatchKiku()) + nRes |= TransliterationFlags::ignoreKiKuFollowedBySa_ja_JP; + if ( IsIgnorePunctuation()) + nRes |= TransliterationFlags::ignoreSeparator_ja_JP; + if ( IsIgnoreWhitespace()) + nRes |= TransliterationFlags::ignoreSpace_ja_JP; + if ( IsIgnoreProlongedSoundMark()) + nRes |= TransliterationFlags::ignoreProlongedSoundMark_ja_JP; + if ( IsIgnoreMiddleDot()) + nRes |= TransliterationFlags::ignoreMiddleDot_ja_JP; + if ( IsIgnoreDiacritics_CTL()) + nRes |= TransliterationFlags::IGNORE_DIACRITICS_CTL; + if ( IsIgnoreKashida_CTL()) + nRes |= TransliterationFlags::IGNORE_KASHIDA_CTL; + return nRes; +} + +bool SvtSearchOptions::IsWholeWordsOnly() const +{ + return pImpl->GetFlag( 0 ); +} + +void SvtSearchOptions::SetWholeWordsOnly( bool bVal ) +{ + pImpl->SetFlag( 0, bVal ); +} + +bool SvtSearchOptions::IsBackwards() const +{ + return pImpl->GetFlag( 1 ); +} + +void SvtSearchOptions::SetBackwards( bool bVal ) +{ + pImpl->SetFlag( 1, bVal ); +} + +bool SvtSearchOptions::IsUseRegularExpression() const +{ + return pImpl->GetFlag( 2 ); +} + +void SvtSearchOptions::SetUseRegularExpression( bool bVal ) +{ + pImpl->SetSearchAlgorithm( 2, bVal ); +} + +void SvtSearchOptions::SetSearchForStyles( bool bVal ) +{ + pImpl->SetFlag( 3, bVal ); +} + +bool SvtSearchOptions::IsSimilaritySearch() const +{ + return pImpl->GetFlag( 4 ); +} + +void SvtSearchOptions::SetSimilaritySearch( bool bVal ) +{ + pImpl->SetSearchAlgorithm( 4, bVal ); +} + +bool SvtSearchOptions::IsUseAsianOptions() const +{ + return pImpl->GetFlag( 5 ); +} + +void SvtSearchOptions::SetUseAsianOptions( bool bVal ) +{ + pImpl->SetFlag( 5, bVal ); +} + +bool SvtSearchOptions::IsMatchCase() const +{ + return pImpl->GetFlag( 6 ); +} + +void SvtSearchOptions::SetMatchCase( bool bVal ) +{ + pImpl->SetFlag( 6, bVal ); +} + +bool SvtSearchOptions::IsMatchFullHalfWidthForms() const +{ + return pImpl->GetFlag( 7 ); +} + +void SvtSearchOptions::SetMatchFullHalfWidthForms( bool bVal ) +{ + pImpl->SetFlag( 7, bVal ); +} + +bool SvtSearchOptions::IsMatchHiraganaKatakana() const +{ + return pImpl->GetFlag( 8 ); +} + +void SvtSearchOptions::SetMatchHiraganaKatakana( bool bVal ) +{ + pImpl->SetFlag( 8, bVal ); +} + +bool SvtSearchOptions::IsMatchContractions() const +{ + return pImpl->GetFlag( 9 ); +} + +void SvtSearchOptions::SetMatchContractions( bool bVal ) +{ + pImpl->SetFlag( 9, bVal ); +} + +bool SvtSearchOptions::IsMatchMinusDashChoon() const +{ + return pImpl->GetFlag( 10 ); +} + +void SvtSearchOptions::SetMatchMinusDashChoon( bool bVal ) +{ + pImpl->SetFlag( 10, bVal ); +} + +bool SvtSearchOptions::IsMatchRepeatCharMarks() const +{ + return pImpl->GetFlag( 11 ); +} + +void SvtSearchOptions::SetMatchRepeatCharMarks( bool bVal ) +{ + pImpl->SetFlag( 11, bVal ); +} + +bool SvtSearchOptions::IsMatchVariantFormKanji() const +{ + return pImpl->GetFlag( 12 ); +} + +void SvtSearchOptions::SetMatchVariantFormKanji( bool bVal ) +{ + pImpl->SetFlag( 12, bVal ); +} + +bool SvtSearchOptions::IsMatchOldKanaForms() const +{ + return pImpl->GetFlag( 13 ); +} + +void SvtSearchOptions::SetMatchOldKanaForms( bool bVal ) +{ + pImpl->SetFlag( 13, bVal ); +} + +bool SvtSearchOptions::IsMatchDiziDuzu() const +{ + return pImpl->GetFlag( 14 ); +} + +void SvtSearchOptions::SetMatchDiziDuzu( bool bVal ) +{ + pImpl->SetFlag( 14, bVal ); +} + +bool SvtSearchOptions::IsMatchBavaHafa() const +{ + return pImpl->GetFlag( 15 ); +} + +void SvtSearchOptions::SetMatchBavaHafa( bool bVal ) +{ + pImpl->SetFlag( 15, bVal ); +} + +bool SvtSearchOptions::IsMatchTsithichiDhizi() const +{ + return pImpl->GetFlag( 16 ); +} + +void SvtSearchOptions::SetMatchTsithichiDhizi( bool bVal ) +{ + pImpl->SetFlag( 16, bVal ); +} + +bool SvtSearchOptions::IsMatchHyuiyuByuvyu() const +{ + return pImpl->GetFlag( 17 ); +} + +void SvtSearchOptions::SetMatchHyuiyuByuvyu( bool bVal ) +{ + pImpl->SetFlag( 17, bVal ); +} + +bool SvtSearchOptions::IsMatchSesheZeje() const +{ + return pImpl->GetFlag( 18 ); +} + +void SvtSearchOptions::SetMatchSesheZeje( bool bVal ) +{ + pImpl->SetFlag( 18, bVal ); +} + +bool SvtSearchOptions::IsMatchIaiya() const +{ + return pImpl->GetFlag( 19 ); +} + +void SvtSearchOptions::SetMatchIaiya( bool bVal ) +{ + pImpl->SetFlag( 19, bVal ); +} + +bool SvtSearchOptions::IsMatchKiku() const +{ + return pImpl->GetFlag( 20 ); +} + +void SvtSearchOptions::SetMatchKiku( bool bVal ) +{ + pImpl->SetFlag( 20, bVal ); +} + +bool SvtSearchOptions::IsIgnorePunctuation() const +{ + return pImpl->GetFlag( 21 ); +} + +void SvtSearchOptions::SetIgnorePunctuation( bool bVal ) +{ + pImpl->SetFlag( 21, bVal ); +} + +bool SvtSearchOptions::IsIgnoreWhitespace() const +{ + return pImpl->GetFlag( 22 ); +} + +void SvtSearchOptions::SetIgnoreWhitespace( bool bVal ) +{ + pImpl->SetFlag( 22, bVal ); +} + +bool SvtSearchOptions::IsIgnoreProlongedSoundMark() const +{ + return pImpl->GetFlag( 23 ); +} + +void SvtSearchOptions::SetIgnoreProlongedSoundMark( bool bVal ) +{ + pImpl->SetFlag( 23, bVal ); +} + +bool SvtSearchOptions::IsIgnoreMiddleDot() const +{ + return pImpl->GetFlag( 24 ); +} + +void SvtSearchOptions::SetIgnoreMiddleDot( bool bVal ) +{ + pImpl->SetFlag( 24, bVal ); +} + +bool SvtSearchOptions::IsNotes() const +{ + return pImpl->GetFlag( 25 ); +} + +void SvtSearchOptions::SetNotes( bool bVal ) +{ + pImpl->SetFlag( 25, bVal ); +} + +bool SvtSearchOptions::IsIgnoreDiacritics_CTL() const +{ + return pImpl->GetFlag( 26 ); +} + +void SvtSearchOptions::SetIgnoreDiacritics_CTL( bool bVal ) +{ + pImpl->SetFlag( 26, bVal ); +} + +bool SvtSearchOptions::IsIgnoreKashida_CTL() const +{ + return pImpl->GetFlag( 27 ); +} + +void SvtSearchOptions::SetIgnoreKashida_CTL( bool bVal ) +{ + pImpl->SetFlag( 27, bVal ); +} + +bool SvtSearchOptions::IsSearchFormatted() const +{ + return pImpl->GetFlag( 28 ); +} + +void SvtSearchOptions::SetSearchFormatted( bool bVal ) +{ + pImpl->SetFlag( 28, bVal ); +} + +bool SvtSearchOptions::IsUseWildcard() const +{ + return pImpl->GetFlag( 29 ); +} + +void SvtSearchOptions::SetUseWildcard( bool bVal ) +{ + pImpl->SetSearchAlgorithm( 29, bVal ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/securityoptions.cxx b/unotools/source/config/securityoptions.cxx new file mode 100644 index 000000000..d81517f25 --- /dev/null +++ b/unotools/source/config/securityoptions.cxx @@ -0,0 +1,1167 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unotools/securityoptions.hxx> +#include <unotools/configmgr.hxx> +#include <unotools/configitem.hxx> +#include <unotools/ucbhelper.hxx> +#include <tools/debug.hxx> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Sequence.hxx> + +#include <com/sun/star/beans/PropertyValue.hpp> +#include <comphelper/sequence.hxx> +#include <tools/urlobj.hxx> + +#include <unotools/pathoptions.hxx> + +#include "itemholder1.hxx" + +// namespaces + +using namespace ::utl; +using namespace ::osl; +using namespace ::com::sun::star::uno; + +#define ROOTNODE_SECURITY "Office.Common/Security/Scripting" +#define DEFAULT_SECUREURL Sequence< OUString >() +#define DEFAULT_TRUSTEDAUTHORS Sequence< SvtSecurityOptions::Certificate >() + +// xmlsec05 deprecated +#define DEFAULT_STAROFFICEBASIC eALWAYS_EXECUTE + +#define PROPERTYNAME_SECUREURL "SecureURL" +#define PROPERTYNAME_DOCWARN_SAVEORSEND "WarnSaveOrSendDoc" +#define PROPERTYNAME_DOCWARN_SIGNING "WarnSignDoc" +#define PROPERTYNAME_DOCWARN_PRINT "WarnPrintDoc" +#define PROPERTYNAME_DOCWARN_CREATEPDF "WarnCreatePDF" +#define PROPERTYNAME_DOCWARN_REMOVEPERSONALINFO "RemovePersonalInfoOnSaving" +#define PROPERTYNAME_DOCWARN_RECOMMENDPASSWORD "RecommendPasswordProtection" +#define PROPERTYNAME_CTRLCLICK_HYPERLINK "HyperlinksWithCtrlClick" +#define PROPERTYNAME_BLOCKUNTRUSTEDREFERERLINKS "BlockUntrustedRefererLinks" +#define PROPERTYNAME_MACRO_SECLEVEL "MacroSecurityLevel" +#define PROPERTYNAME_MACRO_TRUSTEDAUTHORS "TrustedAuthors" +#define PROPERTYNAME_MACRO_DISABLE "DisableMacrosExecution" +#define PROPERTYNAME_TRUSTEDAUTHOR_SUBJECTNAME "SubjectName" +#define PROPERTYNAME_TRUSTEDAUTHOR_SERIALNUMBER "SerialNumber" +#define PROPERTYNAME_TRUSTEDAUTHOR_RAWDATA "RawData" + +// xmlsec05 deprecated +#define PROPERTYNAME_STAROFFICEBASIC "OfficeBasic" +#define PROPERTYNAME_EXECUTEPLUGINS "ExecutePlugins" +#define PROPERTYNAME_WARNINGENABLED "Warning" +#define PROPERTYNAME_CONFIRMATIONENABLED "Confirmation" +// xmlsec05 deprecated + +#define PROPERTYHANDLE_SECUREURL 0 + +// xmlsec05 deprecated +#define PROPERTYHANDLE_STAROFFICEBASIC 1 +#define PROPERTYHANDLE_EXECUTEPLUGINS 2 +#define PROPERTYHANDLE_WARNINGENABLED 3 +#define PROPERTYHANDLE_CONFIRMATIONENABLED 4 +// xmlsec05 deprecated + +#define PROPERTYHANDLE_DOCWARN_SAVEORSEND 5 +#define PROPERTYHANDLE_DOCWARN_SIGNING 6 +#define PROPERTYHANDLE_DOCWARN_PRINT 7 +#define PROPERTYHANDLE_DOCWARN_CREATEPDF 8 +#define PROPERTYHANDLE_DOCWARN_REMOVEPERSONALINFO 9 +#define PROPERTYHANDLE_DOCWARN_RECOMMENDPASSWORD 10 +#define PROPERTYHANDLE_CTRLCLICK_HYPERLINK 11 +#define PROPERTYHANDLE_BLOCKUNTRUSTEDREFERERLINKS 12 +#define PROPERTYHANDLE_MACRO_SECLEVEL 13 +#define PROPERTYHANDLE_MACRO_TRUSTEDAUTHORS 14 +#define PROPERTYHANDLE_MACRO_DISABLE 15 + +#define PROPERTYHANDLE_INVALID -1 + +#define CFG_READONLY_DEFAULT false + +// private declarations! + +class SvtSecurityOptions_Impl : public ConfigItem +{ + + private: + virtual void ImplCommit() override; + + // public methods + + public: + + // constructor / destructor + + SvtSecurityOptions_Impl(); + virtual ~SvtSecurityOptions_Impl() override; + + // override methods of baseclass + + /*-**************************************************************************************************** + @short called for notify of configmanager + @descr This method is called from the ConfigManager before application ends or from the + PropertyChangeListener if the sub tree broadcasts changes. You must update your + internal values. + + @seealso baseclass ConfigItem + + @param "seqPropertyNames" is the list of properties which should be updated. + *//*-*****************************************************************************************************/ + + virtual void Notify( const Sequence< OUString >& seqPropertyNames ) override; + + // public interface + + bool IsReadOnly ( SvtSecurityOptions::EOption eOption ) const; + + const Sequence< OUString >& GetSecureURLs( ) const { return m_seqSecureURLs;} + void SetSecureURLs ( const Sequence< OUString >& seqURLList ); + inline sal_Int32 GetMacroSecurityLevel ( ) const; + void SetMacroSecurityLevel ( sal_Int32 _nLevel ); + + inline bool IsMacroDisabled ( ) const; + + const Sequence< SvtSecurityOptions::Certificate >& GetTrustedAuthors( ) const { return m_seqTrustedAuthors;} + void SetTrustedAuthors ( const Sequence< SvtSecurityOptions::Certificate >& rAuthors ); + + bool IsOptionSet ( SvtSecurityOptions::EOption eOption ) const; + void SetOption ( SvtSecurityOptions::EOption eOption, bool bValue ); + bool IsOptionEnabled ( SvtSecurityOptions::EOption eOption ) const; + + + void SetProperty( sal_Int32 nHandle, const Any& rValue, bool bReadOnly ); + void LoadAuthors(); + static sal_Int32 GetHandle( const OUString& rPropertyName ); + bool GetOption( SvtSecurityOptions::EOption eOption, bool*& rpValue, bool*& rpRO ); + + /*-**************************************************************************************************** + @short return list of key names of our configuration management which represent our module tree + @descr This method returns a static const list of key names. We need it to get needed values from our + configuration management. + @return A list of needed configuration keys is returned. + *//*-*****************************************************************************************************/ + static Sequence< OUString > GetPropertyNames(); + + Sequence< OUString > m_seqSecureURLs; + bool m_bSaveOrSend; + bool m_bSigning; + bool m_bPrint; + bool m_bCreatePDF; + bool m_bRemoveInfo; + bool m_bRecommendPwd; + bool m_bCtrlClickHyperlink; + bool m_bBlockUntrustedRefererLinks; + sal_Int32 m_nSecLevel; + Sequence< SvtSecurityOptions::Certificate > m_seqTrustedAuthors; + bool m_bDisableMacros; + + bool m_bROSecureURLs; + bool m_bROSaveOrSend; + bool m_bROSigning; + bool m_bROPrint; + bool m_bROCreatePDF; + bool m_bRORemoveInfo; + bool m_bRORecommendPwd; + bool m_bROCtrlClickHyperlink; + bool m_bROBlockUntrustedRefererLinks; + bool m_bROSecLevel; + bool m_bROTrustedAuthors; + bool m_bRODisableMacros; + + // xmlsec05 deprecated + EBasicSecurityMode m_eBasicMode; + bool m_bExecutePlugins; + bool m_bWarning; + bool m_bConfirmation; + + bool m_bROConfirmation; + bool m_bROWarning; + bool m_bROExecutePlugins; + bool m_bROBasicMode; + public: + bool IsWarningEnabled() const { return m_bWarning;} + void SetWarningEnabled( bool bSet ); + bool IsConfirmationEnabled() const { return m_bConfirmation;} + void SetConfirmationEnabled( bool bSet ); + bool IsExecutePlugins() const { return m_bExecutePlugins;} + void SetExecutePlugins( bool bSet ); + // xmlsec05 deprecated + EBasicSecurityMode GetBasicMode ( ) const { return m_eBasicMode;} + void SetBasicMode ( EBasicSecurityMode eMode ); +}; + +// constructor + +SvtSecurityOptions_Impl::SvtSecurityOptions_Impl() + :ConfigItem ( ROOTNODE_SECURITY ) + ,m_seqSecureURLs ( DEFAULT_SECUREURL ) + ,m_bSaveOrSend ( true ) + ,m_bSigning ( true ) + ,m_bPrint ( true ) + ,m_bCreatePDF ( true ) + ,m_bRemoveInfo ( true ) + ,m_bRecommendPwd(false) + ,m_bCtrlClickHyperlink(false) + ,m_bBlockUntrustedRefererLinks(false) + ,m_nSecLevel ( 1 ) + ,m_seqTrustedAuthors ( DEFAULT_TRUSTEDAUTHORS ) + ,m_bDisableMacros ( false ) + ,m_bROSecureURLs ( CFG_READONLY_DEFAULT ) + ,m_bROSaveOrSend ( CFG_READONLY_DEFAULT ) + ,m_bROSigning ( CFG_READONLY_DEFAULT ) + ,m_bROPrint ( CFG_READONLY_DEFAULT ) + ,m_bROCreatePDF ( CFG_READONLY_DEFAULT ) + ,m_bRORemoveInfo ( CFG_READONLY_DEFAULT ) + ,m_bRORecommendPwd(CFG_READONLY_DEFAULT) + ,m_bROCtrlClickHyperlink(CFG_READONLY_DEFAULT) + ,m_bROBlockUntrustedRefererLinks(CFG_READONLY_DEFAULT) + ,m_bROSecLevel ( CFG_READONLY_DEFAULT ) + ,m_bROTrustedAuthors ( CFG_READONLY_DEFAULT ) + ,m_bRODisableMacros ( true ) // currently is not intended to be changed + + // xmlsec05 deprecated + , m_eBasicMode ( DEFAULT_STAROFFICEBASIC ) + , m_bExecutePlugins ( true ) + , m_bWarning ( true ) + , m_bConfirmation ( true ) + , m_bROConfirmation ( CFG_READONLY_DEFAULT ) + , m_bROWarning ( CFG_READONLY_DEFAULT ) + , m_bROExecutePlugins ( CFG_READONLY_DEFAULT ) + , m_bROBasicMode ( CFG_READONLY_DEFAULT ) + // xmlsec05 deprecated + +{ + Sequence< OUString > seqNames = GetPropertyNames ( ); + Sequence< Any > seqValues = GetProperties ( seqNames ); + Sequence< sal_Bool > seqRO = GetReadOnlyStates ( seqNames ); + + // Safe impossible cases. + // We need values from ALL configuration keys. + // Follow assignment use order of values in relation to our list of key names! + DBG_ASSERT( !(seqNames.getLength()!=seqValues.getLength()), "SvtSecurityOptions_Impl::SvtSecurityOptions_Impl()\nI miss some values of configuration keys!\n" ); + + // Copy values from list in right order to our internal member. + sal_Int32 nPropertyCount = seqValues.getLength(); + for( sal_Int32 nProperty = 0; nProperty < nPropertyCount; ++nProperty ) + SetProperty( nProperty, seqValues[ nProperty ], seqRO[ nProperty ] ); + + LoadAuthors(); + + // Enable notification mechanism of our baseclass. + // We need it to get information about changes outside these class on our used configuration keys!*/ + + EnableNotification( seqNames ); +} + +// destructor + +SvtSecurityOptions_Impl::~SvtSecurityOptions_Impl() +{ + assert(!IsModified()); // should have been committed +} + +void SvtSecurityOptions_Impl::SetProperty( sal_Int32 nProperty, const Any& rValue, bool bRO ) +{ + switch( nProperty ) + { + case PROPERTYHANDLE_SECUREURL: + { + m_seqSecureURLs.realloc( 0 ); + rValue >>= m_seqSecureURLs; + if (!utl::ConfigManager::IsFuzzing()) + { + SvtPathOptions aOpt; + std::transform(m_seqSecureURLs.begin(), m_seqSecureURLs.end(), m_seqSecureURLs.begin(), + [&aOpt](const OUString& rUrl) -> OUString { return aOpt.SubstituteVariable( rUrl ); }); + } + m_bROSecureURLs = bRO; + } + break; + + case PROPERTYHANDLE_DOCWARN_SAVEORSEND: + { + rValue >>= m_bSaveOrSend; + m_bROSaveOrSend = bRO; + } + break; + + case PROPERTYHANDLE_DOCWARN_SIGNING: + { + rValue >>= m_bSigning; + m_bROSigning = bRO; + } + break; + + case PROPERTYHANDLE_DOCWARN_PRINT: + { + rValue >>= m_bPrint; + m_bROPrint = bRO; + } + break; + + case PROPERTYHANDLE_DOCWARN_CREATEPDF: + { + rValue >>= m_bCreatePDF; + m_bROCreatePDF = bRO; + } + break; + + case PROPERTYHANDLE_DOCWARN_REMOVEPERSONALINFO: + { + rValue >>= m_bRemoveInfo; + m_bRORemoveInfo = bRO; + } + break; + + case PROPERTYHANDLE_DOCWARN_RECOMMENDPASSWORD: + { + rValue >>= m_bRecommendPwd; + m_bRORecommendPwd = bRO; + } + break; + + case PROPERTYHANDLE_CTRLCLICK_HYPERLINK: + { + rValue >>= m_bCtrlClickHyperlink; + m_bROCtrlClickHyperlink = bRO; + } + break; + + case PROPERTYHANDLE_BLOCKUNTRUSTEDREFERERLINKS: + { + rValue >>= m_bBlockUntrustedRefererLinks; + m_bROBlockUntrustedRefererLinks = bRO; + } + break; + + case PROPERTYHANDLE_MACRO_SECLEVEL: + { + rValue >>= m_nSecLevel; + m_bROSecLevel = bRO; + } + break; + + case PROPERTYHANDLE_MACRO_TRUSTEDAUTHORS: + { + // don't care about value here... + m_bROTrustedAuthors = bRO; + } + break; + + case PROPERTYHANDLE_MACRO_DISABLE: + { + rValue >>= m_bDisableMacros; + m_bRODisableMacros = bRO; + } + break; + + // xmlsec05 deprecated + case PROPERTYHANDLE_STAROFFICEBASIC: + { + sal_Int32 nMode = 0; + rValue >>= nMode; + m_eBasicMode = static_cast<EBasicSecurityMode>(nMode); + m_bROBasicMode = bRO; + } + break; + case PROPERTYHANDLE_EXECUTEPLUGINS: + { + rValue >>= m_bExecutePlugins; + m_bROExecutePlugins = bRO; + } + break; + case PROPERTYHANDLE_WARNINGENABLED: + { + rValue >>= m_bWarning; + m_bROWarning = bRO; + } + break; + case PROPERTYHANDLE_CONFIRMATIONENABLED: + { + rValue >>= m_bConfirmation; + m_bROConfirmation = bRO; + } + break; + // xmlsec05 deprecated + +#if OSL_DEBUG_LEVEL > 0 + default: + assert(false && "Unknown property!"); +#endif + } +} + +void SvtSecurityOptions_Impl::LoadAuthors() +{ + m_seqTrustedAuthors.realloc( 0 ); // first clear + const Sequence< OUString > lAuthors = GetNodeNames( PROPERTYNAME_MACRO_TRUSTEDAUTHORS ); + sal_Int32 c1 = lAuthors.getLength(); + if( c1 ) + { + sal_Int32 c2 = c1 * 3; // 3 Properties inside Struct TrustedAuthor + Sequence< OUString > lAllAuthors( c2 ); + + sal_Int32 i2 = 0; + OUString aSep( "/" ); + for( const auto& rAuthor : lAuthors ) + { + lAllAuthors[ i2 ] = PROPERTYNAME_MACRO_TRUSTEDAUTHORS + aSep + rAuthor + aSep + PROPERTYNAME_TRUSTEDAUTHOR_SUBJECTNAME; + ++i2; + lAllAuthors[ i2 ] = PROPERTYNAME_MACRO_TRUSTEDAUTHORS + aSep + rAuthor + aSep + PROPERTYNAME_TRUSTEDAUTHOR_SERIALNUMBER; + ++i2; + lAllAuthors[ i2 ] = PROPERTYNAME_MACRO_TRUSTEDAUTHORS + aSep + rAuthor + aSep + PROPERTYNAME_TRUSTEDAUTHOR_RAWDATA; + ++i2; + } + + Sequence< Any > lValues = GetProperties( lAllAuthors ); + if( lValues.getLength() == c2 ) + { + std::vector< SvtSecurityOptions::Certificate > v; + SvtSecurityOptions::Certificate aCert( 3 ); + i2 = 0; + for( sal_Int32 i1 = 0; i1 < c1; ++i1 ) + { + lValues[ i2 ] >>= aCert[ 0 ]; + ++i2; + lValues[ i2 ] >>= aCert[ 1 ]; + ++i2; + lValues[ i2 ] >>= aCert[ 2 ]; + ++i2; + // Filter out TrustedAuthor entries with empty RawData, which + // would cause an unexpected std::bad_alloc in + // SecurityEnvironment_NssImpl::createCertificateFromAscii and + // have been observed in the wild (fdo#55019): + if( !aCert[ 2 ].isEmpty() ) + { + v.push_back( aCert ); + } + } + m_seqTrustedAuthors = comphelper::containerToSequence(v); + } + } +} + +sal_Int32 SvtSecurityOptions_Impl::GetHandle( const OUString& rName ) +{ + sal_Int32 nHandle; + + if( rName == PROPERTYNAME_SECUREURL ) + nHandle = PROPERTYHANDLE_SECUREURL; + else if( rName == PROPERTYNAME_DOCWARN_SAVEORSEND ) + nHandle = PROPERTYHANDLE_DOCWARN_SAVEORSEND; + else if( rName == PROPERTYNAME_DOCWARN_SIGNING ) + nHandle = PROPERTYHANDLE_DOCWARN_SIGNING; + else if( rName == PROPERTYNAME_DOCWARN_PRINT ) + nHandle = PROPERTYHANDLE_DOCWARN_PRINT; + else if( rName == PROPERTYNAME_DOCWARN_CREATEPDF ) + nHandle = PROPERTYHANDLE_DOCWARN_CREATEPDF; + else if( rName == PROPERTYNAME_DOCWARN_REMOVEPERSONALINFO ) + nHandle = PROPERTYHANDLE_DOCWARN_REMOVEPERSONALINFO; + else if( rName == PROPERTYNAME_DOCWARN_RECOMMENDPASSWORD ) + nHandle = PROPERTYHANDLE_DOCWARN_RECOMMENDPASSWORD; + else if( rName == PROPERTYNAME_CTRLCLICK_HYPERLINK ) + nHandle = PROPERTYHANDLE_CTRLCLICK_HYPERLINK; + else if( rName == PROPERTYNAME_BLOCKUNTRUSTEDREFERERLINKS ) + nHandle = PROPERTYHANDLE_BLOCKUNTRUSTEDREFERERLINKS; + else if( rName == PROPERTYNAME_MACRO_SECLEVEL ) + nHandle = PROPERTYHANDLE_MACRO_SECLEVEL; + else if( rName == PROPERTYNAME_MACRO_TRUSTEDAUTHORS ) + nHandle = PROPERTYHANDLE_MACRO_TRUSTEDAUTHORS; + else if( rName == PROPERTYNAME_MACRO_DISABLE ) + nHandle = PROPERTYHANDLE_MACRO_DISABLE; + + // xmlsec05 deprecated + else if( rName == PROPERTYNAME_STAROFFICEBASIC ) + nHandle = PROPERTYHANDLE_STAROFFICEBASIC; + else if( rName == PROPERTYNAME_EXECUTEPLUGINS ) + nHandle = PROPERTYHANDLE_EXECUTEPLUGINS; + else if( rName == PROPERTYNAME_WARNINGENABLED ) + nHandle = PROPERTYHANDLE_WARNINGENABLED; + else if( rName == PROPERTYNAME_CONFIRMATIONENABLED ) + nHandle = PROPERTYHANDLE_CONFIRMATIONENABLED; + // xmlsec05 deprecated + + else + nHandle = PROPERTYHANDLE_INVALID; + + return nHandle; +} + +bool SvtSecurityOptions_Impl::GetOption( SvtSecurityOptions::EOption eOption, bool*& rpValue, bool*& rpRO ) +{ + switch( eOption ) + { + case SvtSecurityOptions::EOption::DocWarnSaveOrSend: + rpValue = &m_bSaveOrSend; + rpRO = &m_bROSaveOrSend; + break; + case SvtSecurityOptions::EOption::DocWarnSigning: + rpValue = &m_bSigning; + rpRO = &m_bROSigning; + break; + case SvtSecurityOptions::EOption::DocWarnPrint: + rpValue = &m_bPrint; + rpRO = &m_bROPrint; + break; + case SvtSecurityOptions::EOption::DocWarnCreatePdf: + rpValue = &m_bCreatePDF; + rpRO = &m_bROCreatePDF; + break; + case SvtSecurityOptions::EOption::DocWarnRemovePersonalInfo: + rpValue = &m_bRemoveInfo; + rpRO = &m_bRORemoveInfo; + break; + case SvtSecurityOptions::EOption::DocWarnRecommendPassword: + rpValue = &m_bRecommendPwd; + rpRO = &m_bRORecommendPwd; + break; + case SvtSecurityOptions::EOption::CtrlClickHyperlink: + rpValue = &m_bCtrlClickHyperlink; + rpRO = &m_bROCtrlClickHyperlink; + break; + case SvtSecurityOptions::EOption::BlockUntrustedRefererLinks: + rpValue = &m_bBlockUntrustedRefererLinks; + rpRO = &m_bROBlockUntrustedRefererLinks; + break; + default: + rpValue = nullptr; + rpRO = nullptr; + break; + } + + return rpValue != nullptr; +} + +void SvtSecurityOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames ) +{ + // Use given list of updated properties to get his values from configuration directly! + Sequence< Any > seqValues = GetProperties( seqPropertyNames ); + Sequence< sal_Bool > seqRO = GetReadOnlyStates( seqPropertyNames ); + // Safe impossible cases. + // We need values from ALL notified configuration keys. + DBG_ASSERT( !(seqPropertyNames.getLength()!=seqValues.getLength()), "SvtSecurityOptions_Impl::Notify()\nI miss some values of configuration keys!\n" ); + // Step over list of property names and get right value from coreesponding value list to set it on internal members! + sal_Int32 nCount = seqPropertyNames.getLength(); + for( sal_Int32 nProperty = 0; nProperty < nCount; ++nProperty ) + SetProperty( GetHandle( seqPropertyNames[ nProperty ] ), seqValues[ nProperty ], seqRO[ nProperty ] ); + + // read set of trusted authors separately + LoadAuthors(); +} + +void SvtSecurityOptions_Impl::ImplCommit() +{ + // Get names of supported properties, create a list for values and copy current values to it. + Sequence< OUString > lOrgNames = GetPropertyNames(); + sal_Int32 nOrgCount = lOrgNames.getLength(); + + Sequence< OUString > lNames(nOrgCount); + Sequence< Any > lValues(nOrgCount); + sal_Int32 nRealCount = 0; + bool bDone; + + ClearNodeSet( PROPERTYNAME_MACRO_TRUSTEDAUTHORS ); + + for( sal_Int32 nProperty = 0; nProperty < nOrgCount; ++nProperty ) + { + switch( nProperty ) + { + case PROPERTYHANDLE_SECUREURL: + { + bDone = !m_bROSecureURLs; + if( bDone ) + { + Sequence< OUString > lURLs( m_seqSecureURLs ); + SvtPathOptions aOpt; + std::transform(lURLs.begin(), lURLs.end(), lURLs.begin(), + [&aOpt](const OUString& rUrl) -> OUString { return aOpt.UseVariable( rUrl ); }); + lValues[ nRealCount ] <<= lURLs; + } + } + break; + + case PROPERTYHANDLE_DOCWARN_SAVEORSEND: + { + bDone = !m_bROSaveOrSend; + if( bDone ) + lValues[ nRealCount ] <<= m_bSaveOrSend; + } + break; + + case PROPERTYHANDLE_DOCWARN_SIGNING: + { + bDone = !m_bROSigning; + if( bDone ) + lValues[ nRealCount ] <<= m_bSigning; + } + break; + + case PROPERTYHANDLE_DOCWARN_PRINT: + { + bDone = !m_bROPrint; + if( bDone ) + lValues[ nRealCount ] <<= m_bPrint; + } + break; + + case PROPERTYHANDLE_DOCWARN_CREATEPDF: + { + bDone = !m_bROCreatePDF; + if( bDone ) + lValues[ nRealCount ] <<= m_bCreatePDF; + } + break; + + case PROPERTYHANDLE_DOCWARN_REMOVEPERSONALINFO: + { + bDone = !m_bRORemoveInfo; + if( bDone ) + lValues[ nRealCount ] <<= m_bRemoveInfo; + } + break; + + case PROPERTYHANDLE_DOCWARN_RECOMMENDPASSWORD: + { + bDone = !m_bRORecommendPwd; + if( bDone ) + lValues[ nRealCount ] <<= m_bRecommendPwd; + } + break; + + case PROPERTYHANDLE_CTRLCLICK_HYPERLINK: + { + bDone = !m_bROCtrlClickHyperlink; + if( bDone ) + lValues[ nRealCount ] <<= m_bCtrlClickHyperlink; + } + break; + + case PROPERTYHANDLE_BLOCKUNTRUSTEDREFERERLINKS: + { + bDone = !m_bROBlockUntrustedRefererLinks; + if( bDone ) + lValues[ nRealCount ] <<= m_bBlockUntrustedRefererLinks; + } + break; + + case PROPERTYHANDLE_MACRO_SECLEVEL: + { + bDone = !m_bROSecLevel; + if( bDone ) + lValues[ nRealCount ] <<= m_nSecLevel; + } + break; + + case PROPERTYHANDLE_MACRO_TRUSTEDAUTHORS: + { + bDone = !m_bROTrustedAuthors; + if( bDone ) + { + sal_Int32 nCnt = m_seqTrustedAuthors.getLength(); + if( nCnt ) + { + for( sal_Int32 i = 0; i < nCnt; ++i ) + { + OUString aPrefix( + PROPERTYNAME_MACRO_TRUSTEDAUTHORS "/a" + + OUString::number(i) + "/"); + Sequence< css::beans::PropertyValue > lPropertyValues( 3 ); + lPropertyValues[ 0 ].Name = aPrefix + PROPERTYNAME_TRUSTEDAUTHOR_SUBJECTNAME; + lPropertyValues[ 0 ].Value <<= m_seqTrustedAuthors[ i ][0]; + lPropertyValues[ 1 ].Name = aPrefix + PROPERTYNAME_TRUSTEDAUTHOR_SERIALNUMBER; + lPropertyValues[ 1 ].Value <<= m_seqTrustedAuthors[ i ][1]; + lPropertyValues[ 2 ].Name = aPrefix + PROPERTYNAME_TRUSTEDAUTHOR_RAWDATA; + lPropertyValues[ 2 ].Value <<= m_seqTrustedAuthors[ i ][2]; + + SetSetProperties( PROPERTYNAME_MACRO_TRUSTEDAUTHORS, lPropertyValues ); + } + + bDone = false; // because we save in loop above! + } + else + bDone = false; + } + } + break; + + case PROPERTYHANDLE_MACRO_DISABLE: + { + bDone = !m_bRODisableMacros; + if( bDone ) + lValues[ nRealCount ] <<= m_bDisableMacros; + } + break; + + // xmlsec05 deprecated + case PROPERTYHANDLE_STAROFFICEBASIC: + { + bDone = !m_bROBasicMode; + if( bDone ) + lValues[ nRealCount ] <<= static_cast<sal_Int32>(m_eBasicMode); + } + break; + case PROPERTYHANDLE_EXECUTEPLUGINS: + { + bDone = !m_bROExecutePlugins; + if( bDone ) + lValues[ nRealCount ] <<= m_bExecutePlugins; + } + break; + case PROPERTYHANDLE_WARNINGENABLED: + { + bDone = !m_bROWarning; + if( bDone ) + lValues[ nRealCount ] <<= m_bWarning; + } + break; + case PROPERTYHANDLE_CONFIRMATIONENABLED: + { + bDone = !m_bROConfirmation; + if( bDone ) + lValues[ nRealCount ] <<= m_bConfirmation; + } + break; + // xmlsec05 deprecated + + default: + bDone = false; + } + + if( bDone ) + { + lNames[ nRealCount ] = lOrgNames[ nProperty ]; + ++nRealCount; + } + } + // Set properties in configuration. + lNames.realloc(nRealCount); + lValues.realloc(nRealCount); + PutProperties( lNames, lValues ); +} + +bool SvtSecurityOptions_Impl::IsReadOnly( SvtSecurityOptions::EOption eOption ) const +{ + bool bReadonly; + switch(eOption) + { + case SvtSecurityOptions::EOption::SecureUrls : + bReadonly = m_bROSecureURLs; + break; + case SvtSecurityOptions::EOption::DocWarnSaveOrSend: + bReadonly = m_bROSaveOrSend; + break; + case SvtSecurityOptions::EOption::DocWarnSigning: + bReadonly = m_bROSigning; + break; + case SvtSecurityOptions::EOption::DocWarnPrint: + bReadonly = m_bROPrint; + break; + case SvtSecurityOptions::EOption::DocWarnCreatePdf: + bReadonly = m_bROCreatePDF; + break; + case SvtSecurityOptions::EOption::DocWarnRemovePersonalInfo: + bReadonly = m_bRORemoveInfo; + break; + case SvtSecurityOptions::EOption::DocWarnRecommendPassword: + bReadonly = m_bRORecommendPwd; + break; + case SvtSecurityOptions::EOption::MacroSecLevel: + bReadonly = m_bROSecLevel; + break; + case SvtSecurityOptions::EOption::MacroTrustedAuthors: + bReadonly = m_bROTrustedAuthors; + break; + case SvtSecurityOptions::EOption::CtrlClickHyperlink: + bReadonly = m_bROCtrlClickHyperlink; + break; + case SvtSecurityOptions::EOption::BlockUntrustedRefererLinks: + bReadonly = m_bROBlockUntrustedRefererLinks; + break; + + // xmlsec05 deprecated + case SvtSecurityOptions::EOption::BasicMode: + bReadonly = m_bROBasicMode; + break; + case SvtSecurityOptions::EOption::ExecutePlugins: + bReadonly = m_bROExecutePlugins; + break; + case SvtSecurityOptions::EOption::Warning: + bReadonly = m_bROWarning; + break; + case SvtSecurityOptions::EOption::Confirmation: + bReadonly = m_bROConfirmation; + break; + // xmlsec05 deprecated + + default: + bReadonly = true; + } + + return bReadonly; +} + + +void SvtSecurityOptions_Impl::SetSecureURLs( const Sequence< OUString >& seqURLList ) +{ + DBG_ASSERT(!m_bROSecureURLs, "SvtSecurityOptions_Impl::SetSecureURLs()\nYou tried to write on a readonly value!\n"); + if (!m_bROSecureURLs && m_seqSecureURLs!=seqURLList) + { + m_seqSecureURLs = seqURLList; + SetModified(); + } +} + +inline sal_Int32 SvtSecurityOptions_Impl::GetMacroSecurityLevel() const +{ + return m_nSecLevel; +} + +inline bool SvtSecurityOptions_Impl::IsMacroDisabled() const +{ + return m_bDisableMacros; +} + +void SvtSecurityOptions_Impl::SetMacroSecurityLevel( sal_Int32 _nLevel ) +{ + if( !m_bROSecLevel ) + { + if( _nLevel > 3 || _nLevel < 0 ) + _nLevel = 3; + + if( m_nSecLevel != _nLevel ) + { + m_nSecLevel = _nLevel; + SetModified(); + } + } +} + + +void SvtSecurityOptions_Impl::SetTrustedAuthors( const Sequence< SvtSecurityOptions::Certificate >& rAuthors ) +{ + DBG_ASSERT(!m_bROTrustedAuthors, "SvtSecurityOptions_Impl::SetTrustedAuthors()\nYou tried to write on a readonly value!\n"); + if( !m_bROTrustedAuthors && rAuthors != m_seqTrustedAuthors ) + { + m_seqTrustedAuthors = rAuthors; + SetModified(); + } +} + +bool SvtSecurityOptions_Impl::IsOptionSet( SvtSecurityOptions::EOption eOption ) const +{ + bool* pValue; + bool* pRO; + bool bRet = false; + + if( const_cast< SvtSecurityOptions_Impl* >( this )->GetOption( eOption, pValue, pRO ) ) + bRet = *pValue; + + return bRet; +} + +void SvtSecurityOptions_Impl::SetOption( SvtSecurityOptions::EOption eOption, bool bValue ) +{ + bool* pValue; + bool* pRO; + + if( GetOption( eOption, pValue, pRO ) && !*pRO && *pValue != bValue) + { + *pValue = bValue; + SetModified(); + } +} + +bool SvtSecurityOptions_Impl::IsOptionEnabled( SvtSecurityOptions::EOption eOption ) const +{ + bool* pValue; + bool* pRO; + bool bRet = false; + + if( const_cast< SvtSecurityOptions_Impl* >( this )->GetOption( eOption, pValue, pRO ) ) + bRet = !*pRO; + + return bRet; +} + +Sequence< OUString > SvtSecurityOptions_Impl::GetPropertyNames() +{ + return Sequence< OUString > + { + PROPERTYNAME_SECUREURL, + PROPERTYNAME_STAROFFICEBASIC, + PROPERTYNAME_EXECUTEPLUGINS, + PROPERTYNAME_WARNINGENABLED, + PROPERTYNAME_CONFIRMATIONENABLED, + PROPERTYNAME_DOCWARN_SAVEORSEND, + PROPERTYNAME_DOCWARN_SIGNING, + PROPERTYNAME_DOCWARN_PRINT, + PROPERTYNAME_DOCWARN_CREATEPDF, + PROPERTYNAME_DOCWARN_REMOVEPERSONALINFO, + PROPERTYNAME_DOCWARN_RECOMMENDPASSWORD, + PROPERTYNAME_CTRLCLICK_HYPERLINK, + PROPERTYNAME_BLOCKUNTRUSTEDREFERERLINKS, + PROPERTYNAME_MACRO_SECLEVEL, + PROPERTYNAME_MACRO_TRUSTEDAUTHORS, + PROPERTYNAME_MACRO_DISABLE + }; +} + +namespace { + +std::weak_ptr<SvtSecurityOptions_Impl> g_pSecurityOptions; + +} + +SvtSecurityOptions::SvtSecurityOptions() +{ + // Global access, must be guarded (multithreading!). + MutexGuard aGuard( GetInitMutex() ); + + m_pImpl = g_pSecurityOptions.lock(); + if( !m_pImpl ) + { + m_pImpl = std::make_shared<SvtSecurityOptions_Impl>(); + g_pSecurityOptions = m_pImpl; + + ItemHolder1::holdConfigItem(EItem::SecurityOptions); + } +} + +SvtSecurityOptions::~SvtSecurityOptions() +{ + // Global access, must be guarded (multithreading!) + MutexGuard aGuard( GetInitMutex() ); + + m_pImpl.reset(); +} + +bool SvtSecurityOptions::IsReadOnly( EOption eOption ) const +{ + MutexGuard aGuard( GetInitMutex() ); + return m_pImpl->IsReadOnly(eOption); +} + +Sequence< OUString > SvtSecurityOptions::GetSecureURLs() const +{ + MutexGuard aGuard( GetInitMutex() ); + return m_pImpl->GetSecureURLs(); +} + +void SvtSecurityOptions::SetSecureURLs( const Sequence< OUString >& seqURLList ) +{ + MutexGuard aGuard( GetInitMutex() ); + m_pImpl->SetSecureURLs( seqURLList ); +} + +bool SvtSecurityOptions::isSecureMacroUri( + OUString const & uri, OUString const & referer) const +{ + switch (INetURLObject(uri).GetProtocol()) { + case INetProtocol::Macro: + if (uri.startsWithIgnoreAsciiCase("macro:///")) { + // Denotes an App-BASIC macro (see SfxMacroLoader::loadMacro), which + // is considered safe: + return true; + } + [[fallthrough]]; + case INetProtocol::Slot: + return referer.equalsIgnoreAsciiCase("private:user") + || isTrustedLocationUri(referer); + default: + return true; + } +} + +bool SvtSecurityOptions::isUntrustedReferer(OUString const & referer) const { + MutexGuard g(GetInitMutex()); + return m_pImpl->IsOptionSet(EOption::BlockUntrustedRefererLinks) + && !(referer.isEmpty() || referer.startsWithIgnoreAsciiCase("private:") + || isTrustedLocationUri(referer)); +} + +bool SvtSecurityOptions::isTrustedLocationUri(OUString const & uri) const { + MutexGuard g(GetInitMutex()); + for (const auto & url : std::as_const(m_pImpl->m_seqSecureURLs)) + { + if (UCBContentHelper::IsSubPath(url, uri)) + { + return true; + } + } + return false; +} + +bool SvtSecurityOptions::isTrustedLocationUriForUpdatingLinks( + OUString const & uri) const +{ + return GetMacroSecurityLevel() == 0 || uri.isEmpty() + || uri.startsWithIgnoreAsciiCase("private:") + || isTrustedLocationUri(uri); +} + +sal_Int32 SvtSecurityOptions::GetMacroSecurityLevel() const +{ + MutexGuard aGuard( GetInitMutex() ); + return m_pImpl->GetMacroSecurityLevel(); +} + +void SvtSecurityOptions::SetMacroSecurityLevel( sal_Int32 _nLevel ) +{ + MutexGuard aGuard( GetInitMutex() ); + m_pImpl->SetMacroSecurityLevel( _nLevel ); +} + +bool SvtSecurityOptions::IsMacroDisabled() const +{ + MutexGuard aGuard( GetInitMutex() ); + return m_pImpl->IsMacroDisabled(); +} + +Sequence< SvtSecurityOptions::Certificate > SvtSecurityOptions::GetTrustedAuthors() const +{ + MutexGuard aGuard( GetInitMutex() ); + return m_pImpl->GetTrustedAuthors(); +} + +void SvtSecurityOptions::SetTrustedAuthors( const Sequence< Certificate >& rAuthors ) +{ + MutexGuard aGuard( GetInitMutex() ); + m_pImpl->SetTrustedAuthors( rAuthors ); +} + +bool SvtSecurityOptions::IsOptionSet( EOption eOption ) const +{ + MutexGuard aGuard( GetInitMutex() ); + return m_pImpl->IsOptionSet( eOption ); +} + +void SvtSecurityOptions::SetOption( EOption eOption, bool bValue ) +{ + MutexGuard aGuard( GetInitMutex() ); + m_pImpl->SetOption( eOption, bValue ); +} + +bool SvtSecurityOptions::IsOptionEnabled( EOption eOption ) const +{ + MutexGuard aGuard( GetInitMutex() ); + return m_pImpl->IsOptionEnabled( eOption ); +} + +namespace +{ + class theSecurityOptionsMutex : public rtl::Static<osl::Mutex, theSecurityOptionsMutex>{}; +} + +Mutex& SvtSecurityOptions::GetInitMutex() +{ + return theSecurityOptionsMutex::get(); +} + +void SvtSecurityOptions_Impl::SetBasicMode( EBasicSecurityMode eMode ) +{ + DBG_ASSERT(!m_bROBasicMode, "SvtSecurityOptions_Impl::SetBasicMode()\nYou tried to write on a readonly value!\n"); + if (!m_bROBasicMode && m_eBasicMode!=eMode) + { + m_eBasicMode = eMode; + SetModified(); + } +} + + +void SvtSecurityOptions_Impl::SetExecutePlugins( bool bSet ) +{ + DBG_ASSERT(!m_bROExecutePlugins, "SvtSecurityOptions_Impl::SetExecutePlugins()\nYou tried to write on a readonly value!\n"); + if (!m_bROExecutePlugins && m_bExecutePlugins!=bSet) + { + m_bExecutePlugins = bSet; + SetModified(); + } +} + + +void SvtSecurityOptions_Impl::SetWarningEnabled( bool bSet ) +{ + DBG_ASSERT(!m_bROWarning, "SvtSecurityOptions_Impl::SetWarningEnabled()\nYou tried to write on a readonly value!\n"); + if (!m_bROWarning && m_bWarning!=bSet) + { + m_bWarning = bSet; + SetModified(); + } +} + + +void SvtSecurityOptions_Impl::SetConfirmationEnabled( bool bSet ) +{ + DBG_ASSERT(!m_bROConfirmation, "SvtSecurityOptions_Impl::SetConfirmationEnabled()\nYou tried to write on a readonly value!\n"); + if (!m_bROConfirmation && m_bConfirmation!=bSet) + { + m_bConfirmation = bSet; + SetModified(); + } +} + +bool SvtSecurityOptions::IsExecutePlugins() const +{ + MutexGuard aGuard( GetInitMutex() ); + return m_pImpl->IsExecutePlugins(); +} + +void SvtSecurityOptions::SetExecutePlugins( bool bSet ) +{ + MutexGuard aGuard( GetInitMutex() ); + m_pImpl->SetExecutePlugins( bSet ); +} + +bool SvtSecurityOptions::IsWarningEnabled() const +{ + MutexGuard aGuard( GetInitMutex() ); + return m_pImpl->IsWarningEnabled(); +} + +void SvtSecurityOptions::SetWarningEnabled( bool bSet ) +{ + MutexGuard aGuard( GetInitMutex() ); + m_pImpl->SetWarningEnabled( bSet ); +} + +bool SvtSecurityOptions::IsConfirmationEnabled() const +{ + MutexGuard aGuard( GetInitMutex() ); + return m_pImpl->IsConfirmationEnabled(); +} + +void SvtSecurityOptions::SetConfirmationEnabled( bool bSet ) +{ + MutexGuard aGuard( GetInitMutex() ); + m_pImpl->SetConfirmationEnabled( bSet ); +} + +void SvtSecurityOptions::SetBasicMode( EBasicSecurityMode eMode ) +{ + MutexGuard aGuard( GetInitMutex() ); + m_pImpl->SetBasicMode( eMode ); +} + +EBasicSecurityMode SvtSecurityOptions::GetBasicMode() const +{ + MutexGuard aGuard( GetInitMutex() ); + return m_pImpl->GetBasicMode(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/syslocaleoptions.cxx b/unotools/source/config/syslocaleoptions.cxx new file mode 100644 index 000000000..7cbb817ee --- /dev/null +++ b/unotools/source/config/syslocaleoptions.cxx @@ -0,0 +1,710 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <com/sun/star/uno/Sequence.hxx> +#include <rtl/instance.hxx> +#include <sal/log.hxx> +#include <i18nlangtag/mslangid.hxx> +#include <i18nlangtag/languagetag.hxx> +#include <tools/debug.hxx> +#include <tools/link.hxx> +#include <unotools/syslocaleoptions.hxx> +#include <unotools/configmgr.hxx> +#include <unotools/configitem.hxx> +#include <com/sun/star/uno/Any.hxx> + +#include "itemholder1.hxx" + +#define CFG_READONLY_DEFAULT false + +using namespace osl; +using namespace utl; +using namespace com::sun::star::uno; +using namespace com::sun::star::lang; + +namespace +{ + std::weak_ptr<SvtSysLocaleOptions_Impl> g_pSysLocaleOptions; + struct CurrencyChangeLink + : public rtl::Static<Link<LinkParamNone*,void>, CurrencyChangeLink> {}; + +Mutex& GetMutex() +{ + // #i77768# Due to a static reference in the toolkit lib + // we need a mutex that lives longer than the svl library. + // Otherwise the dtor would use a destructed mutex!! + static Mutex* persistentMutex(new Mutex); + + return *persistentMutex; +} + +} + +class SvtSysLocaleOptions_Impl : public utl::ConfigItem +{ + LanguageTag m_aRealLocale; + LanguageTag m_aRealUILocale; + OUString m_aLocaleString; // en-US or de-DE or empty for SYSTEM + OUString m_aUILocaleString; // en-US or de-DE or empty for SYSTEM + OUString m_aCurrencyString; // USD-en-US or EUR-de-DE + OUString m_aDatePatternsString; // "Y-M-D;M-D" + bool m_bDecimalSeparator; //use decimal separator same as locale + bool m_bIgnoreLanguageChange; //OS language change doesn't affect LO document language + + bool m_bROLocale; + bool m_bROUILocale; + bool m_bROCurrency; + bool m_bRODatePatterns; + bool m_bRODecimalSeparator; + bool m_bROIgnoreLanguageChange; + + static Sequence<OUString> GetPropertyNames(); + void MakeRealLocale(); + void MakeRealUILocale(); + + virtual void ImplCommit() override; + +public: + SvtSysLocaleOptions_Impl(); + virtual ~SvtSysLocaleOptions_Impl() override; + + virtual void Notify( const css::uno::Sequence< OUString >& aPropertyNames ) override; + + const OUString& GetLocaleString() const + { return m_aLocaleString; } + void SetLocaleString( const OUString& rStr ); + + void SetUILocaleString( const OUString& rStr ); + + const OUString& GetCurrencyString() const + { return m_aCurrencyString; } + void SetCurrencyString( const OUString& rStr ); + + const OUString& GetDatePatternsString() const + { return m_aDatePatternsString; } + void SetDatePatternsString( const OUString& rStr ); + + bool IsDecimalSeparatorAsLocale() const { return m_bDecimalSeparator;} + void SetDecimalSeparatorAsLocale( bool bSet); + + bool IsIgnoreLanguageChange() const { return m_bIgnoreLanguageChange;} + void SetIgnoreLanguageChange( bool bSet); + + bool IsReadOnly( SvtSysLocaleOptions::EOption eOption ) const; + const LanguageTag& GetRealLocale() const { return m_aRealLocale; } + const LanguageTag& GetRealUILocale() const { return m_aRealUILocale; } +}; + +#define ROOTNODE_SYSLOCALE "Setup/L10N" + +#define PROPERTYNAME_LOCALE "ooSetupSystemLocale" +#define PROPERTYNAME_UILOCALE "ooLocale" +#define PROPERTYNAME_CURRENCY "ooSetupCurrency" +#define PROPERTYNAME_DECIMALSEPARATOR "DecimalSeparatorAsLocale" +#define PROPERTYNAME_DATEPATTERNS "DateAcceptancePatterns" +#define PROPERTYNAME_IGNORELANGCHANGE "IgnoreLanguageChange" + +#define PROPERTYHANDLE_LOCALE 0 +#define PROPERTYHANDLE_UILOCALE 1 +#define PROPERTYHANDLE_CURRENCY 2 +#define PROPERTYHANDLE_DECIMALSEPARATOR 3 +#define PROPERTYHANDLE_DATEPATTERNS 4 +#define PROPERTYHANDLE_IGNORELANGCHANGE 5 + +Sequence< OUString > SvtSysLocaleOptions_Impl::GetPropertyNames() +{ + return Sequence< OUString > + { + PROPERTYNAME_LOCALE, + PROPERTYNAME_UILOCALE, + PROPERTYNAME_CURRENCY, + PROPERTYNAME_DECIMALSEPARATOR, + PROPERTYNAME_DATEPATTERNS, + PROPERTYNAME_IGNORELANGCHANGE + }; +} + +SvtSysLocaleOptions_Impl::SvtSysLocaleOptions_Impl() + : ConfigItem( ROOTNODE_SYSLOCALE ) + , m_aRealLocale( LANGUAGE_SYSTEM) + , m_aRealUILocale( LANGUAGE_SYSTEM) + , m_bDecimalSeparator( true ) + , m_bIgnoreLanguageChange( false) + , m_bROLocale(CFG_READONLY_DEFAULT) + , m_bROUILocale(CFG_READONLY_DEFAULT) + , m_bROCurrency(CFG_READONLY_DEFAULT) + , m_bRODatePatterns(CFG_READONLY_DEFAULT) + , m_bRODecimalSeparator(false) + , m_bROIgnoreLanguageChange(false) + +{ + const Sequence< OUString > aNames = GetPropertyNames(); + Sequence< Any > aValues = GetProperties( aNames ); + Sequence< sal_Bool > aROStates = GetReadOnlyStates( aNames ); + const Any* pValues = aValues.getConstArray(); + const sal_Bool* pROStates = aROStates.getConstArray(); + DBG_ASSERT( aValues.getLength() == aNames.getLength(), "GetProperties failed" ); + DBG_ASSERT( aROStates.getLength() == aNames.getLength(), "GetReadOnlyStates failed" ); + if ( aValues.getLength() == aNames.getLength() && aROStates.getLength() == aNames.getLength() ) + { + for ( sal_Int32 nProp = 0; nProp < aNames.getLength(); nProp++ ) + { + if ( pValues[nProp].hasValue() ) + { + switch ( nProp ) + { + case PROPERTYHANDLE_LOCALE : + { + OUString aStr; + if ( pValues[nProp] >>= aStr ) + m_aLocaleString = aStr; + else + { + SAL_WARN( "unotools.config", "Wrong property type!" ); + } + m_bROLocale = pROStates[nProp]; + } + break; + case PROPERTYHANDLE_UILOCALE : + { + OUString aStr; + if ( pValues[nProp] >>= aStr ) + m_aUILocaleString = aStr; + else + { + SAL_WARN( "unotools.config", "Wrong property type!" ); + } + m_bROUILocale = pROStates[nProp]; + } + break; + case PROPERTYHANDLE_CURRENCY : + { + OUString aStr; + if ( pValues[nProp] >>= aStr ) + m_aCurrencyString = aStr; + else + { + SAL_WARN( "unotools.config", "Wrong property type!" ); + } + m_bROCurrency = pROStates[nProp]; + } + break; + case PROPERTYHANDLE_DECIMALSEPARATOR: + { + bool bValue = false; + if ( pValues[nProp] >>= bValue ) + m_bDecimalSeparator = bValue; + else + { + SAL_WARN( "unotools.config", "Wrong property type!" ); + } + m_bRODecimalSeparator = pROStates[nProp]; + } + break; + case PROPERTYHANDLE_DATEPATTERNS : + { + OUString aStr; + if ( pValues[nProp] >>= aStr ) + m_aDatePatternsString = aStr; + else + { + SAL_WARN( "unotools.config", "Wrong property type!" ); + } + m_bRODatePatterns = pROStates[nProp]; + } + break; + case PROPERTYHANDLE_IGNORELANGCHANGE : + { + bool bValue = false; + if ( pValues[nProp] >>= bValue ) + m_bIgnoreLanguageChange = bValue; + else + { + SAL_WARN( "unotools.config", "Wrong property type!" ); + } + m_bROIgnoreLanguageChange = pROStates[nProp]; + } + break; + default: + SAL_WARN( "unotools.config", "Wrong property type!" ); + } + } + } + } + EnableNotification( aNames ); + + MakeRealLocale(); + MakeRealUILocale(); +} + +SvtSysLocaleOptions_Impl::~SvtSysLocaleOptions_Impl() +{ + assert(!IsModified()); // should have been committed +} + +void SvtSysLocaleOptions_Impl::MakeRealLocale() +{ + if (m_aLocaleString.isEmpty()) + { + LanguageType nLang = MsLangId::getSystemLanguage(); + m_aRealLocale.reset( nLang).makeFallback(); + } + else + { + m_aRealLocale.reset( m_aLocaleString).makeFallback(); + } +} + +void SvtSysLocaleOptions_Impl::MakeRealUILocale() +{ + if (m_aUILocaleString.isEmpty()) + { + LanguageType nLang = MsLangId::getSystemUILanguage(); + m_aRealUILocale.reset( nLang).makeFallback(); + } + else + { + m_aRealUILocale.reset( m_aUILocaleString).makeFallback(); + } +} + +bool SvtSysLocaleOptions_Impl::IsReadOnly( SvtSysLocaleOptions::EOption eOption ) const +{ + bool bReadOnly = CFG_READONLY_DEFAULT; + switch(eOption) + { + case SvtSysLocaleOptions::EOption::Locale : + { + bReadOnly = m_bROLocale; + break; + } + case SvtSysLocaleOptions::EOption::Currency : + { + bReadOnly = m_bROCurrency; + break; + } + case SvtSysLocaleOptions::EOption::DatePatterns : + { + bReadOnly = m_bRODatePatterns; + break; + } + } + return bReadOnly; +} + +void SvtSysLocaleOptions_Impl::ImplCommit() +{ + const Sequence< OUString > aOrgNames = GetPropertyNames(); + sal_Int32 nOrgCount = aOrgNames.getLength(); + + Sequence< OUString > aNames( nOrgCount ); + Sequence< Any > aValues( nOrgCount ); + + OUString* pNames = aNames.getArray(); + Any* pValues = aValues.getArray(); + sal_Int32 nRealCount = 0; + + for ( sal_Int32 nProp = 0; nProp < nOrgCount; nProp++ ) + { + switch ( nProp ) + { + case PROPERTYHANDLE_LOCALE : + { + if (!m_bROLocale) + { + pNames[nRealCount] = aOrgNames[nProp]; + pValues[nRealCount] <<= m_aLocaleString; + ++nRealCount; + } + } + break; + case PROPERTYHANDLE_UILOCALE : + { + if (!m_bROUILocale) + { + pNames[nRealCount] = aOrgNames[nProp]; + pValues[nRealCount] <<= m_aUILocaleString; + ++nRealCount; + } + } + break; + case PROPERTYHANDLE_CURRENCY : + { + if (!m_bROCurrency) + { + pNames[nRealCount] = aOrgNames[nProp]; + pValues[nRealCount] <<= m_aCurrencyString; + ++nRealCount; + } + } + break; + case PROPERTYHANDLE_DECIMALSEPARATOR: + if( !m_bRODecimalSeparator ) + { + pNames[nRealCount] = aOrgNames[nProp]; + pValues[nRealCount] <<= m_bDecimalSeparator; + ++nRealCount; + } + break; + case PROPERTYHANDLE_DATEPATTERNS : + if (!m_bRODatePatterns) + { + pNames[nRealCount] = aOrgNames[nProp]; + pValues[nRealCount] <<= m_aDatePatternsString; + ++nRealCount; + } + break; + case PROPERTYHANDLE_IGNORELANGCHANGE : + if (!m_bROIgnoreLanguageChange) + { + pNames[nRealCount] = aOrgNames[nProp]; + pValues[nRealCount] <<= m_bIgnoreLanguageChange; + ++nRealCount; + } + break; + default: + SAL_WARN( "unotools.config", "invalid index to save a path" ); + } + } + aNames.realloc(nRealCount); + aValues.realloc(nRealCount); + PutProperties( aNames, aValues ); +} + +void SvtSysLocaleOptions_Impl::SetLocaleString( const OUString& rStr ) +{ + ConfigurationHints nHint = ConfigurationHints::Locale; + { + MutexGuard aGuard( GetMutex() ); + if (m_bROLocale || rStr == m_aLocaleString ) + { + return; + } + m_aLocaleString = rStr; + MakeRealLocale(); + LanguageTag::setConfiguredSystemLanguage( m_aRealLocale.getLanguageType() ); + SetModified(); + if ( m_aCurrencyString.isEmpty() ) + nHint |= ConfigurationHints::Currency; + } + NotifyListeners( nHint ); +} + +void SvtSysLocaleOptions_Impl::SetUILocaleString( const OUString& rStr ) +{ + { + MutexGuard aGuard( GetMutex() ); + if (m_bROUILocale || rStr == m_aUILocaleString ) + { + return; + } + m_aUILocaleString = rStr; + + // as we can't switch UILocale at runtime, we only store changes in the configuration + MakeRealUILocale(); + SetModified(); + } + NotifyListeners( ConfigurationHints::UiLocale ); +} + +void SvtSysLocaleOptions_Impl::SetCurrencyString( const OUString& rStr ) +{ + { + MutexGuard aGuard( GetMutex() ); + if (m_bROCurrency || rStr == m_aCurrencyString ) + { + return; + } + m_aCurrencyString = rStr; + SetModified(); + } + NotifyListeners( ConfigurationHints::Currency ); +} + +void SvtSysLocaleOptions_Impl::SetDatePatternsString( const OUString& rStr ) +{ + { + MutexGuard aGuard( GetMutex() ); + if (m_bRODatePatterns || rStr == m_aDatePatternsString ) + { + return; + } + m_aDatePatternsString = rStr; + SetModified(); + } + NotifyListeners( ConfigurationHints::DatePatterns ); +} + +void SvtSysLocaleOptions_Impl::SetDecimalSeparatorAsLocale( bool bSet) +{ + { + MutexGuard aGuard( GetMutex() ); + if(bSet == m_bDecimalSeparator) + { + return; + } + m_bDecimalSeparator = bSet; + SetModified(); + } + NotifyListeners( ConfigurationHints::DecSep ); +} + +void SvtSysLocaleOptions_Impl::SetIgnoreLanguageChange( bool bSet) +{ + { + MutexGuard aGuard( GetMutex() ); + if(bSet == m_bIgnoreLanguageChange) + { + return; + } + m_bIgnoreLanguageChange = bSet; + SetModified(); + } + NotifyListeners( ConfigurationHints::IgnoreLang ); +} + +void SvtSysLocaleOptions_Impl::Notify( const Sequence< OUString >& seqPropertyNames ) +{ + ConfigurationHints nHint = ConfigurationHints::NONE; + Sequence< Any > seqValues = GetProperties( seqPropertyNames ); + Sequence< sal_Bool > seqROStates = GetReadOnlyStates( seqPropertyNames ); + sal_Int32 nCount = seqPropertyNames.getLength(); + for( sal_Int32 nProp = 0; nProp < nCount; ++nProp ) + { + if( seqPropertyNames[nProp] == PROPERTYNAME_LOCALE ) + { + DBG_ASSERT( seqValues[nProp].getValueTypeClass() == TypeClass_STRING, "Locale property type" ); + seqValues[nProp] >>= m_aLocaleString; + m_bROLocale = seqROStates[nProp]; + nHint |= ConfigurationHints::Locale; + if ( m_aCurrencyString.isEmpty() ) + nHint |= ConfigurationHints::Currency; + MakeRealLocale(); + } + if( seqPropertyNames[nProp] == PROPERTYNAME_UILOCALE ) + { + DBG_ASSERT( seqValues[nProp].getValueTypeClass() == TypeClass_STRING, "Locale property type" ); + seqValues[nProp] >>= m_aUILocaleString; + m_bROUILocale = seqROStates[nProp]; + nHint |= ConfigurationHints::UiLocale; + MakeRealUILocale(); + } + else if( seqPropertyNames[nProp] == PROPERTYNAME_CURRENCY ) + { + DBG_ASSERT( seqValues[nProp].getValueTypeClass() == TypeClass_STRING, "Currency property type" ); + seqValues[nProp] >>= m_aCurrencyString; + m_bROCurrency = seqROStates[nProp]; + nHint |= ConfigurationHints::Currency; + } + else if( seqPropertyNames[nProp] == PROPERTYNAME_DECIMALSEPARATOR ) + { + seqValues[nProp] >>= m_bDecimalSeparator; + m_bRODecimalSeparator = seqROStates[nProp]; + } + else if( seqPropertyNames[nProp] == PROPERTYNAME_IGNORELANGCHANGE ) + { + seqValues[nProp] >>= m_bIgnoreLanguageChange; + m_bROIgnoreLanguageChange = seqROStates[nProp]; + } + else if( seqPropertyNames[nProp] == PROPERTYNAME_DATEPATTERNS ) + { + DBG_ASSERT( seqValues[nProp].getValueTypeClass() == TypeClass_STRING, "DatePatterns property type" ); + seqValues[nProp] >>= m_aDatePatternsString; + m_bRODatePatterns = seqROStates[nProp]; + nHint |= ConfigurationHints::DatePatterns; + } + } + if ( nHint != ConfigurationHints::NONE ) + NotifyListeners( nHint ); +} + +SvtSysLocaleOptions::SvtSysLocaleOptions() +{ + MutexGuard aGuard( GetMutex() ); + pImpl = g_pSysLocaleOptions.lock(); + if ( !pImpl ) + { + pImpl = std::make_shared<SvtSysLocaleOptions_Impl>(); + g_pSysLocaleOptions = pImpl; + if (!utl::ConfigManager::IsFuzzing()) + ItemHolder1::holdConfigItem(EItem::SysLocaleOptions); + } + pImpl->AddListener(this); +} + +SvtSysLocaleOptions::~SvtSysLocaleOptions() +{ + MutexGuard aGuard( GetMutex() ); + pImpl->RemoveListener(this); + pImpl.reset(); +} + +bool SvtSysLocaleOptions::IsModified() const +{ + MutexGuard aGuard( GetMutex() ); + return pImpl->IsModified(); +} + +void SvtSysLocaleOptions::Commit() +{ + MutexGuard aGuard( GetMutex() ); + pImpl->Commit(); +} + +void SvtSysLocaleOptions::BlockBroadcasts( bool bBlock ) +{ + MutexGuard aGuard( GetMutex() ); + pImpl->BlockBroadcasts( bBlock ); +} + +void SvtSysLocaleOptions::SetLocaleConfigString( const OUString& rStr ) +{ + pImpl->SetLocaleString( rStr ); +} + +void SvtSysLocaleOptions::SetUILocaleConfigString( const OUString& rStr ) +{ + pImpl->SetUILocaleString( rStr ); +} + +const OUString& SvtSysLocaleOptions::GetCurrencyConfigString() const +{ + MutexGuard aGuard( GetMutex() ); + return pImpl->GetCurrencyString(); +} + +void SvtSysLocaleOptions::SetCurrencyConfigString( const OUString& rStr ) +{ + pImpl->SetCurrencyString( rStr ); +} + +const OUString& SvtSysLocaleOptions::GetDatePatternsConfigString() const +{ + MutexGuard aGuard( GetMutex() ); + return pImpl->GetDatePatternsString(); +} + +void SvtSysLocaleOptions::SetDatePatternsConfigString( const OUString& rStr ) +{ + pImpl->SetDatePatternsString( rStr ); +} + +bool SvtSysLocaleOptions::IsDecimalSeparatorAsLocale() const +{ + MutexGuard aGuard( GetMutex() ); + return pImpl->IsDecimalSeparatorAsLocale(); +} + +void SvtSysLocaleOptions::SetDecimalSeparatorAsLocale( bool bSet) +{ + pImpl->SetDecimalSeparatorAsLocale(bSet); +} + +bool SvtSysLocaleOptions::IsIgnoreLanguageChange() const +{ + MutexGuard aGuard( GetMutex() ); + return pImpl->IsIgnoreLanguageChange(); +} + +void SvtSysLocaleOptions::SetIgnoreLanguageChange( bool bSet) +{ + pImpl->SetIgnoreLanguageChange(bSet); +} + +bool SvtSysLocaleOptions::IsReadOnly( EOption eOption ) const +{ + MutexGuard aGuard( GetMutex() ); + return pImpl->IsReadOnly( eOption ); +} + +// static +void SvtSysLocaleOptions::GetCurrencyAbbrevAndLanguage( OUString& rAbbrev, + LanguageType& eLang, + const OUString& rConfigString ) +{ + sal_Int32 nDelim = rConfigString.indexOf( '-' ); + if ( nDelim >= 0 ) + { + rAbbrev = rConfigString.copy( 0, nDelim ); + OUString aIsoStr( rConfigString.copy( nDelim+1 ) ); + eLang = LanguageTag::convertToLanguageTypeWithFallback( aIsoStr ); + } + else + { + rAbbrev = rConfigString; + eLang = (rAbbrev.isEmpty() ? LANGUAGE_SYSTEM : LANGUAGE_NONE); + } +} + +// static +OUString SvtSysLocaleOptions::CreateCurrencyConfigString( + const OUString& rAbbrev, LanguageType eLang ) +{ + OUString aIsoStr( LanguageTag::convertToBcp47( eLang ) ); + if ( !aIsoStr.isEmpty() ) + { + return rAbbrev + "-" + aIsoStr; + } + else + return rAbbrev; +} + +// static +void SvtSysLocaleOptions::SetCurrencyChangeLink( const Link<LinkParamNone*,void>& rLink ) +{ + MutexGuard aGuard( GetMutex() ); + DBG_ASSERT( !CurrencyChangeLink::get().IsSet(), "SvtSysLocaleOptions::SetCurrencyChangeLink: already set" ); + CurrencyChangeLink::get() = rLink; +} + +// static +const Link<LinkParamNone*,void>& SvtSysLocaleOptions::GetCurrencyChangeLink() +{ + MutexGuard aGuard( GetMutex() ); + return CurrencyChangeLink::get(); +} + +void SvtSysLocaleOptions::ConfigurationChanged( utl::ConfigurationBroadcaster* p, ConfigurationHints nHint ) +{ + if ( nHint & ConfigurationHints::Currency ) + { + const Link<LinkParamNone*,void>& rLink = GetCurrencyChangeLink(); + rLink.Call( nullptr ); + } + + ::utl::detail::Options::ConfigurationChanged( p, nHint ); +} + +LanguageTag SvtSysLocaleOptions::GetLanguageTag() const +{ + MutexGuard aGuard( GetMutex() ); + return LanguageTag( pImpl->GetLocaleString() ); +} + +const LanguageTag & SvtSysLocaleOptions::GetRealLanguageTag() const +{ + return pImpl->GetRealLocale(); +} + +const LanguageTag & SvtSysLocaleOptions::GetRealUILanguageTag() const +{ + return pImpl->GetRealUILocale(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/useroptions.cxx b/unotools/source/config/useroptions.cxx new file mode 100644 index 000000000..66f354ef5 --- /dev/null +++ b/unotools/source/config/useroptions.cxx @@ -0,0 +1,348 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <unotools/useroptions.hxx> +#include <unotools/syslocale.hxx> +#include <com/sun/star/uno/Any.hxx> +#include <osl/mutex.hxx> +#include <rtl/instance.hxx> +#include "itemholder1.hxx" + +#include <cppuhelper/implbase.hxx> +#include <com/sun/star/beans/Property.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/beans/PropertyAttribute.hpp> +#include <com/sun/star/container/XNameAccess.hpp> +#include <com/sun/star/util/XChangesListener.hpp> +#include <com/sun/star/util/XChangesNotifier.hpp> +#include <com/sun/star/util/ChangesEvent.hpp> +#include <comphelper/configurationhelper.hxx> +#include <comphelper/processfactory.hxx> +#include <i18nlangtag/mslangid.hxx> +#include <i18nlangtag/languagetag.hxx> +#include <o3tl/enumarray.hxx> +#include <tools/diagnose_ex.h> + +using namespace utl; +using namespace com::sun::star; + +// vOptionNames[] -- names of the user option entries +// The order must correspond to the enum class UserOptToken in useroptions.hxx. +static o3tl::enumarray<UserOptToken, char const *> vOptionNames = { + "l", // UserOptToken::City + "o", // UserOptToken::Company + "c", // UserOptToken::Country + "mail", // UserOptToken::Email + "facsimiletelephonenumber", // UserOptToken::Fax + "givenname", // UserOptToken::FirstName + "sn", // UserOptToken::LastName + "position", // UserOptToken::Position + "st", // UserOptToken::State + "street", // UserOptToken::Street + "homephone", // UserOptToken::TelephoneHome + "telephonenumber", // UserOptToken::TelephoneWork + "title", // UserOptToken::Title + "initials", // UserOptToken::ID + "postalcode", // UserOptToken::Zip + "fathersname", // UserOptToken::FathersName + "apartment", // UserOptToken::Apartment + "signingkey", // UserOptToken::SigningKey + "encryptionkey", // UserOptToken::EncryptionKey + "encrypttoself" // UserOptToken::EncryptToSelf +}; + +std::weak_ptr<SvtUserOptions::Impl> SvtUserOptions::xSharedImpl; + +class SvtUserOptions::ChangeListener : public cppu::WeakImplHelper<util::XChangesListener> +{ +public: + explicit ChangeListener (Impl& rParent): m_rParent(rParent) { } + + // XChangesListener + virtual void SAL_CALL changesOccurred (util::ChangesEvent const& Event) override; + // XEventListener + virtual void SAL_CALL disposing (lang::EventObject const& Source) override; + +private: + Impl& m_rParent; +}; + +class SvtUserOptions::Impl : public utl::ConfigurationBroadcaster +{ +public: + Impl (); + + OUString GetFullName () const; + + bool IsTokenReadonly (UserOptToken nToken) const; + OUString GetToken (UserOptToken nToken) const; + void SetToken (UserOptToken nToken, OUString const& rNewToken); + bool GetBoolValue (UserOptToken nToken) const; + void SetBoolValue (UserOptToken nToken, bool bNewValue); + void Notify (); + +private: + uno::Reference<util::XChangesListener> m_xChangeListener; + uno::Reference<container::XNameAccess> m_xCfg; + uno::Reference<beans::XPropertySet> m_xData; + + template < typename ValueType > + ValueType GetValue_Impl( UserOptToken nToken ) const; + template < typename ValueType > + void SetValue_Impl( UserOptToken nToken, ValueType const& rNewValue ); +}; + +void SvtUserOptions::ChangeListener::changesOccurred (util::ChangesEvent const& rEvent) +{ + if (rEvent.Changes.hasElements()) + m_rParent.Notify(); +} + +void SvtUserOptions::ChangeListener::disposing (lang::EventObject const& rSource) +{ + try + { + uno::Reference<util::XChangesNotifier> xChgNot(rSource.Source, uno::UNO_QUERY_THROW); + xChgNot->removeChangesListener(this); + } + catch (uno::Exception&) + { + } +} + +SvtUserOptions::Impl::Impl() : + m_xChangeListener( new ChangeListener(*this) ) +{ + try + { + m_xCfg.set( + comphelper::ConfigurationHelper::openConfig( + comphelper::getProcessComponentContext(), + "org.openoffice.UserProfile/Data", + comphelper::EConfigurationModes::Standard + ), + uno::UNO_QUERY + ); + + m_xData.set(m_xCfg, uno::UNO_QUERY); + uno::Reference<util::XChangesNotifier> xChgNot(m_xCfg, uno::UNO_QUERY); + try + { + xChgNot->addChangesListener(m_xChangeListener); + } + catch (uno::RuntimeException&) + { + } + } + catch (uno::Exception const&) + { + DBG_UNHANDLED_EXCEPTION("unotools.config"); + m_xCfg.clear(); + } +} + +template < typename ValueType > +ValueType SvtUserOptions::Impl::GetValue_Impl (UserOptToken nToken) const +{ + ValueType sToken = ValueType(); + try + { + if (m_xData.is()) + m_xData->getPropertyValue(OUString::createFromAscii(vOptionNames[nToken])) >>= sToken; + } + catch (uno::Exception const&) + { + DBG_UNHANDLED_EXCEPTION("unotools.config"); + } + return sToken; +} + +template < typename ValueType > +void SvtUserOptions::Impl::SetValue_Impl (UserOptToken nToken, ValueType const& sToken) +{ + try + { + if (m_xData.is()) + m_xData->setPropertyValue(OUString::createFromAscii(vOptionNames[nToken]), uno::makeAny(sToken)); + comphelper::ConfigurationHelper::flush(m_xCfg); + } + catch (uno::Exception const&) + { + DBG_UNHANDLED_EXCEPTION("unotools.config"); + } +} + +OUString SvtUserOptions::Impl::GetToken (UserOptToken nToken) const +{ + return GetValue_Impl<OUString>( nToken ); +} + +void SvtUserOptions::Impl::SetToken (UserOptToken nToken, OUString const& sToken) +{ + SetValue_Impl<OUString>( nToken, sToken ); +} + +bool SvtUserOptions::Impl::GetBoolValue (UserOptToken nToken) const +{ + return GetValue_Impl<bool>( nToken ); +} + +void SvtUserOptions::Impl::SetBoolValue (UserOptToken nToken, bool bNewValue) +{ + SetValue_Impl<bool>( nToken, bNewValue ); +} + +OUString SvtUserOptions::Impl::GetFullName () const +{ + OUString sFullName; + LanguageType const eLang = SvtSysLocale().GetUILanguageTag().getLanguageType(); + if (eLang == LANGUAGE_RUSSIAN) + { + sFullName = GetToken(UserOptToken::FirstName).trim(); + if (!sFullName.isEmpty()) + sFullName += " "; + sFullName += GetToken(UserOptToken::FathersName).trim(); + if (!sFullName.isEmpty()) + sFullName += " "; + sFullName += GetToken(UserOptToken::LastName).trim(); + } + else + { + if (MsLangId::isFamilyNameFirst(eLang)) + { + sFullName = GetToken(UserOptToken::LastName).trim(); + if (!sFullName.isEmpty()) + sFullName += " "; + sFullName += GetToken(UserOptToken::FirstName).trim(); + } + else + { + sFullName = GetToken(UserOptToken::FirstName).trim(); + if (!sFullName.isEmpty()) + sFullName += " "; + sFullName += GetToken(UserOptToken::LastName).trim(); + } + } + + return sFullName; +} + +void SvtUserOptions::Impl::Notify () +{ + NotifyListeners(ConfigurationHints::NONE); +} + +bool SvtUserOptions::Impl::IsTokenReadonly (UserOptToken nToken) const +{ + uno::Reference<beans::XPropertySet> xData(m_xCfg, uno::UNO_QUERY); + uno::Reference<beans::XPropertySetInfo> xInfo = xData->getPropertySetInfo(); + beans::Property aProp = xInfo->getPropertyByName(OUString::createFromAscii(vOptionNames[nToken])); + return ((aProp.Attributes & beans::PropertyAttribute::READONLY) == + beans::PropertyAttribute::READONLY); +} + +SvtUserOptions::SvtUserOptions () +{ + // Global access, must be guarded (multithreading) + osl::MutexGuard aGuard(GetInitMutex()); + + if (xSharedImpl.expired()) + { + xImpl = std::make_shared<Impl>(); + xSharedImpl = xImpl; + ItemHolder1::holdConfigItem(EItem::UserOptions); + } + xImpl = xSharedImpl.lock(); + xImpl->AddListener(this); +} + +SvtUserOptions::~SvtUserOptions() +{ + // Global access, must be guarded (multithreading) + osl::MutexGuard aGuard( GetInitMutex() ); + xImpl->RemoveListener(this); +} + +namespace +{ + class theUserOptionsMutex : public rtl::Static<osl::Mutex, theUserOptionsMutex>{}; +} + +osl::Mutex& SvtUserOptions::GetInitMutex() +{ + return theUserOptionsMutex::get(); +} + +OUString SvtUserOptions::GetCompany () const { return GetToken(UserOptToken::Company); } +OUString SvtUserOptions::GetFirstName () const { return GetToken(UserOptToken::FirstName); } +OUString SvtUserOptions::GetLastName () const { return GetToken(UserOptToken::LastName); } +OUString SvtUserOptions::GetID () const { return GetToken(UserOptToken::ID); } +OUString SvtUserOptions::GetStreet () const { return GetToken(UserOptToken::Street); } +OUString SvtUserOptions::GetCity () const { return GetToken(UserOptToken::City); } +OUString SvtUserOptions::GetState () const { return GetToken(UserOptToken::State); } +OUString SvtUserOptions::GetZip () const { return GetToken(UserOptToken::Zip); } +OUString SvtUserOptions::GetCountry () const { return GetToken(UserOptToken::Country); } +OUString SvtUserOptions::GetPosition () const { return GetToken(UserOptToken::Position); } +OUString SvtUserOptions::GetTitle () const { return GetToken(UserOptToken::Title); } +OUString SvtUserOptions::GetTelephoneHome () const { return GetToken(UserOptToken::TelephoneHome); } +OUString SvtUserOptions::GetTelephoneWork () const { return GetToken(UserOptToken::TelephoneWork); } +OUString SvtUserOptions::GetFax () const { return GetToken(UserOptToken::Fax); } +OUString SvtUserOptions::GetEmail () const { return GetToken(UserOptToken::Email); } +OUString SvtUserOptions::GetSigningKey () const { return GetToken(UserOptToken::SigningKey); } +OUString SvtUserOptions::GetEncryptionKey () const { return GetToken(UserOptToken::EncryptionKey); } + +bool SvtUserOptions::IsTokenReadonly (UserOptToken nToken) const +{ + osl::MutexGuard aGuard(GetInitMutex()); + return xImpl->IsTokenReadonly(nToken); +} + +OUString SvtUserOptions::GetToken (UserOptToken nToken) const +{ + osl::MutexGuard aGuard(GetInitMutex()); + return xImpl->GetToken(nToken); +} + +void SvtUserOptions::SetToken (UserOptToken nToken, OUString const& rNewToken) +{ + osl::MutexGuard aGuard(GetInitMutex()); + xImpl->SetToken(nToken, rNewToken); +} + +void SvtUserOptions::SetBoolValue (UserOptToken nToken, bool bNewValue) +{ + osl::MutexGuard aGuard(GetInitMutex()); + xImpl->SetBoolValue(nToken, bNewValue); +} + +bool SvtUserOptions::GetEncryptToSelf() const +{ + osl::MutexGuard aGuard(GetInitMutex()); + return xImpl->GetBoolValue(UserOptToken::EncryptToSelf); +} + +OUString SvtUserOptions::GetFullName () const +{ + osl::MutexGuard aGuard(GetInitMutex()); + return xImpl->GetFullName(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/config/viewoptions.cxx b/unotools/source/config/viewoptions.cxx new file mode 100644 index 000000000..4a36ad2c2 --- /dev/null +++ b/unotools/source/config/viewoptions.cxx @@ -0,0 +1,1036 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unotools/viewoptions.hxx> +#include <com/sun/star/uno/Any.hxx> + +#include <com/sun/star/beans/NamedValue.hpp> +#include <com/sun/star/container/XNameContainer.hpp> +#include <com/sun/star/container/XNameAccess.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <osl/diagnose.h> +#include <sal/log.hxx> +#include <unotools/configmgr.hxx> +#include <comphelper/configurationhelper.hxx> +#include <comphelper/processfactory.hxx> +#include <tools/diagnose_ex.h> + +#include "itemholder1.hxx" + +#define PACKAGE_VIEWS "org.openoffice.Office.Views" + +#define LIST_DIALOGS "Dialogs" +#define LIST_TABDIALOGS "TabDialogs" +#define LIST_TABPAGES "TabPages" +#define LIST_WINDOWS "Windows" + +#define PROPERTY_WINDOWSTATE "WindowState" +#define PROPERTY_PAGEID "PageID" +#define PROPERTY_VISIBLE "Visible" +#define PROPERTY_USERDATA "UserData" + +//#define DEBUG_VIEWOPTIONS + +#ifdef DEBUG_VIEWOPTIONS + #define _LOG_COUNTER_( _SVIEW_, _NREAD_, _NWRITE_ ) \ + { \ + FILE* pFile = fopen( "viewdbg.txt", "a" ); \ + fprintf( pFile, "%s[%d, %d]\n", OUStringToOString(_SVIEW_, RTL_TEXTENCODING_UTF8).getStr(), _NREAD_, _NWRITE_ ); \ + fclose( pFile ); \ + } +#endif // DEBUG_VIEWOPTIONS + +// initialization! + +SvtViewOptionsBase_Impl* SvtViewOptions::m_pDataContainer_Dialogs = nullptr ; +sal_Int32 SvtViewOptions::m_nRefCount_Dialogs = 0 ; +SvtViewOptionsBase_Impl* SvtViewOptions::m_pDataContainer_TabDialogs = nullptr ; +sal_Int32 SvtViewOptions::m_nRefCount_TabDialogs = 0 ; +SvtViewOptionsBase_Impl* SvtViewOptions::m_pDataContainer_TabPages = nullptr ; +sal_Int32 SvtViewOptions::m_nRefCount_TabPages = 0 ; +SvtViewOptionsBase_Impl* SvtViewOptions::m_pDataContainer_Windows = nullptr ; +sal_Int32 SvtViewOptions::m_nRefCount_Windows = 0 ; + +/*-************************************************************************************************************ + @descr Implement base data container for view options elements. + Every item support ALL possible configuration information. + But not every superclass should use them! Because some view types don't + have it really. + + @attention We implement a write-through cache! We use it for reading - but write all changes directly to + configuration. (changes are made on internal cache too!). So it's easier to distinguish + between added/changed/removed elements without any complex mask or bool flag information. + Caches from configuration and our own one are synchronized every time - if we do so. +*//*-*************************************************************************************************************/ +class SvtViewOptionsBase_Impl final +{ + + public: + enum State { STATE_NONE, STATE_FALSE, STATE_TRUE }; + + explicit SvtViewOptionsBase_Impl(const OUString& rList); + ~SvtViewOptionsBase_Impl ( ); + bool Exists ( const OUString& sName ); + void Delete ( const OUString& sName ); + OUString GetWindowState ( const OUString& sName ); + void SetWindowState ( const OUString& sName , + const OUString& sState ); + css::uno::Sequence< css::beans::NamedValue > GetUserData ( const OUString& sName ); + void SetUserData ( const OUString& sName , + const css::uno::Sequence< css::beans::NamedValue >& lData ); + OString GetPageID ( const OUString& sName ); + void SetPageID ( const OUString& sName , + const OString& sID ); + State GetVisible ( const OUString& sName ); + void SetVisible ( const OUString& sName , + bool bVisible ); + css::uno::Any GetUserItem ( const OUString& sName , + const OUString& sItem ); + void SetUserItem ( const OUString& sName , + const OUString& sItem , + const css::uno::Any& aValue ); + + private: + css::uno::Reference< css::uno::XInterface > impl_getSetNode( const OUString& sNode , + bool bCreateIfMissing); + + private: + OUString m_sListName; + css::uno::Reference< css::container::XNameAccess > m_xRoot; + css::uno::Reference< css::container::XNameAccess > m_xSet; + + #ifdef DEBUG_VIEWOPTIONS + sal_Int32 m_nReadCount; + sal_Int32 m_nWriteCount; + #endif +}; + +/*-************************************************************************************************************ + @descr Implement the base data container. +*//*-*************************************************************************************************************/ + +/*-************************************************************************************************************ + @short ctor + @descr We use it to open right configuration file and let configuration objects fill her caches. + Then we read all existing entries from right list and cached it inside our object too. + Normally we should enable notifications for changes on these values too ... but these feature + isn't full implemented in the moment. + + @seealso baseclass ::utl::ConfigItem + @seealso method Notify() +*//*-*************************************************************************************************************/ +SvtViewOptionsBase_Impl::SvtViewOptionsBase_Impl( const OUString& sList ) + : m_sListName ( sList ) // we must know, which view type we must support + #ifdef DEBUG_VIEWOPTIONS + , m_nReadCount ( 0 ) + , m_nWriteCount( 0 ) + #endif +{ + if (utl::ConfigManager::IsFuzzing()) + return; + + try + { + m_xRoot.set( ::comphelper::ConfigurationHelper::openConfig( + ::comphelper::getProcessComponentContext(), + PACKAGE_VIEWS, + ::comphelper::EConfigurationModes::Standard), + css::uno::UNO_QUERY); + if (m_xRoot.is()) + m_xRoot->getByName(sList) >>= m_xSet; + } + catch(const css::uno::Exception&) + { + TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception"); + m_xRoot.clear(); + m_xSet.clear(); + } +} + +/*-************************************************************************************************************ + @short dtor + @descr clean up something + + @attention We implement a write through cache! So we mustn't do it really. All changes was written to cfg directly. + Commit isn't necessary then. + + @seealso baseclass ::utl::ConfigItem + @seealso method IsModified() + @seealso method SetModified() + @seealso method Commit() +*//*-*************************************************************************************************************/ +SvtViewOptionsBase_Impl::~SvtViewOptionsBase_Impl() +{ + // don't flush configuration changes here to m_xRoot. + // That must be done inside every SetXXX() method already ! + // Here it's too late - DisposedExceptions from used configuration access can occur otherwise. + + m_xRoot.clear(); + m_xSet.clear(); + + #ifdef DEBUG_VIEWOPTIONS + _LOG_COUNTER_( m_sListName, m_nReadCount, m_nWriteCount ) + #endif // DEBUG_VIEWOPTIONS +} + +/*-************************************************************************************************************ + @short checks for already existing entries + @descr If user don't know, if an entry already exist - he can get this information by calling this method. + + @seealso member m_aList + + @param "sName", name of entry to check exist state + @return true , if item exist + false, otherwise +*//*-*************************************************************************************************************/ +bool SvtViewOptionsBase_Impl::Exists( const OUString& sName ) +{ + bool bExists = false; + + try + { + if (m_xSet.is()) + bExists = m_xSet->hasByName(sName); + } + catch(const css::uno::Exception&) + { + TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception"); + bExists = false; + } + + return bExists; +} + +/*-************************************************************************************************************ + @short delete entry + @descr Use it to delete set entry by given name. + + @seealso member m_aList + + @param "sName", name of entry to delete it +*//*-*************************************************************************************************************/ +void SvtViewOptionsBase_Impl::Delete( const OUString& sName ) +{ + #ifdef DEBUG_VIEWOPTIONS + ++m_nWriteCount; + #endif + + try + { + css::uno::Reference< css::container::XNameContainer > xSet(m_xSet, css::uno::UNO_QUERY_THROW); + xSet->removeByName(sName); + ::comphelper::ConfigurationHelper::flush(m_xRoot); + } + catch(const css::container::NoSuchElementException&) + { } + catch(const css::uno::Exception&) + { + TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception"); + } +} + +/*-************************************************************************************************************ + @short read/write access to cache view items and her properties + @descr Follow methods support read/write access to all cache view items. + + @seealso member m_sList +*//*-*************************************************************************************************************/ +OUString SvtViewOptionsBase_Impl::GetWindowState( const OUString& sName ) +{ + #ifdef DEBUG_VIEWOPTIONS + ++m_nReadCount; + #endif + + OUString sWindowState; + try + { + css::uno::Reference< css::beans::XPropertySet > xNode( + impl_getSetNode(sName, false), + css::uno::UNO_QUERY); + if (xNode.is()) + xNode->getPropertyValue(PROPERTY_WINDOWSTATE) >>= sWindowState; + } + catch(const css::uno::Exception&) + { + TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception"); + sWindowState.clear(); + } + + return sWindowState; +} + +void SvtViewOptionsBase_Impl::SetWindowState( const OUString& sName , + const OUString& sState ) +{ + #ifdef DEBUG_VIEWOPTIONS + ++m_nWriteCount; + #endif + + try + { + css::uno::Reference< css::beans::XPropertySet > xNode( + impl_getSetNode(sName, true), + css::uno::UNO_QUERY_THROW); + xNode->setPropertyValue(PROPERTY_WINDOWSTATE, css::uno::makeAny(sState)); + ::comphelper::ConfigurationHelper::flush(m_xRoot); + } + catch(const css::uno::Exception&) + { + TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception"); + } +} + +css::uno::Sequence< css::beans::NamedValue > SvtViewOptionsBase_Impl::GetUserData( const OUString& sName ) +{ + #ifdef DEBUG_VIEWOPTIONS + ++m_nReadCount; + #endif + + try + { + css::uno::Reference< css::container::XNameAccess > xNode( + impl_getSetNode(sName, false), + css::uno::UNO_QUERY); // no _THROW ! because we don't create missing items here. So we have to live with zero references .-) + css::uno::Reference< css::container::XNameAccess > xUserData; + if (xNode.is()) + xNode->getByName(PROPERTY_USERDATA) >>= xUserData; + if (xUserData.is()) + { + const css::uno::Sequence<OUString> lNames = xUserData->getElementNames(); + sal_Int32 c = lNames.getLength(); + css::uno::Sequence< css::beans::NamedValue > lUserData(c); + + std::transform(lNames.begin(), lNames.end(), lUserData.begin(), + [&xUserData](const OUString& rName) -> css::beans::NamedValue { + return { rName, xUserData->getByName(rName) }; }); + + return lUserData; + } + } + catch(const css::uno::Exception&) + { + TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception"); + } + + return css::uno::Sequence< css::beans::NamedValue >(); +} + +void SvtViewOptionsBase_Impl::SetUserData( const OUString& sName , + const css::uno::Sequence< css::beans::NamedValue >& lData ) +{ + #ifdef DEBUG_VIEWOPTIONS + ++m_nWriteCount; + #endif + + try + { + css::uno::Reference< css::container::XNameAccess > xNode( + impl_getSetNode(sName, true), + css::uno::UNO_QUERY_THROW); + css::uno::Reference< css::container::XNameContainer > xUserData; + xNode->getByName(PROPERTY_USERDATA) >>= xUserData; + if (xUserData.is()) + { + for (const css::beans::NamedValue& rData : lData) + { + if (xUserData->hasByName(rData.Name)) + xUserData->replaceByName(rData.Name, rData.Value); + else + xUserData->insertByName(rData.Name, rData.Value); + } + } + ::comphelper::ConfigurationHelper::flush(m_xRoot); + } + catch(const css::uno::Exception&) + { + TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception"); + } +} + +css::uno::Any SvtViewOptionsBase_Impl::GetUserItem( const OUString& sName , + const OUString& sItem ) +{ + #ifdef DEBUG_VIEWOPTIONS + ++m_nReadCount; + #endif + + css::uno::Any aItem; + try + { + css::uno::Reference< css::container::XNameAccess > xNode( + impl_getSetNode(sName, false), + css::uno::UNO_QUERY); + css::uno::Reference< css::container::XNameAccess > xUserData; + if (xNode.is()) + xNode->getByName(PROPERTY_USERDATA) >>= xUserData; + if (xUserData.is()) + aItem = xUserData->getByName(sItem); + } + catch(const css::container::NoSuchElementException&) + { aItem.clear(); } + catch(const css::uno::Exception&) + { + TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception"); + aItem.clear(); + } + + return aItem; +} + +void SvtViewOptionsBase_Impl::SetUserItem( const OUString& sName , + const OUString& sItem , + const css::uno::Any& aValue ) +{ + #ifdef DEBUG_VIEWOPTIONS + ++m_nWriteCount; + #endif + + try + { + css::uno::Reference< css::container::XNameAccess > xNode( + impl_getSetNode(sName, true), + css::uno::UNO_QUERY_THROW); + css::uno::Reference< css::container::XNameContainer > xUserData; + xNode->getByName(PROPERTY_USERDATA) >>= xUserData; + if (xUserData.is()) + { + if (xUserData->hasByName(sItem)) + xUserData->replaceByName(sItem, aValue); + else + xUserData->insertByName(sItem, aValue); + } + ::comphelper::ConfigurationHelper::flush(m_xRoot); + } + catch(const css::uno::Exception&) + { + TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception"); + } +} + +OString SvtViewOptionsBase_Impl::GetPageID( const OUString& sName ) +{ + #ifdef DEBUG_VIEWOPTIONS + ++m_nReadCount; + #endif + + OUString sID; + try + { + css::uno::Reference< css::beans::XPropertySet > xNode( + impl_getSetNode(sName, false), + css::uno::UNO_QUERY); + if (xNode.is()) + xNode->getPropertyValue(PROPERTY_PAGEID) >>= sID; + } + catch(const css::uno::Exception&) + { + TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception"); + } + + return sID.toUtf8(); +} + +void SvtViewOptionsBase_Impl::SetPageID( const OUString& sName , + const OString& sID ) +{ + #ifdef DEBUG_VIEWOPTIONS + ++m_nWriteCount; + #endif + + try + { + css::uno::Reference< css::beans::XPropertySet > xNode( + impl_getSetNode(sName, true), + css::uno::UNO_QUERY_THROW); + xNode->setPropertyValue(PROPERTY_PAGEID, css::uno::makeAny(OUString::fromUtf8(sID))); + ::comphelper::ConfigurationHelper::flush(m_xRoot); + } + catch(const css::uno::Exception&) + { + TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception"); + } +} + +SvtViewOptionsBase_Impl::State SvtViewOptionsBase_Impl::GetVisible( const OUString& sName ) +{ + #ifdef DEBUG_VIEWOPTIONS + ++m_nReadCount; + #endif + + State eState = STATE_NONE; + try + { + css::uno::Reference< css::beans::XPropertySet > xNode( + impl_getSetNode(sName, false), + css::uno::UNO_QUERY); + if (xNode.is()) + { + bool bVisible = false; + if (xNode->getPropertyValue(PROPERTY_VISIBLE) >>= bVisible) + { + eState = bVisible ? STATE_TRUE : STATE_FALSE; + } + } + } + catch(const css::uno::Exception&) + { + TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception"); + } + + return eState; +} + +void SvtViewOptionsBase_Impl::SetVisible( const OUString& sName , + bool bVisible ) +{ + #ifdef DEBUG_VIEWOPTIONS + ++m_nWriteCount; + #endif + + try + { + css::uno::Reference< css::beans::XPropertySet > xNode( + impl_getSetNode(sName, true), + css::uno::UNO_QUERY_THROW); + xNode->setPropertyValue(PROPERTY_VISIBLE, css::uno::makeAny(bVisible)); + ::comphelper::ConfigurationHelper::flush(m_xRoot); + } + catch(const css::uno::Exception&) + { + TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception"); + } +} + +/*-************************************************************************************************************ + @short create new set node with default values on disk + @descr To create a new UserData item - the super node of these property must already exist! + You can call this method to create these new entry with default values and change UserData then. + + @seealso method impl_writeDirectProp() + + @param "sNode", name of new entry +*//*-*************************************************************************************************************/ +css::uno::Reference< css::uno::XInterface > SvtViewOptionsBase_Impl::impl_getSetNode( const OUString& sNode , + bool bCreateIfMissing) +{ + css::uno::Reference< css::uno::XInterface > xNode; + + try + { + if (bCreateIfMissing) + xNode = ::comphelper::ConfigurationHelper::makeSureSetNodeExists(m_xRoot, m_sListName, sNode); + else + { + if (m_xSet.is() && m_xSet->hasByName(sNode) ) + m_xSet->getByName(sNode) >>= xNode; + } + } + catch(const css::container::NoSuchElementException&) + { xNode.clear(); } + catch(const css::uno::Exception&) + { + TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception"); + xNode.clear(); + } + + return xNode; +} + +// constructor + +SvtViewOptions::SvtViewOptions( EViewType eType , + const OUString& sViewName ) + : m_eViewType ( eType ) + , m_sViewName ( sViewName ) +{ + // Global access, must be guarded (multithreading!) + ::osl::MutexGuard aGuard( GetOwnStaticMutex() ); + + // Search for right dat container for this view type and initialize right data container or set right ref count! + switch( eType ) + { + case EViewType::Dialog: { + // Increase ref count for dialog data container first. + ++m_nRefCount_Dialogs; + // If these instance the first user of the dialog data container - create these impl static container! + if( m_nRefCount_Dialogs == 1 ) + { + //m_pDataContainer_Dialogs = new SvtViewDialogOptions_Impl( LIST_DIALOGS ); + m_pDataContainer_Dialogs = new SvtViewOptionsBase_Impl( LIST_DIALOGS ); + ItemHolder1::holdConfigItem(EItem::ViewOptionsDialog); + } + } + break; + case EViewType::TabDialog: { + // Increase ref count for tab-dialog data container first. + ++m_nRefCount_TabDialogs; + // If these instance the first user of the tab-dialog data container - create these impl static container! + if( m_nRefCount_TabDialogs == 1 ) + { + m_pDataContainer_TabDialogs = new SvtViewOptionsBase_Impl( LIST_TABDIALOGS ); + ItemHolder1::holdConfigItem(EItem::ViewOptionsTabDialog); + } + } + break; + case EViewType::TabPage:{ + // Increase ref count for tab-page data container first. + ++m_nRefCount_TabPages; + // If these instance the first user of the tab-page data container - create these impl static container! + if( m_nRefCount_TabPages == 1 ) + { + m_pDataContainer_TabPages = new SvtViewOptionsBase_Impl( LIST_TABPAGES ); + ItemHolder1::holdConfigItem(EItem::ViewOptionsTabPage); + } + } + break; + case EViewType::Window: { + // Increase ref count for window data container first. + ++m_nRefCount_Windows; + // If these instance the first user of the window data container - create these impl static container! + if( m_nRefCount_Windows == 1 ) + { + m_pDataContainer_Windows = new SvtViewOptionsBase_Impl( LIST_WINDOWS ); + ItemHolder1::holdConfigItem(EItem::ViewOptionsWindow); + } + } + break; + default : OSL_FAIL( "SvtViewOptions::SvtViewOptions()\nThese view type is unknown! All following calls at these instance will do nothing!" ); + } +} + +// destructor + +SvtViewOptions::~SvtViewOptions() +{ + // Global access, must be guarded (multithreading!) + ::osl::MutexGuard aGuard( GetOwnStaticMutex() ); + + // Search for right dat container for this view type and deinitialize right data container or set right ref count! + switch( m_eViewType ) + { + case EViewType::Dialog: { + // Decrease ref count for dialog data container first. + --m_nRefCount_Dialogs; + // If these instance the last user of the dialog data container - delete these impl static container! + if( m_nRefCount_Dialogs == 0 ) + { + delete m_pDataContainer_Dialogs; + m_pDataContainer_Dialogs = nullptr; + } + } + break; + case EViewType::TabDialog: { + // Decrease ref count for tab-dialog data container first. + --m_nRefCount_TabDialogs; + // If these instance the last user of the tab-dialog data container - delete these impl static container! + if( m_nRefCount_TabDialogs == 0 ) + { + delete m_pDataContainer_TabDialogs; + m_pDataContainer_TabDialogs = nullptr; + } + } + break; + case EViewType::TabPage:{ + // Decrease ref count for tab-page data container first. + --m_nRefCount_TabPages; + // If these instance the last user of the tab-page data container - delete these impl static container! + if( m_nRefCount_TabPages == 0 ) + { + delete m_pDataContainer_TabPages; + m_pDataContainer_TabPages = nullptr; + } + } + break; + case EViewType::Window: { + // Decrease ref count for window data container first. + --m_nRefCount_Windows; + // If these instance the last user of the window data container - delete these impl static container! + if( m_nRefCount_Windows == 0 ) + { + delete m_pDataContainer_Windows; + m_pDataContainer_Windows = nullptr; + } + } + break; + } +} + +// public method + +bool SvtViewOptions::Exists() const +{ + // Ready for multithreading + ::osl::MutexGuard aGuard( GetOwnStaticMutex() ); + + bool bExists = false; + switch( m_eViewType ) + { + case EViewType::Dialog: { + bExists = m_pDataContainer_Dialogs->Exists( m_sViewName ); + } + break; + case EViewType::TabDialog: { + bExists = m_pDataContainer_TabDialogs->Exists( m_sViewName ); + } + break; + case EViewType::TabPage:{ + bExists = m_pDataContainer_TabPages->Exists( m_sViewName ); + } + break; + case EViewType::Window: { + bExists = m_pDataContainer_Windows->Exists( m_sViewName ); + } + break; + } + return bExists; +} + +// public method + +void SvtViewOptions::Delete() +{ + // Ready for multithreading + ::osl::MutexGuard aGuard( GetOwnStaticMutex() ); + + switch( m_eViewType ) + { + case EViewType::Dialog : m_pDataContainer_Dialogs->Delete( m_sViewName ); + break; + case EViewType::TabDialog : m_pDataContainer_TabDialogs->Delete( m_sViewName ); + break; + case EViewType::TabPage : m_pDataContainer_TabPages->Delete( m_sViewName ); + break; + case EViewType::Window : m_pDataContainer_Windows->Delete( m_sViewName ); + break; + } +} + +// public method + +OUString SvtViewOptions::GetWindowState() const +{ + // Ready for multithreading + ::osl::MutexGuard aGuard( GetOwnStaticMutex() ); + + OUString sState; + switch( m_eViewType ) + { + case EViewType::Dialog: { + sState = m_pDataContainer_Dialogs->GetWindowState( m_sViewName ); + } + break; + case EViewType::TabDialog:{ + sState = m_pDataContainer_TabDialogs->GetWindowState( m_sViewName ); + } + break; + case EViewType::TabPage:{ + sState = m_pDataContainer_TabPages->GetWindowState( m_sViewName ); + } + break; + case EViewType::Window: { + sState = m_pDataContainer_Windows->GetWindowState( m_sViewName ); + } + break; + } + return sState; +} + +// public method + +void SvtViewOptions::SetWindowState( const OUString& sState ) +{ + // Ready for multithreading + ::osl::MutexGuard aGuard( GetOwnStaticMutex() ); + + switch( m_eViewType ) + { + case EViewType::Dialog: { + m_pDataContainer_Dialogs->SetWindowState( m_sViewName, sState ); + } + break; + case EViewType::TabDialog: { + m_pDataContainer_TabDialogs->SetWindowState( m_sViewName, sState ); + } + break; + case EViewType::TabPage:{ + m_pDataContainer_TabPages->SetWindowState( m_sViewName, sState ); + } + break; + case EViewType::Window: { + m_pDataContainer_Windows->SetWindowState( m_sViewName, sState ); + } + break; + } +} + +// public method + +OString SvtViewOptions::GetPageID() const +{ + // Ready for multithreading + ::osl::MutexGuard aGuard( GetOwnStaticMutex() ); + + // Safe impossible cases. + // These call isn't allowed for dialogs, tab-pages or windows! + OSL_ENSURE( !(m_eViewType==EViewType::Dialog||m_eViewType==EViewType::TabPage||m_eViewType==EViewType::Window), "SvtViewOptions::GetPageID()\nCall not allowed for Dialogs, TabPages or Windows! I do nothing!" ); + + OString sID; + if( m_eViewType == EViewType::TabDialog ) + sID = m_pDataContainer_TabDialogs->GetPageID( m_sViewName ); + return sID; +} + +// public method + +void SvtViewOptions::SetPageID(const OString& rID) +{ + // Ready for multithreading + ::osl::MutexGuard aGuard( GetOwnStaticMutex() ); + + // Safe impossible cases. + // These call isn't allowed for dialogs, tab-pages or windows! + OSL_ENSURE( !(m_eViewType==EViewType::Dialog||m_eViewType==EViewType::TabPage||m_eViewType==EViewType::Window), "SvtViewOptions::SetPageID()\nCall not allowed for Dialogs, TabPages or Windows! I do nothing!" ); + + if( m_eViewType == EViewType::TabDialog ) + m_pDataContainer_TabDialogs->SetPageID(m_sViewName, rID); +} + +// public method + +bool SvtViewOptions::IsVisible() const +{ + // Ready for multithreading + ::osl::MutexGuard aGuard( GetOwnStaticMutex() ); + + // Safe impossible cases. + // These call isn't allowed for dialogs, tab-dialogs or tab-pages! + OSL_ENSURE( !(m_eViewType==EViewType::Dialog||m_eViewType==EViewType::TabDialog||m_eViewType==EViewType::TabPage), "SvtViewOptions::IsVisible()\nCall not allowed for Dialogs, TabDialogs or TabPages! I do nothing!" ); + + bool bState = false; + if( m_eViewType == EViewType::Window ) + bState = m_pDataContainer_Windows->GetVisible( m_sViewName ) == SvtViewOptionsBase_Impl::STATE_TRUE; + + return bState; +} + +// public method + +void SvtViewOptions::SetVisible( bool bState ) +{ + // Ready for multithreading + ::osl::MutexGuard aGuard( GetOwnStaticMutex() ); + + // Safe impossible cases. + // These call isn't allowed for dialogs, tab-dialogs or tab-pages! + OSL_ENSURE( !(m_eViewType==EViewType::Dialog||m_eViewType==EViewType::TabDialog||m_eViewType==EViewType::TabPage), "SvtViewOptions::SetVisible()\nCall not allowed for Dialogs, TabDialogs or TabPages! I do nothing!" ); + + if( m_eViewType == EViewType::Window ) + m_pDataContainer_Windows->SetVisible( m_sViewName, bState ); +} + +// public method + +bool SvtViewOptions::HasVisible() const +{ + // Ready for multithreading + ::osl::MutexGuard aGuard( GetOwnStaticMutex() ); + + // Safe impossible cases. + // These call isn't allowed for dialogs, tab-dialogs or tab-pages! + OSL_ENSURE( !(m_eViewType==EViewType::Dialog||m_eViewType==EViewType::TabDialog||m_eViewType==EViewType::TabPage), "SvtViewOptions::IsVisible()\nCall not allowed for Dialogs, TabDialogs or TabPages! I do nothing!" ); + + bool bState = false; + if( m_eViewType == EViewType::Window ) + bState = m_pDataContainer_Windows->GetVisible( m_sViewName ) != SvtViewOptionsBase_Impl::STATE_NONE; + + return bState; +} + +css::uno::Sequence< css::beans::NamedValue > SvtViewOptions::GetUserData() const +{ + // Ready for multithreading + ::osl::MutexGuard aGuard( GetOwnStaticMutex() ); + + css::uno::Sequence< css::beans::NamedValue > lData; + switch( m_eViewType ) + { + case EViewType::Dialog: { + lData = m_pDataContainer_Dialogs->GetUserData( m_sViewName ); + } + break; + case EViewType::TabDialog: { + lData = m_pDataContainer_TabDialogs->GetUserData( m_sViewName ); + } + break; + case EViewType::TabPage:{ + lData = m_pDataContainer_TabPages->GetUserData( m_sViewName ); + } + break; + case EViewType::Window: { + lData = m_pDataContainer_Windows->GetUserData( m_sViewName ); + } + break; + } + return lData; +} + +void SvtViewOptions::SetUserData( const css::uno::Sequence< css::beans::NamedValue >& lData ) +{ + // Ready for multithreading + ::osl::MutexGuard aGuard( GetOwnStaticMutex() ); + + switch( m_eViewType ) + { + case EViewType::Dialog: { + m_pDataContainer_Dialogs->SetUserData( m_sViewName, lData ); + } + break; + case EViewType::TabDialog: { + m_pDataContainer_TabDialogs->SetUserData( m_sViewName, lData ); + } + break; + case EViewType::TabPage:{ + m_pDataContainer_TabPages->SetUserData( m_sViewName, lData ); + } + break; + case EViewType::Window: { + m_pDataContainer_Windows->SetUserData( m_sViewName, lData ); + } + break; + } +} + +css::uno::Any SvtViewOptions::GetUserItem( const OUString& sName ) const +{ + // Ready for multithreading + ::osl::MutexGuard aGuard( GetOwnStaticMutex() ); + + css::uno::Any aItem; + switch( m_eViewType ) + { + case EViewType::Dialog: { + aItem = m_pDataContainer_Dialogs->GetUserItem( m_sViewName, sName ); + } + break; + case EViewType::TabDialog: { + aItem = m_pDataContainer_TabDialogs->GetUserItem( m_sViewName, sName ); + } + break; + case EViewType::TabPage:{ + aItem = m_pDataContainer_TabPages->GetUserItem( m_sViewName, sName ); + } + break; + case EViewType::Window: { + aItem = m_pDataContainer_Windows->GetUserItem( m_sViewName, sName ); + } + break; + } + return aItem; +} + +void SvtViewOptions::SetUserItem( const OUString& sName , + const css::uno::Any& aValue ) +{ + // Ready for multithreading + ::osl::MutexGuard aGuard( GetOwnStaticMutex() ); + + switch( m_eViewType ) + { + case EViewType::Dialog: { + m_pDataContainer_Dialogs->SetUserItem( m_sViewName, sName, aValue ); + } + break; + case EViewType::TabDialog: { + m_pDataContainer_TabDialogs->SetUserItem( m_sViewName, sName, aValue ); + } + break; + case EViewType::TabPage:{ + m_pDataContainer_TabPages->SetUserItem( m_sViewName, sName, aValue ); + } + break; + case EViewType::Window: { + m_pDataContainer_Windows->SetUserItem( m_sViewName, sName, aValue ); + } + break; + } +} + +namespace +{ + class theViewOptionsMutex : public rtl::Static<osl::Mutex, theViewOptionsMutex>{}; +} + +// private method + +::osl::Mutex& SvtViewOptions::GetOwnStaticMutex() +{ + return theViewOptionsMutex::get(); +} + +void SvtViewOptions::AcquireOptions() +{ + ::osl::MutexGuard aGuard( GetOwnStaticMutex() ); + if( ++m_nRefCount_Dialogs == 1 ) + { + m_pDataContainer_Dialogs = new SvtViewOptionsBase_Impl( LIST_DIALOGS ); + ItemHolder1::holdConfigItem(EItem::ViewOptionsDialog); + } + if( ++m_nRefCount_TabDialogs == 1 ) + { + m_pDataContainer_TabDialogs = new SvtViewOptionsBase_Impl( LIST_TABDIALOGS ); + ItemHolder1::holdConfigItem(EItem::ViewOptionsTabDialog); + } + if( ++m_nRefCount_TabPages == 1 ) + { + m_pDataContainer_TabPages = new SvtViewOptionsBase_Impl( LIST_TABPAGES ); + ItemHolder1::holdConfigItem(EItem::ViewOptionsTabPage); + } + if( ++m_nRefCount_Windows == 1 ) + { + m_pDataContainer_Windows = new SvtViewOptionsBase_Impl( LIST_WINDOWS ); + ItemHolder1::holdConfigItem(EItem::ViewOptionsWindow); + } +} + +void SvtViewOptions::ReleaseOptions() +{ + ::osl::MutexGuard aGuard( GetOwnStaticMutex() ); + if( --m_nRefCount_Dialogs == 0 ) + { + delete m_pDataContainer_Dialogs; + m_pDataContainer_Dialogs = nullptr; + } + if( --m_nRefCount_TabDialogs == 0 ) + { + delete m_pDataContainer_TabDialogs; + m_pDataContainer_TabDialogs = nullptr; + } + if( --m_nRefCount_TabPages == 0 ) + { + delete m_pDataContainer_TabPages; + m_pDataContainer_TabPages = nullptr; + } + if( --m_nRefCount_Windows == 0 ) + { + delete m_pDataContainer_Windows; + m_pDataContainer_Windows = nullptr; + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/i18n/calendarwrapper.cxx b/unotools/source/i18n/calendarwrapper.cxx new file mode 100644 index 000000000..5f4a1669b --- /dev/null +++ b/unotools/source/i18n/calendarwrapper.cxx @@ -0,0 +1,341 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/log.hxx> +#include <unotools/calendarwrapper.hxx> +#include <com/sun/star/i18n/LocaleCalendar2.hpp> +#include <tools/diagnose_ex.h> + +using namespace ::com::sun::star; +using namespace ::com::sun::star::i18n; +using namespace ::com::sun::star::uno; + +CalendarWrapper::CalendarWrapper( + const Reference< uno::XComponentContext > & rxContext + ) + : + aEpochStart( Date( 1, 1, 1970 ) ) +{ + xC = LocaleCalendar2::create(rxContext); +} + +CalendarWrapper::~CalendarWrapper() +{ +} + +void CalendarWrapper::loadDefaultCalendar( const css::lang::Locale& rLocale, bool bTimeZoneUTC ) +{ + try + { + if ( xC.is() ) + xC->loadDefaultCalendarTZ( rLocale, (bTimeZoneUTC ? "UTC" : OUString())); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "loadDefaultCalendar" ); + } +} + +void CalendarWrapper::loadCalendar( const OUString& rUniqueID, const css::lang::Locale& rLocale, bool bTimeZoneUTC ) +{ + try + { + if ( xC.is() ) + xC->loadCalendarTZ( rUniqueID, rLocale, (bTimeZoneUTC ? "UTC" : OUString())); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "loadCalendar: " + << rUniqueID << " Locale: " << rLocale.Language << "_" << rLocale.Country ); + } +} + +css::uno::Sequence< OUString > CalendarWrapper::getAllCalendars( const css::lang::Locale& rLocale ) const +{ + try + { + if ( xC.is() ) + return xC->getAllCalendars( rLocale ); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getAllCalendars" ); + } + + return css::uno::Sequence< OUString > (0); +} + +OUString CalendarWrapper::getUniqueID() const +{ + try + { + if ( xC.is() ) + return xC->getUniqueID(); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getUniqueID" ); + } + return OUString(); +} + +void CalendarWrapper::setDateTime( double fTimeInDays ) +{ + try + { + if ( xC.is() ) + xC->setDateTime( fTimeInDays ); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "setDateTime" ); + } +} + +double CalendarWrapper::getDateTime() const +{ + try + { + if ( xC.is() ) + return xC->getDateTime(); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getDateTime" ); + } + return 0.0; +} + +void CalendarWrapper::setLocalDateTime( double fTimeInDays ) +{ + try + { + if ( xC.is() ) + { + xC->setLocalDateTime( fTimeInDays ); + } + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "setLocalDateTime" ); + } +} + +double CalendarWrapper::getLocalDateTime() const +{ + try + { + if ( xC.is() ) + { + return xC->getLocalDateTime(); + } + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getLocalDateTime" ); + } + return 0.0; +} + +void CalendarWrapper::setValue( sal_Int16 nFieldIndex, sal_Int16 nValue ) +{ + try + { + if ( xC.is() ) + xC->setValue( nFieldIndex, nValue ); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "setValue" ); + } +} + +bool CalendarWrapper::isValid() const +{ + try + { + if ( xC.is() ) + return xC->isValid(); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "isValid" ); + } + return false; +} + +sal_Int16 CalendarWrapper::getValue( sal_Int16 nFieldIndex ) const +{ + try + { + if ( xC.is() ) + return xC->getValue( nFieldIndex ); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getValue" ); + } + return 0; +} + +sal_Int16 CalendarWrapper::getFirstDayOfWeek() const +{ + try + { + if ( xC.is() ) + return xC->getFirstDayOfWeek(); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getFirstDayOfWeek" ); + } + return 0; +} + +sal_Int16 CalendarWrapper::getNumberOfMonthsInYear() const +{ + try + { + if ( xC.is() ) + return xC->getNumberOfMonthsInYear(); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getNumberOfMonthsInYear" ); + } + return 0; +} + +sal_Int16 CalendarWrapper::getNumberOfDaysInWeek() const +{ + try + { + if ( xC.is() ) + return xC->getNumberOfDaysInWeek(); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getNumberOfDaysInWeek" ); + } + return 0; +} + +css::uno::Sequence< css::i18n::CalendarItem2 > CalendarWrapper::getMonths() const +{ + try + { + if ( xC.is() ) + return xC->getMonths2(); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getMonths" ); + } + return css::uno::Sequence< css::i18n::CalendarItem2 > (0); +} + +css::uno::Sequence< css::i18n::CalendarItem2 > CalendarWrapper::getDays() const +{ + try + { + if ( xC.is() ) + return xC->getDays2(); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getDays" ); + } + return css::uno::Sequence< css::i18n::CalendarItem2 > (0); +} + +OUString CalendarWrapper::getDisplayName( sal_Int16 nCalendarDisplayIndex, sal_Int16 nIdx, sal_Int16 nNameType ) const +{ + try + { + if ( xC.is() ) + return xC->getDisplayName( nCalendarDisplayIndex, nIdx, nNameType ); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getDisplayName" ); + } + return OUString(); +} + +// --- XExtendedCalendar ----------------------------------------------------- + +OUString CalendarWrapper::getDisplayString( sal_Int32 nCalendarDisplayCode, sal_Int16 nNativeNumberMode ) const +{ + try + { + if ( xC.is() ) + return xC->getDisplayString( nCalendarDisplayCode, nNativeNumberMode ); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getDisplayString" ); + } + return OUString(); +} + +// --- XCalendar3 ------------------------------------------------------------ + +css::i18n::Calendar2 CalendarWrapper::getLoadedCalendar() const +{ + try + { + if ( xC.is() ) + return xC->getLoadedCalendar2(); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getLoadedCalendar2" ); + } + return css::i18n::Calendar2(); +} + +css::uno::Sequence< css::i18n::CalendarItem2 > CalendarWrapper::getGenitiveMonths() const +{ + try + { + if ( xC.is() ) + return xC->getGenitiveMonths2(); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getGenitiveMonths" ); + } + return css::uno::Sequence< css::i18n::CalendarItem2 > (0); +} + +css::uno::Sequence< css::i18n::CalendarItem2 > CalendarWrapper::getPartitiveMonths() const +{ + try + { + if ( xC.is() ) + return xC->getPartitiveMonths2(); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getPartitiveMonths" ); + } + return css::uno::Sequence< css::i18n::CalendarItem2 > (0); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/i18n/caserotate.cxx b/unotools/source/i18n/caserotate.cxx new file mode 100644 index 000000000..a242b855e --- /dev/null +++ b/unotools/source/i18n/caserotate.cxx @@ -0,0 +1,43 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + */ + +#include <unotools/caserotate.hxx> +#include <i18nutil/transliteration.hxx> + +//TODO Use XCharacterClassification::getStringType to determine the current +//(possibly mixed) case type and rotate to the next one + +TransliterationFlags RotateTransliteration::getNextMode() +{ + TransliterationFlags nMode = TransliterationFlags::NONE; + + switch (nF3ShiftCounter) + { + case 0: + nMode = TransliterationFlags::TITLE_CASE; + break; + case 1: //tdf#116315 + nMode = TransliterationFlags::SENTENCE_CASE; + break; + case 2: + nMode = TransliterationFlags::LOWERCASE_UPPERCASE; + break; + default: + case 3: + nMode = TransliterationFlags::UPPERCASE_LOWERCASE; + nF3ShiftCounter = -1; + break; + } + + nF3ShiftCounter++; + + return nMode; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/i18n/charclass.cxx b/unotools/source/i18n/charclass.cxx new file mode 100644 index 000000000..faec870ff --- /dev/null +++ b/unotools/source/i18n/charclass.cxx @@ -0,0 +1,456 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <comphelper/processfactory.hxx> +#include <sal/log.hxx> +#include <unotools/charclass.hxx> +#include <rtl/character.hxx> +#include <tools/diagnose_ex.h> + +#include <com/sun/star/i18n/CharacterClassification.hpp> + +using namespace ::com::sun::star; +using namespace ::com::sun::star::i18n; +using namespace ::com::sun::star::uno; + +CharClass::CharClass( + const Reference< uno::XComponentContext > & rxContext, + const LanguageTag& rLanguageTag + ) + : + maLanguageTag( rLanguageTag) +{ + xCC = CharacterClassification::create( rxContext ); +} + +CharClass::CharClass( + const LanguageTag& rLanguageTag ) + : + maLanguageTag( rLanguageTag) +{ + xCC = CharacterClassification::create( comphelper::getProcessComponentContext() ); +} + +CharClass::~CharClass() +{ +} + +void CharClass::setLanguageTag( const LanguageTag& rLanguageTag ) +{ + ::osl::MutexGuard aGuard( aMutex ); + maLanguageTag = rLanguageTag; +} + +const LanguageTag& CharClass::getLanguageTag() const +{ + ::osl::MutexGuard aGuard( aMutex ); + return maLanguageTag; +} + +const css::lang::Locale& CharClass::getMyLocale() const +{ + // Mutex locked by callers. + return maLanguageTag.getLocale(); +} + +// static +bool CharClass::isAsciiNumeric( const OUString& rStr ) +{ + if ( rStr.isEmpty() ) + return false; + const sal_Unicode* p = rStr.getStr(); + const sal_Unicode* const pStop = p + rStr.getLength(); + + do + { + if ( !rtl::isAsciiDigit( *p ) ) + return false; + } + while ( ++p < pStop ); + + return true; +} + +// static +bool CharClass::isAsciiAlpha( const OUString& rStr ) +{ + if ( rStr.isEmpty() ) + return false; + const sal_Unicode* p = rStr.getStr(); + const sal_Unicode* const pStop = p + rStr.getLength(); + + do + { + if ( !rtl::isAsciiAlpha( *p ) ) + return false; + } + while ( ++p < pStop ); + + return true; +} + +bool CharClass::isAlpha( const OUString& rStr, sal_Int32 nPos ) const +{ + sal_Unicode c = rStr[nPos]; + if ( c < 128 ) + return rtl::isAsciiAlpha( c ); + + try + { + if ( xCC.is() ) + { + ::osl::MutexGuard aGuard( aMutex ); + return (xCC->getCharacterType( rStr, nPos, getMyLocale() ) & + nCharClassAlphaType) != 0; + } + } + catch ( const Exception& ) + { + SAL_WARN( "unotools.i18n", "isAlpha: Exception caught!" ); + } + return false; +} + +bool CharClass::isLetter( const OUString& rStr, sal_Int32 nPos ) const +{ + sal_Unicode c = rStr[nPos]; + if ( c < 128 ) + return rtl::isAsciiAlpha( c ); + + try + { + if ( xCC.is() ) + { + ::osl::MutexGuard aGuard( aMutex ); + return (xCC->getCharacterType( rStr, nPos, getMyLocale() ) & + nCharClassLetterType) != 0; + } + } + catch ( const Exception& ) + { + SAL_WARN( "unotools.i18n", "isLetter: Exception caught!" ); + } + return false; +} + +bool CharClass::isLetter( const OUString& rStr ) const +{ + try + { + if ( xCC.is() ) + { + ::osl::MutexGuard aGuard( aMutex ); + return isLetterType( xCC->getStringType( rStr, 0, rStr.getLength(), getMyLocale() ) ); + } + } + catch ( const Exception& ) + { + SAL_WARN( "unotools.i18n", "isLetter: Exception caught!" ); + } + return false; +} + +bool CharClass::isDigit( const OUString& rStr, sal_Int32 nPos ) const +{ + sal_Unicode c = rStr[ nPos ]; + if ( c < 128 ) + return rtl::isAsciiDigit( c ); + + try + { + if ( xCC.is() ) + { + ::osl::MutexGuard aGuard( aMutex ); + return (xCC->getCharacterType( rStr, nPos, getMyLocale() ) & + KCharacterType::DIGIT) != 0; + } + } + catch ( const Exception& ) + { + SAL_WARN( "unotools.i18n", "isDigit: Exception caught!" ); + } + return false; +} + +bool CharClass::isNumeric( const OUString& rStr ) const +{ + try + { + if ( xCC.is() ) + { + ::osl::MutexGuard aGuard( aMutex ); + return isNumericType( xCC->getStringType( rStr, 0, rStr.getLength(), getMyLocale() ) ); + } + } + catch ( const Exception& ) + { + SAL_WARN( "unotools.i18n", "isNumeric: Exception caught!" ); + } + return false; +} + +bool CharClass::isAlphaNumeric( const OUString& rStr, sal_Int32 nPos ) const +{ + sal_Unicode c = rStr[nPos]; + if ( c < 128 ) + return rtl::isAsciiAlphanumeric( c ); + + try + { + if ( xCC.is() ) + { + ::osl::MutexGuard aGuard( aMutex ); + return (xCC->getCharacterType( rStr, nPos, getMyLocale() ) & + (nCharClassAlphaType | KCharacterType::DIGIT)) != 0; + } + } + catch ( const Exception& ) + { + SAL_WARN( "unotools.i18n", "isAlphaNumeric: Exception caught!" ); + } + return false; +} + +bool CharClass::isLetterNumeric( const OUString& rStr, sal_Int32 nPos ) const +{ + sal_Unicode c = rStr[nPos]; + if ( c < 128 ) + return rtl::isAsciiAlphanumeric( c ); + + try + { + if ( xCC.is() ) + { + ::osl::MutexGuard aGuard( aMutex ); + return (xCC->getCharacterType( rStr, nPos, getMyLocale() ) & + (nCharClassLetterType | KCharacterType::DIGIT)) != 0; + } + } + catch ( const Exception& ) + { + SAL_WARN( "unotools.i18n", "isLetterNumeric: Exception caught!" ); + } + return false; +} + +bool CharClass::isLetterNumeric( const OUString& rStr ) const +{ + try + { + if ( xCC.is() ) + { + ::osl::MutexGuard aGuard( aMutex ); + return isLetterNumericType( xCC->getStringType( rStr, 0, rStr.getLength(), getMyLocale() ) ); + } + } + catch ( const Exception& ) + { + SAL_WARN( "unotools.i18n", "isLetterNumeric: Exception caught!" ); + } + return false; +} + +OUString CharClass::titlecase(const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount) const +{ + try + { + if ( xCC.is() ) + { + ::osl::MutexGuard aGuard( aMutex ); + return xCC->toTitle( rStr, nPos, nCount, getMyLocale() ); + } + } + catch ( const Exception& ) + { + SAL_WARN( "unotools.i18n", "titlecase: Exception caught!" ); + } + return rStr.copy( nPos, nCount ); +} + +OUString CharClass::uppercase( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const +{ + try + { + if ( xCC.is() ) + { + ::osl::MutexGuard aGuard( aMutex ); + return xCC->toUpper( rStr, nPos, nCount, getMyLocale() ); + } + } + catch ( const Exception& ) + { + SAL_WARN( "unotools.i18n", "uppercase: Exception caught!" ); + } + return rStr.copy( nPos, nCount ); +} + +OUString CharClass::lowercase( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const +{ + try + { + if ( xCC.is() ) + { + ::osl::MutexGuard aGuard( aMutex ); + return xCC->toLower( rStr, nPos, nCount, getMyLocale() ); + } + } + catch ( const Exception& ) + { + SAL_WARN( "unotools.i18n", "lowercase: Exception caught!" ); + } + return rStr.copy( nPos, nCount ); +} + +sal_Int16 CharClass::getType( const OUString& rStr, sal_Int32 nPos ) const +{ + try + { + if ( xCC.is() ) + { + ::osl::MutexGuard aGuard( aMutex ); + return xCC->getType( rStr, nPos ); + } + } + catch ( const Exception& ) + { + SAL_WARN( "unotools.i18n", "getType: Exception caught!" ); + } + return 0; +} + +css::i18n::DirectionProperty CharClass::getCharacterDirection( const OUString& rStr, sal_Int32 nPos ) const +{ + try + { + if ( xCC.is() ) + { + ::osl::MutexGuard aGuard( aMutex ); + return static_cast<css::i18n::DirectionProperty>(xCC->getCharacterDirection( rStr, nPos )); + } + } + catch ( const Exception& ) + { + SAL_WARN( "unotools.i18n", "getCharacterDirection: Exception caught!" ); + } + return css::i18n::DirectionProperty_LEFT_TO_RIGHT; +} + +css::i18n::UnicodeScript CharClass::getScript( const OUString& rStr, sal_Int32 nPos ) const +{ + try + { + if ( xCC.is() ) + { + ::osl::MutexGuard aGuard( aMutex ); + return static_cast<css::i18n::UnicodeScript>(xCC->getScript( rStr, nPos )); + } + } + catch ( const Exception& ) + { + SAL_WARN( "unotools.i18n", "getScript: Exception caught!" ); + } + return UnicodeScript_kBasicLatin; +} + +sal_Int32 CharClass::getCharacterType( const OUString& rStr, sal_Int32 nPos ) const +{ + try + { + if ( xCC.is() ) + { + ::osl::MutexGuard aGuard( aMutex ); + return xCC->getCharacterType( rStr, nPos, getMyLocale() ); + } + } + catch ( const Exception& ) + { + SAL_WARN( "unotools.i18n", "getCharacterType: Exception caught!" ); + } + return 0; +} + +sal_Int32 CharClass::getStringType( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const +{ + try + { + if ( xCC.is() ) + { + ::osl::MutexGuard aGuard( aMutex ); + return xCC->getStringType( rStr, nPos, nCount, getMyLocale() ); + } + } + catch ( const Exception& ) + { + SAL_WARN( "unotools.i18n", "getStringType: Exception caught!" ); + } + return 0; +} + +css::i18n::ParseResult CharClass::parseAnyToken( + const OUString& rStr, + sal_Int32 nPos, + sal_Int32 nStartCharFlags, + const OUString& userDefinedCharactersStart, + sal_Int32 nContCharFlags, + const OUString& userDefinedCharactersCont ) const +{ + try + { + if ( xCC.is() ) + { + ::osl::MutexGuard aGuard( aMutex ); + return xCC->parseAnyToken( rStr, nPos, getMyLocale(), + nStartCharFlags, userDefinedCharactersStart, + nContCharFlags, userDefinedCharactersCont ); + } + } + catch ( const Exception& ) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "parseAnyToken" ); + } + return ParseResult(); +} + +css::i18n::ParseResult CharClass::parsePredefinedToken( + sal_Int32 nTokenType, + const OUString& rStr, + sal_Int32 nPos, + sal_Int32 nStartCharFlags, + const OUString& userDefinedCharactersStart, + sal_Int32 nContCharFlags, + const OUString& userDefinedCharactersCont ) const +{ + try + { + if ( xCC.is() ) + { + ::osl::MutexGuard aGuard( aMutex ); + return xCC->parsePredefinedToken( nTokenType, rStr, nPos, getMyLocale(), + nStartCharFlags, userDefinedCharactersStart, + nContCharFlags, userDefinedCharactersCont ); + } + } + catch ( const Exception& ) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "parsePredefinedToken" ); + } + return ParseResult(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/i18n/collatorwrapper.cxx b/unotools/source/i18n/collatorwrapper.cxx new file mode 100644 index 000000000..4da1398e0 --- /dev/null +++ b/unotools/source/i18n/collatorwrapper.cxx @@ -0,0 +1,97 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <sal/log.hxx> +#include <unotools/collatorwrapper.hxx> +#include <com/sun/star/i18n/Collator.hpp> + +using namespace ::com::sun::star; + +CollatorWrapper::CollatorWrapper ( const uno::Reference< uno::XComponentContext > &rxContext ) +{ + mxInternationalCollator = i18n::Collator::create( rxContext ); +} + +sal_Int32 +CollatorWrapper::compareString (const OUString& s1, const OUString& s2) const +{ + try + { + if (mxInternationalCollator.is()) + return mxInternationalCollator->compareString (s1, s2); + } + catch (const uno::RuntimeException&) + { + SAL_WARN( "unotools.i18n","CollatorWrapper: compareString failed"); + } + + return 0; +} + +uno::Sequence< OUString > +CollatorWrapper::listCollatorAlgorithms (const lang::Locale& rLocale) const +{ + try + { + if (mxInternationalCollator.is()) + return mxInternationalCollator->listCollatorAlgorithms (rLocale); + } + catch (const uno::RuntimeException&) + { + SAL_WARN( "unotools.i18n","CollatorWrapper: listCollatorAlgorithms failed"); + } + + return uno::Sequence< OUString > (); +} + +sal_Int32 +CollatorWrapper::loadDefaultCollator (const lang::Locale& rLocale, sal_Int32 nOptions) +{ + try + { + if (mxInternationalCollator.is()) + return mxInternationalCollator->loadDefaultCollator (rLocale, nOptions); + } + catch (const uno::RuntimeException&) + { + SAL_WARN( "unotools.i18n","CollatorWrapper: loadDefaultCollator failed"); + } + + return 0; +} + +void +CollatorWrapper::loadCollatorAlgorithm (const OUString& rAlgorithm, + const lang::Locale& rLocale, sal_Int32 nOptions) +{ + try + { + if (mxInternationalCollator.is()) + mxInternationalCollator->loadCollatorAlgorithm ( + rAlgorithm, rLocale, nOptions); + } + catch (const uno::RuntimeException&) + { + SAL_WARN( "unotools.i18n","CollatorWrapper: loadCollatorAlgorithm failed"); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/i18n/intlwrapper.cxx b/unotools/source/i18n/intlwrapper.cxx new file mode 100644 index 000000000..f15869fb6 --- /dev/null +++ b/unotools/source/i18n/intlwrapper.cxx @@ -0,0 +1,59 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unotools/intlwrapper.hxx> +#include <unotools/collatorwrapper.hxx> +#include <unotools/localedatawrapper.hxx> +#include <com/sun/star/i18n/CollatorOptions.hpp> +#include <comphelper/processfactory.hxx> + +IntlWrapper::IntlWrapper( + const LanguageTag& rLanguageTag ) + : + maLanguageTag( rLanguageTag ), + m_xContext( comphelper::getProcessComponentContext() ) +{ +} + +IntlWrapper::~IntlWrapper() +{ +} + +void IntlWrapper::ImplNewLocaleData() const +{ + const_cast<IntlWrapper*>(this)->pLocaleData.reset( new LocaleDataWrapper( m_xContext, maLanguageTag ) ); +} + +void IntlWrapper::ImplNewCollator( bool bCaseSensitive ) const +{ + CollatorWrapper* p = new CollatorWrapper( m_xContext ); + if ( bCaseSensitive ) + { + p->loadDefaultCollator( maLanguageTag.getLocale(), 0 ); + const_cast<IntlWrapper*>(this)->pCaseCollator.reset(p); + } + else + { + p->loadDefaultCollator( maLanguageTag.getLocale(), + css::i18n::CollatorOptions::CollatorOptions_IGNORE_CASE ); + const_cast<IntlWrapper*>(this)->pCollator.reset(p); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/i18n/localedatawrapper.cxx b/unotools/source/i18n/localedatawrapper.cxx new file mode 100644 index 000000000..f9ade4996 --- /dev/null +++ b/unotools/source/i18n/localedatawrapper.cxx @@ -0,0 +1,1826 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <stdio.h> +#include <string> + +#include <sal/log.hxx> +#include <unotools/localedatawrapper.hxx> +#include <unotools/calendarwrapper.hxx> +#include <unotools/digitgroupingiterator.hxx> +#include <tools/diagnose_ex.h> +#include <tools/debug.hxx> +#include <i18nlangtag/languagetag.hxx> + +#include <com/sun/star/i18n/KNumberFormatUsage.hpp> +#include <com/sun/star/i18n/KNumberFormatType.hpp> +#include <com/sun/star/i18n/LocaleData2.hpp> +#include <com/sun/star/i18n/CalendarFieldIndex.hpp> +#include <com/sun/star/i18n/CalendarDisplayIndex.hpp> +#include <com/sun/star/i18n/NumberFormatIndex.hpp> +#include <com/sun/star/i18n/NumberFormatMapper.hpp> + +#include <comphelper/processfactory.hxx> +#include <rtl/instance.hxx> +#include <rtl/ustrbuf.hxx> +#include <rtl/math.hxx> + +static const sal_uInt16 nCurrFormatInvalid = 0xffff; +static const sal_uInt16 nCurrFormatDefault = 0; + +using namespace ::com::sun::star; +using namespace ::com::sun::star::i18n; +using namespace ::com::sun::star::uno; + +namespace +{ + struct InstalledLocales + : public rtl::Static< + uno::Sequence< lang::Locale >, InstalledLocales > + {}; + + struct InstalledLanguageTypes + : public rtl::Static< + std::vector< LanguageType >, InstalledLanguageTypes > + {}; +} + +bool LocaleDataWrapper::Locale_Compare::operator()(const css::lang::Locale& rLocale1, const css::lang::Locale& rLocale2) const +{ + if (rLocale1.Language < rLocale2.Language) + return true; + else if (rLocale1.Language > rLocale2.Language) + return false; + + if (rLocale1.Country < rLocale2.Country) + return true; + else if (rLocale1.Country > rLocale2.Country) + return false; + + return rLocale1.Variant < rLocale2.Variant; +} + +sal_uInt8 LocaleDataWrapper::nLocaleDataChecking = 0; + +LocaleDataWrapper::LocaleDataWrapper( + const Reference< uno::XComponentContext > & rxContext, + const LanguageTag& rLanguageTag + ) + : + m_xContext( rxContext ), + xLD( LocaleData2::create(rxContext) ), + maLanguageTag( rLanguageTag ), + bLocaleDataItemValid( false ), + bReservedWordValid( false ), + bSecondaryCalendarValid( false ) +{ + invalidateData(); +} + +LocaleDataWrapper::LocaleDataWrapper( + const LanguageTag& rLanguageTag + ) + : + m_xContext( comphelper::getProcessComponentContext() ), + xLD( LocaleData2::create(m_xContext) ), + maLanguageTag( rLanguageTag ), + bLocaleDataItemValid( false ), + bReservedWordValid( false ), + bSecondaryCalendarValid( false ) +{ + invalidateData(); +} + +LocaleDataWrapper::~LocaleDataWrapper() +{ +} + +void LocaleDataWrapper::setLanguageTag( const LanguageTag& rLanguageTag ) +{ + ::utl::ReadWriteGuard aGuard( aMutex, ReadWriteGuardMode::CriticalChange ); + maLanguageTag = rLanguageTag; + invalidateData(); +} + +const LanguageTag& LocaleDataWrapper::getLanguageTag() const +{ + ::utl::ReadWriteGuard aGuard( aMutex ); + return maLanguageTag; +} + +const css::lang::Locale& LocaleDataWrapper::getMyLocale() const +{ + ::utl::ReadWriteGuard aGuard( aMutex ); + return maLanguageTag.getLocale(); +} + +void LocaleDataWrapper::invalidateData() +{ + aCurrSymbol.clear(); + aCurrBankSymbol.clear(); + nDateOrder = nLongDateOrder = DateOrder::Invalid; + nCurrPositiveFormat = nCurrNegativeFormat = nCurrDigits = nCurrFormatInvalid; + if ( bLocaleDataItemValid ) + { + for (OUString & j : aLocaleItem) + j.clear(); + bLocaleDataItemValid = false; + } + if ( bReservedWordValid ) + { + for (OUString & j : aReservedWord) + j.clear(); + bReservedWordValid = false; + } + xDefaultCalendar.reset(); + xSecondaryCalendar.reset(); + bSecondaryCalendarValid = false; + if (aGrouping.hasElements()) + aGrouping[0] = 0; + if (aDateAcceptancePatterns.hasElements()) + aDateAcceptancePatterns = Sequence<OUString>(); +} + +/* FIXME-BCP47: locale data should provide a language tag instead that could be + * passed on. */ +css::i18n::LanguageCountryInfo LocaleDataWrapper::getLanguageCountryInfo() const +{ + try + { + return xLD->getLanguageCountryInfo( getMyLocale() ); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getLanguageCountryInfo" ); + } + return css::i18n::LanguageCountryInfo(); +} + +const css::i18n::LocaleDataItem2& LocaleDataWrapper::getLocaleItem() const +{ + { + ::utl::ReadWriteGuard aGuard( aMutex ); + const css::lang::Locale& rLocal = getMyLocale(); + auto itr = maDataItemCache.find(rLocal); + if (itr != maDataItemCache.end()) + return itr->second; + } + + try + { + ::utl::ReadWriteGuard aGuard( aMutex ); + + const css::lang::Locale& rLocal = getMyLocale(); + css::i18n::LocaleDataItem2 aItem = xLD->getLocaleItem2( rLocal ); + auto aRet = maDataItemCache.insert(std::make_pair(rLocal, aItem)); + assert(aRet.second); + return aRet.first->second; + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getLocaleItem" ); + } + static css::i18n::LocaleDataItem2 aEmptyItem; + return aEmptyItem; +} + +css::uno::Sequence< css::i18n::Currency2 > LocaleDataWrapper::getAllCurrencies() const +{ + try + { + return xLD->getAllCurrencies2( getMyLocale() ); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getAllCurrencies" ); + } + return css::uno::Sequence< css::i18n::Currency2 >(0); +} + +css::uno::Sequence< css::i18n::FormatElement > LocaleDataWrapper::getAllFormats() const +{ + try + { + return xLD->getAllFormats( getMyLocale() ); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getAllFormats" ); + } + return css::uno::Sequence< css::i18n::FormatElement >(0); +} + +css::i18n::ForbiddenCharacters LocaleDataWrapper::getForbiddenCharacters() const +{ + try + { + return xLD->getForbiddenCharacters( getMyLocale() ); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getForbiddenCharacters" ); + } + return css::i18n::ForbiddenCharacters(); +} + +css::uno::Sequence< OUString > LocaleDataWrapper::getReservedWord() const +{ + try + { + return xLD->getReservedWord( getMyLocale() ); + } + catch ( const Exception& ) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getReservedWord" ); + } + return css::uno::Sequence< OUString >(0); +} + +css::uno::Sequence< css::lang::Locale > LocaleDataWrapper::getAllInstalledLocaleNames() const +{ + uno::Sequence< lang::Locale > &rInstalledLocales = InstalledLocales::get(); + + if ( rInstalledLocales.hasElements() ) + return rInstalledLocales; + + try + { + rInstalledLocales = xLD->getAllInstalledLocaleNames(); + } + catch ( const Exception& ) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getAllInstalledLocaleNames" ); + } + return rInstalledLocales; +} + +// --- Impl and helpers ---------------------------------------------------- + +// static +css::uno::Sequence< css::lang::Locale > LocaleDataWrapper::getInstalledLocaleNames() +{ + const uno::Sequence< lang::Locale > &rInstalledLocales = + InstalledLocales::get(); + + if ( !rInstalledLocales.hasElements() ) + { + LocaleDataWrapper aLDW( ::comphelper::getProcessComponentContext(), LanguageTag( LANGUAGE_SYSTEM) ); + aLDW.getAllInstalledLocaleNames(); + } + return rInstalledLocales; +} + +// static +std::vector< LanguageType > LocaleDataWrapper::getInstalledLanguageTypes() +{ + std::vector< LanguageType > &rInstalledLanguageTypes = + InstalledLanguageTypes::get(); + + if ( !rInstalledLanguageTypes.empty() ) + return rInstalledLanguageTypes; + + const css::uno::Sequence< css::lang::Locale > xLoc = getInstalledLocaleNames(); + sal_Int32 nCount = xLoc.getLength(); + std::vector< LanguageType > xLang; + xLang.reserve(nCount); + for ( const auto& rLoc : xLoc ) + { + LanguageTag aLanguageTag( rLoc ); + OUString aDebugLocale; + if (areChecksEnabled()) + { + aDebugLocale = aLanguageTag.getBcp47( false); + } + + LanguageType eLang = aLanguageTag.getLanguageType( false); + if (areChecksEnabled() && eLang == LANGUAGE_DONTKNOW) + { + OUString aMsg = "ConvertIsoNamesToLanguage: unknown MS-LCID for locale\n" + + aDebugLocale; + outputCheckMessage(aMsg); + } + + if ( eLang == LANGUAGE_NORWEGIAN) // no_NO, not Bokmal (nb_NO), not Nynorsk (nn_NO) + eLang = LANGUAGE_DONTKNOW; // don't offer "Unknown" language + if ( eLang != LANGUAGE_DONTKNOW ) + { + LanguageTag aBackLanguageTag( eLang); + if ( aLanguageTag != aBackLanguageTag ) + { + // In checks, exclude known problems because no MS-LCID defined + // and default for Language found. + if ( areChecksEnabled() + && aDebugLocale != "ar-SD" // Sudan/ar + && aDebugLocale != "en-CB" // Caribbean is not a country +// && aDebugLocale != "en-BG" // ?!? Bulgaria/en +// && aDebugLocale != "es-BR" // ?!? Brazil/es + ) + { + OUStringBuffer aMsg("ConvertIsoNamesToLanguage/ConvertLanguageToIsoNames: ambiguous locale (MS-LCID?)\n"); + aMsg.append(aDebugLocale); + aMsg.append(" -> 0x"); + aMsg.append(static_cast<sal_Int32>(static_cast<sal_uInt16>(eLang)), 16); + aMsg.append(" -> "); + aMsg.append(aBackLanguageTag.getBcp47()); + outputCheckMessage( aMsg.makeStringAndClear() ); + } + eLang = LANGUAGE_DONTKNOW; + } + } + if ( eLang != LANGUAGE_DONTKNOW ) + xLang.push_back(eLang); + } + rInstalledLanguageTypes = xLang; + + return rInstalledLanguageTypes; +} + +const OUString& LocaleDataWrapper::getOneLocaleItem( sal_Int16 nItem ) const +{ + ::utl::ReadWriteGuard aGuard( aMutex ); + if ( nItem >= LocaleItem::COUNT2 ) + { + SAL_WARN( "unotools.i18n", "getOneLocaleItem: bounds" ); + return aLocaleItem[0]; + } + if (aLocaleItem[nItem].isEmpty()) + { // no cached content + aGuard.changeReadToWrite(); + const_cast<LocaleDataWrapper*>(this)->getOneLocaleItemImpl( nItem ); + } + return aLocaleItem[nItem]; +} + +void LocaleDataWrapper::getOneLocaleItemImpl( sal_Int16 nItem ) +{ + if ( !bLocaleDataItemValid ) + { + aLocaleDataItem = getLocaleItem(); + bLocaleDataItemValid = true; + } + switch ( nItem ) + { + case LocaleItem::DATE_SEPARATOR : + aLocaleItem[nItem] = aLocaleDataItem.dateSeparator; + break; + case LocaleItem::THOUSAND_SEPARATOR : + aLocaleItem[nItem] = aLocaleDataItem.thousandSeparator; + break; + case LocaleItem::DECIMAL_SEPARATOR : + aLocaleItem[nItem] = aLocaleDataItem.decimalSeparator; + break; + case LocaleItem::TIME_SEPARATOR : + aLocaleItem[nItem] = aLocaleDataItem.timeSeparator; + break; + case LocaleItem::TIME_100SEC_SEPARATOR : + aLocaleItem[nItem] = aLocaleDataItem.time100SecSeparator; + break; + case LocaleItem::LIST_SEPARATOR : + aLocaleItem[nItem] = aLocaleDataItem.listSeparator; + break; + case LocaleItem::SINGLE_QUOTATION_START : + aLocaleItem[nItem] = aLocaleDataItem.quotationStart; + break; + case LocaleItem::SINGLE_QUOTATION_END : + aLocaleItem[nItem] = aLocaleDataItem.quotationEnd; + break; + case LocaleItem::DOUBLE_QUOTATION_START : + aLocaleItem[nItem] = aLocaleDataItem.doubleQuotationStart; + break; + case LocaleItem::DOUBLE_QUOTATION_END : + aLocaleItem[nItem] = aLocaleDataItem.doubleQuotationEnd; + break; + case LocaleItem::MEASUREMENT_SYSTEM : + aLocaleItem[nItem] = aLocaleDataItem.measurementSystem; + break; + case LocaleItem::TIME_AM : + aLocaleItem[nItem] = aLocaleDataItem.timeAM; + break; + case LocaleItem::TIME_PM : + aLocaleItem[nItem] = aLocaleDataItem.timePM; + break; + case LocaleItem::LONG_DATE_DAY_OF_WEEK_SEPARATOR : + aLocaleItem[nItem] = aLocaleDataItem.LongDateDayOfWeekSeparator; + break; + case LocaleItem::LONG_DATE_DAY_SEPARATOR : + aLocaleItem[nItem] = aLocaleDataItem.LongDateDaySeparator; + break; + case LocaleItem::LONG_DATE_MONTH_SEPARATOR : + aLocaleItem[nItem] = aLocaleDataItem.LongDateMonthSeparator; + break; + case LocaleItem::LONG_DATE_YEAR_SEPARATOR : + aLocaleItem[nItem] = aLocaleDataItem.LongDateYearSeparator; + break; + case LocaleItem::DECIMAL_SEPARATOR_ALTERNATIVE : + aLocaleItem[nItem] = aLocaleDataItem.decimalSeparatorAlternative; + break; + default: + SAL_WARN( "unotools.i18n", "getOneLocaleItemImpl: which one?" ); + } +} + +void LocaleDataWrapper::getOneReservedWordImpl( sal_Int16 nWord ) +{ + if ( !bReservedWordValid ) + { + aReservedWordSeq = getReservedWord(); + bReservedWordValid = true; + } + DBG_ASSERT( nWord < aReservedWordSeq.getLength(), "getOneReservedWordImpl: which one?" ); + if ( nWord < aReservedWordSeq.getLength() ) + aReservedWord[nWord] = aReservedWordSeq[nWord]; +} + +const OUString& LocaleDataWrapper::getOneReservedWord( sal_Int16 nWord ) const +{ + ::utl::ReadWriteGuard aGuard( aMutex ); + if ( nWord < 0 || nWord >= reservedWords::COUNT ) + { + SAL_WARN( "unotools.i18n", "getOneReservedWord: bounds" ); + nWord = reservedWords::FALSE_WORD; + } + if (aReservedWord[nWord].isEmpty()) + { // no cached content + aGuard.changeReadToWrite(); + const_cast<LocaleDataWrapper*>(this)->getOneReservedWordImpl( nWord ); + } + return aReservedWord[nWord]; +} + +MeasurementSystem LocaleDataWrapper::mapMeasurementStringToEnum( const OUString& rMS ) const +{ +//! TODO: could be cached too + if ( rMS.equalsIgnoreAsciiCase( "metric" ) ) + return MeasurementSystem::Metric; +//! TODO: other measurement systems? => extend enum MeasurementSystem + return MeasurementSystem::US; +} + +void LocaleDataWrapper::getSecondaryCalendarImpl() +{ + if (!xSecondaryCalendar && !bSecondaryCalendarValid) + { + Sequence< Calendar2 > xCals = getAllCalendars(); + if (xCals.getLength() > 1) + { + auto pCal = std::find_if(xCals.begin(), xCals.end(), + [](const Calendar2& rCal) { return !rCal.Default; }); + if (pCal != xCals.end()) + xSecondaryCalendar = std::make_shared<Calendar2>( *pCal); + } + bSecondaryCalendarValid = true; + } +} + +bool LocaleDataWrapper::doesSecondaryCalendarUseEC( const OUString& rName ) const +{ + if (rName.isEmpty()) + return false; + + // Check language tag first to avoid loading all calendars of this locale. + LanguageTag aLoaded( getLoadedLanguageTag()); + const OUString& aBcp47( aLoaded.getBcp47()); + // So far determine only by locale, we know for a few. + /* TODO: check date format codes? or add to locale data? */ + if ( aBcp47 != "ja-JP" && + aBcp47 != "lo-LA" && + aBcp47 != "zh-TW") + return false; + + ::utl::ReadWriteGuard aGuard( aMutex ); + + if (!bSecondaryCalendarValid) + { // no cached content + aGuard.changeReadToWrite(); + const_cast<LocaleDataWrapper*>(this)->getSecondaryCalendarImpl(); + } + if (!xSecondaryCalendar) + return false; + if (!xSecondaryCalendar->Name.equalsIgnoreAsciiCase( rName)) + return false; + + return true; +} + +void LocaleDataWrapper::getDefaultCalendarImpl() +{ + if (!xDefaultCalendar) + { + Sequence< Calendar2 > xCals = getAllCalendars(); + auto pCal = xCals.begin(); + if (xCals.getLength() > 1) + { + pCal = std::find_if(xCals.begin(), xCals.end(), + [](const Calendar2& rCal) { return rCal.Default; }); + if (pCal == xCals.end()) + pCal = xCals.begin(); + } + xDefaultCalendar = std::make_shared<Calendar2>( *pCal); + } +} + +const std::shared_ptr< css::i18n::Calendar2 >& LocaleDataWrapper::getDefaultCalendar() const +{ + ::utl::ReadWriteGuard aGuard( aMutex ); + if (!xDefaultCalendar) + { // no cached content + aGuard.changeReadToWrite(); + const_cast<LocaleDataWrapper*>(this)->getDefaultCalendarImpl(); + } + return xDefaultCalendar; +} + +css::uno::Sequence< css::i18n::CalendarItem2 > const & LocaleDataWrapper::getDefaultCalendarDays() const +{ + return getDefaultCalendar()->Days; +} + +css::uno::Sequence< css::i18n::CalendarItem2 > const & LocaleDataWrapper::getDefaultCalendarMonths() const +{ + return getDefaultCalendar()->Months; +} + +// --- currencies ----------------------------------------------------- + +const OUString& LocaleDataWrapper::getCurrSymbol() const +{ + ::utl::ReadWriteGuard aGuard( aMutex ); + if (aCurrSymbol.isEmpty()) + { + aGuard.changeReadToWrite(); + const_cast<LocaleDataWrapper*>(this)->getCurrSymbolsImpl(); + } + return aCurrSymbol; +} + +const OUString& LocaleDataWrapper::getCurrBankSymbol() const +{ + ::utl::ReadWriteGuard aGuard( aMutex ); + if (aCurrBankSymbol.isEmpty()) + { + aGuard.changeReadToWrite(); + const_cast<LocaleDataWrapper*>(this)->getCurrSymbolsImpl(); + } + return aCurrBankSymbol; +} + +sal_uInt16 LocaleDataWrapper::getCurrPositiveFormat() const +{ + ::utl::ReadWriteGuard aGuard( aMutex ); + if ( nCurrPositiveFormat == nCurrFormatInvalid ) + { + aGuard.changeReadToWrite(); + const_cast<LocaleDataWrapper*>(this)->getCurrFormatsImpl(); + } + return nCurrPositiveFormat; +} + +sal_uInt16 LocaleDataWrapper::getCurrNegativeFormat() const +{ + ::utl::ReadWriteGuard aGuard( aMutex ); + if ( nCurrNegativeFormat == nCurrFormatInvalid ) + { + aGuard.changeReadToWrite(); + const_cast<LocaleDataWrapper*>(this)->getCurrFormatsImpl(); + } + return nCurrNegativeFormat; +} + +sal_uInt16 LocaleDataWrapper::getCurrDigits() const +{ + ::utl::ReadWriteGuard aGuard( aMutex ); + if ( nCurrDigits == nCurrFormatInvalid ) + { + aGuard.changeReadToWrite(); + const_cast<LocaleDataWrapper*>(this)->getCurrSymbolsImpl(); + } + return nCurrDigits; +} + +void LocaleDataWrapper::getCurrSymbolsImpl() +{ + Sequence< Currency2 > aCurrSeq = getAllCurrencies(); + if ( !aCurrSeq.hasElements() ) + { + if (areChecksEnabled()) + outputCheckMessage(OUString("LocaleDataWrapper::getCurrSymbolsImpl: no currency at all, using ShellsAndPebbles")); + aCurrSymbol = "ShellsAndPebbles"; + aCurrBankSymbol = aCurrSymbol; + nCurrPositiveFormat = nCurrNegativeFormat = nCurrFormatDefault; + nCurrDigits = 2; + return; + } + auto pCurr = std::find_if(aCurrSeq.begin(), aCurrSeq.end(), + [](const Currency2& rCurr) { return rCurr.Default; }); + if ( pCurr == aCurrSeq.end() ) + { + if (areChecksEnabled()) + { + outputCheckMessage( appendLocaleInfo( "LocaleDataWrapper::getCurrSymbolsImpl: no default currency" ) ); + } + pCurr = aCurrSeq.begin(); + } + aCurrSymbol = pCurr->Symbol; + aCurrBankSymbol = pCurr->BankSymbol; + nCurrDigits = pCurr->DecimalPlaces; +} + +void LocaleDataWrapper::scanCurrFormatImpl( const OUString& rCode, + sal_Int32 nStart, sal_Int32& nSign, sal_Int32& nPar, + sal_Int32& nNum, sal_Int32& nBlank, sal_Int32& nSym ) const +{ + nSign = nPar = nNum = nBlank = nSym = -1; + const sal_Unicode* const pStr = rCode.getStr(); + const sal_Unicode* const pStop = pStr + rCode.getLength(); + const sal_Unicode* p = pStr + nStart; + int nInSection = 0; + bool bQuote = false; + while ( p < pStop ) + { + if ( bQuote ) + { + if ( *p == '"' && *(p-1) != '\\' ) + bQuote = false; + } + else + { + switch ( *p ) + { + case '"' : + if ( pStr == p || *(p-1) != '\\' ) + bQuote = true; + break; + case '-' : + if (!nInSection && nSign == -1) + nSign = p - pStr; + break; + case '(' : + if (!nInSection && nPar == -1) + nPar = p - pStr; + break; + case '0' : + case '#' : + if (!nInSection && nNum == -1) + nNum = p - pStr; + break; + case '[' : + nInSection++; + break; + case ']' : + if ( nInSection ) + { + nInSection--; + if (!nInSection && nBlank == -1 + && nSym != -1 && p < pStop-1 && *(p+1) == ' ' ) + nBlank = p - pStr + 1; + } + break; + case '$' : + if (nSym == -1 && nInSection && *(p-1) == '[') + { + nSym = p - pStr + 1; + if (nNum != -1 && *(p-2) == ' ') + nBlank = p - pStr - 2; + } + break; + case ';' : + if ( !nInSection ) + p = pStop; + break; + default: + if (!nInSection && nSym == -1 && rCode.match(aCurrSymbol, static_cast<sal_Int32>(p - pStr))) + { // currency symbol not surrounded by [$...] + nSym = p - pStr; + if (nBlank == -1 && pStr < p && *(p-1) == ' ') + nBlank = p - pStr - 1; + p += aCurrSymbol.getLength() - 1; + if (nBlank == -1 && p < pStop-2 && *(p+2) == ' ') + nBlank = p - pStr + 2; + } + } + } + p++; + } +} + +void LocaleDataWrapper::getCurrFormatsImpl() +{ + css::uno::Reference< css::i18n::XNumberFormatCode > xNFC = i18n::NumberFormatMapper::create( m_xContext ); + uno::Sequence< NumberFormatCode > aFormatSeq = xNFC->getAllFormatCode( KNumberFormatUsage::CURRENCY, getMyLocale() ); + sal_Int32 nCnt = aFormatSeq.getLength(); + if ( !nCnt ) + { // bad luck + if (areChecksEnabled()) + { + outputCheckMessage( appendLocaleInfo( "LocaleDataWrapper::getCurrFormatsImpl: no currency formats" ) ); + } + nCurrPositiveFormat = nCurrNegativeFormat = nCurrFormatDefault; + return; + } + // find a negative code (medium preferred) and a default (medium preferred) (not necessarily the same) + NumberFormatCode const * const pFormatArr = aFormatSeq.getArray(); + sal_Int32 nElem, nDef, nNeg, nMedium; + nDef = nNeg = nMedium = -1; + for ( nElem = 0; nElem < nCnt; nElem++ ) + { + if ( pFormatArr[nElem].Type == KNumberFormatType::MEDIUM ) + { + if ( pFormatArr[nElem].Default ) + { + nDef = nElem; + nMedium = nElem; + if ( pFormatArr[nElem].Code.indexOf( ';' ) >= 0 ) + nNeg = nElem; + } + else + { + if ( (nNeg == -1 || nMedium == -1) && pFormatArr[nElem].Code.indexOf( ';' ) >= 0 ) + nNeg = nElem; + if ( nMedium == -1 ) + nMedium = nElem; + } + } + else + { + if ( nDef == -1 && pFormatArr[nElem].Default ) + nDef = nElem; + if ( nNeg == -1 && pFormatArr[nElem].Code.indexOf( ';' ) >= 0 ) + nNeg = nElem; + } + } + + // make sure it's loaded + getCurrSymbol(); + + sal_Int32 nSign, nPar, nNum, nBlank, nSym; + + // positive format + nElem = (nDef >= 0 ? nDef : (nNeg >= 0 ? nNeg : 0)); + scanCurrFormatImpl( pFormatArr[nElem].Code, 0, nSign, nPar, nNum, nBlank, nSym ); + if (areChecksEnabled() && (nNum == -1 || nSym == -1)) + { + outputCheckMessage( appendLocaleInfo( "LocaleDataWrapper::getCurrFormatsImpl: CurrPositiveFormat?" ) ); + } + if (nBlank == -1) + { + if ( nSym < nNum ) + nCurrPositiveFormat = 0; // $1 + else + nCurrPositiveFormat = 1; // 1$ + } + else + { + if ( nSym < nNum ) + nCurrPositiveFormat = 2; // $ 1 + else + nCurrPositiveFormat = 3; // 1 $ + } + + // negative format + if ( nNeg < 0 ) + nCurrNegativeFormat = nCurrFormatDefault; + else + { + const OUString& rCode = pFormatArr[nNeg].Code; + sal_Int32 nDelim = rCode.indexOf(';'); + scanCurrFormatImpl( rCode, nDelim+1, nSign, nPar, nNum, nBlank, nSym ); + if (areChecksEnabled() && (nNum == -1 || nSym == -1 || (nPar == -1 && nSign == -1))) + { + outputCheckMessage( appendLocaleInfo( "LocaleDataWrapper::getCurrFormatsImpl: CurrNegativeFormat?" ) ); + } + // NOTE: one of nPar or nSign are allowed to be -1 + if (nBlank == -1) + { + if ( nSym < nNum ) + { + if ( -1 < nPar && nPar < nSym ) + nCurrNegativeFormat = 0; // ($1) + else if ( -1 < nSign && nSign < nSym ) + nCurrNegativeFormat = 1; // -$1 + else if ( nNum < nSign ) + nCurrNegativeFormat = 3; // $1- + else + nCurrNegativeFormat = 2; // $-1 + } + else + { + if ( -1 < nPar && nPar < nNum ) + nCurrNegativeFormat = 4; // (1$) + else if ( -1 < nSign && nSign < nNum ) + nCurrNegativeFormat = 5; // -1$ + else if ( nSym < nSign ) + nCurrNegativeFormat = 7; // 1$- + else + nCurrNegativeFormat = 6; // 1-$ + } + } + else + { + if ( nSym < nNum ) + { + if ( -1 < nPar && nPar < nSym ) + nCurrNegativeFormat = 14; // ($ 1) + else if ( -1 < nSign && nSign < nSym ) + nCurrNegativeFormat = 9; // -$ 1 + else if ( nNum < nSign ) + nCurrNegativeFormat = 12; // $ 1- + else + nCurrNegativeFormat = 11; // $ -1 + } + else + { + if ( -1 < nPar && nPar < nNum ) + nCurrNegativeFormat = 15; // (1 $) + else if ( -1 < nSign && nSign < nNum ) + nCurrNegativeFormat = 8; // -1 $ + else if ( nSym < nSign ) + nCurrNegativeFormat = 10; // 1 $- + else + nCurrNegativeFormat = 13; // 1- $ + } + } + } +} + +// --- date ----------------------------------------------------------- + +DateOrder LocaleDataWrapper::getDateOrder() const +{ + ::utl::ReadWriteGuard aGuard( aMutex ); + if ( nDateOrder == DateOrder::Invalid ) + { + aGuard.changeReadToWrite(); + const_cast<LocaleDataWrapper*>(this)->getDateOrdersImpl(); + } + return nDateOrder; +} + +DateOrder LocaleDataWrapper::getLongDateOrder() const +{ + ::utl::ReadWriteGuard aGuard( aMutex ); + if ( nLongDateOrder == DateOrder::Invalid ) + { + aGuard.changeReadToWrite(); + const_cast<LocaleDataWrapper*>(this)->getDateOrdersImpl(); + } + return nLongDateOrder; +} + +DateOrder LocaleDataWrapper::scanDateOrderImpl( const OUString& rCode ) const +{ + // Only some european versions were translated, the ones with different + // keyword combinations are: + // English DMY, German TMJ, Spanish DMA, French JMA, Italian GMA, + // Dutch DMJ, Finnish PKV + + // default is English keywords for every other language + sal_Int32 nDay = rCode.indexOf('D'); + sal_Int32 nMonth = rCode.indexOf('M'); + sal_Int32 nYear = rCode.indexOf('Y'); + if (nDay == -1 || nMonth == -1 || nYear == -1) + { // This algorithm assumes that all three parts (DMY) are present + if (nMonth == -1) + { // only Finnish has something else than 'M' for month + nMonth = rCode.indexOf('K'); + if (nMonth != -1) + { + nDay = rCode.indexOf('P'); + nYear = rCode.indexOf('V'); + } + } + else if (nDay == -1) + { // We have a month 'M' if we reach this branch. + // Possible languages containing 'M' but no 'D': + // German, French, Italian + nDay = rCode.indexOf('T'); // German + if (nDay != -1) + nYear = rCode.indexOf('J'); + else + { + nYear = rCode.indexOf('A'); // French, Italian + if (nYear != -1) + { + nDay = rCode.indexOf('J'); // French + if (nDay == -1) + nDay = rCode.indexOf('G'); // Italian + } + } + } + else + { // We have a month 'M' and a day 'D'. + // Possible languages containing 'D' and 'M' but not 'Y': + // Spanish, Dutch + nYear = rCode.indexOf('A'); // Spanish + if (nYear == -1) + nYear = rCode.indexOf('J'); // Dutch + } + if (nDay == -1 || nMonth == -1 || nYear == -1) + { + if (areChecksEnabled()) + { + outputCheckMessage( appendLocaleInfo( "LocaleDataWrapper::scanDateOrder: not all DMY present" ) ); + } + if (nDay == -1) + nDay = rCode.getLength(); + if (nMonth == -1) + nMonth = rCode.getLength(); + if (nYear == -1) + nYear = rCode.getLength(); + } + } + // compare with <= because each position may equal rCode.getLength() + if ( nDay <= nMonth && nMonth <= nYear ) + return DateOrder::DMY; // also if every position equals rCode.getLength() + else if ( nMonth <= nDay && nDay <= nYear ) + return DateOrder::MDY; + else if ( nYear <= nMonth && nMonth <= nDay ) + return DateOrder::YMD; + else + { + if (areChecksEnabled()) + { + outputCheckMessage( appendLocaleInfo( "LocaleDataWrapper::scanDateOrder: no magic applicable" ) ); + } + return DateOrder::DMY; + } +} + +void LocaleDataWrapper::getDateOrdersImpl() +{ + css::uno::Reference< css::i18n::XNumberFormatCode > xNFC = i18n::NumberFormatMapper::create( m_xContext ); + uno::Sequence< NumberFormatCode > aFormatSeq = xNFC->getAllFormatCode( KNumberFormatUsage::DATE, getMyLocale() ); + sal_Int32 nCnt = aFormatSeq.getLength(); + if ( !nCnt ) + { // bad luck + if (areChecksEnabled()) + { + outputCheckMessage( appendLocaleInfo( "LocaleDataWrapper::getDateOrdersImpl: no date formats" ) ); + } + nDateOrder = nLongDateOrder = DateOrder::DMY; + return; + } + // find the edit (21), a default (medium preferred), + // a medium (default preferred), and a long (default preferred) + NumberFormatCode const * const pFormatArr = aFormatSeq.getArray(); + sal_Int32 nEdit, nDef, nMedium, nLong; + nEdit = nDef = nMedium = nLong = -1; + for ( sal_Int32 nElem = 0; nElem < nCnt; nElem++ ) + { + if ( nEdit == -1 && pFormatArr[nElem].Index == NumberFormatIndex::DATE_SYS_DDMMYYYY ) + nEdit = nElem; + if ( nDef == -1 && pFormatArr[nElem].Default ) + nDef = nElem; + switch ( pFormatArr[nElem].Type ) + { + case KNumberFormatType::MEDIUM : + { + if ( pFormatArr[nElem].Default ) + { + nDef = nElem; + nMedium = nElem; + } + else if ( nMedium == -1 ) + nMedium = nElem; + } + break; + case KNumberFormatType::LONG : + { + if ( pFormatArr[nElem].Default ) + nLong = nElem; + else if ( nLong == -1 ) + nLong = nElem; + } + break; + } + } + if ( nEdit == -1 ) + { + if (areChecksEnabled()) + { + outputCheckMessage( appendLocaleInfo( "LocaleDataWrapper::getDateOrdersImpl: no edit" ) ); + } + if ( nDef == -1 ) + { + if (areChecksEnabled()) + { + outputCheckMessage( appendLocaleInfo( "LocaleDataWrapper::getDateOrdersImpl: no default" ) ); + } + if ( nMedium != -1 ) + nDef = nMedium; + else if ( nLong != -1 ) + nDef = nLong; + else + nDef = 0; + } + nEdit = nDef; + } + DateOrder nDF = scanDateOrderImpl( pFormatArr[nEdit].Code ); + if ( pFormatArr[nEdit].Type == KNumberFormatType::LONG ) + { // normally this is not the case + nLongDateOrder = nDateOrder = nDF; + } + else + { + nDateOrder = nDF; + if ( nLong == -1 ) + nLongDateOrder = nDF; + else + nLongDateOrder = scanDateOrderImpl( pFormatArr[nLong].Code ); + } +} + +// --- digit grouping ------------------------------------------------- + +void LocaleDataWrapper::getDigitGroupingImpl() +{ + /* TODO: This is a very simplified grouping setup that only serves its + * current purpose for Indian locales. A free-form flexible one would + * obtain grouping from locale data where it could be specified using, for + * example, codes like #,### and #,##,### that would generate the integer + * sequence. Needed additional API and a locale data element. + */ + + if (!aGrouping.hasElements()) + { + aGrouping.realloc(3); // room for {3,2,0} + aGrouping[0] = 0; // invalidate + } + if (!aGrouping[0]) + { + i18n::LanguageCountryInfo aLCInfo( getLanguageCountryInfo()); + if (aLCInfo.Country.equalsIgnoreAsciiCase("IN") || // India + aLCInfo.Country.equalsIgnoreAsciiCase("BT") ) // Bhutan + { + aGrouping[0] = 3; + aGrouping[1] = 2; + aGrouping[2] = 0; + } + else + { + aGrouping[0] = 3; + aGrouping[1] = 0; + } + } +} + +css::uno::Sequence< sal_Int32 > LocaleDataWrapper::getDigitGrouping() const +{ + ::utl::ReadWriteGuard aGuard( aMutex ); + if (!aGrouping.hasElements() || aGrouping[0] == 0) + { // no cached content + aGuard.changeReadToWrite(); + const_cast<LocaleDataWrapper*>(this)->getDigitGroupingImpl(); + } + return aGrouping; +} + +// --- simple number formatting helpers ------------------------------- + +// The ImplAdd... methods are taken from class International and modified to +// suit the needs. + +static void ImplAddUNum( OUStringBuffer& rBuf, sal_uInt64 nNumber ) +{ + // fill temp buffer with digits + sal_Unicode aTempBuf[64]; + sal_Unicode* pTempBuf = aTempBuf; + do + { + *pTempBuf = static_cast<sal_Unicode>(nNumber % 10) + '0'; + pTempBuf++; + nNumber /= 10; + } + while ( nNumber ); + + // copy temp buffer to buffer passed + do + { + pTempBuf--; + rBuf.append(*pTempBuf); + } + while ( pTempBuf != aTempBuf ); +} + +static void ImplAddUNum( OUStringBuffer& rBuf, sal_uInt64 nNumber, int nMinLen ) +{ + // fill temp buffer with digits + sal_Unicode aTempBuf[64]; + sal_Unicode* pTempBuf = aTempBuf; + do + { + *pTempBuf = static_cast<sal_Unicode>(nNumber % 10) + '0'; + pTempBuf++; + nNumber /= 10; + nMinLen--; + } + while ( nNumber ); + + // fill with zeros up to the minimal length + while ( nMinLen > 0 ) + { + rBuf.append('0'); + nMinLen--; + } + + // copy temp buffer to real buffer + do + { + pTempBuf--; + rBuf.append(*pTempBuf); + } + while ( pTempBuf != aTempBuf ); +} + +static void ImplAddNum( OUStringBuffer& rBuf, sal_Int64 nNumber, int nMinLen ) +{ + if (nNumber < 0) + { + rBuf.append('-'); + nNumber = -nNumber; + } + return ImplAddUNum( rBuf, nNumber, nMinLen); +} + +static void ImplAdd2UNum( OUStringBuffer& rBuf, sal_uInt16 nNumber, bool bLeading ) +{ + DBG_ASSERT( nNumber < 100, "ImplAdd2UNum() - Number >= 100" ); + + if ( nNumber < 10 ) + { + if ( bLeading ) + { + rBuf.append('0'); + } + rBuf.append(static_cast<char>(nNumber + '0')); + } + else + { + sal_uInt16 nTemp = nNumber % 10; + nNumber /= 10; + rBuf.append(static_cast<char>(nNumber + '0')); + rBuf.append(static_cast<char>(nTemp + '0')); + } +} + +static void ImplAdd9UNum( OUStringBuffer& rBuf, sal_uInt32 nNumber ) +{ + DBG_ASSERT( nNumber < 1000000000, "ImplAdd9UNum() - Number >= 1000000000" ); + + std::ostringstream ostr; + ostr.fill('0'); + ostr.width(9); + ostr << nNumber; + std::string aStr = ostr.str(); + rBuf.appendAscii(aStr.c_str(), aStr.size()); +} + +void LocaleDataWrapper::ImplAddFormatNum( OUStringBuffer& rBuf, + sal_Int64 nNumber, sal_uInt16 nDecimals, bool bUseThousandSep, + bool bTrailingZeros ) const +{ + OUStringBuffer aNumBuf(64); + sal_uInt16 nNumLen; + + // negative number + if ( nNumber < 0 ) + { + nNumber *= -1; + rBuf.append('-'); + } + + // convert number + ImplAddUNum( aNumBuf, static_cast<sal_uInt64>(nNumber) ); + nNumLen = static_cast<sal_uInt16>(aNumBuf.getLength()); + + if ( nNumLen <= nDecimals ) + { + // strip .0 in decimals? + if ( !nNumber && !bTrailingZeros ) + { + rBuf.append('0'); + } + else + { + // LeadingZero, insert 0 + if ( isNumLeadingZero() ) + { + rBuf.append('0'); + } + + // append decimal separator + rBuf.append( getNumDecimalSep() ); + + // fill with zeros + sal_uInt16 i = 0; + while ( i < (nDecimals-nNumLen) ) + { + rBuf.append('0'); + i++; + } + + // append decimals + rBuf.append(aNumBuf); + } + } + else + { + const OUString& rThoSep = getNumThousandSep(); + + // copy number to buffer (excluding decimals) + sal_uInt16 nNumLen2 = nNumLen-nDecimals; + uno::Sequence< sal_Bool > aGroupPos; + if (bUseThousandSep) + aGroupPos = utl::DigitGroupingIterator::createForwardSequence( + nNumLen2, getDigitGrouping()); + sal_uInt16 i = 0; + for (; i < nNumLen2; ++i ) + { + rBuf.append(aNumBuf[i]); + + // add thousand separator? + if ( bUseThousandSep && aGroupPos[i] ) + rBuf.append( rThoSep ); + } + + // append decimals + if ( nDecimals ) + { + rBuf.append( getNumDecimalSep() ); + + bool bNullEnd = true; + while ( i < nNumLen ) + { + if ( aNumBuf[i] != '0' ) + bNullEnd = false; + + rBuf.append(aNumBuf[i]); + i++; + } + + // strip .0 in decimals? + if ( bNullEnd && !bTrailingZeros ) + rBuf.setLength( rBuf.getLength() - (nDecimals + 1) ); + } + } +} + +// --- simple date and time formatting -------------------------------- + +OUString LocaleDataWrapper::getDate( const Date& rDate ) const +{ + ::utl::ReadWriteGuard aGuard( aMutex, ReadWriteGuardMode::BlockCritical ); +//!TODO: leading zeros et al + OUStringBuffer aBuf(128); + sal_uInt16 nDay = rDate.GetDay(); + sal_uInt16 nMonth = rDate.GetMonth(); + sal_Int16 nYear = rDate.GetYear(); + sal_uInt16 nYearLen; + + if ( (true) /* IsDateCentury() */ ) + nYearLen = 4; + else + { + nYearLen = 2; + nYear %= 100; + } + + switch ( getDateOrder() ) + { + case DateOrder::DMY : + ImplAdd2UNum( aBuf, nDay, true /* IsDateDayLeadingZero() */ ); + aBuf.append( getDateSep() ); + ImplAdd2UNum( aBuf, nMonth, true /* IsDateMonthLeadingZero() */ ); + aBuf.append( getDateSep() ); + ImplAddNum( aBuf, nYear, nYearLen ); + break; + case DateOrder::MDY : + ImplAdd2UNum( aBuf, nMonth, true /* IsDateMonthLeadingZero() */ ); + aBuf.append( getDateSep() ); + ImplAdd2UNum( aBuf, nDay, true /* IsDateDayLeadingZero() */ ); + aBuf.append( getDateSep() ); + ImplAddNum( aBuf, nYear, nYearLen ); + break; + default: + ImplAddNum( aBuf, nYear, nYearLen ); + aBuf.append( getDateSep() ); + ImplAdd2UNum( aBuf, nMonth, true /* IsDateMonthLeadingZero() */ ); + aBuf.append( getDateSep() ); + ImplAdd2UNum( aBuf, nDay, true /* IsDateDayLeadingZero() */ ); + } + + return aBuf.makeStringAndClear(); +} + +OUString LocaleDataWrapper::getTime( const tools::Time& rTime, bool bSec, bool b100Sec ) const +{ + ::utl::ReadWriteGuard aGuard( aMutex, ReadWriteGuardMode::BlockCritical ); +//!TODO: leading zeros et al + OUStringBuffer aBuf(128); + sal_uInt16 nHour = rTime.GetHour(); + + nHour %= 24; + + ImplAdd2UNum( aBuf, nHour, true /* IsTimeLeadingZero() */ ); + aBuf.append( getTimeSep() ); + ImplAdd2UNum( aBuf, rTime.GetMin(), true ); + if ( bSec ) + { + aBuf.append( getTimeSep() ); + ImplAdd2UNum( aBuf, rTime.GetSec(), true ); + + if ( b100Sec ) + { + aBuf.append( getTime100SecSep() ); + ImplAdd9UNum( aBuf, rTime.GetNanoSec() ); + } + } + + return aBuf.makeStringAndClear(); +} + +OUString LocaleDataWrapper::getLongDate( const Date& rDate, CalendarWrapper& rCal, + bool bTwoDigitYear ) const +{ + ::utl::ReadWriteGuard aGuard( aMutex, ReadWriteGuardMode::BlockCritical ); + OUStringBuffer aBuf(20); + OUStringBuffer aStr(120); // complete guess + sal_Int16 nVal; + rCal.setGregorianDateTime( rDate ); + // day of week + nVal = rCal.getValue( CalendarFieldIndex::DAY_OF_WEEK ); + aStr.append(rCal.getDisplayName( CalendarDisplayIndex::DAY, nVal, 1 )); + aStr.append(getLongDateDayOfWeekSep()); + // day of month + nVal = rCal.getValue( CalendarFieldIndex::DAY_OF_MONTH ); + ImplAdd2UNum( aBuf, nVal, false/*bDayOfMonthWithLeadingZero*/ ); + OUString aDay = aBuf.makeStringAndClear(); + // month of year + nVal = rCal.getValue( CalendarFieldIndex::MONTH ); + OUString aMonth( rCal.getDisplayName( CalendarDisplayIndex::MONTH, nVal, 1 ) ); + // year + nVal = rCal.getValue( CalendarFieldIndex::YEAR ); + if ( bTwoDigitYear ) + ImplAddUNum( aBuf, nVal % 100, 2 ); + else + ImplAddUNum( aBuf, nVal ); + OUString aYear = aBuf.makeStringAndClear(); + // concatenate + switch ( getLongDateOrder() ) + { + case DateOrder::DMY : + aStr.append(aDay).append(getLongDateDaySep()).append(aMonth).append(getLongDateMonthSep()).append(aYear); + break; + case DateOrder::MDY : + aStr.append(aMonth).append(getLongDateMonthSep()).append(aDay).append(getLongDateDaySep()).append(aYear); + break; + default: // YMD + aStr.append(aYear).append(getLongDateYearSep()).append(aMonth).append(getLongDateMonthSep()).append(aDay); + } + return aStr.makeStringAndClear(); +} + +OUString LocaleDataWrapper::getDuration( const tools::Time& rTime, bool bSec, bool b100Sec ) const +{ + ::utl::ReadWriteGuard aGuard( aMutex, ReadWriteGuardMode::BlockCritical ); + OUStringBuffer aBuf(128); + + if ( rTime < tools::Time( 0 ) ) + aBuf.append(' ' ); + + if ( (true) /* IsTimeLeadingZero() */ ) + ImplAddUNum( aBuf, rTime.GetHour(), 2 ); + else + ImplAddUNum( aBuf, rTime.GetHour() ); + aBuf.append( getTimeSep() ); + ImplAdd2UNum( aBuf, rTime.GetMin(), true ); + if ( bSec ) + { + aBuf.append( getTimeSep() ); + ImplAdd2UNum( aBuf, rTime.GetSec(), true ); + + if ( b100Sec ) + { + aBuf.append( getTime100SecSep() ); + ImplAdd9UNum( aBuf, rTime.GetNanoSec() ); + } + } + + return aBuf.makeStringAndClear(); +} + +// --- simple number formatting --------------------------------------- + +static size_t ImplGetNumberStringLengthGuess( const LocaleDataWrapper& rLoc, sal_uInt16 nDecimals ) +{ + // approximately 3.2 bits per digit + const size_t nDig = ((sizeof(sal_Int64) * 8) / 3) + 1; + // digits, separators (pessimized for insane "every digit may be grouped"), leading zero, sign + size_t nGuess = ((nDecimals < nDig) ? + (((nDig - nDecimals) * rLoc.getNumThousandSep().getLength()) + nDig) : + nDecimals) + rLoc.getNumDecimalSep().getLength() + 3; + return nGuess; +} + +OUString LocaleDataWrapper::getNum( sal_Int64 nNumber, sal_uInt16 nDecimals, + bool bUseThousandSep, bool bTrailingZeros ) const +{ + ::utl::ReadWriteGuard aGuard( aMutex, ReadWriteGuardMode::BlockCritical ); + // check if digits and separators will fit into fixed buffer or allocate + size_t nGuess = ImplGetNumberStringLengthGuess( *this, nDecimals ); + OUStringBuffer aBuf(int(nGuess + 16)); + + ImplAddFormatNum( aBuf, nNumber, nDecimals, + bUseThousandSep, bTrailingZeros ); + + return aBuf.makeStringAndClear(); +} + +OUString LocaleDataWrapper::getCurr( sal_Int64 nNumber, sal_uInt16 nDecimals, + const OUString& rCurrencySymbol, bool bUseThousandSep ) const +{ + ::utl::ReadWriteGuard aGuard( aMutex, ReadWriteGuardMode::BlockCritical ); + sal_Unicode cZeroChar = getCurrZeroChar(); + + // check if digits and separators will fit into fixed buffer or allocate + size_t nGuess = ImplGetNumberStringLengthGuess( *this, nDecimals ); + OUStringBuffer aNumBuf(int(nGuess + 16)); + OUStringBuffer aBuf(int(rCurrencySymbol.getLength() + nGuess + 20 )); + + bool bNeg; + if ( nNumber < 0 ) + { + bNeg = true; + nNumber *= -1; + } + else + bNeg = false; + + // convert number + ImplAddFormatNum( aNumBuf, nNumber, nDecimals, + bUseThousandSep, true ); + const sal_Int32 nNumLen = aNumBuf.getLength(); + + // replace zeros with zero character + if ( (cZeroChar != '0') && nDecimals /* && IsNumTrailingZeros() */ ) + { + sal_uInt16 i; + bool bZero = true; + + sal_uInt16 nNumBufIndex = nNumLen-nDecimals; + i = 0; + do + { + if ( aNumBuf[nNumBufIndex] != '0' ) + { + bZero = false; + break; + } + + nNumBufIndex++; + i++; + } + while ( i < nDecimals ); + + if ( bZero ) + { + nNumBufIndex = nNumLen-nDecimals; + i = 0; + do + { + aNumBuf[nNumBufIndex] = cZeroChar; + nNumBufIndex++; + i++; + } + while ( i < nDecimals ); + } + } + + if ( !bNeg ) + { + switch( getCurrPositiveFormat() ) + { + case 0: + aBuf.append( rCurrencySymbol ); + aBuf.append( aNumBuf ); + break; + case 1: + aBuf.append( aNumBuf ); + aBuf.append( rCurrencySymbol ); + break; + case 2: + aBuf.append( rCurrencySymbol ); + aBuf.append( ' ' ); + aBuf.append( aNumBuf ); + break; + case 3: + aBuf.append( aNumBuf ); + aBuf.append( ' ' ); + aBuf.append( rCurrencySymbol ); + break; + } + } + else + { + switch( getCurrNegativeFormat() ) + { + case 0: + aBuf.append( '(' ); + aBuf.append( rCurrencySymbol ); + aBuf.append( aNumBuf ); + aBuf.append( ')' ); + break; + case 1: + aBuf.append( '-' ); + aBuf.append( rCurrencySymbol ); + aBuf.append( aNumBuf ); + break; + case 2: + aBuf.append( rCurrencySymbol ); + aBuf.append( '-' ); + aBuf.append( aNumBuf ); + break; + case 3: + aBuf.append( rCurrencySymbol ); + aBuf.append( aNumBuf ); + aBuf.append( '-' ); + break; + case 4: + aBuf.append( '(' ); + aBuf.append( aNumBuf ); + aBuf.append( rCurrencySymbol ); + aBuf.append( ')' ); + break; + case 5: + aBuf.append( '-' ); + aBuf.append( aNumBuf ); + aBuf.append( rCurrencySymbol ); + break; + case 6: + aBuf.append( aNumBuf ); + aBuf.append( '-' ); + aBuf.append( rCurrencySymbol ); + break; + case 7: + aBuf.append( aNumBuf ); + aBuf.append( rCurrencySymbol ); + aBuf.append( '-' ); + break; + case 8: + aBuf.append( '-' ); + aBuf.append( aNumBuf ); + aBuf.append( ' ' ); + aBuf.append( rCurrencySymbol ); + break; + case 9: + aBuf.append( '-' ); + aBuf.append( rCurrencySymbol ); + aBuf.append( ' ' ); + aBuf.append( aNumBuf ); + break; + case 10: + aBuf.append( aNumBuf ); + aBuf.append( ' ' ); + aBuf.append( rCurrencySymbol ); + aBuf.append( '-' ); + break; + case 11: + aBuf.append( rCurrencySymbol ); + aBuf.append( ' ' ); + aBuf.append( '-' ); + aBuf.append( aNumBuf ); + break; + case 12: + aBuf.append( rCurrencySymbol ); + aBuf.append( ' ' ); + aBuf.append( aNumBuf ); + aBuf.append( '-' ); + break; + case 13: + aBuf.append( aNumBuf ); + aBuf.append( '-' ); + aBuf.append( ' ' ); + aBuf.append( rCurrencySymbol ); + break; + case 14: + aBuf.append( '(' ); + aBuf.append( rCurrencySymbol ); + aBuf.append( ' ' ); + aBuf.append( aNumBuf ); + aBuf.append( ')' ); + break; + case 15: + aBuf.append( '(' ); + aBuf.append( aNumBuf ); + aBuf.append( ' ' ); + aBuf.append( rCurrencySymbol ); + aBuf.append( ')' ); + break; + } + } + + return aBuf.makeStringAndClear(); +} + +// --- number parsing ------------------------------------------------- + +double LocaleDataWrapper::stringToDouble( const OUString& rString, bool bUseGroupSep, + rtl_math_ConversionStatus* pStatus, sal_Int32* pParseEnd ) const +{ + const sal_Unicode cGroupSep = (bUseGroupSep ? getNumThousandSep()[0] : 0); + rtl_math_ConversionStatus eStatus = rtl_math_ConversionStatus_Ok; + sal_Int32 nParseEnd = 0; + double fValue = rtl::math::stringToDouble( rString, getNumDecimalSep()[0], cGroupSep, &eStatus, &nParseEnd); + bool bTryAlt = (nParseEnd < rString.getLength() && !getNumDecimalSepAlt().isEmpty() && + rString[nParseEnd] == getNumDecimalSepAlt().toChar()); + // Try re-parsing with alternative if that was the reason to stop. + if (bTryAlt) + fValue = rtl::math::stringToDouble( rString, getNumDecimalSepAlt().toChar(), cGroupSep, &eStatus, &nParseEnd); + if (pStatus) + *pStatus = eStatus; + if (pParseEnd) + *pParseEnd = nParseEnd; + return fValue; +} + +double LocaleDataWrapper::stringToDouble( const sal_Unicode* pBegin, const sal_Unicode* pEnd, bool bUseGroupSep, + rtl_math_ConversionStatus* pStatus, const sal_Unicode** ppParseEnd ) const +{ + const sal_Unicode cGroupSep = (bUseGroupSep ? getNumThousandSep()[0] : 0); + rtl_math_ConversionStatus eStatus = rtl_math_ConversionStatus_Ok; + const sal_Unicode* pParseEnd = nullptr; + double fValue = rtl_math_uStringToDouble( pBegin, pEnd, getNumDecimalSep()[0], cGroupSep, &eStatus, &pParseEnd); + bool bTryAlt = (pParseEnd < pEnd && !getNumDecimalSepAlt().isEmpty() && + *pParseEnd == getNumDecimalSepAlt().toChar()); + // Try re-parsing with alternative if that was the reason to stop. + if (bTryAlt) + fValue = rtl_math_uStringToDouble( pBegin, pEnd, getNumDecimalSepAlt().toChar(), cGroupSep, &eStatus, &pParseEnd); + if (pStatus) + *pStatus = eStatus; + if (ppParseEnd) + *ppParseEnd = pParseEnd; + return fValue; +} + +// --- mixed ---------------------------------------------------------- + +LanguageTag LocaleDataWrapper::getLoadedLanguageTag() const +{ + LanguageCountryInfo aLCInfo = getLanguageCountryInfo(); + return LanguageTag( lang::Locale( aLCInfo.Language, aLCInfo.Country, aLCInfo.Variant )); +} + +OUString LocaleDataWrapper::appendLocaleInfo(const OUString& rDebugMsg) const +{ + ::utl::ReadWriteGuard aGuard( aMutex, ReadWriteGuardMode::BlockCritical ); + OUStringBuffer aDebugMsg(rDebugMsg); + aDebugMsg.append('\n'); + aDebugMsg.append(maLanguageTag.getBcp47()); + aDebugMsg.append(" requested\n"); + LanguageTag aLoaded = getLoadedLanguageTag(); + aDebugMsg.append(aLoaded.getBcp47()); + aDebugMsg.append(" loaded"); + return aDebugMsg.makeStringAndClear(); +} + +// static +void LocaleDataWrapper::outputCheckMessage( const OUString& rMsg ) +{ + outputCheckMessage(OUStringToOString(rMsg, RTL_TEXTENCODING_UTF8).getStr()); +} + +// static +void LocaleDataWrapper::outputCheckMessage( const char* pStr ) +{ + fprintf( stderr, "\n%s\n", pStr); + fflush( stderr); + SAL_WARN("unotools.i18n", pStr); +} + +// static +void LocaleDataWrapper::evaluateLocaleDataChecking() +{ + // Using the rtl_Instance template here wouldn't solve all threaded write + // accesses, since we want to assign the result to the static member + // variable and would need to dereference the pointer returned and assign + // the value unguarded. This is the same pattern manually coded. + sal_uInt8 nCheck = nLocaleDataChecking; + if (!nCheck) + { + ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex()); + nCheck = nLocaleDataChecking; + if (!nCheck) + { +#ifdef DBG_UTIL + nCheck = 1; +#else + const char* pEnv = getenv( "OOO_ENABLE_LOCALE_DATA_CHECKS"); + if (pEnv && (pEnv[0] == 'Y' || pEnv[0] == 'y' || pEnv[0] == '1')) + nCheck = 1; + else + nCheck = 2; +#endif + OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER(); + nLocaleDataChecking = nCheck; + } + } + else { + OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER(); + } +} + +// --- XLocaleData3 ---------------------------------------------------------- + +css::uno::Sequence< css::i18n::Calendar2 > LocaleDataWrapper::getAllCalendars() const +{ + try + { + return xLD->getAllCalendars2( getMyLocale() ); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getAllCalendars" ); + } + return css::uno::Sequence< css::i18n::Calendar2 >(0); +} + +// --- XLocaleData4 ---------------------------------------------------------- + +css::uno::Sequence< OUString > LocaleDataWrapper::getDateAcceptancePatterns() const +{ + ::utl::ReadWriteGuard aGuard( aMutex ); + + if (aDateAcceptancePatterns.hasElements()) + return aDateAcceptancePatterns; + + aGuard.changeReadToWrite(); + + try + { + const_cast<LocaleDataWrapper*>(this)->aDateAcceptancePatterns = + xLD->getDateAcceptancePatterns( getMyLocale() ); + return aDateAcceptancePatterns; + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "getDateAcceptancePatterns" ); + } + return css::uno::Sequence< OUString >(0); +} + +// --- Override layer -------------------------------------------------------- + +void LocaleDataWrapper::setDateAcceptancePatterns( + const css::uno::Sequence< OUString > & rPatterns ) +{ + ::utl::ReadWriteGuard aGuard( aMutex, ReadWriteGuardMode::Write ); + + if (!aDateAcceptancePatterns.hasElements() || !rPatterns.hasElements()) + { + try + { + aDateAcceptancePatterns = xLD->getDateAcceptancePatterns( getMyLocale() ); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "setDateAcceptancePatterns" ); + } + if (!rPatterns.hasElements()) + return; // just a reset + if (!aDateAcceptancePatterns.hasElements()) + { + aDateAcceptancePatterns = rPatterns; + return; + } + } + + // Never overwrite the locale's full date pattern! The first. + if (aDateAcceptancePatterns[0] == rPatterns[0]) + aDateAcceptancePatterns = rPatterns; // sane + else + { + // Copy existing full date pattern and append the sequence passed. + /* TODO: could check for duplicates and shrink target sequence */ + Sequence< OUString > aTmp( rPatterns.getLength() + 1 ); + aTmp[0] = aDateAcceptancePatterns[0]; + std::copy(rPatterns.begin(), rPatterns.end(), std::next(aTmp.begin())); + aDateAcceptancePatterns = aTmp; + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/i18n/nativenumberwrapper.cxx b/unotools/source/i18n/nativenumberwrapper.cxx new file mode 100644 index 000000000..26fcbd7fa --- /dev/null +++ b/unotools/source/i18n/nativenumberwrapper.cxx @@ -0,0 +1,110 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <sal/log.hxx> +#include <unotools/nativenumberwrapper.hxx> +#include <com/sun/star/i18n/NativeNumberSupplier2.hpp> + +using namespace ::com::sun::star; + +NativeNumberWrapper::NativeNumberWrapper( + const uno::Reference< uno::XComponentContext > & rxContext + ) +{ + xNNS = i18n::NativeNumberSupplier2::create(rxContext); +} + +NativeNumberWrapper::~NativeNumberWrapper() +{ +} + +OUString +NativeNumberWrapper::getNativeNumberString( + const OUString& rNumberString, + const css::lang::Locale& rLocale, + sal_Int16 nNativeNumberMode) const +{ + try + { + if ( xNNS.is() ) + return xNNS->getNativeNumberString(rNumberString, rLocale, nNativeNumberMode); + } + catch ( const uno::Exception& ) + { + SAL_WARN( "unotools.i18n", "getNativeNumberString: Exception caught!" ); + } + return OUString(); +} + +OUString +NativeNumberWrapper::getNativeNumberStringParams( + const OUString& rNumberString, + const css::lang::Locale& rLocale, + sal_Int16 nNativeNumberMode, + const OUString& rNativeNumberParams) const +{ + try + { + if ( xNNS.is() ) + return xNNS->getNativeNumberStringParams(rNumberString, rLocale, nNativeNumberMode, + rNativeNumberParams); + } + catch ( const uno::Exception& ) + { + SAL_WARN( "unotools.i18n", "getNativeNumberStringParams: Exception caught!" ); + } + return OUString(); +} + +i18n::NativeNumberXmlAttributes +NativeNumberWrapper::convertToXmlAttributes( + const css::lang::Locale& rLocale, + sal_Int16 nNativeNumberMode ) const +{ + try + { + if ( xNNS.is() ) + return xNNS->convertToXmlAttributes( rLocale, nNativeNumberMode ); + } + catch ( const uno::Exception& ) + { + SAL_WARN( "unotools.i18n", "convertToXmlAttributes: Exception caught!" ); + } + return i18n::NativeNumberXmlAttributes(); +} + +sal_Int16 +NativeNumberWrapper::convertFromXmlAttributes( + const i18n::NativeNumberXmlAttributes& rAttr ) const +{ + try + { + if ( xNNS.is() ) + return xNNS->convertFromXmlAttributes( rAttr ); + } + catch ( const uno::Exception& ) + { + SAL_WARN( "unotools.i18n", "convertFromXmlAttributes: Exception caught!" ); + } + return 0; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/i18n/readwritemutexguard.cxx b/unotools/source/i18n/readwritemutexguard.cxx new file mode 100644 index 000000000..134f06c35 --- /dev/null +++ b/unotools/source/i18n/readwritemutexguard.cxx @@ -0,0 +1,109 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unotools/readwritemutexguard.hxx> +#include <tools/debug.hxx> + +namespace utl { + +ReadWriteGuard::ReadWriteGuard( ReadWriteMutex& rMutexP, + ReadWriteGuardMode nRequestMode ) + : rMutex( rMutexP ) +{ + // don't do anything until a pending write completed (or another + // ReadWriteGuard leaves the ctor phase) + ::osl::MutexGuard aGuard( rMutex.maWriteMutex ); + nMode = nRequestMode; + if ( nMode & ReadWriteGuardMode::Write ) + { + rMutex.maWriteMutex.acquire(); + // wait for any read to complete +// TODO: set up a waiting thread instead of a loop + bool bWait = true; + do + { + rMutex.maMutex.acquire(); + bWait = (rMutex.nReadCount != 0); + if ( nMode & ReadWriteGuardMode::CriticalChange ) + bWait |= (rMutex.nBlockCriticalCount != 0); + rMutex.maMutex.release(); + } while ( bWait ); + } + else if ( nMode & ReadWriteGuardMode::BlockCritical ) + { + rMutex.maMutex.acquire(); + ++rMutex.nBlockCriticalCount; + rMutex.maMutex.release(); + } + else + { + rMutex.maMutex.acquire(); + ++rMutex.nReadCount; + rMutex.maMutex.release(); + } +} + +ReadWriteGuard::~ReadWriteGuard() +{ + if ( nMode & ReadWriteGuardMode::Write ) + rMutex.maWriteMutex.release(); + else if ( nMode & ReadWriteGuardMode::BlockCritical ) + { + rMutex.maMutex.acquire(); + --rMutex.nBlockCriticalCount; + rMutex.maMutex.release(); + } + else + { + rMutex.maMutex.acquire(); + --rMutex.nReadCount; + rMutex.maMutex.release(); + } +} + +void ReadWriteGuard::changeReadToWrite() +{ + bool bOk = !(nMode & (ReadWriteGuardMode::Write | ReadWriteGuardMode::BlockCritical)); + DBG_ASSERT( bOk, "ReadWriteGuard::changeReadToWrite: can't" ); + if ( bOk ) + { + // MUST release read before acquiring write mutex or dead lock would + // occur if there was a write in another thread waiting for this read + // to complete. + rMutex.maMutex.acquire(); + --rMutex.nReadCount; + rMutex.maMutex.release(); + + rMutex.maWriteMutex.acquire(); + nMode |= ReadWriteGuardMode::Write; + // wait for any other read to complete +// TODO: set up a waiting thread instead of a loop + bool bWait = true; + do + { + rMutex.maMutex.acquire(); + bWait = (rMutex.nReadCount != 0); + rMutex.maMutex.release(); + } while ( bWait ); + } +} + +} // namespace utl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/i18n/resmgr.cxx b/unotools/source/i18n/resmgr.cxx new file mode 100644 index 000000000..a1d74ec1b --- /dev/null +++ b/unotools/source/i18n/resmgr.cxx @@ -0,0 +1,280 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +// Needed when #include <boost/locale.hpp> below includes Boost 1.65.1 +// workdir/UnpackedTarball/boost/boost/locale/format.hpp using "std::auto_ptr<data> d;", but must +// come very early here in case <memory> is already (indirectly) included earlier: +#include <config_libcxx.h> +#if HAVE_LIBCXX +#define _LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR +#elif defined _MSC_VER +#define _HAS_AUTO_PTR_ETC 1 +#endif + +#include <sal/config.h> + +#include <cassert> + +#include <string.h> +#include <stdio.h> +#if defined UNX && !defined MACOSX && !defined IOS && !defined ANDROID +# include <libintl.h> +#endif + +#include <comphelper/lok.hxx> +#include <unotools/resmgr.hxx> +#include <osl/thread.h> +#include <osl/file.hxx> +#include <rtl/crc.h> +#include <rtl/bootstrap.hxx> +#include <i18nlangtag/languagetag.hxx> + +#include <boost/locale.hpp> +#include <boost/locale/gnu_gettext.hpp> + +#include <unordered_map> +#include <memory> + +#ifdef ANDROID +#include <osl/detail/android-bootstrap.h> +#endif + +#if defined(_WIN32) && defined(DBG_UTIL) +#include <o3tl/char16_t2wchar_t.hxx> +#include <prewin.h> +#include <crtdbg.h> +#include <postwin.h> +#endif + +namespace +{ + OUString createFromUtf8(const char* data, size_t size) + { + OUString aTarget; + bool bSuccess = rtl_convertStringToUString(&aTarget.pData, + data, + size, + RTL_TEXTENCODING_UTF8, + RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR|RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR|RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR); + (void) bSuccess; + assert(bSuccess); + return aTarget; + } + + OString genKeyId(const OString& rGenerator) + { + sal_uInt32 nCRC = rtl_crc32(0, rGenerator.getStr(), rGenerator.getLength()); + // Use simple ASCII characters, exclude I, l, 1 and O, 0 to avoid confusing IDs + static const char sSymbols[] = + "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789"; + char sKeyId[6]; + for (short nKeyInd = 0; nKeyInd < 5; ++nKeyInd) + { + sKeyId[nKeyInd] = sSymbols[(nCRC & 63) % strlen(sSymbols)]; + nCRC >>= 6; + } + sKeyId[5] = '\0'; + return sKeyId; + } +} + +#if defined(_WIN32) && defined(DBG_UTIL) +static int IgnoringCrtReportHook(int reportType, wchar_t *message, int * /* returnValue */) +{ + OUString sType; + if (reportType == _CRT_WARN) + sType = "WARN"; + else if (reportType == _CRT_ERROR) + sType = "ERROR"; + else if (reportType == _CRT_ASSERT) + sType = "ASSERT"; + else + sType = "?(" + OUString::number(reportType) + ")"; + + SAL_WARN("unotools.i18n", "CRT Report Hook: " << sType << ": " << OUString(o3tl::toU(message))); + + return TRUE; +} +#endif + + +namespace Translate +{ + std::locale Create(const char* pPrefixName, const LanguageTag& rLocale) + { + static std::unordered_map<OString, std::locale> aCache; + OString sIdentifier = rLocale.getGlibcLocaleString(".UTF-8").toUtf8(); + OString sUnique = sIdentifier + pPrefixName; + auto aFind = aCache.find(sUnique); + if (aFind != aCache.end()) + return aFind->second; + boost::locale::generator gen; + gen.characters(boost::locale::char_facet); + gen.categories(boost::locale::message_facet | boost::locale::information_facet); +#if defined(ANDROID) + OString sPath(OString(lo_get_app_data_dir()) + "/program/resource"); +#else + OUString uri("$BRAND_BASE_DIR/$BRAND_SHARE_RESOURCE_SUBDIR/"); + rtl::Bootstrap::expandMacros(uri); + OUString path; + osl::File::getSystemPathFromFileURL(uri, path); + OString sPath(OUStringToOString(path, osl_getThreadTextEncoding())); +#endif + gen.add_messages_path(sPath.getStr()); +#if defined UNX && !defined MACOSX && !defined IOS && !defined ANDROID + // allow gettext to find these .mo files e.g. so gtk dialogs can use them + bindtextdomain(pPrefixName, sPath.getStr()); + // tdf#131069 gtk, and anything sane, always wants utf-8 strings as output + bind_textdomain_codeset(pPrefixName, "UTF-8"); +#endif + gen.add_messages_domain(pPrefixName); + +#if defined(_WIN32) && defined(DBG_UTIL) + // With a newer C++ debug runtime (in an --enable-dbgutil build), passing an invalid locale + // name causes an attempt to display an error dialog. Which does not even show up, at least + // for me, but instead the process (gengal, at least) just hangs. Which is far from ideal. + + // Passing a POSIX-style locale name to the std::locale constructor on Windows is a bit odd, + // but apparently in the normal C++ runtime it "just" causes an exception to be thrown, that + // boost catches (see the loadable(std::string name) in boost's + // libs\locale\src\std\std_backend.cpp), and then instead uses the Windows style locale name + // it knows how to construct. (Why does it even try the POSIX style name I can't + // understand.) + + // Actually it isn't just the locale name part "en_US" of a locale like "en_US.UTF-8" that + // is problematic, but also the encoding part, "UTF-8". The Microsoft C/C++ library does not + // support UTF-8 locales. The error message that our own report hook catches says: + // "f:\dd\vctools\crt\crtw32\stdcpp\xmbtowc.c(89) : Assertion failed: ploc->_Mbcurmax == 1 + // || ploc->_Mbcurmax == 2". Clearly in a UTF-8 locale (perhaps one that boost internally + // constructs?) the maximum bytes per character will be more than 2. + + // With a debug C++ runtime, we need to avoid the error dialog, and just ignore the error. + + struct CrtSetReportHook + { + int mnCrtSetReportHookSucceeded; + + CrtSetReportHook() + { + mnCrtSetReportHookSucceeded = _CrtSetReportHookW2(_CRT_RPTHOOK_INSTALL, IgnoringCrtReportHook); + } + + ~CrtSetReportHook() + { + if (mnCrtSetReportHookSucceeded >= 0) + _CrtSetReportHookW2(_CRT_RPTHOOK_REMOVE, IgnoringCrtReportHook); + } + } aHook; + +#endif + + std::locale aRet(gen(sIdentifier.getStr())); + + aCache[sUnique] = aRet; + return aRet; + } + + OUString get(const char* pContextAndId, const std::locale &loc) + { + OString sContext; + const char *pId = strchr(pContextAndId, '\004'); + if (!pId) + pId = pContextAndId; + else + { + sContext = OString(pContextAndId, pId - pContextAndId); + ++pId; + assert(!strchr(pId, '\004') && "should be using nget, not get"); + } + + //if it's a key id locale, generate it here + if (std::use_facet<boost::locale::info>(loc).language() == "qtz") + { + OString sKeyId(genKeyId(OString(pContextAndId).replace('\004', '|'))); + return OUString::fromUtf8(sKeyId) + u"\u2016" + createFromUtf8(pId, strlen(pId)); + } + + //otherwise translate it + const std::string ret = boost::locale::pgettext(sContext.getStr(), pId, loc); + OUString result(ExpandVariables(createFromUtf8(ret.data(), ret.size()))); + + if (comphelper::LibreOfficeKit::isActive()) + { + // If it is de-CH, change sharp s to double s. + if (std::use_facet<boost::locale::info>(loc).country() == "CH" && + std::use_facet<boost::locale::info>(loc).language() == "de") + result = result.replaceAll(OUString::fromUtf8("\xC3\x9F"), "ss"); + } + return result; + } + + OUString nget(const char* pContextAndIds, int n, const std::locale &loc) + { + OString sContextIdId(pContextAndIds); + std::vector<OString> aContextIdId; + sal_Int32 nIndex = 0; + do + { + aContextIdId.push_back(sContextIdId.getToken(0, '\004', nIndex)); + } + while (nIndex >= 0); + assert(aContextIdId.size() == 3 && "should be using get, not nget"); + + //if it's a key id locale, generate it here + if (std::use_facet<boost::locale::info>(loc).language() == "qtz") + { + OString sKeyId(genKeyId(aContextIdId[0] + "|" + aContextIdId[1])); + int nForm = n == 0 ? 1 : 2; + return OUString::fromUtf8(sKeyId) + u"\u2016" + createFromUtf8(aContextIdId[nForm].getStr(), aContextIdId[nForm].getLength()); + } + + //otherwise translate it + const std::string ret = boost::locale::npgettext(aContextIdId[0].getStr(), aContextIdId[1].getStr(), aContextIdId[2].getStr(), n, loc); + OUString result(ExpandVariables(createFromUtf8(ret.data(), ret.size()))); + + if (comphelper::LibreOfficeKit::isActive()) + { + if (std::use_facet<boost::locale::info>(loc).country() == "CH" && + std::use_facet<boost::locale::info>(loc).language() == "de") + result = result.replaceAll(OUString::fromUtf8("\xC3\x9F"), "ss"); + } + return result; + } + + static ResHookProc pImplResHookProc = nullptr; + + OUString ExpandVariables(const OUString& rString) + { + if (pImplResHookProc) + return pImplResHookProc(rString); + return rString; + } + + void SetReadStringHook( ResHookProc pProc ) + { + pImplResHookProc = pProc; + } + + ResHookProc GetReadStringHook() + { + return pImplResHookProc; + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/i18n/textsearch.cxx b/unotools/source/i18n/textsearch.cxx new file mode 100644 index 000000000..42b995f70 --- /dev/null +++ b/unotools/source/i18n/textsearch.cxx @@ -0,0 +1,401 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <cstdlib> +#include <string_view> + +#include <i18nlangtag/languagetag.hxx> +#include <i18nutil/searchopt.hxx> +#include <i18nutil/transliteration.hxx> +#include <com/sun/star/util/TextSearch2.hpp> +#include <com/sun/star/util/SearchAlgorithms2.hpp> +#include <com/sun/star/util/SearchFlags.hpp> +#include <sal/log.hxx> +#include <unotools/charclass.hxx> +#include <comphelper/processfactory.hxx> +#include <unotools/textsearch.hxx> +#include <rtl/instance.hxx> +#include <rtl/ustrbuf.hxx> + +using namespace ::com::sun::star::util; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::lang; + +namespace utl +{ + +SearchParam::SearchParam( const OUString &rText, + SearchType eType, + bool bCaseSensitive, + sal_uInt32 cWildEscChar, + bool bWildMatchSel ) +{ + sSrchStr = rText; + m_eSrchType = eType; + + m_cWildEscChar = cWildEscChar; + + m_bCaseSense = bCaseSensitive; + m_bWildMatchSel = bWildMatchSel; +} + +SearchParam::SearchParam( const SearchParam& rParam ) +{ + sSrchStr = rParam.sSrchStr; + m_eSrchType = rParam.m_eSrchType; + + m_cWildEscChar = rParam.m_cWildEscChar; + + m_bCaseSense = rParam.m_bCaseSense; + m_bWildMatchSel = rParam.m_bWildMatchSel; +} + +SearchParam::~SearchParam() {} + +static bool lcl_Equals( const i18nutil::SearchOptions2& rSO1, const i18nutil::SearchOptions2& rSO2 ) +{ + return + rSO1.AlgorithmType2 == rSO2.AlgorithmType2 && + rSO1.WildcardEscapeCharacter == rSO2.WildcardEscapeCharacter && + rSO1.algorithmType == rSO2.algorithmType && + rSO1.searchFlag == rSO2.searchFlag && + rSO1.searchString == rSO2.searchString && + rSO1.replaceString == rSO2.replaceString && + rSO1.changedChars == rSO2.changedChars && + rSO1.deletedChars == rSO2.deletedChars && + rSO1.insertedChars == rSO2.insertedChars && + rSO1.Locale.Language == rSO2.Locale.Language && + rSO1.Locale.Country == rSO2.Locale.Country && + rSO1.Locale.Variant == rSO2.Locale.Variant && + rSO1.transliterateFlags == rSO2.transliterateFlags; +} + +namespace +{ + struct CachedTextSearch + { + ::osl::Mutex mutex; + i18nutil::SearchOptions2 Options; + css::uno::Reference< css::util::XTextSearch2 > xTextSearch; + }; + + struct theCachedTextSearch + : public rtl::Static< CachedTextSearch, theCachedTextSearch > {}; +} + +Reference<XTextSearch2> TextSearch::getXTextSearch( const i18nutil::SearchOptions2& rPara ) +{ + CachedTextSearch &rCache = theCachedTextSearch::get(); + + osl::MutexGuard aGuard(rCache.mutex); + + if ( lcl_Equals(rCache.Options, rPara) ) + return rCache.xTextSearch; + + Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext(); + rCache.xTextSearch.set( ::TextSearch2::create(xContext) ); + rCache.xTextSearch->setOptions2( rPara.toUnoSearchOptions2() ); + rCache.Options = rPara; + + return rCache.xTextSearch; +} + +TextSearch::TextSearch(const SearchParam & rParam, LanguageType eLang ) +{ + if( LANGUAGE_NONE == eLang ) + eLang = LANGUAGE_SYSTEM; + css::lang::Locale aLocale( LanguageTag::convertToLocale( eLang ) ); + + Init( rParam, aLocale); +} + +TextSearch::TextSearch(const SearchParam & rParam, const CharClass& rCClass ) +{ + Init( rParam, rCClass.getLanguageTag().getLocale() ); +} + +TextSearch::TextSearch( const i18nutil::SearchOptions2& rPara ) +{ + xTextSearch = getXTextSearch( rPara ); +} + +i18nutil::SearchOptions2 TextSearch::UpgradeToSearchOptions2( const i18nutil::SearchOptions& rOptions ) +{ + sal_Int16 nAlgorithmType2; + switch (rOptions.algorithmType) + { + case SearchAlgorithms_REGEXP: + nAlgorithmType2 = SearchAlgorithms2::REGEXP; + break; + case SearchAlgorithms_APPROXIMATE: + nAlgorithmType2 = SearchAlgorithms2::APPROXIMATE; + break; + case SearchAlgorithms_ABSOLUTE: + nAlgorithmType2 = SearchAlgorithms2::ABSOLUTE; + break; + default: + for (;;) std::abort(); + } + // It would be nice if an inherited struct had a ctor that takes an + // instance of the object the struct derived from... + i18nutil::SearchOptions2 aOptions2( + rOptions.algorithmType, + rOptions.searchFlag, + rOptions.searchString, + rOptions.replaceString, + rOptions.Locale, + rOptions.changedChars, + rOptions.deletedChars, + rOptions.insertedChars, + rOptions.transliterateFlags, + nAlgorithmType2, + 0 // no wildcard search, no escape character... + ); + return aOptions2; +} + +void TextSearch::Init( const SearchParam & rParam, + const css::lang::Locale& rLocale ) +{ + // convert SearchParam to the UNO SearchOptions2 + i18nutil::SearchOptions2 aSOpt; + + switch( rParam.GetSrchType() ) + { + case SearchParam::SearchType::Wildcard: + aSOpt.AlgorithmType2 = SearchAlgorithms2::WILDCARD; + aSOpt.algorithmType = SearchAlgorithms::SearchAlgorithms_MAKE_FIXED_SIZE; // no old enum for that + aSOpt.WildcardEscapeCharacter = rParam.GetWildEscChar(); + if (rParam.IsWildMatchSel()) + aSOpt.searchFlag |= SearchFlags::WILD_MATCH_SELECTION; + break; + + case SearchParam::SearchType::Regexp: + aSOpt.AlgorithmType2 = SearchAlgorithms2::REGEXP; + aSOpt.algorithmType = SearchAlgorithms_REGEXP; + break; + + case SearchParam::SearchType::Normal: + aSOpt.AlgorithmType2 = SearchAlgorithms2::ABSOLUTE; + aSOpt.algorithmType = SearchAlgorithms_ABSOLUTE; + break; + + default: + for (;;) std::abort(); + } + aSOpt.searchString = rParam.GetSrchStr(); + aSOpt.replaceString = ""; + aSOpt.Locale = rLocale; + aSOpt.transliterateFlags = TransliterationFlags::NONE; + if( !rParam.IsCaseSensitive() ) + { + aSOpt.searchFlag |= SearchFlags::ALL_IGNORE_CASE; + aSOpt.transliterateFlags |= TransliterationFlags::IGNORE_CASE; + } + + xTextSearch = getXTextSearch( aSOpt ); +} + +void TextSearch::SetLocale( const i18nutil::SearchOptions2& rOptions, + const css::lang::Locale& rLocale ) +{ + i18nutil::SearchOptions2 aSOpt( rOptions ); + aSOpt.Locale = rLocale; + + xTextSearch = getXTextSearch( aSOpt ); +} + +TextSearch::~TextSearch() +{ +} + +/* + * General search methods. These methods will call the respective + * methods, such as ordinary string searching or regular expression + * matching, using the method pointer. + */ +bool TextSearch::SearchForward( const OUString &rStr, + sal_Int32* pStart, sal_Int32* pEnd, + css::util::SearchResult* pRes) +{ + bool bRet = false; + try + { + if( xTextSearch.is() ) + { + SearchResult aRet( xTextSearch->searchForward( rStr, *pStart, *pEnd )); + if( aRet.subRegExpressions > 0 ) + { + bRet = true; + // the XTextsearch returns in startOffset the higher position + // and the endposition is always exclusive. + // The caller of this function will have in startPos the + // lower pos. and end + *pStart = aRet.startOffset[ 0 ]; + *pEnd = aRet.endOffset[ 0 ]; + if( pRes ) + *pRes = aRet; + } + } + } + catch ( Exception& ) + { + SAL_WARN( "unotools.i18n", "SearchForward: Exception caught!" ); + } + return bRet; +} + +bool TextSearch::searchForward( const OUString &rStr ) +{ + sal_Int32 pStart = 0; + sal_Int32 pEnd = rStr.getLength(); + + bool bResult = SearchForward(rStr, &pStart, &pEnd); + + return bResult; +} + +bool TextSearch::SearchBackward( const OUString & rStr, sal_Int32* pStart, + sal_Int32* pEnd, SearchResult* pRes ) +{ + bool bRet = false; + try + { + if( xTextSearch.is() ) + { + SearchResult aRet( xTextSearch->searchBackward( rStr, *pStart, *pEnd )); + if( aRet.subRegExpressions ) + { + bRet = true; + // the XTextsearch returns in startOffset the higher position + // and the endposition is always exclusive. + // The caller of this function will have in startPos the + // lower pos. and end + *pEnd = aRet.startOffset[ 0 ]; + *pStart = aRet.endOffset[ 0 ]; + if( pRes ) + *pRes = aRet; + } + } + } + catch ( Exception& ) + { + SAL_WARN( "unotools.i18n", "SearchBackward: Exception caught!" ); + } + return bRet; +} + +void TextSearch::ReplaceBackReferences( OUString& rReplaceStr, const OUString &rStr, const SearchResult& rResult ) const +{ + if( rResult.subRegExpressions > 0 ) + { + sal_Unicode sFndChar; + sal_Int32 i; + OUStringBuffer sBuff(rReplaceStr.getLength()*4); + for(i = 0; i < rReplaceStr.getLength(); i++) + { + if( rReplaceStr[i] == '&') + { + sal_Int32 nStart = rResult.startOffset[0]; + sal_Int32 nLength = rResult.endOffset[0] - rResult.startOffset[0]; + sBuff.append(std::u16string_view(rStr).substr(nStart, nLength)); + } + else if((i < rReplaceStr.getLength() - 1) && rReplaceStr[i] == '$') + { + sFndChar = rReplaceStr[ i + 1 ]; + switch(sFndChar) + { // placeholder for a backward reference? + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + { + int j = sFndChar - '0'; // index + if(j < rResult.subRegExpressions) + { + sal_Int32 nSttReg = rResult.startOffset[j]; + sal_Int32 nRegLen = rResult.endOffset[j]; + if (nSttReg < 0 || nRegLen < 0) // A "not found" optional capture + { + nSttReg = nRegLen = 0; // Copy empty string + } + else if (nRegLen >= nSttReg) + { + nRegLen = nRegLen - nSttReg; + } + else + { + nRegLen = nSttReg - nRegLen; + nSttReg = rResult.endOffset[j]; + } + // Copy reference from found string + sBuff.append(std::u16string_view(rStr).substr(nSttReg, nRegLen)); + } + i += 1; + } + break; + default: + sBuff.append(rReplaceStr[i]); + sBuff.append(rReplaceStr[i+1]); + i += 1; + break; + } + } + else if((i < rReplaceStr.getLength() - 1) && rReplaceStr[i] == '\\') + { + sFndChar = rReplaceStr[ i+1 ]; + switch(sFndChar) + { + case '\\': + case '&': + case '$': + sBuff.append(sFndChar); + i+=1; + break; + case 't': + sBuff.append('\t'); + i += 1; + break; + default: + sBuff.append(rReplaceStr[i]); + sBuff.append(rReplaceStr[i+1]); + i += 1; + break; + } + } + else + { + sBuff.append(rReplaceStr[i]); + } + } + rReplaceStr = sBuff.makeStringAndClear(); + } +} + +} // namespace utl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/i18n/transliterationwrapper.cxx b/unotools/source/i18n/transliterationwrapper.cxx new file mode 100644 index 000000000..5deb0e5a6 --- /dev/null +++ b/unotools/source/i18n/transliterationwrapper.cxx @@ -0,0 +1,232 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <sal/log.hxx> +#include <unotools/transliterationwrapper.hxx> +#include <i18nlangtag/languagetag.hxx> +#include <i18nutil/transliteration.hxx> +#include <tools/diagnose_ex.h> + +#include <com/sun/star/i18n/Transliteration.hpp> + +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::i18n; +using namespace ::com::sun::star::uno; +using namespace ::utl; + +TransliterationWrapper::TransliterationWrapper( + const Reference< XComponentContext > & rxContext, + TransliterationFlags nTyp ) + : xTrans( Transliteration::create(rxContext) ), + aLanguageTag( LANGUAGE_SYSTEM ), nType( nTyp ), bFirstCall( true ) +{ +} + +TransliterationWrapper::~TransliterationWrapper() +{ +} + +OUString TransliterationWrapper::transliterate(const OUString& rStr, LanguageType nLang, + sal_Int32 nStart, sal_Int32 nLen, + Sequence <sal_Int32>* pOffset ) +{ + OUString sRet; + if( xTrans.is() ) + { + try + { + loadModuleIfNeeded( nLang ); + + if ( pOffset ) + sRet = xTrans->transliterate( rStr, nStart, nLen, *pOffset ); + else + sRet = xTrans->transliterateString2String( rStr, nStart, nLen); + } + catch( Exception& ) + { + SAL_WARN( "unotools.i18n", "transliterate: Exception caught!" ); + } + } + return sRet; +} + +OUString TransliterationWrapper::transliterate( const OUString& rStr, + sal_Int32 nStart, sal_Int32 nLen ) const +{ + OUString sRet( rStr ); + if( xTrans.is() ) + { + try + { + sRet = xTrans->transliterateString2String( rStr, nStart, nLen); + } + catch( Exception& ) + { + SAL_WARN( "unotools.i18n", "transliterate: Exception caught!" ); + } + } + return sRet; +} + +bool TransliterationWrapper::needLanguageForTheMode() const +{ + return TransliterationFlags::UPPERCASE_LOWERCASE == nType || + TransliterationFlags::LOWERCASE_UPPERCASE == nType || + TransliterationFlags::IGNORE_CASE == nType || + TransliterationFlags::SENTENCE_CASE == nType || + TransliterationFlags::TITLE_CASE == nType || + TransliterationFlags::TOGGLE_CASE == nType; +} + +void TransliterationWrapper::setLanguageLocaleImpl( LanguageType nLang ) +{ + if( LANGUAGE_NONE == nLang ) + nLang = LANGUAGE_SYSTEM; + aLanguageTag.reset( nLang); +} + +void TransliterationWrapper::loadModuleIfNeeded( LanguageType nLang ) +{ + bool bLoad = bFirstCall; + bFirstCall = false; + + if( nType == TransliterationFlags::SENTENCE_CASE ) + { + if( bLoad ) + loadModuleByImplName("SENTENCE_CASE", nLang); + } + else if( nType == TransliterationFlags::TITLE_CASE ) + { + if( bLoad ) + loadModuleByImplName("TITLE_CASE", nLang); + } + else if( nType == TransliterationFlags::TOGGLE_CASE ) + { + if( bLoad ) + loadModuleByImplName("TOGGLE_CASE", nLang); + } + else + { + if( aLanguageTag.getLanguageType() != nLang ) + { + setLanguageLocaleImpl( nLang ); + if( !bLoad ) + bLoad = needLanguageForTheMode(); + } + if( bLoad ) + loadModuleImpl(); + } +} + +void TransliterationWrapper::loadModuleImpl() const +{ + if ( bFirstCall ) + const_cast<TransliterationWrapper*>(this)->setLanguageLocaleImpl( LANGUAGE_SYSTEM ); + + try + { + if ( xTrans.is() ) + xTrans->loadModule( static_cast<TransliterationModules>(nType), aLanguageTag.getLocale() ); + } + catch ( const Exception& ) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "loadModuleImpl" ); + } + + bFirstCall = false; +} + +void TransliterationWrapper::loadModuleByImplName(const OUString& rModuleName, + LanguageType nLang ) +{ + try + { + setLanguageLocaleImpl( nLang ); + css::lang::Locale aLocale( aLanguageTag.getLocale()); + // Reset LanguageTag, so the next call to loadModuleIfNeeded() forces + // new settings. + aLanguageTag.reset( LANGUAGE_DONTKNOW); + if ( xTrans.is() ) + xTrans->loadModuleByImplName( rModuleName, aLocale ); + } + catch ( const Exception& ) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "loadModuleByImplName" ); + } + + bFirstCall = false; +} + +bool TransliterationWrapper::equals( + const OUString& rStr1, sal_Int32 nPos1, sal_Int32 nCount1, sal_Int32& nMatch1, + const OUString& rStr2, sal_Int32 nPos2, sal_Int32 nCount2, sal_Int32& nMatch2 ) const +{ + try + { + if( bFirstCall ) + loadModuleImpl(); + if ( xTrans.is() ) + return xTrans->equals( rStr1, nPos1, nCount1, nMatch1, rStr2, nPos2, nCount2, nMatch2 ); + } + catch ( const Exception& ) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "equals" ); + } + return false; +} + +sal_Int32 TransliterationWrapper::compareString( const OUString& rStr1, const OUString& rStr2 ) const +{ + try + { + if( bFirstCall ) + loadModuleImpl(); + if ( xTrans.is() ) + return xTrans->compareString( rStr1, rStr2 ); + } + catch (const Exception&) + { + TOOLS_WARN_EXCEPTION( "unotools.i18n", "compareString" ); + } + return 0; +} + +// --- helpers -------------------------------------------------------- + +bool TransliterationWrapper::isEqual( const OUString& rStr1, const OUString& rStr2 ) const +{ + sal_Int32 nMatch1(0), nMatch2(0); + bool bMatch = equals( + rStr1, 0, rStr1.getLength(), nMatch1, + rStr2, 0, rStr2.getLength(), nMatch2 ); + return bMatch; +} + +bool TransliterationWrapper::isMatch( const OUString& rStr1, const OUString& rStr2 ) const +{ + sal_Int32 nMatch1(0), nMatch2(0); + equals( + rStr1, 0, rStr1.getLength(), nMatch1, + rStr2, 0, rStr2.getLength(), nMatch2 ); + return (nMatch1 <= nMatch2) && (nMatch1 == rStr1.getLength()); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/misc/ServiceDocumenter.cxx b/unotools/source/misc/ServiceDocumenter.cxx new file mode 100644 index 000000000..0823d2a66 --- /dev/null +++ b/unotools/source/misc/ServiceDocumenter.cxx @@ -0,0 +1,74 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + */ +#include "ServiceDocumenter.hxx" +#include <unotoolsservices.hxx> +#include <comphelper/servicedecl.hxx> +#include <com/sun/star/system/SystemShellExecuteFlags.hpp> +#include <com/sun/star/system/XSystemShellExecute.hpp> +using namespace com::sun::star; +using uno::Reference; +using lang::XServiceInfo; +using lang::XTypeProvider; + +void unotools::misc::ServiceDocumenter::showCoreDocs(const Reference<XServiceInfo>& xService) +{ + if(!xService.is()) + return; + auto xMSF(m_xContext->getServiceManager()); + Reference<system::XSystemShellExecute> xShell(xMSF->createInstanceWithContext("com.sun.star.system.SystemShellExecute", m_xContext), uno::UNO_QUERY); + xShell->execute( + m_sCoreBaseUrl + xService->getImplementationName() + ".html", "", + css::system::SystemShellExecuteFlags::URIS_ONLY); +} + +void unotools::misc::ServiceDocumenter::showInterfaceDocs(const Reference<XTypeProvider>& xTypeProvider) +{ + if(!xTypeProvider.is()) + return; + auto xMSF(m_xContext->getServiceManager()); + Reference<system::XSystemShellExecute> xShell(xMSF->createInstanceWithContext("com.sun.star.system.SystemShellExecute", m_xContext), uno::UNO_QUERY); + const css::uno::Sequence<css::uno::Type> aTypes = xTypeProvider->getTypes(); + for(const auto& aType : aTypes) + { + auto sUrl = aType.getTypeName(); + sal_Int32 nIdx = 0; + while(nIdx != -1) + sUrl = sUrl.replaceFirst(".", "_1_1", &nIdx); + xShell->execute( + m_sServiceBaseUrl + "/interface" + sUrl + ".html", "", + css::system::SystemShellExecuteFlags::URIS_ONLY); + } +} + +void unotools::misc::ServiceDocumenter::showServiceDocs(const Reference<XServiceInfo>& xService) +{ + if(!xService.is()) + return; + auto xMSF(m_xContext->getServiceManager()); + Reference<system::XSystemShellExecute> xShell(xMSF->createInstanceWithContext("com.sun.star.system.SystemShellExecute", m_xContext), uno::UNO_QUERY); + const css::uno::Sequence<OUString> aServiceNames = xService->getSupportedServiceNames(); + for(const auto& sService : aServiceNames) + { + auto sUrl = sService; + sal_Int32 nIdx = 0; + while(nIdx != -1) + sUrl = sUrl.replaceFirst(".", "_1_1", &nIdx); + xShell->execute( + m_sServiceBaseUrl + "/service" + sUrl + ".html", "", + css::system::SystemShellExecuteFlags::URIS_ONLY); + } +} + +namespace sdecl = ::comphelper::service_decl; +sdecl::class_< unotools::misc::ServiceDocumenter > const ServiceDocumenterImpl; +const sdecl::ServiceDecl ServiceDocumenterDecl( + ServiceDocumenterImpl, + "com.sun.star.comp.unotools.misc.ServiceDocumenter", + ""); + diff --git a/unotools/source/misc/ServiceDocumenter.hxx b/unotools/source/misc/ServiceDocumenter.hxx new file mode 100644 index 000000000..cad333ddb --- /dev/null +++ b/unotools/source/misc/ServiceDocumenter.hxx @@ -0,0 +1,51 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + */ +#ifndef INCLUDED_UNOTOOLS_INC_SERVICEDOCUMENTER_HXX +#define INCLUDED_UNOTOOLS_INC_SERVICEDOCUMENTER_HXX + +#include <cppuhelper/implbase.hxx> +#include <com/sun/star/script/XServiceDocumenter.hpp> + +namespace com::sun::star::uno { class XComponentContext; } + +namespace unotools::misc { + +class ServiceDocumenter : public ::cppu::WeakImplHelper< + css::script::XServiceDocumenter> +{ + public: + ServiceDocumenter(css::uno::Reference< css::uno::XComponentContext> const& xContext) + : m_xContext(xContext) + , m_sCoreBaseUrl("http://example.com") + , m_sServiceBaseUrl("https://api.libreoffice.org/docs/idl/ref") + {}; + // XServiceDocumenter + virtual OUString SAL_CALL getCoreBaseUrl() override + { return m_sCoreBaseUrl; }; + virtual void SAL_CALL setCoreBaseUrl( const OUString& sCoreBaseUrl ) override + { m_sCoreBaseUrl = sCoreBaseUrl; }; + virtual OUString SAL_CALL getServiceBaseUrl() override + { return m_sServiceBaseUrl; }; + virtual void SAL_CALL setServiceBaseUrl( const OUString& sServiceBaseUrl ) override + { m_sServiceBaseUrl = sServiceBaseUrl; }; + virtual void SAL_CALL showServiceDocs( const ::css::uno::Reference< ::css::lang::XServiceInfo >& xService) override; + virtual void SAL_CALL showInterfaceDocs( const ::css::uno::Reference< ::css::lang::XTypeProvider >& xTypeProvider ) override; + virtual void SAL_CALL showCoreDocs( const ::css::uno::Reference< ::css::lang::XServiceInfo >& xService) override; + protected: + virtual ~ServiceDocumenter() override + {}; + private: + css::uno::Reference< css::uno::XComponentContext> m_xContext; + OUString m_sCoreBaseUrl; + OUString m_sServiceBaseUrl; +}; + +} +#endif +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/misc/ZipPackageHelper.cxx b/unotools/source/misc/ZipPackageHelper.cxx new file mode 100644 index 000000000..d690e3015 --- /dev/null +++ b/unotools/source/misc/ZipPackageHelper.cxx @@ -0,0 +1,183 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <com/sun/star/io/XActiveDataSink.hpp> +#include <com/sun/star/beans/NamedValue.hpp> +#include <com/sun/star/container/XNamed.hpp> +#include <com/sun/star/container/XChild.hpp> +#include <com/sun/star/container/XNameContainer.hpp> +#include <com/sun/star/util/XChangesBatch.hpp> +#include <com/sun/star/lang/XUnoTunnel.hpp> +#include <com/sun/star/uno/XComponentContext.hpp> +#include <com/sun/star/container/XHierarchicalNameAccess.hpp> +#include <com/sun/star/lang/XSingleServiceFactory.hpp> + +#include <unotools/ZipPackageHelper.hxx> +#include <comphelper/storagehelper.hxx> +#include <osl/file.hxx> +#include <unotools/streamwrap.hxx> +#include <tools/stream.hxx> +#include <tools/urlobj.hxx> + +#include <rtl/uri.hxx> + +namespace com::sun::star::io { class XInputStream; } + +using namespace utl; +using namespace osl; +using namespace comphelper; +using namespace com::sun::star; +using namespace com::sun::star::lang; +using namespace com::sun::star::uno; +using namespace com::sun::star::util; +using namespace com::sun::star::container; +using namespace com::sun::star::beans; +using namespace com::sun::star::io; + +using ::rtl::Uri; + +ZipPackageHelper::ZipPackageHelper( + const Reference< XComponentContext >& rxContext, + const OUString& sPackageURL) +: mxContext( rxContext ), + mxHNameAccess(), + mxFactory(), + mxRootFolder() +{ + // create the package zip file + Sequence< Any > aArguments( 2 ); + aArguments[ 0 ] <<= sPackageURL; + + // let ZipPackage be used + beans::NamedValue aArg; + aArg.Name = "StorageFormat"; + aArg.Value <<= OUString(ZIP_STORAGE_FORMAT_STRING); + aArguments[ 1 ] <<= aArg; + + Reference< XHierarchicalNameAccess > xHNameAccess( + mxContext->getServiceManager()->createInstanceWithArgumentsAndContext( + "com.sun.star.packages.comp.ZipPackage", + aArguments, mxContext ), UNO_QUERY); + mxHNameAccess = xHNameAccess; + + if( !mxHNameAccess.is() ) + return; + + Reference<XSingleServiceFactory> xFactory(mxHNameAccess, UNO_QUERY); + mxFactory = xFactory; + + // get root zip folder + mxHNameAccess->getByHierarchicalName( "/" ) >>= mxRootFolder; +} + +static OUString encodeZipUri( const OUString& rURI ) +{ + return Uri::encode( rURI, rtl_UriCharClassUric, rtl_UriEncodeCheckEscapes, RTL_TEXTENCODING_UTF8 ); +} + +Reference< XInterface >& ZipPackageHelper::getRootFolder() +{ + return mxRootFolder; +} + +Reference< XInterface > ZipPackageHelper::addFolder( Reference< XInterface > const & xRootFolder, + const OUString& rName ) +{ + if ( rName == ".." || rName == "." ) + throw lang::IllegalArgumentException(); + + Sequence< Any > aArgs(1); + aArgs[0] <<= true; + + Reference< XInterface > xFolder( mxFactory->createInstanceWithArguments(aArgs) ); + Reference< XNamed > xNamed( xFolder, UNO_QUERY ); + Reference< XChild > xChild( xFolder, UNO_QUERY ); + + if( xNamed.is() && xChild.is() ) + { + OUString aName( encodeZipUri( rName ) ); + xNamed->setName( aName ); + xChild->setParent( xRootFolder ); + } + + return xFolder; +} + +void ZipPackageHelper::addFolderWithContent( Reference< XInterface > const & xRootFolder, const OUString& rDirURL ) +{ + if (rDirURL.isEmpty()) + return; + + osl::Directory aDirectory(rDirURL); + + if (aDirectory.open() != osl::FileBase::E_None) + return; + + osl::DirectoryItem aDirectoryItem; + + while (osl::FileBase::E_None == aDirectory.getNextItem(aDirectoryItem)) + { + osl::FileStatus aFileStatus(osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL | osl_FileStatus_Mask_FileName); + + if (osl::FileBase::E_None == aDirectoryItem.getFileStatus(aFileStatus)) + { + if (aFileStatus.isDirectory()) + { + const OUString aFileName(aFileStatus.getFileName()); + + if (!aFileName.isEmpty()) + { + Reference<XInterface> folder(addFolder(xRootFolder, aFileName)); + addFolderWithContent(folder, aFileStatus.getFileURL()); + } + } + else if (aFileStatus.isRegular()) + { + addFile(xRootFolder, aFileStatus.getFileURL()); + } + } + } +} + +void ZipPackageHelper::addFile( css::uno::Reference< css::uno::XInterface > const & xRootFolder, + const OUString& rSourceFileURL ) +{ + INetURLObject aURL( rSourceFileURL ); + OUString aName( aURL.getName() ); + + SvFileStream* pStream = new SvFileStream(rSourceFileURL, StreamMode::READ ); + Reference< XInputStream > xInput( new utl::OSeekableInputStreamWrapper( pStream, true ) ); + Reference< XActiveDataSink > xSink( mxFactory->createInstance(), UNO_QUERY ); + Reference< XUnoTunnel > xTunnel( xSink, UNO_QUERY ); + if( !xSink.is() || !xTunnel.is()) + return; + + Reference< XNameContainer > xNameContainer(xRootFolder, UNO_QUERY ); + xNameContainer->insertByName(encodeZipUri( aName ), makeAny(xTunnel)); + xSink->setInputStream( xInput ); +} + +void ZipPackageHelper::savePackage() +{ + Reference< XChangesBatch > xBatch( mxHNameAccess, UNO_QUERY ); + if( xBatch.is() ) + xBatch->commitChanges(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/misc/closeveto.cxx b/unotools/source/misc/closeveto.cxx new file mode 100644 index 000000000..bf2cb8382 --- /dev/null +++ b/unotools/source/misc/closeveto.cxx @@ -0,0 +1,147 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unotools/closeveto.hxx> + +#include <com/sun/star/util/CloseVetoException.hpp> +#include <com/sun/star/util/XCloseable.hpp> + +#include <cppuhelper/implbase.hxx> +#include <rtl/ref.hxx> +#include <tools/diagnose_ex.h> + +namespace utl +{ + + using ::com::sun::star::uno::Reference; + using ::com::sun::star::uno::XInterface; + using ::com::sun::star::uno::UNO_QUERY; + using ::com::sun::star::uno::Exception; + using ::com::sun::star::util::XCloseable; + using ::com::sun::star::util::XCloseListener; + using ::com::sun::star::util::CloseVetoException; + using ::com::sun::star::lang::EventObject; + + //= CloseListener_Impl + + typedef ::cppu::WeakImplHelper < XCloseListener + > CloseListener_Base; + + namespace { + + class CloseListener_Impl : public CloseListener_Base + { + public: + explicit CloseListener_Impl(bool const bHasOwnership) + : m_bHasOwnership(bHasOwnership) + { + } + + // XCloseListener + virtual void SAL_CALL queryClosing( const EventObject& Source, sal_Bool GetsOwnership ) override; + virtual void SAL_CALL notifyClosing( const EventObject& Source ) override; + + // XEventListener + virtual void SAL_CALL disposing( const EventObject& Source) override; + + bool hasOwnership() const { return m_bHasOwnership; } + + protected: + virtual ~CloseListener_Impl() override + { + } + + private: + bool m_bHasOwnership; + }; + + } + + void SAL_CALL CloseListener_Impl::queryClosing( const EventObject&, sal_Bool i_deliverOwnership ) + { + if ( !m_bHasOwnership ) + m_bHasOwnership = i_deliverOwnership; + + throw CloseVetoException(); + } + + void SAL_CALL CloseListener_Impl::notifyClosing( const EventObject& ) {} + + void SAL_CALL CloseListener_Impl::disposing( const EventObject& ) {} + + //= CloseVeto_Data + + struct CloseVeto_Data + { + Reference< XCloseable > xCloseable; + ::rtl::Reference< CloseListener_Impl > pListener; + }; + + //= operations + + namespace + { + + void lcl_init( CloseVeto_Data& i_data, const Reference< XInterface >& i_closeable, + bool const hasOwnership) + { + i_data.xCloseable.set( i_closeable, UNO_QUERY ); + ENSURE_OR_RETURN_VOID( i_data.xCloseable.is(), "CloseVeto: the component is not closeable!" ); + + i_data.pListener = new CloseListener_Impl(hasOwnership); + i_data.xCloseable->addCloseListener( i_data.pListener.get() ); + } + + void lcl_deinit( CloseVeto_Data const & i_data ) + { + if ( !i_data.xCloseable.is() ) + return; + + i_data.xCloseable->removeCloseListener( i_data.pListener.get() ); + if ( i_data.pListener->hasOwnership() ) + { + try + { + i_data.xCloseable->close( true ); + } + catch( const CloseVetoException& ) { } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION("unotools"); + } + } + } + } + + //= CloseVeto + CloseVeto::CloseVeto(const Reference< XInterface >& i_closeable, + bool const hasOwnership) + : m_xData(new CloseVeto_Data) + { + lcl_init(*m_xData, i_closeable, hasOwnership); + } + + CloseVeto::~CloseVeto() + { + lcl_deinit(*m_xData); + } + +} // namespace utl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/misc/datetime.cxx b/unotools/source/misc/datetime.cxx new file mode 100644 index 000000000..703ce9127 --- /dev/null +++ b/unotools/source/misc/datetime.cxx @@ -0,0 +1,525 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unotools/datetime.hxx> +#include <unotools/localedatawrapper.hxx> +#include <unotools/syslocale.hxx> +#include <tools/date.hxx> +#include <tools/time.hxx> +#include <tools/datetime.hxx> +#include <rtl/ustrbuf.hxx> +#include <rtl/math.hxx> +#include <osl/diagnose.h> +#include <comphelper/string.hxx> +#include <sstream> + +namespace +{ + bool checkAllNumber(const OUString& rString) + { + sal_Int32 nPos = 0; + sal_Int32 nLen = rString.getLength(); + + // skip white space + while( nPos < nLen && ' ' == rString[nPos] ) + nPos++; + + if( nPos < nLen && '-' == rString[nPos] ) + nPos++; + + // get number + while( nPos < nLen && + '0' <= rString[nPos] && + '9' >= rString[nPos] ) + { + nPos++; + } + + return nPos == nLen; + } + + /** convert string to number with optional min and max values */ + bool convertNumber32(sal_Int32& rValue, + const OUString& rString, + sal_Int32 /*nMin*/ = -1, sal_Int32 /*nMax*/ = -1) + { + if (!checkAllNumber(rString)) + { + rValue = 0; + return false; + } + + rValue = rString.toInt32(); + return true; + } + + bool convertNumber64(sal_Int64& rValue, + const OUString& rString, + sal_Int64 /*nMin*/ = -1, sal_Int64 /*nMax*/ = -1) + { + if (!checkAllNumber(rString)) + { + rValue = 0; + return false; + } + + rValue = rString.toInt64(); + return true; + } + + // although the standard calls for fixed-length (zero-padded) tokens + // (in their integer part), we are here liberal and allow shorter tokens + // (when there are separators, else it is ambiguous). + // Note that: + // the token separator is OPTIONAL + // empty string is a valid token! (to recognise hh or hhmm or hh:mm formats) + // returns: success / failure + // in case of failure, no reference argument is changed + // arguments: + // i_str: string to extract token from + // index: index in i_str where to start tokenizing + // after return, start of *next* token (if any) + // if this was the last token, then the value is UNDEFINED + // o_strInt: output; integer part of token + // o_bFraction: output; was there a fractional part? + // o_strFrac: output; fractional part of token + bool impl_getISO8601TimeToken(const OUString &i_str, sal_Int32 &nPos, OUString &resInt, bool &bFraction, OUString &resFrac) + { + bFraction = false; + // all tokens are of length 2 + const sal_Int32 nEndPos = nPos + 2; + const sal_Unicode c0 = '0'; + const sal_Unicode c9 = '9'; + const sal_Unicode sep = ':'; + for (;nPos < nEndPos && nPos < i_str.getLength(); ++nPos) + { + const sal_Unicode c = i_str[nPos]; + if (c == sep) + return true; + if (c < c0 || c > c9) + return false; + resInt += OUStringChar(c); + } + if (nPos == i_str.getLength() || i_str[nPos] == sep) + return true; + if (i_str[nPos] == ',' || i_str[nPos] == '.') + { + bFraction = true; + ++nPos; + for (; nPos < i_str.getLength(); ++nPos) + { + const sal_Unicode c = i_str[nPos]; + if (c == 'Z' || c == '+' || c == '-') + { + --nPos; // we don't want to skip the tz separator + return true; + } + if (c == sep) + // fractional part allowed only in *last* token + return false; + if (c < c0 || c > c9) + return false; + resFrac += OUStringChar(c); + } + OSL_ENSURE(nPos == i_str.getLength(), "impl_getISO8601TimeToken internal error; expected to be at end of string"); + return true; + } + if (i_str[nPos] == 'Z' || i_str[nPos] == '+' || i_str[nPos] == '-') + { + --nPos; // we don't want to skip the tz separator + return true; + } + else + return false; + } + bool getISO8601TimeToken(const OUString &i_str, sal_Int32 &io_index, OUString &o_strInt, bool &o_bFraction, OUString &o_strFrac) + { + OUString resInt; + OUString resFrac; + bool bFraction = false; + sal_Int32 index = io_index; + if(!impl_getISO8601TimeToken(i_str, index, resInt, bFraction, resFrac)) + return false; + else + { + io_index = index+1; + o_strInt = resInt; + o_strFrac = resFrac; + o_bFraction = bFraction; + return true; + } + } + bool getISO8601TimeZoneToken(const OUString &i_str, sal_Int32 &io_index, OUString &o_strInt) + { + const sal_Unicode c0 = '0'; + const sal_Unicode c9 = '9'; + const sal_Unicode sep = ':'; + if (i_str[io_index] == 'Z') // UTC timezone indicator + { + ++io_index; + o_strInt = "Z"; + return true; + } + else if (i_str[io_index] == '+' || i_str[io_index] == '-') // other timezones indicator + { + ++io_index; + o_strInt.clear(); + for (; io_index < i_str.getLength(); ++io_index) + { + const sal_Unicode c = i_str[io_index]; + if ((c < c0 || c > c9) && c != sep) + return false; + o_strInt += OUStringChar(c); + } + return true; + } + else + return false; + } +} + +namespace utl +{ +const LocaleDataWrapper& GetLocaleData() +{ + static SvtSysLocale ourSysLocale; + return ourSysLocale.GetLocaleData(); +} + +DateTime GetDateTime(const css::util::DateTime& _rDT) { return DateTime(_rDT); } + +OUString GetDateTimeString(const css::util::DateTime& _rDT) +{ + // String with date and time information (#i20172#) + DateTime aDT(GetDateTime(_rDT)); + const LocaleDataWrapper& rLoDa = GetLocaleData(); + + return rLoDa.getDate(aDT) + " " + rLoDa.getTime(aDT); +} + +OUString GetDateTimeString(sal_Int32 _nDate, sal_Int32 _nTime) +{ + const LocaleDataWrapper& rLoDa = GetLocaleData(); + + Date aDate(_nDate); + tools::Time aTime(_nTime * tools::Time::nanoPerCenti); + return rLoDa.getDate(aDate) + ", " + rLoDa.getTime(aTime); +} + +OUString GetDateString(const css::util::DateTime& _rDT) +{ + return GetLocaleData().getDate(GetDateTime(_rDT)); +} + +void typeConvert(const Date& _rDate, css::util::Date& _rOut) +{ + _rOut.Day = _rDate.GetDay(); + _rOut.Month = _rDate.GetMonth(); + _rOut.Year = _rDate.GetYear(); +} + +void typeConvert(const css::util::Date& _rDate, Date& _rOut) +{ + _rOut = Date(_rDate.Day, _rDate.Month, _rDate.Year); +} + +void typeConvert(const DateTime& _rDateTime, css::util::DateTime& _rOut) +{ + _rOut.Year = _rDateTime.GetYear(); + _rOut.Month = _rDateTime.GetMonth(); + _rOut.Day = _rDateTime.GetDay(); + _rOut.Hours = _rDateTime.GetHour(); + _rOut.Minutes = _rDateTime.GetMin(); + _rOut.Seconds = _rDateTime.GetSec(); + _rOut.NanoSeconds = _rDateTime.GetNanoSec(); +} + +void typeConvert(const css::util::DateTime& _rDateTime, DateTime& _rOut) +{ + Date aDate(_rDateTime.Day, _rDateTime.Month, _rDateTime.Year); + tools::Time aTime(_rDateTime.Hours, _rDateTime.Minutes, _rDateTime.Seconds, _rDateTime.NanoSeconds); + _rOut = DateTime(aDate, aTime); +} + +OUString toISO8601(const css::util::DateTime& rDateTime) +{ + OUStringBuffer rBuffer(32); + rBuffer.append(static_cast<sal_Int32>(rDateTime.Year)); + rBuffer.append('-'); + if( rDateTime.Month < 10 ) + rBuffer.append('0'); + rBuffer.append(static_cast<sal_Int32>(rDateTime.Month)); + rBuffer.append('-'); + if( rDateTime.Day < 10 ) + rBuffer.append('0'); + rBuffer.append(static_cast<sal_Int32>(rDateTime.Day)); + + if( rDateTime.NanoSeconds != 0 || + rDateTime.Seconds != 0 || + rDateTime.Minutes != 0 || + rDateTime.Hours != 0 ) + { + rBuffer.append('T'); + if( rDateTime.Hours < 10 ) + rBuffer.append('0'); + rBuffer.append(static_cast<sal_Int32>(rDateTime.Hours)); + rBuffer.append(':'); + if( rDateTime.Minutes < 10 ) + rBuffer.append('0'); + rBuffer.append(static_cast<sal_Int32>(rDateTime.Minutes)); + rBuffer.append(':'); + if( rDateTime.Seconds < 10 ) + rBuffer.append('0'); + rBuffer.append(static_cast<sal_Int32>(rDateTime.Seconds)); + if ( rDateTime.NanoSeconds > 0) + { + OSL_ENSURE(rDateTime.NanoSeconds < 1000000000,"NanoSeconds cannot be more than 999 999 999"); + rBuffer.append(','); + std::ostringstream ostr; + ostr.fill('0'); + ostr.width(9); + ostr << rDateTime.NanoSeconds; + rBuffer.append(OUString::createFromAscii(ostr.str().c_str())); + } + } + return rBuffer.makeStringAndClear(); +} + +/** convert ISO8601 DateTime String to util::DateTime */ +bool ISO8601parseDateTime(const OUString &rString, css::util::DateTime& rDateTime) +{ + bool bSuccess = true; + + OUString aDateStr, aTimeStr; + css::util::Date aDate; + css::util::Time aTime; + sal_Int32 nPos = rString.indexOf( 'T' ); + if ( nPos >= 0 ) + { + aDateStr = rString.copy( 0, nPos ); + aTimeStr = rString.copy( nPos + 1 ); + } + else + aDateStr = rString; // no separator: only date part + + bSuccess = ISO8601parseDate(aDateStr, aDate); + + if ( bSuccess && !aTimeStr.isEmpty() ) // time is optional + { + bSuccess = ISO8601parseTime(aTimeStr, aTime); + } + + if (bSuccess) + { + rDateTime = css::util::DateTime(aTime.NanoSeconds, aTime.Seconds, aTime.Minutes, aTime.Hours, + aDate.Day, aDate.Month, aDate.Year, false); + } + + return bSuccess; +} + +/** convert ISO8601 Date String to util::Date */ +// TODO: supports only calendar dates YYYY-MM-DD +// MISSING: calendar dates YYYYMMDD YYYY-MM +// year, week date, ordinal date +bool ISO8601parseDate(const OUString &aDateStr, css::util::Date& rDate) +{ + const sal_Int32 nDateTokens {comphelper::string::getTokenCount(aDateStr, '-')}; + + if (nDateTokens<1 || nDateTokens>3) + return false; + + sal_Int32 nYear = 1899; + sal_Int32 nMonth = 12; + sal_Int32 nDay = 30; + + sal_Int32 nIdx {0}; + if ( !convertNumber32( nYear, aDateStr.getToken( 0, '-', nIdx ), 0, 9999 ) ) + return false; + if ( nDateTokens >= 2 ) + if ( !convertNumber32( nMonth, aDateStr.getToken( 0, '-', nIdx ), 0, 12 ) ) + return false; + if ( nDateTokens >= 3 ) + if ( !convertNumber32( nDay, aDateStr.getToken( 0, '-', nIdx ), 0, 31 ) ) + return false; + + rDate.Year = static_cast<sal_uInt16>(nYear); + rDate.Month = static_cast<sal_uInt16>(nMonth); + rDate.Day = static_cast<sal_uInt16>(nDay); + + return true; +} + +/** convert ISO8601 Time String to util::Time */ +bool ISO8601parseTime(const OUString &aTimeStr, css::util::Time& rTime) +{ + sal_Int32 nHour = 0; + sal_Int32 nMin = 0; + sal_Int32 nSec = 0; + sal_Int32 nNanoSec = 0; + + sal_Int32 n = 0; + OUString tokInt; + OUString tokFrac; + OUString tokTz; + bool bFrac = false; + // hours + bool bSuccess = getISO8601TimeToken(aTimeStr, n, tokInt, bFrac, tokFrac); + if (!bSuccess) + return false; + + if ( bFrac && n < aTimeStr.getLength()) + { + // is it junk or the timezone? + bSuccess = getISO8601TimeZoneToken(aTimeStr, n, tokTz); + if (!bSuccess) + return false; + } + bSuccess = convertNumber32( nHour, tokInt, 0, 23 ); + if (!bSuccess) + return false; + + if (bFrac) + { + sal_Int64 fracNumerator; + bSuccess = convertNumber64(fracNumerator, tokFrac); + if ( bSuccess ) + { + double frac = static_cast<double>(fracNumerator) / pow(static_cast<double>(10), static_cast<double>(tokFrac.getLength())); + // minutes + OSL_ENSURE(frac < 1 && frac >= 0, "ISO8601parse internal error frac hours (of hours) not between 0 and 1"); + frac *= 60; + nMin = floor(frac); + frac -= nMin; + // seconds + OSL_ENSURE(frac < 1 && frac >= 0, "ISO8601parse internal error frac minutes (of hours) not between 0 and 1"); + frac *= 60; + nSec = floor(frac); + frac -= nSec; + // nanoseconds + OSL_ENSURE(frac < 1 && frac >= 0, "ISO8601parse internal error frac seconds (of hours) not between 0 and 1"); + frac *= 1000000000; + nNanoSec = ::rtl::math::round(frac); + } + goto end; + } + if(n >= aTimeStr.getLength()) + goto end; + + // minutes + bSuccess = getISO8601TimeToken(aTimeStr, n, tokInt, bFrac, tokFrac); + if (!bSuccess) + return false; + if ( bFrac && n < aTimeStr.getLength()) + { + // is it junk or the timezone? + bSuccess = getISO8601TimeZoneToken(aTimeStr, n, tokTz); + if (!bSuccess) + return false; + } + bSuccess = convertNumber32( nMin, tokInt, 0, 59 ); + if (!bSuccess) + return false; + if (bFrac) + { + sal_Int64 fracNumerator; + bSuccess = convertNumber64(fracNumerator, tokFrac); + if ( bSuccess ) + { + double frac = static_cast<double>(fracNumerator) / pow(static_cast<double>(10), static_cast<double>(tokFrac.getLength())); + // seconds + OSL_ENSURE(frac < 1 && frac >= 0, "ISO8601parse internal error frac minutes (of minutes) not between 0 and 1"); + frac *= 60; + nSec = floor(frac); + frac -= nSec; + // nanoseconds + OSL_ENSURE(frac < 1 && frac >= 0, "ISO8601parse internal error frac seconds (of minutes) not between 0 and 1"); + frac *= 1000000000; + nNanoSec = ::rtl::math::round(frac); + } + goto end; + } + if(n >= aTimeStr.getLength()) + goto end; + + // seconds + bSuccess = getISO8601TimeToken(aTimeStr, n, tokInt, bFrac, tokFrac); + if (!bSuccess) + return false; + if (n < aTimeStr.getLength()) + { + // is it junk or the timezone? + bSuccess = getISO8601TimeZoneToken(aTimeStr, n, tokTz); + if (!bSuccess) + return false; + } + // max 60 for leap seconds + bSuccess = convertNumber32( nSec, tokInt, 0, 60 ); + if (!bSuccess) + return false; + if (bFrac) + { + sal_Int64 fracNumerator; + bSuccess = convertNumber64(fracNumerator, tokFrac); + if ( bSuccess ) + { + double frac = static_cast<double>(fracNumerator) / pow(static_cast<double>(10), static_cast<double>(tokFrac.getLength())); + // nanoseconds + OSL_ENSURE(frac < 1 && frac >= 0, "ISO8601parse internal error frac seconds (of seconds) not between 0 and 1"); + frac *= 1000000000; + nNanoSec = ::rtl::math::round(frac); + } + goto end; + } + + end: + if (bSuccess) + { + // normalise time + const int secondsOverflow = (nSec == 60) ? 61 : 60; + if (nNanoSec == 1000000000) + { + nNanoSec = 0; + ++nSec; + } + if(nSec == secondsOverflow) + { + nSec = 0; + ++nMin; + } + if(nMin == 60) + { + nMin = 0; + ++nHour; + } + if(!tokTz.isEmpty()) + rTime.IsUTC = (tokTz == "Z"); + + rTime.Hours = static_cast<sal_uInt16>(nHour); + rTime.Minutes = static_cast<sal_uInt16>(nMin); + rTime.Seconds = static_cast<sal_uInt16>(nSec); + rTime.NanoSeconds = nNanoSec; + } + + return bSuccess; +} + +} // namespace utl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/misc/defaultencoding.cxx b/unotools/source/misc/defaultencoding.cxx new file mode 100644 index 000000000..552f3b8c4 --- /dev/null +++ b/unotools/source/misc/defaultencoding.cxx @@ -0,0 +1,33 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + */ + +#include <sal/config.h> + +#include <unotools/configmgr.hxx> +#include <unotools/defaultencoding.hxx> +#include <officecfg/Office/Linguistic.hxx> +#include <officecfg/Setup.hxx> +#include <officecfg/System.hxx> + +OUString utl_getLocaleForGlobalDefaultEncoding() +{ + if (utl::ConfigManager::IsFuzzing()) + return "en-US"; + // First try document default language + OUString result(officecfg::Office::Linguistic::General::DefaultLocale::get()); + // Fallback to LO locale + if (result.isEmpty()) + result = officecfg::Setup::L10N::ooSetupSystemLocale::get(); + // Fallback to system locale + if (result.isEmpty()) + result = officecfg::System::L10N::Locale::get(); + return result; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/misc/desktopterminationobserver.cxx b/unotools/source/misc/desktopterminationobserver.cxx new file mode 100644 index 000000000..f5ea04bd4 --- /dev/null +++ b/unotools/source/misc/desktopterminationobserver.cxx @@ -0,0 +1,188 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unotools/desktopterminationobserver.hxx> + +#include <com/sun/star/frame/TerminationVetoException.hpp> +#include <com/sun/star/frame/XTerminateListener.hpp> +#include <com/sun/star/frame/Desktop.hpp> +#include <cppuhelper/implbase.hxx> +#include <comphelper/processfactory.hxx> +#include <osl/diagnose.h> +#include <tools/diagnose_ex.h> + +#include <vector> + +namespace utl +{ + + using namespace ::com::sun::star::uno; + using namespace ::com::sun::star::lang; + using namespace ::com::sun::star::frame; + + namespace + { + + typedef ::std::vector< ITerminationListener* > Listeners; + + struct ListenerAdminData + { + Listeners aListeners; + bool bAlreadyTerminated; + bool bCreatedAdapter; + + ListenerAdminData() : bAlreadyTerminated( false ), bCreatedAdapter( false ) { } + }; + + ListenerAdminData& getListenerAdminData() + { + static ListenerAdminData s_aData; + return s_aData; + } + + //= OObserverImpl + + class OObserverImpl : public ::cppu::WeakImplHelper< XTerminateListener > + { + public: + static void ensureObservation(); + + private: + OObserverImpl(); + virtual ~OObserverImpl() override; + + // XTerminateListener + virtual void SAL_CALL queryTermination( const EventObject& Event ) override; + virtual void SAL_CALL notifyTermination( const EventObject& Event ) override; + + // XEventListener + virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override; + }; + + OObserverImpl::OObserverImpl() + { + } + + OObserverImpl::~OObserverImpl() + { + } + + void OObserverImpl::ensureObservation() + { + { + if ( getListenerAdminData().bCreatedAdapter ) + return; + ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() ); + if ( getListenerAdminData().bCreatedAdapter ) + return; + + getListenerAdminData().bCreatedAdapter = true; + } + + try + { + Reference< XDesktop2 > xDesktop = Desktop::create( ::comphelper::getProcessComponentContext() ); + xDesktop->addTerminateListener( new OObserverImpl ); + } + catch( const Exception& ) + { + TOOLS_WARN_EXCEPTION( "unotools", "OObserverImpl::ensureObservation" ); + } + } + + void SAL_CALL OObserverImpl::queryTermination( const EventObject& /*Event*/ ) + { + Listeners aToNotify; + { + ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() ); + aToNotify = getListenerAdminData().aListeners; + } + + for (auto const& listener : aToNotify) + { + if ( !listener->queryTermination() ) + throw TerminationVetoException(); + } + } + + void SAL_CALL OObserverImpl::notifyTermination( const EventObject& /*Event*/ ) + { + // get the listeners + Listeners aToNotify; + { + ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() ); + OSL_ENSURE( !getListenerAdminData().bAlreadyTerminated, "OObserverImpl::notifyTermination: terminated twice?" ); + aToNotify = getListenerAdminData().aListeners; + getListenerAdminData().bAlreadyTerminated = true; + } + + // notify the listeners + for (auto const& listener : aToNotify) + { + listener->notifyTermination(); + } + + // clear the listener container + { + ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() ); + getListenerAdminData().aListeners.clear(); + } + } + + void SAL_CALL OObserverImpl::disposing( const EventObject& /*Event*/ ) + { +#if OSL_DEBUG_LEVEL > 0 + ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() ); + OSL_ENSURE( getListenerAdminData().bAlreadyTerminated, "OObserverImpl::disposing: disposing without terminated?" ); +#endif + // not interested in + } + } + + //= DesktopTerminationObserver + + void DesktopTerminationObserver::registerTerminationListener( ITerminationListener* _pListener ) + { + if ( !_pListener ) + return; + + { + ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() ); + if ( getListenerAdminData().bAlreadyTerminated ) + { + _pListener->notifyTermination(); + return; + } + + getListenerAdminData().aListeners.push_back( _pListener ); + } + + OObserverImpl::ensureObservation(); + } + + void DesktopTerminationObserver::revokeTerminationListener( ITerminationListener const * _pListener ) + { + ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() ); + Listeners& rListeners = getListenerAdminData().aListeners; + rListeners.erase(std::remove(rListeners.begin(), rListeners.end(), _pListener), rListeners.end()); + } + +} // namespace utl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/misc/eventlisteneradapter.cxx b/unotools/source/misc/eventlisteneradapter.cxx new file mode 100644 index 000000000..d9736905d --- /dev/null +++ b/unotools/source/misc/eventlisteneradapter.cxx @@ -0,0 +1,155 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <vector> + +#include <com/sun/star/lang/XComponent.hpp> +#include <unotools/eventlisteneradapter.hxx> +#include <osl/diagnose.h> +#include <cppuhelper/implbase.hxx> +#include <rtl/ref.hxx> + +namespace utl +{ + + using namespace ::com::sun::star::uno; + using namespace ::com::sun::star::lang; + + //= OEventListenerImpl + + class OEventListenerImpl : public ::cppu::WeakImplHelper< XEventListener > + { + protected: + OEventListenerAdapter* m_pAdapter; + Reference< XEventListener > m_xKeepMeAlive; + // imagine an implementation of XComponent which holds it's listeners with a weak reference ... + // would be very bad if we don't hold ourself + Reference< XComponent > m_xComponent; + + public: + OEventListenerImpl( OEventListenerAdapter* _pAdapter, const Reference< XComponent >& _rxComp ); + + void dispose(); + const Reference< XComponent >& getComponent() const { return m_xComponent; } + + protected: + virtual void SAL_CALL disposing( const EventObject& _rSource ) override; + }; + + OEventListenerImpl::OEventListenerImpl( OEventListenerAdapter* _pAdapter, const Reference< XComponent >& _rxComp ) + :m_pAdapter(_pAdapter) + { + OSL_ENSURE(m_pAdapter, "OEventListenerImpl::OEventListenerImpl: invalid adapter!"); + // no checks of _rxComp !! + // (OEventListenerAdapter is responsible for this) + + // just in case addEventListener throws an exception ... don't initialize m_xKeepMeAlive before this + // is done + Reference< XEventListener > xMeMyselfAndI = this; + _rxComp->addEventListener(xMeMyselfAndI); + + m_xComponent = _rxComp; + m_xKeepMeAlive = xMeMyselfAndI; + } + + void OEventListenerImpl::dispose() + { + if (m_xComponent.is()) + { + if (m_xKeepMeAlive.is()) + m_xComponent->removeEventListener(m_xKeepMeAlive); + m_xComponent.clear(); + m_xKeepMeAlive.clear(); + } + } + + void SAL_CALL OEventListenerImpl::disposing( const EventObject& _rSource ) + { + Reference< XEventListener > xDeleteUponLeaving = m_xKeepMeAlive; + m_xKeepMeAlive.clear(); + + m_pAdapter->_disposing(_rSource); + } + + //= OEventListenerAdapterImpl + + struct OEventListenerAdapterImpl + { + public: + std::vector< rtl::Reference<OEventListenerImpl> > aListeners; + }; + + //= OEventListenerAdapter + + OEventListenerAdapter::OEventListenerAdapter() + :m_pImpl(new OEventListenerAdapterImpl) + { + } + + OEventListenerAdapter::~OEventListenerAdapter() + { + stopAllComponentListening( ); + } + + void OEventListenerAdapter::stopComponentListening( const css::uno::Reference< css::lang::XComponent >& _rxComp ) + { + if ( m_pImpl->aListeners.empty() ) + return; + + auto it = m_pImpl->aListeners.begin(); + do + { + rtl::Reference<OEventListenerImpl>& pListenerImpl = *it; + if ((pListenerImpl->getComponent().get() == _rxComp.get()) || (pListenerImpl->getComponent() == _rxComp)) + { + pListenerImpl->dispose(); + it = m_pImpl->aListeners.erase( it ); + } + else + ++it; + } + while ( it != m_pImpl->aListeners.end() ); + } + + void OEventListenerAdapter::stopAllComponentListening( ) + { + for ( const auto & i : m_pImpl->aListeners ) + { + i->dispose(); + } + m_pImpl->aListeners.clear(); + } + + void OEventListenerAdapter::startComponentListening( const Reference< XComponent >& _rxComp ) + { + if (!_rxComp.is()) + { + OSL_FAIL("OEventListenerAdapter::startComponentListening: invalid component!"); + return; + } + + OEventListenerImpl* pListenerImpl = new OEventListenerImpl(this, _rxComp); + m_pImpl->aListeners.emplace_back(pListenerImpl); + } + +} // namespace utl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/misc/fontcvt.cxx b/unotools/source/misc/fontcvt.cxx new file mode 100644 index 000000000..6a04e7d61 --- /dev/null +++ b/unotools/source/misc/fontcvt.cxx @@ -0,0 +1,1443 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <rtl/ustrbuf.hxx> +#include <sal/log.hxx> +#include <unotools/fontcvt.hxx> +#include <unotools/fontdefs.hxx> +#include <sal/macros.h> + +#include <map> +#include <vector> + +//These conversion tables were designed for StarSymbol. OpenSymbol +//originally didn't have the same code points as StarSymbol, and +//then gained some extra code points, but there are still potentially +//various holes in OpenSymbol which were filled by StarSymbol, i.e. +//destination mapping points which are empty in OpenSymbol + +// note: the character mappings that are only approximations +// are marked (with an empty comment) + +static const sal_Unicode aStarBatsTab[224] = +{ + // F020 + 0x0020, 0x263a, 0x25cf, 0x274d, + 0x25a0, 0x25a1, 0xE000, 0x2751, + 0x2752, 0xE001, 0xE002, 0xE003, + 0x2756, 0xE004, 0xE005, 0x27a2, + // F030 + 0xE006, 0x2794, 0x2713, 0x2612, + 0x2611, 0x27b2, 0x261b, 0x270d, + 0x270e, 0xE007, 0x2714, 0xE008, + 0xE009, 0xE00A, 0x274f, 0x2750, + // F040 + 0xE00B, 0xE00C, 0xE00D, 0xE00E, + 0x2722, 0x2723, 0x2724, 0x2725, + 0x2733, 0x2734, 0x2735, 0x2736, + 0x2737, 0x2738, 0x2739, 0x2717, + // F050 + 0x2718, 0x2719, 0x271a, 0x271b, + 0x271c, 0x272b, 0x272c, 0x272d, + 0x272e, 0x272f, 0x2730, 0, + 0xE00F, 0x278a, 0x278b, 0x278c, + // F060 + 0x278d, 0x278e, 0x278f, 0x2790, + 0x2791, 0x2792, 0x2793, 0xE010, + 0x2780, 0x2781, 0x2782, 0x2783, + 0x2784, 0x2785, 0x2786, 0x2787, + // F070 + 0x2788, 0x2789, 0xE011, 0xE012, + 0x260e, 0xE013, 0xE014, 0xE015, + 0xE016, 0xE017, 0xE018, 0xE019, + 0xE01A, 0x261e, 0xE01B, 0, + // F080 + 0x20ac, 0, 0x201a, 0x0192, + 0x201e, 0x2026, 0x2020, 0x2021, + 0xE01c, 0x2030, 0x0160, 0x2039, + 0x0152, 0, 0x017d, 0, + // F090 + 0, 0x2018, 0x2019, 0x201c, + 0x201d, 0x2022, 0x2013, 0x2014, + 0xE01d, 0x2122, 0x0161, 0x203a, + 0x0153, 0, 0x017e, 0x0178, + // F0A0 + 0, 0x21e7, 0x21e8, 0x21e9, + 0x21e6, 0xE01e, 0xE01f, 0x00a7, + 0xE020, 0xE021, 0xE022, 0x00ab, + 0xE023, 0x2639, 0xE024, 0xE025, + // F0B0 + 0xE026, 0xE027, 0xE028, 0x21e5, + 0x21e4, 0x2192, 0x2193, 0x2190, + 0x2191, 0xE029, 0xE02a, 0x00bb, + 0xE02b, 0xE02c, 0xE02d, 0xE02e, + // F0C0 + 0xE02f, 0xE030, 0xE031, 0xE032, + 0x25be, 0x25b4, 0x25bf, 0x25b5, + 0xE033, 0xE034, 0xE035, 0x2702, + 0x2708, 0x2721, 0x273f, 0x2744, + // F0D0 + 0x25d7, 0x2759, 0xE036, 0xE037, + 0x2762, 0x2663, 0x2665, 0x2660, + 0x2194, 0x2195, 0x2798, 0x279a, + 0x27b8, 0, 0x00b6, 0, + // F0E0 + 0x00a2, 0x00a4, 0x00a5, 0xE038, + 0x20a1, 0x20a2, 0x20a3, 0x20a4, + 0x20a9, 0x20ab, 0x20a8, 0xE039, + 0, 0, 0, 0, + // F0F0 + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0xE03a +}; + +static const sal_Unicode aStarMathTab[224] = +{ + // F020 + 0x0020, 0x0021, 0x0022, 0x0023, + 0xE080, 0x0025, 0x0026, 0x221e, + 0x0028, 0x0029, 0x002a, 0x002b, + 0x002c, 0x002d, 0x002e, 0x002f, + // F030 + 0x2224, 0x21d2, 0x21d0, 0x21d4, + 0xE081, 0xE082, 0x00b0, 0, + 0, 0, 0x003a, 0x003b, + 0x003c, 0x003d, 0x003e, 0x00bf, + // F040 + 0x2260, 0xE083, 0x2212, 0x2217, + 0x00d7, 0x22c5, 0x00f7, 0x00b1, + 0x2213, 0x2295, 0x2296, 0x2297, + 0x2298, 0x2299, 0x222a, 0x2229, + // F050 + 0xE084, 0xE085, 0x2264, 0x2265, + 0xE086, 0xE087, 0x226a, 0x226b, + 0x007e, 0x2243, 0x2248, 0x225d, + 0x2261, 0x221d, 0x2202, 0x2282, + // F060 + 0x2283, 0x2286, 0x2287, 0x2284, + 0x2285, 0x2288, 0x2289, 0x2208, + 0x2209, 0xE089, 0x2203, 0x220d, + 0x2135, 0x2111, 0x211c, 0x2118, + // F070 + 0xE08a, 0x2221, 0x2222, 0x2223, + 0x2225, 0x22a5, 0xE08b, 0x22ef, + 0x22ee, 0x22f0, 0x22f1, 0x22b6, + 0x22b7, 0xE08c, 0x2216, 0x00ac, + // F080 + 0x222b, 0x222c, 0x222d, 0x222e, + 0x222f, 0x2230, 0x221a, 0xE08d, + 0xE08e, 0xE08f, 0x2210, 0x220f, + 0x2211, 0x2207, 0x2200, 0xE090, + // F090 + 0xE091, 0xE092, 0xE093, 0xE094, + 0xE095, 0xE096, 0xE097, 0xE098, + 0x02d9, 0x00a8, 0xE09b, 0x02da, + 0x2227, 0x2228, 0x220b, 0x2205, + // F0A0 + 0x007b, 0x007d, 0xe09e, 0xe09f, + 0x2329, 0x232a, 0x005b, 0x005d, + 0xE0a0, 0x22be, 0xE0a1, 0x2259, + 0x2254, 0x2255, 0x21b3, 0x2197, + // F0B0 + 0x2198, 0x2245, 0x301a, 0x301b, + 0x2373, 0xE0a5, 0xE0a6, 0x22a4, + 0x2112, 0x2130, 0x2131, 0, + 0xE0a7, 0xE0a8, 0xE0a9, 0xE0aa, + // F0C0 + 0x2308, 0x230a, 0x2309, 0x230b, + 0, 0xe0ab, 0xe0ac, 0xe0ad, + 0xe0ae, 0xe0af, 0xe0b0, 0xe0b1, + 0xe0b2, 0xe0b3, 0xe0b4, 0xe0b5, + // F0D0 + 0xe0b6, 0xe0b7, 0xe0b8, 0xe0b9, + 0xe0ba, 0xe0bb, 0xe0bc, 0xe0bd, + 0xe0be, 0xe0bf, 0xe0c0, 0xe0c1, + 0xe0c2, 0xe0c3, 0xe0c4, 0xe0c5, + // F0E0 + 0xe0c6, 0xe0c7, 0xe0c8, 0xe0c9, + 0xe0ca, 0xe0cb, 0xe0cc, 0xe0cd, + 0xe0ce, 0xe0cf, 0xe0d0, 0xe0d1, + 0x03f1, 0xe0d3, 0xe0d4, 0xe0d5, + // F0F0 + 0x2113, 0xe0d6, 0x2107, 0x2127, + 0x210a, 0xe0d9, 0x210f, 0x019b, + 0xe0db, 0xe0dc, 0xe0dd, 0x2115, + 0x2124, 0x211a, 0x211d, 0x2102 +}; + +static const sal_Unicode aWingDingsTab[224] = +{ + // F020 + 0x0020, 0xe400, 0xe401, 0xe402, + 0xe403, 0xe404, 0xe405, 0xe406, + 0xe407, 0xe408, 0xe409, 0xe40a, + 0xe40b, 0xe40c, 0xe40d, 0xe40e, + // F030 + 0xe40f, 0xe410, 0xe411, 0xe412, + 0xe413, 0xe414, 0xe415, 0xe416, + 0xe417, 0xe418, 0xe419, 0xe41a, + 0xe41b, 0xe41c, 0x2707, 0xe41d, + // F040 + 0xe41e, 0xe41f, 0xe420, 0xe421, + 0x261c, 0xe423, 0x261d, 0x261f, + 0x261f, 0xe424, 0xe425, 0xe426, + 0xe427, 0xe428, 0xe429, 0xe42a, + // F050 + 0xe42b, 0xe42c, 0x263c, 0xe42d, + 0xe42e, 0xe42f, 0xe430, 0xe431, + 0xe432, 0xe433, 0x262a, 0x262f, + 0x0950, 0xe434, 0x2648, 0x2649, + // F060 + 0x264a, 0x264b, 0x264c, 0x264d, + 0x264e, 0x264f, 0x2650, 0x2651, + 0x2652, 0x2653, 0xe435, 0xe436, + 0xe437, 0xe438, 0xe439, 0xe43a, + // F070 + 0xe43b, 0xe43c, 0xe43d, 0xe43e, + 0xe43f, 0xe440, 0xe441, 0xe442, + 0xe443, 0x2353, 0x2318, 0xe444, + 0xe445, 0xe446, 0xe447, 0, + // F080 + 0xe448, 0xe449, 0xe44a, 0xe44b, + 0xe44c, 0xe44d, 0xe44e, 0xe44f, + 0xe450, 0xe451, 0xe452, 0xe453, + 0xe454, 0xe455, 0xe456, 0xe457, + // F090 + 0xe458, 0xe459, 0xe45a, 0xe45b, + 0xe45c, 0xe45d, 0xe45e, 0xe45f, + 0xe460, 0xe461, 0xe462, 0xe463, + 0xe464, 0xe465, 0xe466, 0xe467, + // F0a0 + 0xe468, 0xe469, 0xe46a, 0xe46b, + 0xe46c, 0xe46d, 0xe46e, 0xe46f, + 0xe470, 0xe471, 0xe472, 0xe473, + 0xe474, 0xe475, 0xe476, 0xe477, + // F0b0 + 0xe478, 0xe479, 0xe47a, 0xe47b, + 0xe47c, 0xe47d, 0xe47e, 0xe47f, + 0xe480, 0xe481, 0xe482, 0xe483, + 0xe484, 0xe485, 0xe486, 0xe487, + // F0c0 + 0xe488, 0xe489, 0xe48a, 0xe48b, + 0xe48c, 0xe48d, 0xe48e, 0xe48f, + 0xe490, 0xe491, 0xe492, 0xe493, + 0xe494, 0xe495, 0xe496, 0xe497, + // F0d0 + 0xe498, 0xe499, 0xe49a, 0xe49b, + 0xe49c, 0x232b, 0x2326, 0xe49d, + 0xe49e, 0xe49f, 0xe4a0, 0xe4a1, + 0xe4a2, 0xe4a3, 0xe4a4, 0xe4a5, + // F0e0 + 0xe4a6, 0xe4a7, 0xe4a8, 0xe4a9, + 0xe4aa, 0xe4ab, 0xe4ac, 0xe4ad, + 0xe4ae, 0xe4af, 0xe4b0, 0xe4b1, + 0xe4b2, 0xe4b3, 0xe4b4, 0xe4b5, + // F0f0 + 0xe4b6, 0xe4b7, 0xe4b8, 0xe4b9, + 0xe4ba, 0xe4bb, 0xe4bc, 0xe4bd, + 0xe4be, 0xe4bf, 0xe4c0, 0xe4c1, + 0xe4c2, 0xe4c3, 0xe4c4, 0xe4c5 +}; + +static const sal_Unicode aWingDings2Tab[224] = +{ + // F020 + 0x0020, 0xe500, 0xe501, 0xe502, + 0xe503, 0xe504, 0xe505, 0xe506, + 0xe507, 0xe508, 0xe509, 0xe50a, + 0xe50b, 0xe50c, 0xe50d, 0xe50e, + // F030 + 0xe50f, 0xe510, 0xe511, 0xe512, + 0xe513, 0xe514, 0xe515, 0xe516, + 0xe517, 0xe518, 0xe519, 0xe51a, + 0xe51b, 0xe51c, 0xe51d, 0xe51e, + // F040 + 0xe51f, 0xe520, 0xe521, 0xe522, + 0xe523, 0xe524, 0xe525, 0xe526, + 0xe527, 0xe528, 0xe529, 0xe52a, + 0xe52b, 0xe52c, 0xe52d, 0xe52e, + // F050 + 0xe52f, 0xe530, 0xe531, 0xe532, + 0xe533, 0xe534, 0xe535, 0xe536, + 0xe537, 0x203D, 0x203D, 0x203D, + 0xe53b, 0xe53c, 0xe53d, 0xe53e, + // F060 + 0xe53f, 0xe540, 0xe541, 0xe542, + 0xe543, 0xe544, 0xe545, 0xe546, + 0xe547, 0x24EA, 0x2460, 0x2461, + 0x2462, 0x2463, 0x2464, 0x2465, + // F070 + 0x2466, 0x2467, 0x2468, 0x2469, + 0xE453, 0x278A, 0x278B, 0x278C, + 0x278D, 0x278E, 0x278F, 0x2790, + 0x2791, 0x2792, 0x2793, 0, + // F080 + 0x2609, 0x25cb, 0x263d, 0x263e, + 0xe55d, 0xe55e, 0xe55f, 0xe560, + 0xe561, 0xe562, 0xe563, 0xe564, + 0xe565, 0xe566, 0xe567, 0xe568, + // F090 + 0xe569, 0xe56a, 0xe56b, 0xe56c, + 0xe56d, 0xe56e, 0xe56f, 0xe570, + 0xe571, 0xe572, 0xe573, 0xe574, + 0xe575, 0, 0, 0xe578, + // F0a0 + 0xe579, 0xe57a, 0xe57b, 0xe57c, + 0, 0, 0, 0, + 0, 0, 0, 0xe584, + 0xe585, 0, 0xe586, 0, + // F0b0 + 0, 0, 0, 0, + 0xe58d, 0xe58e, 0xe58f, 0xe590, + 0, 0, 0xe593, 0xe594, + 0, 0, 0, 0xe587, + // F0c0 + 0xe599, 0xe59a, 0xe59b, 0xe59c, + 0xe59d, 0xe59e, 0xe59f, 0xe5a0, + 0xe5a1, 0xe5a2, 0xe5a3, 0xe5a4, + 0xe5a5, 0xe5a6, 0xe5a7, 0xe5a8, + // F0d0 + 0xe5a9, 0xe5aa, 0xe5ab, 0xe5ac, + 0xe5ad, 0xe5ae, 0xe5af, 0xe5b0, + 0xe5b1, 0xe5b2, 0xe5b3, 0xe5b4, + 0xe5b5, 0xe5b6, 0xe5b7, 0xe5b8, + // F0e0 + 0xe5b9, 0xe5ba, 0xe5bb, 0xe5bc, + 0xe5bd, 0xe5be, 0xe5bf, 0xe5c0, + 0xe5c1, 0xe5c2, 0xe5c3, 0xe5c4, + 0xe5c5, 0xe5c6, 0xe5c7, 0xe5c8, + // F0f0 + 0xe5c9, 0, 0xe5cb, 0xe477, + 0xe5cd, 0xe5ce, 0xe5cf, 0xe5d0, + 0x203b, 0x2042, 0, 0, + 0, 0, 0, 0 +}; + +static const sal_Unicode aWingDings3Tab[224] = +{ + // F020 + 0x0020, 0xe600, 0xe601, 0xe602, + 0xe603, 0x2196, 0xe604, 0x2199, + 0xe605, 0xe606, 0xe607, 0xe608, + 0xe609, 0xe60a, 0xe60b, 0x21de, + // F030 + 0x21df, 0xe60c, 0xe60d, 0xe60e, + 0x21e2, 0x21e1, 0x21e3, 0x21af, + 0x21b5, 0xe60f, 0xe610, 0xe611, + 0xe612, 0xe613, 0xe614, 0xe615, + // F040 + 0xe616, 0xe617, 0xe618, 0xe619, + 0x21c4, 0x21c5, 0xe61a, 0xe61b, + 0x21c7, 0x21c9, 0x21c8, 0x21ca, + 0x21b6, 0x21b7, 0xe61c, 0xe61d, + // F050 + 0x21bb, 0x21ba, 0xe61e, 0x2324, + 0x2303, 0x2325, 0x2334, 0xe61f, + 0x21ea, 0xe620, 0xe621, 0xe622, + 0xe623, 0xe624, 0xe625, 0xe626, + // F060 + 0xe627, 0xe628, 0xe629, 0xe62a, + 0xe62b, 0xe62c, 0xe62d, 0xe62e, + 0xe62f, 0xe630, 0xe631, 0xe632, + 0xe633, 0xe634, 0xe635, 0xe636, + // F070 + 0xe637, 0xe638, 0x25b3, 0x25bd, + 0x25c0, 0x25b6, 0x25c1, 0x25b7, + 0x25e3, 0xe639, 0x25e4, 0x25e5, + 0x25c2, 0x25b8, 0xe63a, 0, + // F080 + 0xe63b, 0xe63c, 0xe63d, 0xe63e, + 0xe63f, 0xe640, 0xe641, 0xe642, + 0xe643, 0xe644, 0xe645, 0xe646, + 0xe647, 0xe648, 0xe649, 0xe64a, + // F090 + 0xe64b, 0xe64c, 0xe64d, 0xe64e, + 0xe64f, 0xe650, 0xe651, 0xe652, + 0xe653, 0xe654, 0xe655, 0xe656, + 0xe657, 0xe658, 0xe659, 0xe65a, + // F0a0 + 0xe65b, 0xe65c, 0xe65d, 0xe65e, + 0xe65f, 0xe660, 0xe661, 0xe662, + 0xe663, 0xe664, 0xe665, 0xe666, + 0xe667, 0xe668, 0xe669, 0xe66a, + // F0b0 + 0xe66b, 0xe66c, 0xe66d, 0xe66e, + 0xe66f, 0xe670, 0xe671, 0xe672, + 0xe673, 0xe674, 0xe675, 0xe676, + 0xe677, 0xe678, 0xe679, 0xe67a, + // F0c0 + 0xe67b, 0xe67c, 0xe67d, 0xe67e, + 0xe67f, 0xe680, 0xe681, 0xe682, + 0xe683, 0xe684, 0xe685, 0xe686, + 0xe687, 0xe688, 0xe689, 0xe68a, + // F0d0 + 0xe68b, 0xe68c, 0xe68d, 0xe68e, + 0xe68f, 0xe690, 0xe691, 0xe692, + 0xe693, 0xe694, 0xe695, 0xe696, + 0xe697, 0xe698, 0xe699, 0xe69a, + // F0e0 + 0xe69b, 0xe69c, 0xe69d, 0xe69e, + 0xe69f, 0xe6a0, 0xe6a1, 0xe6a2, + 0xe6a3, 0xe6a4, 0xe6a5, 0xe6a6, + 0xe6a7, 0xe6a8, 0xe6a9, 0xe6aa, + // F0f0 + 0xe6ab, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0 +}; + +static const sal_Unicode aWebDingsTab[224] = +{ + // F020 + 0x0020, 0xe300, 0xe301, 0xe302, + 0xe303, 0xe304, 0xe305, 0xe306, + 0xe307, 0xe308, 0xe309, 0xe30a, + 0xe30b, 0xe30c, 0xe30d, 0xe30e, + // F030 + 0xe30f, 0xe310, 0xe311, 0xe312, + 0xe313, 0xe314, 0xe315, 0xe316, + 0xe317, 0xe318, 0xe319, 0xe31a, + 0xe31b, 0xe31c, 0xe31d, 0xe31e, + // F040 + 0xe31f, 0xe320, 0xe321, 0xe322, + 0xe323, 0xe324, 0xe325, 0xe326, + 0xe327, 0xe328, 0xe329, 0xe32a, + 0xe32b, 0xe32c, 0xe32d, 0xe32e, + // F050 + 0xe32f, 0xe330, 0xe331, 0xe332, + 0xe333, 0xe334, 0xe335, 0xe336, + 0xe337, 0xe338, 0xe339, 0xe33a, + 0xe33b, 0xe33c, 0xe33d, 0xe33e, + // F060 + 0xe33f, 0xe340, 0xe341, 0xe342, + 0xe343, 0xe344, 0xe345, 0xe346, + 0xe347, 0xe348, 0xe349, 0xe34a, + 0xe34b, 0xe34c, 0xe34d, 0xe34e, + // F070 + 0xe34f, 0xe350, 0xe351, 0xe352, + 0xe353, 0xe354, 0xe355, 0xe356, + 0xe357, 0xe358, 0xe359, 0xe35a, + 0xe35b, 0xe35c, 0xe35d, 0, + // F080 + 0xe35e, 0xe35f, 0xe360, 0xe361, + 0xe362, 0xe363, 0xe364, 0xe365, + 0xe366, 0xe367, 0xe368, 0xe369, + 0xe36a, 0xe36b, 0xe36c, 0xe36d, + // F090 + 0xe36e, 0xe36f, 0xe370, 0xe371, + 0xe372, 0xe373, 0xe374, 0xe375, + 0xe376, 0xe377, 0xe378, 0xe379, + 0xe37a, 0xe37b, 0xe37c, 0xe37d, + // F0a0 + 0xe37e, 0xe37f, 0xe380, 0xe381, + 0xe382, 0xe383, 0xe384, 0xe385, + 0xe386, 0xe387, 0xe388, 0xe389, + 0xe38a, 0xe38b, 0xe38c, 0xe38d, + // F0b0 + 0xe38e, 0xe38f, 0xe390, 0xe391, + 0xe392, 0xe393, 0xe394, 0xe395, + 0xe396, 0xe397, 0xe398, 0xe399, + 0xe39a, 0xe39b, 0xe39c, 0xe39d, + // F0c0 + 0xe39e, 0xe39f, 0xe3a0, 0xe3a1, + 0xe3a2, 0xe3a3, 0xe3a4, 0xe3a5, + 0xe3a6, 0xe3a7, 0xe3a8, 0xe3a9, + 0xe3aa, 0xe3ab, 0xe3ac, 0xe3ad, + // F0d0 + 0xe3ae, 0xe3af, 0xe3b0, 0xe3b1, + 0xe3b2, 0xe3b3, 0xe3b4, 0xe3b5, + 0xe3b6, 0xe3b7, 0xe3b8, 0xe3b9, + 0xe3ba, 0xe3bb, 0xe3bc, 0xe3bd, + // F0e0 + 0xe3be, 0xe3bf, 0xe3c0, 0xe3c1, + 0xe3c2, 0xe3c3, 0xe3c4, 0xe3c5, + 0xe3c6, 0xe3c7, 0xe3c8, 0xe3c9, + 0xe3ca, 0xe3cb, 0xe3cd, 0xe3ce, + // F0f0 + 0xe3cf, 0xe3d0, 0xe3d1, 0xe3d2, + 0xe3d3, 0xe3d4, 0xe3d5, 0xe3d6, + 0xe3d7, 0xe3d8, 0xe3d9, 0xe3da, + 0xe3db, 0xe3dc, 0xe3dd, 0xe3de +}; + +// See http://www.iana.org/assignments/character-sets/character-sets.xml +// See ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/ADOBE/symbol.txt + +static const sal_Unicode aAdobeSymbolTab[224] = +{ +//TODO: + // F020 + 0x0020, 0xe100, 0xe101, 0xe102, + 0xe103, 0xe104, 0xe16a, 0xe105, + 0xe106, 0xe107, 0xe108, 0xe109, + 0xe10a, 0xe10b, 0xe10c, 0xe10d, + // F030 + 0x0030, 0x0031, 0x0032, 0x0033, + 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0xe10e, 0xe10f, + 0xe110, 0xe111, 0xe112, 0x003f, + // F040 + 0xe113, 0x0391, 0x0392, 0x03a7, + 0x0394, 0x0395, 0x03a6, 0x0393, + 0x0397, 0x0399, 0x03d1, 0x039a, + 0x039b, 0x039c, 0x039d, 0x039f, + // F050 + 0x03a0, 0x0398, 0x03a1, 0x03a3, + 0x03a4, 0x03a5, 0x03c2, 0x03a9, + 0x039e, 0x03a8, 0x0396, 0xe114, + 0x2234, 0xe115, 0xe116, 0x005f, + // F060 + 0x00af, 0x03b1, 0x03b2, 0x03c7, + 0x03b4, 0x03b5, 0x03d5, 0x03b3, + 0x03b7, 0x03b9, 0x03c6, 0x03ba, + 0x03bb, 0x03bc, 0x03bd, 0x03bf, + // F070 + 0x03c0, 0x03b8, 0x03c1, 0x03c3, + 0x03c4, 0x03c5, 0x03d6, 0x03c9, + 0x03be, 0x03c8, 0x03b6, 0xe117, + 0x007c, 0xe118, 0xe119, 0, + // F080 + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + // F090 + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + // F0a0 + 0xe11a, 0x03d2, 0x2032, 0xe11b, + 0x2215, 0xe11c, 0xe11d, 0xe11e, + 0x2666, 0xe11f, 0xe120, 0xe121, + 0xe122, 0xe123, 0xe124, 0xe125, + // F0b0 + 0xe126, 0xe127, 0x2033, 0xe128, + 0xe129, 0xe12a, 0xe12b, 0xe12c, + 0xe12d, 0xe12e, 0xe12f, 0xe130, + 0xe131, 0x2502, 0x2500, 0x21b2, + // F0c0 + 0xe132, 0xe133, 0xe134, 0xe135, + 0xe136, 0xe137, 0xe138, 0xe139, + 0xe13a, 0xe13b, 0xe13c, 0xe13d, + 0xe13e, 0xe13f, 0xe140, 0xe141, + // F0d0 + 0x2220, 0xe142, 0x00ae, 0x00a9, + 0xe143, 0xe144, 0xe145, 0xe146, + 0xe147, 0xe148, 0xe149, 0xe14a, + 0xe14b, 0x21d1, 0xe14c, 0x21d3, + // F0e0 + 0x25ca, 0xe14d, 0xe14e, 0xe14f, + 0xe150, 0xe151, 0xf8eb, 0xf8ec, + 0xf8ed, 0xf8ee, 0xf8ef, 0xf8f0, + 0xf8f1, 0xf8f2, 0xf8f3, 0xf8f4, + // F0f0 + 0, 0x232a, 0x222b, 0x2320, + 0xf8f5, 0x2321, 0xf8f6, 0xf8f7, + 0xf8f8, 0xf8f9, 0xf8fa, 0xf8fb, + 0xf8fc, 0xf8fd, 0xf8fe, 0, +}; + +static const sal_Unicode aMonotypeSortsTab[224] = +{ + // F020 + 0x0020, 0x2701, 0xe200, 0x2703, + 0x2704, 0xe201, 0x2706, 0xe202, + 0xe203, 0xe203, 0xe204, 0xe205, + 0x270c, 0xe206, 0xe207, 0xe208, + // F030 + 0x2710, 0x2711, 0x2712, 0xe209, + 0xe20a, 0x2715, 0x2716, 0xe20b, + 0xe20c, 0xe20d, 0xe20e, 0xe20f, + 0xe210, 0x271d, 0x271e, 0x271f, + // F040 + 0x2720, 0xe211, 0xe212, 0xe213, + 0xe214, 0xe215, 0x2726, 0x2727, + 0x2605, 0x2729, 0x272a, 0xe216, + 0xe217, 0xe218, 0xe219, 0xe21a, + // F050 + 0xe21b, 0xe21c, 0x2732, 0xe21d, + 0xe21e, 0xe21f, 0xe220, 0xe221, + 0xe222, 0xe223, 0x273a, 0x273b, + 0x273c, 0x273d, 0x273e, 0xe224, + // F060 + 0x2740, 0x2741, 0x2742, 0x2743, + 0xe225, 0x2745, 0x2746, 0x2747, + 0x2748, 0x2749, 0x274a, 0x274b, + 0xe226, 0xe227, 0xe228, 0xe229, + // F070 + 0xe22a, 0xe22b, 0xe22c, 0x25b2, + 0x25bc, 0xe22d, 0xe22e, 0xe22f, + 0x2758, 0xe230, 0x275a, 0x275b, + 0x275c, 0x275d, 0x275e, 0, + // F080 + 0xe231, 0xe232, 0xe233, 0xe234, + 0xe235, 0xe236, 0xe237, 0xe238, + 0xe239, 0xe23a, 0xe23b, 0xe23c, + 0xe23d, 0xe23e, 0, 0, + // F090 + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + // F0a0 + 0, 0xe23f, 0xe240, 0x2763, + 0x2764, 0x2765, 0x2766, 0x2767, + 0xe241, 0xe242, 0xe243, 0xe244, + 0x2460, 0x2461, 0x2462, 0x2463, + // F0b0 + 0x2464, 0x2465, 0x2466, 0x2467, + 0x2468, 0x2469, 0x2776, 0x2777, + 0x2778, 0x2779, 0x277a, 0x277b, + 0x277c, 0x277d, 0x277e, 0x277f, + // F0c0 + 0xe245, 0xe246, 0xe247, 0xe248, + 0xe249, 0xe24a, 0xe24b, 0xe24c, + 0xe24d, 0xe24e, 0xe24f, 0xe250, + 0xe251, 0xe252, 0xe253, 0xe254, + // F0d0 + 0xe255, 0xe256, 0xe257, 0xe258, + 0xe259, 0xe25a, 0xe25b, 0xe25c, + 0xe25d, 0x2799, 0xe25e, 0x279b, + 0x279c, 0x279d, 0x279e, 0x279f, + // F0e0 + 0x27a0, 0x27a1, 0xe25f, 0x27a3, + 0x27a4, 0x27a5, 0x27a6, 0x27a7, + 0x27a8, 0x27a9, 0x27aa, 0x27ab, + 0x27ac, 0x27ad, 0x27ae, 0x27af, + // F0f0 + 0, 0x27b1, 0xe260, 0x27b3, + 0x27b4, 0x27b5, 0x27b6, 0x27b7, + 0xe261, 0x27b9, 0x27ba, 0x27bb, + 0x27bc, 0x27bd, 0x27be, 0, +}; + +static const sal_Unicode aMTExtraTab[224] = +{ + // F020 + 0x0020, 0, 0, 0x0300, + 0x0302, 0x0303, 0x0307, 0, + 0x2323, 0x2322, 0, 0, + 0, 0, 0, 0, + // F030 + 0, 0xEC00, 0xEC01, 0xEC02, + 0xEC03, 0xEC0B, 0xEC04, 0xEC05, + 0xEC06, 0, 0x223C, 0x2243, + 0x22B2, 0x226A, 0x22B3, 0x226B, + // F040 + 0x225C, 0x2259, 0x2250, 0x2210, + 0x019B, 0xEC0E, 0xEC0F, 0xEC10, + 0xEC11, 0x2229, 0x2127, 0x2026, + 0x22EF, 0x22EE, 0x22F0, 0x22F1, + // F050 + 0x2225, 0x2235, 0x2221, 0x2222, + 0, 0x222A, 0x25B3, 0x25A1, + 0x25AD, 0x25B1, 0x2197, 0x2199, + 0xEB05, 0x2198, 0x2196, 0xEB06, + // F060 + 0x2035, 0x21A6, 0x2195, 0x21D5, + 0x25CB, 0x2299, 0x227B, 0xE98F, + 0x210F, 0xEE00, 0xEE01, 0, + 0x2113, 0x2213, 0xFFFD, 0x2218, + // F070 + 0x227A, 0xEB1A, 0x20D7, 0x20D6, + 0x20E1, 0xEB00, 0x20D1, 0x20D0, + 0xEB19, 0, 0, 0xFE38, + 0xEC0C, 0xFE37, 0xEC0D, 0, + // F080 + 0x21C4, 0xEB01, 0xEB02, 0x21CC, + 0xEB03, 0xEB04, 0x21C0, 0x21BD, + 0xF8E7, 0, 0, 0, + 0, 0, 0, 0, + // F090 + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + // F0a0 + 0, 0x211D, 0x2124, 0x2102, + 0x211A, 0x2115, 0, 0x301A, + 0x301B, 0xEC22, 0xEC23, 0xEC24, + 0xEC25, 0xEC26, 0xEC27, 0, + // F0b0 + 0xEE04, 0xEE05, 0xEE06, 0, + 0, 0xEE07, 0xEE08, 0xEE09, + 0, 0, 0xEE0A, 0xEE0B, + 0xEE0C, 0, 0, 0, + // F0c0 + 0xEE0D, 0xEE0E, 0xEE0F, 0xEE10, + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + // F0d0 + 0, 0xEE11, 0xEE12, 0xEE13, + 0, 0xEE15, 0xEE16, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + // F0e0 + 0, 0, 0, 0, + 0, 0, 0, 0xF8FF, + 0x0160, 0x00DD, 0x00DE, 0x00D0, + 0x0161, 0x00FD, 0x00FE, 0x00F0, + // F0f0 + 0xFB01, 0xFB02, 0x0131, 0x00B9, + 0x00B2, 0x00B3, 0x00BD, 0x00BC, + 0x00BE, 0x2044, 0x00A6, 0x02DD, + 0x02D8, 0x02C7, 0x02DA, 0x02DB, +}; + +static const sal_Unicode aAdobeSymbolToAppleSymbolTab[224] = +{ + // F020 + 0x0020, 0x0021, 0x2200, 0x0023, + 0x2203, 0x0025, 0x0026, 0x220D, + 0x0028, 0x0029, 0x2217, 0x002B, + 0x002C, 0x2212, 0x002E, 0x002F, + // F030 + 0x0030, 0x0031, 0x0032, 0x0033, + 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, + 0x003C, 0x003D, 0x003E, 0x003F, + // F040 + 0x2245, 0x0391, 0x0392, 0x03A7, + 0x0394, 0x0395, 0x03A6, 0x0393, + 0x0397, 0x0399, 0x03D1, 0x039A, + 0x039B, 0x039C, 0x039D, 0x039F, + // F050 + 0x03A0, 0x0398, 0x03A1, 0x03A3, + 0x03A4, 0x03A5, 0x03C2, 0x03A9, + 0x039E, 0x03A8, 0x0396, 0x005B, + 0x2234, 0x005D, 0x22A5, 0x005F, + // F060 + 0xF8E5, 0x03B1, 0x03B2, 0x03C7, + 0x03B4, 0x03B5, 0x03C6, 0x03B3, + 0x03B7, 0x03B9, 0x03D5, 0x03BA, + 0x03BB, 0x03BC, 0x03BD, 0x03BF, + // F070 + 0x03C0, 0x03B8, 0x03C1, 0x03C3, + 0x03C4, 0x03C5, 0x03D6, 0x03C9, + 0x03BE, 0x03C8, 0x03B6, 0x007B, + 0x007C, 0x007D, 0x223C, 0x007F, + // F080 + 0x0080, 0x0081, 0x0082, 0x0083, + 0x0084, 0x0085, 0x0086, 0x0087, + 0x0088, 0x0089, 0x008A, 0x008B, + 0x008C, 0x008D, 0x008E, 0x008F, + // F090 + 0x0090, 0x0091, 0x0092, 0x0093, + 0x0094, 0x0095, 0x0096, 0x0097, + 0x0098, 0x0099, 0x009A, 0x009B, + 0x009C, 0x009D, 0x009E, 0x009F, + // F0a0 + 0x20AC, 0x03D2, 0x2032, 0x2264, + 0x2044, 0x221E, 0x0192, 0x2663, + 0x2666, 0x2665, 0x2660, 0x2194, + 0x2190, 0x2191, 0x2192, 0x2193, + // F0b0 + 0x00B0, 0x00B1, 0x2033, 0x2065, + 0x00D7, 0x221D, 0x2202, 0x2022, + 0x00F7, 0x2260, 0x2261, 0x2248, + 0x2026, 0x23D0, 0x23AF, 0x21B5, + // F0c0 + 0x2135, 0x2111, 0x211C, 0x2118, + 0x2297, 0x2295, 0x2205, 0x2229, + 0x222A, 0x2283, 0x2287, 0x2284, + 0x2282, 0x2286, 0x2208, 0x2209, + // F0d0 + 0x2220, 0x2207, 0x00AE, 0x00A9, + 0x2122, 0x220F, 0x221A, 0x22C5, + 0x00AC, 0x2227, 0x2228, 0x21D4, + 0x21D0, 0x21D1, 0x21D2, 0x21D3, + // F0e0 + 0x25CA, 0x3008, 0x00AE, 0x00A9, + 0x2122, 0x2211, 0x239B, 0x239C, + 0x239D, 0x23A1, 0x23A2, 0x23A3, + 0x23A7, 0x23A8, 0x23A9, 0x23AA, + // F0f0 + 0xF8FF, 0x3009, 0x222B, 0x2320, + 0x23AE, 0x2321, 0x239E, 0x239F, + 0x23A0, 0x23A4, 0x23A5, 0x23A6, + 0x23AB, 0x23AC, 0x23AD, 0x00FF +}; + +static sal_Unicode ImplStarSymbolToStarBats( sal_Unicode c ) +{ + switch ( c ) + { + case 0x00A2: c = 0xF0E0; break; + case 0x00A4: c = 0xF0E1; break; + case 0x00A5: c = 0xF0E2; break; + case 0x00A7: c = 0xF0A7; break; + case 0x00AB: c = 0xF0AB; break; + case 0x00B6: c = 0xF0DE; break; + case 0x00BB: c = 0xF0BB; break; + + case 0x0152: c = 0xF08C; break; + case 0x0153: c = 0xF09C; break; + case 0x0160: c = 0xF08A; break; + case 0x0161: c = 0xF09A; break; + case 0x0178: c = 0xF09F; break; + case 0x017D: c = 0xF08E; break; + case 0x017E: c = 0xF09E; break; + + case 0x0192: c = 0xF083; break; + case 0x02C6: c = 0xF088; break; + case 0x02DC: c = 0xF098; break; + case 0x2013: c = 0xF096; break; + case 0x2014: c = 0xF097; break; + case 0x2018: c = 0xF091; break; + + case 0x2019: c = 0xF092; break; + case 0x201A: c = 0xF082; break; + case 0x201C: c = 0xF093; break; + case 0x201D: c = 0xF094; break; + case 0x201E: c = 0xF084; break; + case 0x2020: c = 0xF086; break; + + case 0x2021: c = 0xF087; break; + case 0x2022: c = 0xF095; break; + case 0x2026: c = 0xF085; break; + case 0x2030: c = 0xF089; break; + case 0x2039: c = 0xF08B; break; + case 0x203A: c = 0xF09B; break; + + case 0x20A1: c = 0xF0E4; break; + case 0x20A2: c = 0xF0E5; break; + case 0x20A3: c = 0xF0E6; break; + case 0x20A4: c = 0xF0E7; break; + case 0x20A8: c = 0xF0EA; break; + case 0x20A9: c = 0xF0E8; break; + case 0x20AB: c = 0xF0E9; break; + case 0x20AC: c = 0xF080; break; + + case 0x2122: c = 0xF099; break; + case 0x2190: c = 0xF0B7; break; + case 0x2191: c = 0xF0B8; break; + case 0x2192: c = 0xF0B5; break; + case 0x2193: c = 0xF0B6; break; + case 0x2194: c = 0xF0D8; break; + case 0x2195: c = 0xF0D9; break; + + case 0x21E4: c = 0xF0B4; break; + case 0x21E5: c = 0xF0B3; break; + case 0x21E6: c = 0xF0A4; break; + case 0x21E7: c = 0xF0B2; break; + case 0x21E8: c = 0xF0AF; break; + case 0x21E9: c = 0xF0A3; break; + + case 0x25A0: c = 0xF024; break; + case 0x25A1: c = 0xF025; break; + case 0x25B4: c = 0xF0C5; break; + case 0x25B5: c = 0xF0C7; break; + case 0x25BE: c = 0xF0C4; break; + case 0x25BF: c = 0xF0C6; break; + case 0x25C6: c = 0xF043; break; + case 0x25CF: c = 0xF022; break; + case 0x25D7: c = 0xF0D0; break; + + case 0x260E: c = 0xF074; break; + case 0x2611: c = 0xF034; break; + case 0x2612: c = 0xF033; break; + case 0x261B: c = 0xF036; break; + case 0x261E: c = 0xF07D; break; + case 0x2639: c = 0xF0AD; break; + case 0x263A: c = 0xF021; break; + + case 0x2702: c = 0xF0CB; break; + case 0x2708: c = 0xF0CC; break; + case 0x270D: c = 0xF07E; break; + case 0x270E: c = 0xF038; break; + + case 0x2713: c = 0xF039; break; + case 0x2714: c = 0xF03A; break; + case 0x2717: c = 0xF04F; break; + case 0x2718: c = 0xF050; break; + case 0x2719: c = 0xF051; break; + case 0x271A: c = 0xF052; break; + case 0x271B: c = 0xF053; break; + case 0x271C: c = 0xF054; break; + + case 0x2721: c = 0xF0CD; break; + case 0x2722: c = 0xF044; break; + case 0x2723: c = 0xF045; break; + case 0x2724: c = 0xF046; break; + case 0x2725: c = 0xF047; break; + case 0x272B: c = 0xF055; break; + case 0x272C: c = 0xF056; break; + case 0x272D: c = 0xF057; break; + case 0x272E: c = 0xF058; break; + case 0x272F: c = 0xF059; break; + + case 0x2730: c = 0xF05A; break; + case 0x2733: c = 0xF048; break; + case 0x2734: c = 0xF049; break; + case 0x2735: c = 0xF04A; break; + case 0x2736: c = 0xF04B; break; + case 0x2737: c = 0xF04C; break; + case 0x2738: c = 0xF04D; break; + case 0x2739: c = 0xF04E; break; + case 0x273F: c = 0xF0CE; break; + + case 0x2744: c = 0xF0CF; break; + case 0x274D: c = 0xF023; break; + case 0x274F: c = 0xF03E; break; + case 0x2750: c = 0xF03F; break; + case 0x2751: c = 0xF027; break; + case 0x2752: c = 0xF028; break; + case 0x2756: c = 0xF02C; break; + case 0x2759: c = 0xF0D1; break; + case 0x2762: c = 0xF0D4; break; + + case 0x2780: c = 0xF068; break; + case 0x2781: c = 0xF069; break; + case 0x2782: c = 0xF06A; break; + case 0x2783: c = 0xF06B; break; + case 0x2784: c = 0xF06C; break; + case 0x2785: c = 0xF06D; break; + case 0x2786: c = 0xF06E; break; + case 0x2787: c = 0xF06F; break; + case 0x2788: c = 0xF070; break; + case 0x2789: c = 0xF071; break; + case 0x278A: c = 0xF05D; break; + case 0x278B: c = 0xF05E; break; + case 0x278C: c = 0xF05F; break; + case 0x278D: c = 0xF060; break; + case 0x278E: c = 0xF061; break; + case 0x278F: c = 0xF062; break; + + case 0x2790: c = 0xF063; break; + case 0x2791: c = 0xF064; break; + case 0x2792: c = 0xF065; break; + case 0x2793: c = 0xF066; break; + case 0x2794: c = 0xF031; break; + case 0x2798: c = 0xF0DA; break; + case 0x279A: c = 0xF0DB; break; + + case 0x27A2: c = 0xF02F; break; + case 0x27B2: c = 0xF035; break; + case 0x27B8: c = 0xF0DC; break; + + case 0xE000: c = 0xF000+38; break; + case 0xE001: c = 0xF000+41; break; + case 0xE002: c = 0xF000+42; break; + case 0xE003: c = 0xF000+43; break; + case 0xE004: c = 0xF000+45; break; + case 0xE005: c = 0xF000+46; break; + case 0xE006: c = 0xF000+48; break; + case 0xE007: c = 0xF000+57; break; + + case 0xE008: c = 0xF000+59; break; + case 0xE009: c = 0xF000+60; break; + case 0xE00a: c = 0xF000+61; break; + case 0xE00b: c = 0xF000+64; break; + case 0xE00c: c = 0xF000+65; break; + case 0xE00d: c = 0xF000+66; break; + case 0xE00e: c = 0xF000+67; break; + case 0xE00f: c = 0xF000+92; break; + + case 0xE010: c = 0xF000+103; break; + case 0xE011: c = 0xF000+114; break; + case 0xE012: c = 0xF000+115; break; + case 0xE013: c = 0xF000+117; break; + case 0xE014: c = 0xF000+118; break; + case 0xE015: c = 0xF000+119; break; + case 0xE016: c = 0xF000+120; break; + case 0xE017: c = 0xF000+121; break; + + case 0xE018: c = 0xF000+122; break; + case 0xE019: c = 0xF000+123; break; + case 0xE01a: c = 0xF000+124; break; + case 0xE01b: c = 0xF000+126; break; + case 0xE01c: c = 0xF000+136; break; + case 0xE01d: c = 0xF000+155; break; + case 0xE01e: c = 0xF000+165; break; + case 0xE01f: c = 0xF000+166; break; + + case 0xE020: c = 0xF000+168; break; + case 0xE021: c = 0xF000+169; break; + case 0xE022: c = 0xF000+170; break; + case 0xE023: c = 0xF000+172; break; + case 0xE024: c = 0xF000+174; break; + case 0xE025: c = 0xF000+175; break; + case 0xE026: c = 0xF000+176; break; + case 0xE027: c = 0xF000+177; break; + + case 0xE028: c = 0xF000+178; break; + case 0xE029: c = 0xF000+185; break; + case 0xE02a: c = 0xF000+186; break; + case 0xE02b: c = 0xF000+188; break; + case 0xE02c: c = 0xF000+189; break; + case 0xE02d: c = 0xF000+190; break; + case 0xE02e: c = 0xF000+191; break; + case 0xE02f: c = 0xF000+192; break; + + case 0xE030: c = 0xF000+193; break; + case 0xE031: c = 0xF000+194; break; + case 0xE032: c = 0xF000+195; break; + case 0xE033: c = 0xF000+200; break; + case 0xE034: c = 0xF000+201; break; + case 0xE035: c = 0xF000+202; break; + case 0xE036: c = 0xF000+210; break; + case 0xE037: c = 0xF000+211; break; + + case 0xE038: c = 0xF000+227; break; + case 0xE039: c = 0xF000+235; break; + case 0xE03a: c = 0xF000+255; break; + + default: c = 0; break; + } + + return c; +} + +namespace { + +enum SymbolFont +{ + Symbol=1, Wingdings=2, MonotypeSorts=4, Webdings=8, Wingdings2=16, + Wingdings3=32, MTExtra=64, TimesNewRoman=128 +}; + +} + +const char * const aSymbolNames[] = +{ + "Symbol", "Wingdings", "Monotype Sorts", "Webdings", "Wingdings 2", + "Wingdings 3", "MT Extra", "Times New Roman" +}; + +namespace { + +struct SymbolEntry +{ + sal_uInt8 cIndex; + enum SymbolFont eFont; +}; + +class StarSymbolToMSMultiFontImpl : public StarSymbolToMSMultiFont +{ +private: + ::std::multimap<sal_Unicode, SymbolEntry> maMagicMap; +public: + explicit StarSymbolToMSMultiFontImpl(); + OUString ConvertChar(sal_Unicode &rChar) override; +}; + +struct ExtraTable { sal_Unicode cStar; sal_uInt8 cMS;}; + +} + +ExtraTable const aWingDingsExtraTab[] = +{ + {0x25cf, 0x6C}, {0x2714, 0xFC}, {0x2717, 0xFB}, {0x2794, 0xE8}, + {0x27a2, 0xD8}, {0xe000, 0x6F}, {0xe001, 0x73}, {0xe002, 0x74}, + {0xe003, 0x75}, {0xe004, 0x77}, {0xe005, 0xA6}, {0xe006, 0xE0}, + {0xe007, 0xFC}, {0xe008, 0x6C}, {0xe009, 0x6D}, {0xe00a, 0x6E}, + {0xe00b, 0x72}, {0xe00c, 0x75}, {0xe00d, 0x76}, {0xe00e, 0x74}, + {0xe00f, 0x8B}, {0xe010, 0x80}, {0xe011, 0x2B}, {0xe012, 0x3A}, + {0xe013, 0x5D}, {0xe014, 0x29}, {0xe015, 0x3A}, {0xe016, 0x3C}, + {0xe017, 0x38}, {0xe018, 0x3A}, {0xe019, 0x2A}, {0xe01a, 0x2B}, + {0xe01b, 0x3F}, {0xe01c, 0x9F}, {0xe01d, 0x80}, {0xe01e, 0x8B}, + {0xe023, 0x4A}, {0xe025, 0xF0}, {0xe026, 0xF2}, {0xe027, 0xEF}, + {0xe028, 0xF1}, {0xe029, 0x52}, {0xe02a, 0x29}, {0xe02b, 0xE0}, + {0xe02c, 0xE2}, {0xe02d, 0xDF}, {0xe02e, 0xE1}, {0xe02f, 0xAC}, + {0xe030, 0xAD}, {0xe031, 0xAE}, {0xe032, 0x7C}, {0xe033, 0x43}, + {0xe034, 0x4D}, {0xe0aa, 0x71}, {0xe422, 0x44} +}; + +ExtraTable const aSymbolExtraTab2[] = +{ + {0x0020, 0x20}, {0x00A0, 0x20}, {0x0021, 0x21}, {0x2200, 0x22}, + {0x0023, 0x23}, {0x2203, 0x24}, {0x0025, 0x25}, {0x0026, 0x26}, + {0x220B, 0x27}, {0x0028, 0x28}, {0x0029, 0x29}, {0x2217, 0x2A}, + {0x002B, 0x2B}, {0x002C, 0x2C}, {0x2212, 0x2D}, {0x002E, 0x2E}, + {0x002F, 0x2F}, {0x003A, 0x3A}, {0x003B, 0x3B}, {0x003C, 0x3C}, + {0x003D, 0x3D}, {0x003E, 0x3E}, {0x2245, 0x40}, {0x2206, 0x44}, + {0x2126, 0x57}, {0x005B, 0x5B}, {0x005D, 0x5D}, {0x22A5, 0x5E}, + {0x03C6, 0x66}, {0x03D5, 0x6A}, {0x00B5, 0x6D}, {0x007B, 0x7B}, + {0x007C, 0x7C}, {0x007D, 0x7D}, {0x223C, 0x7E}, {0x20AC, 0xA0}, + {0x2032, 0xA2}, {0x2264, 0xA3}, {0x2044, 0xA4}, {0x221E, 0xA5}, + {0x0192, 0xA6}, {0x2663, 0xA7}, {0x2665, 0xA9}, {0x2660, 0xAA}, + {0x2194, 0xAB}, {0x2190, 0xAC}, {0x2191, 0xAD}, {0x2192, 0xAE}, + {0x2193, 0xAF}, {0x00B0, 0xB0}, {0x00B1, 0xB1}, {0x2265, 0xB3}, + {0x00D7, 0xB4}, {0x221D, 0xB5}, {0x2202, 0xB6}, {0x2022, 0xB7}, + {0x00F7, 0xB8}, {0x2260, 0xB9}, {0x2261, 0xBA}, {0x2248, 0xBB}, + {0x2026, 0xBC}, {0x21B5, 0xBF}, {0x2135, 0xC0}, {0x2111, 0xC1}, + {0x211C, 0xC2}, {0x2118, 0xC3}, {0x2297, 0xC4}, {0x2295, 0xC5}, + {0x2205, 0xC6}, {0x2229, 0xC7}, {0x222A, 0xC8}, {0x2283, 0xC9}, + {0x2287, 0xCA}, {0x2284, 0xCB}, {0x2282, 0xCC}, {0x2286, 0xCD}, + {0x2208, 0xCE}, {0x2209, 0xCF}, {0x2207, 0xD1}, {0x220F, 0xD5}, + {0x221A, 0xD6}, {0x22C5, 0xD7}, {0x00AC, 0xD8}, {0x2227, 0xD9}, + {0x2228, 0xDA}, {0x21D4, 0xDB}, {0x21D0, 0xDC}, {0x21D2, 0xDE}, + {0x2329, 0xE1}, {0x2211, 0xE5}, {0x232A, 0xF1}, {0x222B, 0xF2}, + {0x2320, 0xF3}, {0x2321, 0xF5}, {0x2013, 0x2D} +}; + +ExtraTable const aSymbolExtraTab[] = +{ + {0xe021, 0xD3}, {0xe024, 0xD2}, {0xe035, 0x20}, {0xe036, 0x28}, + {0xe037, 0x29}, {0xe039, 0x20}, {0xe083, 0x2B}, {0xe084, 0x3C}, + {0xe085, 0x3E}, {0xe086, 0xA3}, {0xe087, 0xB3}, {0xe089, 0xCE}, + {0xe08a, 0xA6}, {0xe08c, 0xAE}, {0xe08d, 0xD6}, {0xe08e, 0xD6}, + {0xe08f, 0xD6}, {0xe094, 0xA2}, {0xe09e, 0x28}, {0xe09f, 0x29}, + {0xe0a0, 0xD0}, {0xe0a6, 0xA2}, {0xe0a7, 0x7C}, {0xe0a8, 0x2F}, + {0xe0ab, 0x7C}, {0xe0ac, 0x47}, {0xe0ad, 0x44}, {0xe0ae, 0x51}, + {0xe0af, 0x4C}, {0xe0b0, 0x58}, {0xe0b1, 0x50}, {0xe0b2, 0x53}, + {0xe0b3, 0x55}, {0xe0b4, 0x46}, {0xe0b5, 0x59}, {0xe0b6, 0x57}, + {0xe0b7, 0x61}, {0xe0b8, 0x62}, {0xe0b9, 0x67}, {0xe0ba, 0x64}, + {0xe0bb, 0x65}, {0xe0bc, 0x7A}, {0xe0bd, 0x68}, {0xe0be, 0x71}, + {0xe0bf, 0x69}, {0xe0c0, 0x6B}, {0xe0c1, 0x6C}, {0xe0c2, 0x6D}, + {0xe0c3, 0x6E}, {0xe0c4, 0x78}, {0xe0c5, 0x6F}, {0xe0c6, 0x70}, + {0xe0c7, 0x72}, {0xe0c8, 0x73}, {0xe0c9, 0x74}, {0xe0ca, 0x75}, + {0xe0cb, 0x66}, {0xe0cc, 0x63}, {0xe0cd, 0x79}, {0xe0ce, 0x77}, + {0xe0cf, 0x65}, {0xe0d0, 0x4A}, {0xe0d1, 0x76}, {0xe0d3, 0x56}, + {0xe0d4, 0x6A}, {0xe0d5, 0xB6}, {0xe0d6, 0x69}, {0xe0db, 0xAC}, + {0xe0dc, 0xAD}, {0xe0dd, 0xAF} +}; + +ExtraTable const aTNRExtraTab[] = +{ + {0xe021, 0xA9}, + {0xe022, 0x40}, + {0xe024, 0xAE}, + {0xe035, 0x20}, + {0xe036, '('}, + {0xe037, ')'}, + {0xe039, 0x20}, + {0xe03a, 0x80}, + {0xe080, 0x89}, + {0xe083, '+'}, + {0xe084, '<'}, + {0xe085, '>'}, + {0xe0a9, '\\'} +}; + +StarSymbolToMSMultiFontImpl::StarSymbolToMSMultiFontImpl() +{ + struct ConvertTable + { + enum SymbolFont meFont; + const sal_Unicode* pTab; + }; + + //In order of preference + static const ConvertTable aConservativeTable[] = + { + {Symbol, aAdobeSymbolTab}, + {Wingdings, aWingDingsTab}, + {MonotypeSorts, aMonotypeSortsTab}, + {Webdings, aWebDingsTab}, + {Wingdings2, aWingDings2Tab}, + {Wingdings3, aWingDings3Tab}, + {MTExtra, aMTExtraTab} + }; + + struct ExtendedConvertTable + { + enum SymbolFont meFont; + const ExtraTable *mpTable; + size_t mnSize; + ExtendedConvertTable(SymbolFont eFont, const ExtraTable *pTable, + size_t nSize) + : meFont(eFont), mpTable(pTable), mnSize(nSize) {} + }; + + //Reverse map from a given starsymbol char to exact matches in ms symbol + //fonts. + int nEntries = SAL_N_ELEMENTS(aConservativeTable); + int i; + for (i = 0; i < nEntries; ++i) + { + const ConvertTable& r = aConservativeTable[i]; + SymbolEntry aEntry; + aEntry.eFont = r.meFont; + for (aEntry.cIndex = 0xFF; aEntry.cIndex >= 0x20; --aEntry.cIndex) + { + if (sal_Unicode cChar = r.pTab[aEntry.cIndex-0x20]) + maMagicMap.emplace(cChar, aEntry); + } + } + + //In order of preference + static const ExtendedConvertTable aAgressiveTable[] = + { + ExtendedConvertTable(Symbol, aSymbolExtraTab2, + sizeof(aSymbolExtraTab2)), + ExtendedConvertTable(Symbol, aSymbolExtraTab, + sizeof(aSymbolExtraTab)), + ExtendedConvertTable(Wingdings, aWingDingsExtraTab, + sizeof(aWingDingsExtraTab)), + ExtendedConvertTable(TimesNewRoman, aTNRExtraTab, + sizeof(aTNRExtraTab)) + }; + + //Allow extra conversions that are not perfect, but "good enough" + nEntries = SAL_N_ELEMENTS(aAgressiveTable); + + for (i = 0; i < nEntries; ++i) + { + const ExtendedConvertTable& r = aAgressiveTable[i]; + SymbolEntry aEntry; + aEntry.eFont = r.meFont; + for (int j = r.mnSize / sizeof(r.mpTable[0]) - 1; j >=0; --j) + { + aEntry.cIndex = r.mpTable[j].cMS; + maMagicMap.emplace(r.mpTable[j].cStar, aEntry); + } + } +} + +static const char *SymbolFontToString(int nResult) +{ + const char * const *ppName = aSymbolNames; + int nI = Symbol; + while (nI <= nResult) + { + if (!(nI & nResult)) + nI = nI << 1; + else + break; + ppName++; + } + return *ppName; +} + +OUString StarSymbolToMSMultiFontImpl::ConvertChar(sal_Unicode &rChar) +{ + OUString sRet; + + ::std::multimap<sal_Unicode, SymbolEntry>::const_iterator aResult = + maMagicMap.find(rChar); + + if (aResult != maMagicMap.end()) + { + const SymbolEntry &rEntry = (*aResult).second; + const char* pc = SymbolFontToString(rEntry.eFont); + sRet = OUString(pc, strlen(pc), RTL_TEXTENCODING_ASCII_US); + rChar = rEntry.cIndex; + } + + return sRet; +} + +StarSymbolToMSMultiFont *CreateStarSymbolToMSMultiFont() +{ + return new StarSymbolToMSMultiFontImpl; +} + +sal_Unicode ConvertChar::RecodeChar( sal_Unicode cChar ) const +{ + sal_Unicode cRetVal = 0; + if( mpCvtFunc ) + { + // use a conversion function for recoding + cRetVal = mpCvtFunc( cChar ); + } + else + { + // use a conversion table for recoding + sal_Unicode cIndex = cChar; + // allow symbol aliasing + if( cIndex & 0xFF00 ) + cIndex -= 0xF000; + // recode the symbol + if( cIndex>=0x0020 && cIndex<=0x00FF ) + { + cRetVal = mpCvtTab[ cIndex - 0x0020 ]; + + if (!cRetVal && mpSubsFontName) + { + if ( IsStarSymbol( OUString::createFromAscii(mpSubsFontName) ) ) + { + cRetVal = 0xE12C; + SAL_WARN( "unotools.misc", "Forcing a bullet substitution from 0x" << + OString::number(cChar, 16) << " to 0x" << + OString::number(cRetVal, 16)); + } + } + } + } + + return cRetVal ? cRetVal : cChar; +} + +// recode the string assuming the character codes are symbol codes +// from a traditional symbol font (i.e. U+F020..U+F0FF) +void ConvertChar::RecodeString( OUString& rStr, sal_Int32 nIndex, sal_Int32 nLen ) const +{ + sal_Int32 nLastIndex = nIndex + nLen; + OUStringBuffer aTmpStr(rStr); + + if( nLastIndex > aTmpStr.getLength() ) + nLastIndex = aTmpStr.getLength(); + + for(; nIndex < nLastIndex; ++nIndex ) + { + sal_Unicode cOrig = rStr[ nIndex ]; + // only recode symbols and their U+00xx aliases + if( ((cOrig < 0x0020) || (cOrig > 0x00FF)) + && ((cOrig < 0xF020) || (cOrig > 0xF0FF)) ) + continue; + + // recode a symbol + sal_Unicode cNew = RecodeChar( cOrig ); + if( cOrig != cNew ) + aTmpStr[ nIndex ] = cNew; + } + rStr = aTmpStr.makeStringAndClear(); +} + +namespace { + +struct RecodeTable { const char* pOrgName; ConvertChar aCvt;}; + +} + +static const RecodeTable aStarSymbolRecodeTable[] = +{ + // the first two entries must be StarMath and StarBats; do not reorder! + // reason: see CreateFontToSubsFontConverter method + {"starbats", {aStarBatsTab, "StarSymbol", nullptr}}, + {"starmath", {aStarMathTab, "StarSymbol", nullptr}}, + + {"symbol", {aAdobeSymbolTab, "StarSymbol", nullptr}}, + {"standardsymbols", {aAdobeSymbolTab, "StarSymbol", nullptr}}, + {"standardsymbolsl",{aAdobeSymbolTab, "StarSymbol", nullptr}}, + + {"monotypesorts", {aMonotypeSortsTab, "StarSymbol", nullptr}}, +// {"monotypesorts2", {aMonotypeSorts2Tab, "StarSymbol", NULL}} + {"zapfdingbats", {aMonotypeSortsTab, "StarSymbol", nullptr}}, //ZapfDingbats=MonotypeSorts-X? + {"itczapfdingbats", {aMonotypeSortsTab, "StarSymbol", nullptr}}, + {"dingbats", {aMonotypeSortsTab, "StarSymbol", nullptr}}, + + {"webdings", {aWebDingsTab, "StarSymbol", nullptr}}, + {"wingdings", {aWingDingsTab, "StarSymbol", nullptr}}, + {"wingdings2", {aWingDings2Tab, "StarSymbol", nullptr}}, + {"wingdings3", {aWingDings3Tab, "StarSymbol", nullptr}}, + {"mtextra", {aMTExtraTab, "StarSymbol", nullptr}} +}; + +static const RecodeTable aAppleSymbolRecodeTable[] = { + {"symbol", {aAdobeSymbolToAppleSymbolTab, "AppleSymbol", nullptr}} +}; + +static ConvertChar aImplStarSymbolCvt = { nullptr, "StarBats", ImplStarSymbolToStarBats }; + +const ConvertChar* ConvertChar::GetRecodeData( const OUString& rOrgFontName, const OUString& rMapFontName ) +{ + const ConvertChar* pCvt = nullptr; + + // clean up and lowercase font name + OUString aOrgName( GetEnglishSearchFontName( rOrgFontName ) ); + OUString aMapName( GetEnglishSearchFontName( rMapFontName ) ); + + if( aMapName == "starsymbol" + || aMapName == "opensymbol" ) + { + for( int i = 0; i < int(SAL_N_ELEMENTS(aStarSymbolRecodeTable)); ++i) + { + const RecodeTable& r = aStarSymbolRecodeTable[i]; + if( aOrgName.equalsAscii( r.pOrgName ) ) + { + pCvt = &r.aCvt; + break; + } + } + } + //It's plausible that it's better to implement this + //as an additional encoding alongside the existing + //adobe-symbol to unicode conversion in rtl instead + else if( aMapName == "applesymbol" ) + { + for( int i = 0; i < int(SAL_N_ELEMENTS(aAppleSymbolRecodeTable)); ++i) + { + const RecodeTable& r = aAppleSymbolRecodeTable[i]; + if( aOrgName.equalsAscii( r.pOrgName ) ) + { + pCvt = &r.aCvt; + break; + } + } + } + else if( aMapName == "starbats" ) + { + if( aOrgName == "starsymbol" ) + pCvt = &aImplStarSymbolCvt; + else if( aOrgName == "opensymbol" ) + pCvt = &aImplStarSymbolCvt; + } + + return pCvt; +} + +FontToSubsFontConverter CreateFontToSubsFontConverter( const OUString& rOrgName, FontToSubsFontFlags nFlags ) +{ + const ConvertChar* pCvt = nullptr; + + OUString aName = GetEnglishSearchFontName( rOrgName ); + + if ( nFlags == FontToSubsFontFlags::IMPORT ) + { + const int nEntries = 2; // only StarMath+StarBats + for( int i = 0; i < nEntries; ++i ) + { + const RecodeTable& r = aStarSymbolRecodeTable[i]; + if( aName.equalsAscii( r.pOrgName ) ) + { + pCvt = &r.aCvt; + break; + } + } + } + else + { + // TODO: only StarMath+StarBats + if( aName == "starsymbol" ) + pCvt = &aImplStarSymbolCvt; + else if( aName == "opensymbol" ) + pCvt = &aImplStarSymbolCvt; + } + + return const_cast<ConvertChar *>(pCvt); +} + +sal_Unicode ConvertFontToSubsFontChar( + FontToSubsFontConverter hConverter, sal_Unicode cChar ) +{ + if ( hConverter ) + return static_cast<ConvertChar*>(hConverter)->RecodeChar( cChar ); + else + return cChar; +} + +OUString GetFontToSubsFontName( FontToSubsFontConverter hConverter ) +{ + if ( !hConverter ) + return OUString(); + + const char* pName = static_cast<ConvertChar*>(hConverter)->mpSubsFontName; + return OUString::createFromAscii( pName ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/misc/fontdefs.cxx b/unotools/source/misc/fontdefs.cxx new file mode 100644 index 000000000..3eb4d69bd --- /dev/null +++ b/unotools/source/misc/fontdefs.cxx @@ -0,0 +1,560 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unotools/fontdefs.hxx> +#include <unotools/fontcfg.hxx> +#include <rtl/ustrbuf.hxx> +#include <unordered_map> + +namespace { + +struct ImplLocalizedFontName +{ + const char* mpEnglishName; + const sal_Unicode* mpLocalizedNames; +}; + +} + +// TODO: where did the 0,0 delimiters come from? A single 0 should suffice... +static sal_Unicode const aBatang[] = { 0xBC14, 0xD0D5, 0, 0 }; +static sal_Unicode const aBatangChe[] = { 0xBC14, 0xD0D5, 0xCCB4, 0, 0 }; +static sal_Unicode const aGungsuh[] = { 0xAD81, 0xC11C, 0, 0 }; +static sal_Unicode const aGungsuhChe[] = { 0xAD81, 0xC11C, 0xCCB4, 0, 0 }; +static sal_Unicode const aGulim[] = { 0xAD74, 0xB9BC, 0, 0 }; +static sal_Unicode const aGulimChe[] = { 0xAD74, 0xB9BC, 0xCCB4, 0, 0 }; +static sal_Unicode const aDotum[] = { 0xB3CB, 0xC6C0, 0, 0 }; +static sal_Unicode const aDotumChe[] = { 0xB3CB, 0xC6C0, 0xCCB4, 0, 0 }; +static sal_Unicode const aSimSun[] = { 0x5B8B, 0x4F53, 0, 0 }; +static sal_Unicode const aNSimSun[] = { 0x65B0, 0x5B8B, 0x4F53, 0, 0 }; +static sal_Unicode const aSimHei[] = { 0x9ED1, 0x4F53, 0, 0 }; +static sal_Unicode const aSimKai[] = { 0x6977, 0x4F53, 0, 0 }; +static sal_Unicode const azycjkSun[] = { 0x4E2D, 0x6613, 0x5B8B, 0x4F53, 0, 0 }; +static sal_Unicode const azycjkHei[] = { 0x4E2D, 0x6613, 0x9ED1, 0x4F53, 0, 0 }; +static sal_Unicode const azycjkKai[] = { 0x4E2D, 0x6613, 0x6977, 0x4F53, 0, 0 }; +static sal_Unicode const aFZHei[] = { 0x65B9, 0x6B63, 0x9ED1, 0x4F53, 0, 0 }; +static sal_Unicode const aFZKai[] = { 0x65B9, 0x6B63, 0x6977, 0x4F53, 0, 0 }; +static sal_Unicode const aFZSongYI[] = { 0x65B9, 0x6B63, 0x5B8B, 0x4E00, 0, 0 }; +static sal_Unicode const aFZShuSong[] = { 0x65B9, 0x6B63, 0x4E66, 0x5B8B, 0, 0 }; +static sal_Unicode const aFZFangSong[] = { 0x65B9, 0x6B63, 0x4EFF, 0x5B8B, 0, 0 }; +// Attention: this fonts includes the wrong encoding vector - so we double the names with correct and wrong encoding +// First one is the GB-Encoding (we think the correct one), second is the big5 encoded name +static sal_Unicode const aMHei[] = { 'm', 0x7B80, 0x9ED1, 0, 'm', 0x6F60, 0x7AAA, 0, 0 }; +static sal_Unicode const aMKai[] = { 'm', 0x7B80, 0x6977, 0x566C, 0, 'm', 0x6F60, 0x7FF1, 0x628E, 0, 0 }; +static sal_Unicode const aMSong[] = { 'm', 0x7B80, 0x5B8B, 0, 'm', 0x6F60, 0x51BC, 0, 0 }; +static sal_Unicode const aCFangSong[] = { 'm', 0x7B80, 0x592B, 0x5B8B, 0, 'm', 0x6F60, 0x6E98, 0x51BC, 0, 0 }; +static sal_Unicode const aMingLiU[] = { 0x7D30, 0x660E, 0x9AD4, 0, 0 }; +static sal_Unicode const aPMingLiU[] = { 0x65B0, 0x7D30, 0x660E, 0x9AD4, 0, 0 }; +static sal_Unicode const aHei[] = { 0x6865, 0, 0 }; +static sal_Unicode const aKai[] = { 0x6B61, 0, 0 }; +static sal_Unicode const aMing[] = { 0x6D69, 0x6E67, 0, 0 }; +static sal_Unicode const aMSGothic[] = { 'm','s', 0x30B4, 0x30B7, 0x30C3, 0x30AF, 0, 0 }; +static sal_Unicode const aMSPGothic[] = { 'm','s','p', 0x30B4, 0x30B7, 0x30C3, 0x30AF, 0, 0 }; +static sal_Unicode const aMSMincho[] = { 'm', 's', 0x660E, 0x671D, 0 }; +static sal_Unicode const aMSPMincho[] = { 'm','s','p', 0x660E, 0x671D, 0 }; +static sal_Unicode const aMSYaHei[] = { 0x5FAE, 0x8F6F, 0x96C5, 0x9ED1, 0 }; +static sal_Unicode const aMSJhengHei[] = { 0x5FAE, 0x8EDF, 0x6B63, 0x9ED1, 0x9AD4, 0 }; +static sal_Unicode const aMeiryo[] = { 0x30e1, 0x30a4, 0x30ea, 0x30aa, 0 }; +static sal_Unicode const aHGMinchoL[] = { 'h','g', 0x660E, 0x671D, 'l', 0, 0 }; +static sal_Unicode const aHGGothicB[] = { 'h','g', 0x30B4, 0x30B7, 0x30C3, 0x30AF, 'b', 0 }; +static sal_Unicode const aHGPMinchoL[] = { 'h','g','p', 0x660E, 0x671D, 'l', 0 }; +static sal_Unicode const aHGPGothicB[] = { 'h','g','p', 0x30B4, 0x30B7, 0x30C3, 0x30AF, 'b', 0 }; +static sal_Unicode const aHGMinchoLSun[] = { 'h','g', 0x660E, 0x671D, 'l', 's', 'u', 'n', 0 }; +static sal_Unicode const aHGPMinchoLSun[] = { 'h','g','p', 0x660E, 0x671D, 'l', 's', 'u', 'n', 0 }; +static sal_Unicode const aHGGothicBSun[] = { 'h', 'g', 0x30B4, 0x30B7, 0x30C3, 0x30AF, 'b', 's', 'u', 'n', 0 }; +static sal_Unicode const aHGPGothicBSun[] = { 'h', 'g', 'p', 0x30B4, 0x30B7, 0x30C3, 0x30AF, 'b', 's', 'u', 'n', 0 }; +static sal_Unicode const aHGHeiseiMin[] = { 'h', 'g', 0x5E73, 0x6210, 0x660E, 0x671D, 0x4F53, 0, 'h', 'g', 0x5E73, 0x6210, 0x660E, 0x671D, 0x4F53, 'w', '3', 'x', '1', '2', 0, 0 }; +static sal_Unicode const aIPAMincho[] = { 'i', 'p', 'a', 0x660E, 0x671D, 0 }; +static sal_Unicode const aIPAPMincho[] = { 'i', 'p', 'a', 'p', 0x660E, 0x671D, 0 }; +static sal_Unicode const aIPAGothic[] = { 'i', 'p', 'a', 0x30B4, 0x30B7, 0x30C3, 0x30AF, 0 }; +static sal_Unicode const aIPAPGothic[] = { 'i', 'p', 'a', 'p', 0x30B4, 0x30B7, 0x30C3, 0x30AF, 0 }; +static sal_Unicode const aIPAUIGothic[] = { 'i', 'p', 'a', 'u', 'i', 0x30B4, 0x30B7, 0x30C3, 0x30AF, 0 }; +static sal_Unicode const aTakaoMincho[] = { 't', 'a', 'k', 'a', 'o', 0x660E, 0x671D, 0 }; +static sal_Unicode const aTakaoPMincho[] = { 't', 'a', 'k', 'a', 'o', 'p', 0x660E, 0x671D, 0 }; +static sal_Unicode const aTakaoGothic[] = { 't', 'a', 'k', 'a', 'o', 0x30B4, 0x30B7, 0x30C3, 0x30AF, 0 }; +static sal_Unicode const aTakaoPGothic[] = { 't', 'a', 'k', 'a', 'o', 'p', 0x30B4, 0x30B7, 0x30C3, 0x30AF, 0 }; +static sal_Unicode const aSazanamiMincho[] = { 0x3055, 0x3056, 0x306A, 0x307F, 0x660E, 0x671D, 0, 0 }; +static sal_Unicode const aSazanamiGothic[] = { 0x3055, 0x3056, 0x306A, 0x307F, 0x30B4, 0x30B7, 0x30C3, 0x30AF, 0, 0 }; +static sal_Unicode const aKochiMincho[] = { 0x6771, 0x98A8, 0x660E, 0x671D, 0, 0 }; +static sal_Unicode const aKochiGothic[] = { 0x6771, 0x98A8, 0x30B4, 0x30B7, 0x30C3, 0x30AF, 0, 0 }; +static sal_Unicode const aSunDotum[] = { 0xC36C, 0xB3CB, 0xC6C0, 0, 0 }; +static sal_Unicode const aSunGulim[] = { 0xC36C, 0xAD74, 0xB9BC, 0, 0 }; +static sal_Unicode const aSunBatang[] = { 0xC36C, 0xBC14, 0xD0D5, 0, 0 }; +static sal_Unicode const aBaekmukDotum[] = { 0xBC31, 0xBB35, 0xB3CB, 0xC6C0, 0, 0 }; +static sal_Unicode const aBaekmukGulim[] = { 0xBC31, 0xBB35, 0xAD74, 0xB9BC, 0, 0 }; +static sal_Unicode const aBaekmukBatang[] = { 0xBC31, 0xBB35, 0xBC14, 0xD0D5, 0, 0 }; +static sal_Unicode const aFzMingTi[] = { 0x65B9, 0x6B63, 0x660E, 0x9AD4, 0, 0 }; +static sal_Unicode const aFzHeiTiTW[]= { 0x65B9, 0x6B63, 0x9ED1, 0x9AD4, 0, 0 }; +static sal_Unicode const aFzKaiTiTW[]= { 0x65B9, 0x6B63, 0x6977, 0x9AD4, 0, 0 }; +static sal_Unicode const aFzHeiTiCN[]= { 0x65B9, 0x6B63, 0x9ED1, 0x4F53, 0, 0 }; +static sal_Unicode const aFzKaiTiCN[]= { 0x65B9, 0x6B63, 0x6977, 0x4F53, 0, 0 }; +static sal_Unicode const aFzSongTi[] = { 0x65B9, 0x6B63, 0x5B8B, 0x4F53, 0, 0 }; +static sal_Unicode const aHYMyeongJoExtra[] = { 'h', 'y', 0xACAC, 0xBA85, 0xC870, 0, 0 }; +static sal_Unicode const aHYSinMyeongJoMedium[] = { 'h', 'y', 0xC2E0, 0xBA85, 0xC870, 0, 0 }; +static sal_Unicode const aHYGothicMedium[] = { 'h', 'y', 0xC911, 0xACE0, 0xB515, 0, 0 }; +static sal_Unicode const aHYGraphicMedium[] = { 'h', 'y', 0xADF8, 0xB798, 0xD53D, 'm', 0, 0 }; +static sal_Unicode const aHYGraphic[] = { 'h', 'y', 0xADF8, 0xB798, 0xD53D, 0, 0 }; +static sal_Unicode const aNewGulim[] = { 0xC0C8, 0xAD74, 0xB9BC, 0, 0 }; +static sal_Unicode const aSunGungseo[] = { 0xC36C, 0xAD81, 0xC11C, 0, 0 }; +static sal_Unicode const aHYGungSoBold[] = { 'h','y', 0xAD81, 0xC11C, 'b', 0, 0 }; +static sal_Unicode const aHYGungSo[] = { 'h','y', 0xAD81, 0xC11C, 0, 0 }; +static sal_Unicode const aSunHeadLine[] = { 0xC36C, 0xD5E4, 0xB4DC, 0xB77C, 0xC778, 0, 0 }; +static sal_Unicode const aHYHeadLineMedium[] = { 'h', 'y', 0xD5E4, 0xB4DC, 0xB77C, 0xC778, 'm', 0, 0 }; +static sal_Unicode const aHYHeadLine[] = { 'h', 'y', 0xD5E4, 0xB4DC, 0xB77C, 0xC778, 0, 0 }; +static sal_Unicode const aYetR[] = { 0xD734, 0xBA3C, 0xC61B, 0xCCB4, 0, 0 }; +static sal_Unicode const aHYGothicExtra[] = { 'h', 'y', 0xACAC, 0xACE0, 0xB515, 0, 0 }; +static sal_Unicode const aSunMokPan[] = { 0xC36C, 0xBAA9, 0xD310, 0, 0 }; +static sal_Unicode const aSunYeopseo[] = { 0xC36C, 0xC5FD, 0xC11C, 0, 0 }; +static sal_Unicode const aSunBaekSong[] = { 0xC36C, 0xBC31, 0xC1A1, 0, 0 }; +static sal_Unicode const aHYPostLight[] = { 'h', 'y', 0xC5FD, 0xC11C, 'l', 0, 0 }; +static sal_Unicode const aHYPost[] = { 'h', 'y', 0xC5FD, 0xC11C, 0, 0 }; +static sal_Unicode const aMagicR[] = { 0xD734, 0xBA3C, 0xB9E4, 0xC9C1, 0xCCB4, 0, 0 }; +static sal_Unicode const aSunCrystal[] = { 0xC36C, 0xD06C, 0xB9AC, 0xC2A4, 0xD0C8, 0, 0 }; +static sal_Unicode const aSunSaemmul[] = { 0xC36C, 0xC0D8, 0xBB3C, 0, 0 }; +static sal_Unicode const aHaansoftBatang[] = { 0xD55C, 0xCEF4, 0xBC14, 0xD0D5, 0, 0 }; +static sal_Unicode const aHaansoftDotum[] = { 0xD55C, 0xCEF4, 0xB3CB, 0xC6C0, 0, 0 }; +static sal_Unicode const aHyhaeseo[] = { 0xD55C, 0xC591, 0xD574, 0xC11C, 0, 0 }; +static sal_Unicode const aMDSol[] = { 'm', 'd', 0xC194, 0xCCB4, 0, 0 }; +static sal_Unicode const aMDGaesung[] = { 'm', 'd', 0xAC1C, 0xC131, 0xCCB4, 0, 0 }; +static sal_Unicode const aMDArt[] = { 'm', 'd', 0xC544, 0xD2B8, 0xCCB4, 0, 0 }; +static sal_Unicode const aMDAlong[] = { 'm', 'd', 0xC544, 0xB871, 0xCCB4, 0, 0 }; +static sal_Unicode const aMDEasop[] = { 'm', 'd', 0xC774, 0xC19D, 0xCCB4, 0, 0 }; +static sal_Unicode const aHYShortSamulMedium[] = { 'h', 'y', 0xC595, 0xC740, 0xC0D8, 0xBB3C, 'm', 0 }; +static sal_Unicode const aHYShortSamul[] = { 'h', 'y', 0xC595, 0xC740, 0xC0D8, 0xBB3C, 0 }; +static sal_Unicode const aHGGothicE[] = { 'h','g', 0xFF7A, 0xFF9E, 0xFF7C, 0xFF6F, 0xFF78, 'e', 0 }; +static sal_Unicode const aHGPGothicE[] = { 'h','g','p', 0xFF7A, 0xFF9E, 0xFF7C, 0xFF6F, 0xFF78, 'e', 0 }; +static sal_Unicode const aHGSGothicE[] = { 'h','g','s', 0xFF7A, 0xFF9E, 0xFF7C, 0xFF6F, 0xFF78, 'e', 0 }; +static sal_Unicode const aHGGothicM[] = { 'h','g', 0xFF7A, 0xFF9E, 0xFF7C, 0xFF6F, 0xFF78, 'm', 0 }; +static sal_Unicode const aHGPGothicM[] = { 'h','g','p', 0xFF7A, 0xFF9E, 0xFF7C, 0xFF6F, 0xFF78, 'm', 0 }; +static sal_Unicode const aHGSGothicM[] = { 'h','g','s', 0xFF7A, 0xFF9E, 0xFF7C, 0xFF6F, 0xFF78, 'm', 0 }; +static sal_Unicode const aHGGyoshotai[] = { 'h','g', 0x884C, 0x66F8, 0x4F53, 0 }; +static sal_Unicode const aHGPGyoshotai[] = { 'h','g','p', 0x884C, 0x66F8, 0x4F53, 0 }; +static sal_Unicode const aHGSGyoshotai[] = { 'h','g','s', 0x884C, 0x66F8, 0x4F53, 0 }; +static sal_Unicode const aHGKyokashotai[] = { 'h','g', 0x6559, 0x79D1, 0x66F8, 0x4F53, 0 }; +static sal_Unicode const aHGPKyokashotai[] = { 'h','g','p', 0x6559, 0x79D1, 0x66F8, 0x4F53, 0 }; +static sal_Unicode const aHGSKyokashotai[] = { 'h','g','s', 0x6559, 0x79D1, 0x66F8, 0x4F53, 0 }; +static sal_Unicode const aHGMinchoB[] = { 'h','g', 0x660E, 0x671D, 'b', 0 }; +static sal_Unicode const aHGPMinchoB[] = { 'h','g','p', 0x660E, 0x671D, 'b', 0 }; +static sal_Unicode const aHGSMinchoB[] = { 'h','g','s', 0x660E, 0x671D, 'b', 0 }; +static sal_Unicode const aHGMinchoE[] = { 'h','g', 0x660E, 0x671D, 'e', 0 }; +static sal_Unicode const aHGPMinchoE[] = { 'h','g','p', 0x660E, 0x671D, 'e', 0 }; +static sal_Unicode const aHGSMinchoE[] = { 'h','g','s', 0x660E, 0x671D, 'e', 0 }; +static sal_Unicode const aHGSoeiKakupoptai[] = { 'h','g', 0x5275,0x82F1,0x89D2,0xFF8E, + 0xFF9F,0xFF6F,0xFF8C,0xFF9F,0x4F53,0}; +static sal_Unicode const aHGPSoeiKakupoptai[] = { 'h','g', 'p', 0x5275,0x82F1,0x89D2,0xFF8E, + 0xFF9F,0xFF6F,0xFF8C,0xFF9F,0x4F53,0}; +static sal_Unicode const aHGSSoeiKakupoptai[] = { 'h','g', 's', 0x5275,0x82F1,0x89D2,0xFF8E, + 0xFF9F,0xFF6F,0xFF8C,0xFF9F,0x4F53,0}; +static sal_Unicode const aHGSoeiPresenceEB[] = { 'h','g', 0x5275,0x82F1,0xFF8C,0xFF9F, + 0xFF9A,0xFF7E,0xFF9E,0xFF9D,0xFF7D, 'e','b',0}; +static sal_Unicode const aHGPSoeiPresenceEB[] = { 'h','g','p', 0x5275,0x82F1,0xFF8C,0xFF9F, + 0xFF9A,0xFF7E,0xFF9E,0xFF9D,0xFF7D, 'e','b',0}; +static sal_Unicode const aHGSSoeiPresenceEB[] = { 'h','g','s', 0x5275,0x82F1,0xFF8C,0xFF9F, + 0xFF9A,0xFF7E,0xFF9E,0xFF9D,0xFF7D, 'e','b',0}; +static sal_Unicode const aHGSoeiKakugothicUB[] = { 'h','g', 0x5275,0x82F1,0x89D2,0xFF7A, + 0xFF9E,0xFF7C,0xFF6F,0xFF78,'u','b',0}; +static sal_Unicode const aHGPSoeiKakugothicUB[] = { 'h','g','p', 0x5275,0x82F1,0x89D2,0xFF7A, + 0xFF9E,0xFF7C,0xFF6F,0xFF78,'u','b',0}; +static sal_Unicode const aHGSSoeiKakugothicUB[] = { 'h','g','s', 0x5275,0x82F1,0x89D2,0xFF7A, + 0xFF9E,0xFF7C,0xFF6F,0xFF78,'u','b',0}; +static sal_Unicode const aHGSeikaishotaiPRO[] = { 'h','g', 0x6B63,0x6977,0x66F8,0x4F53, '-','p','r','o',0}; +static sal_Unicode const aHGMaruGothicMPRO[] = { 'h','g', 0x4E38,0xFF7A,0xFF9E,0xFF7C,0xFF6F,0xFF78, '-','p','r','o',0}; +static sal_Unicode const aHiraginoMinchoPro[] = { 0x30D2, 0x30E9, 0x30AE, 0x30CE, 0x660E, 0x671D, 'p','r','o',0}; +static sal_Unicode const aHiraginoMinchoProN[] = { 0x30D2, 0x30E9, 0x30AE, 0x30CE, 0x660E, 0x671D, 'p','r','o','n',0}; +static sal_Unicode const aHiraginoKakuGothic[] = { 0x30D2, 0x30E9, 0x30AE, 0x30CE, 0x89D2, 0x30B4, 0x30B7, 0x30C3, 0x30AF,0}; +static sal_Unicode const aHiraginoKakuGothicPro[] = { 0x30D2, 0x30E9, 0x30AE, 0x30CE, 0x89D2, 0x30B4, 'p','r','o',0}; +static sal_Unicode const aHiraginoKakuGothicProN[] = { 0x30D2, 0x30E9, 0x30AE, 0x30CE, 0x89D2, 0x30B4, 'p','r','o','n',0}; +static sal_Unicode const aHiraginoMaruGothicPro[] = { 0x30D2, 0x30E9, 0x30AE, 0x30CE, 0x4E38, 0x30B4, 'p','r','o',0}; +static sal_Unicode const aHiraginoMaruGothicProN[] = { 0x30D2, 0x30E9, 0x30AE, 0x30CE, 0x4E38, 0x30B4, 'p','r','o','n',0}; + +static const ImplLocalizedFontName aImplLocalizedNamesList[] = +{ +{ "batang", aBatang }, +{ "batangche", aBatangChe }, +{ "gungshu", aGungsuh }, +{ "gungshuche", aGungsuhChe }, +{ "gulim", aGulim }, +{ "gulimche", aGulimChe }, +{ "dotum", aDotum }, +{ "dotumche", aDotumChe }, +{ "simsun", aSimSun }, +{ "nsimsun", aNSimSun }, +{ "simhei", aSimHei }, +{ "simkai", aSimKai }, +{ "zycjksun", azycjkSun }, +{ "zycjkhei", azycjkHei }, +{ "zycjkkai", azycjkKai }, +{ "fzhei", aFZHei }, +{ "fzkai", aFZKai }, +{ "fzsong", aFZSongYI }, +{ "fzshusong", aFZShuSong }, +{ "fzfangsong", aFZFangSong }, +{ "mhei", aMHei }, +{ "mkai", aMKai }, +{ "msong", aMSong }, +{ "cfangsong", aCFangSong }, +{ "mingliu", aMingLiU }, +{ "pmingliu", aPMingLiU }, +{ "hei", aHei }, +{ "kai", aKai }, +{ "ming", aMing }, +{ "msgothic", aMSGothic }, +{ "mspgothic", aMSPGothic }, +{ "msmincho", aMSMincho }, +{ "mspmincho", aMSPMincho }, +{ "microsoftjhenghei", aMSJhengHei }, +{ "microsoftyahei", aMSYaHei }, +{ "meiryo", aMeiryo }, +{ "hgminchol", aHGMinchoL }, +{ "hggothicb", aHGGothicB }, +{ "hgpminchol", aHGPMinchoL }, +{ "hgpgothicb", aHGPGothicB }, +{ "hgmincholsun", aHGMinchoLSun }, +{ "hggothicbsun", aHGGothicBSun }, +{ "hgpmincholsun", aHGPMinchoLSun }, +{ "hgpgothicbsun", aHGPGothicBSun }, +{ "hgheiseimin", aHGHeiseiMin }, +{ "ipamincho", aIPAMincho }, +{ "ipapmincho", aIPAPMincho }, +{ "ipagothic", aIPAGothic }, +{ "ipapgothic", aIPAPGothic }, +{ "ipauigothic", aIPAUIGothic }, +{ "takaomincho", aTakaoMincho }, +{ "takaopmincho", aTakaoPMincho }, +{ "takaogothic", aTakaoGothic }, +{ "takaopgothic", aTakaoPGothic }, +{ "sazanamimincho", aSazanamiMincho }, +{ "sazanamigothic", aSazanamiGothic }, +{ "kochimincho", aKochiMincho }, +{ "kochigothic", aKochiGothic }, +{ "sundotum", aSunDotum }, +{ "sungulim", aSunGulim }, +{ "sunbatang", aSunBatang }, +{ "baekmukdotum", aBaekmukDotum }, +{ "baekmukgulim", aBaekmukGulim }, +{ "baekmukbatang", aBaekmukBatang }, +{ "fzheiti", aFzHeiTiCN }, +{ "fzheiti", aFzHeiTiTW }, +{ "fzkaiti", aFzKaiTiCN }, +{ "fzkaitib", aFzKaiTiTW }, +{ "fzmingtib", aFzMingTi }, +{ "fzsongti", aFzSongTi }, +{ "hymyeongjoextra", aHYMyeongJoExtra }, +{ "hysinmyeongjomedium", aHYSinMyeongJoMedium }, +{ "hygothicmedium", aHYGothicMedium }, +{ "hygraphicmedium", aHYGraphicMedium }, +{ "hygraphic", aHYGraphic }, +{ "newgulim", aNewGulim }, +{ "sungungseo", aSunGungseo }, +{ "hygungsobold", aHYGungSoBold }, +{ "hygungso", aHYGungSo }, +{ "sunheadline", aSunHeadLine }, +{ "hyheadlinemedium", aHYHeadLineMedium }, +{ "hyheadline", aHYHeadLine }, +{ "yetr", aYetR }, +{ "hygothicextra", aHYGothicExtra }, +{ "sunmokpan", aSunMokPan }, +{ "sunyeopseo", aSunYeopseo }, +{ "sunbaeksong", aSunBaekSong }, +{ "hypostlight", aHYPostLight }, +{ "hypost", aHYPost }, +{ "magicr", aMagicR }, +{ "suncrystal", aSunCrystal }, +{ "sunsaemmul", aSunSaemmul }, +{ "hyshortsamulmedium", aHYShortSamulMedium }, +{ "hyshortsamul", aHYShortSamul }, +{ "haansoftbatang", aHaansoftBatang }, +{ "haansoftdotum", aHaansoftDotum }, +{ "hyhaeseo", aHyhaeseo }, +{ "mdsol", aMDSol }, +{ "mdgaesung", aMDGaesung }, +{ "mdart", aMDArt }, +{ "mdalong", aMDAlong }, +{ "mdeasop", aMDEasop }, +{ "hggothice", aHGGothicE }, +{ "hgpgothice", aHGPGothicE }, +{ "hgsgothice", aHGSGothicE }, +{ "hggothicm", aHGGothicM }, +{ "hgpgothicm", aHGPGothicM }, +{ "hgsgothicm", aHGSGothicM }, +{ "hggyoshotai", aHGGyoshotai }, +{ "hgpgyoshotai", aHGPGyoshotai }, +{ "hgsgyoshotai", aHGSGyoshotai }, +{ "hgkyokashotai", aHGKyokashotai }, +{ "hgpkyokashotai", aHGPKyokashotai }, +{ "hgskyokashotai", aHGSKyokashotai }, +{ "hgminchob", aHGMinchoB }, +{ "hgpminchob", aHGPMinchoB }, +{ "hgsminchob", aHGSMinchoB }, +{ "hgminchoe", aHGMinchoE }, +{ "hgpminchoe", aHGPMinchoE }, +{ "hgsminchoe", aHGSMinchoE }, +{ "hgsoeikakupoptai", aHGSoeiKakupoptai }, +{ "hgpsoeikakupopta", aHGPSoeiKakupoptai }, +{ "hgssoeikakupopta", aHGSSoeiKakupoptai }, +{ "hgsoeipresenceeb", aHGSoeiPresenceEB }, +{ "hgpsoeipresenceeb", aHGPSoeiPresenceEB }, +{ "hgssoeipresenceeb", aHGSSoeiPresenceEB }, +{ "hgsoeikakugothicub", aHGSoeiKakugothicUB }, +{ "hgpsoeikakugothicub", aHGPSoeiKakugothicUB }, +{ "hgssoeikakugothicub", aHGSSoeiKakugothicUB }, +{ "hgseikaishotaipro", aHGSeikaishotaiPRO }, +{ "hgmarugothicmpro", aHGMaruGothicMPRO }, +{ "hiraginominchopro", aHiraginoMinchoPro }, +{ "hiraginominchopron", aHiraginoMinchoProN }, +{ "hiraginosans", aHiraginoKakuGothic }, +{ "hiraginokakugothicpro", aHiraginoKakuGothicPro }, +{ "hiraginokakugothicpron", aHiraginoKakuGothicProN }, +{ "hiraginomarugothicpro", aHiraginoMaruGothicPro }, +{ "hiraginomarugothicpron", aHiraginoMaruGothicProN }, +{ nullptr, nullptr }, +}; + +OUString StripScriptFromName(const OUString& _aName) +{ + // I worry that someone will have a font which *does* have + // e.g. "Greek" legitimately at the end of its name :-( + const char*const suffixes[] = { " baltic", + " ce", + " cyr", + " greek", + " tur", + " (arabic)", + " (hebrew)", + " (thai)", + " (vietnamese)" + }; + + OUString aName = _aName; + // These can be crazily piled up, e.g. Times New Roman CYR Greek + bool bFinished = false; + while (!bFinished) + { + bFinished = true; + for (const char* suffix : suffixes) + { + size_t nLen = strlen(suffix); + if (aName.endsWithIgnoreAsciiCaseAsciiL(suffix, nLen)) + { + bFinished = false; + aName = aName.copy(0, aName.getLength() - nLen); + } + } + } + return aName; +} + +OUString GetEnglishSearchFontName(const OUString& rInName) +{ + OUStringBuffer rName(rInName); + bool bNeedTranslation = false; + sal_Int32 nLen = rName.getLength(); + + // Remove trailing whitespaces + sal_Int32 i = nLen; + while ( i && (rName[ i-1 ] < 32) ) + i--; + if ( i != nLen ) + rName.truncate(i); + + nLen = rName.getLength(); + + // remove all whitespaces and converts to lower case ASCII + // TODO: better transliteration to ASCII e.g. all digits + i = 0; + while ( i < nLen ) + { + sal_Unicode c = rName[ i ]; + if ( c > 127 ) + { + // Translate to Lowercase-ASCII + // FullWidth-ASCII to half ASCII + if ( (c >= 0xFF00) && (c <= 0xFF5E) ) + { + c -= 0xFF00-0x0020; + // Upper to Lower + if ( (c >= 'A') && (c <= 'Z') ) + c += 'a' - 'A'; + + rName[ i ] = c; + + } + else + { + // Only Fontnames with None-Ascii-Characters must be translated + bNeedTranslation = true; + } + } + // not lowercase Ascii + else if ( !((c >= 'a') && (c <= 'z')) ) + { + // To Lowercase-Ascii + if ( (c >= 'A') && (c <= 'Z') ) + { + c += 'a' - 'A'; + rName[ i ] = c; + } + else if( ((c < '0') || (c > '9')) && (c != ';') && (c != '(') && (c != ')') ) // not 0-9, semicolon, or brackets + { + // Remove white spaces and special characters + rName.remove(i,1); + nLen--; + continue; + } + } + + i++; + } + OUString rNameStr = rName.makeStringAndClear(); + // translate normalized localized name to its normalized English ASCII name + if( bNeedTranslation ) + { + typedef std::unordered_map<OUString, const char*> FontNameDictionary; + static FontNameDictionary aDictionary( SAL_N_ELEMENTS(aImplLocalizedNamesList) ); + // the font name dictionary needs to be initialized once + if( aDictionary.empty() ) + { + // TODO: check if all dictionary entries are already normalized? + const ImplLocalizedFontName* pList = aImplLocalizedNamesList; + for(; pList->mpEnglishName; ++pList ) + aDictionary[ pList->mpLocalizedNames ] = pList->mpEnglishName; + } + + FontNameDictionary::const_iterator it = aDictionary.find( rNameStr ); + if( it != aDictionary.end() ) + rNameStr = OUString::createFromAscii ( it->second ); + } + + return rNameStr; +} + +OUString GetNextFontToken( const OUString& rTokenStr, sal_Int32& rIndex ) +{ + // check for valid start index + sal_Int32 nStringLen = rTokenStr.getLength(); + if( rIndex >= nStringLen ) + { + rIndex = -1; + return OUString(); + } + + // find the next token delimiter and return the token substring + const sal_Unicode* pStr = rTokenStr.getStr() + rIndex; + const sal_Unicode* pEnd = rTokenStr.getStr() + nStringLen; + for(; pStr < pEnd; ++pStr ) + if( (*pStr == ';') || (*pStr == ',') ) + break; + + sal_Int32 nTokenStart = rIndex; + sal_Int32 nTokenLen; + if( pStr < pEnd ) + { + rIndex = sal::static_int_cast<sal_Int32>(pStr - rTokenStr.getStr()); + nTokenLen = rIndex - nTokenStart; + ++rIndex; // skip over token separator + } + else + { + // no token delimiter found => handle last token + rIndex = -1; + + // optimize if the token string consists of just one token + if( !nTokenStart ) + { + return rTokenStr; + } + else + { + nTokenLen = nStringLen - nTokenStart; + } + } + + return rTokenStr.copy( nTokenStart, nTokenLen ); +} + +static bool ImplIsFontToken( const OUString& rName, const OUString& rToken ) +{ + OUString aTempName; + sal_Int32 nIndex = 0; + do + { + aTempName = GetNextFontToken( rName, nIndex ); + if ( rToken == aTempName ) + return true; + } + while ( nIndex != -1 ); + + return false; +} + +static void ImplAppendFontToken( OUString& rName, const OUString& rNewToken ) +{ + if ( !rName.isEmpty() ) + { + rName += ";"; + } + rName += rNewToken; +} + +void AddTokenFontName( OUString& rName, const OUString& rNewToken ) +{ + if ( !ImplIsFontToken( rName, rNewToken ) ) + ImplAppendFontToken( rName, rNewToken ); +} + +OUString GetSubsFontName( const OUString& rName, SubsFontFlags nFlags ) +{ + OUString aName; + + sal_Int32 nIndex = 0; + OUString aOrgName = GetEnglishSearchFontName( + GetNextFontToken( rName, nIndex ) ); + + // #93662# do not try to replace StarSymbol with MS only font + if( nFlags == (SubsFontFlags::MS|SubsFontFlags::ONLYONE) + && ( aOrgName == "starsymbol" + || aOrgName == "opensymbol" ) ) + return aName; + + const utl::FontNameAttr* pAttr = utl::FontSubstConfiguration::get().getSubstInfo( aOrgName ); + if ( pAttr && (nFlags & SubsFontFlags::MS) ) + { + for( const auto& rSubstitution : pAttr->MSSubstitutions ) + if( ! ImplIsFontToken( rName, rSubstitution ) ) + { + ImplAppendFontToken( aName, rSubstitution ); + if( nFlags & SubsFontFlags::ONLYONE ) + { + break; + } + } + } + + return aName; +} + +bool IsStarSymbol(const OUString &rFontName) +{ + sal_Int32 nIndex = 0; + OUString sFamilyNm(GetNextFontToken(rFontName, nIndex)); + return (sFamilyNm.equalsIgnoreAsciiCase("starsymbol") || + sFamilyNm.equalsIgnoreAsciiCase("opensymbol")); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/misc/mediadescriptor.cxx b/unotools/source/misc/mediadescriptor.cxx new file mode 100644 index 000000000..7f8f4e3c3 --- /dev/null +++ b/unotools/source/misc/mediadescriptor.cxx @@ -0,0 +1,766 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <comphelper/docpasswordhelper.hxx> +#include <sal/log.hxx> +#include <unotools/configmgr.hxx> +#include <unotools/mediadescriptor.hxx> +#include <unotools/securityoptions.hxx> +#include <unotools/ucbhelper.hxx> +#include <comphelper/namedvaluecollection.hxx> +#include <comphelper/stillreadwriteinteraction.hxx> + +#include <com/sun/star/ucb/ContentCreationException.hpp> +#include <com/sun/star/ucb/XContent.hpp> +#include <com/sun/star/ucb/XCommandEnvironment.hpp> +#include <com/sun/star/task/XInteractionHandler.hpp> +#include <com/sun/star/io/XStream.hpp> +#include <com/sun/star/io/XActiveDataSink.hpp> +#include <com/sun/star/io/XSeekable.hpp> +#include <com/sun/star/lang/IllegalArgumentException.hpp> +#include <com/sun/star/uri/UriReferenceFactory.hpp> +#include <com/sun/star/uri/XUriReference.hpp> +#include <com/sun/star/ucb/PostCommandArgument2.hpp> +#include <officecfg/Office/Common.hxx> +#include <ucbhelper/content.hxx> +#include <ucbhelper/commandenvironment.hxx> +#include <ucbhelper/activedatasink.hxx> +#include <comphelper/processfactory.hxx> +#include <tools/urlobj.hxx> +#include <osl/diagnose.h> +#include <tools/diagnose_ex.h> + +namespace utl { + +namespace { + +OUString removeFragment(OUString const & uri) { + css::uno::Reference< css::uri::XUriReference > ref( + css::uri::UriReferenceFactory::create( + comphelper::getProcessComponentContext())-> + parse(uri)); + if (ref.is()) { + ref->clearFragment(); + return ref->getUriReference(); + } else { + SAL_WARN("unotools.misc", "cannot parse <" << uri << ">"); + return uri; + } +} + +} + +const OUString& MediaDescriptor::PROP_ABORTED() +{ + static const OUString sProp("Aborted"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_ASTEMPLATE() +{ + static const OUString sProp("AsTemplate"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_COMPONENTDATA() +{ + static const OUString sProp("ComponentData"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_DOCUMENTSERVICE() +{ + static const OUString sProp("DocumentService"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_ENCRYPTIONDATA() +{ + static const OUString sProp("EncryptionData"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_FILENAME() +{ + static const OUString sProp("FileName"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_FILTERNAME() +{ + static const OUString sProp("FilterName"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_FILTERPROVIDER() +{ + static const OUString aProp("FilterProvider"); + return aProp; +} + +const OUString& MediaDescriptor::PROP_FILTEROPTIONS() +{ + static const OUString sProp("FilterOptions"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_FRAME() +{ + static const OUString sProp("Frame"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_FRAMENAME() +{ + static const OUString sProp("FrameName"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_HIDDEN() +{ + static const OUString sProp("Hidden"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_INPUTSTREAM() +{ + static const OUString sProp("InputStream"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_INTERACTIONHANDLER() +{ + static const OUString sProp("InteractionHandler"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_AUTHENTICATIONHANDLER() +{ + static const OUString sProp("AuthenticationHandler"); + return sProp; +} + + const OUString& MediaDescriptor::PROP_JUMPMARK() +{ + static const OUString sProp("JumpMark"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_MACROEXECUTIONMODE() +{ + static const OUString sProp("MacroExecutionMode"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_MEDIATYPE() +{ + static const OUString sProp("MediaType"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_MINIMIZED() +{ + static const OUString sProp("Minimized"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_NOAUTOSAVE() +{ + static const OUString sProp("NoAutoSave"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_OPENNEWVIEW() +{ + static const OUString sProp("OpenNewView"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_OUTPUTSTREAM() +{ + static const OUString sProp("OutputStream"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_POSTDATA() +{ + static const OUString sProp("PostData"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_PREVIEW() +{ + static const OUString sProp("Preview"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_READONLY() +{ + static const OUString sProp("ReadOnly"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_REFERRER() +{ + static const OUString sProp("Referer"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_REPLACEABLE() +{ + static const OUString sProp("Replaceable"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_STATUSINDICATOR() +{ + static const OUString sProp("StatusIndicator"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_STREAM() +{ + static const OUString sProp("Stream"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_STREAMFOROUTPUT() +{ + static const OUString sProp("StreamForOutput"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_TEMPLATENAME() +{ + static const OUString sProp("TemplateName"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_TYPENAME() +{ + static const OUString sProp("TypeName"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_UCBCONTENT() +{ + static const OUString sProp("UCBContent"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_UPDATEDOCMODE() +{ + static const OUString sProp("UpdateDocMode"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_URL() +{ + static const OUString sProp("URL"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_VERSION() +{ + static const OUString sProp("Version"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_DOCUMENTTITLE() +{ + static const OUString sProp("DocumentTitle"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_MODEL() +{ + static const OUString sProp("Model"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_PASSWORD() +{ + static const OUString sProp("Password"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_TITLE() +{ + static const OUString sProp("Title"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_SALVAGEDFILE() +{ + static const OUString sProp("SalvagedFile"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_VIEWONLY() +{ + static const OUString sProp("ViewOnly"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_DOCUMENTBASEURL() +{ + static const OUString sProp("DocumentBaseURL"); + return sProp; +} + +const OUString& MediaDescriptor::PROP_SUGGESTEDSAVEASNAME() +{ + static const OUString sProp("SuggestedSaveAsName"); + return sProp; +} + +MediaDescriptor::MediaDescriptor() + : SequenceAsHashMap() +{ +} + +MediaDescriptor::MediaDescriptor(const css::uno::Sequence< css::beans::PropertyValue >& lSource) + : SequenceAsHashMap(lSource) +{ +} + +bool MediaDescriptor::isStreamReadOnly() const +{ + bool bReadOnly = false; + + // check for explicit readonly state + const_iterator pIt = find(MediaDescriptor::PROP_READONLY()); + if (pIt != end()) + { + pIt->second >>= bReadOnly; + return bReadOnly; + } + + // streams based on post data are readonly by definition + pIt = find(MediaDescriptor::PROP_POSTDATA()); + if (pIt != end()) + return true; + + // A XStream capsulate XInputStream and XOutputStream ... + // If it exists - the file must be open in read/write mode! + pIt = find(MediaDescriptor::PROP_STREAM()); + if (pIt != end()) + return false; + + // Only file system content provider is able to provide XStream + // so for this content impossibility to create XStream triggers + // switch to readonly mode. + try + { + css::uno::Reference< css::ucb::XContent > xContent = getUnpackedValueOrDefault(MediaDescriptor::PROP_UCBCONTENT(), css::uno::Reference< css::ucb::XContent >()); + if (xContent.is()) + { + css::uno::Reference< css::ucb::XContentIdentifier > xId = xContent->getIdentifier(); + OUString aScheme; + if (xId.is()) + aScheme = xId->getContentProviderScheme(); + + if (aScheme.equalsIgnoreAsciiCase("file")) + bReadOnly = true; + else + { + ::ucbhelper::Content aContent(xContent, + utl::UCBContentHelper::getDefaultCommandEnvironment(), + comphelper::getProcessComponentContext()); + aContent.getPropertyValue("IsReadOnly") >>= bReadOnly; + } + } + } + catch(const css::uno::RuntimeException& ) + { throw; } + catch(const css::uno::Exception&) + {} + + return bReadOnly; +} + +css::uno::Any MediaDescriptor::getComponentDataEntry( const OUString& rName ) const +{ + comphelper::SequenceAsHashMap::const_iterator aPropertyIter = find( PROP_COMPONENTDATA() ); + if( aPropertyIter != end() ) + return comphelper::NamedValueCollection( aPropertyIter->second ).get( rName ); + return css::uno::Any(); +} + +void MediaDescriptor::setComponentDataEntry( const OUString& rName, const css::uno::Any& rValue ) +{ + if( rValue.hasValue() ) + { + // get or create the 'ComponentData' property entry + css::uno::Any& rCompDataAny = operator[]( PROP_COMPONENTDATA() ); + // insert the value (retain sequence type, create NamedValue elements by default) + bool bHasNamedValues = !rCompDataAny.hasValue() || rCompDataAny.has< css::uno::Sequence< css::beans::NamedValue > >(); + bool bHasPropValues = rCompDataAny.has< css::uno::Sequence< css::beans::PropertyValue > >(); + OSL_ENSURE( bHasNamedValues || bHasPropValues, "MediaDescriptor::setComponentDataEntry - incompatible 'ComponentData' property in media descriptor" ); + if( bHasNamedValues || bHasPropValues ) + { + // insert or overwrite the passed value + comphelper::SequenceAsHashMap aCompDataMap( rCompDataAny ); + aCompDataMap[ rName ] = rValue; + // write back the sequence (restore sequence with correct element type) + rCompDataAny = aCompDataMap.getAsConstAny( bHasPropValues ); + } + } + else + { + // if an empty Any is passed, clear the entry + clearComponentDataEntry( rName ); + } +} + +void MediaDescriptor::clearComponentDataEntry( const OUString& rName ) +{ + comphelper::SequenceAsHashMap::iterator aPropertyIter = find( PROP_COMPONENTDATA() ); + if( aPropertyIter != end() ) + { + css::uno::Any& rCompDataAny = aPropertyIter->second; + bool bHasNamedValues = rCompDataAny.has< css::uno::Sequence< css::beans::NamedValue > >(); + bool bHasPropValues = rCompDataAny.has< css::uno::Sequence< css::beans::PropertyValue > >(); + OSL_ENSURE( bHasNamedValues || bHasPropValues, "MediaDescriptor::clearComponentDataEntry - incompatible 'ComponentData' property in media descriptor" ); + if( bHasNamedValues || bHasPropValues ) + { + // remove the value with the passed name + comphelper::SequenceAsHashMap aCompDataMap( rCompDataAny ); + aCompDataMap.erase( rName ); + // write back the sequence, or remove it completely if it is empty + if( aCompDataMap.empty() ) + erase( aPropertyIter ); + else + rCompDataAny = aCompDataMap.getAsConstAny( bHasPropValues ); + } + } +} + +css::uno::Sequence< css::beans::NamedValue > MediaDescriptor::requestAndVerifyDocPassword( + comphelper::IDocPasswordVerifier& rVerifier, + comphelper::DocPasswordRequestType eRequestType, + const ::std::vector< OUString >* pDefaultPasswords ) +{ + css::uno::Sequence< css::beans::NamedValue > aMediaEncData = getUnpackedValueOrDefault( + PROP_ENCRYPTIONDATA(), css::uno::Sequence< css::beans::NamedValue >() ); + OUString aMediaPassword = getUnpackedValueOrDefault( + PROP_PASSWORD(), OUString() ); + css::uno::Reference< css::task::XInteractionHandler > xInteractHandler = getUnpackedValueOrDefault( + PROP_INTERACTIONHANDLER(), css::uno::Reference< css::task::XInteractionHandler >() ); + OUString aDocumentName = getUnpackedValueOrDefault( + PROP_URL(), OUString() ); + + bool bIsDefaultPassword = false; + css::uno::Sequence< css::beans::NamedValue > aEncryptionData = comphelper::DocPasswordHelper::requestAndVerifyDocPassword( + rVerifier, aMediaEncData, aMediaPassword, xInteractHandler, aDocumentName, eRequestType, pDefaultPasswords, &bIsDefaultPassword ); + + erase( PROP_PASSWORD() ); + erase( PROP_ENCRYPTIONDATA() ); + + // insert encryption info into media descriptor + // TODO + if( aEncryptionData.hasElements() ) + (*this)[ PROP_ENCRYPTIONDATA() ] <<= aEncryptionData; + + return aEncryptionData; +} + +bool MediaDescriptor::addInputStream() +{ + return impl_addInputStream( true ); +} + +/*-----------------------------------------------*/ +bool MediaDescriptor::addInputStreamOwnLock() +{ + const bool bLock = !utl::ConfigManager::IsFuzzing() + && officecfg::Office::Common::Misc::UseDocumentSystemFileLocking::get(); + return impl_addInputStream(bLock); +} + +/*-----------------------------------------------*/ +bool MediaDescriptor::impl_addInputStream( bool bLockFile ) +{ + // check for an already existing stream item first + const_iterator pIt = find(MediaDescriptor::PROP_INPUTSTREAM()); + if (pIt != end()) + return true; + + try + { + // No stream available - create a new one + // a) data comes as PostData ... + pIt = find(MediaDescriptor::PROP_POSTDATA()); + if (pIt != end()) + { + const css::uno::Any& rPostData = pIt->second; + css::uno::Reference< css::io::XInputStream > xPostData; + rPostData >>= xPostData; + + return impl_openStreamWithPostData( xPostData ); + } + + // b) ... or we must get it from the given URL + OUString sURL = getUnpackedValueOrDefault(MediaDescriptor::PROP_URL(), OUString()); + if (sURL.isEmpty()) + throw css::uno::Exception("Found no URL.", + css::uno::Reference< css::uno::XInterface >()); + + return impl_openStreamWithURL( removeFragment(sURL), bLockFile ); + } + catch(const css::uno::Exception&) + { + DBG_UNHANDLED_EXCEPTION("unotools.misc", "invalid MediaDescriptor detected"); + return false; + } +} + +bool MediaDescriptor::impl_openStreamWithPostData( const css::uno::Reference< css::io::XInputStream >& _rxPostData ) +{ + if ( !_rxPostData.is() ) + throw css::lang::IllegalArgumentException("Found invalid PostData.", + css::uno::Reference< css::uno::XInterface >(), 1); + + // PostData can't be used in read/write mode! + (*this)[MediaDescriptor::PROP_READONLY()] <<= true; + + // prepare the environment + css::uno::Reference< css::task::XInteractionHandler > xInteraction = getUnpackedValueOrDefault( + MediaDescriptor::PROP_INTERACTIONHANDLER(), + css::uno::Reference< css::task::XInteractionHandler >()); + css::uno::Reference< css::ucb::XProgressHandler > xProgress; + ::ucbhelper::CommandEnvironment* pCommandEnv = new ::ucbhelper::CommandEnvironment(xInteraction, xProgress); + css::uno::Reference< css::ucb::XCommandEnvironment > xCommandEnv(static_cast< css::ucb::XCommandEnvironment* >(pCommandEnv), css::uno::UNO_QUERY); + + // media type + OUString sMediaType = getUnpackedValueOrDefault(MediaDescriptor::PROP_MEDIATYPE(), OUString()); + if (sMediaType.isEmpty()) + { + sMediaType = "application/x-www-form-urlencoded"; + (*this)[MediaDescriptor::PROP_MEDIATYPE()] <<= sMediaType; + } + + // url + OUString sURL( getUnpackedValueOrDefault( PROP_URL(), OUString() ) ); + + css::uno::Reference< css::io::XInputStream > xResultStream; + try + { + // seek PostData stream to the beginning + css::uno::Reference< css::io::XSeekable > xSeek( _rxPostData, css::uno::UNO_QUERY ); + if ( xSeek.is() ) + xSeek->seek( 0 ); + + // a content for the URL + ::ucbhelper::Content aContent( sURL, xCommandEnv, comphelper::getProcessComponentContext() ); + + // use post command + css::ucb::PostCommandArgument2 aPostArgument; + aPostArgument.Source = _rxPostData; + css::uno::Reference< css::io::XActiveDataSink > xSink( new ucbhelper::ActiveDataSink ); + aPostArgument.Sink = xSink; + aPostArgument.MediaType = sMediaType; + aPostArgument.Referer = getUnpackedValueOrDefault( PROP_REFERRER(), OUString() ); + + aContent.executeCommand( "post", css::uno::makeAny( aPostArgument ) ); + + // get result + xResultStream = xSink->getInputStream(); + } + catch( const css::uno::Exception& ) + { + } + + // success? + if ( !xResultStream.is() ) + { + OSL_FAIL( "no valid reply to the HTTP-Post" ); + return false; + } + + (*this)[MediaDescriptor::PROP_INPUTSTREAM()] <<= xResultStream; + return true; +} + +/*-----------------------------------------------*/ +bool MediaDescriptor::impl_openStreamWithURL( const OUString& sURL, bool bLockFile ) +{ + OUString referer(getUnpackedValueOrDefault(PROP_REFERRER(), OUString())); + if (SvtSecurityOptions().isUntrustedReferer(referer)) { + return false; + } + + // prepare the environment + css::uno::Reference< css::task::XInteractionHandler > xOrgInteraction = getUnpackedValueOrDefault( + MediaDescriptor::PROP_INTERACTIONHANDLER(), + css::uno::Reference< css::task::XInteractionHandler >()); + + css::uno::Reference< css::task::XInteractionHandler > xAuthenticationInteraction = getUnpackedValueOrDefault( + MediaDescriptor::PROP_AUTHENTICATIONHANDLER(), + css::uno::Reference< css::task::XInteractionHandler >()); + + comphelper::StillReadWriteInteraction* pInteraction = new comphelper::StillReadWriteInteraction(xOrgInteraction,xAuthenticationInteraction); + css::uno::Reference< css::task::XInteractionHandler > xInteraction(static_cast< css::task::XInteractionHandler* >(pInteraction), css::uno::UNO_QUERY); + + css::uno::Reference< css::ucb::XProgressHandler > xProgress; + ::ucbhelper::CommandEnvironment* pCommandEnv = new ::ucbhelper::CommandEnvironment(xInteraction, xProgress); + css::uno::Reference< css::ucb::XCommandEnvironment > xCommandEnv(static_cast< css::ucb::XCommandEnvironment* >(pCommandEnv), css::uno::UNO_QUERY); + + // try to create the content + // no content -> no stream => return immediately with FALSE + ::ucbhelper::Content aContent; + css::uno::Reference< css::ucb::XContent > xContent; + try + { + aContent = ::ucbhelper::Content(sURL, xCommandEnv, comphelper::getProcessComponentContext()); + xContent = aContent.get(); + } + catch(const css::uno::RuntimeException&) + { throw; } + catch(const css::ucb::ContentCreationException&) + { + TOOLS_WARN_EXCEPTION("unotools.misc", "url: '" << sURL << "'"); + return false; // TODO error handling + } + catch(const css::uno::Exception&) + { + TOOLS_WARN_EXCEPTION("unotools.misc", "url: '" << sURL << "'"); + return false; // TODO error handling + } + + // try to open the file in read/write mode + // (if it's allowed to do so). + // But handle errors in a "hidden mode". Because + // we try it readonly later - if read/write is not an option. + css::uno::Reference< css::io::XStream > xStream; + css::uno::Reference< css::io::XInputStream > xInputStream; + + bool bReadOnly = false; + bool bModeRequestedExplicitly = false; + const_iterator pIt = find(MediaDescriptor::PROP_READONLY()); + if (pIt != end()) + { + pIt->second >>= bReadOnly; + bModeRequestedExplicitly = true; + } + + if ( !bReadOnly && bLockFile ) + { + try + { + // TODO: use "special" still interaction to suppress error messages + xStream = aContent.openWriteableStream(); + if (xStream.is()) + xInputStream = xStream->getInputStream(); + } + catch(const css::uno::RuntimeException&) + { throw; } + catch(const css::uno::Exception&) + { + css::uno::Any ex( cppu::getCaughtException() ); + // ignore exception, if reason was problem reasoned on + // open it in WRITABLE mode! Then we try it READONLY + // later a second time. + // All other errors must be handled as real error an + // break this method. + if (!pInteraction->wasWriteError() || bModeRequestedExplicitly) + { + SAL_WARN("unotools.misc","url: '" << sURL << "' " << exceptionToString(ex)); + // If the protocol is webdav, then we need to treat the stream as readonly, even if the + // operation was requested as read/write explicitly (the WebDAV UCB implementation is monodirectional + // read or write not both at the same time). + if ( !INetURLObject( sURL ).isAnyKnownWebDAVScheme() ) + return false; + } + xStream.clear(); + xInputStream.clear(); + } + } + + // If opening of the stream in read/write mode was not allowed + // or failed by an error - we must try it in readonly mode. + if (!xInputStream.is()) + { + OUString aScheme; + + try + { + css::uno::Reference< css::ucb::XContentIdentifier > xContId( + aContent.get().is() ? aContent.get()->getIdentifier() : nullptr ); + + if ( xContId.is() ) + aScheme = xContId->getContentProviderScheme(); + + // Only file system content provider is able to provide XStream + // so for this content impossibility to create XStream triggers + // switch to readonly mode in case of opening with locking on + if( bLockFile && aScheme.equalsIgnoreAsciiCase("file") ) + bReadOnly = true; + else + { + bool bRequestReadOnly = bReadOnly; + aContent.getPropertyValue("IsReadOnly") >>= bReadOnly; + if ( bReadOnly && !bRequestReadOnly && bModeRequestedExplicitly ) + return false; // the document is explicitly requested with WRITABLE mode + } + } + catch(const css::uno::RuntimeException&) + { throw; } + catch(const css::uno::Exception&) + { /* no error handling if IsReadOnly property does not exist for UCP */ } + + if ( bReadOnly ) + (*this)[MediaDescriptor::PROP_READONLY()] <<= bReadOnly; + + pInteraction->resetInterceptions(); + pInteraction->resetErrorStates(); + try + { + // all the contents except file-URLs should be opened as usual + if ( bLockFile || !aScheme.equalsIgnoreAsciiCase("file") ) + xInputStream = aContent.openStream(); + else + xInputStream = aContent.openStreamNoLock(); + } + catch(const css::uno::RuntimeException&) + { + throw; + } + catch(const css::uno::Exception&) + { + TOOLS_INFO_EXCEPTION("unotools.misc","url: '" << sURL << "'"); + return false; + } + } + + // add streams to the descriptor + if (xContent.is()) + (*this)[MediaDescriptor::PROP_UCBCONTENT()] <<= xContent; + if (xStream.is()) + (*this)[MediaDescriptor::PROP_STREAM()] <<= xStream; + if (xInputStream.is()) + (*this)[MediaDescriptor::PROP_INPUTSTREAM()] <<= xInputStream; + + // At least we need an input stream. The r/w stream is optional ... + return xInputStream.is(); +} + +} // namespace comphelper + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/misc/sharedunocomponent.cxx b/unotools/source/misc/sharedunocomponent.cxx new file mode 100644 index 000000000..789054104 --- /dev/null +++ b/unotools/source/misc/sharedunocomponent.cxx @@ -0,0 +1,199 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> +#include <osl/diagnose.h> + +#include <unotools/sharedunocomponent.hxx> +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/util/CloseVetoException.hpp> +#include <com/sun/star/util/XCloseable.hpp> +#include <cppuhelper/implbase.hxx> +#include <tools/debug.hxx> +#include <tools/diagnose_ex.h> + +namespace utl +{ + + using ::com::sun::star::uno::XInterface; + using ::com::sun::star::uno::Reference; + using ::com::sun::star::uno::Exception; + using ::com::sun::star::uno::UNO_QUERY; + using ::com::sun::star::lang::EventObject; + using ::com::sun::star::util::XCloseable; + using ::com::sun::star::util::XCloseListener; + using ::com::sun::star::util::CloseVetoException; + + //= DisposableComponent + + DisposableComponent::DisposableComponent( const Reference< XInterface >& _rxComponent ) + :m_xComponent( _rxComponent, UNO_QUERY ) + { + DBG_ASSERT( m_xComponent.is() || !_rxComponent.is(), "DisposableComponent::DisposableComponent: should be an XComponent!" ); + } + + DisposableComponent::~DisposableComponent() + { + if ( m_xComponent.is() ) + { + try + { + m_xComponent->dispose(); + } + catch( const Exception& ) + { + TOOLS_WARN_EXCEPTION( "unotools", "DisposableComponent::~DisposableComponent" ); + } + m_xComponent.clear(); + } + } + + typedef ::cppu::WeakImplHelper < XCloseListener + > CloseableComponentImpl_Base; + class CloseableComponentImpl : public CloseableComponentImpl_Base + { + private: + Reference< XCloseable > m_xCloseable; + + CloseableComponentImpl(const CloseableComponentImpl&) = delete; + CloseableComponentImpl& operator=(const CloseableComponentImpl&) = delete; + + public: + explicit CloseableComponentImpl( const Reference< XInterface >& _rxComponent ); + + /** closes the component + + @nofail + */ + void nf_closeComponent(); + + protected: + virtual ~CloseableComponentImpl() override; + + // XCloseListener overridables + virtual void SAL_CALL queryClosing( const EventObject& Source, sal_Bool GetsOwnership ) override; + virtual void SAL_CALL notifyClosing( const EventObject& Source ) override; + + // XEventListener overridables + virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override; + + private: + /** starts or stops being a CloseListener at the component + + Only to be called upon construction of the instance, or when the component + is to be closed. + + @nofail + */ + void impl_nf_switchListening( bool _bListen ); + }; + + CloseableComponentImpl::CloseableComponentImpl( const Reference< XInterface >& _rxComponent ) + :m_xCloseable( _rxComponent, UNO_QUERY ) + { + DBG_ASSERT( m_xCloseable.is() || !_rxComponent.is(), "CloseableComponentImpl::CloseableComponentImpl: component is not an XCloseable!" ); + impl_nf_switchListening( true ); + } + + CloseableComponentImpl::~CloseableComponentImpl() + { + nf_closeComponent(); + } + + void CloseableComponentImpl::nf_closeComponent() + { + if ( !m_xCloseable.is() ) + // nothing to do + return; + + // stop listening + impl_nf_switchListening( false ); + + // close + try + { + m_xCloseable->close( true ); + } + catch( const CloseVetoException& ) { /* fine */ } + catch( const Exception& ) + { + OSL_FAIL( "CloseableComponentImpl::nf_closeComponent: caught an unexpected exception!" ); + } + + // reset + m_xCloseable.clear(); + } + + void CloseableComponentImpl::impl_nf_switchListening( bool _bListen ) + { + if ( !m_xCloseable.is() ) + return; + + try + { + if ( _bListen ) + m_xCloseable->addCloseListener( this ); + else + m_xCloseable->removeCloseListener( this ); + } + catch( const Exception& ) + { + TOOLS_WARN_EXCEPTION( "unotools", "CloseableComponentImpl::impl_nf_switchListening" ); + } + } + + void SAL_CALL CloseableComponentImpl::queryClosing( const EventObject& Source, sal_Bool /*GetsOwnership*/ ) + { + // as long as we live, somebody wants to keep the object alive. So, veto the + // closing + DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::queryClosing: where did this come from?" ); + throw CloseVetoException(); + } + + void SAL_CALL CloseableComponentImpl::notifyClosing( const EventObject& Source ) + { + DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::notifyClosing: where did this come from?" ); + + // this should be unreachable: As long as we're a CloseListener, we veto the closing. If we're going + // to close the component ourself, then we revoke ourself as listener *before* the close call. So, + // if this here fires, something went definitely wrong. + OSL_FAIL( "CloseableComponentImpl::notifyClosing: unreachable!" ); + } + + void SAL_CALL CloseableComponentImpl::disposing( const EventObject& Source ) + { + DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::disposing: where did this come from?" ); + OSL_FAIL( "CloseableComponentImpl::disposing: unreachable!" ); + // same reasoning for this assertion as in ->notifyClosing + } + + CloseableComponent::CloseableComponent( const Reference< XInterface >& _rxComponent ) + :m_pImpl( new CloseableComponentImpl( _rxComponent ) ) + { + } + + CloseableComponent::~CloseableComponent() + { + // close the component, deliver ownership to anybody who wants to veto the close + m_pImpl->nf_closeComponent(); + } + +} // namespace utl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/misc/syslocale.cxx b/unotools/source/misc/syslocale.cxx new file mode 100644 index 000000000..7f89d34f3 --- /dev/null +++ b/unotools/source/misc/syslocale.cxx @@ -0,0 +1,222 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <sal/log.hxx> +#include <unotools/localedatawrapper.hxx> +#include <unotools/charclass.hxx> +#include <unotools/syslocale.hxx> +#include <unotools/syslocaleoptions.hxx> +#include <comphelper/lok.hxx> +#include <comphelper/sequence.hxx> +#include <rtl/tencinfo.h> +#include <rtl/locale.h> +#include <osl/thread.h> +#include <osl/nlsupport.h> + +#include <vector> +#include <memory> + +using namespace osl; +using namespace com::sun::star; + +namespace { + +std::weak_ptr<SvtSysLocale_Impl> g_pSysLocale; + +} + +class SvtSysLocale_Impl : public utl::ConfigurationListener +{ +public: + SvtSysLocaleOptions aSysLocaleOptions; + std::unique_ptr<LocaleDataWrapper> pLocaleData; + std::unique_ptr<CharClass> pCharClass; + + SvtSysLocale_Impl(); + virtual ~SvtSysLocale_Impl() override; + + CharClass* GetCharClass(); + virtual void ConfigurationChanged( utl::ConfigurationBroadcaster*, ConfigurationHints ) override; + +private: + void setDateAcceptancePatternsConfig(); +}; + +SvtSysLocale_Impl::SvtSysLocale_Impl() +{ + pLocaleData.reset(new LocaleDataWrapper( aSysLocaleOptions.GetRealLanguageTag() )); + setDateAcceptancePatternsConfig(); + + // listen for further changes + aSysLocaleOptions.AddListener( this ); +} + +SvtSysLocale_Impl::~SvtSysLocale_Impl() +{ + aSysLocaleOptions.RemoveListener( this ); +} + +CharClass* SvtSysLocale_Impl::GetCharClass() +{ + if ( !pCharClass ) + pCharClass.reset(new CharClass( aSysLocaleOptions.GetRealLanguageTag() )); + return pCharClass.get(); +} + +void SvtSysLocale_Impl::ConfigurationChanged( utl::ConfigurationBroadcaster*, ConfigurationHints nHint ) +{ + MutexGuard aGuard( SvtSysLocale::GetMutex() ); + + if ( nHint & ConfigurationHints::Locale ) + { + const LanguageTag& rLanguageTag = aSysLocaleOptions.GetRealLanguageTag(); + pLocaleData->setLanguageTag( rLanguageTag ); + GetCharClass()->setLanguageTag( rLanguageTag ); + } + if ( nHint & ConfigurationHints::DatePatterns ) + { + setDateAcceptancePatternsConfig(); + } +} + +void SvtSysLocale_Impl::setDateAcceptancePatternsConfig() +{ + OUString aStr( aSysLocaleOptions.GetDatePatternsConfigString()); + if (aStr.isEmpty()) + pLocaleData->setDateAcceptancePatterns( uno::Sequence<OUString>()); // reset + else + { + ::std::vector< OUString > aVec; + for (sal_Int32 nIndex = 0; nIndex >= 0; /*nop*/) + { + OUString aTok( aStr.getToken( 0, ';', nIndex)); + if (!aTok.isEmpty()) + aVec.push_back( aTok); + } + pLocaleData->setDateAcceptancePatterns( comphelper::containerToSequence(aVec) ); + } +} + +SvtSysLocale::SvtSysLocale() +{ + MutexGuard aGuard( GetMutex() ); + pImpl = g_pSysLocale.lock(); + if ( !pImpl ) + { + pImpl = std::make_shared<SvtSysLocale_Impl>(); + g_pSysLocale = pImpl; + } +} + +SvtSysLocale::~SvtSysLocale() +{ + MutexGuard aGuard( GetMutex() ); + pImpl.reset(); +} + +// static +Mutex& SvtSysLocale::GetMutex() +{ + // #i77768# Due to a static reference in the toolkit lib + // we need a mutex that lives longer than the svl library. + // Otherwise the dtor would use a destructed mutex!! + static Mutex* persistentMutex(new Mutex); + + return *persistentMutex; +} + +const LocaleDataWrapper& SvtSysLocale::GetLocaleData() const +{ + return *(pImpl->pLocaleData); +} + +const LocaleDataWrapper* SvtSysLocale::GetLocaleDataPtr() const +{ + return pImpl->pLocaleData.get(); +} + +const CharClass& SvtSysLocale::GetCharClass() const +{ + return *(pImpl->GetCharClass()); +} + +const CharClass* SvtSysLocale::GetCharClassPtr() const +{ + return pImpl->GetCharClass(); +} + +SvtSysLocaleOptions& SvtSysLocale::GetOptions() const +{ + return pImpl->aSysLocaleOptions; +} + +const LanguageTag& SvtSysLocale::GetLanguageTag() const +{ + if (comphelper::LibreOfficeKit::isActive()) + return comphelper::LibreOfficeKit::getLocale(); + + return pImpl->aSysLocaleOptions.GetRealLanguageTag(); +} + +const LanguageTag& SvtSysLocale::GetUILanguageTag() const +{ + if (comphelper::LibreOfficeKit::isActive()) + return comphelper::LibreOfficeKit::getLanguageTag(); + + return pImpl->aSysLocaleOptions.GetRealUILanguageTag(); +} + +// static +rtl_TextEncoding SvtSysLocale::GetBestMimeEncoding() +{ + const char* pCharSet = rtl_getBestMimeCharsetFromTextEncoding( + osl_getThreadTextEncoding() ); + if ( !pCharSet ) + { + // If the system locale is unknown to us, e.g. LC_ALL=xx, match the UI + // language if possible. + SvtSysLocale aSysLocale; + const LanguageTag& rLanguageTag = aSysLocale.GetUILanguageTag(); + // Converting blindly to Locale and then to rtl_Locale may feed the + // 'qlt' to rtl_locale_register() and the underlying system locale + // stuff, which doesn't know about it nor about BCP47 in the Variant + // field. So use the real language and for non-pure ISO cases hope for + // the best... the fallback to UTF-8 should solve these cases nowadays. + /* FIXME-BCP47: the script needs to go in here as well, so actually + * we'd need some variant fiddling or glibc locale string and tweak + * rtl_locale_register() to know about it! But then again the Windows + * implementation still wouldn't know anything about it ... */ + SAL_WARN_IF( !rLanguageTag.isIsoLocale(), "unotools.i18n", + "SvtSysLocale::GetBestMimeEncoding - non-ISO UI locale"); + rtl_Locale * pLocale = rtl_locale_register( rLanguageTag.getLanguage().getStr(), + rLanguageTag.getCountry().getStr(), OUString().getStr() ); + rtl_TextEncoding nEnc = osl_getTextEncodingFromLocale( pLocale ); + pCharSet = rtl_getBestMimeCharsetFromTextEncoding( nEnc ); + } + rtl_TextEncoding nRet; + if ( pCharSet ) + nRet = rtl_getTextEncodingFromMimeCharset( pCharSet ); + else + nRet = RTL_TEXTENCODING_UTF8; + return nRet; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/misc/unotoolsservices.cxx b/unotools/source/misc/unotoolsservices.cxx new file mode 100644 index 000000000..4d1d897f1 --- /dev/null +++ b/unotools/source/misc/unotoolsservices.cxx @@ -0,0 +1,23 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + */ + +#include <comphelper/servicedecl.hxx> + +#include <unotoolsservices.hxx> + +namespace sdecl = ::comphelper::service_decl; + +extern "C" SAL_DLLPUBLIC_EXPORT void* utl_component_getFactory( + char const* pImplName, void*, void*) +{ + return sdecl::component_getFactoryHelper( pImplName, + {&OTempFileServiceDecl, &ServiceDocumenterDecl}); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/misc/wincodepage.cxx b/unotools/source/misc/wincodepage.cxx new file mode 100644 index 000000000..1fefabecb --- /dev/null +++ b/unotools/source/misc/wincodepage.cxx @@ -0,0 +1,138 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + */ + +#include <unotools/wincodepage.hxx> +#include <rtl/string.h> +#include <rtl/textenc.h> + +namespace{ + +struct LangEncodingDef +{ + const OUStringLiteral msLangStr; + rtl_TextEncoding meTextEncoding; +}; + +// See https://msdn.microsoft.com/en-us/library/windows/desktop/dd317756 +rtl_TextEncoding impl_getWinTextEncodingFromLangStrANSI(const OUString& sLanguage) +{ + static constexpr LangEncodingDef aLanguageTab[] = + { + { "en", RTL_TEXTENCODING_MS_1252 }, // Most used -> first in list + { "th", RTL_TEXTENCODING_MS_874 }, + { "ja", RTL_TEXTENCODING_MS_932 }, + { "zh-cn", RTL_TEXTENCODING_MS_936 }, // Chinese (simplified) - must go before "zh" + { "ko", RTL_TEXTENCODING_MS_949 }, + { "zh", RTL_TEXTENCODING_MS_950 }, // Chinese (traditional) + { "bs", RTL_TEXTENCODING_MS_1250 }, + { "cs", RTL_TEXTENCODING_MS_1250 }, + { "hr", RTL_TEXTENCODING_MS_1250 }, + { "hu", RTL_TEXTENCODING_MS_1250 }, + { "pl", RTL_TEXTENCODING_MS_1250 }, + { "ro", RTL_TEXTENCODING_MS_1250 }, + { "sk", RTL_TEXTENCODING_MS_1250 }, + { "sl", RTL_TEXTENCODING_MS_1250 }, +// { "sr", RTL_TEXTENCODING_MS_1250 }, + { "sq", RTL_TEXTENCODING_MS_1250 }, + { "be", RTL_TEXTENCODING_MS_1251 }, + { "bg", RTL_TEXTENCODING_MS_1251 }, + { "mk", RTL_TEXTENCODING_MS_1251 }, + { "ru", RTL_TEXTENCODING_MS_1251 }, + { "sr", RTL_TEXTENCODING_MS_1251 }, + { "uk", RTL_TEXTENCODING_MS_1251 }, + { "es", RTL_TEXTENCODING_MS_1252 }, + { "el", RTL_TEXTENCODING_MS_1253 }, + { "tr", RTL_TEXTENCODING_MS_1254 }, + { "he", RTL_TEXTENCODING_MS_1255 }, + { "ar", RTL_TEXTENCODING_MS_1256 }, + { "et", RTL_TEXTENCODING_MS_1257 }, + { "lt", RTL_TEXTENCODING_MS_1257 }, + { "lv", RTL_TEXTENCODING_MS_1257 }, + { "vi", RTL_TEXTENCODING_MS_1258 }, + }; + + for (auto& def : aLanguageTab) + { + if (sLanguage.startsWithIgnoreAsciiCase(def.msLangStr)) + return def.meTextEncoding; + } + + return RTL_TEXTENCODING_MS_1252; +} + +/* ----------------------------------------------------------------------- */ + +// See https://msdn.microsoft.com/en-us/library/windows/desktop/dd317756 +// See http://shapelib.maptools.org/codepage.html +rtl_TextEncoding impl_getWinTextEncodingFromLangStrOEM(const OUString& sLanguage) +{ + static constexpr LangEncodingDef aLanguageTab[] = + { + { "de", RTL_TEXTENCODING_IBM_437 }, // OEM United States + { "en-us", RTL_TEXTENCODING_IBM_437 }, // OEM United States + { "fi", RTL_TEXTENCODING_IBM_437 }, // OEM United States + { "fr-ca", RTL_TEXTENCODING_IBM_863 }, // OEM French Canadian; French Canadian (DOS) + { "fr", RTL_TEXTENCODING_IBM_437 }, // OEM United States + { "it", RTL_TEXTENCODING_IBM_437 }, // OEM United States + { "nl", RTL_TEXTENCODING_IBM_437 }, // OEM United States + { "sv", RTL_TEXTENCODING_IBM_437 }, // OEM United States + { "el", RTL_TEXTENCODING_IBM_737 }, // OEM Greek (formerly 437G); Greek (DOS) + { "et", RTL_TEXTENCODING_IBM_775 }, // OEM Baltic; Baltic (DOS) + { "lt", RTL_TEXTENCODING_IBM_775 }, // OEM Baltic; Baltic (DOS) + { "lv", RTL_TEXTENCODING_IBM_775 }, // OEM Baltic; Baltic (DOS) + { "en", RTL_TEXTENCODING_IBM_850 }, // OEM Multilingual Latin 1; Western European (DOS) + { "bs", RTL_TEXTENCODING_IBM_852 }, // OEM Latin 2; Central European (DOS) + { "cs", RTL_TEXTENCODING_IBM_852 }, // OEM Latin 2; Central European (DOS) + { "hr", RTL_TEXTENCODING_IBM_852 }, // OEM Latin 2; Central European (DOS) + { "hu", RTL_TEXTENCODING_IBM_852 }, // OEM Latin 2; Central European (DOS) + { "pl", RTL_TEXTENCODING_IBM_852 }, // OEM Latin 2; Central European (DOS) + { "ro", RTL_TEXTENCODING_IBM_852 }, // OEM Latin 2; Central European (DOS) + { "sk", RTL_TEXTENCODING_IBM_852 }, // OEM Latin 2; Central European (DOS) + { "sl", RTL_TEXTENCODING_IBM_852 }, // OEM Latin 2; Central European (DOS) +// { "sr", RTL_TEXTENCODING_IBM_852 }, // OEM Latin 2; Central European (DOS) + { "bg", RTL_TEXTENCODING_IBM_855 }, // OEM Cyrillic (primarily Russian) + { "mk", RTL_TEXTENCODING_IBM_855 }, // OEM Cyrillic (primarily Russian) + { "sr", RTL_TEXTENCODING_IBM_855 }, // OEM Cyrillic (primarily Russian) + { "tr", RTL_TEXTENCODING_IBM_857 }, // OEM Turkish; Turkish (DOS) + { "pt", RTL_TEXTENCODING_IBM_860 }, // OEM Portuguese; Portuguese (DOS) + { "is", RTL_TEXTENCODING_IBM_861 }, // OEM Icelandic; Icelandic (DOS) + { "he", RTL_TEXTENCODING_IBM_862 }, // OEM Hebrew; Hebrew (DOS) + { "ar", RTL_TEXTENCODING_IBM_864 }, // OEM Arabic; Arabic (864) + { "da", RTL_TEXTENCODING_IBM_865 }, // OEM Nordic; Nordic (DOS) + { "nn", RTL_TEXTENCODING_IBM_865 }, // OEM Nordic; Nordic (DOS) + { "be", RTL_TEXTENCODING_IBM_866 }, // OEM Russian; Cyrillic (DOS) + { "ru", RTL_TEXTENCODING_IBM_866 }, // OEM Russian; Cyrillic (DOS) + { "uk", RTL_TEXTENCODING_IBM_866 }, // OEM Russian; Cyrillic (DOS) + { "th", RTL_TEXTENCODING_MS_874 }, // ANSI/OEM Thai (ISO 8859-11); Thai (Windows) + { "ja", RTL_TEXTENCODING_MS_932 }, // ANSI/OEM Japanese; Japanese (Shift-JIS) + { "zh-cn", RTL_TEXTENCODING_MS_936 }, // ANSI/OEM Simplified Chinese (PRC, Singapore); Chinese Simplified (GB2312) + { "ko", RTL_TEXTENCODING_MS_949 }, // ANSI/OEM Korean (Unified Hangul Code) + { "zh", RTL_TEXTENCODING_MS_950 }, // ANSI/OEM Traditional Chinese (Taiwan; Hong Kong SAR, PRC); Chinese Traditional (Big5) + { "vi", RTL_TEXTENCODING_MS_1258 }, // ANSI/OEM Vietnamese; Vietnamese (Windows) + }; + + for (auto& def : aLanguageTab) + { + if (sLanguage.startsWithIgnoreAsciiCase(def.msLangStr)) + return def.meTextEncoding; + } + + return RTL_TEXTENCODING_IBM_850; +} + +} // namespace + +rtl_TextEncoding utl_getWinTextEncodingFromLangStr(const OUString& sLanguage, bool bOEM) +{ + return bOEM ? + impl_getWinTextEncodingFromLangStrOEM(sLanguage) : + impl_getWinTextEncodingFromLangStrANSI(sLanguage); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/streaming/streamhelper.cxx b/unotools/source/streaming/streamhelper.cxx new file mode 100644 index 000000000..cf5930d43 --- /dev/null +++ b/unotools/source/streaming/streamhelper.cxx @@ -0,0 +1,118 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <com/sun/star/io/BufferSizeExceededException.hpp> +#include <com/sun/star/io/IOException.hpp> +#include <com/sun/star/io/NotConnectedException.hpp> +#include <o3tl/safeint.hxx> +#include <unotools/streamhelper.hxx> + +namespace utl +{ + +sal_Int32 SAL_CALL OInputStreamHelper::readBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead) +{ + if (!m_xLockBytes.is()) + throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this)); + + if (nBytesToRead < 0) + throw css::io::BufferSizeExceededException(OUString(), static_cast<css::uno::XWeak*>(this)); + + ::osl::MutexGuard aGuard( m_aMutex ); + if (aData.getLength() < nBytesToRead) + aData.realloc(nBytesToRead); + + std::size_t nRead(0); + ErrCode nError = m_xLockBytes->ReadAt(m_nActPos, static_cast<void*>(aData.getArray()), nBytesToRead, &nRead); + m_nActPos += nRead; + + if (nError != ERRCODE_NONE) + throw css::io::IOException(OUString(), static_cast<css::uno::XWeak*>(this)); + + // adjust sequence if data read is lower than the desired data + if (nRead < o3tl::make_unsigned(aData.getLength())) + aData.realloc( nRead ); + + return nRead; +} + +void SAL_CALL OInputStreamHelper::seek( sal_Int64 location ) +{ + ::osl::MutexGuard aGuard( m_aMutex ); + m_nActPos = location; +} + +sal_Int64 SAL_CALL OInputStreamHelper::getPosition( ) +{ + return m_nActPos; +} + +sal_Int64 SAL_CALL OInputStreamHelper::getLength( ) +{ + if (!m_xLockBytes.is()) + return 0; + + ::osl::MutexGuard aGuard( m_aMutex ); + SvLockBytesStat aStat; + m_xLockBytes->Stat( &aStat ); + return aStat.nSize; +} + +sal_Int32 SAL_CALL OInputStreamHelper::readSomeBytes(css::uno::Sequence< sal_Int8 >& aData, + sal_Int32 nMaxBytesToRead) +{ + // read all data desired + return readBytes(aData, nMaxBytesToRead); +} + +void SAL_CALL OInputStreamHelper::skipBytes(sal_Int32 nBytesToSkip) +{ + ::osl::MutexGuard aGuard( m_aMutex ); + if (!m_xLockBytes.is()) + throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this)); + + if (nBytesToSkip < 0) + throw css::io::BufferSizeExceededException(OUString(), static_cast<css::uno::XWeak*>(this)); + + m_nActPos += nBytesToSkip; +} + +sal_Int32 SAL_CALL OInputStreamHelper::available() +{ + ::osl::MutexGuard aGuard( m_aMutex ); + if (!m_xLockBytes.is()) + throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this)); + + return m_nAvailable; +} + +void SAL_CALL OInputStreamHelper::closeInput() +{ + ::osl::MutexGuard aGuard( m_aMutex ); + if (!m_xLockBytes.is()) + throw css::io::NotConnectedException(OUString(), static_cast<css::uno::XWeak*>(this)); + + m_xLockBytes = nullptr; +} + +} // namespace utl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/streaming/streamwrap.cxx b/unotools/source/streaming/streamwrap.cxx new file mode 100644 index 000000000..70054d0f5 --- /dev/null +++ b/unotools/source/streaming/streamwrap.cxx @@ -0,0 +1,317 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <com/sun/star/io/BufferSizeExceededException.hpp> +#include <com/sun/star/io/NotConnectedException.hpp> +#include <o3tl/safeint.hxx> +#include <unotools/streamwrap.hxx> +#include <tools/stream.hxx> + +namespace utl +{ + +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::io; +using namespace ::com::sun::star::lang; + +OInputStreamWrapper::OInputStreamWrapper( SvStream& _rStream ) + :m_pSvStream(&_rStream) + ,m_bSvStreamOwner(false) +{ +} + +OInputStreamWrapper::OInputStreamWrapper( SvStream* pStream, bool bOwner ) + :m_pSvStream( pStream ) + ,m_bSvStreamOwner( bOwner ) +{ +} + +OInputStreamWrapper::OInputStreamWrapper( std::unique_ptr<SvStream> pStream ) + :m_pSvStream( pStream.release() ) + ,m_bSvStreamOwner( true ) +{ +} + +OInputStreamWrapper::~OInputStreamWrapper() +{ + if( m_bSvStreamOwner ) + delete m_pSvStream; +} + +sal_Int32 SAL_CALL OInputStreamWrapper::readBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead) +{ + checkConnected(); + + if (nBytesToRead < 0) + throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this)); + + ::osl::MutexGuard aGuard( m_aMutex ); + + if (aData.getLength() < nBytesToRead) + aData.realloc(nBytesToRead); + + sal_uInt32 nRead = m_pSvStream->ReadBytes(static_cast<void*>(aData.getArray()), nBytesToRead); + checkError(); + + // If read characters < MaxLength, adjust css::uno::Sequence + if (nRead < o3tl::make_unsigned(aData.getLength())) + aData.realloc( nRead ); + + return nRead; +} + +sal_Int32 SAL_CALL OInputStreamWrapper::readSomeBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) +{ + checkError(); + + if (nMaxBytesToRead < 0) + throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this)); + + if (m_pSvStream->eof()) + { + aData.realloc(0); + return 0; + } + else + return readBytes(aData, nMaxBytesToRead); +} + +void SAL_CALL OInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip) +{ + ::osl::MutexGuard aGuard( m_aMutex ); + checkError(); + + m_pSvStream->SeekRel(nBytesToSkip); + checkError(); +} + +sal_Int32 SAL_CALL OInputStreamWrapper::available() +{ + ::osl::MutexGuard aGuard( m_aMutex ); + checkConnected(); + + sal_Int64 nAvailable = m_pSvStream->remainingSize(); + checkError(); + + return std::min<sal_Int64>(SAL_MAX_INT32, nAvailable); +} + +void SAL_CALL OInputStreamWrapper::closeInput() +{ + ::osl::MutexGuard aGuard( m_aMutex ); + checkConnected(); + + if (m_bSvStreamOwner) + delete m_pSvStream; + + m_pSvStream = nullptr; +} + +void OInputStreamWrapper::checkConnected() const +{ + if (!m_pSvStream) + throw css::io::NotConnectedException(OUString(), const_cast<css::uno::XWeak*>(static_cast<const css::uno::XWeak*>(this))); +} + +void OInputStreamWrapper::checkError() const +{ + checkConnected(); + + auto const e = m_pSvStream->SvStream::GetError(); + if (e != ERRCODE_NONE) + // TODO: really evaluate the error + throw css::io::NotConnectedException("utl::OInputStreamWrapper error " + e.toHexString(), const_cast<css::uno::XWeak*>(static_cast<const css::uno::XWeak*>(this))); +} + +//= OSeekableInputStreamWrapper + +OSeekableInputStreamWrapper::~OSeekableInputStreamWrapper() = default; + +OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream& _rStream) +{ + SetStream( &_rStream, false ); +} + +OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream* _pStream, bool _bOwner) +{ + SetStream( _pStream, _bOwner ); +} + +void SAL_CALL OSeekableInputStreamWrapper::seek( sal_Int64 _nLocation ) +{ + ::osl::MutexGuard aGuard( m_aMutex ); + checkConnected(); + + m_pSvStream->Seek(static_cast<sal_uInt32>(_nLocation)); + checkError(); +} + +sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getPosition( ) +{ + ::osl::MutexGuard aGuard( m_aMutex ); + checkConnected(); + + sal_uInt32 nPos = m_pSvStream->Tell(); + checkError(); + return static_cast<sal_Int64>(nPos); +} + +sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getLength( ) +{ + ::osl::MutexGuard aGuard( m_aMutex ); + checkConnected(); + + checkError(); + + sal_Int64 nEndPos = m_pSvStream->TellEnd(); + + return nEndPos; +} + +//= OOutputStreamWrapper + +OOutputStreamWrapper::OOutputStreamWrapper(SvStream& _rStream): + rStream(_rStream) +{} + +OOutputStreamWrapper::~OOutputStreamWrapper() {} + +void SAL_CALL OOutputStreamWrapper::writeBytes(const css::uno::Sequence< sal_Int8 >& aData) +{ + sal_uInt32 nWritten = rStream.WriteBytes(aData.getConstArray(), aData.getLength()); + ErrCode err = rStream.GetError(); + if ( (ERRCODE_NONE != err) + || (nWritten != static_cast<sal_uInt32>(aData.getLength())) + ) + { + throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this)); + } +} + +void SAL_CALL OOutputStreamWrapper::flush() +{ + rStream.Flush(); + checkError(); +} + +void SAL_CALL OOutputStreamWrapper::closeOutput() +{ +} + +void OOutputStreamWrapper::checkError() const +{ + if (rStream.GetError() != ERRCODE_NONE) + // TODO: really evaluate the error + throw css::io::NotConnectedException(OUString(), const_cast<css::uno::XWeak*>(static_cast<const css::uno::XWeak*>(this))); +} + +//= OSeekableOutputStreamWrapper + +OSeekableOutputStreamWrapper::OSeekableOutputStreamWrapper(SvStream& _rStream) + :OOutputStreamWrapper(_rStream) +{ +} + +OSeekableOutputStreamWrapper::~OSeekableOutputStreamWrapper() {} + +Any SAL_CALL OSeekableOutputStreamWrapper::queryInterface( const Type& _rType ) +{ + Any aReturn = OOutputStreamWrapper::queryInterface(_rType); + if (!aReturn.hasValue()) + aReturn = OSeekableOutputStreamWrapper_Base::queryInterface(_rType); + return aReturn; +} + +void SAL_CALL OSeekableOutputStreamWrapper::seek( sal_Int64 _nLocation ) +{ + rStream.Seek(static_cast<sal_uInt32>(_nLocation)); + checkError(); +} + +sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getPosition( ) +{ + sal_uInt32 nPos = rStream.Tell(); + checkError(); + return static_cast<sal_Int64>(nPos); +} + +sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getLength( ) +{ + checkError(); + + sal_Int64 nEndPos = rStream.TellEnd(); + + return nEndPos; +} + +OStreamWrapper::~OStreamWrapper() = default; + +OStreamWrapper::OStreamWrapper(SvStream& _rStream) +{ + SetStream( &_rStream, false ); +} + +OStreamWrapper::OStreamWrapper(std::unique_ptr<SvStream> pStream) +{ + SetStream( pStream.release(), true ); +} + +css::uno::Reference< css::io::XInputStream > SAL_CALL OStreamWrapper::getInputStream( ) +{ + return this; +} + +css::uno::Reference< css::io::XOutputStream > SAL_CALL OStreamWrapper::getOutputStream( ) +{ + return this; +} + +void SAL_CALL OStreamWrapper::writeBytes(const css::uno::Sequence< sal_Int8 >& aData) +{ + sal_uInt32 nWritten = m_pSvStream->WriteBytes(aData.getConstArray(), aData.getLength()); + ErrCode err = m_pSvStream->GetError(); + if ( (ERRCODE_NONE != err) + || (nWritten != static_cast<sal_uInt32>(aData.getLength())) + ) + { + throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this)); + } +} + +void SAL_CALL OStreamWrapper::flush() +{ + m_pSvStream->Flush(); + if (m_pSvStream->GetError() != ERRCODE_NONE) + throw css::io::NotConnectedException(OUString(),static_cast<css::uno::XWeak*>(this)); +} + +void SAL_CALL OStreamWrapper::closeOutput() +{ +} + +void SAL_CALL OStreamWrapper::truncate() +{ + m_pSvStream->SetStreamSize(0); +} + +} // namespace utl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/ucbhelper/XTempFile.hxx b/unotools/source/ucbhelper/XTempFile.hxx new file mode 100644 index 000000000..f96bbb8f5 --- /dev/null +++ b/unotools/source/ucbhelper/XTempFile.hxx @@ -0,0 +1,116 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#ifndef INCLUDED_UNOTOOLS_SOURCE_UCBHELPER_XTEMPFILE_HXX +#define INCLUDED_UNOTOOLS_SOURCE_UCBHELPER_XTEMPFILE_HXX + +#include <memory> +#include <com/sun/star/io/XInputStream.hpp> +#include <com/sun/star/io/XOutputStream.hpp> +#include <com/sun/star/io/XTempFile.hpp> +#include <com/sun/star/io/XTruncate.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/beans/XPropertyAccess.hpp> +#include <com/sun/star/beans/XFastPropertySet.hpp> +#include <cppuhelper/implbase.hxx> +#include <osl/mutex.hxx> + +namespace com::sun::star::uno { class XComponentContext; } + +class SvStream; +namespace utl { class TempFile; } + + +typedef ::cppu::WeakImplHelper< css::io::XTempFile + , css::io::XInputStream + , css::io::XOutputStream + , css::io::XTruncate + , css::beans::XPropertySet + , css::beans::XFastPropertySet + , css::beans::XPropertyAccess > OTempFileBase; + +class OTempFileService : public OTempFileBase +{ +protected: + std::unique_ptr<utl::TempFile> mpTempFile; + ::osl::Mutex maMutex; + SvStream* mpStream; + bool mbRemoveFile; + bool mbInClosed; + bool mbOutClosed; + + sal_Int64 mnCachedPos; + bool mbHasCachedPos; + + void checkError () const; + void checkConnected (); + +public: + explicit OTempFileService (css::uno::Reference< css::uno::XComponentContext > const & context); + + //Methods + // XTypeProvider + virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes( ) override; + + // XTempFile + virtual sal_Bool SAL_CALL getRemoveFile() override; + virtual void SAL_CALL setRemoveFile( sal_Bool _removefile ) override; + virtual OUString SAL_CALL getUri() override; + virtual OUString SAL_CALL getResourceName() override; + + // XInputStream + virtual ::sal_Int32 SAL_CALL readBytes( css::uno::Sequence< ::sal_Int8 >& aData, ::sal_Int32 nBytesToRead ) override; + virtual ::sal_Int32 SAL_CALL readSomeBytes( css::uno::Sequence< ::sal_Int8 >& aData, ::sal_Int32 nMaxBytesToRead ) override; + virtual void SAL_CALL skipBytes( ::sal_Int32 nBytesToSkip ) override; + virtual ::sal_Int32 SAL_CALL available( ) override; + virtual void SAL_CALL closeInput( ) override; + // XOutputStream + virtual void SAL_CALL writeBytes( const css::uno::Sequence< ::sal_Int8 >& aData ) override; + virtual void SAL_CALL flush( ) override; + virtual void SAL_CALL closeOutput( ) override; + // XSeekable + virtual void SAL_CALL seek( sal_Int64 location ) override; + virtual sal_Int64 SAL_CALL getPosition( ) override; + virtual sal_Int64 SAL_CALL getLength( ) override; + // XStream + virtual css::uno::Reference< css::io::XInputStream > SAL_CALL getInputStream( ) override; + virtual css::uno::Reference< css::io::XOutputStream > SAL_CALL getOutputStream( ) override; + // XTruncate + virtual void SAL_CALL truncate() override; + + // XPropertySet + virtual ::css::uno::Reference< ::css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override; + virtual void SAL_CALL setPropertyValue( const ::rtl::OUString& aPropertyName, const ::css::uno::Any& aValue ) override; + virtual ::css::uno::Any SAL_CALL getPropertyValue( const ::rtl::OUString& PropertyName ) override; + virtual void SAL_CALL addPropertyChangeListener( const ::rtl::OUString& aPropertyName, const ::css::uno::Reference< ::css::beans::XPropertyChangeListener >& xListener ) override; + virtual void SAL_CALL removePropertyChangeListener( const ::rtl::OUString& aPropertyName, const ::css::uno::Reference< ::css::beans::XPropertyChangeListener >& aListener ) override; + virtual void SAL_CALL addVetoableChangeListener( const ::rtl::OUString& PropertyName, const ::css::uno::Reference< ::css::beans::XVetoableChangeListener >& aListener ) override; + virtual void SAL_CALL removeVetoableChangeListener( const ::rtl::OUString& PropertyName, const ::css::uno::Reference< ::css::beans::XVetoableChangeListener >& aListener ) override; + // XFastPropertySet + virtual void SAL_CALL setFastPropertyValue( ::sal_Int32 nHandle, const ::css::uno::Any& aValue ) override; + virtual ::css::uno::Any SAL_CALL getFastPropertyValue( ::sal_Int32 nHandle ) override; + // XPropertyAccess + virtual ::css::uno::Sequence< ::css::beans::PropertyValue > SAL_CALL getPropertyValues() override; + virtual void SAL_CALL setPropertyValues( const ::css::uno::Sequence< ::css::beans::PropertyValue >& aProps ) override; + + + virtual ~OTempFileService () override; +}; +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/ucbhelper/localfilehelper.cxx b/unotools/source/ucbhelper/localfilehelper.cxx new file mode 100644 index 000000000..19de32703 --- /dev/null +++ b/unotools/source/ucbhelper/localfilehelper.cxx @@ -0,0 +1,138 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <com/sun/star/sdbc/XResultSet.hpp> +#include <com/sun/star/ucb/XContentAccess.hpp> +#include <com/sun/star/ucb/CommandAbortedException.hpp> +#include <comphelper/processfactory.hxx> +#include <comphelper/sequence.hxx> +#include <sal/log.hxx> +#include <unotools/localfilehelper.hxx> +#include <rtl/ustring.hxx> +#include <osl/file.hxx> +#include <ucbhelper/content.hxx> +#include <vector> + +using namespace ::osl; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::ucb; + +namespace utl +{ + +css::uno::Sequence < OUString > LocalFileHelper::GetFolderContents( const OUString& rFolder, bool bFolder ) +{ + std::vector< OUString > vFiles; + try + { + ::ucbhelper::Content aCnt( + rFolder, Reference< XCommandEnvironment >(), + comphelper::getProcessComponentContext() ); + Reference< css::sdbc::XResultSet > xResultSet; + css::uno::Sequence< OUString > aProps { "Url" }; + + try + { + ::ucbhelper::ResultSetInclude eInclude = bFolder ? ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS : ::ucbhelper::INCLUDE_DOCUMENTS_ONLY; + xResultSet = aCnt.createCursor( aProps, eInclude ); + } + catch( css::ucb::CommandAbortedException& ) + { + } + catch( Exception& ) + { + } + + if ( xResultSet.is() ) + { + Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY ); + try + { + while ( xResultSet->next() ) + vFiles.push_back( xContentAccess->queryContentIdentifierString() ); + } + catch( css::ucb::CommandAbortedException& ) + { + } + catch( Exception& ) + { + } + } + } + catch( Exception& ) + { + } + + return comphelper::containerToSequence(vFiles); +} + +void removeTree(OUString const & url) { + osl::Directory dir(url); + osl::FileBase::RC rc = dir.open(); + switch (rc) { + case osl::FileBase::E_None: + break; + case osl::FileBase::E_NOENT: + return; //TODO: SAL_WARN if recursive + default: + SAL_WARN("desktop.app", "cannot open directory " << dir.getURL() << ": " << +rc); + return; + } + for (;;) { + osl::DirectoryItem i; + rc = dir.getNextItem(i, SAL_MAX_UINT32); + if (rc == osl::FileBase::E_NOENT) { + break; + } + if (rc != osl::FileBase::E_None) { + SAL_WARN( "desktop.app", "cannot iterate directory " << dir.getURL() << ": " << +rc); + break; + } + osl::FileStatus stat( + osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileName | + osl_FileStatus_Mask_FileURL); + rc = i.getFileStatus(stat); + if (rc != osl::FileBase::E_None) { + SAL_WARN( "desktop.app", "cannot stat in directory " << dir.getURL() << ": " << +rc); + continue; + } + if (stat.getFileType() == osl::FileStatus::Directory) { //TODO: symlinks + removeTree(stat.getFileURL()); + } else { + rc = osl::File::remove(stat.getFileURL()); + SAL_WARN_IF( + rc != osl::FileBase::E_None, "desktop.app", + "cannot remove file " << stat.getFileURL() << ": " << +rc); + } + } + if (dir.isOpen()) { + rc = dir.close(); + SAL_WARN_IF( + rc != osl::FileBase::E_None, "desktop.app", + "cannot close directory " << dir.getURL() << ": " << +rc); + } + rc = osl::Directory::remove(url); + SAL_WARN_IF( + rc != osl::FileBase::E_None, "desktop.app", + "cannot remove directory " << url << ": " << +rc); +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/ucbhelper/progresshandlerwrap.cxx b/unotools/source/ucbhelper/progresshandlerwrap.cxx new file mode 100644 index 000000000..093463383 --- /dev/null +++ b/unotools/source/ucbhelper/progresshandlerwrap.cxx @@ -0,0 +1,87 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <unotools/progresshandlerwrap.hxx> +#include <com/sun/star/task/XStatusIndicator.hpp> + +namespace utl +{ + +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::task; +using namespace ::com::sun::star::ucb; + +ProgressHandlerWrap::ProgressHandlerWrap( css::uno::Reference< css::task::XStatusIndicator > const & xSI ) +: m_xStatusIndicator( xSI ) +{ +} + +static bool getStatusFromAny_Impl( const Any& aAny, OUString& aText, sal_Int32& nNum ) +{ + bool bNumIsSet = false; + + Sequence< Any > aSetList; + if( aAny >>= aSetList ) + for( const auto& rSet : std::as_const(aSetList) ) + { + if( !bNumIsSet && ( rSet >>= nNum ) ) + bNumIsSet = true; + else + aText.isEmpty() && ( rSet >>= aText ); + } + + return bNumIsSet; +} + +void SAL_CALL ProgressHandlerWrap::push( const Any& Status ) +{ + if( !m_xStatusIndicator.is() ) + return; + + OUString aText; + sal_Int32 nRange; + + if( getStatusFromAny_Impl( Status, aText, nRange ) ) + m_xStatusIndicator->start( aText, nRange ); +} + +void SAL_CALL ProgressHandlerWrap::update( const Any& Status ) +{ + if( !m_xStatusIndicator.is() ) + return; + + OUString aText; + sal_Int32 nValue; + + if( getStatusFromAny_Impl( Status, aText, nValue ) ) + { + if( !aText.isEmpty() ) m_xStatusIndicator->setText( aText ); + m_xStatusIndicator->setValue( nValue ); + } +} + +void SAL_CALL ProgressHandlerWrap::pop() +{ + if( m_xStatusIndicator.is() ) + m_xStatusIndicator->end(); +} + +} // namespace utl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/ucbhelper/tempfile.cxx b/unotools/source/ucbhelper/tempfile.cxx new file mode 100644 index 000000000..d8f406b7a --- /dev/null +++ b/unotools/source/ucbhelper/tempfile.cxx @@ -0,0 +1,485 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <cassert> +#include <utility> + +#include <unotools/tempfile.hxx> +#include <rtl/ustring.hxx> +#include <rtl/instance.hxx> +#include <osl/detail/file.h> +#include <osl/file.hxx> +#include <tools/time.hxx> +#include <tools/debug.hxx> +#include <comphelper/DirectoryHelper.hxx> + +#ifdef UNX +#include <unistd.h> +#elif defined( _WIN32 ) +#include <process.h> +#endif + +using namespace osl; + +namespace +{ + struct TempNameBase_Impl + : public rtl::Static< OUString, TempNameBase_Impl > {}; +} + +namespace utl +{ + +static OUString getParentName( const OUString& aFileName ) +{ + sal_Int32 lastIndex = aFileName.lastIndexOf( '/' ); + OUString aParent; + + if (lastIndex > -1) + { + aParent = aFileName.copy(0, lastIndex); + + if (aParent.endsWith(":") && aParent.getLength() == 6) + aParent += "/"; + + if (aParent.equalsIgnoreAsciiCase("file://")) + aParent = "file:///"; + } + + return aParent; +} + +static bool ensuredir( const OUString& rUnqPath ) +{ + OUString aPath; + if ( rUnqPath.isEmpty() ) + return false; + + // remove trailing slash + if ( rUnqPath.endsWith("/") ) + aPath = rUnqPath.copy( 0, rUnqPath.getLength() - 1 ); + else + aPath = rUnqPath; + + // HACK: create directory on a mount point with nobrowse option + // returns ENOSYS in any case !! + osl::Directory aDirectory( aPath ); + osl::FileBase::RC nError = aDirectory.open(); + aDirectory.close(); + if( nError == osl::File::E_None ) + return true; + + // try to create the directory + nError = osl::Directory::create( aPath ); + bool bSuccess = ( nError == osl::File::E_None || nError == osl::FileBase::E_EXIST ); + if( !bSuccess ) + { + // perhaps parent(s) don't exist + OUString aParentDir = getParentName( aPath ); + if ( aParentDir != aPath ) + { + bSuccess = ensuredir( getParentName( aPath ) ); + + // After parent directory structure exists try it one's more + if ( bSuccess ) + { + // Parent directory exists, retry creation of directory + nError = osl::Directory::create( aPath ); + bSuccess =( nError == osl::File::E_None || nError == osl::FileBase::E_EXIST ); + } + } + } + + return bSuccess; +} + +static OUString ConstructTempDir_Impl( const OUString* pParent, bool bCreateParentDirs ) +{ + OUString aName; + + // Ignore pParent on iOS. We don't want to create any temp files + // in the same directory where the document being edited is. +#ifndef IOS + if ( pParent && !pParent->isEmpty() ) + { + // test for valid filename + OUString aRet; + if ((osl::FileBase::getSystemPathFromFileURL(*pParent, aRet) + == osl::FileBase::E_None) + && (osl::FileBase::getFileURLFromSystemPath(aRet, aRet) + == osl::FileBase::E_None)) + { + ::osl::DirectoryItem aItem; + sal_Int32 i = aRet.getLength(); + if ( aRet[i-1] == '/' ) + i--; + + if ( DirectoryItem::get( aRet.copy(0, i), aItem ) == FileBase::E_None || bCreateParentDirs ) + aName = aRet; + } + } +#else + (void) pParent; + (void) bCreateParentDirs; +#endif + + if ( aName.isEmpty() ) + { + OUString &rTempNameBase_Impl = TempNameBase_Impl::get(); + if (rTempNameBase_Impl.isEmpty()) + { + OUString ustrTempDirURL; + ::osl::FileBase::RC rc = ::osl::File::getTempDirURL( + ustrTempDirURL ); + if (rc == ::osl::FileBase::E_None) + rTempNameBase_Impl = ustrTempDirURL; + } + // if no parent or invalid parent : use default directory + DBG_ASSERT( !rTempNameBase_Impl.isEmpty(), "No TempDir!" ); + aName = rTempNameBase_Impl; + ensuredir( aName ); + } + + // Make sure that directory ends with a separator + if( !aName.isEmpty() && !aName.endsWith("/") ) + aName += "/"; + + return aName; +} + +namespace { + +class Tokens { +public: + virtual bool next(OUString *) = 0; + +protected: + virtual ~Tokens() {} // avoid warnings +}; + +class SequentialTokens: public Tokens { +public: + explicit SequentialTokens(bool showZero): m_value(0), m_show(showZero) {} + + bool next(OUString * token) override { + assert(token != nullptr); + if (m_value == SAL_MAX_UINT32) { + return false; + } + *token = m_show ? OUString::number(m_value) : OUString(); + ++m_value; + m_show = true; + return true; + } + +private: + sal_uInt32 m_value; + bool m_show; +}; + +class UniqueTokens: public Tokens { +public: + UniqueTokens(): m_count(0) {} + + bool next(OUString * token) override { + assert(token != nullptr); + // Because of the shared globalValue, no single instance of UniqueTokens + // is guaranteed to exhaustively test all 36^6 possible values, but stop + // after that many attempts anyway: + sal_uInt32 radix = 36; + sal_uInt32 max = radix * radix * radix * radix * radix * radix; + // 36^6 == 2'176'782'336 < SAL_MAX_UINT32 == 4'294'967'295 + if (m_count == max) { + return false; + } + sal_uInt32 v; + { + osl::MutexGuard g(osl::Mutex::getGlobalMutex()); + globalValue + = ((globalValue == SAL_MAX_UINT32 + ? tools::Time::GetSystemTicks() : globalValue + 1) + % max); + v = globalValue; + } + *token = OUString::number(v, radix); + ++m_count; + return true; + } + +private: + static sal_uInt32 globalValue; + + sal_uInt32 m_count; +}; + +} + +sal_uInt32 UniqueTokens::globalValue = SAL_MAX_UINT32; + +namespace +{ + class TempDirCreatedObserver : public DirectoryCreationObserver + { + public: + virtual void DirectoryCreated(const OUString& aDirectoryUrl) override + { + File::setAttributes( aDirectoryUrl, osl_File_Attribute_OwnRead | + osl_File_Attribute_OwnWrite | osl_File_Attribute_OwnExe ); + }; + }; +}; + +static OUString lcl_createName( + const OUString& rLeadingChars, Tokens & tokens, const OUString* pExtension, + const OUString* pParent, bool bDirectory, bool bKeep, bool bLock, + bool bCreateParentDirs ) +{ + OUString aName = ConstructTempDir_Impl( pParent, bCreateParentDirs ); + if ( bCreateParentDirs ) + { + sal_Int32 nOffset = rLeadingChars.lastIndexOf("/"); + OUString aDirName; + if (-1 != nOffset) + aDirName = aName + rLeadingChars.copy( 0, nOffset ); + else + aDirName = aName; + TempDirCreatedObserver observer; + FileBase::RC err = Directory::createPath( aDirName, &observer ); + if ( err != FileBase::E_None && err != FileBase::E_EXIST ) + return OUString(); + } + aName += rLeadingChars; + + OUString token; + while (tokens.next(&token)) + { + OUString aTmp( aName + token ); + if ( pExtension ) + aTmp += *pExtension; + else + aTmp += ".tmp"; + if ( bDirectory ) + { + FileBase::RC err = Directory::create( + aTmp, + (osl_File_OpenFlag_Read | osl_File_OpenFlag_Write + | osl_File_OpenFlag_Private)); + if ( err == FileBase::E_None ) + { + // !bKeep: only for creating a name, not a file or directory + if ( bKeep || Directory::remove( aTmp ) == FileBase::E_None ) + return aTmp; + else + return OUString(); + } + else if ( err != FileBase::E_EXIST ) + // if f.e. name contains invalid chars stop trying to create dirs + return OUString(); + } + else + { + DBG_ASSERT( bKeep, "Too expensive, use directory for creating name!" ); + File aFile( aTmp ); + FileBase::RC err = aFile.open( + osl_File_OpenFlag_Create | osl_File_OpenFlag_Private + | (bLock ? 0 : osl_File_OpenFlag_NoLock)); + if ( err == FileBase::E_None || (bLock && err == FileBase::E_NOLCK) ) + { + aFile.close(); + return aTmp; + } + else if ( err != FileBase::E_EXIST ) + { + // if f.e. name contains invalid chars stop trying to create dirs + // but if there is a folder with such name proceed further + + DirectoryItem aTmpItem; + FileStatus aTmpStatus( osl_FileStatus_Mask_Type ); + if ( DirectoryItem::get( aTmp, aTmpItem ) != FileBase::E_None + || aTmpItem.getFileStatus( aTmpStatus ) != FileBase::E_None + || aTmpStatus.getFileType() != FileStatus::Directory ) + return OUString(); + } + } + } + return OUString(); +} + +static OUString CreateTempName_Impl( const OUString* pParent, bool bKeep, bool bDir = true ) +{ + OUString aEyeCatcher = "lu"; +#ifdef UNX +#ifdef DBG_UTIL + const char* eye = getenv("LO_TESTNAME"); + if(eye) + { + aEyeCatcher = OUString(eye, strlen(eye), RTL_TEXTENCODING_ASCII_US); + } +#else + static const pid_t pid = getpid(); + static const OUString aPidString = OUString::number(pid); + aEyeCatcher += aPidString; +#endif +#elif defined(_WIN32) + static const int pid = _getpid(); + static const OUString aPidString = OUString::number(pid); + aEyeCatcher += aPidString; +#endif + UniqueTokens t; + return lcl_createName( aEyeCatcher, t, nullptr, pParent, bDir, bKeep, + false, false); +} + +OUString TempFile::CreateTempName() +{ + OUString aName(CreateTempName_Impl( nullptr, false )); + + // convert to file URL + OUString aTmp; + if ( !aName.isEmpty() ) + FileBase::getSystemPathFromFileURL( aName, aTmp ); + return aTmp; +} + +TempFile::TempFile( const OUString* pParent, bool bDirectory ) + : bIsDirectory( bDirectory ) + , bKillingFileEnabled( false ) +{ + aName = CreateTempName_Impl( pParent, true, bDirectory ); +} + +TempFile::TempFile( const OUString& rLeadingChars, bool _bStartWithZero, + const OUString* pExtension, const OUString* pParent, + bool bCreateParentDirs ) + : bIsDirectory( false ) + , bKillingFileEnabled( false ) +{ + SequentialTokens t(_bStartWithZero); + aName = lcl_createName( rLeadingChars, t, pExtension, pParent, false, + true, true, bCreateParentDirs ); +} + +TempFile::TempFile(TempFile && other) noexcept : + aName(std::move(other.aName)), pStream(std::move(other.pStream)), bIsDirectory(other.bIsDirectory), + bKillingFileEnabled(other.bKillingFileEnabled) +{ + other.bKillingFileEnabled = false; +} + +TempFile::~TempFile() +{ + pStream.reset(); + if ( bKillingFileEnabled ) + { + if ( bIsDirectory ) + { + comphelper::DirectoryHelper::deleteDirRecursively(aName); + } + else + { + File::remove( aName ); + } + } +} + +bool TempFile::IsValid() const +{ + return !aName.isEmpty(); +} + +OUString TempFile::GetFileName() const +{ + OUString aTmp; + FileBase::getSystemPathFromFileURL(aName, aTmp); + return aTmp; +} + +OUString const & TempFile::GetURL() const +{ + return aName; +} + +SvStream* TempFile::GetStream( StreamMode eMode ) +{ + if (!pStream) + { + if (!aName.isEmpty()) + pStream.reset(new SvFileStream(aName, eMode | StreamMode::TEMPORARY)); + else + pStream.reset(new SvMemoryStream(nullptr, 0, eMode)); + } + + return pStream.get(); +} + +void TempFile::CloseStream() +{ + pStream.reset(); +} + +OUString TempFile::SetTempNameBaseDirectory( const OUString &rBaseName ) +{ + if( rBaseName.isEmpty() ) + return OUString(); + + OUString aUnqPath( rBaseName ); + + // remove trailing slash + if ( rBaseName.endsWith("/") ) + aUnqPath = rBaseName.copy( 0, rBaseName.getLength() - 1 ); + + // try to create the directory + bool bRet = false; + osl::FileBase::RC err = osl::Directory::create( aUnqPath ); + if ( err != FileBase::E_None && err != FileBase::E_EXIST ) + // perhaps parent(s) don't exist + bRet = ensuredir( aUnqPath ); + else + bRet = true; + + // failure to create base directory means returning an empty string + OUString aTmp; + if ( bRet ) + { + // append own internal directory + OUString &rTempNameBase_Impl = TempNameBase_Impl::get(); + rTempNameBase_Impl = rBaseName + "/"; + + TempFile aBase( nullptr, true ); + if ( aBase.IsValid() ) + // use it in case of success + rTempNameBase_Impl = aBase.aName; + + // return system path of used directory + FileBase::getSystemPathFromFileURL( rTempNameBase_Impl, aTmp ); + } + + return aTmp; +} + +OUString TempFile::GetTempNameBaseDirectory() +{ + return ConstructTempDir_Impl(nullptr, false); +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/ucbhelper/ucbhelper.cxx b/unotools/source/ucbhelper/ucbhelper.cxx new file mode 100644 index 000000000..7916259f1 --- /dev/null +++ b/unotools/source/ucbhelper/ucbhelper.cxx @@ -0,0 +1,420 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <cassert> +#include <vector> + +#include <com/sun/star/sdbc/XResultSet.hpp> +#include <com/sun/star/task/XInteractionHandler.hpp> +#include <com/sun/star/task/InteractionHandler.hpp> +#include <com/sun/star/ucb/CommandAbortedException.hpp> +#include <com/sun/star/ucb/ContentInfo.hpp> +#include <com/sun/star/ucb/ContentInfoAttribute.hpp> +#include <com/sun/star/ucb/IOErrorCode.hpp> +#include <com/sun/star/ucb/InteractiveIOException.hpp> +#include <com/sun/star/ucb/NameClashException.hpp> +#include <com/sun/star/ucb/UniversalContentBroker.hpp> +#include <com/sun/star/ucb/XCommandEnvironment.hpp> +#include <com/sun/star/ucb/XContentAccess.hpp> +#include <com/sun/star/ucb/XUniversalContentBroker.hpp> +#include <com/sun/star/uno/Any.hxx> +#include <com/sun/star/uno/Exception.hpp> +#include <com/sun/star/uno/Reference.hxx> +#include <com/sun/star/uno/RuntimeException.hpp> +#include <com/sun/star/uno/Sequence.hxx> +#include <comphelper/processfactory.hxx> +#include <cppuhelper/exc_hlp.hxx> +#include <comphelper/simplefileaccessinteraction.hxx> +#include <osl/file.hxx> +#include <rtl/ustring.hxx> +#include <sal/log.hxx> +#include <sal/types.h> +#include <tools/datetime.hxx> +#include <tools/urlobj.hxx> +#include <tools/diagnose_ex.h> +#include <ucbhelper/commandenvironment.hxx> +#include <ucbhelper/content.hxx> +#include <unotools/ucbhelper.hxx> + +namespace com::sun::star::ucb { class XProgressHandler; } +namespace com::sun::star::uno { class XComponentContext; } +namespace com::sun::star::util { struct DateTime; } + +namespace { + +OUString canonic(OUString const & url) { + INetURLObject o(url); + SAL_WARN_IF(o.HasError(), "unotools.ucbhelper", "Invalid URL \"" << url << '"'); + return o.GetMainURL(INetURLObject::DecodeMechanism::NONE); +} + +ucbhelper::Content content(OUString const & url) { + return ucbhelper::Content( + canonic(url), + utl::UCBContentHelper::getDefaultCommandEnvironment(), + comphelper::getProcessComponentContext()); +} + +ucbhelper::Content content(INetURLObject const & url) { + return ucbhelper::Content( + url.GetMainURL(INetURLObject::DecodeMechanism::NONE), + utl::UCBContentHelper::getDefaultCommandEnvironment(), + comphelper::getProcessComponentContext()); +} + +std::vector<OUString> getContents(OUString const & url) { + try { + std::vector<OUString> cs; + ucbhelper::Content c(content(url)); + css::uno::Sequence<OUString> args { "Title" }; + css::uno::Reference<css::sdbc::XResultSet> res( c.createCursor(args), css::uno::UNO_SET_THROW); + css::uno::Reference<css::ucb::XContentAccess> acc( res, css::uno::UNO_QUERY_THROW); + while (res->next()) { + cs.push_back(acc->queryContentIdentifierString()); + } + return cs; + } catch (css::uno::RuntimeException const &) { + throw; + } catch (css::ucb::CommandAbortedException const &) { + assert(false && "this cannot happen"); + throw; + } catch (css::uno::Exception const &) { + TOOLS_INFO_EXCEPTION("unotools.ucbhelper", "getContents(" << url << ")"); + return std::vector<OUString>(); + } +} + +OUString getCasePreservingUrl(const INetURLObject& url) { + return + content(url).executeCommand( + "getCasePreservingURL", + css::uno::Any()). + get<OUString>(); +} + +DateTime convert(css::util::DateTime const & dt) { + return DateTime(dt); +} + +} + +css::uno::Reference< css::ucb::XCommandEnvironment > utl::UCBContentHelper::getDefaultCommandEnvironment() +{ + css::uno::Reference< css::task::XInteractionHandler > xIH( + css::task::InteractionHandler::createWithParent( + comphelper::getProcessComponentContext(), nullptr ) ); + + css::uno::Reference< css::ucb::XProgressHandler > xProgress; + ucbhelper::CommandEnvironment* pCommandEnv = + new ::ucbhelper::CommandEnvironment( + new comphelper::SimpleFileAccessInteraction( xIH ), xProgress ); + + css::uno::Reference < css::ucb::XCommandEnvironment > xEnv( + static_cast< css::ucb::XCommandEnvironment* >(pCommandEnv), css::uno::UNO_QUERY ); + return xEnv; +} + +bool utl::UCBContentHelper::IsDocument(OUString const & url) { + try { + return content(url).isDocument(); + } catch (css::uno::RuntimeException const &) { + throw; + } catch (css::ucb::CommandAbortedException const &) { + assert(false && "this cannot happen"); + throw; + } catch (css::uno::Exception const &) { + TOOLS_INFO_EXCEPTION("unotools.ucbhelper", "UCBContentHelper::IsDocument(" << url << ")"); + return false; + } +} + +css::uno::Any utl::UCBContentHelper::GetProperty( + OUString const & url, OUString const & property) +{ + try { + return content(url).getPropertyValue(property); + } catch (css::uno::RuntimeException const &) { + throw; + } catch (css::ucb::CommandAbortedException const &) { + assert(false && "this cannot happen"); + throw; + } catch (css::uno::Exception const &) { + TOOLS_INFO_EXCEPTION("unotools.ucbhelper", "UCBContentHelper::GetProperty(" << url << ", " << property << ")"); + return css::uno::Any(); + } +} + +bool utl::UCBContentHelper::IsFolder(OUString const & url) { + try { + return content(url).isFolder(); + } catch (css::uno::RuntimeException const &) { + throw; + } catch (css::ucb::CommandAbortedException const &) { + assert(false && "this cannot happen"); + throw; + } catch (css::uno::Exception const &) { + TOOLS_INFO_EXCEPTION("unotools.ucbhelper", "UCBContentHelper::IsFolder(" << url << ")"); + return false; + } +} + +bool utl::UCBContentHelper::GetTitle( + OUString const & url, OUString * title) +{ + assert(title != nullptr); + try { + return content(url).getPropertyValue("Title") >>= *title; + } catch (css::uno::RuntimeException const &) { + throw; + } catch (css::ucb::CommandAbortedException const &) { + assert(false && "this cannot happen"); + throw; + } catch (css::uno::Exception const &) { + TOOLS_INFO_EXCEPTION("unotools.ucbhelper", "UCBContentHelper::GetTitle(" << url << ")"); + return false; + } +} + +bool utl::UCBContentHelper::Kill(OUString const & url) { + try { + content(url).executeCommand( + "delete", + css::uno::makeAny(true)); + return true; + } catch (css::uno::RuntimeException const &) { + throw; + } catch (css::ucb::CommandAbortedException const &) { + assert(false && "this cannot happen"); + throw; + } catch (css::uno::Exception const &) { + TOOLS_INFO_EXCEPTION("unotools.ucbhelper", "UCBContentHelper::Kill(" << url << ")"); + return false; + } +} + +bool utl::UCBContentHelper::MakeFolder( + ucbhelper::Content & parent, OUString const & title, + ucbhelper::Content & result) +{ + bool exists = false; + try { + const css::uno::Sequence<css::ucb::ContentInfo> info( + parent.queryCreatableContentsInfo()); + for (const auto& rInfo : info) { + // Simply look for the first KIND_FOLDER: + if ((rInfo.Attributes + & css::ucb::ContentInfoAttribute::KIND_FOLDER) + != 0) + { + // Make sure the only required bootstrap property is "Title": + if ( rInfo.Properties.getLength() != 1 || rInfo.Properties[0].Name != "Title" ) + { + continue; + } + css::uno::Sequence<OUString> keys { "Title" }; + css::uno::Sequence<css::uno::Any> values(1); + values[0] <<= title; + if (parent.insertNewContent(rInfo.Type, keys, values, result)) + { + return true; + } + } + } + } catch (css::ucb::InteractiveIOException const & e) { + if (e.Code == css::ucb::IOErrorCode_ALREADY_EXISTING) { + exists = true; + } else { + TOOLS_INFO_EXCEPTION( + "unotools.ucbhelper", + "UCBContentHelper::MakeFolder(" << title << ")"); + } + } catch (css::ucb::NameClashException const &) { + exists = true; + } catch (css::uno::RuntimeException const &) { + throw; + } catch (css::ucb::CommandAbortedException const &) { + assert(false && "this cannot happen"); + throw; + } catch (css::uno::Exception const &) { + TOOLS_INFO_EXCEPTION( + "unotools.ucbhelper", + "UCBContentHelper::MakeFolder(" << title << ") "); + } + if (exists) { + INetURLObject o(parent.getURL()); + o.Append(title); + result = content(o); + return true; + } else { + return false; + } +} + +bool utl::UCBContentHelper::IsYounger( + OUString const & younger, OUString const & older) +{ + try { + return + convert( + content(younger).getPropertyValue( + "DateModified"). + get<css::util::DateTime>()) + > convert( + content(older).getPropertyValue( + "DateModified"). + get<css::util::DateTime>()); + } catch (css::uno::RuntimeException const &) { + throw; + } catch (css::ucb::CommandAbortedException const &) { + assert(false && "this cannot happen"); + throw; + } catch (css::uno::Exception const &) { + TOOLS_INFO_EXCEPTION( + "unotools.ucbhelper", + "UCBContentHelper::IsYounger(" << younger << ", " << older << ")"); + return false; + } +} + +bool utl::UCBContentHelper::Exists(OUString const & url) { + OUString pathname; + if (osl::FileBase::getSystemPathFromFileURL(url, pathname) + == osl::FileBase::E_None) + { + // Try to create a directory entry for the given URL: + OUString url2; + if (osl::FileBase::getFileURLFromSystemPath(pathname, url2) + == osl::FileBase::E_None) + { + // #106526 osl_getDirectoryItem is an existence check, no further + // osl_getFileStatus call necessary: + osl::DirectoryItem item; + return osl::DirectoryItem::get(url2, item) == osl::FileBase::E_None; + } else { + return false; + } + } else { + // Divide URL into folder and name part: + INetURLObject o(url); + OUString name( + o.getName( + INetURLObject::LAST_SEGMENT, true, + INetURLObject::DecodeMechanism::WithCharset)); + o.removeSegment(); + o.removeFinalSlash(); + std::vector<OUString> cs( + getContents(o.GetMainURL(INetURLObject::DecodeMechanism::NONE))); + return std::any_of(cs.begin(), cs.end(), + [&name](const OUString& rItem) { + return INetURLObject(rItem). + getName(INetURLObject::LAST_SEGMENT, true, INetURLObject::DecodeMechanism::WithCharset). + equalsIgnoreAsciiCase(name); }); + } +} + +bool utl::UCBContentHelper::IsSubPath( + OUString const & parent, OUString const & child) +{ + // The comparison is done in the following way: + // - First, compare case sensitively + // - If names are different, try a fallback comparing case insensitively + // - If the last comparison succeeded, get case preserving normalized names + // for the files and compare them + // (The second step is required because retrieving the normalized names + // might be very expensive in some cases.) + INetURLObject candidate(child); + INetURLObject folder(parent); + if (candidate.GetProtocol() != folder.GetProtocol()) { + return false; + } + INetURLObject candidateLower(child.toAsciiLowerCase()); + INetURLObject folderLower(parent.toAsciiLowerCase()); + try { + INetURLObject tmp; + do { + if (candidate == folder + || (candidate.GetProtocol() == INetProtocol::File + && candidateLower == folderLower + && (getCasePreservingUrl(candidate) + == getCasePreservingUrl(folder)))) + { + return true; + } + tmp = candidate; + } while (candidate.removeSegment() && candidateLower.removeSegment() + && candidate != tmp); + // INetURLObject::removeSegment sometimes returns true without + // modifying the URL, e.g., in case of "file:///" + } catch (css::uno::RuntimeException const &) { + throw; + } catch (css::ucb::CommandAbortedException const &) { + assert(false && "this cannot happen"); + throw; + } catch (css::uno::Exception const &) { + TOOLS_INFO_EXCEPTION( + "unotools.ucbhelper", + "UCBContentHelper::IsSubPath(" << parent << ", " << child << ")"); + } + return false; +} + +bool utl::UCBContentHelper::EqualURLs( + OUString const & url1, OUString const & url2) +{ + if (url1.isEmpty() || url2.isEmpty()) { + return false; + } + css::uno::Reference< css::ucb::XUniversalContentBroker > ucb( + css::ucb::UniversalContentBroker::create( + comphelper::getProcessComponentContext())); + return + ucb->compareContentIds( + ucb->createContentIdentifier(canonic(url1)), + ucb->createContentIdentifier(canonic(url2))) + == 0; +} + +bool utl::UCBContentHelper::ensureFolder( + const css::uno::Reference< css::uno::XComponentContext >& xCtx, + const css::uno::Reference< css::ucb::XCommandEnvironment >& xEnv, + const OUString& rFolder, ucbhelper::Content & result) throw() +{ + try + { + INetURLObject aURL( rFolder ); + OUString aTitle = aURL.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DecodeMechanism::WithCharset ); + aURL.removeSegment(); + ::ucbhelper::Content aParent; + + if ( ::ucbhelper::Content::create( aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE ), + xEnv, xCtx, aParent ) ) + { + return ::utl::UCBContentHelper::MakeFolder(aParent, aTitle, result); + } + } + catch (...) + { + } + + return false; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/ucbhelper/ucblockbytes.cxx b/unotools/source/ucbhelper/ucblockbytes.cxx new file mode 100644 index 000000000..b410352bf --- /dev/null +++ b/unotools/source/ucbhelper/ucblockbytes.cxx @@ -0,0 +1,1327 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include "ucblockbytes.hxx" + +#include <sal/log.hxx> +#include <comphelper/processfactory.hxx> +#include <salhelper/condition.hxx> +#include <osl/thread.hxx> +#include <osl/diagnose.h> +#include <tools/urlobj.hxx> +#include <tools/solar.h> +#include <ucbhelper/interactionrequest.hxx> +#include <com/sun/star/task/XInteractionAbort.hpp> +#include <com/sun/star/ucb/InteractiveNetworkConnectException.hpp> +#include <com/sun/star/ucb/CommandFailedException.hpp> +#include <com/sun/star/ucb/ContentCreationException.hpp> +#include <com/sun/star/ucb/CommandAbortedException.hpp> +#include <com/sun/star/ucb/UnsupportedDataSinkException.hpp> +#include <com/sun/star/ucb/InteractiveIOException.hpp> +#include <com/sun/star/ucb/XContentIdentifier.hpp> +#include <com/sun/star/ucb/XContent.hpp> +#include <com/sun/star/io/IOException.hpp> +#include <com/sun/star/io/XActiveDataStreamer.hpp> +#include <com/sun/star/io/TempFile.hpp> +#include <com/sun/star/ucb/XCommandProcessor.hpp> +#include <com/sun/star/task/XInteractionHandler.hpp> +#include <com/sun/star/ucb/OpenCommandArgument2.hpp> +#include <com/sun/star/ucb/PostCommandArgument2.hpp> +#include <com/sun/star/ucb/OpenMode.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/beans/XPropertiesChangeNotifier.hpp> +#include <com/sun/star/beans/XPropertiesChangeListener.hpp> +#include <com/sun/star/io/XActiveDataSink.hpp> +#include <com/sun/star/io/XActiveDataControl.hpp> +#include <com/sun/star/io/XSeekable.hpp> +#include <cppuhelper/implbase.hxx> +#include <tools/debug.hxx> +#include <com/sun/star/io/XTruncate.hpp> +#include <com/sun/star/lang/IllegalArgumentException.hpp> + +#include <comphelper/storagehelper.hxx> +#include <ucbhelper/content.hxx> + +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::io; +using namespace ::com::sun::star::ucb; +using namespace ::com::sun::star::task; +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::beans; + +namespace utl +{ + +namespace { + +/** + Helper class for getting a XInputStream when opening a content + */ +class UcbDataSink_Impl : public ::cppu::WeakImplHelper< XActiveDataControl, XActiveDataSink > +{ + UcbLockBytesRef m_xLockBytes; + +public: + explicit UcbDataSink_Impl( UcbLockBytes* pLockBytes ) + : m_xLockBytes( pLockBytes ) + {} + + // XActiveDataControl. + virtual void SAL_CALL addListener ( const Reference<XStreamListener> &/*rxListener*/) override {} + virtual void SAL_CALL removeListener ( const Reference<XStreamListener> &/*rxListener*/) override {} + virtual void SAL_CALL start() override {} + virtual void SAL_CALL terminate() override + { m_xLockBytes->terminate_Impl(); } + + // XActiveDataSink. + virtual void SAL_CALL setInputStream ( const Reference<XInputStream> &rxInputStream) override + { m_xLockBytes->setInputStream_Impl (rxInputStream); } + virtual Reference<XInputStream> SAL_CALL getInputStream() override + { return m_xLockBytes->getInputStream_Impl(); } +}; + +/** + Helper class for getting a XStream when opening a content + */ +class UcbStreamer_Impl : public ::cppu::WeakImplHelper< XActiveDataStreamer, XActiveDataControl > +{ + Reference < XStream > m_xStream; + UcbLockBytesRef m_xLockBytes; + +public: + explicit UcbStreamer_Impl( UcbLockBytes* pLockBytes ) + : m_xLockBytes( pLockBytes ) + {} + + // XActiveDataControl. + virtual void SAL_CALL addListener ( const Reference<XStreamListener> &/*rxListener*/) override {} + virtual void SAL_CALL removeListener ( const Reference<XStreamListener> &/*rxListener*/) override {} + virtual void SAL_CALL start() override {} + virtual void SAL_CALL terminate() override + { m_xLockBytes->terminate_Impl(); } + + // XActiveDataStreamer + virtual void SAL_CALL setStream( const Reference< XStream >& aStream ) override + { m_xStream = aStream; m_xLockBytes->setStream_Impl( aStream ); } + virtual Reference< XStream > SAL_CALL getStream() override + { return m_xStream; } +}; + +/** + Helper class for managing interactions and progress when executing UCB commands + */ +class UcbTaskEnvironment : public ::cppu::WeakImplHelper< XCommandEnvironment > +{ + Reference< XInteractionHandler > m_xInteractionHandler; + Reference< XProgressHandler > m_xProgressHandler; + +public: + UcbTaskEnvironment( const Reference< XInteractionHandler>& rxInteractionHandler, + const Reference< XProgressHandler>& rxProgressHandler ) + : m_xInteractionHandler( rxInteractionHandler ) + , m_xProgressHandler( rxProgressHandler ) + {} + + virtual Reference<XInteractionHandler> SAL_CALL getInteractionHandler() override + { return m_xInteractionHandler; } + + virtual Reference<XProgressHandler> SAL_CALL getProgressHandler() override + { return m_xProgressHandler; } +}; + +/** + Helper class for property change notifies when executing UCB commands +*/ +class UcbPropertiesChangeListener_Impl : public ::cppu::WeakImplHelper< XPropertiesChangeListener > +{ +public: + UcbLockBytesRef m_xLockBytes; + + explicit UcbPropertiesChangeListener_Impl( UcbLockBytesRef const & rRef ) + : m_xLockBytes( rRef ) + {} + + virtual void SAL_CALL disposing ( const EventObject &/*rEvent*/) override {} + virtual void SAL_CALL propertiesChange ( const Sequence<PropertyChangeEvent> &rEvent) override; +}; + +} + +void SAL_CALL UcbPropertiesChangeListener_Impl::propertiesChange ( const Sequence<PropertyChangeEvent> &rEvent) +{ + for (const auto& rPropChangeEvent : rEvent) + { + if (rPropChangeEvent.PropertyName == "DocumentHeader") + { + m_xLockBytes->SetStreamValid_Impl(); + } + } +} + +namespace { + +class Moderator + : public osl::Thread +{ + // usage restriction: + // It might be possible, that the call to the interactionhandler and/or + // progresshandler is done asynchronously, while the 'execute' simply + // returns. This would imply that these class must be refcounted!!! + +public: + /// @throws ContentCreationException + /// @throws RuntimeException + Moderator( + Reference < XContent > const & xContent, + Reference < XInteractionHandler > const & xInteract, + const Command& rArg + ); + + enum class ResultType { + NORESULT, + + INTERACTIONREQUEST, // reply expected + + INPUTSTREAM, + STREAM, + + RESULT, + TIMEDOUT, + COMMANDABORTED, + COMMANDFAILED, + INTERACTIVEIO, + UNSUPPORTED, + GENERAL + }; + + class ConditionRes + : public salhelper::Condition + { + public: + ConditionRes(osl::Mutex& aMutex,Moderator& aModerator) + : salhelper::Condition(aMutex), + m_aModerator(aModerator) + { + } + + protected: + bool applies() const override { + return m_aModerator.m_aResultType != ResultType::NORESULT; + } + + private: + Moderator& m_aModerator; + }; + + struct Result { + ResultType type; + Any result; + IOErrorCode ioErrorCode; + }; + + Result getResult(const sal_uInt32 milliSec); + + enum ReplyType { + NOREPLY, + EXIT, + REQUESTHANDLED + }; + + class ConditionRep + : public salhelper::Condition + { + public: + ConditionRep(osl::Mutex& aMutex,Moderator& aModerator) + : salhelper::Condition(aMutex), + m_aModerator(aModerator) + { + } + + protected: + bool applies() const override { + return m_aModerator.m_aReplyType != NOREPLY; + } + + private: + Moderator& m_aModerator; + }; + + void setReply(ReplyType); + + void handle( const Reference<XInteractionRequest >& Request ); + + void setStream(const Reference< XStream >& aStream); + void setInputStream(const Reference<XInputStream> &rxInputStream); + +protected: + virtual void SAL_CALL run() override; + virtual void SAL_CALL onTerminated() override; + +private: + osl::Mutex m_aMutex; + + friend class ConditionRes; + + ConditionRes m_aRes; + ResultType m_aResultType; + IOErrorCode m_nIOErrorCode; + Any m_aResult; + + friend class ConditionRep; + + ConditionRep m_aRep; + ReplyType m_aReplyType; + + Command m_aArg; + ::ucbhelper::Content m_aContent; +}; + +class ModeratorsActiveDataStreamer + : public ::cppu::WeakImplHelper<XActiveDataStreamer> +{ +public: + + explicit ModeratorsActiveDataStreamer(Moderator &theModerator); + + // XActiveDataStreamer + virtual void SAL_CALL + setStream( + const Reference< XStream >& aStream + ) override; + + virtual Reference<XStream> SAL_CALL getStream () override + { + osl::MutexGuard aGuard(m_aMutex); + return m_xStream; + } + +private: + Moderator& m_aModerator; + + osl::Mutex m_aMutex; + Reference<XStream> m_xStream; +}; + +class ModeratorsActiveDataSink + : public ::cppu::WeakImplHelper<XActiveDataSink> +{ +public: + + explicit ModeratorsActiveDataSink(Moderator &theModerator); + + // XActiveDataSink. + virtual void SAL_CALL + setInputStream ( + const Reference<XInputStream> &rxInputStream + ) override; + + virtual Reference<XInputStream> SAL_CALL getInputStream() override + { + osl::MutexGuard aGuard(m_aMutex); + return m_xStream; + } + +private: + Moderator& m_aModerator; + osl::Mutex m_aMutex; + Reference<XInputStream> m_xStream; +}; + +} + +ModeratorsActiveDataSink::ModeratorsActiveDataSink(Moderator &theModerator) + : m_aModerator(theModerator) +{ +} + +// XActiveDataSink. +void SAL_CALL +ModeratorsActiveDataSink::setInputStream ( + const Reference<XInputStream> &rxInputStream +) +{ + m_aModerator.setInputStream(rxInputStream); + osl::MutexGuard aGuard(m_aMutex); + m_xStream = rxInputStream; +} + +ModeratorsActiveDataStreamer::ModeratorsActiveDataStreamer( + Moderator &theModerator +) + : m_aModerator(theModerator) +{ +} + +// XActiveDataStreamer. +void SAL_CALL +ModeratorsActiveDataStreamer::setStream ( + const Reference<XStream> &rxStream +) +{ + m_aModerator.setStream(rxStream); + osl::MutexGuard aGuard(m_aMutex); + m_xStream = rxStream; +} + +namespace { + +class ModeratorsInteractionHandler + : public ::cppu::WeakImplHelper<XInteractionHandler> +{ +public: + + explicit ModeratorsInteractionHandler(Moderator &theModerator); + + virtual void SAL_CALL + handle( const Reference<XInteractionRequest >& Request ) override; + +private: + + Moderator& m_aModerator; +}; + +} + +ModeratorsInteractionHandler::ModeratorsInteractionHandler( + Moderator &aModerator) + : m_aModerator(aModerator) +{ +} + +void SAL_CALL +ModeratorsInteractionHandler::handle( + const Reference<XInteractionRequest >& Request +) +{ + // wakes up the mainthread + m_aModerator.handle(Request); +} + +Moderator::Moderator( + Reference < XContent > const & xContent, + Reference < XInteractionHandler > const & xInteract, + const Command& rArg +) + : m_aMutex(), + + m_aRes(m_aMutex,*this), + m_aResultType(ResultType::NORESULT), + m_nIOErrorCode(IOErrorCode_ABORT), + m_aResult(), + + m_aRep(m_aMutex,*this), + m_aReplyType(NOREPLY), + + m_aArg(rArg), + m_aContent( + xContent, + new UcbTaskEnvironment( + xInteract.is() ? new ModeratorsInteractionHandler(*this) : nullptr, + nullptr), + comphelper::getProcessComponentContext()) +{ + // now exchange the whole data sink stuff + // with a thread safe version + + Reference<XInterface> *pxSink = nullptr; + + PostCommandArgument2 aPostArg; + OpenCommandArgument2 aOpenArg; + + int dec(2); + if(m_aArg.Argument >>= aPostArg) { + pxSink = &aPostArg.Sink; + dec = 0; + } + else if(m_aArg.Argument >>= aOpenArg) { + pxSink = &aOpenArg.Sink; + dec = 1; + } + + if(dec ==2) + throw ContentCreationException(); + + Reference < XActiveDataSink > xActiveSink(*pxSink,UNO_QUERY); + if(xActiveSink.is()) + pxSink->set( static_cast<cppu::OWeakObject*>(new ModeratorsActiveDataSink(*this))); + + Reference<XActiveDataStreamer> xStreamer( *pxSink, UNO_QUERY ); + if ( xStreamer.is() ) + pxSink->set( static_cast<cppu::OWeakObject*>(new ModeratorsActiveDataStreamer(*this))); + + if(dec == 0) + m_aArg.Argument <<= aPostArg; + else if(dec == 1) + m_aArg.Argument <<= aOpenArg; +} + +Moderator::Result Moderator::getResult(const sal_uInt32 milliSec) +{ + Result ret; + try { + salhelper::ConditionWaiter aWaiter(m_aRes,milliSec); + ret.type = m_aResultType; + ret.result = m_aResult; + ret.ioErrorCode = m_nIOErrorCode; + + // reset + m_aResultType = ResultType::NORESULT; + } + catch (const salhelper::ConditionWaiter::timedout&) + { + ret.type = ResultType::TIMEDOUT; + } + + return ret; +} + +void Moderator::setReply(ReplyType aReplyType ) +{ + salhelper::ConditionModifier aMod(m_aRep); + m_aReplyType = aReplyType; +} + +void Moderator::handle( const Reference<XInteractionRequest >& Request ) +{ + ReplyType aReplyType; + + do { + { + salhelper::ConditionModifier aMod(m_aRes); + m_aResultType = ResultType::INTERACTIONREQUEST; + m_aResult <<= Request; + } + + { + salhelper::ConditionWaiter aWait(m_aRep); + aReplyType = m_aReplyType; + + // reset + m_aReplyType = NOREPLY; + } + + if(aReplyType == EXIT) { + const Sequence<Reference<XInteractionContinuation> > aSeq( + Request->getContinuations()); + for(const auto& rContinuation : aSeq) { + Reference<XInteractionAbort> aRef(rContinuation,UNO_QUERY); + if(aRef.is()) { + aRef->select(); + } + } + + // resignal the exit condition + setReply(EXIT); + break; + } + } while(aReplyType != REQUESTHANDLED); +} + +void Moderator::setStream(const Reference< XStream >& aStream) +{ + { + salhelper::ConditionModifier aMod(m_aRes); + m_aResultType = ResultType::STREAM; + m_aResult <<= aStream; + } + ReplyType aReplyType; + { + salhelper::ConditionWaiter aWait(m_aRep); + aReplyType = m_aReplyType; + m_aReplyType = NOREPLY; + } + if(aReplyType == EXIT) + setReply(EXIT); +} + +void Moderator::setInputStream(const Reference<XInputStream> &rxInputStream) +{ + { + salhelper::ConditionModifier aMod(m_aRes); + m_aResultType = ResultType::INPUTSTREAM; + m_aResult <<= rxInputStream; + } + ReplyType aReplyType; + { + salhelper::ConditionWaiter aWait(m_aRep); + aReplyType = m_aReplyType; + m_aReplyType = NOREPLY; + } + if(aReplyType == EXIT) + setReply(EXIT); +} + +void SAL_CALL Moderator::run() +{ + osl_setThreadName("utl::Moderator"); + + ResultType aResultType; + Any aResult; + IOErrorCode nIOErrorCode = IOErrorCode_ABORT; + + try + { + aResult = m_aContent.executeCommand(m_aArg.Name,m_aArg.Argument); + aResultType = ResultType::RESULT; + } + catch (const CommandAbortedException&) + { + aResultType = ResultType::COMMANDABORTED; + } + catch (const CommandFailedException&) + { + aResultType = ResultType::COMMANDFAILED; + } + catch (const InteractiveIOException& r) + { + nIOErrorCode = r.Code; + aResultType = ResultType::INTERACTIVEIO; + } + catch (const UnsupportedDataSinkException &) + { + aResultType = ResultType::UNSUPPORTED; + } + catch (const Exception&) + { + aResultType = ResultType::GENERAL; + } + + { + salhelper::ConditionModifier aMod(m_aRes); + m_aResultType = aResultType; + m_aResult = aResult; + m_nIOErrorCode = nIOErrorCode; + } +} + +void SAL_CALL Moderator::onTerminated() +{ + { + salhelper::ConditionWaiter aWaiter(m_aRep); + } + delete this; +} + +/** + Function for opening UCB contents synchronously, + but with handled timeout; +*/ +static bool UCBOpenContentSync_( + const UcbLockBytesRef& xLockBytes, + const Reference < XContent >& xContent, + const Command& rArg, + const Reference < XInterface >& xSink, + const Reference < XInteractionHandler >& xInteract ); + +static bool UCBOpenContentSync( + const UcbLockBytesRef& xLockBytes, + Reference < XContent > const & xContent, + const Command& rArg, + const Reference < XInterface >& xSink, + Reference < XInteractionHandler > const & xInteract ) +{ + // http protocol must be handled in a special way: + // during the opening process the input stream may change + // only the last inputstream after notifying the document + // headers is valid + + Reference<XContentIdentifier> xContId( + xContent.is() ? xContent->getIdentifier() : nullptr ); + + OUString aScheme; + if(xContId.is()) + aScheme = xContId->getContentProviderScheme(); + + // now determine whether we use a timeout or not; + if( ! aScheme.equalsIgnoreAsciiCase("http") && + ! aScheme.equalsIgnoreAsciiCase("https") && + ! aScheme.equalsIgnoreAsciiCase("vnd.sun.star.webdav") && + ! aScheme.equalsIgnoreAsciiCase("vnd.sun.star.webdavs") && + ! aScheme.equalsIgnoreAsciiCase("ftp")) + return UCBOpenContentSync_( + xLockBytes,xContent,rArg,xSink,xInteract); + + if ( !aScheme.equalsIgnoreAsciiCase( "http" ) && + !aScheme.equalsIgnoreAsciiCase( "https" ) ) + xLockBytes->SetStreamValid_Impl(); + + Reference< XPropertiesChangeListener > xListener; + Reference< XPropertiesChangeNotifier > xProps(xContent,UNO_QUERY); + if(xProps.is()) { + xListener = + new UcbPropertiesChangeListener_Impl(xLockBytes); + xProps->addPropertiesChangeListener( + Sequence< OUString >(), + xListener); + } + + bool bException(false); + bool bAborted(false); + bool bResultAchieved(false); + + Moderator* pMod = nullptr; + try + { + pMod = new Moderator(xContent,xInteract,rArg); + pMod->create(); + } + catch (const ContentCreationException&) + { + bResultAchieved = bException = true; + xLockBytes->SetError( ERRCODE_IO_GENERAL ); + } + + sal_uInt32 nTimeout(5000); // initially 5000 milliSec + while(!bResultAchieved) { + + // try to get the result for with timeout + Moderator::Result res = pMod->getResult(nTimeout); + + switch(res.type) { + case Moderator::ResultType::STREAM: + { + Reference<XStream> result; + if(res.result >>= result) { + Reference < XActiveDataStreamer > xStreamer( + xSink, UNO_QUERY + ); + + if(xStreamer.is()) + xStreamer->setStream(result); + } + pMod->setReply(Moderator::REQUESTHANDLED); + break; + } + case Moderator::ResultType::INPUTSTREAM: + { + Reference<XInputStream> result; + res.result >>= result; + Reference < XActiveDataSink > xActiveSink( + xSink, UNO_QUERY + ); + + if(xActiveSink.is()) + xActiveSink->setInputStream(result); + pMod->setReply(Moderator::REQUESTHANDLED); + break; + } + case Moderator::ResultType::TIMEDOUT: + { + Reference<XInteractionRetry> xRet; + if(xInteract.is()) { + InteractiveNetworkConnectException aExcep; + INetURLObject aURL( + xContId.is() ? + xContId->getContentIdentifier() : + OUString() ); + aExcep.Server = aURL.GetHost(); + aExcep.Classification = InteractionClassification_ERROR; + aExcep.Message = "server not responding after five seconds"; + Any request; + request <<= aExcep; + ucbhelper::InteractionRequest *ir = + new ucbhelper::InteractionRequest(request); + Reference<XInteractionRequest> xIR(ir); + Sequence<Reference<XInteractionContinuation> > aSeq(2); + ucbhelper::InteractionRetry *retryP = + new ucbhelper::InteractionRetry(ir); + aSeq[0] = retryP; + ucbhelper::InteractionAbort *abortP = + new ucbhelper::InteractionAbort(ir); + aSeq[1] = abortP; + + ir->setContinuations(aSeq); + xInteract->handle(xIR); + rtl::Reference< ucbhelper::InteractionContinuation > ref + = ir->getSelection(); + if(ref.is()) { + Reference<XInterface> xInt(ref.get()); + xRet.set(xInt,UNO_QUERY); + } + } + + if(!xRet.is()) { + bAborted = true; + xLockBytes->SetError(ERRCODE_ABORT); + } + + break; + } + case Moderator::ResultType::INTERACTIONREQUEST: + { + Reference<XInteractionRequest> Request; + res.result >>= Request; + xInteract->handle(Request); + pMod->setReply(Moderator::REQUESTHANDLED); + break; + } + case Moderator::ResultType::RESULT: + { + bResultAchieved = true; + break; + } + case Moderator::ResultType::COMMANDABORTED: + { + bAborted = true; + xLockBytes->SetError( ERRCODE_ABORT ); + break; + } + case Moderator::ResultType::COMMANDFAILED: + { + bAborted = true; + xLockBytes->SetError( ERRCODE_ABORT ); + break; + } + case Moderator::ResultType::INTERACTIVEIO: + { + bException = true; + if ( res.ioErrorCode == IOErrorCode_ACCESS_DENIED || + res.ioErrorCode == IOErrorCode_LOCKING_VIOLATION ) + xLockBytes->SetError( ERRCODE_IO_ACCESSDENIED ); + else if ( res.ioErrorCode == IOErrorCode_NOT_EXISTING ) + xLockBytes->SetError( ERRCODE_IO_NOTEXISTS ); + else if ( res.ioErrorCode == IOErrorCode_CANT_READ ) + xLockBytes->SetError( ERRCODE_IO_CANTREAD ); + else + xLockBytes->SetError( ERRCODE_IO_GENERAL ); + break; + } + case Moderator::ResultType::UNSUPPORTED: + { + bException = true; + xLockBytes->SetError( ERRCODE_IO_NOTSUPPORTED ); + break; + } + default: + { + bException = true; + xLockBytes->SetError( ERRCODE_IO_GENERAL ); + break; + } + } + + bResultAchieved |= bException; + bResultAchieved |= bAborted; + if(nTimeout == 5000) nTimeout *= 2; + } + + if(pMod) pMod->setReply(Moderator::EXIT); + + if ( bAborted || bException ) + { + Reference < XActiveDataSink > xActiveSink( xSink, UNO_QUERY ); + if ( xActiveSink.is() ) + xActiveSink->setInputStream( Reference < XInputStream >() ); + + Reference < XActiveDataStreamer > xStreamer( xSink, UNO_QUERY ); + if ( xStreamer.is() ) + xStreamer->setStream( Reference < XStream >() ); + } + + Reference < XActiveDataControl > xControl( xSink, UNO_QUERY ); + if ( xControl.is() ) + xControl->terminate(); + + if ( xProps.is() ) + xProps->removePropertiesChangeListener( + Sequence< OUString >(), + xListener ); + + return ( bAborted || bException ); +} + +/** + Function for opening UCB contents synchronously + */ +static bool UCBOpenContentSync_( + const UcbLockBytesRef& xLockBytes, + const Reference < XContent >& xContent, + const Command& rArg, + const Reference < XInterface >& xSink, + const Reference < XInteractionHandler >& xInteract ) +{ + ::ucbhelper::Content aContent( + xContent, new UcbTaskEnvironment( xInteract, nullptr ), + comphelper::getProcessComponentContext() ); + Reference < XContentIdentifier > xIdent = xContent->getIdentifier(); + OUString aScheme = xIdent->getContentProviderScheme(); + + // http protocol must be handled in a special way: during the opening process the input stream may change + // only the last inputstream after notifying the document headers is valid + if ( !aScheme.equalsIgnoreAsciiCase("http") ) + xLockBytes->SetStreamValid_Impl(); + + Reference< XPropertiesChangeListener > xListener = new UcbPropertiesChangeListener_Impl( xLockBytes ); + Reference< XPropertiesChangeNotifier > xProps ( xContent, UNO_QUERY ); + if ( xProps.is() ) + xProps->addPropertiesChangeListener( Sequence< OUString >(), xListener ); + + bool bException = false; + bool bAborted = false; + + try + { + aContent.executeCommand( rArg.Name, rArg.Argument ); + } + catch (const CommandAbortedException&) + { + bAborted = true; + xLockBytes->SetError( ERRCODE_ABORT ); + } + catch (const CommandFailedException&) + { + bAborted = true; + xLockBytes->SetError( ERRCODE_ABORT ); + } + catch (const InteractiveIOException& r) + { + bException = true; + if ( r.Code == IOErrorCode_ACCESS_DENIED || r.Code == IOErrorCode_LOCKING_VIOLATION ) + xLockBytes->SetError( ERRCODE_IO_ACCESSDENIED ); + else if ( r.Code == IOErrorCode_NOT_EXISTING ) + xLockBytes->SetError( ERRCODE_IO_NOTEXISTS ); + else if ( r.Code == IOErrorCode_CANT_READ ) + xLockBytes->SetError( ERRCODE_IO_CANTREAD ); + else + xLockBytes->SetError( ERRCODE_IO_GENERAL ); + } + catch (const UnsupportedDataSinkException&) + { + bException = true; + xLockBytes->SetError( ERRCODE_IO_NOTSUPPORTED ); + } + catch (const Exception&) + { + bException = true; + xLockBytes->SetError( ERRCODE_IO_GENERAL ); + } + + if ( bAborted || bException ) + { + Reference < XActiveDataSink > xActiveSink( xSink, UNO_QUERY ); + if ( xActiveSink.is() ) + xActiveSink->setInputStream( Reference < XInputStream >() ); + + Reference < XActiveDataStreamer > xStreamer( xSink, UNO_QUERY ); + if ( xStreamer.is() ) + xStreamer->setStream( Reference < XStream >() ); + } + + Reference < XActiveDataControl > xControl( xSink, UNO_QUERY ); + if ( xControl.is() ) + xControl->terminate(); + + if ( xProps.is() ) + xProps->removePropertiesChangeListener( Sequence< OUString >(), xListener ); + + return ( bAborted || bException ); +} + +UcbLockBytes::UcbLockBytes() + : m_nError( ERRCODE_NONE ) + , m_bTerminated (false) + , m_bDontClose( false ) + , m_bStreamValid (false) +{ + SetSynchronMode(); +} + +UcbLockBytes::~UcbLockBytes() +{ + if ( !m_bDontClose ) + { + if ( m_xInputStream.is() ) + { + try + { + m_xInputStream->closeInput(); + } + catch (const RuntimeException&) + { + } + catch (const IOException&) + { + } + } + } + + if ( !m_xInputStream.is() && m_xOutputStream.is() ) + { + try + { + m_xOutputStream->closeOutput(); + } + catch (const RuntimeException&) + { + } + catch (const IOException&) + { + } + } +} + +Reference < XInputStream > UcbLockBytes::getInputStream() +{ + osl::MutexGuard aGuard( m_aMutex ); + m_bDontClose = true; + return m_xInputStream; +} + +void UcbLockBytes::setStream_Impl( const Reference<XStream>& aStream ) +{ + osl::MutexGuard aGuard( m_aMutex ); + if ( aStream.is() ) + { + m_xOutputStream = aStream->getOutputStream(); + setInputStream_Impl( aStream->getInputStream(), false ); + m_xSeekable.set( aStream, UNO_QUERY ); + } + else + { + m_xOutputStream.clear(); + setInputStream_Impl( Reference < XInputStream >() ); + } +} + +bool UcbLockBytes::setInputStream_Impl( const Reference<XInputStream> &rxInputStream, bool bSetXSeekable ) +{ + bool bRet = false; + + try + { + osl::MutexGuard aGuard( m_aMutex ); + + if ( !m_bDontClose && m_xInputStream.is() ) + m_xInputStream->closeInput(); + + m_xInputStream = rxInputStream; + + if( bSetXSeekable ) + { + m_xSeekable.set( rxInputStream, UNO_QUERY ); + if( !m_xSeekable.is() && rxInputStream.is() ) + { + Reference < XComponentContext > xContext = ::comphelper::getProcessComponentContext(); + Reference< XOutputStream > rxTempOut( css::io::TempFile::create(xContext), UNO_QUERY_THROW ); + + ::comphelper::OStorageHelper::CopyInputToOutput( rxInputStream, rxTempOut ); + m_xInputStream.set( rxTempOut, UNO_QUERY ); + m_xSeekable.set( rxTempOut, UNO_QUERY ); + } + } + + bRet = m_xInputStream.is(); + } + catch (const Exception&) + { + } + + if ( m_bStreamValid && m_xInputStream.is() ) + m_aInitialized.set(); + + return bRet; +} + +void UcbLockBytes::SetStreamValid_Impl() +{ + m_bStreamValid = true; + if ( m_xInputStream.is() ) + m_aInitialized.set(); +} + +void UcbLockBytes::terminate_Impl() +{ + m_bTerminated = true; + m_aInitialized.set(); + m_aTerminated.set(); + + if ( GetError() == ERRCODE_NONE && !m_xInputStream.is() ) + { + OSL_FAIL("No InputStream, but no error set!" ); + SetError( ERRCODE_IO_NOTEXISTS ); + } +} + +ErrCode UcbLockBytes::ReadAt(sal_uInt64 const nPos, + void *pBuffer, std::size_t nCount, std::size_t *pRead) const +{ + if ( IsSynchronMode() ) + { + UcbLockBytes* pThis = const_cast < UcbLockBytes* >( this ); + pThis->m_aInitialized.wait(); + } + + Reference <XInputStream> xStream = getInputStream_Impl(); + if ( !xStream.is() ) + { + if ( m_bTerminated ) + return ERRCODE_IO_CANTREAD; + else + return ERRCODE_IO_PENDING; + } + + if ( pRead ) + *pRead = 0; + + Reference <XSeekable> xSeekable = getSeekable_Impl(); + if ( !xSeekable.is() ) + return ERRCODE_IO_CANTREAD; + + try + { + xSeekable->seek( nPos ); + } + catch (const IOException&) + { + return ERRCODE_IO_CANTSEEK; + } + catch (const css::lang::IllegalArgumentException&) + { + return ERRCODE_IO_CANTSEEK; + } + + Sequence<sal_Int8> aData; + sal_Int32 nSize; + + if(nCount > 0x7FFFFFFF) + { + nCount = 0x7FFFFFFF; + } + try + { + if ( !m_bTerminated && !IsSynchronMode() ) + { + sal_uInt64 nLen = xSeekable->getLength(); + if ( nPos + nCount > nLen ) + return ERRCODE_IO_PENDING; + } + + nSize = xStream->readBytes( aData, sal_Int32(nCount) ); + } + catch (const IOException&) + { + return ERRCODE_IO_CANTREAD; + } + + memcpy (pBuffer, aData.getConstArray(), nSize); + if (pRead) + *pRead = static_cast<std::size_t>(nSize); + + return ERRCODE_NONE; +} + +ErrCode UcbLockBytes::WriteAt(sal_uInt64 const nPos, const void *pBuffer, + std::size_t nCount, std::size_t *pWritten) +{ + if ( pWritten ) + *pWritten = 0; + + DBG_ASSERT( IsSynchronMode(), "Writing is only possible in SynchronMode!" ); + DBG_ASSERT( m_aInitialized.check(), "Writing bevor stream is ready!" ); + + Reference <XSeekable> xSeekable = getSeekable_Impl(); + Reference <XOutputStream> xOutputStream = getOutputStream_Impl(); + if ( !xOutputStream.is() || !xSeekable.is() ) + return ERRCODE_IO_CANTWRITE; + + try + { + xSeekable->seek( nPos ); + } + catch (const IOException&) + { + return ERRCODE_IO_CANTSEEK; + } + + sal_Int8 const * pData = static_cast<sal_Int8 const *>(pBuffer); + Sequence<sal_Int8> aData( pData, nCount ); + try + { + xOutputStream->writeBytes( aData ); + if ( pWritten ) + *pWritten = nCount; + } + catch (const Exception&) + { + return ERRCODE_IO_CANTWRITE; + } + + return ERRCODE_NONE; +} + +ErrCode UcbLockBytes::Flush() const +{ + Reference <XOutputStream > xOutputStream = getOutputStream_Impl(); + if ( !xOutputStream.is() ) + return ERRCODE_IO_CANTWRITE; + + try + { + xOutputStream->flush(); + } + catch (const Exception&) + { + return ERRCODE_IO_CANTWRITE; + } + + return ERRCODE_NONE; +} + +ErrCode UcbLockBytes::SetSize (sal_uInt64 const nNewSize) +{ + SvLockBytesStat aStat; + Stat( &aStat ); + std::size_t nSize = aStat.nSize; + + if ( nSize > nNewSize ) + { + Reference < XTruncate > xTrunc( getOutputStream_Impl(), UNO_QUERY ); + if ( xTrunc.is() ) + { + xTrunc->truncate(); + nSize = 0; + } + else { + SAL_INFO("unotools.ucbhelper", "Not truncable!"); + } + } + + if ( nSize < nNewSize ) + { + std::size_t nDiff = nNewSize-nSize, nCount=0; + std::unique_ptr<sal_uInt8[]> pBuffer(new sal_uInt8[ nDiff ]); + memset(pBuffer.get(), 0, nDiff); // initialize for enhanced security + WriteAt( nSize, pBuffer.get(), nDiff, &nCount ); + if ( nCount != nDiff ) + return ERRCODE_IO_CANTWRITE; + } + + return ERRCODE_NONE; +} + +ErrCode UcbLockBytes::Stat( SvLockBytesStat *pStat ) const +{ + if ( IsSynchronMode() ) + { + UcbLockBytes* pThis = const_cast < UcbLockBytes* >( this ); + pThis->m_aInitialized.wait(); + } + + if (!pStat) + return ERRCODE_IO_INVALIDPARAMETER; + + Reference <XInputStream> xStream = getInputStream_Impl(); + Reference <XSeekable> xSeekable = getSeekable_Impl(); + + if ( !xStream.is() ) + { + if ( m_bTerminated ) + return ERRCODE_IO_INVALIDACCESS; + else + return ERRCODE_IO_PENDING; + } + else if( !xSeekable.is() ) + return ERRCODE_IO_CANTTELL; + + try + { + pStat->nSize = sal_uLong(xSeekable->getLength()); + } + catch (const IOException&) + { + return ERRCODE_IO_CANTTELL; + } + + return ERRCODE_NONE; +} + +UcbLockBytesRef UcbLockBytes::CreateInputLockBytes( const Reference< XInputStream >& xInputStream ) +{ + if( !xInputStream.is() ) + return nullptr; + + UcbLockBytesRef xLockBytes = new UcbLockBytes; + xLockBytes->setDontClose_Impl(); + xLockBytes->setInputStream_Impl( xInputStream ); + xLockBytes->terminate_Impl(); + return xLockBytes; +} + +UcbLockBytesRef UcbLockBytes::CreateLockBytes( const Reference< XStream >& xStream ) +{ + if( !xStream.is() ) + return nullptr; + + UcbLockBytesRef xLockBytes = new UcbLockBytes; + xLockBytes->setDontClose_Impl(); + xLockBytes->setStream_Impl( xStream ); + xLockBytes->terminate_Impl(); + return xLockBytes; +} + +UcbLockBytesRef UcbLockBytes::CreateLockBytes( const Reference < XContent >& xContent, const Sequence < PropertyValue >& rProps, + StreamMode eOpenMode, const Reference < XInteractionHandler >& xInteractionHandler ) +{ + if( !xContent.is() ) + return nullptr; + + UcbLockBytesRef xLockBytes = new UcbLockBytes; + xLockBytes->SetSynchronMode(); + Reference< XActiveDataControl > xSink; + if ( eOpenMode & StreamMode::WRITE ) + xSink = new UcbStreamer_Impl(xLockBytes.get()); + else + xSink = new UcbDataSink_Impl(xLockBytes.get()); + + if ( rProps.hasElements() ) + { + Reference < XCommandProcessor > xProcessor( xContent, UNO_QUERY ); + Command aCommand; + aCommand.Name = "setPropertyValues"; + aCommand.Handle = -1; /* unknown */ + aCommand.Argument <<= rProps; + xProcessor->execute( aCommand, 0, Reference < XCommandEnvironment >() ); + } + + OpenCommandArgument2 aArgument; + aArgument.Sink = xSink; + aArgument.Mode = OpenMode::DOCUMENT; + + Command aCommand; + aCommand.Name = "open"; + aCommand.Argument <<= aArgument; + + bool bError = UCBOpenContentSync( xLockBytes, + xContent, + aCommand, + xSink, + xInteractionHandler ); + + if ( xLockBytes->GetError() == ERRCODE_NONE && ( bError || !xLockBytes->getInputStream().is() ) ) + { + OSL_FAIL("No InputStream, but no error set!" ); + xLockBytes->SetError( ERRCODE_IO_GENERAL ); + } + + return xLockBytes; +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/ucbhelper/ucblockbytes.hxx b/unotools/source/ucbhelper/ucblockbytes.hxx new file mode 100644 index 000000000..df1e237e9 --- /dev/null +++ b/unotools/source/ucbhelper/ucblockbytes.hxx @@ -0,0 +1,142 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#ifndef INCLUDED_UNOTOOLS_UCBLOCKBYTES_HXX +#define INCLUDED_UNOTOOLS_UCBLOCKBYTES_HXX + +#include <com/sun/star/uno/Reference.hxx> + +#include <osl/conditn.hxx> +#include <osl/mutex.hxx> +#include <tools/stream.hxx> +#include <vcl/errcode.hxx> + +namespace com +{ + namespace sun + { + namespace star + { + namespace task + { + class XInteractionHandler; + } + namespace io + { + class XStream; + class XInputStream; + class XOutputStream; + class XSeekable; + } + namespace ucb + { + class XContent; + } + namespace beans + { + struct PropertyValue; + } + } + } +} + +namespace utl +{ +class UcbLockBytes; +typedef tools::SvRef<UcbLockBytes> UcbLockBytesRef; + +class UcbLockBytes : public virtual SvLockBytes +{ + osl::Condition m_aInitialized; + osl::Condition m_aTerminated; + osl::Mutex m_aMutex; + + css::uno::Reference < css::io::XInputStream > m_xInputStream; + css::uno::Reference < css::io::XOutputStream > m_xOutputStream; + css::uno::Reference < css::io::XSeekable > m_xSeekable; + + ErrCode m_nError; + + bool m_bTerminated; + bool m_bDontClose; + bool m_bStreamValid; + + UcbLockBytes(); +protected: + virtual ~UcbLockBytes() override; + +public: + // properties: Referer, PostMimeType + static UcbLockBytesRef CreateLockBytes( const css::uno::Reference < css::ucb::XContent >& xContent, + const css::uno::Sequence < css::beans::PropertyValue >& rProps, + StreamMode eMode, + const css::uno::Reference < css::task::XInteractionHandler >& xInter ); + + static UcbLockBytesRef CreateInputLockBytes( const css::uno::Reference < css::io::XInputStream >& xContent ); + static UcbLockBytesRef CreateLockBytes( const css::uno::Reference < css::io::XStream >& xContent ); + + // SvLockBytes + virtual ErrCode ReadAt(sal_uInt64 nPos, void *pBuffer, std::size_t nCount, std::size_t *pRead) const override; + virtual ErrCode WriteAt(sal_uInt64, const void*, std::size_t, std::size_t *pWritten) override; + virtual ErrCode Flush() const override; + virtual ErrCode SetSize(sal_uInt64) override; + virtual ErrCode Stat ( SvLockBytesStat *pStat ) const override; + + void SetError( ErrCode nError ) + { m_nError = nError; } + + ErrCode const & GetError() const + { return m_nError; } + + // calling this method delegates the responsibility to call closeinput to the caller! + css::uno::Reference < css::io::XInputStream > getInputStream(); + + bool setInputStream_Impl( const css::uno::Reference < css::io::XInputStream > &rxInputStream, + bool bSetXSeekable = true ); + void setStream_Impl( const css::uno::Reference < css::io::XStream > &rxStream ); + void terminate_Impl(); + + css::uno::Reference < css::io::XInputStream > getInputStream_Impl() const + { + osl::MutexGuard aGuard( const_cast< UcbLockBytes* >(this)->m_aMutex ); + return m_xInputStream; + } + + css::uno::Reference < css::io::XOutputStream > getOutputStream_Impl() const + { + osl::MutexGuard aGuard( const_cast< UcbLockBytes* >(this)->m_aMutex ); + return m_xOutputStream; + } + + css::uno::Reference < css::io::XSeekable > getSeekable_Impl() const + { + osl::MutexGuard aGuard( const_cast< UcbLockBytes* >(this)->m_aMutex ); + return m_xSeekable; + } + + void setDontClose_Impl() + { m_bDontClose = true; } + + void SetStreamValid_Impl(); +}; + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/ucbhelper/ucbstreamhelper.cxx b/unotools/source/ucbhelper/ucbstreamhelper.cxx new file mode 100644 index 000000000..6f72c00bc --- /dev/null +++ b/unotools/source/ucbhelper/ucbstreamhelper.cxx @@ -0,0 +1,238 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <rtl/ustring.hxx> +#include <unotools/ucbstreamhelper.hxx> +#include <comphelper/processfactory.hxx> +#include <com/sun/star/task/InteractionHandler.hpp> +#include <com/sun/star/ucb/ContentCreationException.hpp> +#include <com/sun/star/ucb/CommandAbortedException.hpp> +#include <com/sun/star/ucb/InsertCommandArgument.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> + +#include <comphelper/simplefileaccessinteraction.hxx> +#include <ucbhelper/content.hxx> +#include <unotools/streamwrap.hxx> +#include "ucblockbytes.hxx" + +namespace com::sun::star::ucb { class XCommandEnvironment; } + +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::io; +using namespace ::com::sun::star::ucb; +using namespace ::com::sun::star::task; +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::beans; + +namespace utl +{ + +static std::unique_ptr<SvStream> lcl_CreateStream( const OUString& rFileName, StreamMode eOpenMode, + const Reference < XInteractionHandler >& xInteractionHandler, + bool bEnsureFileExists ) +{ + std::unique_ptr<SvStream> pStream; + UcbLockBytesRef xLockBytes; + if ( eOpenMode & StreamMode::WRITE ) + { + bool bTruncate = bool( eOpenMode & StreamMode::TRUNC ); + if ( bTruncate ) + { + try + { + // truncate is implemented with deleting the original file + ::ucbhelper::Content aCnt( + rFileName, Reference < XCommandEnvironment >(), + comphelper::getProcessComponentContext() ); + aCnt.executeCommand( "delete", css::uno::makeAny( true ) ); + } + + catch ( const CommandAbortedException& ) + { + // couldn't truncate/delete + } + catch ( const ContentCreationException& ) + { + } + catch ( const Exception& ) + { + } + } + + if ( bEnsureFileExists || bTruncate ) + { + try + { + // make sure that the desired file exists before trying to open + SvMemoryStream aStream(0,0); + ::utl::OInputStreamWrapper* pInput = new ::utl::OInputStreamWrapper( aStream ); + Reference< XInputStream > xInput( pInput ); + + ::ucbhelper::Content aContent( + rFileName, Reference < XCommandEnvironment >(), + comphelper::getProcessComponentContext() ); + InsertCommandArgument aInsertArg; + aInsertArg.Data = xInput; + + aInsertArg.ReplaceExisting = false; + Any aCmdArg; + aCmdArg <<= aInsertArg; + aContent.executeCommand( "insert", aCmdArg ); + } + + // it is NOT an error when the stream already exists and no truncation was desired + catch ( const CommandAbortedException& ) + { + // currently never an error is detected ! + } + catch ( const ContentCreationException& ) + { + } + catch ( const Exception& ) + { + } + } + } + + try + { + // create LockBytes using UCB + ::ucbhelper::Content aContent( + rFileName, Reference < XCommandEnvironment >(), + comphelper::getProcessComponentContext() ); + xLockBytes = UcbLockBytes::CreateLockBytes( aContent.get(), Sequence < PropertyValue >(), + eOpenMode, xInteractionHandler ); + if ( xLockBytes.is() ) + { + pStream.reset( new SvStream( xLockBytes.get() ) ); + pStream->SetBufferSize( 4096 ); + pStream->SetError( xLockBytes->GetError() ); + } + } + catch ( const CommandAbortedException& ) + { + } + catch ( const ContentCreationException& ) + { + } + catch ( const Exception& ) + { + } + + return pStream; +} + +std::unique_ptr<SvStream> UcbStreamHelper::CreateStream(const OUString& rFileName, StreamMode eOpenMode, css::uno::Reference<css::awt::XWindow> xParentWin) +{ + // related tdf#99312 + // create a specialized interaction handler to manages Web certificates and Web credentials when needed + Reference< XInteractionHandler > xIH( + css::task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), xParentWin)); + Reference<XInteractionHandler> xIHScoped(new comphelper::SimpleFileAccessInteraction(xIH)); + + return lcl_CreateStream( rFileName, eOpenMode, xIHScoped, true /* bEnsureFileExists */ ); +} + +std::unique_ptr<SvStream> UcbStreamHelper::CreateStream(const OUString& rFileName, StreamMode eOpenMode, + bool bFileExists, css::uno::Reference<css::awt::XWindow> xParentWin) +{ + // related tdf#99312 + // create a specialized interaction handler to manages Web certificates and Web credentials when needed + Reference< XInteractionHandler > xIH( + css::task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), xParentWin)); + Reference<XInteractionHandler> xIHScoped(new comphelper::SimpleFileAccessInteraction(xIH)); + return lcl_CreateStream( rFileName, eOpenMode, xIHScoped,!bFileExists ); +} + + +std::unique_ptr<SvStream> UcbStreamHelper::CreateStream( const Reference < XInputStream >& xStream ) +{ + std::unique_ptr<SvStream> pStream; + UcbLockBytesRef xLockBytes = UcbLockBytes::CreateInputLockBytes( xStream ); + if ( xLockBytes.is() ) + { + pStream.reset( new SvStream( xLockBytes.get() ) ); + pStream->SetBufferSize( 4096 ); + pStream->SetError( xLockBytes->GetError() ); + } + + return pStream; +} + +std::unique_ptr<SvStream> UcbStreamHelper::CreateStream( const Reference < XStream >& xStream ) +{ + std::unique_ptr<SvStream> pStream; + if ( xStream->getOutputStream().is() ) + { + UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream ); + if ( xLockBytes.is() ) + { + pStream.reset( new SvStream( xLockBytes.get() ) ); + pStream->SetBufferSize( 4096 ); + pStream->SetError( xLockBytes->GetError() ); + } + } + else + return CreateStream( xStream->getInputStream() ); + + return pStream; +} + +std::unique_ptr<SvStream> UcbStreamHelper::CreateStream( const Reference < XInputStream >& xStream, bool bCloseStream ) +{ + std::unique_ptr<SvStream> pStream; + UcbLockBytesRef xLockBytes = UcbLockBytes::CreateInputLockBytes( xStream ); + if ( xLockBytes.is() ) + { + if ( !bCloseStream ) + xLockBytes->setDontClose_Impl(); + + pStream.reset( new SvStream( xLockBytes.get() ) ); + pStream->SetBufferSize( 4096 ); + pStream->SetError( xLockBytes->GetError() ); + } + + return pStream; +}; + +std::unique_ptr<SvStream> UcbStreamHelper::CreateStream( const Reference < XStream >& xStream, bool bCloseStream ) +{ + std::unique_ptr<SvStream> pStream; + if ( xStream->getOutputStream().is() ) + { + UcbLockBytesRef xLockBytes = UcbLockBytes::CreateLockBytes( xStream ); + if ( xLockBytes.is() ) + { + if ( !bCloseStream ) + xLockBytes->setDontClose_Impl(); + + pStream.reset( new SvStream( xLockBytes.get() ) ); + pStream->SetBufferSize( 4096 ); + pStream->SetError( xLockBytes->GetError() ); + } + } + else + return CreateStream( xStream->getInputStream(), bCloseStream ); + + return pStream; +}; + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/ucbhelper/xtempfile.cxx b/unotools/source/ucbhelper/xtempfile.cxx new file mode 100644 index 000000000..fe47394d9 --- /dev/null +++ b/unotools/source/ucbhelper/xtempfile.cxx @@ -0,0 +1,449 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include "XTempFile.hxx" +#include <unotoolsservices.hxx> +#include <com/sun/star/io/BufferSizeExceededException.hpp> +#include <com/sun/star/io/NotConnectedException.hpp> +#include <com/sun/star/beans/PropertyAttribute.hpp> +#include <cppuhelper/typeprovider.hxx> +#include <comphelper/servicedecl.hxx> +#include <o3tl/safeint.hxx> +#include <unotools/tempfile.hxx> +#include <cppuhelper/propshlp.hxx> + +OTempFileService::OTempFileService(css::uno::Reference< css::uno::XComponentContext > const &) +: mpStream( nullptr ) +, mbRemoveFile( true ) +, mbInClosed( false ) +, mbOutClosed( false ) +, mnCachedPos( 0 ) +, mbHasCachedPos( false ) + +{ + mpTempFile.reset(new utl::TempFile()); + mpTempFile->EnableKillingFile(); +} + +OTempFileService::~OTempFileService () +{ +} + +// XTypeProvider + +css::uno::Sequence< css::uno::Type > SAL_CALL OTempFileService::getTypes( ) +{ + static ::cppu::OTypeCollection ourTypeCollection( + cppu::UnoType<css::beans::XPropertySet>::get() + ,OTempFileBase::getTypes() ); + + return ourTypeCollection.getTypes(); +}; + +// XTempFile + +sal_Bool SAL_CALL OTempFileService::getRemoveFile() +{ + ::osl::MutexGuard aGuard( maMutex ); + + if ( !mpTempFile ) + { + // the stream is already disconnected + throw css::uno::RuntimeException("Not connected to a file."); + } + + return mbRemoveFile; +}; +void SAL_CALL OTempFileService::setRemoveFile( sal_Bool _removefile ) +{ + ::osl::MutexGuard aGuard( maMutex ); + + if ( !mpTempFile ) + { + // the stream is already disconnected + throw css::uno::RuntimeException("Not connected to a file."); + } + + mbRemoveFile = _removefile; + mpTempFile->EnableKillingFile( mbRemoveFile ); +}; +OUString SAL_CALL OTempFileService::getUri() +{ + ::osl::MutexGuard aGuard( maMutex ); + + if ( !mpTempFile ) + { + throw css::uno::RuntimeException("Not connected to a file."); + } + + return mpTempFile->GetURL(); + +}; +OUString SAL_CALL OTempFileService::getResourceName() +{ + ::osl::MutexGuard aGuard( maMutex ); + + if ( !mpTempFile ) + { + throw css::uno::RuntimeException("Not connected to a file."); +} + + return mpTempFile->GetFileName(); +}; + +// XInputStream + +sal_Int32 SAL_CALL OTempFileService::readBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) +{ + ::osl::MutexGuard aGuard( maMutex ); + if ( mbInClosed ) + throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak * > (this ) ); + + checkConnected(); + if (nBytesToRead < 0) + throw css::io::BufferSizeExceededException( OUString(), static_cast< css::uno::XWeak * >(this)); + + if (aData.getLength() < nBytesToRead) + aData.realloc(nBytesToRead); + + sal_uInt32 nRead = mpStream->ReadBytes(static_cast<void*>(aData.getArray()), nBytesToRead); + checkError(); + + if (nRead < o3tl::make_unsigned(aData.getLength())) + aData.realloc( nRead ); + + if ( sal::static_int_cast<sal_uInt32>(nBytesToRead) > nRead ) + { + // usually that means that the stream was read till the end + // TODO/LATER: it is better to get rid of this optimization by avoiding using of multiple temporary files ( there should be only one temporary file? ) + mnCachedPos = mpStream->Tell(); + mbHasCachedPos = true; + + mpStream = nullptr; + if ( mpTempFile ) + mpTempFile->CloseStream(); + } + + return nRead; +} +sal_Int32 SAL_CALL OTempFileService::readSomeBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) +{ + ::osl::MutexGuard aGuard( maMutex ); + if ( mbInClosed ) + throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak * > (this ) ); + + checkConnected(); + checkError(); + + if (nMaxBytesToRead < 0) + throw css::io::BufferSizeExceededException( OUString(), static_cast < css::uno::XWeak * >( this ) ); + + if (mpStream->eof()) + { + aData.realloc(0); + return 0; + } + else + return readBytes(aData, nMaxBytesToRead); +} +void SAL_CALL OTempFileService::skipBytes( sal_Int32 nBytesToSkip ) +{ + ::osl::MutexGuard aGuard( maMutex ); + if ( mbInClosed ) + throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak * > (this ) ); + + checkConnected(); + checkError(); + mpStream->SeekRel(nBytesToSkip); + checkError(); +} +sal_Int32 SAL_CALL OTempFileService::available( ) +{ + ::osl::MutexGuard aGuard( maMutex ); + if ( mbInClosed ) + throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak * > (this ) ); + + checkConnected(); + + sal_Int64 nAvailable = mpStream->remainingSize(); + checkError(); + + return std::min<sal_Int64>(SAL_MAX_INT32, nAvailable); +} +void SAL_CALL OTempFileService::closeInput( ) +{ + ::osl::MutexGuard aGuard( maMutex ); + if ( mbInClosed ) + throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak * > (this ) ); + + mbInClosed = true; + + if ( mbOutClosed ) + { + // stream will be deleted by TempFile implementation + mpStream = nullptr; + mpTempFile.reset(); + } +} + +// XOutputStream + +void SAL_CALL OTempFileService::writeBytes( const css::uno::Sequence< sal_Int8 >& aData ) +{ + ::osl::MutexGuard aGuard( maMutex ); + if ( mbOutClosed ) + throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak * > (this ) ); + + checkConnected(); + sal_uInt32 nWritten = mpStream->WriteBytes(aData.getConstArray(), aData.getLength()); + checkError(); + if ( nWritten != static_cast<sal_uInt32>(aData.getLength())) + throw css::io::BufferSizeExceededException( OUString(),static_cast < css::uno::XWeak * > ( this ) ); +} +void SAL_CALL OTempFileService::flush( ) +{ + ::osl::MutexGuard aGuard( maMutex ); + if ( mbOutClosed ) + throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak * > (this ) ); + + checkConnected(); + mpStream->Flush(); + checkError(); +} +void SAL_CALL OTempFileService::closeOutput( ) +{ + ::osl::MutexGuard aGuard( maMutex ); + if ( mbOutClosed ) + throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak * > (this ) ); + + mbOutClosed = true; + + // TODO/LATER: it is better to get rid of this optimization by avoiding using of multiple temporary files ( there should be only one temporary file? ) + if ( mpStream ) + { + mnCachedPos = mpStream->Tell(); + mbHasCachedPos = true; + + mpStream = nullptr; + if ( mpTempFile ) + mpTempFile->CloseStream(); + } + + if ( mbInClosed ) + { + // stream will be deleted by TempFile implementation + mpStream = nullptr; + mpTempFile.reset(); + } +} + +void OTempFileService::checkError () const +{ + if (!mpStream || mpStream->SvStream::GetError () != ERRCODE_NONE ) + throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) ); +} +void OTempFileService::checkConnected () +{ + if (!mpStream && mpTempFile) + { + mpStream = mpTempFile->GetStream( StreamMode::STD_READWRITE ); + if ( mpStream && mbHasCachedPos ) + { + mpStream->Seek( sal::static_int_cast<std::size_t>(mnCachedPos) ); + if ( mpStream->SvStream::GetError () == ERRCODE_NONE ) + { + mbHasCachedPos = false; + mnCachedPos = 0; + } + else + { + mpStream = nullptr; + mpTempFile->CloseStream(); + } + } + } + + if (!mpStream) + throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak * > (this ) ); +} + +// XSeekable + +void SAL_CALL OTempFileService::seek( sal_Int64 nLocation ) +{ + ::osl::MutexGuard aGuard( maMutex ); + checkConnected(); + if ( nLocation < 0 || nLocation > getLength() ) + throw css::lang::IllegalArgumentException(); + + mpStream->Seek(static_cast<sal_uInt32>(nLocation) ); + checkError(); +} +sal_Int64 SAL_CALL OTempFileService::getPosition( ) +{ + ::osl::MutexGuard aGuard( maMutex ); + checkConnected(); + + sal_uInt32 nPos = mpStream->Tell(); + checkError(); + return static_cast<sal_Int64>(nPos); +} +sal_Int64 SAL_CALL OTempFileService::getLength( ) +{ + ::osl::MutexGuard aGuard( maMutex ); + checkConnected(); + + checkError(); + + sal_Int64 nEndPos = mpStream->TellEnd(); + + return nEndPos; +} + +// XStream + +css::uno::Reference< css::io::XInputStream > SAL_CALL OTempFileService::getInputStream() + { + return css::uno::Reference< css::io::XInputStream >( *this, css::uno::UNO_QUERY ); +} + +css::uno::Reference< css::io::XOutputStream > SAL_CALL OTempFileService::getOutputStream() + { + return css::uno::Reference< css::io::XOutputStream >( *this, css::uno::UNO_QUERY ); + } + +// XTruncate + +void SAL_CALL OTempFileService::truncate() +{ + ::osl::MutexGuard aGuard( maMutex ); + checkConnected(); + // SetStreamSize() call does not change the position + mpStream->Seek( 0 ); + mpStream->SetStreamSize( 0 ); + checkError(); +} + +#define PROPERTY_HANDLE_URI 1 +#define PROPERTY_HANDLE_REMOVE_FILE 2 +#define PROPERTY_HANDLE_RESOURCE_NAME 3 + +// XPropertySet +::css::uno::Reference< ::css::beans::XPropertySetInfo > OTempFileService::getPropertySetInfo() +{ + // Create a table that map names to index values. + // attention: properties need to be sorted by name! + static cppu::OPropertyArrayHelper ourPropertyInfo( + { + css::beans::Property( "Uri", PROPERTY_HANDLE_URI, cppu::UnoType<OUString>::get(), + css::beans::PropertyAttribute::READONLY ), + css::beans::Property( "RemoveFile", PROPERTY_HANDLE_REMOVE_FILE, cppu::UnoType<bool>::get(), + 0 ), + css::beans::Property( "ResourceName", PROPERTY_HANDLE_RESOURCE_NAME, cppu::UnoType<OUString>::get(), + css::beans::PropertyAttribute::READONLY ) + }, + true ); + static css::uno::Reference< css::beans::XPropertySetInfo > xInfo( + ::cppu::OPropertySetHelper::createPropertySetInfo( ourPropertyInfo ) ); + return xInfo; +} +void OTempFileService::setPropertyValue( const ::rtl::OUString& aPropertyName, const ::css::uno::Any& aValue ) +{ + if ( aPropertyName == "RemoveFile" ) + setRemoveFile( aValue.get<bool>() ); + else + { + assert(false); + throw css::beans::UnknownPropertyException(aPropertyName); + } +} +::css::uno::Any OTempFileService::getPropertyValue( const ::rtl::OUString& aPropertyName ) +{ + if ( aPropertyName == "RemoveFile" ) + return css::uno::Any(getRemoveFile()); + else if ( aPropertyName == "ResourceName" ) + return css::uno::Any(getResourceName()); + else if ( aPropertyName == "Uri" ) + return css::uno::Any(getUri()); + else + { + assert(false); + throw css::beans::UnknownPropertyException(aPropertyName); + } +} +void OTempFileService::addPropertyChangeListener( const ::rtl::OUString& /*aPropertyName*/, const ::css::uno::Reference< ::css::beans::XPropertyChangeListener >& /*xListener*/ ) +{ + assert(false); +} +void OTempFileService::removePropertyChangeListener( const ::rtl::OUString& /*aPropertyName*/, const ::css::uno::Reference< ::css::beans::XPropertyChangeListener >& /*xListener*/ ) +{ + assert(false); +} +void OTempFileService::addVetoableChangeListener( const ::rtl::OUString& /*aPropertyName*/, const ::css::uno::Reference< ::css::beans::XVetoableChangeListener >& /*xListener*/ ) +{ + assert(false); +} +void OTempFileService::removeVetoableChangeListener( const ::rtl::OUString& /*aPropertyName*/, const ::css::uno::Reference< ::css::beans::XVetoableChangeListener >& /*xListener*/ ) +{ + assert(false); +} +// XFastPropertySet +void OTempFileService::setFastPropertyValue( ::sal_Int32 nHandle, const ::css::uno::Any& aValue ) +{ + switch (nHandle) + { + case PROPERTY_HANDLE_REMOVE_FILE: setRemoveFile( aValue.get<bool>() ); return; + } + assert(false); + throw css::beans::UnknownPropertyException(OUString::number(nHandle)); +} +::css::uno::Any OTempFileService::getFastPropertyValue( ::sal_Int32 nHandle ) +{ + switch (nHandle) + { + case PROPERTY_HANDLE_REMOVE_FILE: return css::uno::Any(getRemoveFile()); + case PROPERTY_HANDLE_RESOURCE_NAME: return css::uno::Any(getResourceName()); + case PROPERTY_HANDLE_URI: return css::uno::Any(getUri()); + } + assert(false); + throw css::beans::UnknownPropertyException(OUString::number(nHandle)); +} +// XPropertyAccess +::css::uno::Sequence< ::css::beans::PropertyValue > OTempFileService::getPropertyValues() +{ + return { + css::beans::PropertyValue("Uri", PROPERTY_HANDLE_URI, css::uno::Any(getUri()), css::beans::PropertyState_DEFAULT_VALUE), + css::beans::PropertyValue("RemoveFile", PROPERTY_HANDLE_REMOVE_FILE, css::uno::Any(getRemoveFile()), css::beans::PropertyState_DEFAULT_VALUE), + css::beans::PropertyValue("ResourceName", PROPERTY_HANDLE_RESOURCE_NAME, css::uno::Any(getResourceName()), css::beans::PropertyState_DEFAULT_VALUE) + }; +} +void OTempFileService::setPropertyValues( const ::css::uno::Sequence< ::css::beans::PropertyValue >& aProps ) +{ + for ( auto const & rPropVal : aProps ) + setPropertyValue( rPropVal.Name, rPropVal.Value ); +} + +namespace sdecl = ::comphelper::service_decl; +sdecl::class_< OTempFileService> const OTempFileServiceImpl; +const sdecl::ServiceDecl OTempFileServiceDecl( + OTempFileServiceImpl, + "com.sun.star.io.comp.TempFile", + "com.sun.star.io.TempFile"); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/util/utl.component b/unotools/util/utl.component new file mode 100644 index 000000000..8b9371343 --- /dev/null +++ b/unotools/util/utl.component @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * This file is part of the LibreOffice project. + * + * 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + --> + +<component loader="com.sun.star.loader.SharedLibrary" environment="@CPPU_ENV@" + prefix="utl" xmlns="http://openoffice.org/2010/uno-components"> + <implementation name="com.sun.star.io.comp.TempFile"> + <service name="com.sun.star.io.TempFile"/> + </implementation> + <implementation name="com.sun.star.comp.unotools.misc.ServiceDocumenter"> + <singleton name="com.sun.star.util.theServiceDocumenter"/> + </implementation> +</component> |