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