Avoid potential negative array index access to cached text.
[LibreOffice.git] / extensions / source / bibliography / bibload.cxx
blobc8d88d8d01603a5326311acd357e6f3337acafa3
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 <tools/debug.hxx>
21 #include <comphelper/diagnose_ex.hxx>
22 #include <cppuhelper/supportsservice.hxx>
23 #include <svl/itemprop.hxx>
24 #include <toolkit/helper/vclunohelper.hxx>
25 #include <comphelper/processfactory.hxx>
26 #include <com/sun/star/awt/XVclWindowPeer.hpp>
27 #include <com/sun/star/sdbc/ResultSetType.hpp>
28 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
29 #include <com/sun/star/sdbc/SQLException.hpp>
30 #include <com/sun/star/sdb/XColumn.hpp>
31 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
32 #include <com/sun/star/sdbc/XRowSet.hpp>
33 #include <com/sun/star/frame/XFrameLoader.hpp>
34 #include <com/sun/star/lang/XServiceInfo.hpp>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/beans/PropertyAttribute.hpp>
37 #include <com/sun/star/beans/XPropertySet.hpp>
38 #include <com/sun/star/container/XNameAccess.hpp>
39 #include <com/sun/star/text/BibliographyDataField.hpp>
40 #include <com/sun/star/form/XLoadable.hpp>
41 #include <com/sun/star/frame/XLayoutManager.hpp>
42 #include <vcl/window.hxx>
43 #include <vcl/svapp.hxx>
45 #include "bibresid.hxx"
46 #include <strings.hrc>
47 #include "bibcont.hxx"
48 #include "bibbeam.hxx"
49 #include "bibmod.hxx"
50 #include "bibview.hxx"
51 #include "framectr.hxx"
52 #include "datman.hxx"
53 #include "bibconfig.hxx"
54 #include <cppuhelper/implbase.hxx>
55 #include <rtl/ref.hxx>
56 #include <o3tl/string_view.hxx>
58 using namespace ::com::sun::star;
59 using namespace ::com::sun::star::uno;
60 using namespace ::com::sun::star::beans;
61 using namespace ::com::sun::star::lang;
62 using namespace ::com::sun::star::sdb;
63 using namespace ::com::sun::star::sdbc;
64 using namespace ::com::sun::star::form;
65 using namespace ::com::sun::star::container;
66 using namespace ::com::sun::star::frame;
68 namespace {
70 class BibliographyLoader : public cppu::WeakImplHelper
71 < XServiceInfo, XNameAccess, XPropertySet, XFrameLoader >
73 HdlBibModul m_pBibMod;
74 rtl::Reference<BibDataManager> m_xDatMan;
75 Reference< XNameAccess > m_xColumns;
76 Reference< XResultSet > m_xCursor;
78 private:
80 void loadView(const Reference< XFrame > & aFrame,
81 const Reference< XLoadEventListener > & aListener);
83 BibDataManager* GetDataManager()const;
84 Reference< XNameAccess > const & GetDataColumns() const;
85 Reference< XResultSet > const & GetDataCursor() const;
86 Reference< sdb::XColumn > GetIdentifierColumn() const;
88 public:
89 BibliographyLoader();
90 virtual ~BibliographyLoader() override;
92 // XServiceInfo
93 OUString SAL_CALL getImplementationName() override;
94 sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
95 Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
97 //XNameAccess
98 virtual Any SAL_CALL getByName(const OUString& aName) override;
99 virtual Sequence< OUString > SAL_CALL getElementNames() override;
100 virtual sal_Bool SAL_CALL hasByName(const OUString& aName) override;
102 //XElementAccess
103 virtual Type SAL_CALL getElementType() override;
104 virtual sal_Bool SAL_CALL hasElements() override;
106 //XPropertySet
107 virtual Reference< XPropertySetInfo > SAL_CALL getPropertySetInfo() override;
108 virtual void SAL_CALL setPropertyValue(const OUString& PropertyName, const Any& aValue) override;
109 virtual Any SAL_CALL getPropertyValue(const OUString& PropertyName) override;
110 virtual void SAL_CALL addPropertyChangeListener(const OUString& PropertyName, const Reference< XPropertyChangeListener > & aListener) override;
111 virtual void SAL_CALL removePropertyChangeListener(const OUString& PropertyName, const Reference< XPropertyChangeListener > & aListener) override;
112 virtual void SAL_CALL addVetoableChangeListener(const OUString& PropertyName, const Reference< XVetoableChangeListener > & aListener) override;
113 virtual void SAL_CALL removeVetoableChangeListener(const OUString& PropertyName, const Reference< XVetoableChangeListener > & aListener) override;
115 // XLoader
116 virtual void SAL_CALL load(const Reference< XFrame > & aFrame, const OUString& aURL,
117 const Sequence< PropertyValue >& aArgs,
118 const Reference< XLoadEventListener > & aListener) override;
119 virtual void SAL_CALL cancel() override;
124 BibliographyLoader::BibliographyLoader() :
125 m_pBibMod(nullptr)
129 BibliographyLoader::~BibliographyLoader()
131 Reference< lang::XComponent > xComp(m_xCursor, UNO_QUERY);
132 if (xComp.is())
133 xComp->dispose();
134 if(m_pBibMod)
135 CloseBibModul(m_pBibMod);
139 // XServiceInfo
140 OUString BibliographyLoader::getImplementationName()
142 return "com.sun.star.extensions.Bibliography";
145 // XServiceInfo
146 sal_Bool BibliographyLoader::supportsService(const OUString& ServiceName)
148 return cppu::supportsService(this, ServiceName);
151 // XServiceInfo
152 Sequence< OUString > BibliographyLoader::getSupportedServiceNames()
154 return { "com.sun.star.frame.FrameLoader", "com.sun.star.frame.Bibliography" };
157 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
158 extensions_BibliographyLoader_get_implementation(
159 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
161 return cppu::acquire(new BibliographyLoader());
164 void BibliographyLoader::cancel()
170 void BibliographyLoader::load(const Reference< XFrame > & rFrame, const OUString& rURL,
171 const Sequence< PropertyValue >& /*rArgs*/,
172 const Reference< XLoadEventListener > & rListener)
175 SolarMutexGuard aGuard;
177 m_pBibMod = OpenBibModul();
179 std::u16string_view aPartName = o3tl::getToken(rURL, 1, '/' );
180 Reference<XPropertySet> xPrSet(rFrame, UNO_QUERY);
181 if(xPrSet.is())
183 Any aTitle;
184 aTitle <<= BibResId(RID_BIB_STR_FRAME_TITLE);
185 xPrSet->setPropertyValue("Title", aTitle);
187 if(aPartName == u"View" || aPartName == u"View1")
189 loadView(rFrame, rListener);
194 void BibliographyLoader::loadView(const Reference< XFrame > & rFrame,
195 const Reference< XLoadEventListener > & rListener)
197 SolarMutexGuard aGuard;
199 if(!m_pBibMod)
200 m_pBibMod = OpenBibModul();
202 m_xDatMan = BibModul::createDataManager();
203 BibDBDescriptor aBibDesc = BibModul::GetConfig()->GetBibliographyURL();
205 if(aBibDesc.sDataSource.isEmpty())
207 DBChangeDialogConfig_Impl aConfig;
208 const Sequence<OUString> aSources = aConfig.GetDataSourceNames();
209 if(aSources.hasElements())
210 aBibDesc.sDataSource = aSources.getConstArray()[0];
213 m_xDatMan->createDatabaseForm( aBibDesc );
215 Reference<awt::XWindow> aWindow = rFrame->getContainerWindow();
217 VclPtr<vcl::Window> pParent = VCLUnoHelper::GetWindow( aWindow );
219 VclPtrInstance<BibBookContainer> pMyWindow( pParent );
220 pMyWindow->Show();
222 VclPtrInstance< ::bib::BibView> pView( pMyWindow, m_xDatMan.get(), WB_VSCROLL | WB_HSCROLL | WB_3DLOOK );
223 pView->Show();
224 m_xDatMan->SetView( pView );
226 VclPtrInstance< ::bib::BibBeamer> pBeamer( pMyWindow, m_xDatMan.get() );
227 pBeamer->Show();
228 pMyWindow->createTopFrame(pBeamer);
230 pMyWindow->createBottomFrame(pView);
232 Reference< awt::XWindow > xWin ( pMyWindow->GetComponentInterface(), UNO_QUERY );
234 Reference< XController > xCtrRef( new BibFrameController_Impl( xWin, m_xDatMan.get() ) );
236 xCtrRef->attachFrame(rFrame);
237 rFrame->setComponent( xWin, xCtrRef);
238 pBeamer->SetXController(xCtrRef);
240 if (aWindow)
242 // not earlier because SetFocus() is triggered in setVisible()
243 aWindow->setVisible(true);
246 Reference<XLoadable>(m_xDatMan)->load();
247 m_xDatMan->RegisterInterceptor(pBeamer);
249 if ( rListener.is() )
250 rListener->loadFinished( this );
252 // attach menu bar
253 Reference< XPropertySet > xPropSet( rFrame, UNO_QUERY );
254 Reference< css::frame::XLayoutManager > xLayoutManager;
255 if ( xPropSet.is() )
259 Any a = xPropSet->getPropertyValue("LayoutManager");
260 a >>= xLayoutManager;
262 catch ( const uno::Exception& )
267 if ( xLayoutManager.is() )
268 xLayoutManager->createElement( "private:resource/menubar/menubar" );
271 BibDataManager* BibliographyLoader::GetDataManager()const
273 if(!m_xDatMan.is())
275 if(!m_pBibMod)
276 const_cast< BibliographyLoader* >( this )->m_pBibMod = OpenBibModul();
277 const_cast< BibliographyLoader* >( this )->m_xDatMan = BibModul::createDataManager();
279 return m_xDatMan.get();
282 Reference< XNameAccess > const & BibliographyLoader::GetDataColumns() const
284 if (!m_xColumns.is())
286 Reference< XMultiServiceFactory > xMgr = comphelper::getProcessServiceFactory();
287 Reference< XRowSet > xRowSet(xMgr->createInstance("com.sun.star.sdb.RowSet"), UNO_QUERY);
288 Reference< XPropertySet > xResultSetProps(xRowSet, UNO_QUERY);
289 DBG_ASSERT(xResultSetProps.is() , "BibliographyLoader::GetDataCursor : invalid row set (no XResultSet or no XPropertySet) !");
291 BibDBDescriptor aBibDesc = BibModul::GetConfig()->GetBibliographyURL();
293 Any aBibUrlAny; aBibUrlAny <<= aBibDesc.sDataSource;
294 xResultSetProps->setPropertyValue("DataSourceName", aBibUrlAny);
295 Any aCommandType; aCommandType <<= aBibDesc.nCommandType;
296 xResultSetProps->setPropertyValue("CommandType", aCommandType);
297 Any aTableName; aTableName <<= aBibDesc.sTableOrQuery;
298 xResultSetProps->setPropertyValue("Command", aTableName);
299 Any aResultSetType; aResultSetType <<= sal_Int32(ResultSetType::SCROLL_INSENSITIVE);
300 xResultSetProps->setPropertyValue("ResultSetType", aResultSetType);
301 Any aResultSetCurrency; aResultSetCurrency <<= sal_Int32(ResultSetConcurrency::UPDATABLE);
302 xResultSetProps->setPropertyValue("ResultSetConcurrency", aResultSetCurrency);
304 bool bSuccess = false;
307 xRowSet->execute();
308 bSuccess = true;
310 catch(const SQLException&)
312 DBG_UNHANDLED_EXCEPTION("extensions.biblio");
314 catch(const Exception& )
316 DBG_UNHANDLED_EXCEPTION("extensions.biblio");
317 bSuccess = false;
320 if (!bSuccess)
322 Reference< XComponent > xSetComp(xRowSet, UNO_QUERY);
323 if (xSetComp.is())
324 xSetComp->dispose();
325 xRowSet = nullptr;
327 else
328 const_cast<BibliographyLoader*>(this)->m_xCursor = xRowSet.get();
330 Reference< sdbcx::XColumnsSupplier > xSupplyCols(m_xCursor, UNO_QUERY);
331 if (xSupplyCols.is())
332 const_cast<BibliographyLoader*>(this)->m_xColumns = xSupplyCols->getColumns();
335 return m_xColumns;
338 Reference< sdb::XColumn > BibliographyLoader::GetIdentifierColumn() const
340 BibDataManager* pDatMan = GetDataManager();
341 Reference< XNameAccess > xColumns = GetDataColumns();
342 OUString sIdentifierColumnName = pDatMan->GetIdentifierMapping();
344 Reference< sdb::XColumn > xReturn;
345 if (xColumns.is() && xColumns->hasByName(sIdentifierColumnName))
347 xReturn.set(xColumns->getByName(sIdentifierColumnName), UNO_QUERY);
349 return xReturn;
352 Reference< XResultSet > const & BibliographyLoader::GetDataCursor() const
354 if (!m_xCursor.is())
355 GetDataColumns();
356 if (m_xCursor.is())
357 m_xCursor->first();
358 return m_xCursor;
361 static OUString lcl_AddProperty(const Reference< XNameAccess >& xColumns,
362 const Mapping* pMapping, const OUString& rColumnName)
364 OUString sColumnName(rColumnName);
365 if(pMapping)
367 for(const auto & aColumnPair : pMapping->aColumnPairs)
369 if(aColumnPair.sLogicalColumnName == rColumnName)
371 sColumnName = aColumnPair.sRealColumnName;
372 break;
376 OUString uColumnName(sColumnName);
377 OUString uRet;
378 Reference< sdb::XColumn > xCol;
379 if (xColumns->hasByName(uColumnName))
380 xCol.set(xColumns->getByName(uColumnName), UNO_QUERY);
381 if (xCol.is())
382 uRet = xCol->getString();
383 return uRet;
386 Any BibliographyLoader::getByName(const OUString& rName)
388 Any aRet;
391 BibDataManager* pDatMan = GetDataManager();
392 Reference< XResultSet > xCursor = GetDataCursor();
393 Reference< sdbcx::XColumnsSupplier > xSupplyCols(xCursor, UNO_QUERY);
394 Reference< XNameAccess > xColumns;
395 if (!xSupplyCols.is())
396 return aRet;
397 xColumns = xSupplyCols->getColumns();
398 DBG_ASSERT(xSupplyCols.is(), "BibliographyLoader::getByName : invalid columns returned by the data cursor (may be the result set is not alive ?) !");
399 if (!xColumns.is())
400 return aRet;
402 const OUString sIdentifierMapping = pDatMan->GetIdentifierMapping();
403 Reference< sdb::XColumn > xColumn;
404 if (xColumns->hasByName(sIdentifierMapping))
405 xColumn.set(xColumns->getByName(sIdentifierMapping), UNO_QUERY);
406 if (xColumn.is())
410 if ((rName == xColumn->getString()) && !xColumn->wasNull())
412 Sequence<PropertyValue> aPropSequ(COLUMN_COUNT);
413 PropertyValue* pValues = aPropSequ.getArray();
414 BibConfig* pConfig = BibModul::GetConfig();
415 BibDBDescriptor aBibDesc = BibModul::GetConfig()->GetBibliographyURL();
416 const Mapping* pMapping = pConfig->GetMapping(aBibDesc);
417 for(sal_uInt16 nEntry = 0; nEntry < COLUMN_COUNT; nEntry++)
419 const OUString& sColName = pConfig->GetDefColumnName(
420 nEntry);
421 pValues[nEntry].Name = sColName;
422 pValues[nEntry].Value <<= lcl_AddProperty(xColumns, pMapping, sColName);
424 aRet <<= aPropSequ;
426 break;
429 while(xCursor->next());
432 catch(const Exception&)
434 DBG_UNHANDLED_EXCEPTION("extensions.biblio");
436 return aRet;
439 Sequence< OUString > BibliographyLoader::getElementNames()
441 Sequence< OUString > aRet(10);
442 int nRealNameCount = 0;
445 Reference< XResultSet > xCursor(GetDataCursor());
446 Reference< sdb::XColumn > xIdColumn(GetIdentifierColumn());
447 if (xIdColumn.is()) // implies xCursor.is()
451 OUString sTemp = xIdColumn->getString();
452 if (!sTemp.isEmpty() && !xIdColumn->wasNull())
454 int nLen = aRet.getLength();
455 if(nLen == nRealNameCount)
456 aRet.realloc(nLen + 10);
457 OUString* pArray = aRet.getArray();
458 pArray[nRealNameCount] = sTemp;
459 nRealNameCount++;
462 while (xCursor->next());
465 catch(const Exception&)
467 DBG_UNHANDLED_EXCEPTION("extensions.biblio");
470 aRet.realloc(nRealNameCount);
471 return aRet;
474 sal_Bool BibliographyLoader::hasByName(const OUString& rName)
476 bool bRet = false;
479 Reference< XResultSet > xCursor = GetDataCursor();
480 Reference< sdb::XColumn > xIdColumn = GetIdentifierColumn();
482 if (xIdColumn.is()) // implies xCursor.is()
486 OUString sCurrentId = xIdColumn->getString();
487 if (!xIdColumn->wasNull() && rName.startsWith(sCurrentId))
489 bRet = true;
490 break;
493 while(xCursor->next());
496 catch(const Exception&)
498 DBG_UNHANDLED_EXCEPTION("extensions.biblio");
500 return bRet;
503 Type BibliographyLoader::getElementType()
505 return cppu::UnoType<Sequence<PropertyValue>>::get();
508 sal_Bool BibliographyLoader::hasElements()
510 Reference< XNameAccess > xColumns = GetDataColumns();
511 return xColumns.is() && xColumns->getElementNames().hasElements();
514 Reference< XPropertySetInfo > BibliographyLoader::getPropertySetInfo()
516 static const SfxItemPropertyMapEntry aBibProps_Impl[] =
518 { u"BibliographyDataFieldNames"_ustr, 0, cppu::UnoType<Sequence<PropertyValue>>::get(), PropertyAttribute::READONLY, 0},
520 static Reference< XPropertySetInfo > xRet =
521 SfxItemPropertySet(aBibProps_Impl).getPropertySetInfo();
522 return xRet;
525 void BibliographyLoader::setPropertyValue(const OUString& /*PropertyName*/,
526 const Any& /*aValue*/)
528 throw UnknownPropertyException();
529 //no changeable properties
532 Any BibliographyLoader::getPropertyValue(const OUString& rPropertyName)
534 Any aRet;
535 static const sal_uInt16 aInternalMapping[] =
537 IDENTIFIER_POS , // BibliographyDataField_IDENTIFIER
538 AUTHORITYTYPE_POS , // BibliographyDataField_BIBILIOGRAPHIC_TYPE
539 ADDRESS_POS , // BibliographyDataField_ADDRESS
540 ANNOTE_POS , // BibliographyDataField_ANNOTE
541 AUTHOR_POS , // BibliographyDataField_AUTHOR
542 BOOKTITLE_POS , // BibliographyDataField_BOOKTITLE
543 CHAPTER_POS , // BibliographyDataField_CHAPTER
544 EDITION_POS , // BibliographyDataField_EDITION
545 EDITOR_POS , // BibliographyDataField_EDITOR
546 HOWPUBLISHED_POS , // BibliographyDataField_HOWPUBLISHED
547 INSTITUTION_POS , // BibliographyDataField_INSTITUTION
548 JOURNAL_POS , // BibliographyDataField_JOURNAL
549 MONTH_POS , // BibliographyDataField_MONTH
550 NOTE_POS , // BibliographyDataField_NOTE
551 NUMBER_POS , // BibliographyDataField_NUMBER
552 ORGANIZATIONS_POS , // BibliographyDataField_ORGANIZATIONS
553 PAGES_POS , // BibliographyDataField_PAGES
554 PUBLISHER_POS , // BibliographyDataField_PUBLISHER
555 SCHOOL_POS , // BibliographyDataField_SCHOOL
556 SERIES_POS , // BibliographyDataField_SERIES
557 TITLE_POS , // BibliographyDataField_TITLE
558 REPORTTYPE_POS , // BibliographyDataField_REPORT_TYPE
559 VOLUME_POS , // BibliographyDataField_VOLUME
560 YEAR_POS , // BibliographyDataField_YEAR
561 URL_POS , // BibliographyDataField_URL
562 CUSTOM1_POS , // BibliographyDataField_CUSTOM1
563 CUSTOM2_POS , // BibliographyDataField_CUSTOM2
564 CUSTOM3_POS , // BibliographyDataField_CUSTOM3
565 CUSTOM4_POS , // BibliographyDataField_CUSTOM4
566 CUSTOM5_POS , // BibliographyDataField_CUSTOM5
567 ISBN_POS , // BibliographyDataField_ISBN
568 LOCAL_URL_POS // BibliographyDataField_LOCAL_URL
570 if(rPropertyName != "BibliographyDataFieldNames")
571 throw UnknownPropertyException(rPropertyName);
572 Sequence<PropertyValue> aSeq(COLUMN_COUNT);
573 PropertyValue* pArray = aSeq.getArray();
574 BibConfig* pConfig = BibModul::GetConfig();
575 for(sal_uInt16 i = 0; i <= text::BibliographyDataField::LOCAL_URL ; i++)
577 pArray[i].Name = pConfig->GetDefColumnName(aInternalMapping[i]);
578 pArray[i].Value <<= static_cast<sal_Int16>(i);
580 aRet <<= aSeq;
581 return aRet;
584 void BibliographyLoader::addPropertyChangeListener(
585 const OUString& /*PropertyName*/, const Reference< XPropertyChangeListener > & /*aListener*/)
587 //no bound properties
590 void BibliographyLoader::removePropertyChangeListener(
591 const OUString& /*PropertyName*/, const Reference< XPropertyChangeListener > & /*aListener*/)
593 //no bound properties
596 void BibliographyLoader::addVetoableChangeListener(
597 const OUString& /*PropertyName*/, const Reference< XVetoableChangeListener > & /*aListener*/)
599 //no vetoable properties
602 void BibliographyLoader::removeVetoableChangeListener(
603 const OUString& /*PropertyName*/, const Reference< XVetoableChangeListener > & /*aListener*/)
605 //no vetoable properties
609 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */