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 "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>
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
;
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
71 TODO: If there is real need for caching the content, it must
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())
119 void SmartContent::disableInteractionHandler()
121 m_xOwnInteraction
.clear();
126 void SmartContent::bindTo( const OUString
& _rURL
)
128 if ( getURL() == _rURL
)
129 // nothing to do, regardless of the state
133 m_eState
= INVALID
; // default to INVALID
136 if ( !m_sURL
.isEmpty() )
140 m_oContent
.emplace( _rURL
, m_xCmdEnv
, comphelper::getProcessComponentContext() );
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
146 catch( const ContentCreationException
& )
149 catch( const Exception
& )
151 TOOLS_WARN_EXCEPTION( "fpicker", "SmartContent::bindTo: unexpected exception caught!" );
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();
165 pHandler
->resetUseState();
166 pHandler
->forgetRequest();
171 bool SmartContent::implIs( const OUString
& _rURL
, Type _eType
)
173 // bind to this content
176 // did we survive this?
177 if ( isInvalid() || !isBound() )
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)
187 if ( Folder
== _eType
)
188 bIs
= m_oContent
->isFolder();
190 bIs
= m_oContent
->isDocument();
192 // from here on, we definitely know that the content is valid
195 catch( const Exception
& )
197 // now we're definitely invalid
204 void SmartContent::getTitle( OUString
& /* [out] */ _rTitle
)
206 if ( !isBound() || isInvalid() )
212 m_oContent
->getPropertyValue("Title") >>= sTitle
;
215 // from here on, we definitely know that the content is valid
218 catch( const css::uno::Exception
& )
220 // now we're definitely invalid
226 bool SmartContent::hasParentFolder( )
228 if ( !isBound() || isInvalid() )
234 Reference
< XChild
> xChild( m_oContent
->get(), UNO_QUERY
);
237 Reference
< XContent
> xParent( xChild
->getParent(), UNO_QUERY
);
240 const OUString
aParentURL( xParent
->getIdentifier()->getContentIdentifier() );
241 bRet
= ( !aParentURL
.isEmpty() && aParentURL
!= m_oContent
->getURL() );
243 // now we're definitely valid
248 catch( const Exception
& )
250 // now we're definitely invalid
257 bool SmartContent::canCreateFolder( )
259 if ( !isBound() || isInvalid() )
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
)
276 // now we're definitely valid
279 catch( const Exception
& )
281 // now we're definitely invalid
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
;
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
& )
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */