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