Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / commontools / filtermanager.cxx
blob1c97944fb4bda3be2f032af1226404b72aa73f82
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>
30 namespace dbtools
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;
42 //= FilterManager
45 FilterManager::FilterManager( )
46 :m_aFilterComponents( FC_COMPONENT_COUNT )
47 ,m_bApplyPublicFilter( true )
52 void FilterManager::initialize( const Reference< XPropertySet >& _rxComponentAggregate )
54 m_xComponentAggregate = _rxComponentAggregate;
55 OSL_ENSURE( m_xComponentAggregate.is(), "FilterManager::initialize: invalid arguments!" );
57 if ( m_xComponentAggregate.is() )
58 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_APPLYFILTER), makeAny( true ) );
62 void FilterManager::dispose( )
64 m_xComponentAggregate.clear();
68 const OUString& FilterManager::getFilterComponent( FilterComponent _eWhich ) const
70 return m_aFilterComponents[ _eWhich ];
74 void FilterManager::setFilterComponent( FilterComponent _eWhich, const OUString& _rComponent )
76 m_aFilterComponents[ _eWhich ] = _rComponent;
77 try
79 if ( m_xComponentAggregate.is() && (( _eWhich != fcPublicFilter ) || m_bApplyPublicFilter ) )
80 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FILTER), makeAny( getComposedFilter() ) );
82 catch( const Exception& )
84 DBG_UNHANDLED_EXCEPTION();
89 void FilterManager::setApplyPublicFilter( bool _bApply )
91 if ( m_bApplyPublicFilter == _bApply )
92 return;
94 m_bApplyPublicFilter = _bApply;
96 try
98 if ( m_xComponentAggregate.is() && !getFilterComponent( fcPublicFilter ).isEmpty() )
99 { // only if there changed something
100 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FILTER), makeAny( getComposedFilter() ) );
103 catch( const Exception& )
105 DBG_UNHANDLED_EXCEPTION();
110 void FilterManager::appendFilterComponent( OUStringBuffer& io_appendTo, const OUString& i_component ) const
112 if ( !io_appendTo.isEmpty() )
114 io_appendTo.insert( 0, '(' );
115 io_appendTo.insert( 1, ' ' );
116 io_appendTo.appendAscii( " ) AND " );
119 io_appendTo.appendAscii( "( " );
120 io_appendTo.append( i_component );
121 io_appendTo.appendAscii( " )" );
125 bool FilterManager::isThereAtMostOneComponent( OUStringBuffer& o_singleComponent ) const
127 sal_Int32 nOnlyNonEmpty = -1;
128 sal_Int32 i;
129 for ( i = getFirstApplicableFilterIndex(); i < FC_COMPONENT_COUNT; ++i )
131 if ( !m_aFilterComponents[ i ].isEmpty() )
133 if ( nOnlyNonEmpty != -1 )
134 // it's the second non-empty component
135 break;
136 else
137 nOnlyNonEmpty = i;
140 if ( nOnlyNonEmpty == -1 )
142 o_singleComponent.makeStringAndClear();
143 return true;
146 if ( i == FC_COMPONENT_COUNT )
148 // we found only one non-empty filter component
149 o_singleComponent = m_aFilterComponents[ nOnlyNonEmpty ];
150 return true;
152 return false;
156 OUString FilterManager::getComposedFilter( ) const
158 OUStringBuffer aComposedFilter;
160 // if we have only one non-empty component, then there's no need to compose anything
161 if ( !isThereAtMostOneComponent( aComposedFilter ) )
163 // append the single components
164 for ( sal_Int32 i = getFirstApplicableFilterIndex(); i < FC_COMPONENT_COUNT; ++i )
165 appendFilterComponent( aComposedFilter, m_aFilterComponents[ i ] );
168 return aComposedFilter.makeStringAndClear();
172 } // namespace dbtools
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */