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