1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_comphelper.hxx"
26 
27 #include "comphelper_module.hxx"
28 
29 #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
30 #include <com/sun/star/i18n/XCollator.hpp>
31 #include <com/sun/star/lang/Locale.hpp>
32 #include <com/sun/star/uno/Sequence.h>
33 #include <com/sun/star/beans/PropertyValue.hpp>
34 #include <cppuhelper/implbase3.hxx>
35 #include <cppuhelper/implbase1.hxx>
36 #include <com/sun/star/lang/XServiceInfo.hpp>
37 #include <com/sun/star/lang/XInitialization.hpp>
38 #include <com/sun/star/lang/IllegalArgumentException.hpp>
39 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
40 #include <comphelper/stl_types.hxx>
41 #include <map>
42 
43 
44 using namespace com::sun::star::uno;
45 using namespace com::sun::star::ucb;
46 using namespace com::sun::star::lang;
47 using namespace com::sun::star::i18n;
48 using namespace rtl;
49 
50 //=============================================================================
51 
52 class AnyCompare : public ::cppu::WeakImplHelper1< XAnyCompare >
53 {
54 	Reference< XCollator > m_rCollator;
55 
56 public:
AnyCompare(Reference<XComponentContext> xContext,const Locale & rLocale)57     AnyCompare( Reference< XComponentContext > xContext, const Locale& rLocale ) throw()
58     {
59         Reference< XMultiComponentFactory > xFactory = xContext->getServiceManager();
60         if ( xFactory.is() )
61 	{
62 		m_rCollator = Reference< XCollator >(
63                 xFactory->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.i18n.Collator" ), xContext ),
64 						UNO_QUERY );
65 		m_rCollator->loadDefaultCollator( rLocale,
66 										  0 ); //???
67         }
68 
69 	}
70 
71 	virtual sal_Int16 SAL_CALL compare( const Any& any1, const Any& any2 ) throw(RuntimeException);
72 };
73 
74 //=============================================================================
75 
76 class AnyCompareFactory : public cppu::WeakImplHelper3< XAnyCompareFactory, XInitialization, XServiceInfo >
77 {
78 	Reference< XAnyCompare > 			m_rAnyCompare;
79     Reference< XComponentContext >      m_rContext;
80 	Locale							  	m_Locale;
81 
82 public:
AnyCompareFactory(Reference<XComponentContext> xContext)83     AnyCompareFactory( Reference< XComponentContext > xContext ) : m_rContext( xContext )
84     {}
85 
86 	// XAnyCompareFactory
87 	virtual Reference< XAnyCompare > SAL_CALL createAnyCompareByName ( const OUString& aPropertyName ) throw(::com::sun::star::uno::RuntimeException);
88 
89 	// XInitialization
90     virtual void SAL_CALL initialize( const Sequence< Any >& aArguments )
91 			throw ( Exception, RuntimeException );
92 
93 	// XServiceInfo
94 	virtual OUString SAL_CALL getImplementationName(  ) throw(RuntimeException);
95 	virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException);
96 	virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) throw(RuntimeException);
97 
98     // XServiceInfo - static versions (used for component registration)
99     static ::rtl::OUString SAL_CALL getImplementationName_static();
100     static Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
101     static Reference< XInterface > SAL_CALL Create( const Reference< XComponentContext >& );
102 };
103 
104 //===========================================================================================
105 
compare(const Any & any1,const Any & any2)106 sal_Int16 SAL_CALL AnyCompare::compare( const Any& any1, const Any& any2 ) throw(::com::sun::star::uno::RuntimeException)
107 {
108 	sal_Int16 aResult = 0;
109 
110 	if( m_rCollator.is() )
111 	{
112 		OUString aStr1;
113 		OUString aStr2;
114 
115 		any1 >>= aStr1;
116 		any2 >>= aStr2;
117 
118 		aResult = ( sal_Int16 )m_rCollator->compareString( aStr1, aStr2 );
119 	}
120 
121 	return aResult;
122 }
123 
124 //===========================================================================================
125 
createAnyCompareByName(const OUString & aPropertyName)126 Reference< XAnyCompare > SAL_CALL AnyCompareFactory::createAnyCompareByName( const OUString& aPropertyName ) throw(::com::sun::star::uno::RuntimeException)
127 {
128 	// for now only OUString properties compare is implemented
129 	// so no check for the property name is done
130 
131 	if( aPropertyName.equals( OUString::createFromAscii( "Title" ) ) )
132 		return m_rAnyCompare;
133 
134 	return Reference< XAnyCompare >();
135 }
136 
initialize(const Sequence<Any> & aArguments)137 void SAL_CALL AnyCompareFactory::initialize( const Sequence< Any >& aArguments ) throw ( Exception, RuntimeException )
138 {
139 	if( aArguments.getLength() )
140 	{
141 		if( aArguments[0] >>= m_Locale )
142 		{
143             m_rAnyCompare = new AnyCompare( m_rContext, m_Locale );
144 			return;
145 		}
146 	}
147 
148 }
149 
getImplementationName()150 OUString SAL_CALL AnyCompareFactory::getImplementationName(  ) throw( RuntimeException )
151 {
152 	return getImplementationName_static();
153 }
154 
getImplementationName_static()155 OUString SAL_CALL AnyCompareFactory::getImplementationName_static(  )
156 {
157 	return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "AnyCompareFactory" ) );
158 }
159 
supportsService(const OUString & ServiceName)160 sal_Bool SAL_CALL AnyCompareFactory::supportsService( const OUString& ServiceName ) throw(RuntimeException)
161 {
162 	rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ucb.AnyCompareFactory" ) );
163 	return aServiceName == ServiceName;
164 }
165 
getSupportedServiceNames()166 Sequence< OUString > SAL_CALL AnyCompareFactory::getSupportedServiceNames(  ) throw(RuntimeException)
167 {
168 	return getSupportedServiceNames_static();
169 }
170 
getSupportedServiceNames_static()171 Sequence< OUString > SAL_CALL AnyCompareFactory::getSupportedServiceNames_static(  )
172 {
173 	const rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ucb.AnyCompareFactory" ) );
174 	const Sequence< rtl::OUString > aSeq( &aServiceName, 1 );
175 	return aSeq;
176 }
177 
Create(const Reference<XComponentContext> & rxContext)178 Reference< XInterface > SAL_CALL AnyCompareFactory::Create(
179                 const Reference< XComponentContext >& rxContext )
180 {
181     return (cppu::OWeakObject*)new AnyCompareFactory( rxContext );
182 }
183 
createRegistryInfo_AnyCompareFactory()184 void createRegistryInfo_AnyCompareFactory()
185 {
186     static ::comphelper::module::OAutoRegistration< AnyCompareFactory > aAutoRegistration;
187 }
188