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