1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <com/sun/star/lang/NoSupportException.hpp>
21 #include <com/sun/star/uno/XComponentContext.hpp>
22 #include <cppuhelper/supportsservice.hxx>
23 #include <textconversionImpl.hxx>
24 #include <localedata.hxx>
25 
26 using namespace com::sun::star::lang;
27 using namespace ::com::sun::star::i18n;
28 using namespace com::sun::star::uno;
29 
30 namespace i18npool {
31 
32 TextConversionResult SAL_CALL
getConversions(const OUString & aText,sal_Int32 nStartPos,sal_Int32 nLength,const Locale & rLocale,sal_Int16 nConversionType,sal_Int32 nConversionOptions)33 TextConversionImpl::getConversions( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
34     const Locale& rLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
35 {
36     getLocaleSpecificTextConversion(rLocale);
37 
38     sal_Int32 len = aText.getLength() - nStartPos;
39     if (nLength > len)
40         nLength = std::max<sal_Int32>(len, 0);
41     return xTC->getConversions(aText, nStartPos, nLength, rLocale, nConversionType, nConversionOptions);
42 }
43 
44 OUString SAL_CALL
getConversion(const OUString & aText,sal_Int32 nStartPos,sal_Int32 nLength,const Locale & rLocale,sal_Int16 nConversionType,sal_Int32 nConversionOptions)45 TextConversionImpl::getConversion( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
46     const Locale& rLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions)
47 {
48     getLocaleSpecificTextConversion(rLocale);
49 
50     sal_Int32 len = aText.getLength() - nStartPos;
51     if (nLength > len)
52         nLength = std::max<sal_Int32>(len, 0);
53     return xTC->getConversion(aText, nStartPos, nLength, rLocale, nConversionType, nConversionOptions);
54 }
55 
56 OUString SAL_CALL
getConversionWithOffset(const OUString & aText,sal_Int32 nStartPos,sal_Int32 nLength,const Locale & rLocale,sal_Int16 nConversionType,sal_Int32 nConversionOptions,Sequence<sal_Int32> & offset)57 TextConversionImpl::getConversionWithOffset( const OUString& aText, sal_Int32 nStartPos, sal_Int32 nLength,
58     const Locale& rLocale, sal_Int16 nConversionType, sal_Int32 nConversionOptions, Sequence< sal_Int32>& offset)
59 {
60     getLocaleSpecificTextConversion(rLocale);
61 
62     sal_Int32 len = aText.getLength() - nStartPos;
63     if (nLength > len)
64         nLength = std::max<sal_Int32>(len, 0);
65     return xTC->getConversionWithOffset(aText, nStartPos, nLength, rLocale, nConversionType, nConversionOptions, offset);
66 }
67 
68 sal_Bool SAL_CALL
interactiveConversion(const Locale & rLocale,sal_Int16 nTextConversionType,sal_Int32 nTextConversionOptions)69 TextConversionImpl::interactiveConversion( const Locale& rLocale, sal_Int16 nTextConversionType, sal_Int32 nTextConversionOptions )
70 {
71     getLocaleSpecificTextConversion(rLocale);
72 
73     return xTC->interactiveConversion(rLocale, nTextConversionType, nTextConversionOptions);
74 }
75 
76 void
getLocaleSpecificTextConversion(const Locale & rLocale)77 TextConversionImpl::getLocaleSpecificTextConversion(const Locale& rLocale)
78 {
79     if (rLocale != aLocale) {
80         aLocale = rLocale;
81 
82         OUString aPrefix("com.sun.star.i18n.TextConversion_");
83         Reference < XInterface > xI = m_xContext->getServiceManager()->createInstanceWithContext(
84                 aPrefix + LocaleDataImpl::getFirstLocaleServiceName( aLocale), m_xContext);
85         if (!xI.is())
86         {
87             ::std::vector< OUString > aFallbacks( LocaleDataImpl::getFallbackLocaleServiceNames( aLocale));
88             for (auto const& fallback : aFallbacks)
89             {
90                 xI = m_xContext->getServiceManager()->createInstanceWithContext( aPrefix + fallback, m_xContext);
91                 if (xI.is())
92                     break;
93             }
94         }
95         if (xI.is())
96             xTC.set( xI, UNO_QUERY );
97         else if (xTC.is())
98             xTC.clear();
99     }
100     if (! xTC.is())
101         throw NoSupportException(); // aLocale is not supported
102 }
103 
104 OUString SAL_CALL
getImplementationName()105 TextConversionImpl::getImplementationName()
106 {
107     return "com.sun.star.i18n.TextConversion";
108 }
109 
110 sal_Bool SAL_CALL
supportsService(const OUString & rServiceName)111 TextConversionImpl::supportsService(const OUString& rServiceName)
112 {
113     return cppu::supportsService(this, rServiceName);
114 }
115 
116 Sequence< OUString > SAL_CALL
getSupportedServiceNames()117 TextConversionImpl::getSupportedServiceNames()
118 {
119     return { "com.sun.star.i18n.TextConversion" };
120 }
121 
122 }
123 
124 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
com_sun_star_i18n_TextConversion_get_implementation(css::uno::XComponentContext * context,css::uno::Sequence<css::uno::Any> const &)125 com_sun_star_i18n_TextConversion_get_implementation(
126     css::uno::XComponentContext *context,
127     css::uno::Sequence<css::uno::Any> const &)
128 {
129     return cppu::acquire(new i18npool::TextConversionImpl(context));
130 }
131 
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
133