LibreOffice Module sw (master)  1
selectdbtabledialog.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 <swtypes.hxx>
21 #include "selectdbtabledialog.hxx"
22 #include "dbtablepreviewdialog.hxx"
23 #include <svtools/simptabl.hxx>
24 #include <osl/diagnose.h>
25 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
26 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
27 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <com/sun/star/beans/PropertyValue.hpp>
29 #include <com/sun/star/container/XChild.hpp>
30 #include <com/sun/star/sdbc/XDataSource.hpp>
31 
32 #include <unomid.h>
33 
34 #include <dbui.hrc>
35 #include <strings.hrc>
36 
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::sdbcx;
39 using namespace ::com::sun::star::sdbc;
40 using namespace ::com::sun::star::sdb;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::container;
43 using namespace ::com::sun::star::beans;
44 
46  const uno::Reference< sdbc::XConnection>& rConnection)
47  : SfxDialogController(pParent, "modules/swriter/ui/selecttabledialog.ui", "SelectTableDialog")
48  , m_xConnection(rConnection)
49  , m_xTable(m_xBuilder->weld_tree_view("table"))
50  , m_xPreviewPB(m_xBuilder->weld_button("preview"))
51 {
52  m_xTable->set_size_request(m_xTable->get_approximate_digit_width() * 60,
53  m_xTable->get_height_rows(6));
54 
55  std::vector<int> aWidths;
56  aWidths.push_back(m_xTable->get_approximate_digit_width() * 30);
57  m_xTable->set_column_fixed_widths(aWidths);
58 
59  m_xPreviewPB->connect_clicked(LINK(this, SwSelectDBTableDialog, PreviewHdl));
60 
61  Reference<XTablesSupplier> xTSupplier(m_xConnection, UNO_QUERY);
62  if (xTSupplier.is())
63  {
64  Reference<XNameAccess> xTables = xTSupplier->getTables();
65  Sequence<OUString> aTables = xTables->getElementNames();
66  const OUString* pTables = aTables.getConstArray();
67  for (int i = 0; i < aTables.getLength(); i++)
68  {
69  OUString sEntry = pTables[i];
70  m_xTable->append_text(sEntry);
71  m_xTable->set_text(i, SwResId(ST_TABLE), 1);
72  }
73  }
74  Reference<XQueriesSupplier> xQSupplier(m_xConnection, UNO_QUERY);
75  if (xQSupplier.is())
76  {
77  Reference<XNameAccess> xQueries = xQSupplier->getQueries();
78  const Sequence<OUString> aQueries = xQueries->getElementNames();
79  int nPos = m_xTable->n_children();
80  for (const OUString& rQuery : aQueries)
81  {
82  m_xTable->append_text(rQuery);
83  m_xTable->set_text(nPos, SwResId(ST_QUERY), 1);
84  m_xTable->set_id(nPos, OUString::number(1));
85  ++nPos;
86  }
87  }
88 }
89 
91 {
92 }
93 
95 {
96  int nEntry = m_xTable->get_selected_index();
97  if (nEntry == -1)
98  return;
99 
100  OUString sTableOrQuery = m_xTable->get_text(nEntry, 0);
101  sal_Int32 nCommandType = m_xTable->get_id(nEntry).isEmpty() ? 0 : 1;
102 
103  OUString sDataSourceName;
104  Reference<XChild> xChild(m_xConnection, UNO_QUERY);
105  if(xChild.is())
106  {
107  Reference<XDataSource> xSource(xChild->getParent(), UNO_QUERY);
108  Reference<XPropertySet> xPrSet(xSource, UNO_QUERY);
109  xPrSet->getPropertyValue("Name") >>= sDataSourceName;
110  }
111  OSL_ENSURE(!sDataSourceName.isEmpty(), "no data source found");
112  Sequence<PropertyValue> aProperties(5);
113  PropertyValue* pProperties = aProperties.getArray();
114  pProperties[0].Name = "DataSourceName";
115  pProperties[0].Value <<= sDataSourceName;
116  pProperties[1].Name = "Command";
117  pProperties[1].Value <<= sTableOrQuery;
118  pProperties[2].Name = "CommandType";
119  pProperties[2].Value <<= nCommandType;
120  pProperties[3].Name = "ShowTreeView";
121  pProperties[3].Value <<= false;
122  pProperties[4].Name = "ShowTreeViewButton";
123  pProperties[4].Value <<= false;
124 
125  VclPtrInstance< SwDBTablePreviewDialog > pDlg(nullptr, aProperties); //TODO
126  pDlg->Execute();
127 }
128 
130 {
131  int nEntry = m_xTable->get_selected_index();
132  if (nEntry != -1)
133  {
134  bIsTable = m_xTable->get_id(nEntry).isEmpty();
135  return m_xTable->get_text(nEntry, 0);
136  }
137  bIsTable = false;
138  return OUString();
139 }
140 
141 void SwSelectDBTableDialog::SetSelectedTable(const OUString& rTable, bool bIsTable)
142 {
143  for (int i = 0, nCount = m_xTable->n_children(); i < nCount; ++i)
144  {
145  if (m_xTable->get_text(i, 0) == rTable &&
146  m_xTable->get_id(i).isEmpty() == bIsTable)
147  {
148  m_xTable->select(i);
149  break;
150  }
151  }
152 }
153 
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
IMPL_LINK_NOARG(SwSelectDBTableDialog, PreviewHdl, weld::Button &, void)
void SetSelectedTable(const OUString &rTable, bool bIsTable)
css::uno::Reference< css::sdbc::XConnection > m_xConnection
SwSelectDBTableDialog(weld::Window *pParent, const css::uno::Reference< css::sdbc::XConnection > &xConnection)
OUString GetSelectedTable(bool &bIsTable)
virtual ~SwSelectDBTableDialog() override
int i
OUString SwResId(const char *pId)
Definition: swmodule.cxx:191
std::unique_ptr< weld::TreeView > m_xTable
std::unique_ptr< weld::Button > m_xPreviewPB
sal_Int32 nPos