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/ContentInfo.hpp>
25 #include <com/sun/star/ucb/ContentInfoAttribute.hpp>
26 #include <com/sun/star/ucb/XContent.hpp>
28 #include <comphelper/processfactory.hxx>
29 #include <ucbhelper/commandenvironment.hxx>
30 #include <tools/solar.h>
31 #include <tools/debug.hxx>
32 #include <osl/diagnose.h>
39 using namespace ::com::sun::star::uno
;
40 using namespace ::com::sun::star::task
;
41 using namespace ::com::sun::star::ucb
;
42 using namespace ::com::sun::star::lang
;
43 using namespace ::com::sun::star::container
;
49 SmartContent::SmartContent()
51 ,m_eState( NOT_BOUND
)
52 ,m_pOwnInteraction( NULL
)
57 SmartContent::SmartContent( const OUString
& _rInitialURL
)
59 ,m_eState( NOT_BOUND
)
60 ,m_pOwnInteraction( NULL
)
62 bindTo( _rInitialURL
);
66 SmartContent::~SmartContent()
68 /* This destructor originally contained the following blurb: "Do
69 not delete the content. Because the content will be used by
70 the cache." This is just plain silly, because it relies on
71 the provider caching created contents (which is done by
72 ucbhelper::ContentProviderImplHelper, but we do not actually
73 expect all providers to use that, right?) Otherwise we are
76 TODO: If there is real need for caching the content, it must
83 void SmartContent::enableOwnInteractionHandler(::svt::OFilePickerInteractionHandler::EInterceptedInteractions eInterceptions
)
85 Reference
< XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
86 Reference
< XInteractionHandler
> xGlobalInteractionHandler(
87 InteractionHandler::createWithParent(xContext
, 0), UNO_QUERY_THROW
);
89 m_pOwnInteraction
= new ::svt::OFilePickerInteractionHandler(xGlobalInteractionHandler
);
90 m_pOwnInteraction
->enableInterceptions(eInterceptions
);
91 m_xOwnInteraction
= m_pOwnInteraction
;
93 m_xCmdEnv
= new ::ucbhelper::CommandEnvironment( m_xOwnInteraction
, Reference
< XProgressHandler
>() );
97 void SmartContent::enableDefaultInteractionHandler()
99 // Don't free the memory here! It will be done by the next
100 // call automatically - releasing of the uno reference ...
101 m_pOwnInteraction
= NULL
;
102 m_xOwnInteraction
.clear();
104 Reference
< XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
105 Reference
< XInteractionHandler
> xGlobalInteractionHandler(
106 InteractionHandler::createWithParent(xContext
, 0), UNO_QUERY_THROW
);
107 m_xCmdEnv
= new ucbhelper::CommandEnvironment( xGlobalInteractionHandler
, Reference
< XProgressHandler
>() );
111 ::svt::OFilePickerInteractionHandler
* SmartContent::getOwnInteractionHandler() const
113 if (!m_xOwnInteraction
.is())
115 return m_pOwnInteraction
;
119 SmartContent::InteractionHandlerType
SmartContent::queryCurrentInteractionHandler() const
121 if (m_xOwnInteraction
.is())
131 void SmartContent::disableInteractionHandler()
133 // Don't free the memory here! It will be done by the next
134 // call automatically - releasing of the uno reference ...
135 m_pOwnInteraction
= NULL
;
136 m_xOwnInteraction
.clear();
142 void SmartContent::bindTo( const OUString
& _rURL
)
144 if ( getURL() == _rURL
)
145 // nothing to do, regardless of the state
148 DELETEZ( m_pContent
);
149 m_eState
= INVALID
; // default to INVALID
152 if ( !m_sURL
.isEmpty() )
156 m_pContent
= new ::ucbhelper::Content( _rURL
, m_xCmdEnv
, comphelper::getProcessComponentContext() );
158 // from now on, the state is unknown -> we cannot know for sure if the content
159 // is really valid (some UCP's only tell this when asking for properties, not upon
162 catch( const ContentCreationException
& )
165 catch( const Exception
& )
167 OSL_FAIL( "SmartContent::bindTo: unexpected exception caught!" );
172 m_eState
= NOT_BOUND
;
176 // don't forget to reset the may internal used interaction handler ...
177 // But do it only for our own specialized interaction helper!
178 ::svt::OFilePickerInteractionHandler
* pHandler
= getOwnInteractionHandler();
181 pHandler
->resetUseState();
182 pHandler
->forgetRequest();
187 bool SmartContent::implIs( const OUString
& _rURL
, Type _eType
)
189 // bind to this content
192 // did we survive this?
193 if ( isInvalid() || !isBound() )
196 DBG_ASSERT( m_pContent
, "SmartContent::implIs: inconsistence!" );
197 // if, after an bindTo, we don't have a content, then we should be INVALID, or at least
198 // NOT_BOUND (the latter happens, for example, if somebody tries to ask for an empty URL)
203 if ( Folder
== _eType
)
204 bIs
= m_pContent
->isFolder();
206 bIs
= m_pContent
->isDocument();
208 // from here on, we definitely know that the content is valid
211 catch( const Exception
& )
213 // now we're definitely invalid
220 void SmartContent::getTitle( OUString
& /* [out] */ _rTitle
)
222 if ( !isBound() || isInvalid() )
228 m_pContent
->getPropertyValue("Title") >>= sTitle
;
231 // from here on, we definitely know that the content is valid
234 catch( const ::com::sun::star::uno::Exception
& )
236 // now we're definitely invalid
242 bool SmartContent::hasParentFolder( )
244 if ( !isBound() || isInvalid() )
250 Reference
< XChild
> xChild( m_pContent
->get(), UNO_QUERY
);
253 Reference
< XContent
> xParent( xChild
->getParent(), UNO_QUERY
);
256 const OUString
aParentURL( xParent
->getIdentifier()->getContentIdentifier() );
257 bRet
= ( !aParentURL
.isEmpty() && aParentURL
!= m_pContent
->getURL() );
259 // now we're definitely valid
264 catch( const Exception
& )
266 // now we're definitely invalid
273 bool SmartContent::canCreateFolder( )
275 if ( !isBound() || isInvalid() )
281 Sequence
< ContentInfo
> aInfo
= m_pContent
->queryCreatableContentsInfo();
282 const ContentInfo
* pInfo
= aInfo
.getConstArray();
283 sal_Int32 nCount
= aInfo
.getLength();
284 for ( sal_Int32 i
= 0; i
< nCount
; ++i
, ++pInfo
)
286 // Simply look for the first KIND_FOLDER...
287 if ( pInfo
->Attributes
& ContentInfoAttribute::KIND_FOLDER
)
294 // now we're definitely valid
297 catch( const Exception
& )
299 // now we're definitely invalid
305 OUString
SmartContent::createFolder( const OUString
& _rTitle
)
307 OUString aCreatedUrl
;
310 OUString sFolderType
;
312 Sequence
< ContentInfo
> aInfo
= m_pContent
->queryCreatableContentsInfo();
313 const ContentInfo
* pInfo
= aInfo
.getConstArray();
314 sal_Int32 nCount
= aInfo
.getLength();
315 for ( sal_Int32 i
= 0; i
< nCount
; ++i
, ++pInfo
)
317 // Simply look for the first KIND_FOLDER...
318 if ( pInfo
->Attributes
& ContentInfoAttribute::KIND_FOLDER
)
320 sFolderType
= pInfo
->Type
;
325 if ( !sFolderType
.isEmpty() )
327 ucbhelper::Content aCreated
;
328 Sequence
< OUString
> aNames( 1 );
329 OUString
* pNames
= aNames
.getArray();
331 Sequence
< Any
> aValues( 1 );
332 Any
* pValues
= aValues
.getArray();
333 pValues
[0] = makeAny( _rTitle
);
334 m_pContent
->insertNewContent( sFolderType
, aNames
, aValues
, aCreated
);
336 aCreatedUrl
= aCreated
.getURL();
339 catch( const Exception
& )
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */