LibreOffice Module dbaccess (master) 1
CollectionView.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 <CollectionView.hxx>
22#include <core_resource.hxx>
23#include <strings.hrc>
27#include <com/sun/star/container/XChild.hpp>
28#include <com/sun/star/container/XNameContainer.hpp>
29#include <o3tl/safeint.hxx>
30#include <vcl/svapp.hxx>
31#include <vcl/weld.hxx>
32#include <UITools.hxx>
33#include <com/sun/star/container/XHierarchicalNameContainer.hpp>
34#include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
35#include <com/sun/star/ucb/IOErrorCode.hpp>
36#include <com/sun/star/ucb/XDynamicResultSet.hpp>
37#include <com/sun/star/sdbc/XRow.hpp>
38#include <com/sun/star/task/InteractionHandler.hpp>
39#include <com/sun/star/task/InteractionClassification.hpp>
40#include <com/sun/star/sdbc/SQLException.hpp>
43#include <ucbhelper/content.hxx>
45#include <utility>
46
47namespace dbaui
48{
49
50using namespace ::com::sun::star::uno;
51using namespace ::com::sun::star::ucb;
52using namespace ::com::sun::star::lang;
53using namespace ::com::sun::star::beans;
54using namespace ::com::sun::star::container;
55using namespace ::com::sun::star::task;
56using namespace ::com::sun::star::sdbc;
57using namespace comphelper;
58
60 const Reference< XContent>& _xContent,
61 const OUString& _sDefaultName,
62 css::uno::Reference< css::uno::XComponentContext > _xContext)
63 : GenericDialogController(pParent, "dbaccess/ui/collectionviewdialog.ui", "CollectionView")
64 , m_xContent(_xContent)
65 , m_xContext(std::move(_xContext))
66 , m_bCreateForm(true)
67 , m_xFTCurrentPath(m_xBuilder->weld_label("currentPathLabel"))
68 , m_xNewFolder(m_xBuilder->weld_button("newFolderButton"))
69 , m_xUp(m_xBuilder->weld_button("upButton"))
70 , m_xView(m_xBuilder->weld_tree_view("viewTreeview"))
71 , m_xName(m_xBuilder->weld_entry("fileNameEntry"))
72 , m_xPB_OK(m_xBuilder->weld_button("ok"))
73{
75 InteractionHandler::createWithParent(m_xContext, m_xDialog->GetXWindow()));
76 m_xCmdEnv = new ::ucbhelper::CommandEnvironment(xHandler, nullptr);
77
78 OSL_ENSURE(m_xContent.is(),"No valid content!");
79 m_xView->set_size_request(m_xView->get_approximate_digit_width() * 60, m_xView->get_height_rows(8));
80 m_xView->make_sorted();
81 Initialize();
83
84 m_xName->set_text(_sDefaultName);
85 m_xName->grab_focus();
86
87 m_xView->connect_row_activated( LINK( this, OCollectionView, Dbl_Click_FileView ) );
88 m_xUp->connect_clicked( LINK( this, OCollectionView, Up_Click ) );
89 m_xNewFolder->connect_clicked( LINK( this, OCollectionView, NewFolder_Click ) );
90 m_xPB_OK->connect_clicked( LINK( this, OCollectionView, Save_Click ) );
91}
92
94{
95}
96
98{
99 OUString sName = m_xName->get_text();
100 if (sName.isEmpty())
101 return;
102 try
103 {
104 sal_Int32 nIndex = sName.lastIndexOf('/') + 1;
105 if ( nIndex )
106 {
107 if ( nIndex == 1 ) // special handling for root
108 {
109 Reference<XChild> xChild(m_xContent,UNO_QUERY);
110 Reference<XNameAccess> xNameAccess(xChild,UNO_QUERY);
111 while( xNameAccess.is() )
112 {
113 xNameAccess.set(xChild->getParent(),UNO_QUERY);
114 if ( xNameAccess.is() )
115 {
116 m_xContent.set(xNameAccess,UNO_QUERY);
117 xChild.set(m_xContent,UNO_QUERY);
118 }
119 }
120 Initialize();
121 initCurrentPath();
122 }
123 OUString sSubFolder = sName.copy(0,nIndex-1);
124 sName = sName.copy(nIndex);
126 OSL_ENSURE(xHier.is(),"XHierarchicalNameContainer not supported!");
127 if ( !sSubFolder.isEmpty() && xHier.is() )
128 {
129 if ( xHier->hasByHierarchicalName(sSubFolder) )
130 {
131 m_xContent.set(xHier->getByHierarchicalName(sSubFolder),UNO_QUERY);
132 }
133 else // sub folder doesn't exist
134 {
136 {
137 {"ResourceName", Any(sSubFolder)},
138 {"ResourceType", Any(OUString("folder"))}
139 }));
140 InteractiveAugmentedIOException aException(OUString(),Reference<XInterface>(),
141 InteractionClassification_ERROR,
142 IOErrorCode_NOT_EXISTING_PATH,aValues);
143
145 InteractionHandler::createWithParent(m_xContext, m_xDialog->GetXWindow()));
147
149 pRequest->addContinuation(pApprove);
150 xHandler->handle(pRequest);
151
152 return;
153 }
154 }
155 }
156 Reference<XNameContainer> xNameContainer(m_xContent,UNO_QUERY);
157 if ( xNameContainer.is() )
158 {
159 if ( xNameContainer->hasByName(sName) )
160 {
161 std::unique_ptr<weld::MessageDialog> xQueryBox(Application::CreateMessageDialog(m_xDialog.get(),
162 VclMessageType::Question, VclButtonsType::YesNo,
163 DBA_RES(STR_ALREADYEXISTOVERWRITE)));
164 if (xQueryBox->run() != RET_YES)
165 return;
166 }
167 m_xName->set_text(sName);
168 m_xDialog->response(RET_OK);
169 }
170 }
171 catch( const Exception& )
172 {
173 DBG_UNHANDLED_EXCEPTION("dbaccess");
174 }
175}
176
178{
179 try
180 {
181 Reference<XHierarchicalNameContainer> xNameContainer(m_xContent,UNO_QUERY);
182 if ( dbaui::insertHierarchyElement(m_xDialog.get(),m_xContext,xNameContainer,OUString(),m_bCreateForm) )
183 Initialize();
184 }
185 catch( const SQLException& )
186 {
187 showError(::dbtools::SQLExceptionInfo(::cppu::getCaughtException()), m_xDialog->GetXWindow(), m_xContext);
188 }
189 catch( const Exception& )
190 {
191 DBG_UNHANDLED_EXCEPTION("dbaccess");
192 }
193}
194
196{
197 try
198 {
199 Reference<XChild> xChild(m_xContent,UNO_QUERY);
200 if ( xChild.is() )
201 {
202 Reference<XNameAccess> xNameAccess(xChild->getParent(),UNO_QUERY);
203 if ( xNameAccess.is() )
204 {
205 m_xContent.set(xNameAccess,UNO_QUERY);
206 Initialize();
207 initCurrentPath();
208 }
209 else
210 m_xUp->set_sensitive(false);
211 }
212 }
213 catch( const Exception& )
214 {
215 DBG_UNHANDLED_EXCEPTION("dbaccess");
216 }
217}
218
220{
221 try
222 {
223 Reference<XNameAccess> xNameAccess(m_xContent,UNO_QUERY);
224 if ( xNameAccess.is() )
225 {
226 OUString sSubFolder = m_xView->get_selected_text();
227 if (!sSubFolder.isEmpty())
228 {
229 Reference< XContent> xContent;
230 if ( xNameAccess->hasByName(sSubFolder) )
231 xContent.set(xNameAccess->getByName(sSubFolder),UNO_QUERY);
232 if ( xContent.is() )
233 {
234 m_xContent = xContent;
235 Initialize();
236 initCurrentPath();
237 }
238 }
239 }
240 }
241 catch( const Exception& )
242 {
243 DBG_UNHANDLED_EXCEPTION("dbaccess");
244 }
245
246 return true;
247}
248
250{
251 bool bEnable = false;
252 try
253 {
254 if ( m_xContent.is() )
255 {
256 const OUString sCID = m_xContent->getIdentifier()->getContentIdentifier();
257 static const char s_sFormsCID[] = "private:forms";
258 static const char s_sReportsCID[] = "private:reports";
259 m_bCreateForm = s_sFormsCID == sCID;
260 OUString sPath("/");
261 if ( m_bCreateForm && o3tl::make_unsigned(sCID.getLength()) != strlen(s_sFormsCID))
262 sPath = sCID.copy(strlen(s_sFormsCID));
263 else if ( !m_bCreateForm && o3tl::make_unsigned(sCID.getLength()) != strlen(s_sReportsCID) )
264 sPath = sCID.copy(strlen(s_sReportsCID) - 2);
265
266 m_xFTCurrentPath->set_label(sPath);
267 Reference<XChild> xChild(m_xContent,UNO_QUERY);
268 bEnable = xChild.is() && Reference<XNameAccess>(xChild->getParent(),UNO_QUERY).is();
269 }
270 }
271 catch( const Exception& )
272 {
273 DBG_UNHANDLED_EXCEPTION("dbaccess");
274 }
275 m_xUp->set_sensitive(bEnable);
276}
277
279{
280 return m_xName->get_text();
281}
282
283#define ROW_TITLE 1
284#define ROW_IS_FOLDER 2
285
287{
288 weld::WaitObject aWaitCursor(m_xDialog.get());
289
290 m_xView->clear();
291
292 try
293 {
295 Sequence<OUString> aProps { "Title", "IsFolder" };
296 auto xDynResultSet = aContent.createDynamicCursor(aProps, ucbhelper::INCLUDE_FOLDERS_ONLY);
297 if (!xDynResultSet.is())
298 return;
299
300 Reference<XResultSet> xResultSet = xDynResultSet->getStaticResultSet();
301 Reference<XRow> xRow(xResultSet, UNO_QUERY);
302 while (xResultSet->next())
303 {
304 if (!xRow->getBoolean(ROW_IS_FOLDER))
305 continue;
306 m_xView->append_text(xRow->getString(ROW_TITLE));
307 }
308 }
309 catch (const Exception&)
310 {
311 DBG_UNHANDLED_EXCEPTION("dbaccess");
312 }
313}
314
315} // namespace dbaui
316
317/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define ROW_IS_FOLDER
#define ROW_TITLE
OptionalString sName
uno::Reference< ucb::XContent > m_xContent
Reference< XExecutableDialog > m_xDialog
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
css::uno::Reference< css::uno::XComponentContext > m_xContext
void initCurrentPath()
sets the fixedtext to the right content
std::unique_ptr< weld::TreeView > m_xView
OCollectionView(weld::Window *pParent, const css::uno::Reference< css::ucb::XContent > &_xContent, const OUString &_sDefaultName, css::uno::Reference< css::uno::XComponentContext > _xContext)
css::uno::Reference< css::ucb::XContent > m_xContent
std::unique_ptr< weld::Entry > m_xName
std::unique_ptr< weld::Button > m_xPB_OK
OUString getName() const
virtual ~OCollectionView() override
std::unique_ptr< weld::Button > m_xUp
std::unique_ptr< weld::Label > m_xFTCurrentPath
css::uno::Reference< css::ucb::XCommandEnvironment > m_xCmdEnv
std::unique_ptr< weld::Button > m_xNewFolder
css::uno::Reference< css::ucb::XDynamicResultSet > createDynamicCursor(const css::uno::Sequence< OUString > &rPropertyNames, ResultSetInclude eMode=INCLUDE_FOLDERS_AND_DOCUMENTS)
std::shared_ptr< weld::Dialog > m_xDialog
Reference< XComponentContext > m_xContext
#define DBA_RES(id)
#define DBG_UNHANDLED_EXCEPTION(...)
sal_Int32 nIndex
@ Exception
css::uno::Sequence< css::uno::Any > InitAnyPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
Reference< XComponentContext > getProcessComponentContext()
IMPL_LINK_NOARG(OApplicationController, OnClipboardChanged, TransferableDataHelper *, void)
bool insertHierarchyElement(weld::Window *pParent, const css::uno::Reference< css::uno::XComponentContext > &_rxContext, const css::uno::Reference< css::container::XHierarchicalNameContainer > &_xNames, const OUString &_sParentFolder, bool _bForm, bool _bCollection=true, const css::uno::Reference< css::ucb::XContent > &_xContent=nullptr, bool _bMove=false)
opens a save dialog to store a form or report folder in the current hierarchy.
void showError(const SQLExceptionInfo &_rInfo, const Reference< XWindow > &_xParent, const Reference< XComponentContext > &_rxContext)
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
Reference< XComponentContext > _xContext
RET_OK
RET_YES