Bump version to 24.04.3.4
[LibreOffice.git] / fpicker / source / office / fpsmartcontent.cxx
blob4cc504ddaa14b511ece5f872832a291fd0d44252
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 "fpsmartcontent.hxx"
22 #include <com/sun/star/container/XChild.hpp>
23 #include <com/sun/star/task/InteractionHandler.hpp>
24 #include <com/sun/star/ucb/ContentCreationException.hpp>
25 #include <com/sun/star/ucb/ContentInfo.hpp>
26 #include <com/sun/star/ucb/ContentInfoAttribute.hpp>
27 #include <com/sun/star/ucb/XContent.hpp>
29 #include <comphelper/processfactory.hxx>
30 #include <ucbhelper/commandenvironment.hxx>
31 #include <comphelper/diagnose_ex.hxx>
34 namespace svt
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::task;
40 using namespace ::com::sun::star::ucb;
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star::container;
45 //= SmartContent
48 SmartContent::SmartContent()
49 :m_eState( NOT_BOUND )
54 SmartContent::SmartContent( const OUString& _rInitialURL )
55 :m_eState( NOT_BOUND )
57 bindTo( _rInitialURL );
61 SmartContent::~SmartContent()
63 /* This destructor originally contained the following blurb: "Do
64 not delete the content. Because the content will be used by
65 the cache." This is just plain silly, because it relies on
66 the provider caching created contents (which is done by
67 ucbhelper::ContentProviderImplHelper, but we do not actually
68 expect all providers to use that, right?) Otherwise we are
69 just leaking memory.
71 TODO: If there is real need for caching the content, it must
72 be done here.
77 void SmartContent::enableOwnInteractionHandler(::svt::OFilePickerInteractionHandler::EInterceptedInteractions eInterceptions)
79 Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
80 Reference< XInteractionHandler > xGlobalInteractionHandler(
81 InteractionHandler::createWithParent(xContext, nullptr), UNO_QUERY_THROW );
83 m_xOwnInteraction = new ::svt::OFilePickerInteractionHandler(xGlobalInteractionHandler);
84 m_xOwnInteraction->enableInterceptions(eInterceptions);
86 m_xCmdEnv = new ::ucbhelper::CommandEnvironment( m_xOwnInteraction, Reference< XProgressHandler >() );
90 void SmartContent::enableDefaultInteractionHandler()
92 m_xOwnInteraction.clear();
94 Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
95 Reference< XInteractionHandler > xGlobalInteractionHandler(
96 InteractionHandler::createWithParent(xContext, nullptr), UNO_QUERY_THROW );
97 m_xCmdEnv = new ucbhelper::CommandEnvironment( xGlobalInteractionHandler, Reference< XProgressHandler >() );
101 ::svt::OFilePickerInteractionHandler* SmartContent::getOwnInteractionHandler() const
103 return m_xOwnInteraction.get();
107 SmartContent::InteractionHandlerType SmartContent::queryCurrentInteractionHandler() const
109 if (m_xOwnInteraction.is())
110 return IHT_OWN;
112 if (!m_xCmdEnv.is())
113 return IHT_NONE;
115 return IHT_DEFAULT;
119 void SmartContent::disableInteractionHandler()
121 m_xOwnInteraction.clear();
122 m_xCmdEnv.clear();
126 void SmartContent::bindTo( const OUString& _rURL )
128 if ( getURL() == _rURL )
129 // nothing to do, regardless of the state
130 return;
132 m_oContent.reset();
133 m_eState = INVALID; // default to INVALID
134 m_sURL = _rURL;
136 if ( !m_sURL.isEmpty() )
140 m_oContent.emplace( _rURL, m_xCmdEnv, comphelper::getProcessComponentContext() );
141 m_eState = UNKNOWN;
142 // from now on, the state is unknown -> we cannot know for sure if the content
143 // is really valid (some UCP's only tell this when asking for properties, not upon
144 // creation)
146 catch( const ContentCreationException& )
149 catch( const Exception& )
151 TOOLS_WARN_EXCEPTION( "fpicker", "SmartContent::bindTo: unexpected exception caught!" );
154 else
156 m_eState = NOT_BOUND;
160 // don't forget to reset the may internal used interaction handler ...
161 // But do it only for our own specialized interaction helper!
162 ::svt::OFilePickerInteractionHandler* pHandler = getOwnInteractionHandler();
163 if (pHandler)
165 pHandler->resetUseState();
166 pHandler->forgetRequest();
171 bool SmartContent::implIs( const OUString& _rURL, Type _eType )
173 // bind to this content
174 bindTo( _rURL );
176 // did we survive this?
177 if ( isInvalid() || !isBound() )
178 return false;
180 assert( m_oContent && "SmartContent::implIs: inconsistence!" );
181 // if, after a bindTo, we don't have a content, then we should be INVALID, or at least
182 // NOT_BOUND (the latter happens, for example, if somebody tries to ask for an empty URL)
184 bool bIs = false;
187 if ( Folder == _eType )
188 bIs = m_oContent->isFolder();
189 else
190 bIs = m_oContent->isDocument();
192 // from here on, we definitely know that the content is valid
193 m_eState = VALID;
195 catch( const Exception& )
197 // now we're definitely invalid
198 m_eState = INVALID;
200 return bIs;
204 void SmartContent::getTitle( OUString& /* [out] */ _rTitle )
206 if ( !isBound() || isInvalid() )
207 return;
211 OUString sTitle;
212 m_oContent->getPropertyValue("Title") >>= sTitle;
213 _rTitle = sTitle;
215 // from here on, we definitely know that the content is valid
216 m_eState = VALID;
218 catch( const css::uno::Exception& )
220 // now we're definitely invalid
221 m_eState = INVALID;
226 bool SmartContent::hasParentFolder( )
228 if ( !isBound() || isInvalid() )
229 return false;
231 bool bRet = false;
234 Reference< XChild > xChild( m_oContent->get(), UNO_QUERY );
235 if ( xChild.is() )
237 Reference< XContent > xParent( xChild->getParent(), UNO_QUERY );
238 if ( xParent.is() )
240 const OUString aParentURL( xParent->getIdentifier()->getContentIdentifier() );
241 bRet = ( !aParentURL.isEmpty() && aParentURL != m_oContent->getURL() );
243 // now we're definitely valid
244 m_eState = VALID;
248 catch( const Exception& )
250 // now we're definitely invalid
251 m_eState = INVALID;
253 return bRet;
257 bool SmartContent::canCreateFolder( )
259 if ( !isBound() || isInvalid() )
260 return false;
262 bool bRet = false;
265 const css::uno::Sequence<css::ucb::ContentInfo> aContentsInfo = m_oContent->queryCreatableContentsInfo();
266 for ( auto const& rInfo : aContentsInfo )
268 // Simply look for the first KIND_FOLDER...
269 if ( rInfo.Attributes & ContentInfoAttribute::KIND_FOLDER )
271 bRet = true;
272 break;
276 // now we're definitely valid
277 m_eState = VALID;
279 catch( const Exception& )
281 // now we're definitely invalid
282 m_eState = INVALID;
284 return bRet;
287 OUString SmartContent::createFolder( const OUString& _rTitle )
289 OUString aCreatedUrl;
292 OUString sFolderType;
294 const css::uno::Sequence<css::ucb::ContentInfo> aContentsInfo = m_oContent->queryCreatableContentsInfo();
295 for ( auto const& rInfo : aContentsInfo )
297 // Simply look for the first KIND_FOLDER...
298 if ( rInfo.Attributes & ContentInfoAttribute::KIND_FOLDER )
300 sFolderType = rInfo.Type;
301 break;
305 if ( !sFolderType.isEmpty() )
307 ucbhelper::Content aCreated;
308 Sequence< OUString > aNames { "Title" };
309 Sequence< Any > aValues { Any(_rTitle) };
310 m_oContent->insertNewContent( sFolderType, aNames, aValues, aCreated );
312 aCreatedUrl = aCreated.getURL();
315 catch( const Exception& )
318 return aCreatedUrl;
322 } // namespace svt
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */