Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / dbaccess / source / ui / dlg / CollectionView.cxx
blob355b3c9bd46ee0975ccce4c45711c27514a8124e
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 <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>
45 #include <utility>
47 namespace dbaui
50 using namespace ::com::sun::star::uno;
51 using namespace ::com::sun::star::ucb;
52 using namespace ::com::sun::star::container;
53 using namespace ::com::sun::star::task;
54 using namespace ::com::sun::star::sdbc;
55 using namespace comphelper;
57 OCollectionView::OCollectionView(weld::Window* pParent,
58 const Reference< XContent>& _xContent,
59 const OUString& _sDefaultName,
60 css::uno::Reference< css::uno::XComponentContext > _xContext)
61 : GenericDialogController(pParent, u"dbaccess/ui/collectionviewdialog.ui"_ustr, u"CollectionView"_ustr)
62 , m_xContent(_xContent)
63 , m_xContext(std::move(_xContext))
64 , m_bCreateForm(true)
65 , m_xFTCurrentPath(m_xBuilder->weld_label(u"currentPathLabel"_ustr))
66 , m_xNewFolder(m_xBuilder->weld_button(u"newFolderButton"_ustr))
67 , m_xUp(m_xBuilder->weld_button(u"upButton"_ustr))
68 , m_xView(m_xBuilder->weld_tree_view(u"viewTreeview"_ustr))
69 , m_xName(m_xBuilder->weld_entry(u"fileNameEntry"_ustr))
70 , m_xPB_OK(m_xBuilder->weld_button(u"ok"_ustr))
72 Reference<XInteractionHandler2> xHandler(
73 InteractionHandler::createWithParent(m_xContext, m_xDialog->GetXWindow()));
74 m_xCmdEnv = new ::ucbhelper::CommandEnvironment(xHandler, nullptr);
76 OSL_ENSURE(m_xContent.is(),"No valid content!");
77 m_xView->set_size_request(m_xView->get_approximate_digit_width() * 60, m_xView->get_height_rows(8));
78 m_xView->make_sorted();
79 Initialize();
80 initCurrentPath();
82 m_xName->set_text(_sDefaultName);
83 m_xName->grab_focus();
85 m_xView->connect_row_activated( LINK( this, OCollectionView, Dbl_Click_FileView ) );
86 m_xUp->connect_clicked( LINK( this, OCollectionView, Up_Click ) );
87 m_xNewFolder->connect_clicked( LINK( this, OCollectionView, NewFolder_Click ) );
88 m_xPB_OK->connect_clicked( LINK( this, OCollectionView, Save_Click ) );
91 OCollectionView::~OCollectionView()
95 IMPL_LINK_NOARG(OCollectionView, Save_Click, weld::Button&, void)
97 OUString sName = m_xName->get_text();
98 if (sName.isEmpty())
99 return;
102 sal_Int32 nIndex = sName.lastIndexOf('/') + 1;
103 if ( nIndex )
105 if ( nIndex == 1 ) // special handling for root
107 Reference<XChild> xChild(m_xContent,UNO_QUERY);
108 Reference<XNameAccess> xNameAccess(xChild,UNO_QUERY);
109 while( xNameAccess.is() )
111 xNameAccess.set(xChild->getParent(),UNO_QUERY);
112 if ( xNameAccess.is() )
114 m_xContent.set(xNameAccess,UNO_QUERY);
115 xChild.set(m_xContent,UNO_QUERY);
118 Initialize();
119 initCurrentPath();
121 OUString sSubFolder = sName.copy(0,nIndex-1);
122 sName = sName.copy(nIndex);
123 Reference<XHierarchicalNameContainer> xHier(m_xContent,UNO_QUERY);
124 OSL_ENSURE(xHier.is(),"XHierarchicalNameContainer not supported!");
125 if ( !sSubFolder.isEmpty() && xHier.is() )
127 if ( xHier->hasByHierarchicalName(sSubFolder) )
129 m_xContent.set(xHier->getByHierarchicalName(sSubFolder),UNO_QUERY);
131 else // sub folder doesn't exist
133 Sequence<Any> aValues(comphelper::InitAnyPropertySequence(
135 {"ResourceName", Any(sSubFolder)},
136 {"ResourceType", Any(u"folder"_ustr)}
137 }));
138 InteractiveAugmentedIOException aException(OUString(),Reference<XInterface>(),
139 InteractionClassification_ERROR,
140 IOErrorCode_NOT_EXISTING_PATH,aValues);
142 Reference<XInteractionHandler2> xHandler(
143 InteractionHandler::createWithParent(m_xContext, m_xDialog->GetXWindow()));
144 rtl::Reference<OInteractionRequest> pRequest = new OInteractionRequest(Any(aException));
146 rtl::Reference<OInteractionApprove> pApprove = new OInteractionApprove;
147 pRequest->addContinuation(pApprove);
148 xHandler->handle(pRequest);
150 return;
154 Reference<XNameContainer> xNameContainer(m_xContent,UNO_QUERY);
155 if ( xNameContainer.is() )
157 if ( xNameContainer->hasByName(sName) )
159 std::unique_ptr<weld::MessageDialog> xQueryBox(Application::CreateMessageDialog(m_xDialog.get(),
160 VclMessageType::Question, VclButtonsType::YesNo,
161 DBA_RES(STR_ALREADYEXISTOVERWRITE)));
162 if (xQueryBox->run() != RET_YES)
163 return;
165 m_xName->set_text(sName);
166 m_xDialog->response(RET_OK);
169 catch( const Exception& )
171 DBG_UNHANDLED_EXCEPTION("dbaccess");
175 IMPL_LINK_NOARG(OCollectionView, NewFolder_Click, weld::Button&, void)
179 Reference<XHierarchicalNameContainer> xNameContainer(m_xContent,UNO_QUERY);
180 if ( dbaui::insertHierarchyElement(m_xDialog.get(),m_xContext,xNameContainer,OUString(),m_bCreateForm) )
181 Initialize();
183 catch( const SQLException& )
185 showError(::dbtools::SQLExceptionInfo(::cppu::getCaughtException()), m_xDialog->GetXWindow(), m_xContext);
187 catch( const Exception& )
189 DBG_UNHANDLED_EXCEPTION("dbaccess");
193 IMPL_LINK_NOARG(OCollectionView, Up_Click, weld::Button&, void)
197 Reference<XChild> xChild(m_xContent,UNO_QUERY);
198 if ( xChild.is() )
200 Reference<XNameAccess> xNameAccess(xChild->getParent(),UNO_QUERY);
201 if ( xNameAccess.is() )
203 m_xContent.set(xNameAccess,UNO_QUERY);
204 Initialize();
205 initCurrentPath();
207 else
208 m_xUp->set_sensitive(false);
211 catch( const Exception& )
213 DBG_UNHANDLED_EXCEPTION("dbaccess");
217 IMPL_LINK_NOARG(OCollectionView, Dbl_Click_FileView, weld::TreeView&, bool)
221 Reference<XNameAccess> xNameAccess(m_xContent,UNO_QUERY);
222 if ( xNameAccess.is() )
224 OUString sSubFolder = m_xView->get_selected_text();
225 if (!sSubFolder.isEmpty())
227 Reference< XContent> xContent;
228 if ( xNameAccess->hasByName(sSubFolder) )
229 xContent.set(xNameAccess->getByName(sSubFolder),UNO_QUERY);
230 if ( xContent.is() )
232 m_xContent = std::move(xContent);
233 Initialize();
234 initCurrentPath();
239 catch( const Exception& )
241 DBG_UNHANDLED_EXCEPTION("dbaccess");
244 return true;
247 void OCollectionView::initCurrentPath()
249 bool bEnable = false;
252 if ( m_xContent.is() )
254 const OUString sCID = m_xContent->getIdentifier()->getContentIdentifier();
255 static const char s_sFormsCID[] = "private:forms";
256 static const char s_sReportsCID[] = "private:reports";
257 m_bCreateForm = s_sFormsCID == sCID;
258 OUString sPath(u"/"_ustr);
259 if ( m_bCreateForm && o3tl::make_unsigned(sCID.getLength()) != strlen(s_sFormsCID))
260 sPath = sCID.copy(strlen(s_sFormsCID));
261 else if ( !m_bCreateForm && o3tl::make_unsigned(sCID.getLength()) != strlen(s_sReportsCID) )
262 sPath = sCID.copy(strlen(s_sReportsCID) - 2);
264 m_xFTCurrentPath->set_label(sPath);
265 Reference<XChild> xChild(m_xContent,UNO_QUERY);
266 bEnable = xChild.is() && Reference<XNameAccess>(xChild->getParent(),UNO_QUERY).is();
269 catch( const Exception& )
271 DBG_UNHANDLED_EXCEPTION("dbaccess");
273 m_xUp->set_sensitive(bEnable);
276 OUString OCollectionView::getName() const
278 return m_xName->get_text();
281 #define ROW_TITLE 1
282 #define ROW_IS_FOLDER 2
284 void OCollectionView::Initialize()
286 weld::WaitObject aWaitCursor(m_xDialog.get());
288 m_xView->clear();
292 ::ucbhelper::Content aContent(m_xContent, m_xCmdEnv, comphelper::getProcessComponentContext());
293 Sequence<OUString> aProps { u"Title"_ustr, u"IsFolder"_ustr };
294 auto xDynResultSet = aContent.createDynamicCursor(aProps, ucbhelper::INCLUDE_FOLDERS_ONLY);
295 if (!xDynResultSet.is())
296 return;
298 Reference<XResultSet> xResultSet = xDynResultSet->getStaticResultSet();
299 Reference<XRow> xRow(xResultSet, UNO_QUERY);
300 while (xResultSet->next())
302 if (!xRow->getBoolean(ROW_IS_FOLDER))
303 continue;
304 m_xView->append_text(xRow->getString(ROW_TITLE));
307 catch (const Exception&)
309 DBG_UNHANDLED_EXCEPTION("dbaccess");
313 } // namespace dbaui
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */