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