LibreOffice Module sw (master) 1
changedb.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 <sal/config.h>
21
22#include <com/sun/star/lang/XMultiServiceFactory.hpp>
23#include <com/sun/star/sdb/DatabaseContext.hpp>
24#include <com/sun/star/sdb/CommandType.hpp>
27#include <sfx2/viewfrm.hxx>
28#include <o3tl/string_view.hxx>
29
30#include <view.hxx>
31#include <wrtsh.hxx>
32#include <dbmgr.hxx>
33#include <changedb.hxx>
34
35#include <strings.hrc>
36#include <bitmaps.hlst>
37
38using namespace ::com::sun::star::container;
39using namespace ::com::sun::star::lang;
40using namespace ::com::sun::star::sdb;
41using namespace ::com::sun::star::uno;
42
43// edit insert-field
45 : SfxDialogController(rVw.GetViewFrame()->GetFrameWeld(), "modules/swriter/ui/exchangedatabases.ui",
46 "ExchangeDatabasesDialog")
47 , m_pSh(rVw.GetWrtShellPtr())
48 , m_xUsedDBTLB(m_xBuilder->weld_tree_view("inuselb"))
49 , m_xAvailDBTLB(new SwDBTreeList(m_xBuilder->weld_tree_view("availablelb")))
50 , m_xAddDBPB(m_xBuilder->weld_button("browse"))
51 , m_xDocDBNameFT(m_xBuilder->weld_label("dbnameft"))
52 , m_xDefineBT(m_xBuilder->weld_button("ok"))
53{
54 int nWidth = m_xUsedDBTLB->get_approximate_digit_width() * 25;
55 int nHeight = m_xUsedDBTLB->get_height_rows(8);
56 m_xUsedDBTLB->set_size_request(nWidth, nHeight);
57 m_xAvailDBTLB->set_size_request(nWidth, nHeight);
58
59 m_xAvailDBTLB->SetWrtShell(*m_pSh);
61
63 m_xDefineBT->connect_clicked(LINK(this, SwChangeDBDlg, ButtonHdl));
64 m_xAddDBPB->connect_clicked(LINK(this, SwChangeDBDlg, AddDBHdl));
65
66 m_xUsedDBTLB->set_selection_mode(SelectionMode::Multiple);
67 m_xUsedDBTLB->make_sorted();
68
69 Link<weld::TreeView&,void> aLink = LINK(this, SwChangeDBDlg, TreeSelectHdl);
70
71 m_xUsedDBTLB->connect_changed(aLink);
72 m_xAvailDBTLB->connect_changed(aLink);
73 TreeSelect();
74}
75
76// initialise database listboxes
78{
79 Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
80 Reference<XDatabaseContext> xDBContext = DatabaseContext::create(xContext);
81 const SwDBData& rDBData = m_pSh->GetDBData();
82 m_xAvailDBTLB->Select(rDBData.sDataSource, rDBData.sCommand, u"");
83 TreeSelect();
84
85 Sequence< OUString > aDBNames = xDBContext->getElementNames();
86 auto aAllDBNames = comphelper::sequenceToContainer<std::vector<OUString>>(aDBNames);
87
88 std::vector<OUString> aDBNameList;
89 m_pSh->GetAllUsedDB( aDBNameList, &aAllDBNames );
90
91 size_t nCount = aDBNameList.size();
92 m_xUsedDBTLB->clear();
93 std::unique_ptr<weld::TreeIter> xFirst;
94
95 for(size_t k = 0; k < nCount; k++)
96 {
97 std::unique_ptr<weld::TreeIter> xLast = Insert(o3tl::getToken(aDBNameList[k], 0, ';'));
98 if (!xFirst)
99 xFirst = std::move(xLast);
100 }
101
102 if (xFirst)
103 {
104 m_xUsedDBTLB->expand_row(*xFirst);
105 m_xUsedDBTLB->scroll_to_row(*xFirst);
106 m_xUsedDBTLB->select(*xFirst);
107 }
108}
109
110std::unique_ptr<weld::TreeIter> SwChangeDBDlg::Insert(std::u16string_view rDBName)
111{
112 sal_Int32 nIdx{ 0 };
113 const OUString sDBName(o3tl::getToken(rDBName, 0, DB_DELIM, nIdx));
114 const OUString sTableName(o3tl::getToken(rDBName, 0, DB_DELIM, nIdx));
115 OUString sUserData( o3tl::getToken(rDBName, 0, DB_DELIM, nIdx) );
116 sal_Int32 nCommandType = sUserData.toInt32();
117
118 const OUString & rToInsert ( nCommandType ? RID_BMP_DBQUERY : RID_BMP_DBTABLE );
119
120 std::unique_ptr<weld::TreeIter> xIter(m_xUsedDBTLB->make_iterator());
121 if (m_xUsedDBTLB->get_iter_first(*xIter))
122 {
123 do
124 {
125 if (sDBName == m_xUsedDBTLB->get_text(*xIter))
126 {
127 if (m_xUsedDBTLB->iter_has_child(*xIter))
128 {
129 std::unique_ptr<weld::TreeIter> xChild(m_xUsedDBTLB->make_iterator(xIter.get()));
130 if (m_xUsedDBTLB->iter_children(*xChild))
131 {
132 do
133 {
134 if (sTableName == m_xUsedDBTLB->get_text(*xChild))
135 return xChild;
136 } while (m_xUsedDBTLB->iter_next_sibling(*xChild));
137 }
138 }
139 m_xUsedDBTLB->insert(xIter.get(), -1, &sTableName, &sUserData, nullptr, nullptr,
140 false, xIter.get());
141 m_xUsedDBTLB->set_image(*xIter, rToInsert);
142 return xIter;
143 }
144 } while (m_xUsedDBTLB->iter_next_sibling(*xIter));
145 }
146
147 m_xUsedDBTLB->insert(nullptr, -1, &sDBName, nullptr, nullptr, nullptr,
148 false, xIter.get());
149 m_xUsedDBTLB->set_image(*xIter, RID_BMP_DB);
150 m_xUsedDBTLB->insert(xIter.get(), -1, &sTableName, &sUserData, nullptr, nullptr,
151 false, xIter.get());
152 m_xUsedDBTLB->set_image(*xIter, rToInsert);
153 return xIter;
154}
155
156// destroy dialog
158{
159}
160
162{
163 short nRet = SfxDialogController::run();
164 if (nRet == RET_OK)
165 UpdateFields();
166 return nRet;
167}
168
170{
171 std::vector<OUString> aDBNames;
172
173 m_xUsedDBTLB->selected_foreach([this, &aDBNames](weld::TreeIter& rEntry){
174 if (m_xUsedDBTLB->get_iter_depth(rEntry))
175 {
176 std::unique_ptr<weld::TreeIter> xIter(m_xUsedDBTLB->make_iterator(&rEntry));
177 m_xUsedDBTLB->iter_parent(*xIter);
178 OUString sTmp(m_xUsedDBTLB->get_text(*xIter) +
179 OUStringChar(DB_DELIM) + m_xUsedDBTLB->get_text(rEntry) + OUStringChar(DB_DELIM) +
180 m_xUsedDBTLB->get_id(rEntry));
181 aDBNames.push_back(sTmp);
182 }
183 return false;
184 });
185
187 OUString sTableName;
188 OUString sColumnName;
189 sal_Bool bIsTable = false;
190 const OUString DBName(m_xAvailDBTLB->GetDBName(sTableName, sColumnName, &bIsTable));
191 const OUString sTemp = DBName
192 + OUStringChar(DB_DELIM)
193 + sTableName
194 + OUStringChar(DB_DELIM)
195 + OUString::number(bIsTable
196 ? CommandType::TABLE
197 : CommandType::QUERY);
198 m_pSh->ChangeDBFields( aDBNames, sTemp);
200}
201
203{
204 OUString sTableName;
205 OUString sColumnName;
207 sal_Bool bIsTable = false;
208 aData.sDataSource = m_xAvailDBTLB->GetDBName(sTableName, sColumnName, &bIsTable);
209 aData.sCommand = sTableName;
210 aData.nCommandType = bIsTable ? 0 : 1;
211 m_pSh->ChgDBData(aData);
212 ShowDBName(m_pSh->GetDBData());
213 m_xDialog->response(RET_OK);
214}
215
217{
218 TreeSelect();
219}
220
222{
223 bool bEnable = false;
224 std::unique_ptr<weld::TreeIter> xIter(m_xAvailDBTLB->make_iterator());
225 if (m_xAvailDBTLB->get_selected(xIter.get()))
226 {
227 if (m_xAvailDBTLB->get_iter_depth(*xIter))
228 bEnable = true;
229 }
230 m_xDefineBT->set_sensitive(bEnable);
231}
232
233
234// convert database name for display
236{
237 if (rDBData.sDataSource.isEmpty() && rDBData.sCommand.isEmpty())
238 {
239 m_xDocDBNameFT->set_label(SwResId(SW_STR_NONE));
240 }
241 else
242 {
243 const OUString sName(rDBData.sDataSource + "." + rDBData.sCommand);
244 m_xDocDBNameFT->set_label(sName.replaceAll("~", "~~"));
245 }
246}
247
249{
250 const OUString sNewDB = SwDBManager::LoadAndRegisterDataSource(m_xDialog.get());
251 if (!sNewDB.isEmpty())
252 {
253 m_xAvailDBTLB->AddDataSource(sNewDB);
254 TreeSelect();
255 }
256}
257
258/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XExecutableDialog > m_xDialog
IMPL_LINK_NOARG(SwChangeDBDlg, ButtonHdl, weld::Button &, void)
Definition: changedb.cxx:202
SwChangeDBDlg(SwView const &rVw)
Definition: changedb.cxx:44
std::unique_ptr< weld::TreeIter > Insert(std::u16string_view rDBName)
Definition: changedb.cxx:110
void UpdateFields()
Definition: changedb.cxx:169
SwWrtShell * m_pSh
Definition: changedb.hxx:33
std::unique_ptr< weld::TreeView > m_xUsedDBTLB
Definition: changedb.hxx:35
void FillDBPopup()
Definition: changedb.cxx:77
void TreeSelect()
Definition: changedb.cxx:221
std::unique_ptr< weld::Label > m_xDocDBNameFT
Definition: changedb.hxx:38
virtual ~SwChangeDBDlg() override
Definition: changedb.cxx:157
virtual short run() override
Definition: changedb.cxx:161
std::unique_ptr< SwDBTreeList > m_xAvailDBTLB
Definition: changedb.hxx:36
std::unique_ptr< weld::Button > m_xDefineBT
Definition: changedb.hxx:39
void ShowDBName(const SwDBData &rDBData)
Definition: changedb.cxx:235
std::unique_ptr< weld::Button > m_xAddDBPB
Definition: changedb.hxx:37
static OUString LoadAndRegisterDataSource(weld::Window *pParent, SwDocShell *pDocShell=nullptr)
Loads a data source from file and registers it.
Definition: dbmgr.cxx:2786
void StartAllAction()
For all views of this document.
Definition: edws.cxx:86
void ChangeDBFields(const std::vector< OUString > &rOldNames, const OUString &rNewName)
Definition: edfld.cxx:307
SwDBData const & GetDBData() const
Database information.
Definition: edfld.cxx:291
void GetAllUsedDB(std::vector< OUString > &rDBNameList, std::vector< OUString > const *pAllDBNames)
Definition: edfld.cxx:301
void EndAllAction()
Definition: edws.cxx:97
Definition: view.hxx:146
int nCount
weld::Window * GetFrameWeld(const SfxFrame *pFrame)
Definition: dialoghelp.cxx:19
float u
const char * sName
constexpr OUStringLiteral aData
Definition: ww8scan.hxx:48
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
OUString sDataSource
Definition: swdbdata.hxx:30
OUString sCommand
Definition: swdbdata.hxx:31
OUString SwResId(TranslateId aId)
Definition: swmodule.cxx:165
#define DB_DELIM
Definition: swtypes.hxx:130
unsigned char sal_Bool
RET_OK