1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <CollectionView.hxx>
21 #include <comphelper/diagnose_ex.hxx>
22 #include <core_resource.hxx>
23 #include <strings.hrc>
24 #include <comphelper/interaction.hxx>
25 #include <comphelper/propertysequence.hxx>
26 #include <cppuhelper/exc_hlp.hxx>
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>
41 #include <comphelper/processfactory.hxx>
42 #include <ucbhelper/commandenvironment.hxx>
43 #include <ucbhelper/content.hxx>
44 #include <connectivity/dbexception.hxx>
50 using namespace ::com::sun::star::uno
;
51 using namespace ::com::sun::star::ucb
;
52 using namespace ::com::sun::star::lang
;
53 using namespace ::com::sun::star::beans
;
54 using namespace ::com::sun::star::container
;
55 using namespace ::com::sun::star::task
;
56 using namespace ::com::sun::star::sdbc
;
57 using namespace comphelper
;
59 OCollectionView::OCollectionView(weld::Window
* pParent
,
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
))
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"))
74 Reference
<XInteractionHandler2
> xHandler(
75 InteractionHandler::createWithParent(m_xContext
, m_xDialog
->GetXWindow()));
76 m_xCmdEnv
= new ::ucbhelper::CommandEnvironment(xHandler
, nullptr);
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();
84 m_xName
->set_text(_sDefaultName
);
85 m_xName
->grab_focus();
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
) );
93 OCollectionView::~OCollectionView()
97 IMPL_LINK_NOARG(OCollectionView
, Save_Click
, weld::Button
&, void)
99 OUString sName
= m_xName
->get_text();
104 sal_Int32 nIndex
= sName
.lastIndexOf('/') + 1;
107 if ( nIndex
== 1 ) // special handling for root
109 Reference
<XChild
> xChild(m_xContent
,UNO_QUERY
);
110 Reference
<XNameAccess
> xNameAccess(xChild
,UNO_QUERY
);
111 while( xNameAccess
.is() )
113 xNameAccess
.set(xChild
->getParent(),UNO_QUERY
);
114 if ( xNameAccess
.is() )
116 m_xContent
.set(xNameAccess
,UNO_QUERY
);
117 xChild
.set(m_xContent
,UNO_QUERY
);
123 OUString sSubFolder
= sName
.copy(0,nIndex
-1);
124 sName
= sName
.copy(nIndex
);
125 Reference
<XHierarchicalNameContainer
> xHier(m_xContent
,UNO_QUERY
);
126 OSL_ENSURE(xHier
.is(),"XHierarchicalNameContainer not supported!");
127 if ( !sSubFolder
.isEmpty() && xHier
.is() )
129 if ( xHier
->hasByHierarchicalName(sSubFolder
) )
131 m_xContent
.set(xHier
->getByHierarchicalName(sSubFolder
),UNO_QUERY
);
133 else // sub folder doesn't exist
135 Sequence
<Any
> aValues(comphelper::InitAnyPropertySequence(
137 {"ResourceName", Any(sSubFolder
)},
138 {"ResourceType", Any(OUString("folder"))}
140 InteractiveAugmentedIOException
aException(OUString(),Reference
<XInterface
>(),
141 InteractionClassification_ERROR
,
142 IOErrorCode_NOT_EXISTING_PATH
,aValues
);
144 Reference
<XInteractionHandler2
> xHandler(
145 InteractionHandler::createWithParent(m_xContext
, m_xDialog
->GetXWindow()));
146 rtl::Reference
<OInteractionRequest
> pRequest
= new OInteractionRequest(Any(aException
));
148 rtl::Reference
<OInteractionApprove
> pApprove
= new OInteractionApprove
;
149 pRequest
->addContinuation(pApprove
);
150 xHandler
->handle(pRequest
);
156 Reference
<XNameContainer
> xNameContainer(m_xContent
,UNO_QUERY
);
157 if ( xNameContainer
.is() )
159 if ( xNameContainer
->hasByName(sName
) )
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
)
167 m_xName
->set_text(sName
);
168 m_xDialog
->response(RET_OK
);
171 catch( const Exception
& )
173 DBG_UNHANDLED_EXCEPTION("dbaccess");
177 IMPL_LINK_NOARG(OCollectionView
, NewFolder_Click
, weld::Button
&, void)
181 Reference
<XHierarchicalNameContainer
> xNameContainer(m_xContent
,UNO_QUERY
);
182 if ( dbaui::insertHierarchyElement(m_xDialog
.get(),m_xContext
,xNameContainer
,OUString(),m_bCreateForm
) )
185 catch( const SQLException
& )
187 showError(::dbtools::SQLExceptionInfo(::cppu::getCaughtException()), m_xDialog
->GetXWindow(), m_xContext
);
189 catch( const Exception
& )
191 DBG_UNHANDLED_EXCEPTION("dbaccess");
195 IMPL_LINK_NOARG(OCollectionView
, Up_Click
, weld::Button
&, void)
199 Reference
<XChild
> xChild(m_xContent
,UNO_QUERY
);
202 Reference
<XNameAccess
> xNameAccess(xChild
->getParent(),UNO_QUERY
);
203 if ( xNameAccess
.is() )
205 m_xContent
.set(xNameAccess
,UNO_QUERY
);
210 m_xUp
->set_sensitive(false);
213 catch( const Exception
& )
215 DBG_UNHANDLED_EXCEPTION("dbaccess");
219 IMPL_LINK_NOARG(OCollectionView
, Dbl_Click_FileView
, weld::TreeView
&, bool)
223 Reference
<XNameAccess
> xNameAccess(m_xContent
,UNO_QUERY
);
224 if ( xNameAccess
.is() )
226 OUString sSubFolder
= m_xView
->get_selected_text();
227 if (!sSubFolder
.isEmpty())
229 Reference
< XContent
> xContent
;
230 if ( xNameAccess
->hasByName(sSubFolder
) )
231 xContent
.set(xNameAccess
->getByName(sSubFolder
),UNO_QUERY
);
234 m_xContent
= xContent
;
241 catch( const Exception
& )
243 DBG_UNHANDLED_EXCEPTION("dbaccess");
249 void OCollectionView::initCurrentPath()
251 bool bEnable
= false;
254 if ( m_xContent
.is() )
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
;
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);
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();
271 catch( const Exception
& )
273 DBG_UNHANDLED_EXCEPTION("dbaccess");
275 m_xUp
->set_sensitive(bEnable
);
278 OUString
OCollectionView::getName() const
280 return m_xName
->get_text();
284 #define ROW_IS_FOLDER 2
286 void OCollectionView::Initialize()
288 weld::WaitObject
aWaitCursor(m_xDialog
.get());
294 ::ucbhelper::Content
aContent(m_xContent
, m_xCmdEnv
, comphelper::getProcessComponentContext());
295 Sequence
<OUString
> aProps
{ "Title", "IsFolder" };
296 auto xDynResultSet
= aContent
.createDynamicCursor(aProps
, ucbhelper::INCLUDE_FOLDERS_ONLY
);
297 if (!xDynResultSet
.is())
300 Reference
<XResultSet
> xResultSet
= xDynResultSet
->getStaticResultSet();
301 Reference
<XRow
> xRow(xResultSet
, UNO_QUERY
);
302 while (xResultSet
->next())
304 if (!xRow
->getBoolean(ROW_IS_FOLDER
))
306 m_xView
->append_text(xRow
->getString(ROW_TITLE
));
309 catch (const Exception
&)
311 DBG_UNHANDLED_EXCEPTION("dbaccess");
317 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */