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