LibreOffice Module comphelper (master)  1
AnyCompareFactory.cxx
Go to the documentation of this file.
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/ucb/XAnyCompareFactory.hpp>
21 #include <com/sun/star/i18n/Collator.hpp>
22 #include <com/sun/star/lang/Locale.hpp>
23 #include <com/sun/star/uno/Sequence.h>
24 #include <cppuhelper/implbase.hxx>
26 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include <com/sun/star/lang/XInitialization.hpp>
28 
29 using namespace com::sun::star::uno;
30 using namespace com::sun::star::ucb;
31 using namespace com::sun::star::lang;
32 using namespace com::sun::star::i18n;
33 
34 namespace {
35 
36 class AnyCompare : public ::cppu::WeakImplHelper< XAnyCompare >
37 {
38  Reference< XCollator > m_xCollator;
39 
40 public:
41  AnyCompare( Reference< XComponentContext > const & xContext, const Locale& rLocale )
42  : m_xCollator(Collator::create( xContext ))
43  {
44  m_xCollator->loadDefaultCollator( rLocale,
45  0 ); //???
46  }
47 
48  virtual sal_Int16 SAL_CALL compare( const Any& any1, const Any& any2 ) override;
49 };
50 
51 class AnyCompareFactory : public cppu::WeakImplHelper< XAnyCompareFactory, XInitialization, XServiceInfo >
52 {
53  Reference< XAnyCompare > m_xAnyCompare;
55  Locale m_Locale;
56 
57 public:
58  explicit AnyCompareFactory( Reference< XComponentContext > const & xContext ) : m_xContext( xContext )
59  {}
60 
61  // XAnyCompareFactory
62  virtual Reference< XAnyCompare > SAL_CALL createAnyCompareByName ( const OUString& aPropertyName ) override;
63 
64  // XInitialization
65  virtual void SAL_CALL initialize( const Sequence< Any >& aArguments ) override;
66 
67  // XServiceInfo
68  virtual OUString SAL_CALL getImplementationName( ) override;
69  virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
70  virtual Sequence< OUString > SAL_CALL getSupportedServiceNames( ) override;
71 };
72 
73 }
74 
75 sal_Int16 SAL_CALL AnyCompare::compare( const Any& any1, const Any& any2 )
76 {
77  sal_Int16 aResult = 0;
78 
79  OUString aStr1;
80  OUString aStr2;
81 
82  any1 >>= aStr1;
83  any2 >>= aStr2;
84 
85  aResult = static_cast<sal_Int16>(m_xCollator->compareString(aStr1, aStr2));
86 
87  return aResult;
88 }
89 
90 Reference< XAnyCompare > SAL_CALL AnyCompareFactory::createAnyCompareByName( const OUString& aPropertyName )
91 {
92  // for now only OUString properties compare is implemented
93  // so no check for the property name is done
94 
95  if( aPropertyName == "Title" )
96  return m_xAnyCompare;
97 
98  return Reference< XAnyCompare >();
99 }
100 
101 void SAL_CALL AnyCompareFactory::initialize( const Sequence< Any >& aArguments )
102 {
103  if( aArguments.hasElements() )
104  {
105  if( aArguments[0] >>= m_Locale )
106  {
107  m_xAnyCompare = new AnyCompare( m_xContext, m_Locale );
108  return;
109  }
110  }
111 }
112 
113 OUString SAL_CALL AnyCompareFactory::getImplementationName( )
114 {
115  return "AnyCompareFactory";
116 }
117 
118 sal_Bool SAL_CALL AnyCompareFactory::supportsService( const OUString& ServiceName )
119 {
120  return cppu::supportsService(this, ServiceName);
121 }
122 
123 Sequence< OUString > SAL_CALL AnyCompareFactory::getSupportedServiceNames( )
124 {
125  return { "com.sun.star.ucb.AnyCompareFactory" };
126 }
127 
128 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
130  css::uno::XComponentContext *context,
131  css::uno::Sequence<css::uno::Any> const &)
132 {
133  return cppu::acquire(new AnyCompareFactory(context));
134 }
135 
136 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Reference< deployment::XPackageRegistry > create(Reference< deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, Reference< XComponentContext > const &xComponentContext)
unsigned char sal_Bool
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * AnyCompareFactory_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Reference< XComponentContext > m_xContext
SbxDecimal::CmpResult compare(SAL_UNUSED_PARAMETER const SbxDecimal &, SAL_UNUSED_PARAMETER const SbxDecimal &)