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