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