LibreOffice Module sw (master)  1
dbtree.cxx
Go to the documentation of this file.
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 <com/sun/star/container/XNameAccess.hpp>
21 #include <com/sun/star/sdbc/XConnection.hpp>
22 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
23 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
24 #include <com/sun/star/sdb/DatabaseContext.hpp>
25 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
26 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <comphelper/string.hxx>
29 #include <com/sun/star/container/XContainerListener.hpp>
30 #include <cppuhelper/implbase.hxx>
32 
33 #include <dbmgr.hxx>
34 #include <wrtsh.hxx>
35 #include <dbtree.hxx>
36 #include <vcl/settings.hxx>
37 #include <vcl/svapp.hxx>
38 
39 #include <bitmaps.hlst>
40 
41 
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::container;
45 using namespace ::com::sun::star::lang;
46 using namespace ::com::sun::star::sdb;
47 using namespace ::com::sun::star::sdbc;
48 using namespace ::com::sun::star::sdbcx;
49 using namespace ::com::sun::star::beans;
50 
51 class SwDBTreeList_Impl : public cppu::WeakImplHelper < XContainerListener >
52 {
53  Reference< XDatabaseContext > m_xDatabaseContext;
55 
56  public:
57  explicit SwDBTreeList_Impl()
58  : m_pWrtShell(nullptr)
59  {
60  }
61  virtual ~SwDBTreeList_Impl() override;
62 
63  virtual void SAL_CALL elementInserted( const ContainerEvent& Event ) override;
64  virtual void SAL_CALL elementRemoved( const ContainerEvent& Event ) override;
65  virtual void SAL_CALL elementReplaced( const ContainerEvent& Event ) override;
66  virtual void SAL_CALL disposing( const EventObject& Source ) override;
67 
68  bool HasContext();
70  void SetWrtShell(SwWrtShell& rSh) { m_pWrtShell = &rSh;}
71  const Reference<XDatabaseContext>& GetContext() const {return m_xDatabaseContext;}
72  Reference<XConnection> GetConnection(const OUString& rSourceName);
73 };
74 
76 {
77  if(m_xDatabaseContext.is())
78  {
79  osl_atomic_increment(&m_refCount);
80  //block necessary due to solaris' compiler behaviour to
81  //remove temporaries at the block's end
82  {
83  m_xDatabaseContext->removeContainerListener( this );
84  }
85  osl_atomic_decrement(&m_refCount);
86  }
87 }
88 
89 void SwDBTreeList_Impl::elementInserted( const ContainerEvent& )
90 {
91  // information not needed
92 }
93 
94 void SwDBTreeList_Impl::elementRemoved( const ContainerEvent& )
95 {
96 }
97 
98 void SwDBTreeList_Impl::disposing( const EventObject& )
99 {
100  m_xDatabaseContext = nullptr;
101 }
102 
103 void SwDBTreeList_Impl::elementReplaced( const ContainerEvent& rEvent )
104 {
105  elementRemoved(rEvent);
106 }
107 
109 {
110  if(!m_xDatabaseContext.is())
111  {
113  m_xDatabaseContext = DatabaseContext::create(xContext);
114  m_xDatabaseContext->addContainerListener( this );
115  }
116  return m_xDatabaseContext.is();
117 }
118 
120 {
122  if (m_xDatabaseContext.is() && m_pWrtShell)
123  {
124  xRet = m_pWrtShell->GetDBManager()->RegisterConnection(rSourceName);
125  }
126  return xRet;
127 }
128 
129 SwDBTreeList::SwDBTreeList(std::unique_ptr<weld::TreeView> xTreeView)
130  : bInitialized(false)
131  , bShowColumns(false)
132  , pImpl(new SwDBTreeList_Impl)
133  , m_xTreeView(std::move(xTreeView))
134  , m_xScratchIter(m_xTreeView->make_iterator())
135 {
136  m_xTreeView->connect_expanding(LINK(this, SwDBTreeList, RequestingChildrenHdl));
137 }
138 
140 {
141 }
142 
144 {
145  if (!pImpl->HasContext() && pImpl->GetWrtShell())
146  return;
147 
148  Sequence< OUString > aDBNames = pImpl->GetContext()->getElementNames();
149  auto const sort = comphelper::string::NaturalStringSorter(
151  Application::GetSettings().GetUILanguageTag().getLocale());
152  std::sort(
153  aDBNames.begin(), aDBNames.end(),
154  [&sort](OUString const & x, OUString const & y)
155  { return sort.compare(x, y) < 0; });
156 
157  OUString aImg(RID_BMP_DB);
158  for (const OUString& rDBName : std::as_const(aDBNames))
159  {
160  Reference<XConnection> xConnection = pImpl->GetConnection(rDBName);
161  if (xConnection.is())
162  {
163  m_xTreeView->insert(nullptr, -1, &rDBName, nullptr, nullptr, nullptr, true, m_xScratchIter.get());
164  m_xTreeView->set_image(*m_xScratchIter, aImg);
165  }
166  }
167  Select(OUString(), OUString(), OUString());
168 
169  bInitialized = true;
170 }
171 
172 void SwDBTreeList::AddDataSource(const OUString& rSource)
173 {
174  m_xTreeView->insert(nullptr, -1, &rSource, nullptr, nullptr, nullptr, true, m_xScratchIter.get());
175  m_xTreeView->set_image(*m_xScratchIter, RID_BMP_DB);
176  m_xTreeView->select(*m_xScratchIter);
177 }
178 
179 IMPL_LINK(SwDBTreeList, RequestingChildrenHdl, const weld::TreeIter&, rParent, bool)
180 {
181  if (!m_xTreeView->iter_has_child(rParent))
182  {
183  if (m_xTreeView->get_iter_depth(rParent)) // column names
184  {
185  try
186  {
187  std::unique_ptr<weld::TreeIter> xGrandParent(m_xTreeView->make_iterator(&rParent));
188  m_xTreeView->iter_parent(*xGrandParent);
189  OUString sSourceName = m_xTreeView->get_text(*xGrandParent);
190  OUString sTableName = m_xTreeView->get_text(rParent);
191 
192  if(!pImpl->GetContext()->hasByName(sSourceName))
193  return true;
194  Reference<XConnection> xConnection = pImpl->GetConnection(sSourceName);
195  bool bTable = m_xTreeView->get_id(rParent).isEmpty();
196  Reference<XColumnsSupplier> xColsSupplier;
197  if(bTable)
198  {
199  Reference<XTablesSupplier> xTSupplier(xConnection, UNO_QUERY);
200  if(xTSupplier.is())
201  {
202  Reference<XNameAccess> xTables = xTSupplier->getTables();
203  OSL_ENSURE(xTables->hasByName(sTableName), "table not available anymore?");
204  try
205  {
206  Any aTable = xTables->getByName(sTableName);
207  Reference<XPropertySet> xPropSet;
208  aTable >>= xPropSet;
209  xColsSupplier.set(xPropSet, UNO_QUERY);
210  }
211  catch (const Exception&)
212  {
213  }
214  }
215  }
216  else
217  {
218  Reference<XQueriesSupplier> xQSupplier(xConnection, UNO_QUERY);
219  if(xQSupplier.is())
220  {
221  Reference<XNameAccess> xQueries = xQSupplier->getQueries();
222  OSL_ENSURE(xQueries->hasByName(sTableName), "table not available anymore?");
223  try
224  {
225  Any aQuery = xQueries->getByName(sTableName);
226  Reference<XPropertySet> xPropSet;
227  aQuery >>= xPropSet;
228  xColsSupplier.set(xPropSet, UNO_QUERY);
229  }
230  catch (const Exception&)
231  {
232  }
233  }
234  }
235 
236  if(xColsSupplier.is())
237  {
238  Reference <XNameAccess> xCols = xColsSupplier->getColumns();
239  const Sequence< OUString> aColNames = xCols->getElementNames();
240  for (const OUString& rColName : aColNames)
241  {
242  m_xTreeView->append(&rParent, rColName);
243  }
244  }
245  }
246  catch (const Exception&)
247  {
248  }
249  }
250  else // table names
251  {
252  try
253  {
254  OUString sSourceName = m_xTreeView->get_text(rParent);
255  if (!pImpl->GetContext()->hasByName(sSourceName))
256  return true;
257  Reference<XConnection> xConnection = pImpl->GetConnection(sSourceName);
258  if (xConnection.is())
259  {
260  Reference<XTablesSupplier> xTSupplier(xConnection, UNO_QUERY);
261  if(xTSupplier.is())
262  {
263  Reference<XNameAccess> xTables = xTSupplier->getTables();
264  const Sequence< OUString> aTableNames = xTables->getElementNames();
265  OUString aImg(RID_BMP_DBTABLE);
266  for (const OUString& rTableName : aTableNames)
267  {
268  m_xTreeView->insert(&rParent, -1, &rTableName, nullptr,
269  nullptr, nullptr, bShowColumns, m_xScratchIter.get());
270  m_xTreeView->set_image(*m_xScratchIter, aImg);
271  }
272  }
273 
274  Reference<XQueriesSupplier> xQSupplier(xConnection, UNO_QUERY);
275  if(xQSupplier.is())
276  {
277  Reference<XNameAccess> xQueries = xQSupplier->getQueries();
278  const Sequence< OUString> aQueryNames = xQueries->getElementNames();
279  OUString aImg(RID_BMP_DBQUERY);
280  for (const OUString& rQueryName : aQueryNames)
281  {
282  //to discriminate between queries and tables the user data of query entries is set
283  OUString sId(OUString::number(1));
284  m_xTreeView->insert(&rParent, -1, &rQueryName, &sId,
285  nullptr, nullptr, bShowColumns, m_xScratchIter.get());
286  m_xTreeView->set_image(*m_xScratchIter, aImg);
287  }
288  }
289  }
290  }
291  catch (const Exception&)
292  {
293  }
294  }
295  }
296  return true;
297 }
298 
299 OUString SwDBTreeList::GetDBName(OUString& rTableName, OUString& rColumnName, sal_Bool* pbIsTable)
300 {
301  OUString sDBName;
302  std::unique_ptr<weld::TreeIter> xIter(m_xTreeView->make_iterator());
303  if (m_xTreeView->get_selected(xIter.get()))
304  {
305  if (m_xTreeView->get_iter_depth(*xIter) == 2)
306  {
307  rColumnName = m_xTreeView->get_text(*xIter);
308  m_xTreeView->iter_parent(*xIter); // column name was selected
309  }
310  if (m_xTreeView->get_iter_depth(*xIter) == 1)
311  {
312  if (pbIsTable)
313  *pbIsTable = m_xTreeView->get_id(*xIter).isEmpty();
314  rTableName = m_xTreeView->get_text(*xIter);
315  m_xTreeView->iter_parent(*xIter);
316  }
317  sDBName = m_xTreeView->get_text(*xIter);
318  }
319  return sDBName;
320 }
321 
322 // Format: database.table
323 void SwDBTreeList::Select(const OUString& rDBName, const OUString& rTableName, const OUString& rColumnName)
324 {
325  std::unique_ptr<weld::TreeIter> xParent(m_xTreeView->make_iterator());
326  if (!m_xTreeView->get_iter_first(*xParent))
327  return;
328 
329  do
330  {
331  if (rDBName == m_xTreeView->get_text(*xParent))
332  {
333  if (!m_xTreeView->iter_has_child(*xParent))
334  {
335  RequestingChildrenHdl(*xParent);
336  m_xTreeView->expand_row(*xParent);
337  }
338  std::unique_ptr<weld::TreeIter> xChild(m_xTreeView->make_iterator(xParent.get()));
339  if (!m_xTreeView->iter_children(*xChild))
340  continue;
341  do
342  {
343  if (rTableName == m_xTreeView->get_text(*xChild))
344  {
345  m_xTreeView->copy_iterator(*xChild, *xParent);
346 
347  bool bNoChild = false;
348  if (bShowColumns && !rColumnName.isEmpty())
349  {
350  if (!m_xTreeView->iter_has_child(*xParent))
351  {
352  RequestingChildrenHdl(*xParent);
353  m_xTreeView->expand_row(*xParent);
354  }
355 
356  bNoChild = true;
357  if (m_xTreeView->iter_children(*xChild))
358  {
359  do
360  {
361  if (rColumnName == m_xTreeView->get_text(*xChild))
362  {
363  bNoChild = false;
364  break;
365  }
366  }
367  while (m_xTreeView->iter_next_sibling(*xChild));
368  }
369  }
370 
371  if (bNoChild)
372  m_xTreeView->copy_iterator(*xParent, *xChild);
373 
374  m_xTreeView->scroll_to_row(*xChild);
375  m_xTreeView->select(*xChild);
376  return;
377  }
378  }
379  while (m_xTreeView->iter_next_sibling(*xChild));
380  }
381  } while (m_xTreeView->iter_next_sibling(*xParent));
382 }
383 
385 {
386  pImpl->SetWrtShell(rSh);
387  if (m_xTreeView->get_visible() && !bInitialized)
388  InitTreeList();
389 }
390 
391 namespace
392 {
393  void GotoRootLevelParent(const weld::TreeView& rTreeView, weld::TreeIter& rEntry)
394  {
395  while (rTreeView.get_iter_depth(rEntry))
396  rTreeView.iter_parent(rEntry);
397  }
398 }
399 
400 void SwDBTreeList::ShowColumns(bool bShowCol)
401 {
402  if (bShowCol == bShowColumns)
403  return;
404 
405  bShowColumns = bShowCol;
406  OUString sTableName;
407  OUString sColumnName;
408  const OUString sDBName(GetDBName(sTableName, sColumnName));
409 
410  m_xTreeView->freeze();
411 
412  std::unique_ptr<weld::TreeIter> xIter(m_xTreeView->make_iterator());
413  std::unique_ptr<weld::TreeIter> xChild(m_xTreeView->make_iterator());
414  if (m_xTreeView->get_iter_first(*xIter))
415  {
416  do
417  {
418  GotoRootLevelParent(*m_xTreeView, *xIter);
419  m_xTreeView->collapse_row(*xIter);
420  while (m_xTreeView->iter_has_child(*xIter))
421  {
422  m_xTreeView->copy_iterator(*xIter, *xChild);
423  (void)m_xTreeView->iter_children(*xChild);
424  m_xTreeView->remove(*xChild);
425  }
426  } while (m_xTreeView->iter_next(*xIter));
427  }
428 
429  m_xTreeView->thaw();
430 
431  if (!sDBName.isEmpty())
432  {
433  Select(sDBName, sTableName, sColumnName); // force RequestingChildren
434  }
435 }
436 
437 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual bool iter_parent(TreeIter &rIter) const =0
css::uno::Reference< css::sdbc::XConnection > const & RegisterConnection(OUString const &rSource)
create and store or find an already stored connection to a data source for use in SwFieldMgr and SwDB...
Definition: dbmgr.cxx:2333
const Color aColNames[SC_RANGECOLORS]
ULONG m_refCount
static const AllSettings & GetSettings()
void SetWrtShell(SwWrtShell &rSh)
Definition: dbtree.cxx:70
Reference< XDatabaseContext > m_xDatabaseContext
Definition: dbtree.cxx:53
virtual void SAL_CALL elementReplaced(const ContainerEvent &Event) override
Definition: dbtree.cxx:103
float x
Used by the UI to modify the document model.
Definition: wrtsh.hxx:90
SwDBTreeList(std::unique_ptr< weld::TreeView > xTreeView)
Definition: dbtree.cxx:129
SwWrtShell * m_pWrtShell
Definition: dbtree.cxx:54
Reference< XConnection > GetConnection(const OUString &rSourceName)
Definition: dbtree.cxx:119
std::unique_ptr< weld::TreeView > m_xTreeView
Definition: dbtree.hxx:36
float y
SwDBManager * GetDBManager() const
For evaluation of DB fields (new DB-manager).
Definition: edfld.cxx:331
SwWrtShell * GetWrtShell()
Definition: dbtree.cxx:69
const Reference< XDatabaseContext > & GetContext() const
Definition: dbtree.cxx:71
void SetWrtShell(SwWrtShell &rSh)
Definition: dbtree.cxx:384
unsigned char sal_Bool
bool bShowColumns
Definition: dbtree.hxx:33
void Select(const OUString &rDBName, const OUString &rTableName, const OUString &rColumnName)
Definition: dbtree.cxx:323
const LanguageTag & getLocale()
rtl::Reference< SwDBTreeList_Impl > pImpl
Definition: dbtree.hxx:35
SAL_DLLPRIVATE void InitTreeList()
Definition: dbtree.cxx:143
void AddDataSource(const OUString &rSource)
Definition: dbtree.cxx:172
virtual void SAL_CALL elementInserted(const ContainerEvent &Event) override
Definition: dbtree.cxx:89
void ShowColumns(bool bShowCol)
Definition: dbtree.cxx:400
bool HasContext()
Definition: dbtree.cxx:108
virtual void SAL_CALL elementRemoved(const ContainerEvent &Event) override
Definition: dbtree.cxx:94
Reference< XComponentContext > getProcessComponentContext()
bool bInitialized
Definition: dbtree.hxx:32
OUString sId
std::unique_ptr< weld::TreeIter > m_xScratchIter
Definition: dbtree.hxx:37
virtual void SAL_CALL disposing(const EventObject &Source) override
Definition: dbtree.cxx:98
OUString GetDBName(OUString &rTableName, OUString &rColumnName, sal_Bool *pbIsTable=nullptr)
Definition: dbtree.cxx:299
virtual ~SwDBTreeList_Impl() override
Definition: dbtree.cxx:75
virtual int get_iter_depth(const TreeIter &rIter) const =0
IMPL_LINK(SwDBTreeList, RequestingChildrenHdl, const weld::TreeIter &, rParent, bool)
Definition: dbtree.cxx:179
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo