bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / bibliography / datman.cxx
blob8f5871cd15499398f1fd06be2f25ca7a88e1061f
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 <osl/mutex.hxx>
21 #include <sal/log.hxx>
22 #include <tools/diagnose_ex.h>
23 #include <comphelper/processfactory.hxx>
24 #include <com/sun/star/io/XPersistObject.hpp>
25 #include <com/sun/star/sdbc/ResultSetType.hpp>
26 #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
27 #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
28 #include <com/sun/star/sdbcx/XRowLocate.hpp>
29 #include <com/sun/star/sdbc/DataType.hpp>
30 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
31 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
32 #include <com/sun/star/sdb/XDatabaseEnvironment.hpp>
33 #include <com/sun/star/uno/XNamingService.hpp>
34 #include <com/sun/star/sdbc/XDataSource.hpp>
35 #include <com/sun/star/sdb/CommandType.hpp>
36 #include <com/sun/star/sdb/DatabaseContext.hpp>
37 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
38 #include <com/sun/star/sdbc/XConnection.hpp>
39 #include <com/sun/star/sdb/XCompletedConnection.hpp>
40 #include <com/sun/star/task/InteractionHandler.hpp>
41 #include <com/sun/star/form/ListSourceType.hpp>
42 #include <com/sun/star/form/XLoadable.hpp>
43 #include <com/sun/star/form/runtime/FormController.hpp>
44 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
45 #include <com/sun/star/form/XGridColumnFactory.hpp>
46 #include <com/sun/star/io/XDataInputStream.hpp>
47 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
48 #include <com/sun/star/container/XNameContainer.hpp>
49 #include <comphelper/container.hxx>
50 #include <svl/urihelper.hxx>
51 #include <svtools/svtabbx.hxx>
52 #include <svtools/headbar.hxx>
53 #include <vcl/dialog.hxx>
54 #include <vcl/button.hxx>
55 #include <vcl/fixed.hxx>
56 #include <vcl/group.hxx>
57 #include <vcl/lstbox.hxx>
58 #include <vcl/edit.hxx>
59 #include <vcl/msgbox.hxx>
60 #include <tools/debug.hxx>
61 #include "datman.hxx"
62 #include "bibresid.hxx"
63 #include "bibmod.hxx"
64 #include "bibview.hxx"
65 #include "bibprop.hrc"
66 #include "toolbar.hxx"
67 #include "toolbar.hrc"
68 #include "bibconfig.hxx"
69 #include "bibbeam.hxx"
70 #include "general.hxx"
71 #include "bib.hrc"
72 #include "bibliography.hrc"
73 #include <connectivity/dbtools.hxx>
74 #include <boost/scoped_ptr.hpp>
76 using namespace ::com::sun::star;
77 using namespace ::com::sun::star::beans;
78 using namespace ::com::sun::star::container;
79 using namespace ::com::sun::star::uno;
80 using namespace ::com::sun::star::sdb;
81 using namespace ::com::sun::star::sdbc;
82 using namespace ::com::sun::star::sdbcx;
83 using namespace ::com::sun::star::form;
84 using namespace ::com::sun::star::frame;
85 using namespace ::com::sun::star::lang;
87 Reference< XConnection > getConnection(const OUString& _rURL)
89 // first get the sdb::DataSource corresponding to the url
90 Reference< XDataSource > xDataSource;
91 // is it a favorite title ?
92 Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
93 Reference< XDatabaseContext > xNamingContext = DatabaseContext::create(xContext);
94 if (xNamingContext->hasByName(_rURL))
96 DBG_ASSERT(xNamingContext.is(), "::getDataSource : no NamingService interface on the sdb::DatabaseAccessContext !");
97 try
99 xDataSource = Reference< XDataSource > (xNamingContext->getRegisteredObject(_rURL), UNO_QUERY);
101 catch (const Exception&)
103 OSL_FAIL("Exception caught in ODatabaseContext::getRegisteredObject()");
106 // build the connection from the data source
107 Reference< XConnection > xConn;
108 if (xDataSource.is())
110 // need user/pwd for this
111 Reference< XPropertySet > xDataSourceProps(xDataSource, UNO_QUERY);
112 Reference< XCompletedConnection > xComplConn(xDataSource, UNO_QUERY);
115 Reference<task::XInteractionHandler> xIHdl( task::InteractionHandler::createWithParent(xContext, 0), UNO_QUERY_THROW);
116 xConn = xComplConn->connectWithCompletion(xIHdl);
118 catch (const SQLException&)
120 // TODO : a real error handling
122 catch (const Exception&)
126 return xConn;
129 Reference< XConnection > getConnection(const Reference< XInterface > & xRowSet)
131 Reference< XConnection > xConn;
134 Reference< XPropertySet > xFormProps(xRowSet, UNO_QUERY);
135 if (!xFormProps.is())
136 return xConn;
138 xConn = Reference< XConnection > (*static_cast<Reference< XInterface > const *>(xFormProps->getPropertyValue("ActiveConnection").getValue()), UNO_QUERY);
139 if (!xConn.is())
141 DBG_WARNING("no active connection");
144 catch (const Exception&)
146 OSL_FAIL("exception in getConnection");
149 return xConn;
152 Reference< XNameAccess > getColumns(const Reference< XForm > & _rxForm)
154 Reference< XNameAccess > xReturn;
155 // check if the form is alive
156 Reference< XColumnsSupplier > xSupplyCols( _rxForm, UNO_QUERY );
157 if (xSupplyCols.is())
158 xReturn = xSupplyCols->getColumns();
160 if (!xReturn.is() || (xReturn->getElementNames().getLength() == 0))
161 { // no ....
162 xReturn = NULL;
163 // -> get the table the form is bound to and ask it for their columns
164 Reference< XTablesSupplier > xSupplyTables( getConnection( _rxForm ), UNO_QUERY );
165 Reference< XPropertySet > xFormProps( _rxForm, UNO_QUERY );
166 if (xFormProps.is() && xSupplyTables.is())
170 DBG_ASSERT((*static_cast<sal_Int32 const *>(xFormProps->getPropertyValue("CommandType").getValue())) == CommandType::TABLE,
171 "::getColumns : invalid form (has no table as data source) !");
172 OUString sTable;
173 xFormProps->getPropertyValue("Command") >>= sTable;
174 Reference< XNameAccess > xTables = xSupplyTables->getTables();
175 if (xTables.is() && xTables->hasByName(sTable))
176 xSupplyCols = Reference< XColumnsSupplier > (
177 *static_cast<Reference< XInterface > const *>(xTables->getByName(sTable).getValue()), UNO_QUERY);
178 if (xSupplyCols.is())
179 xReturn = xSupplyCols->getColumns();
181 catch (const Exception& e)
183 #ifdef DBG_UTIL
184 OUString sMsg( "::getColumns : catched an exception (" + e.Message + ") ..." );
186 OSL_FAIL(OUStringToOString(sMsg, RTL_TEXTENCODING_ASCII_US ).getStr());
187 #else
188 (void)e;
189 #endif
194 return xReturn;
197 class MappingDialog_Impl : public ModalDialog
199 BibDataManager* pDatMan;
200 VclPtr<OKButton> pOKBT;
201 VclPtr<ListBox> pIdentifierLB;
202 VclPtr<ListBox> pAuthorityTypeLB;
203 VclPtr<ListBox> pAuthorLB;
204 VclPtr<ListBox> pTitleLB;
205 VclPtr<ListBox> pMonthLB;
206 VclPtr<ListBox> pYearLB;
207 VclPtr<ListBox> pISBNLB;
208 VclPtr<ListBox> pBooktitleLB;
209 VclPtr<ListBox> pChapterLB;
210 VclPtr<ListBox> pEditionLB;
211 VclPtr<ListBox> pEditorLB;
212 VclPtr<ListBox> pHowpublishedLB;
213 VclPtr<ListBox> pInstitutionLB;
214 VclPtr<ListBox> pJournalLB;
215 VclPtr<ListBox> pNoteLB;
216 VclPtr<ListBox> pAnnoteLB;
217 VclPtr<ListBox> pNumberLB;
218 VclPtr<ListBox> pOrganizationsLB;
219 VclPtr<ListBox> pPagesLB;
220 VclPtr<ListBox> pPublisherLB;
221 VclPtr<ListBox> pAddressLB;
222 VclPtr<ListBox> pSchoolLB;
223 VclPtr<ListBox> pSeriesLB;
224 VclPtr<ListBox> pReportTypeLB;
225 VclPtr<ListBox> pVolumeLB;
226 VclPtr<ListBox> pURLLB;
227 VclPtr<ListBox> pCustom1LB;
228 VclPtr<ListBox> pCustom2LB;
229 VclPtr<ListBox> pCustom3LB;
230 VclPtr<ListBox> pCustom4LB;
231 VclPtr<ListBox> pCustom5LB;
232 VclPtr<ListBox> aListBoxes[COLUMN_COUNT];
233 OUString sNone;
235 bool bModified;
239 DECL_LINK(OkHdl, void *);
240 DECL_LINK(ListBoxSelectHdl, ListBox*);
242 public:
243 MappingDialog_Impl(vcl::Window* pParent, BibDataManager* pDatMan);
244 virtual ~MappingDialog_Impl();
245 virtual void dispose() SAL_OVERRIDE;
247 void SetModified() {bModified = true;}
251 static sal_uInt16 lcl_FindLogicalName(BibConfig* pConfig ,
252 const OUString& rLogicalColumnName)
254 for(sal_uInt16 i = 0; i < COLUMN_COUNT; i++)
256 if(rLogicalColumnName == pConfig->GetDefColumnName(i))
257 return i;
259 return USHRT_MAX;
262 MappingDialog_Impl::MappingDialog_Impl(vcl::Window* pParent, BibDataManager* pMan)
263 : ModalDialog(pParent, "MappingDialog", "modules/sbibliography/ui/mappingdialog.ui")
264 , pDatMan(pMan)
265 , sNone(BIB_RESSTR(RID_BIB_STR_NONE))
266 , bModified(false)
268 get(pOKBT, "ok");
269 get(pIdentifierLB, "identifierCombobox");
270 get(pAuthorityTypeLB, "authorityTypeCombobox");
271 get(pAuthorLB, "authorCombobox");
272 get(pTitleLB, "titleCombobox");
273 get(pMonthLB, "monthCombobox");
274 get(pYearLB, "yearCombobox");
275 get(pISBNLB, "ISBNCombobox");
276 get(pBooktitleLB, "bookTitleCombobox");
277 get(pChapterLB, "chapterCombobox");
278 get(pEditionLB, "editionCombobox");
279 get(pEditorLB, "editorCombobox");
280 get(pHowpublishedLB, "howPublishedCombobox");
281 get(pInstitutionLB, "institutionCombobox");
282 get(pJournalLB, "journalCombobox");
283 get(pNoteLB, "noteCombobox");
284 get(pAnnoteLB, "annoteCombobox");
285 get(pNumberLB, "numberCombobox");
286 get(pOrganizationsLB, "organizationCombobox");
287 get(pPagesLB, "pagesCombobox");
288 get(pPublisherLB, "publisherCombobox");
289 get(pAddressLB, "addressCombobox");
290 get(pSchoolLB, "schoolCombobox");
291 get(pSeriesLB, "seriesCombobox");
292 get(pReportTypeLB, "reportTypeCombobox");
293 get(pVolumeLB, "volumeCombobox");
294 get(pURLLB, "URLCombobox");
295 get(pCustom1LB, "custom1Combobox");
296 get(pCustom2LB, "custom2Combobox");
297 get(pCustom3LB, "custom3Combobox");
298 get(pCustom4LB, "custom4Combobox");
299 get(pCustom5LB, "custom5Combobox");
301 pOKBT->SetClickHdl(LINK(this, MappingDialog_Impl, OkHdl));
302 OUString sTitle = GetText();
303 sTitle = sTitle.replaceFirst("%1", pDatMan->getActiveDataTable());
304 SetText(sTitle);
306 aListBoxes[0] = pIdentifierLB;
307 aListBoxes[1] = pAuthorityTypeLB;
308 aListBoxes[2] = pAuthorLB;
309 aListBoxes[3] = pTitleLB;
310 aListBoxes[4] = pYearLB;
311 aListBoxes[5] = pISBNLB;
312 aListBoxes[6] = pBooktitleLB;
313 aListBoxes[7] = pChapterLB;
314 aListBoxes[8] = pEditionLB;
315 aListBoxes[9] = pEditorLB;
316 aListBoxes[10] = pHowpublishedLB;
317 aListBoxes[11] = pInstitutionLB;
318 aListBoxes[12] = pJournalLB;
319 aListBoxes[13] = pMonthLB;
320 aListBoxes[14] = pNoteLB;
321 aListBoxes[15] = pAnnoteLB;
322 aListBoxes[16] = pNumberLB;
323 aListBoxes[17] = pOrganizationsLB;
324 aListBoxes[18] = pPagesLB;
325 aListBoxes[19] = pPublisherLB;
326 aListBoxes[20] = pAddressLB;
327 aListBoxes[21] = pSchoolLB;
328 aListBoxes[22] = pSeriesLB;
329 aListBoxes[23] = pReportTypeLB;
330 aListBoxes[24] = pVolumeLB;
331 aListBoxes[25] = pURLLB;
332 aListBoxes[26] = pCustom1LB;
333 aListBoxes[27] = pCustom2LB;
334 aListBoxes[28] = pCustom3LB;
335 aListBoxes[29] = pCustom4LB;
336 aListBoxes[30] = pCustom5LB;
338 aListBoxes[0]->InsertEntry(sNone);
339 Reference< XNameAccess > xFields = getColumns( pDatMan->getForm() );
340 DBG_ASSERT(xFields.is(), "MappingDialog_Impl::MappingDialog_Impl : gave me an invalid form !");
341 if(xFields.is())
343 Sequence< OUString > aNames = xFields->getElementNames();
344 sal_Int32 nFieldsCount = aNames.getLength();
345 const OUString* pNames = aNames.getConstArray();
347 for(sal_Int32 nField = 0; nField < nFieldsCount; nField++)
348 aListBoxes[0]->InsertEntry(pNames[nField]);
351 Link<> aLnk = LINK(this, MappingDialog_Impl, ListBoxSelectHdl);
353 aListBoxes[0]->SelectEntryPos(0);
354 aListBoxes[0]->SetSelectHdl(aLnk);
355 for(sal_uInt16 i = 1; i < COLUMN_COUNT; i++)
357 for(sal_uInt16 j = 0; j < aListBoxes[0]->GetEntryCount();j++)
358 aListBoxes[i]->InsertEntry(aListBoxes[0]->GetEntry(j));
359 aListBoxes[i]->SelectEntryPos(0);
360 aListBoxes[i]->SetSelectHdl(aLnk);
362 BibConfig* pConfig = BibModul::GetConfig();
363 BibDBDescriptor aDesc;
364 aDesc.sDataSource = pDatMan->getActiveDataSource();
365 aDesc.sTableOrQuery = pDatMan->getActiveDataTable();
366 aDesc.nCommandType = CommandType::TABLE;
367 const Mapping* pMapping = pConfig->GetMapping(aDesc);
368 if(pMapping)
370 for(sal_uInt16 nEntry = 0; nEntry < COLUMN_COUNT; nEntry++)
372 sal_uInt16 nListBoxIndex = lcl_FindLogicalName( pConfig, pMapping->aColumnPairs[nEntry].sLogicalColumnName);
373 if(nListBoxIndex < COLUMN_COUNT)
375 aListBoxes[nListBoxIndex]->SelectEntry(pMapping->aColumnPairs[nEntry].sRealColumnName);
381 MappingDialog_Impl::~MappingDialog_Impl()
383 disposeOnce();
386 void MappingDialog_Impl::dispose()
388 pOKBT.clear();
389 pIdentifierLB.clear();
390 pAuthorityTypeLB.clear();
391 pAuthorLB.clear();
392 pTitleLB.clear();
393 pMonthLB.clear();
394 pYearLB.clear();
395 pISBNLB.clear();
396 pBooktitleLB.clear();
397 pChapterLB.clear();
398 pEditionLB.clear();
399 pEditorLB.clear();
400 pHowpublishedLB.clear();
401 pInstitutionLB.clear();
402 pJournalLB.clear();
403 pNoteLB.clear();
404 pAnnoteLB.clear();
405 pNumberLB.clear();
406 pOrganizationsLB.clear();
407 pPagesLB.clear();
408 pPublisherLB.clear();
409 pAddressLB.clear();
410 pSchoolLB.clear();
411 pSeriesLB.clear();
412 pReportTypeLB.clear();
413 pVolumeLB.clear();
414 pURLLB.clear();
415 pCustom1LB.clear();
416 pCustom2LB.clear();
417 pCustom3LB.clear();
418 pCustom4LB.clear();
419 pCustom5LB.clear();
420 for(auto & a : aListBoxes)
421 a.clear();
422 ModalDialog::dispose();
425 IMPL_LINK(MappingDialog_Impl, ListBoxSelectHdl, ListBox*, pListBox)
427 sal_uInt16 nEntryPos = pListBox->GetSelectEntryPos();
428 if(0 < nEntryPos)
430 for(sal_uInt16 i = 0; i < COLUMN_COUNT; i++)
432 if(pListBox != aListBoxes[i] && aListBoxes[i]->GetSelectEntryPos() == nEntryPos)
433 aListBoxes[i]->SelectEntryPos(0);
436 SetModified();
437 return 0;
440 IMPL_LINK_NOARG(MappingDialog_Impl, OkHdl)
442 if(bModified)
444 Mapping aNew;
445 aNew.sTableName = pDatMan->getActiveDataTable();
446 aNew.sURL = pDatMan->getActiveDataSource();
448 sal_uInt16 nWriteIndex = 0;
449 BibConfig* pConfig = BibModul::GetConfig();
450 for(sal_uInt16 nEntry = 0; nEntry < COLUMN_COUNT; nEntry++)
452 OUString sSel = aListBoxes[nEntry]->GetSelectEntry();
453 if(sSel != sNone)
455 aNew.aColumnPairs[nWriteIndex].sRealColumnName = sSel;
456 aNew.aColumnPairs[nWriteIndex].sLogicalColumnName = pConfig->GetDefColumnName(nEntry);
457 nWriteIndex++;
460 BibDBDescriptor aDesc;
461 aDesc.sDataSource = pDatMan->getActiveDataSource();
462 aDesc.sTableOrQuery = pDatMan->getActiveDataTable();
463 aDesc.nCommandType = CommandType::TABLE;
464 pDatMan->ResetIdentifierMapping();
465 pConfig->SetMapping(aDesc, &aNew);
467 EndDialog(bModified ? RET_OK : RET_CANCEL);
468 return 0;
471 class DBChangeDialog_Impl : public ModalDialog
473 VclPtr<ListBox> m_pSelectionLB;
474 DBChangeDialogConfig_Impl aConfig;
476 BibDataManager* pDatMan;
478 DECL_LINK(DoubleClickHdl, SvTabListBox*);
479 public:
480 DBChangeDialog_Impl(vcl::Window* pParent, BibDataManager* pMan );
481 virtual ~DBChangeDialog_Impl();
482 virtual void dispose() SAL_OVERRIDE;
484 OUString GetCurrentURL()const;
487 DBChangeDialog_Impl::DBChangeDialog_Impl(vcl::Window* pParent, BibDataManager* pMan )
488 : ModalDialog(pParent, "ChooseDataSourceDialog",
489 "modules/sbibliography/ui/choosedatasourcedialog.ui")
491 pDatMan(pMan)
493 get(m_pSelectionLB, "treeview");
494 m_pSelectionLB->set_height_request(m_pSelectionLB->GetTextHeight() * 6);
496 m_pSelectionLB->SetStyle(m_pSelectionLB->GetStyle() | WB_SORT);
497 m_pSelectionLB->SetDoubleClickHdl( LINK(this, DBChangeDialog_Impl, DoubleClickHdl));
501 OUString sActiveSource = pDatMan->getActiveDataSource();
502 const Sequence< OUString >& rSources = aConfig.GetDataSourceNames();
503 const OUString* pSourceNames = rSources.getConstArray();
504 for (sal_Int32 i = 0; i < rSources.getLength(); ++i)
505 m_pSelectionLB->InsertEntry(pSourceNames[i]);
507 m_pSelectionLB->SelectEntry(sActiveSource);
509 catch (const Exception& e)
511 SAL_WARN("extensions.biblio",
512 "Exception in BibDataManager::DBChangeDialog_Impl::DBChangeDialog_Impl "
513 << e.Message);
517 IMPL_LINK(DBChangeDialog_Impl, DoubleClickHdl, SvTabListBox*, /*pLB*/)
519 EndDialog(RET_OK);
520 return 0;
523 DBChangeDialog_Impl::~DBChangeDialog_Impl()
525 disposeOnce();
528 void DBChangeDialog_Impl::dispose()
530 m_pSelectionLB.clear();
531 ModalDialog::dispose();
534 OUString DBChangeDialog_Impl::GetCurrentURL()const
536 return m_pSelectionLB->GetSelectEntry();
539 // XDispatchProvider
540 BibInterceptorHelper::BibInterceptorHelper( ::bib::BibBeamer* pBibBeamer, ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > xDispatch)
542 if( pBibBeamer )
544 xInterception = pBibBeamer->getDispatchProviderInterception();
545 if( xInterception.is() )
546 xInterception->registerDispatchProviderInterceptor( this );
548 if( xDispatch.is() )
549 xFormDispatch = xDispatch;
552 BibInterceptorHelper::~BibInterceptorHelper( )
556 void BibInterceptorHelper::ReleaseInterceptor()
558 if ( xInterception.is() )
559 xInterception->releaseDispatchProviderInterceptor( this );
560 xInterception.clear();
563 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > SAL_CALL
564 BibInterceptorHelper::queryDispatch( const ::com::sun::star::util::URL& aURL, const OUString& aTargetFrameName, sal_Int32 nSearchFlags ) throw (::com::sun::star::uno::RuntimeException, std::exception)
566 Reference< XDispatch > xReturn;
568 OUString aCommand( aURL.Path );
569 if ( aCommand == "FormSlots/ConfirmDeletion" )
570 xReturn = xFormDispatch;
571 else
572 if ( xSlaveDispatchProvider.is() )
573 xReturn = xSlaveDispatchProvider->queryDispatch( aURL, aTargetFrameName, nSearchFlags);
575 return xReturn;
578 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > > SAL_CALL
579 BibInterceptorHelper::queryDispatches( const ::com::sun::star::uno::Sequence< ::com::sun::star::frame::DispatchDescriptor >& aDescripts ) throw (::com::sun::star::uno::RuntimeException, std::exception)
581 Sequence< Reference< XDispatch> > aReturn( aDescripts.getLength() );
582 Reference< XDispatch >* pReturn = aReturn.getArray();
583 const DispatchDescriptor* pDescripts = aDescripts.getConstArray();
584 for ( sal_Int16 i=0; i<aDescripts.getLength(); ++i, ++pReturn, ++pDescripts )
586 *pReturn = queryDispatch( pDescripts->FeatureURL, pDescripts->FrameName, pDescripts->SearchFlags );
588 return aReturn;
591 // XDispatchProviderInterceptor
592 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider > SAL_CALL
593 BibInterceptorHelper::getSlaveDispatchProvider( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
595 return xSlaveDispatchProvider;
598 void SAL_CALL BibInterceptorHelper::setSlaveDispatchProvider( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider >& xNewSlaveDispatchProvider ) throw (::com::sun::star::uno::RuntimeException, std::exception)
600 xSlaveDispatchProvider = xNewSlaveDispatchProvider;
603 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider > SAL_CALL
604 BibInterceptorHelper::getMasterDispatchProvider( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
606 return xMasterDispatchProvider;
609 void SAL_CALL BibInterceptorHelper::setMasterDispatchProvider( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider >& xNewMasterDispatchProvider ) throw (::com::sun::star::uno::RuntimeException, std::exception)
611 xMasterDispatchProvider = xNewMasterDispatchProvider;
615 #define STR_UID "uid"
616 OUString gGridName("theGrid");
617 OUString gViewName("theView");
618 OUString gGlobalName("theGlobals");
619 OUString gBeamerSize("theBeamerSize");
620 OUString gViewSize("theViewSize");
622 BibDataManager::BibDataManager()
623 :BibDataManager_Base( GetMutex() )
624 ,m_pInterceptorHelper( NULL )
625 ,m_aLoadListeners(m_aMutex)
626 ,pBibView( NULL )
627 ,pToolbar(0)
632 BibDataManager::~BibDataManager()
634 Reference< XLoadable > xLoad( m_xForm, UNO_QUERY );
635 Reference< XPropertySet > xPrSet( m_xForm, UNO_QUERY );
636 Reference< XComponent > xComp( m_xForm, UNO_QUERY );
637 if ( m_xForm.is() )
639 Reference< XComponent > xConnection;
640 xPrSet->getPropertyValue("ActiveConnection") >>= xConnection;
641 RemoveMeAsUidListener();
642 if (xLoad.is())
643 xLoad->unload();
644 if (xComp.is())
645 xComp->dispose();
646 if(xConnection.is())
647 xConnection->dispose();
648 m_xForm = NULL;
650 if( m_pInterceptorHelper )
652 m_pInterceptorHelper->ReleaseInterceptor();
653 m_pInterceptorHelper->release();
654 m_pInterceptorHelper = NULL;
658 void BibDataManager::InsertFields(const Reference< XFormComponent > & _rxGrid)
660 if ( !_rxGrid.is() )
661 return;
665 Reference< XNameContainer > xColContainer( _rxGrid, UNO_QUERY );
666 // remove the old fields
667 if ( xColContainer->hasElements() )
669 Sequence< OUString > aNames = xColContainer->getElementNames();
670 const OUString* pNames = aNames.getConstArray();
671 const OUString* pNamesEnd = pNames + aNames.getLength();
672 for ( ; pNames != pNamesEnd; ++pNames )
673 xColContainer->removeByName( *pNames );
676 Reference< XNameAccess > xFields = getColumns( m_xForm );
677 if (!xFields.is())
678 return;
680 Reference< XGridColumnFactory > xColFactory( _rxGrid, UNO_QUERY );
682 Reference< XPropertySet > xField;
684 Sequence< OUString > aFields( xFields->getElementNames() );
685 const OUString* pFields = aFields.getConstArray();
686 const OUString* pFieldsEnd = pFields + aFields.getLength();
688 for ( ; pFields != pFieldsEnd; ++pFields )
690 xFields->getByName( *pFields ) >>= xField;
692 OUString sCurrentModelType;
693 const OUString sType("Type");
694 sal_Int32 nType = 0;
695 bool bIsFormatted = false;
696 sal_Bool bFormattedIsNumeric = sal_True;
697 xField->getPropertyValue(sType) >>= nType;
698 switch(nType)
700 case DataType::BIT:
701 case DataType::BOOLEAN:
702 sCurrentModelType = "CheckBox";
703 break;
705 case DataType::BINARY:
706 case DataType::VARBINARY:
707 case DataType::LONGVARBINARY:
708 case DataType::BLOB:
709 sCurrentModelType = "TextField";
710 break;
712 case DataType::VARCHAR:
713 case DataType::LONGVARCHAR:
714 case DataType::CHAR:
715 case DataType::CLOB:
716 bFormattedIsNumeric = sal_False;
717 // _NO_ break !
718 default:
719 sCurrentModelType = "FormattedField";
720 bIsFormatted = true;
721 break;
724 Reference< XPropertySet > xCurrentCol = xColFactory->createColumn(sCurrentModelType);
725 if (bIsFormatted)
727 OUString sFormatKey("FormatKey");
728 xCurrentCol->setPropertyValue(sFormatKey, xField->getPropertyValue(sFormatKey));
729 Any aFormatted(&bFormattedIsNumeric, cppu::UnoType<bool>::get());
730 xCurrentCol->setPropertyValue("TreatAsNumber", aFormatted);
732 Any aColName = makeAny( *pFields );
733 xCurrentCol->setPropertyValue(FM_PROP_CONTROLSOURCE, aColName);
734 xCurrentCol->setPropertyValue(FM_PROP_LABEL, aColName);
736 xColContainer->insertByName( *pFields, makeAny( xCurrentCol ) );
739 catch (const Exception&)
741 OSL_FAIL("Exception in BibDataManager::InsertFields");
745 Reference< awt::XControlModel > BibDataManager::updateGridModel()
747 return updateGridModel( m_xForm );
750 Reference< awt::XControlModel > BibDataManager::updateGridModel(const Reference< XForm > & xDbForm)
754 Reference< XPropertySet > aFormPropSet( xDbForm, UNO_QUERY );
755 OUString sName;
756 aFormPropSet->getPropertyValue("Command") >>= sName;
758 if ( !m_xGridModel.is() )
760 m_xGridModel = createGridModel( gGridName );
762 Reference< XNameContainer > xNameCont(xDbForm, UNO_QUERY);
763 xNameCont->insertByName( sName, makeAny( m_xGridModel ) );
766 // insert the fields
767 Reference< XFormComponent > xFormComp( m_xGridModel, UNO_QUERY );
768 InsertFields( xFormComp );
770 catch (const Exception&)
772 OSL_FAIL("::updateGridModel: something went wrong !");
775 return m_xGridModel;
778 Reference< XForm > BibDataManager::createDatabaseForm(BibDBDescriptor& rDesc)
780 Reference< XForm > xResult;
783 Reference< XMultiServiceFactory > xMgr = comphelper::getProcessServiceFactory();
784 m_xForm = Reference< XForm > ( xMgr->createInstance( "com.sun.star.form.component.Form" ), UNO_QUERY );
786 Reference< XPropertySet > aPropertySet( m_xForm, UNO_QUERY );
788 aDataSourceURL = rDesc.sDataSource;
789 if(aPropertySet.is())
791 Any aVal;
792 aVal <<= (sal_Int32)ResultSetType::SCROLL_INSENSITIVE;
793 aPropertySet->setPropertyValue("ResultSetType",aVal );
794 aVal <<= (sal_Int32)ResultSetConcurrency::READ_ONLY;
795 aPropertySet->setPropertyValue("ResultSetConcurrency", aVal);
797 //Caching for Performance
798 aVal <<= (sal_Int32)50;
799 aPropertySet->setPropertyValue("FetchSize", aVal);
801 Reference< XConnection > xConnection = getConnection(rDesc.sDataSource);
802 aVal <<= xConnection;
803 aPropertySet->setPropertyValue("ActiveConnection", aVal);
805 Reference< XTablesSupplier > xSupplyTables(xConnection, UNO_QUERY);
806 Reference< XNameAccess > xTables = xSupplyTables.is() ?
807 xSupplyTables->getTables() : Reference< XNameAccess > ();
809 Sequence< OUString > aTableNameSeq;
810 if (xTables.is())
811 aTableNameSeq = xTables->getElementNames();
813 if(aTableNameSeq.getLength() > 0)
815 const OUString* pTableNames = aTableNameSeq.getConstArray();
816 if(!rDesc.sTableOrQuery.isEmpty())
817 aActiveDataTable = rDesc.sTableOrQuery;
818 else
820 rDesc.sTableOrQuery = aActiveDataTable = pTableNames[0];
821 rDesc.nCommandType = CommandType::TABLE;
824 aVal <<= aActiveDataTable;
825 aPropertySet->setPropertyValue("Command", aVal);
826 aVal <<= rDesc.nCommandType;
827 aPropertySet->setPropertyValue("CommandType", aVal);
830 Reference< XDatabaseMetaData > xMetaData = xConnection->getMetaData();
831 aQuoteChar = xMetaData->getIdentifierQuoteString();
833 Reference< XMultiServiceFactory > xFactory(xConnection, UNO_QUERY);
834 if ( xFactory.is() )
835 m_xParser.set( xFactory->createInstance("com.sun.star.sdb.SingleSelectQueryComposer"), UNO_QUERY );
837 OUString aString("SELECT * FROM ");
839 OUString sCatalog, sSchema, sName;
840 ::dbtools::qualifiedNameComponents( xMetaData, aActiveDataTable, sCatalog, sSchema, sName, ::dbtools::eInDataManipulation );
841 aString += ::dbtools::composeTableNameForSelect( xConnection, sCatalog, sSchema, sName );
843 m_xParser->setElementaryQuery(aString);
844 BibConfig* pConfig = BibModul::GetConfig();
845 pConfig->setQueryField(getQueryField());
846 startQueryWith(pConfig->getQueryText());
848 xResult = m_xForm;
852 catch (const Exception&)
854 OSL_FAIL("::createDatabaseForm: something went wrong !");
857 return xResult;
860 Sequence< OUString > BibDataManager::getDataSources()
862 Sequence< OUString > aTableNameSeq;
866 Reference< XTablesSupplier > xSupplyTables( getConnection( m_xForm ), UNO_QUERY );
867 Reference< XNameAccess > xTables;
868 if (xSupplyTables.is())
869 xTables = xSupplyTables->getTables();
870 if (xTables.is())
871 aTableNameSeq = xTables->getElementNames();
873 catch (const Exception&)
875 OSL_FAIL("::getDataSources: something went wrong !");
878 return aTableNameSeq;
882 void BibDataManager::setFilter(const OUString& rQuery)
884 if(!m_xParser.is())
885 return;
888 m_xParser->setFilter( rQuery );
889 OUString aQuery = m_xParser->getFilter();
890 Reference< XPropertySet > xFormProps( m_xForm, UNO_QUERY_THROW );
891 xFormProps->setPropertyValue( "Filter", makeAny( aQuery ) );
892 xFormProps->setPropertyValue( "ApplyFilter", makeAny( sal_True ) );
893 reload();
895 catch (const Exception&)
897 DBG_UNHANDLED_EXCEPTION();
903 OUString BibDataManager::getFilter()
906 OUString aQueryString;
909 Reference< XPropertySet > xFormProps( m_xForm, UNO_QUERY_THROW );
910 OSL_VERIFY( xFormProps->getPropertyValue( "Filter" ) >>= aQueryString );
912 catch (const Exception&)
914 DBG_UNHANDLED_EXCEPTION();
918 return aQueryString;
922 Sequence< OUString > BibDataManager::getQueryFields()
924 Sequence< OUString > aFieldSeq;
925 Reference< XNameAccess > xFields = getColumns( m_xForm );
926 if (xFields.is())
927 aFieldSeq = xFields->getElementNames();
928 return aFieldSeq;
931 OUString BibDataManager::getQueryField()
933 BibConfig* pConfig = BibModul::GetConfig();
934 OUString aFieldString = pConfig->getQueryField();
935 if(aFieldString.isEmpty())
937 Sequence< OUString > aSeq = getQueryFields();
938 const OUString* pFields = aSeq.getConstArray();
939 if(aSeq.getLength()>0)
941 aFieldString=pFields[0];
944 return aFieldString;
947 void BibDataManager::startQueryWith(const OUString& rQuery)
949 BibConfig* pConfig = BibModul::GetConfig();
950 pConfig->setQueryText( rQuery );
952 OUString aQueryString;
953 if(!rQuery.isEmpty())
955 aQueryString=aQuoteChar;
956 aQueryString+=getQueryField();
957 aQueryString+=aQuoteChar;
958 aQueryString+=" like '";
959 OUString sQuery(rQuery);
960 sQuery = sQuery.replaceAll("?","_");
961 sQuery = sQuery.replaceAll("*","%");
962 aQueryString += sQuery;
963 aQueryString+="%'";
965 setFilter(aQueryString);
968 void BibDataManager::setActiveDataSource(const OUString& rURL)
970 OUString sTmp(aDataSourceURL);
971 aDataSourceURL = rURL;
973 Reference< XPropertySet > aPropertySet( m_xForm, UNO_QUERY );
974 if(aPropertySet.is())
976 unload();
978 Reference< XComponent > xOldConnection;
979 aPropertySet->getPropertyValue("ActiveConnection") >>= xOldConnection;
981 Reference< XConnection > xConnection = getConnection(rURL);
982 if(!xConnection.is())
984 aDataSourceURL = sTmp;
985 return;
987 Any aVal; aVal <<= xConnection;
988 aPropertySet->setPropertyValue("ActiveConnection", aVal);
989 Reference< XMultiServiceFactory > xFactory(xConnection, UNO_QUERY);
990 if ( xFactory.is() )
991 m_xParser.set( xFactory->createInstance("com.sun.star.sdb.SingleSelectQueryComposer"), UNO_QUERY );
993 if(xOldConnection.is())
994 xOldConnection->dispose();
996 Sequence< OUString > aTableNameSeq;
997 Reference< XTablesSupplier > xSupplyTables(xConnection, UNO_QUERY);
998 if(xSupplyTables.is())
1000 Reference< XNameAccess > xAccess = xSupplyTables->getTables();
1001 aTableNameSeq = xAccess->getElementNames();
1003 if(aTableNameSeq.getLength() > 0)
1005 const OUString* pTableNames = aTableNameSeq.getConstArray();
1006 aActiveDataTable = pTableNames[0];
1007 aVal <<= aActiveDataTable;
1008 aPropertySet->setPropertyValue("Command", aVal);
1009 aPropertySet->setPropertyValue("CommandType", makeAny(CommandType::TABLE));
1010 //Caching for Performance
1011 aVal <<= (sal_Int32)50;
1012 aPropertySet->setPropertyValue("FetchSize", aVal);
1013 OUString aString("SELECT * FROM ");
1014 // quote the table name which may contain catalog.schema.table
1015 Reference<XDatabaseMetaData> xMetaData(xConnection->getMetaData(),UNO_QUERY);
1016 aQuoteChar = xMetaData->getIdentifierQuoteString();
1018 OUString sCatalog, sSchema, sName;
1019 ::dbtools::qualifiedNameComponents( xMetaData, aActiveDataTable, sCatalog, sSchema, sName, ::dbtools::eInDataManipulation );
1020 aString += ::dbtools::composeTableNameForSelect( xConnection, sCatalog, sSchema, sName );
1022 m_xParser->setElementaryQuery(aString);
1023 BibConfig* pConfig = BibModul::GetConfig();
1024 pConfig->setQueryField(getQueryField());
1025 startQueryWith(pConfig->getQueryText());
1026 setActiveDataTable(aActiveDataTable);
1028 FeatureStateEvent aEvent;
1029 util::URL aURL;
1030 aEvent.IsEnabled = sal_True;
1031 aEvent.Requery = sal_False;
1032 aEvent.FeatureDescriptor = getActiveDataTable();
1034 aEvent.State = makeAny( getDataSources() );
1036 if(pToolbar)
1038 aURL.Complete =".uno:Bib/source";
1039 aEvent.FeatureURL = aURL;
1040 pToolbar->statusChanged( aEvent );
1043 updateGridModel();
1044 load();
1049 void BibDataManager::setActiveDataTable(const OUString& rTable)
1051 ResetIdentifierMapping();
1054 Reference< XPropertySet > aPropertySet( m_xForm, UNO_QUERY );
1056 if(aPropertySet.is())
1058 Reference< XConnection > xConnection = getConnection( m_xForm );
1059 Reference< XTablesSupplier > xSupplyTables(xConnection, UNO_QUERY);
1060 Reference< XNameAccess > xAccess = xSupplyTables->getTables();
1061 Sequence< OUString > aTableNameSeq = xAccess->getElementNames();
1062 sal_uInt32 nCount = aTableNameSeq.getLength();
1064 const OUString* pTableNames = aTableNameSeq.getConstArray();
1065 const OUString* pTableNamesEnd = pTableNames + nCount;
1067 for ( ; pTableNames != pTableNamesEnd; ++pTableNames )
1069 if ( rTable == *pTableNames )
1071 aActiveDataTable = rTable;
1072 Any aVal; aVal <<= rTable;
1073 aPropertySet->setPropertyValue( "Command", aVal );
1074 break;
1077 if (pTableNames != pTableNamesEnd)
1079 Reference< XDatabaseMetaData > xMetaData = xConnection->getMetaData();
1080 aQuoteChar = xMetaData->getIdentifierQuoteString();
1082 Reference< XMultiServiceFactory > xFactory(xConnection, UNO_QUERY);
1083 if ( xFactory.is() )
1084 m_xParser.set( xFactory->createInstance("com.sun.star.sdb.SingleSelectQueryComposer"), UNO_QUERY );
1086 OUString aString("SELECT * FROM ");
1088 OUString sCatalog, sSchema, sName;
1089 ::dbtools::qualifiedNameComponents( xMetaData, aActiveDataTable, sCatalog, sSchema, sName, ::dbtools::eInDataManipulation );
1090 aString += ::dbtools::composeTableNameForSelect( xConnection, sCatalog, sSchema, sName );
1092 m_xParser->setElementaryQuery(aString);
1094 BibConfig* pConfig = BibModul::GetConfig();
1095 pConfig->setQueryField(getQueryField());
1096 startQueryWith(pConfig->getQueryText());
1098 BibDBDescriptor aDesc;
1099 aDesc.sDataSource = aDataSourceURL;
1100 aDesc.sTableOrQuery = aActiveDataTable;
1101 aDesc.nCommandType = CommandType::TABLE;
1102 BibModul::GetConfig()->SetBibliographyURL(aDesc);
1106 catch (const Exception&)
1108 OSL_FAIL("::setActiveDataTable: something went wrong !");
1113 void SAL_CALL BibDataManager::load( ) throw (RuntimeException, std::exception)
1115 if ( isLoaded() )
1116 // nothing to do
1117 return;
1119 Reference< XLoadable > xFormAsLoadable( m_xForm, UNO_QUERY );
1120 DBG_ASSERT( xFormAsLoadable.is() || !m_xForm.is(), "BibDataManager::load: invalid form!");
1121 if ( xFormAsLoadable.is() )
1123 xFormAsLoadable->load();
1124 SetMeAsUidListener();
1126 EventObject aEvt( static_cast< XWeak* >( this ) );
1127 m_aLoadListeners.notifyEach( &XLoadListener::loaded, aEvt );
1132 void SAL_CALL BibDataManager::unload( ) throw (RuntimeException, std::exception)
1134 if ( !isLoaded() )
1135 // nothing to do
1136 return;
1138 Reference< XLoadable >xFormAsLoadable( m_xForm, UNO_QUERY );
1139 DBG_ASSERT( xFormAsLoadable.is() || !m_xForm.is(), "BibDataManager::unload: invalid form!");
1140 if ( xFormAsLoadable.is() )
1142 EventObject aEvt( static_cast< XWeak* >( this ) );
1145 m_aLoadListeners.notifyEach( &XLoadListener::unloading, aEvt );
1148 RemoveMeAsUidListener();
1149 xFormAsLoadable->unload();
1152 m_aLoadListeners.notifyEach( &XLoadListener::unloaded, aEvt );
1158 void SAL_CALL BibDataManager::reload( ) throw (RuntimeException, std::exception)
1160 if ( !isLoaded() )
1161 // nothing to do
1162 return;
1164 Reference< XLoadable >xFormAsLoadable( m_xForm, UNO_QUERY );
1165 DBG_ASSERT( xFormAsLoadable.is() || !m_xForm.is(), "BibDataManager::unload: invalid form!");
1166 if ( xFormAsLoadable.is() )
1168 EventObject aEvt( static_cast< XWeak* >( this ) );
1171 m_aLoadListeners.notifyEach( &XLoadListener::reloading, aEvt );
1174 xFormAsLoadable->reload();
1177 m_aLoadListeners.notifyEach( &XLoadListener::reloaded, aEvt );
1183 sal_Bool SAL_CALL BibDataManager::isLoaded( ) throw (RuntimeException, std::exception)
1185 Reference< XLoadable >xFormAsLoadable( m_xForm, UNO_QUERY );
1186 DBG_ASSERT( xFormAsLoadable.is() || !m_xForm.is(), "BibDataManager::isLoaded: invalid form!");
1188 bool bLoaded = false;
1189 if ( xFormAsLoadable.is() )
1190 bLoaded = xFormAsLoadable->isLoaded();
1191 return bLoaded;
1195 void SAL_CALL BibDataManager::addLoadListener( const Reference< XLoadListener >& aListener ) throw (RuntimeException, std::exception)
1197 m_aLoadListeners.addInterface( aListener );
1201 void SAL_CALL BibDataManager::removeLoadListener( const Reference< XLoadListener >& aListener ) throw (RuntimeException, std::exception)
1203 m_aLoadListeners.removeInterface( aListener );
1207 Reference< awt::XControlModel > BibDataManager::createGridModel(const OUString& rName)
1209 Reference< awt::XControlModel > xModel;
1213 // create the control model
1214 Reference< XMultiServiceFactory > xMgr = ::comphelper::getProcessServiceFactory();
1215 Reference< XInterface > xObject = xMgr->createInstance("com.sun.star.form.component.GridControl");
1216 xModel=Reference< awt::XControlModel > ( xObject, UNO_QUERY );
1218 // set the
1219 Reference< XPropertySet > xPropSet( xModel, UNO_QUERY );
1220 xPropSet->setPropertyValue( "Name", makeAny( rName ) );
1222 // set the name of the to-be-created control
1223 OUString aControlName("com.sun.star.form.control.InteractionGridControl");
1224 Any aAny; aAny <<= aControlName;
1225 xPropSet->setPropertyValue( "DefaultControl",aAny );
1227 // the helpURL
1228 OUString uProp("HelpURL");
1229 Reference< XPropertySetInfo > xPropInfo = xPropSet->getPropertySetInfo();
1230 if (xPropInfo->hasPropertyByName(uProp))
1232 xPropSet->setPropertyValue(
1233 uProp, makeAny<OUString>(INET_HID_SCHEME HID_BIB_DB_GRIDCTRL));
1236 catch (const Exception&)
1238 OSL_FAIL("::createGridModel: something went wrong !");
1241 return xModel;
1244 OUString BibDataManager::getControlName(sal_Int32 nFormatKey )
1246 OUString aResStr;
1247 switch (nFormatKey)
1249 case DataType::BIT:
1250 case DataType::BOOLEAN:
1251 aResStr="CheckBox";
1252 break;
1253 case DataType::TINYINT:
1254 case DataType::SMALLINT:
1255 case DataType::INTEGER:
1256 aResStr="NumericField";
1257 break;
1258 case DataType::REAL:
1259 case DataType::DOUBLE:
1260 case DataType::NUMERIC:
1261 case DataType::DECIMAL:
1262 aResStr="FormattedField";
1263 break;
1264 case DataType::TIMESTAMP:
1265 aResStr="FormattedField";
1266 break;
1267 case DataType::DATE:
1268 aResStr="DateField";
1269 break;
1270 case DataType::TIME:
1271 aResStr="TimeField";
1272 break;
1273 case DataType::CHAR:
1274 case DataType::VARCHAR:
1275 case DataType::LONGVARCHAR:
1276 default:
1277 aResStr="TextField";
1278 break;
1280 return aResStr;
1283 Reference< awt::XControlModel > BibDataManager::loadControlModel(
1284 const OUString& rName, bool bForceListBox)
1286 Reference< awt::XControlModel > xModel;
1287 OUString aName("View_");
1288 aName += rName;
1292 Reference< XNameAccess > xFields = getColumns( m_xForm );
1293 if (!xFields.is())
1294 return xModel;
1295 Reference< XPropertySet > xField;
1297 Any aElement;
1299 if(xFields->hasByName(rName))
1301 aElement = xFields->getByName(rName);
1302 aElement >>= xField;
1303 Reference< XPropertySetInfo > xInfo = xField.is() ? xField->getPropertySetInfo() : Reference< XPropertySetInfo > ();
1305 const OUString sType("Type");
1306 sal_Int32 nFormatKey = 0;
1307 xField->getPropertyValue(sType) >>= nFormatKey;
1309 OUString aInstanceName("com.sun.star.form.component.");
1311 if (bForceListBox)
1312 aInstanceName += "ListBox";
1313 else
1314 aInstanceName += getControlName(nFormatKey);
1316 Reference< XComponentContext > xContext = comphelper::getProcessComponentContext();
1317 Reference< XInterface > xObject = xContext->getServiceManager()->createInstanceWithContext(aInstanceName, xContext);
1318 xModel=Reference< awt::XControlModel > ( xObject, UNO_QUERY );
1319 Reference< XPropertySet > xPropSet( xModel, UNO_QUERY );
1320 Any aFieldName; aFieldName <<= aName;
1322 xPropSet->setPropertyValue( FM_PROP_NAME,aFieldName);
1323 xPropSet->setPropertyValue( FM_PROP_CONTROLSOURCE, makeAny( rName ) );
1324 xPropSet->setPropertyValue("NativeWidgetLook", makeAny( true ) );
1326 if (bForceListBox)
1328 uno::Any aAny;
1330 //uno::Reference< beans::XPropertySet > xPropSet(xControl, UNO_QUERY);
1331 aAny <<= (sal_Int16)1;
1332 xPropSet->setPropertyValue("BoundColumn", aAny);
1333 ListSourceType eSet = ListSourceType_VALUELIST;
1334 aAny.setValue( &eSet, ::cppu::UnoType<ListSourceType>::get() );
1335 xPropSet->setPropertyValue("ListSourceType", aAny);
1337 uno::Sequence<OUString> aListSource(TYPE_COUNT);
1338 OUString* pListSourceArr = aListSource.getArray();
1339 //pListSourceArr[0] = "select TypeName, TypeIndex from TypeNms";
1340 for(sal_Int32 i = 0; i < TYPE_COUNT; ++i)
1341 pListSourceArr[i] = OUString::number(i);
1342 aAny.setValue(&aListSource, cppu::UnoType<uno::Sequence<OUString>>::get());
1344 xPropSet->setPropertyValue("ListSource", aAny);
1346 uno::Sequence<OUString> aValues(TYPE_COUNT + 1);
1347 OUString* pValuesArr = aValues.getArray();
1348 pValuesArr[0] = BIB_RESSTR(ST_TYPE_ARTICLE);
1349 pValuesArr[1] = BIB_RESSTR(ST_TYPE_BOOK);
1350 pValuesArr[2] = BIB_RESSTR(ST_TYPE_BOOKLET);
1351 pValuesArr[3] = BIB_RESSTR(ST_TYPE_CONFERENCE);
1352 pValuesArr[4] = BIB_RESSTR(ST_TYPE_INBOOK );
1353 pValuesArr[5] = BIB_RESSTR(ST_TYPE_INCOLLECTION);
1354 pValuesArr[6] = BIB_RESSTR(ST_TYPE_INPROCEEDINGS);
1355 pValuesArr[7] = BIB_RESSTR(ST_TYPE_JOURNAL );
1356 pValuesArr[8] = BIB_RESSTR(ST_TYPE_MANUAL );
1357 pValuesArr[9] = BIB_RESSTR(ST_TYPE_MASTERSTHESIS);
1358 pValuesArr[10] = BIB_RESSTR(ST_TYPE_MISC );
1359 pValuesArr[11] = BIB_RESSTR(ST_TYPE_PHDTHESIS );
1360 pValuesArr[12] = BIB_RESSTR(ST_TYPE_PROCEEDINGS );
1361 pValuesArr[13] = BIB_RESSTR(ST_TYPE_TECHREPORT );
1362 pValuesArr[14] = BIB_RESSTR(ST_TYPE_UNPUBLISHED );
1363 pValuesArr[15] = BIB_RESSTR(ST_TYPE_EMAIL );
1364 pValuesArr[16] = BIB_RESSTR(ST_TYPE_WWW );
1365 pValuesArr[17] = BIB_RESSTR(ST_TYPE_CUSTOM1 );
1366 pValuesArr[18] = BIB_RESSTR(ST_TYPE_CUSTOM2 );
1367 pValuesArr[19] = BIB_RESSTR(ST_TYPE_CUSTOM3 );
1368 pValuesArr[20] = BIB_RESSTR(ST_TYPE_CUSTOM4 );
1369 pValuesArr[21] = BIB_RESSTR(ST_TYPE_CUSTOM5 );
1370 // empty string if an invalid value no values is set
1371 pValuesArr[TYPE_COUNT].clear();
1373 aAny.setValue(&aValues, cppu::UnoType<uno::Sequence<OUString>>::get());
1375 xPropSet->setPropertyValue("StringItemList", aAny);
1377 sal_Bool bTrue = sal_True;
1378 aAny.setValue( &bTrue, cppu::UnoType<bool>::get() );
1379 xPropSet->setPropertyValue( "Dropdown", aAny );
1382 Reference< XFormComponent > aFormComp(xModel,UNO_QUERY );
1384 Reference< XNameContainer > xNameCont( m_xForm, UNO_QUERY );
1385 xNameCont->insertByName(aName, makeAny( aFormComp ) );
1387 // now if the form where we inserted the new model is already loaded, notify the model of this
1388 // Note that this implementation below is a HACK as it relies on the fact that the model adds itself
1389 // as load listener to its parent, which is an implementation detail of the model.
1391 // the better solution would be the following:
1392 // in the current scenario, we insert a control model into a form. This results in the control model
1393 // adding itself as load listener to the form. Now, the form should realize that it's already loaded
1394 // and notify the model (which it knows as XLoadListener only) immediately. This seems to make sense.
1395 // (as an anologon to the XStatusListener semantics).
1397 // But this would be way too risky for this last-day fix here.
1398 Reference< XLoadable > xLoad( m_xForm, UNO_QUERY );
1399 if ( xLoad.is() && xLoad->isLoaded() )
1401 Reference< XLoadListener > xListener( aFormComp, UNO_QUERY );
1402 if ( xListener.is() )
1404 EventObject aLoadSource;
1405 aLoadSource.Source = xLoad;
1406 xListener->loaded( aLoadSource );
1411 catch (const Exception&)
1413 OSL_FAIL("::loadControlModel: something went wrong !");
1415 return xModel;
1418 void SAL_CALL BibDataManager::disposing()
1420 BibDataManager_Base::WeakComponentImplHelperBase::disposing();
1424 void BibDataManager::disposing( const EventObject& /*Source*/ ) throw( ::com::sun::star::uno::RuntimeException, std::exception )
1426 // not interested in
1430 void BibDataManager::propertyChange(const beans::PropertyChangeEvent& evt) throw( RuntimeException, std::exception )
1434 if(evt.PropertyName == FM_PROP_VALUE)
1436 if( evt.NewValue.getValueType() == cppu::UnoType<io::XInputStream>::get())
1438 Reference< io::XDataInputStream > xStream(
1439 *static_cast<const Reference< io::XInputStream > *>(evt.NewValue.getValue()), UNO_QUERY );
1440 aUID <<= xStream->readUTF();
1442 else
1443 aUID = evt.NewValue;
1445 Reference< XRowLocate > xLocate(xBibCursor, UNO_QUERY);
1446 DBG_ASSERT(xLocate.is(), "BibDataManager::propertyChange : invalid cursor !");
1447 xLocate->moveToBookmark(aUID);
1450 catch (const Exception&)
1452 OSL_FAIL("::propertyChange: something went wrong !");
1457 void BibDataManager::SetMeAsUidListener()
1461 Reference< XNameAccess > xFields = getColumns( m_xForm );
1462 if (!xFields.is())
1463 return;
1465 Sequence< OUString > aFields(xFields->getElementNames());
1466 const OUString* pFields = aFields.getConstArray();
1467 sal_Int32 nCount=aFields.getLength();
1468 OUString StrUID(STR_UID);
1469 OUString theFieldName;
1470 for( sal_Int32 i=0; i<nCount; i++ )
1472 const OUString& rName = pFields[i];
1474 if (rName.equalsIgnoreAsciiCase(StrUID))
1476 theFieldName=pFields[i];
1477 break;
1481 if(!theFieldName.isEmpty())
1483 Reference< XPropertySet > xPropSet;
1484 Any aElement;
1486 aElement = xFields->getByName(theFieldName);
1487 xPropSet = *static_cast<Reference< XPropertySet > const *>(aElement.getValue());
1489 xPropSet->addPropertyChangeListener(FM_PROP_VALUE, this);
1493 catch (const Exception&)
1495 OSL_FAIL("Exception in BibDataManager::SetMeAsUidListener");
1500 void BibDataManager::RemoveMeAsUidListener()
1504 Reference< XNameAccess > xFields = getColumns( m_xForm );
1505 if (!xFields.is())
1506 return;
1509 Sequence< OUString > aFields(xFields->getElementNames());
1510 const OUString* pFields = aFields.getConstArray();
1511 sal_Int32 nCount=aFields.getLength();
1512 OUString StrUID(STR_UID);
1513 OUString theFieldName;
1514 for( sal_Int32 i=0; i<nCount; i++ )
1516 const OUString& rName = pFields[i];
1518 if (rName.equalsIgnoreAsciiCase(StrUID))
1520 theFieldName=pFields[i];
1521 break;
1525 if(!theFieldName.isEmpty())
1527 Reference< XPropertySet > xPropSet;
1528 Any aElement;
1530 aElement = xFields->getByName(theFieldName);
1531 xPropSet = *static_cast<Reference< XPropertySet > const *>(aElement.getValue());
1533 xPropSet->removePropertyChangeListener(FM_PROP_VALUE, this);
1537 catch (const Exception&)
1539 OSL_FAIL("Exception in BibDataManager::RemoveMeAsUidListener");
1543 void BibDataManager::CreateMappingDialog(vcl::Window* pParent)
1545 VclPtrInstance< MappingDialog_Impl > pDlg(pParent, this);
1546 if(RET_OK == pDlg->Execute() && pBibView)
1548 reload();
1552 OUString BibDataManager::CreateDBChangeDialog(vcl::Window* pParent)
1554 OUString uRet;
1555 VclPtrInstance< DBChangeDialog_Impl > pDlg(pParent, this );
1556 if(RET_OK == pDlg->Execute())
1558 OUString sNewURL = pDlg->GetCurrentURL();
1559 if(sNewURL != getActiveDataSource())
1561 uRet = sNewURL;
1564 return uRet;
1567 void BibDataManager::DispatchDBChangeDialog()
1569 if(pToolbar)
1570 pToolbar->SendDispatch(TBC_BT_CHANGESOURCE, Sequence< PropertyValue >());
1573 const OUString& BibDataManager::GetIdentifierMapping()
1575 if(sIdentifierMapping.isEmpty())
1577 BibConfig* pConfig = BibModul::GetConfig();
1578 BibDBDescriptor aDesc;
1579 aDesc.sDataSource = getActiveDataSource();
1580 aDesc.sTableOrQuery = getActiveDataTable();
1581 aDesc.nCommandType = CommandType::TABLE;
1582 const Mapping* pMapping = pConfig->GetMapping(aDesc);
1583 sIdentifierMapping = pConfig->GetDefColumnName(IDENTIFIER_POS);
1584 if(pMapping)
1586 for(sal_uInt16 nEntry = 0; nEntry < COLUMN_COUNT; nEntry++)
1588 if(pMapping->aColumnPairs[nEntry].sLogicalColumnName == sIdentifierMapping)
1590 sIdentifierMapping = pMapping->aColumnPairs[nEntry].sRealColumnName;
1591 break;
1596 return sIdentifierMapping;
1599 void BibDataManager::SetToolbar(BibToolBar* pSet)
1601 pToolbar = pSet;
1602 if(pToolbar)
1603 pToolbar->SetDatMan(*this);
1606 uno::Reference< form::runtime::XFormController > BibDataManager::GetFormController()
1608 if(!m_xFormCtrl.is())
1610 Reference< uno::XComponentContext > xContext = comphelper::getProcessComponentContext();
1611 m_xFormCtrl = form::runtime::FormController::create(xContext);
1612 m_xFormCtrl->setModel(uno::Reference< awt::XTabControllerModel > (getForm(), UNO_QUERY));
1613 m_xFormDispatch = uno::Reference< frame::XDispatch > ( m_xFormCtrl, UNO_QUERY);
1615 return m_xFormCtrl;
1618 void BibDataManager::RegisterInterceptor( ::bib::BibBeamer* pBibBeamer)
1620 DBG_ASSERT( !m_pInterceptorHelper, "BibDataManager::RegisterInterceptor: called twice!" );
1622 if( pBibBeamer )
1623 m_pInterceptorHelper = new BibInterceptorHelper( pBibBeamer, m_xFormDispatch);
1624 if( m_pInterceptorHelper )
1625 m_pInterceptorHelper->acquire();
1629 bool BibDataManager::HasActiveConnection()const
1631 bool bRet = false;
1632 Reference< XPropertySet > xPrSet( m_xForm, UNO_QUERY );
1633 if( xPrSet.is() )
1635 Reference< XComponent > xConnection;
1636 xPrSet->getPropertyValue("ActiveConnection") >>= xConnection;
1637 bRet = xConnection.is();
1639 return bRet;
1642 bool BibDataManager::HasActiveConnection()
1644 return getConnection( m_xForm ).is();
1647 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */