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