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_children(*xIter))
139  {
140  do
141  {
142  if (sTableName == m_xUsedDBTLB->get_text(*xIter))
143  return xIter;
144  } while (m_xUsedDBTLB->iter_next_sibling(*xIter));
145  m_xUsedDBTLB->iter_parent(*xIter);
146  }
147  m_xUsedDBTLB->insert(xIter.get(), -1, &sTableName, &sUserData, nullptr, nullptr,
148  &rToInsert, false, xIter.get());
149  return xIter;
150  }
151  } while (m_xUsedDBTLB->iter_next_sibling(*xIter));
152  }
153 
154  m_xUsedDBTLB->insert(nullptr, -1, &sDBName, nullptr, nullptr, nullptr,
155  &aDBImg, false, xIter.get());
156  m_xUsedDBTLB->insert(xIter.get(), -1, &sTableName, &sUserData, nullptr, nullptr,
157  &rToInsert, false, xIter.get());
158  return xIter;
159 }
160 
161 // destroy dialog
163 {
164 }
165 
167 {
168  short nRet = SfxDialogController::run();
169  if (nRet == RET_OK)
170  UpdateFields();
171  return nRet;
172 }
173 
175 {
176  std::vector<OUString> aDBNames;
177 
178  m_xUsedDBTLB->selected_foreach([this, &aDBNames](weld::TreeIter& rEntry){
179  if (m_xUsedDBTLB->get_iter_depth(rEntry))
180  {
181  std::unique_ptr<weld::TreeIter> xIter(m_xUsedDBTLB->make_iterator(&rEntry));
182  m_xUsedDBTLB->iter_parent(*xIter);
183  OUString sTmp(m_xUsedDBTLB->get_text(*xIter) +
184  OUStringLiteral1(DB_DELIM) + m_xUsedDBTLB->get_text(rEntry) + OUStringLiteral1(DB_DELIM) +
185  m_xUsedDBTLB->get_id(rEntry));
186  aDBNames.push_back(sTmp);
187  }
188  return false;
189  });
190 
191  pSh->StartAllAction();
192  OUString sTableName;
193  OUString sColumnName;
194  sal_Bool bIsTable = false;
195  const OUString DBName(m_xAvailDBTLB->GetDBName(sTableName, sColumnName, &bIsTable));
196  const OUString sTemp = DBName
197  + OUStringLiteral1(DB_DELIM)
198  + sTableName
199  + OUStringLiteral1(DB_DELIM)
200  + OUString::number(bIsTable
201  ? CommandType::TABLE
202  : CommandType::QUERY);
203  pSh->ChangeDBFields( aDBNames, sTemp);
204  pSh->EndAllAction();
205 }
206 
208 {
209  OUString sTableName;
210  OUString sColumnName;
211  SwDBData aData;
212  sal_Bool bIsTable = false;
213  aData.sDataSource = m_xAvailDBTLB->GetDBName(sTableName, sColumnName, &bIsTable);
214  aData.sCommand = sTableName;
215  aData.nCommandType = bIsTable ? 0 : 1;
216  pSh->ChgDBData(aData);
217  ShowDBName(pSh->GetDBData());
218  m_xDialog->response(RET_OK);
219 }
220 
222 {
223  TreeSelect();
224 }
225 
227 {
228  bool bEnable = false;
229  std::unique_ptr<weld::TreeIter> xIter(m_xAvailDBTLB->make_iterator());
230  if (m_xAvailDBTLB->get_selected(xIter.get()))
231  {
232  if (m_xAvailDBTLB->get_iter_depth(*xIter))
233  bEnable = true;
234  }
235  m_xDefineBT->set_sensitive(bEnable);
236 }
237 
238 
239 // convert database name for display
241 {
242  if (rDBData.sDataSource.isEmpty() && rDBData.sCommand.isEmpty())
243  {
244  m_xDocDBNameFT->set_label(SwResId(SW_STR_NONE));
245  }
246  else
247  {
248  const OUString sName(rDBData.sDataSource + "." + rDBData.sCommand);
249  m_xDocDBNameFT->set_label(sName.replaceAll("~", "~~"));
250  }
251 }
252 
254 {
255  const OUString sNewDB = SwDBManager::LoadAndRegisterDataSource(m_xDialog.get());
256  if (!sNewDB.isEmpty())
257  {
258  m_xAvailDBTLB->AddDataSource(sNewDB);
259  TreeSelect();
260  }
261 }
262 
263 /* 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:226
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:162
void EndAllAction()
Definition: edws.cxx:96
void ShowDBName(const SwDBData &rDBData)
Definition: changedb.cxx:240
void GetAllUsedDB(std::vector< OUString > &rDBNameList, std::vector< OUString > const *pAllDBNames)
Definition: edfld.cxx:313
std::unique_ptr< weld::Builder > m_xBuilder
void UpdateFields()
Definition: changedb.cxx:174
OUString sName
virtual short run() override
Definition: changedb.cxx:166
SwWrtShell * pSh
Definition: changedb.hxx:33
unsigned char sal_Bool
weld::Window * GetFrameWeld(SfxFrame *pFrame)
Definition: dialoghelp.cxx:20
SwDBData const & GetDBData() const
Database information.
Definition: edfld.cxx:298
GUIDCNamePair const aData
OUString SwResId(const char *pId)
Definition: swmodule.cxx:191
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:207
static OUString LoadAndRegisterDataSource(weld::Window *pParent, SwDocShell *pDocShell=nullptr)
Loads a data source from file and registers it.
Definition: dbmgr.cxx:2876
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