Add Marathi autocorrect
[LibreOffice.git] / sw / source / ui / fldui / changedb.cxx
bloba6b748d2e8b56fc968dbe2d4b17cafe3104016a7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 #include <com/sun/star/sdb/DatabaseContext.hpp>
24 #include <com/sun/star/sdb/CommandType.hpp>
25 #include <comphelper/processfactory.hxx>
26 #include <comphelper/sequence.hxx>
27 #include <sfx2/viewfrm.hxx>
28 #include <o3tl/string_view.hxx>
30 #include <view.hxx>
31 #include <wrtsh.hxx>
32 #include <dbmgr.hxx>
33 #include <changedb.hxx>
35 #include <strings.hrc>
36 #include <bitmaps.hlst>
38 using namespace ::com::sun::star::container;
39 using namespace ::com::sun::star::sdb;
40 using namespace ::com::sun::star::uno;
42 // edit insert-field
43 SwChangeDBDlg::SwChangeDBDlg(SwView const & rVw)
44 : SfxDialogController(rVw.GetViewFrame().GetFrameWeld(), u"modules/swriter/ui/exchangedatabases.ui"_ustr,
45 u"ExchangeDatabasesDialog"_ustr)
46 , m_pSh(rVw.GetWrtShellPtr())
47 , m_xUsedDBTLB(m_xBuilder->weld_tree_view(u"inuselb"_ustr))
48 , m_xAvailDBTLB(new SwDBTreeList(m_xBuilder->weld_tree_view(u"availablelb"_ustr)))
49 , m_xAddDBPB(m_xBuilder->weld_button(u"browse"_ustr))
50 , m_xDocDBNameFT(m_xBuilder->weld_label(u"dbnameft"_ustr))
51 , m_xDefineBT(m_xBuilder->weld_button(u"ok"_ustr))
53 int nWidth = m_xUsedDBTLB->get_approximate_digit_width() * 25;
54 int nHeight = m_xUsedDBTLB->get_height_rows(8);
55 m_xUsedDBTLB->set_size_request(nWidth, nHeight);
56 m_xAvailDBTLB->set_size_request(nWidth, nHeight);
58 m_xAvailDBTLB->SetWrtShell(*m_pSh);
59 FillDBPopup();
61 ShowDBName(m_pSh->GetDBData());
62 m_xDefineBT->connect_clicked(LINK(this, SwChangeDBDlg, ButtonHdl));
63 m_xAddDBPB->connect_clicked(LINK(this, SwChangeDBDlg, AddDBHdl));
65 m_xUsedDBTLB->set_selection_mode(SelectionMode::Multiple);
66 m_xUsedDBTLB->make_sorted();
68 Link<weld::TreeView&,void> aLink = LINK(this, SwChangeDBDlg, TreeSelectHdl);
70 m_xUsedDBTLB->connect_selection_changed(aLink);
71 m_xAvailDBTLB->connect_changed(aLink);
72 TreeSelect();
75 // initialise database listboxes
76 void SwChangeDBDlg::FillDBPopup()
78 const Reference< XComponentContext >& xContext( ::comphelper::getProcessComponentContext() );
79 Reference<XDatabaseContext> xDBContext = DatabaseContext::create(xContext);
80 const SwDBData& rDBData = m_pSh->GetDBData();
81 m_xAvailDBTLB->Select(rDBData.sDataSource, rDBData.sCommand, u"");
82 TreeSelect();
84 Sequence< OUString > aDBNames = xDBContext->getElementNames();
85 auto aAllDBNames = comphelper::sequenceToContainer<std::vector<OUString>>(aDBNames);
87 std::vector<OUString> aDBNameList;
88 m_pSh->GetAllUsedDB( aDBNameList, &aAllDBNames );
90 size_t nCount = aDBNameList.size();
91 m_xUsedDBTLB->clear();
92 std::unique_ptr<weld::TreeIter> xFirst;
94 for(size_t k = 0; k < nCount; k++)
96 std::unique_ptr<weld::TreeIter> xLast = Insert(o3tl::getToken(aDBNameList[k], 0, ';'));
97 if (!xFirst)
98 xFirst = std::move(xLast);
101 if (xFirst)
103 m_xUsedDBTLB->expand_row(*xFirst);
104 m_xUsedDBTLB->scroll_to_row(*xFirst);
105 m_xUsedDBTLB->select(*xFirst);
109 std::unique_ptr<weld::TreeIter> SwChangeDBDlg::Insert(std::u16string_view rDBName)
111 sal_Int32 nIdx{ 0 };
112 const OUString sDBName(o3tl::getToken(rDBName, 0, DB_DELIM, nIdx));
113 const OUString sTableName(o3tl::getToken(rDBName, 0, DB_DELIM, nIdx));
114 OUString sUserData( o3tl::getToken(rDBName, 0, DB_DELIM, nIdx) );
115 sal_Int32 nCommandType = sUserData.toInt32();
117 const OUString & rToInsert ( nCommandType ? RID_BMP_DBQUERY : RID_BMP_DBTABLE );
119 std::unique_ptr<weld::TreeIter> xIter(m_xUsedDBTLB->make_iterator());
120 if (m_xUsedDBTLB->get_iter_first(*xIter))
124 if (sDBName == m_xUsedDBTLB->get_text(*xIter))
126 if (m_xUsedDBTLB->iter_has_child(*xIter))
128 std::unique_ptr<weld::TreeIter> xChild(m_xUsedDBTLB->make_iterator(xIter.get()));
129 if (m_xUsedDBTLB->iter_children(*xChild))
133 if (sTableName == m_xUsedDBTLB->get_text(*xChild))
134 return xChild;
135 } while (m_xUsedDBTLB->iter_next_sibling(*xChild));
138 m_xUsedDBTLB->insert(xIter.get(), -1, &sTableName, &sUserData, nullptr, nullptr,
139 false, xIter.get());
140 m_xUsedDBTLB->set_image(*xIter, rToInsert);
141 return xIter;
143 } while (m_xUsedDBTLB->iter_next_sibling(*xIter));
146 m_xUsedDBTLB->insert(nullptr, -1, &sDBName, nullptr, nullptr, nullptr,
147 false, xIter.get());
148 m_xUsedDBTLB->set_image(*xIter, RID_BMP_DB);
149 m_xUsedDBTLB->insert(xIter.get(), -1, &sTableName, &sUserData, nullptr, nullptr,
150 false, xIter.get());
151 m_xUsedDBTLB->set_image(*xIter, rToInsert);
152 return xIter;
155 // destroy dialog
156 SwChangeDBDlg::~SwChangeDBDlg()
160 void SwChangeDBDlg::UpdateFields()
162 std::vector<OUString> aDBNames;
164 m_xUsedDBTLB->selected_foreach([this, &aDBNames](weld::TreeIter& rEntry){
165 if (m_xUsedDBTLB->get_iter_depth(rEntry))
167 std::unique_ptr<weld::TreeIter> xIter(m_xUsedDBTLB->make_iterator(&rEntry));
168 m_xUsedDBTLB->iter_parent(*xIter);
169 OUString sTmp(m_xUsedDBTLB->get_text(*xIter) +
170 OUStringChar(DB_DELIM) + m_xUsedDBTLB->get_text(rEntry) + OUStringChar(DB_DELIM) +
171 m_xUsedDBTLB->get_id(rEntry));
172 aDBNames.push_back(sTmp);
174 return false;
177 m_pSh->StartAllAction();
178 OUString sTableName;
179 OUString sColumnName;
180 sal_Bool bIsTable = false;
181 const OUString DBName(m_xAvailDBTLB->GetDBName(sTableName, sColumnName, &bIsTable));
182 const OUString sTemp = DBName
183 + OUStringChar(DB_DELIM)
184 + sTableName
185 + OUStringChar(DB_DELIM)
186 + OUString::number(bIsTable
187 ? CommandType::TABLE
188 : CommandType::QUERY);
189 m_pSh->ChangeDBFields( aDBNames, sTemp);
190 m_pSh->EndAllAction();
193 IMPL_LINK_NOARG(SwChangeDBDlg, ButtonHdl, weld::Button&, void)
195 OUString sTableName;
196 OUString sColumnName;
197 SwDBData aData;
198 sal_Bool bIsTable = false;
199 aData.sDataSource = m_xAvailDBTLB->GetDBName(sTableName, sColumnName, &bIsTable);
200 aData.sCommand = sTableName;
201 aData.nCommandType = bIsTable ? 0 : 1;
202 m_pSh->ChgDBData(aData);
203 ShowDBName(m_pSh->GetDBData());
204 m_xDialog->response(RET_OK);
207 IMPL_LINK_NOARG(SwChangeDBDlg, TreeSelectHdl, weld::TreeView&, void)
209 TreeSelect();
212 void SwChangeDBDlg::TreeSelect()
214 bool bEnable = false;
215 std::unique_ptr<weld::TreeIter> xIter(m_xAvailDBTLB->make_iterator());
216 if (m_xAvailDBTLB->get_selected(xIter.get()))
218 if (m_xAvailDBTLB->get_iter_depth(*xIter))
219 bEnable = true;
221 m_xDefineBT->set_sensitive(bEnable);
225 // convert database name for display
226 void SwChangeDBDlg::ShowDBName(const SwDBData& rDBData)
228 if (rDBData.sDataSource.isEmpty() && rDBData.sCommand.isEmpty())
230 m_xDocDBNameFT->set_label(SwResId(SW_STR_NONE));
232 else
234 const OUString sName(rDBData.sDataSource + "." + rDBData.sCommand);
235 m_xDocDBNameFT->set_label(sName.replaceAll("~", "~~"));
239 IMPL_LINK_NOARG(SwChangeDBDlg, AddDBHdl, weld::Button&, void)
241 const OUString sNewDB = SwDBManager::LoadAndRegisterDataSource(m_xDialog.get());
242 if (!sNewDB.isEmpty())
244 m_xAvailDBTLB->AddDataSource(sNewDB);
245 TreeSelect();
249 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */