Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <sot/formats.hxx>
21 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
22 : #include <com/sun/star/container/XNameAccess.hpp>
23 : #include <com/sun/star/sdbc/XDataSource.hpp>
24 : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
25 : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
26 : #include <com/sun/star/sdb/DatabaseContext.hpp>
27 : #include <com/sun/star/sdb/XQueriesSupplier.hpp>
28 : #include <com/sun/star/sdb/XDatabaseAccess.hpp>
29 : #include <com/sun/star/sdb/CommandType.hpp>
30 : #include <com/sun/star/beans/XPropertySet.hpp>
31 : #include <comphelper/processfactory.hxx>
32 : #include <com/sun/star/sdb/XCompletedConnection.hpp>
33 : #include <com/sun/star/container/XContainerListener.hpp>
34 : #include <com/sun/star/container/XContainer.hpp>
35 : #include <cppuhelper/implbase.hxx>
36 : #include <svx/dbaexchange.hxx>
37 :
38 : #include <dbmgr.hxx>
39 : #include <swmodule.hxx>
40 : #include <view.hxx>
41 : #include <wrtsh.hxx>
42 : #include <dbtree.hxx>
43 : #include <osl/mutex.hxx>
44 : #include <vcl/builderfactory.hxx>
45 : #include <vcl/svapp.hxx>
46 : #include <svtools/treelistentry.hxx>
47 :
48 : #include <helpid.h>
49 : #include <utlui.hrc>
50 :
51 : #include <unomid.h>
52 :
53 : #include <boost/ptr_container/ptr_vector.hpp>
54 :
55 : using namespace ::com::sun::star;
56 : using namespace ::com::sun::star::uno;
57 : using namespace ::com::sun::star::container;
58 : using namespace ::com::sun::star::lang;
59 : using namespace ::com::sun::star::sdb;
60 : using namespace ::com::sun::star::sdbc;
61 : using namespace ::com::sun::star::sdbcx;
62 : using namespace ::com::sun::star::task;
63 : using namespace ::com::sun::star::beans;
64 :
65 0 : struct SwConnectionData
66 : {
67 : OUString sSourceName;
68 : Reference<XConnection> xConnection;
69 : };
70 :
71 : typedef boost::ptr_vector<SwConnectionData> SwConnectionArr;
72 :
73 : class SwDBTreeList_Impl : public cppu::WeakImplHelper < XContainerListener >
74 : {
75 : Reference< XDatabaseContext > xDBContext;
76 : SwConnectionArr aConnections;
77 : SwWrtShell* pWrtSh;
78 :
79 : public:
80 0 : explicit SwDBTreeList_Impl(SwWrtShell* pShell)
81 0 : : pWrtSh(pShell)
82 : {
83 0 : }
84 : virtual ~SwDBTreeList_Impl();
85 :
86 : virtual void SAL_CALL elementInserted( const ContainerEvent& Event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
87 : virtual void SAL_CALL elementRemoved( const ContainerEvent& Event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
88 : virtual void SAL_CALL elementReplaced( const ContainerEvent& Event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
89 : virtual void SAL_CALL disposing( const EventObject& Source ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
90 :
91 : bool HasContext();
92 0 : SwWrtShell* GetWrtShell() { return pWrtSh;}
93 0 : void SetWrtShell(SwWrtShell& rSh) { pWrtSh = &rSh;}
94 0 : Reference<XDatabaseContext> GetContext() const {return xDBContext;}
95 : Reference<XConnection> GetConnection(const OUString& rSourceName);
96 : };
97 :
98 0 : SwDBTreeList_Impl::~SwDBTreeList_Impl()
99 : {
100 0 : if(xDBContext.is())
101 : {
102 0 : m_refCount++;
103 : //block necessary due to solaris' compiler behaviour to
104 : //remove temporaries at the block's end
105 : {
106 0 : xDBContext->removeContainerListener( this );
107 : }
108 0 : m_refCount--;
109 : }
110 0 : }
111 :
112 0 : void SwDBTreeList_Impl::elementInserted( const ContainerEvent& ) throw (RuntimeException, std::exception)
113 : {
114 : // information not needed
115 0 : }
116 :
117 0 : void SwDBTreeList_Impl::elementRemoved( const ContainerEvent& rEvent ) throw (RuntimeException, std::exception)
118 : {
119 0 : SolarMutexGuard aGuard;
120 0 : OUString sSource;
121 0 : rEvent.Accessor >>= sSource;
122 0 : for(SwConnectionArr::iterator i = aConnections.begin(); i != aConnections.end(); ++i)
123 : {
124 0 : if(i->sSourceName == sSource)
125 : {
126 0 : aConnections.erase(i);
127 0 : break;
128 : }
129 0 : }
130 0 : }
131 :
132 0 : void SwDBTreeList_Impl::disposing( const EventObject& ) throw (RuntimeException, std::exception)
133 : {
134 0 : xDBContext = 0;
135 0 : }
136 :
137 0 : void SwDBTreeList_Impl::elementReplaced( const ContainerEvent& rEvent ) throw (RuntimeException, std::exception)
138 : {
139 0 : elementRemoved(rEvent);
140 0 : }
141 :
142 0 : bool SwDBTreeList_Impl::HasContext()
143 : {
144 0 : if(!xDBContext.is())
145 : {
146 0 : Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
147 0 : xDBContext = DatabaseContext::create(xContext);
148 0 : xDBContext->addContainerListener( this );
149 : }
150 0 : return xDBContext.is();
151 : }
152 :
153 0 : Reference<XConnection> SwDBTreeList_Impl::GetConnection(const OUString& rSourceName)
154 : {
155 0 : Reference<XConnection> xRet;
156 0 : for(SwConnectionArr::const_iterator i = aConnections.begin(); i != aConnections.end(); ++i)
157 : {
158 0 : if(i->sSourceName == rSourceName)
159 : {
160 0 : xRet = i->xConnection;
161 0 : break;
162 : }
163 : }
164 0 : if(!xRet.is() && xDBContext.is() && pWrtSh)
165 : {
166 0 : SwConnectionData* pPtr = new SwConnectionData();
167 0 : pPtr->sSourceName = rSourceName;
168 0 : xRet = pWrtSh->GetDBManager()->RegisterConnection(pPtr->sSourceName);
169 0 : aConnections.push_back(pPtr);
170 : }
171 0 : return xRet;
172 : }
173 :
174 0 : SwDBTreeList::SwDBTreeList(vcl::Window *pParent, WinBits nStyle)
175 : : SvTreeListBox(pParent, nStyle)
176 : , aImageList(SW_RES(ILIST_DB_DLG))
177 : , bInitialized(false)
178 : , bShowColumns(false)
179 0 : , pImpl(new SwDBTreeList_Impl(NULL))
180 : {
181 0 : if (IsVisible())
182 0 : InitTreeList();
183 0 : }
184 :
185 0 : VCL_BUILDER_DECL_FACTORY(SwDBTreeList)
186 : {
187 0 : WinBits nStyle = WB_TABSTOP;
188 0 : OString sBorder = VclBuilder::extractCustomProperty(rMap);
189 0 : if (!sBorder.isEmpty())
190 0 : nStyle |= WB_BORDER;
191 0 : rRet = VclPtr<SwDBTreeList>::Create(pParent, nStyle);
192 0 : }
193 :
194 0 : Size SwDBTreeList::GetOptimalSize() const
195 : {
196 0 : return LogicToPixel(Size(100, 62), MapMode(MAP_APPFONT));
197 : }
198 :
199 0 : SwDBTreeList::~SwDBTreeList()
200 : {
201 0 : disposeOnce();
202 0 : }
203 :
204 0 : void SwDBTreeList::dispose()
205 : {
206 0 : delete pImpl;
207 0 : pImpl = NULL;
208 0 : SvTreeListBox::dispose();
209 0 : }
210 :
211 0 : void SwDBTreeList::InitTreeList()
212 : {
213 0 : if(!pImpl->HasContext() && pImpl->GetWrtShell())
214 0 : return;
215 0 : SetSelectionMode(SINGLE_SELECTION);
216 0 : SetStyle(GetStyle()|WB_HASLINES|WB_CLIPCHILDREN|WB_HASBUTTONS|WB_HASBUTTONSATROOT|WB_HSCROLL);
217 : // don't set font, so that the Control's font is being applied!
218 0 : SetSpaceBetweenEntries(0);
219 : SetNodeBitmaps( aImageList.GetImage(IMG_COLLAPSE),
220 0 : aImageList.GetImage(IMG_EXPAND ) );
221 :
222 0 : SetDragDropMode(DragDropMode::APP_COPY);
223 :
224 0 : GetModel()->SetCompareHdl(LINK(this, SwDBTreeList, DBCompare));
225 :
226 0 : Sequence< OUString > aDBNames = pImpl->GetContext()->getElementNames();
227 0 : const OUString* pDBNames = aDBNames.getConstArray();
228 0 : long nCount = aDBNames.getLength();
229 :
230 0 : Image aImg = aImageList.GetImage(IMG_DB);
231 0 : for(long i = 0; i < nCount; i++)
232 : {
233 0 : OUString sDBName(pDBNames[i]);
234 0 : InsertEntry(sDBName, aImg, aImg, NULL, true);
235 0 : }
236 0 : OUString sDBName(sDefDBName.getToken(0, DB_DELIM));
237 0 : OUString sTableName(sDefDBName.getToken(1, DB_DELIM));
238 0 : OUString sColumnName(sDefDBName.getToken(2, DB_DELIM));
239 0 : Select(sDBName, sTableName, sColumnName);
240 :
241 0 : bInitialized = true;
242 : }
243 :
244 0 : void SwDBTreeList::AddDataSource(const OUString& rSource)
245 : {
246 0 : Image aImg = aImageList.GetImage(IMG_DB);
247 0 : SvTreeListEntry* pEntry = InsertEntry(rSource, aImg, aImg, NULL, true);
248 0 : SvTreeListBox::Select(pEntry);
249 0 : }
250 :
251 0 : void SwDBTreeList::ShowColumns(bool bShowCol)
252 : {
253 0 : if (bShowCol != bShowColumns)
254 : {
255 0 : bShowColumns = bShowCol;
256 0 : OUString sTableName;
257 0 : OUString sColumnName;
258 0 : const OUString sDBName(GetDBName(sTableName, sColumnName));
259 :
260 0 : SetUpdateMode(false);
261 :
262 0 : SvTreeListEntry* pEntry = First();
263 :
264 0 : while (pEntry)
265 : {
266 0 : pEntry = GetRootLevelParent( pEntry );
267 0 : Collapse(pEntry); // zuklappen
268 :
269 : SvTreeListEntry* pChild;
270 0 : while ((pChild = FirstChild(pEntry)) != 0L)
271 0 : GetModel()->Remove(pChild);
272 :
273 0 : pEntry = Next(pEntry);
274 : }
275 :
276 0 : if (!sDBName.isEmpty())
277 : {
278 0 : Select(sDBName, sTableName, sColumnName); // force RequestingChildren
279 : }
280 0 : SetUpdateMode(true);
281 : }
282 0 : }
283 :
284 0 : void SwDBTreeList::RequestingChildren(SvTreeListEntry* pParent)
285 : {
286 0 : if (!pParent->HasChildren())
287 : {
288 0 : if (GetParent(pParent)) // column names
289 : {
290 : try
291 : {
292 :
293 0 : OUString sSourceName = GetEntryText(GetParent(pParent));
294 0 : OUString sTableName = GetEntryText(pParent);
295 :
296 0 : if(!pImpl->GetContext()->hasByName(sSourceName))
297 0 : return;
298 0 : Reference<XConnection> xConnection = pImpl->GetConnection(sSourceName);
299 0 : bool bTable = pParent->GetUserData() == 0;
300 0 : Reference<XColumnsSupplier> xColsSupplier;
301 0 : if(bTable)
302 : {
303 0 : Reference<XTablesSupplier> xTSupplier = Reference<XTablesSupplier>(xConnection, UNO_QUERY);
304 0 : if(xTSupplier.is())
305 : {
306 0 : Reference<XNameAccess> xTables = xTSupplier->getTables();
307 : OSL_ENSURE(xTables->hasByName(sTableName), "table not available anymore?");
308 : try
309 : {
310 0 : Any aTable = xTables->getByName(sTableName);
311 0 : Reference<XPropertySet> xPropSet;
312 0 : aTable >>= xPropSet;
313 0 : xColsSupplier = Reference<XColumnsSupplier>(xPropSet, UNO_QUERY);
314 : }
315 0 : catch (const Exception&)
316 : {
317 0 : }
318 0 : }
319 : }
320 : else
321 : {
322 0 : Reference<XQueriesSupplier> xQSupplier = Reference<XQueriesSupplier>(xConnection, UNO_QUERY);
323 0 : if(xQSupplier.is())
324 : {
325 0 : Reference<XNameAccess> xQueries = xQSupplier->getQueries();
326 : OSL_ENSURE(xQueries->hasByName(sTableName), "table not available anymore?");
327 : try
328 : {
329 0 : Any aQuery = xQueries->getByName(sTableName);
330 0 : Reference<XPropertySet> xPropSet;
331 0 : aQuery >>= xPropSet;
332 0 : xColsSupplier = Reference<XColumnsSupplier>(xPropSet, UNO_QUERY);
333 : }
334 0 : catch (const Exception&)
335 : {
336 0 : }
337 0 : }
338 : }
339 :
340 0 : if(xColsSupplier.is())
341 : {
342 0 : Reference <XNameAccess> xCols = xColsSupplier->getColumns();
343 0 : Sequence< OUString> aColNames = xCols->getElementNames();
344 0 : const OUString* pColNames = aColNames.getConstArray();
345 0 : long nCount = aColNames.getLength();
346 0 : for (long i = 0; i < nCount; i++)
347 : {
348 0 : OUString sName = pColNames[i];
349 0 : InsertEntry(sName, pParent);
350 0 : }
351 0 : }
352 : }
353 0 : catch (const Exception&)
354 : {
355 : }
356 : }
357 : else // table names
358 : {
359 : try
360 : {
361 0 : OUString sSourceName = GetEntryText(pParent);
362 0 : if(!pImpl->GetContext()->hasByName(sSourceName))
363 0 : return;
364 0 : Reference<XConnection> xConnection = pImpl->GetConnection(sSourceName);
365 0 : if (xConnection.is())
366 : {
367 0 : Reference<XTablesSupplier> xTSupplier = Reference<XTablesSupplier>(xConnection, UNO_QUERY);
368 0 : if(xTSupplier.is())
369 : {
370 0 : Reference<XNameAccess> xTables = xTSupplier->getTables();
371 0 : Sequence< OUString> aTableNames = xTables->getElementNames();
372 0 : OUString sTableName;
373 0 : long nCount = aTableNames.getLength();
374 0 : const OUString* pTableNames = aTableNames.getConstArray();
375 0 : Image aImg = aImageList.GetImage(IMG_DBTABLE);
376 0 : for (long i = 0; i < nCount; i++)
377 : {
378 0 : sTableName = pTableNames[i];
379 0 : SvTreeListEntry* pTableEntry = InsertEntry(sTableName, aImg, aImg, pParent, bShowColumns);
380 : //to discriminate between queries and tables the user data of table entries is set
381 0 : pTableEntry->SetUserData(nullptr);
382 0 : }
383 : }
384 :
385 0 : Reference<XQueriesSupplier> xQSupplier = Reference<XQueriesSupplier>(xConnection, UNO_QUERY);
386 0 : if(xQSupplier.is())
387 : {
388 0 : Reference<XNameAccess> xQueries = xQSupplier->getQueries();
389 0 : Sequence< OUString> aQueryNames = xQueries->getElementNames();
390 0 : OUString sQueryName;
391 0 : long nCount = aQueryNames.getLength();
392 0 : const OUString* pQueryNames = aQueryNames.getConstArray();
393 0 : Image aImg = aImageList.GetImage(IMG_DBQUERY);
394 0 : for (long i = 0; i < nCount; i++)
395 : {
396 0 : sQueryName = pQueryNames[i];
397 0 : SvTreeListEntry* pQueryEntry = InsertEntry(sQueryName, aImg, aImg, pParent, bShowColumns);
398 0 : pQueryEntry->SetUserData(reinterpret_cast<void*>(1));
399 0 : }
400 0 : }
401 0 : }
402 : }
403 0 : catch (const Exception&)
404 : {
405 : }
406 : }
407 : }
408 : }
409 :
410 0 : IMPL_LINK( SwDBTreeList, DBCompare, SvSortData*, pData )
411 : {
412 0 : SvTreeListEntry* pRight = const_cast<SvTreeListEntry*>(pData->pRight);
413 :
414 0 : if (GetParent(pRight) && GetParent(GetParent(pRight)))
415 0 : return 1; // don't sort column names
416 :
417 0 : return DefaultCompare(pData); // otherwise call base class
418 : }
419 :
420 0 : OUString SwDBTreeList::GetDBName(OUString& rTableName, OUString& rColumnName, sal_Bool* pbIsTable)
421 : {
422 0 : OUString sDBName;
423 0 : SvTreeListEntry* pEntry = FirstSelected();
424 :
425 0 : if (pEntry && GetParent(pEntry))
426 : {
427 0 : if (GetParent(GetParent(pEntry)))
428 : {
429 0 : rColumnName = GetEntryText(pEntry);
430 0 : pEntry = GetParent(pEntry); // column name was selected
431 : }
432 0 : sDBName = GetEntryText(GetParent(pEntry));
433 0 : if(pbIsTable)
434 : {
435 0 : *pbIsTable = pEntry->GetUserData() == 0;
436 : }
437 0 : rTableName = GetEntryText(pEntry);
438 : }
439 0 : return sDBName;
440 : }
441 :
442 : // Format: database.table
443 0 : void SwDBTreeList::Select(const OUString& rDBName, const OUString& rTableName, const OUString& rColumnName)
444 : {
445 : SvTreeListEntry* pParent;
446 : SvTreeListEntry* pChild;
447 0 : sal_uInt16 nParent = 0;
448 0 : sal_uInt16 nChild = 0;
449 :
450 0 : while ((pParent = GetEntry(nParent++)) != NULL)
451 : {
452 0 : if (rDBName == GetEntryText(pParent))
453 : {
454 0 : if (!pParent->HasChildren())
455 0 : RequestingChildren(pParent);
456 0 : while ((pChild = GetEntry(pParent, nChild++)) != NULL)
457 : {
458 0 : if (rTableName == GetEntryText(pChild))
459 : {
460 0 : pParent = pChild;
461 :
462 0 : if (bShowColumns && !rColumnName.isEmpty())
463 : {
464 0 : nChild = 0;
465 :
466 0 : if (!pParent->HasChildren())
467 0 : RequestingChildren(pParent);
468 :
469 0 : while ((pChild = GetEntry(pParent, nChild++)) != NULL)
470 0 : if (rColumnName == GetEntryText(pChild))
471 0 : break;
472 : }
473 0 : if (!pChild)
474 0 : pChild = pParent;
475 :
476 0 : MakeVisible(pChild);
477 0 : SvTreeListBox::Select(pChild);
478 0 : return;
479 : }
480 : }
481 : }
482 : }
483 : }
484 :
485 0 : void SwDBTreeList::StartDrag( sal_Int8 /*nAction*/, const Point& /*rPosPixel*/ )
486 : {
487 0 : OUString sTableName;
488 0 : OUString sColumnName;
489 0 : OUString sDBName( GetDBName( sTableName, sColumnName ));
490 0 : if (!sDBName.isEmpty())
491 : {
492 0 : TransferDataContainer* pContainer = new TransferDataContainer;
493 0 : ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::XTransferable > xRef( pContainer );
494 0 : if( !sColumnName.isEmpty() )
495 : {
496 : // drag database field
497 : svx::OColumnTransferable aColTransfer(
498 : sDBName,
499 : OUString(),
500 : sdb::CommandType::TABLE,
501 : sTableName,
502 : sColumnName,
503 0 : (ColumnTransferFormatFlags::FIELD_DESCRIPTOR|ColumnTransferFormatFlags::COLUMN_DESCRIPTOR) );
504 0 : aColTransfer.addDataToContainer( pContainer );
505 : }
506 :
507 0 : sDBName += "." + sTableName;
508 0 : if (!sColumnName.isEmpty())
509 : {
510 0 : sDBName += "." + sColumnName;
511 : }
512 :
513 0 : pContainer->CopyString( SotClipboardFormatId::STRING, sDBName );
514 : pContainer->StartDrag( this, DND_ACTION_COPY | DND_ACTION_LINK,
515 0 : Link<>() );
516 0 : }
517 0 : }
518 :
519 0 : sal_Int8 SwDBTreeList::AcceptDrop( const AcceptDropEvent& /*rEvt*/ )
520 : {
521 0 : return DND_ACTION_NONE;
522 : }
523 :
524 0 : void SwDBTreeList::SetWrtShell(SwWrtShell& rSh)
525 : {
526 0 : pImpl->SetWrtShell(rSh);
527 0 : if (IsVisible() && !bInitialized)
528 0 : InitTreeList();
529 177 : }
530 :
531 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|