Use COMReference to handle COM pointers in CreateShortcut
[LibreOffice.git] / connectivity / source / commontools / filtermanager.cxx
blobfa9fc7116d92d85a16fbe727ad4d5c767c703ab8
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 <comphelper/diagnose_ex.hxx>
26 #include <rtl/ustrbuf.hxx>
29 namespace dbtools
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::beans;
35 using namespace connectivity;
37 FilterManager::FilterManager( )
38 :m_bApplyPublicFilter( true )
43 void FilterManager::initialize( const Reference< XPropertySet >& _rxComponentAggregate )
45 m_xComponentAggregate = _rxComponentAggregate;
46 OSL_ENSURE( m_xComponentAggregate.is(), "FilterManager::initialize: invalid arguments!" );
48 if ( m_xComponentAggregate.is() )
49 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_APPLYFILTER), Any( true ) );
53 void FilterManager::dispose( )
55 m_xComponentAggregate.clear();
59 const OUString& FilterManager::getFilterComponent( FilterComponent _eWhich ) const
61 switch (_eWhich)
63 case FilterComponent::PublicFilter:
64 return m_aPublicFilterComponent;
65 case FilterComponent::PublicHaving:
66 return m_aPublicHavingComponent;
67 case FilterComponent::LinkFilter:
68 return m_aLinkFilterComponent;
69 case FilterComponent::LinkHaving:
70 return m_aLinkHavingComponent;
72 assert(false);
74 static constexpr OUString sErr(u"#FilterManager::getFilterComponent unknown component#"_ustr);
75 return sErr;
79 void FilterManager::setFilterComponent( FilterComponent _eWhich, const OUString& _rComponent )
81 switch (_eWhich)
83 case FilterComponent::PublicFilter:
84 m_aPublicFilterComponent = _rComponent;
85 break;
86 case FilterComponent::PublicHaving:
87 m_aPublicHavingComponent = _rComponent;
88 break;
89 case FilterComponent::LinkFilter:
90 m_aLinkFilterComponent = _rComponent;
91 break;
92 case FilterComponent::LinkHaving:
93 m_aLinkHavingComponent = _rComponent;
94 break;
96 try
98 if ( m_xComponentAggregate.is() )
100 bool propagate(true);
101 switch (_eWhich)
103 case FilterComponent::PublicFilter:
104 propagate = propagate && m_bApplyPublicFilter;
105 [[fallthrough]];
106 case FilterComponent::LinkFilter:
107 if (propagate)
108 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FILTER), Any( getComposedFilter() ) );
109 break;
110 case FilterComponent::PublicHaving:
111 propagate = propagate && m_bApplyPublicFilter;
112 [[fallthrough]];
113 case FilterComponent::LinkHaving:
114 if (propagate)
115 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_HAVINGCLAUSE), Any( getComposedHaving() ) );
116 break;
120 catch( const Exception& )
122 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
127 void FilterManager::setApplyPublicFilter( bool _bApply )
129 if ( m_bApplyPublicFilter == _bApply )
130 return;
132 m_bApplyPublicFilter = _bApply;
136 if ( m_xComponentAggregate.is())
138 // only where/if something changed
139 if (!getFilterComponent( FilterComponent::PublicFilter ).isEmpty())
140 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FILTER), Any( getComposedFilter() ) );
141 if (!getFilterComponent( FilterComponent::PublicHaving ).isEmpty())
142 m_xComponentAggregate->setPropertyValue( OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_HAVINGCLAUSE), Any( getComposedHaving() ) );
145 catch( const Exception& )
147 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
152 void FilterManager::appendFilterComponent( OUStringBuffer& io_appendTo, std::u16string_view i_component )
154 if ( !io_appendTo.isEmpty() )
156 io_appendTo.insert( 0, '(' );
157 io_appendTo.insert( 1, ' ' );
158 io_appendTo.append( " ) AND " );
161 io_appendTo.append( "( " );
162 io_appendTo.append( i_component );
163 io_appendTo.append( " )" );
167 bool FilterManager::isThereAtMostOneFilterComponent( OUString& o_singleComponent ) const
169 if (m_bApplyPublicFilter) {
170 if (!m_aPublicFilterComponent.isEmpty() && !m_aLinkFilterComponent.isEmpty())
171 return false;
172 if (!m_aPublicFilterComponent.isEmpty())
173 o_singleComponent = m_aPublicFilterComponent;
174 else if (!m_aLinkFilterComponent.isEmpty())
175 o_singleComponent = m_aLinkFilterComponent;
176 else
177 o_singleComponent.clear();
178 return true;
180 else
182 if (m_aLinkFilterComponent.isEmpty())
183 o_singleComponent.clear();
184 else
185 o_singleComponent = m_aLinkFilterComponent;
186 return true;
190 bool FilterManager::isThereAtMostOneHavingComponent( OUString& o_singleComponent ) const
192 if (m_bApplyPublicFilter) {
193 if (!m_aPublicHavingComponent.isEmpty() && !m_aLinkHavingComponent.isEmpty())
194 return false;
195 if (!m_aPublicHavingComponent.isEmpty())
196 o_singleComponent = m_aPublicHavingComponent;
197 else if (!m_aLinkHavingComponent.isEmpty())
198 o_singleComponent = m_aLinkHavingComponent;
199 else
200 o_singleComponent.clear();
201 return true;
203 else
205 if (m_aLinkHavingComponent.isEmpty())
206 o_singleComponent.clear();
207 else
208 o_singleComponent = m_aLinkHavingComponent;
209 return true;
214 OUString FilterManager::getComposedFilter( ) const
216 // if we have only one non-empty component, then there's no need to compose anything
217 OUString singleComponent;
218 if ( isThereAtMostOneFilterComponent( singleComponent ) )
220 return singleComponent;
222 // append the single components
223 OUStringBuffer aComposedFilter(singleComponent);
224 if (m_bApplyPublicFilter)
225 appendFilterComponent( aComposedFilter, m_aPublicFilterComponent );
226 appendFilterComponent( aComposedFilter, m_aLinkFilterComponent );
227 return aComposedFilter.makeStringAndClear();
231 OUString FilterManager::getComposedHaving( ) const
233 // if we have only one non-empty component, then there's no need to compose anything
234 OUString singleComponent;
235 if ( isThereAtMostOneHavingComponent( singleComponent ) )
237 return singleComponent;
239 // append the single components
240 OUStringBuffer aComposedFilter(singleComponent);
241 if (m_bApplyPublicFilter)
242 appendFilterComponent( aComposedFilter, m_aPublicHavingComponent );
243 appendFilterComponent( aComposedFilter, m_aLinkHavingComponent );
244 return aComposedFilter.makeStringAndClear();
248 } // namespace dbtools
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */