bump product version to 4.1.6.2
[LibreOffice.git] / connectivity / source / commontools / filtermanager.cxx
blobbe526c38409e56abca5faf598b0c5707c0a800f0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "connectivity/filtermanager.hxx"
22 #include <com/sun/star/sdb/XSQLQueryComposerFactory.hpp>
23 #include "TConnection.hxx"
24 #include <osl/diagnose.h>
25 #include "connectivity/dbtools.hxx"
26 #include <tools/diagnose_ex.h>
27 #include <rtl/ustrbuf.hxx>
29 //........................................................................
30 namespace dbtools
32 //........................................................................
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::sdbc;
36 using namespace ::com::sun::star::sdb;
37 using namespace ::com::sun::star::lang;
38 using namespace ::com::sun::star::beans;
39 using namespace connectivity;
41 //====================================================================
42 //= FilterManager
43 //====================================================================
44 //--------------------------------------------------------------------
45 FilterManager::FilterManager( const Reference< XMultiServiceFactory >& _rxORB )
46 :m_xORB( _rxORB )
47 ,m_aFilterComponents( FC_COMPONENT_COUNT )
48 ,m_bApplyPublicFilter( true )
52 //--------------------------------------------------------------------
53 void FilterManager::initialize( const Reference< XPropertySet >& _rxComponentAggregate )
55 m_xComponentAggregate = _rxComponentAggregate;
56 OSL_ENSURE( m_xComponentAggregate.is(), "FilterManager::initialize: invalid arguments!" );
58 if ( m_xComponentAggregate.is() )
59 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_APPLYFILTER), makeAny( (sal_Bool)sal_True ) );
62 //--------------------------------------------------------------------
63 void FilterManager::dispose( )
65 m_xComponentAggregate.clear();
68 //--------------------------------------------------------------------
69 const OUString& FilterManager::getFilterComponent( FilterComponent _eWhich ) const
71 return m_aFilterComponents[ _eWhich ];
74 //--------------------------------------------------------------------
75 void FilterManager::setFilterComponent( FilterComponent _eWhich, const OUString& _rComponent )
77 m_aFilterComponents[ _eWhich ] = _rComponent;
78 try
80 if ( m_xComponentAggregate.is() && (( _eWhich != fcPublicFilter ) || m_bApplyPublicFilter ) )
81 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FILTER), makeAny( getComposedFilter() ) );
83 catch( const Exception& )
85 DBG_UNHANDLED_EXCEPTION();
89 //--------------------------------------------------------------------
90 void FilterManager::setApplyPublicFilter( sal_Bool _bApply )
92 if ( m_bApplyPublicFilter == _bApply )
93 return;
95 m_bApplyPublicFilter = _bApply;
97 try
99 if ( m_xComponentAggregate.is() && !getFilterComponent( fcPublicFilter ).isEmpty() )
100 { // only if there changed something
101 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FILTER), makeAny( getComposedFilter() ) );
104 catch( const Exception& )
106 DBG_UNHANDLED_EXCEPTION();
110 //--------------------------------------------------------------------
111 void FilterManager::appendFilterComponent( OUStringBuffer& io_appendTo, const OUString& i_component ) const
113 if ( io_appendTo.getLength() > 0 )
115 io_appendTo.insert( 0, sal_Unicode( '(' ) );
116 io_appendTo.insert( 1, sal_Unicode( ' ' ) );
117 io_appendTo.appendAscii( " ) AND " );
120 io_appendTo.appendAscii( "( " );
121 io_appendTo.append( i_component );
122 io_appendTo.appendAscii( " )" );
125 //--------------------------------------------------------------------
126 bool FilterManager::isThereAtMostOneComponent( OUStringBuffer& o_singleComponent ) const
128 sal_Int32 nOnlyNonEmpty = -1;
129 sal_Int32 i;
130 for ( i = getFirstApplicableFilterIndex(); i < FC_COMPONENT_COUNT; ++i )
132 if ( !m_aFilterComponents[ i ].isEmpty() )
134 if ( nOnlyNonEmpty != -1 )
135 // it's the second non-empty component
136 break;
137 else
138 nOnlyNonEmpty = i;
141 if ( nOnlyNonEmpty == -1 )
143 o_singleComponent.makeStringAndClear();
144 return true;
147 if ( i == FC_COMPONENT_COUNT )
149 // we found only one non-empty filter component
150 o_singleComponent = m_aFilterComponents[ nOnlyNonEmpty ];
151 return true;
153 return false;
156 //--------------------------------------------------------------------
157 OUString FilterManager::getComposedFilter( ) const
159 OUStringBuffer aComposedFilter;
161 // if we have only one non-empty component, then there's no need to compose anything
162 if ( !isThereAtMostOneComponent( aComposedFilter ) )
164 // append the single components
165 for ( sal_Int32 i = getFirstApplicableFilterIndex(); i < FC_COMPONENT_COUNT; ++i )
166 appendFilterComponent( aComposedFilter, m_aFilterComponents[ i ] );
169 return aComposedFilter.makeStringAndClear();
172 //........................................................................
173 } // namespace dbtools
174 //........................................................................
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */