1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 
31 #include "comphelper_module.hxx"
32 
33 #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
34 #include <com/sun/star/i18n/XCollator.hpp>
35 #include <com/sun/star/lang/Locale.hpp>
36 #include <com/sun/star/uno/Sequence.h>
37 #include <com/sun/star/beans/PropertyValue.hpp>
38 #include <cppuhelper/implbase3.hxx>
39 #include <cppuhelper/implbase1.hxx>
40 #include <com/sun/star/lang/XServiceInfo.hpp>
41 #include <com/sun/star/lang/XInitialization.hpp>
42 #include <com/sun/star/lang/IllegalArgumentException.hpp>
43 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
44 #include <comphelper/stl_types.hxx>
45 #include <map>
46 
47 
48 using namespace com::sun::star::uno;
49 using namespace com::sun::star::ucb;
50 using namespace com::sun::star::lang;
51 using namespace com::sun::star::i18n;
52 using namespace rtl;
53 
54 //=============================================================================
55 
56 class AnyCompare : public ::cppu::WeakImplHelper1< XAnyCompare >
57 {
58 	Reference< XCollator > m_rCollator;
59 
60 public:
61     AnyCompare( Reference< XComponentContext > xContext, const Locale& rLocale ) throw()
62     {
63         Reference< XMultiComponentFactory > xFactory = xContext->getServiceManager();
64         if ( xFactory.is() )
65 	{
66 		m_rCollator = Reference< XCollator >(
67                 xFactory->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.i18n.Collator" ), xContext ),
68 						UNO_QUERY );
69 		m_rCollator->loadDefaultCollator( rLocale,
70 										  0 ); //???
71         }
72 
73 	}
74 
75 	virtual sal_Int16 SAL_CALL compare( const Any& any1, const Any& any2 ) throw(RuntimeException);
76 };
77 
78 //=============================================================================
79 
80 class AnyCompareFactory : public cppu::WeakImplHelper3< XAnyCompareFactory, XInitialization, XServiceInfo >
81 {
82 	Reference< XAnyCompare > 			m_rAnyCompare;
83     Reference< XComponentContext >      m_rContext;
84 	Locale							  	m_Locale;
85 
86 public:
87     AnyCompareFactory( Reference< XComponentContext > xContext ) : m_rContext( xContext )
88     {}
89 
90 	// XAnyCompareFactory
91 	virtual Reference< XAnyCompare > SAL_CALL createAnyCompareByName ( const OUString& aPropertyName ) throw(::com::sun::star::uno::RuntimeException);
92 
93 	// XInitialization
94     virtual void SAL_CALL initialize( const Sequence< Any >& aArguments )
95 			throw ( Exception, RuntimeException );
96 
97 	// XServiceInfo
98 	virtual OUString SAL_CALL getImplementationName(  ) throw(RuntimeException);
99 	virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException);
100 	virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) throw(RuntimeException);
101 
102     // XServiceInfo - static versions (used for component registration)
103     static ::rtl::OUString SAL_CALL getImplementationName_static();
104     static Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
105     static Reference< XInterface > SAL_CALL Create( const Reference< XComponentContext >& );
106 };
107 
108 //===========================================================================================
109 
110 sal_Int16 SAL_CALL AnyCompare::compare( const Any& any1, const Any& any2 ) throw(::com::sun::star::uno::RuntimeException)
111 {
112 	sal_Int16 aResult = 0;
113 
114 	if( m_rCollator.is() )
115 	{
116 		OUString aStr1;
117 		OUString aStr2;
118 
119 		any1 >>= aStr1;
120 		any2 >>= aStr2;
121 
122 		aResult = ( sal_Int16 )m_rCollator->compareString( aStr1, aStr2 );
123 	}
124 
125 	return aResult;
126 }
127 
128 //===========================================================================================
129 
130 Reference< XAnyCompare > SAL_CALL AnyCompareFactory::createAnyCompareByName( const OUString& aPropertyName ) throw(::com::sun::star::uno::RuntimeException)
131 {
132 	// for now only OUString properties compare is implemented
133 	// so no check for the property name is done
134 
135 	if( aPropertyName.equals( OUString::createFromAscii( "Title" ) ) )
136 		return m_rAnyCompare;
137 
138 	return Reference< XAnyCompare >();
139 }
140 
141 void SAL_CALL AnyCompareFactory::initialize( const Sequence< Any >& aArguments ) throw ( Exception, RuntimeException )
142 {
143 	if( aArguments.getLength() )
144 	{
145 		if( aArguments[0] >>= m_Locale )
146 		{
147             m_rAnyCompare = new AnyCompare( m_rContext, m_Locale );
148 			return;
149 		}
150 	}
151 
152 }
153 
154 OUString SAL_CALL AnyCompareFactory::getImplementationName(  ) throw( RuntimeException )
155 {
156 	return getImplementationName_static();
157 }
158 
159 OUString SAL_CALL AnyCompareFactory::getImplementationName_static(  )
160 {
161 	return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "AnyCompareFactory" ) );
162 }
163 
164 sal_Bool SAL_CALL AnyCompareFactory::supportsService( const OUString& ServiceName ) throw(RuntimeException)
165 {
166 	rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ucb.AnyCompareFactory" ) );
167 	return aServiceName == ServiceName;
168 }
169 
170 Sequence< OUString > SAL_CALL AnyCompareFactory::getSupportedServiceNames(  ) throw(RuntimeException)
171 {
172 	return getSupportedServiceNames_static();
173 }
174 
175 Sequence< OUString > SAL_CALL AnyCompareFactory::getSupportedServiceNames_static(  )
176 {
177 	const rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ucb.AnyCompareFactory" ) );
178 	const Sequence< rtl::OUString > aSeq( &aServiceName, 1 );
179 	return aSeq;
180 }
181 
182 Reference< XInterface > SAL_CALL AnyCompareFactory::Create(
183                 const Reference< XComponentContext >& rxContext )
184 {
185     return (cppu::OWeakObject*)new AnyCompareFactory( rxContext );
186 }
187 
188 void createRegistryInfo_AnyCompareFactory()
189 {
190     static ::comphelper::module::OAutoRegistration< AnyCompareFactory > aAutoRegistration;
191 }
192