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>
33 //........................................................................
36 //........................................................................
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
;
44 //====================================================================
46 //====================================================================
47 //--------------------------------------------------------------------
48 SmartContent::SmartContent()
50 ,m_eState( NOT_BOUND
)
51 ,m_pOwnInteraction( NULL
)
55 //--------------------------------------------------------------------
56 SmartContent::SmartContent( const OUString
& _rInitialURL
)
58 ,m_eState( NOT_BOUND
)
60 bindTo( _rInitialURL
);
63 //--------------------------------------------------------------------
64 SmartContent::~SmartContent()
66 /* This destructor originally contained the following blurb: "Do
67 not delete the content. Because the content will be used by
68 the cache." This is just plain silly, because it relies on
69 the provider caching created contents (which is done by
70 ucbhelper::ContentProviderImplHelper, but we do not actually
71 expect all providers to use that, right?) Otherwise we are
74 TODO: If there is real need for caching the content, it must
80 //--------------------------------------------------------------------
81 void SmartContent::enableOwnInteractionHandler(::svt::OFilePickerInteractionHandler::EInterceptedInteractions eInterceptions
)
83 Reference
< XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
84 Reference
< XInteractionHandler
> xGlobalInteractionHandler(
85 InteractionHandler::createWithParent(xContext
, 0), UNO_QUERY_THROW
);
87 m_pOwnInteraction
= new ::svt::OFilePickerInteractionHandler(xGlobalInteractionHandler
);
88 m_pOwnInteraction
->enableInterceptions(eInterceptions
);
89 m_xOwnInteraction
= m_pOwnInteraction
;
91 m_xCmdEnv
= new ::ucbhelper::CommandEnvironment( m_xOwnInteraction
, Reference
< XProgressHandler
>() );
94 //--------------------------------------------------------------------
95 void SmartContent::enableDefaultInteractionHandler()
97 // Don't free the memory here! It will be done by the next
98 // call automaticly - releasing of the uno reference ...
99 m_pOwnInteraction
= NULL
;
100 m_xOwnInteraction
= Reference
< XInteractionHandler
>();
102 Reference
< XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
103 Reference
< XInteractionHandler
> xGlobalInteractionHandler(
104 InteractionHandler::createWithParent(xContext
, 0), UNO_QUERY_THROW
);
105 m_xCmdEnv
= new ucbhelper::CommandEnvironment( xGlobalInteractionHandler
, Reference
< XProgressHandler
>() );
108 //--------------------------------------------------------------------
109 ::svt::OFilePickerInteractionHandler
* SmartContent::getOwnInteractionHandler() const
111 if (!m_xOwnInteraction
.is())
113 return m_pOwnInteraction
;
116 //--------------------------------------------------------------------
117 SmartContent::InteractionHandlerType
SmartContent::queryCurrentInteractionHandler() const
119 if (m_xOwnInteraction
.is())
128 //--------------------------------------------------------------------
129 void SmartContent::disableInteractionHandler()
131 // Don't free the memory here! It will be done by the next
132 // call automaticly - releasing of the uno reference ...
133 m_pOwnInteraction
= NULL
;
134 m_xOwnInteraction
.clear();
139 //--------------------------------------------------------------------
140 void SmartContent::bindTo( const OUString
& _rURL
)
142 if ( getURL() == _rURL
)
143 // nothing to do, regardless of the state
146 DELETEZ( m_pContent
);
147 m_eState
= INVALID
; // default to INVALID
150 if ( !m_sURL
.isEmpty() )
154 m_pContent
= new ::ucbhelper::Content( _rURL
, m_xCmdEnv
, comphelper::getProcessComponentContext() );
156 // from now on, the state is unknown -> we cannot know for sure if the content
157 // is really valid (some UCP's only tell this when asking for properties, not upon
160 catch( const ContentCreationException
& )
163 catch( const Exception
& )
165 OSL_FAIL( "SmartContent::bindTo: unexpected exception caught!" );
170 m_eState
= NOT_BOUND
;
174 // don't forget to reset the may internal used interaction handler ...
175 // But do it only for our own specialized interaction helper!
176 ::svt::OFilePickerInteractionHandler
* pHandler
= getOwnInteractionHandler();
179 pHandler
->resetUseState();
180 pHandler
->forgetRequest();
184 //--------------------------------------------------------------------
185 sal_Bool
SmartContent::implIs( const OUString
& _rURL
, Type _eType
)
187 // bind to this content
190 // did we survive this?
191 if ( isInvalid() || !isBound() )
194 DBG_ASSERT( m_pContent
, "SmartContent::implIs: inconsistence!" );
195 // if, after an bindTo, we don't have a content, then we should be INVALID, or at least
196 // NOT_BOUND (the latter happens, for example, if somebody tries to ask for an empty URL)
198 sal_Bool bIs
= sal_False
;
201 if ( Folder
== _eType
)
202 bIs
= m_pContent
->isFolder();
204 bIs
= m_pContent
->isDocument();
206 // from here on, we definitely know that the content is valid
209 catch( const Exception
& )
211 // now we're definitely invalid
217 //--------------------------------------------------------------------
218 void SmartContent::getTitle( OUString
& /* [out] */ _rTitle
)
220 if ( !isBound() || isInvalid() )
226 m_pContent
->getPropertyValue( OUString( "Title" ) ) >>= sTitle
;
229 // from here on, we definitely know that the content is valid
232 catch( const ::com::sun::star::uno::Exception
& )
234 // now we're definitely invalid
239 //--------------------------------------------------------------------
240 sal_Bool
SmartContent::hasParentFolder( )
242 if ( !isBound() || isInvalid() )
245 sal_Bool bRet
= sal_False
;
248 Reference
< XChild
> xChild( m_pContent
->get(), UNO_QUERY
);
251 Reference
< XContent
> xParent( xChild
->getParent(), UNO_QUERY
);
254 const OUString
aParentURL( xParent
->getIdentifier()->getContentIdentifier() );
255 bRet
= ( !aParentURL
.isEmpty() && aParentURL
!= m_pContent
->getURL() );
257 // now we're definitely valid
262 catch( const Exception
& )
264 // now we're definitely invalid
270 //--------------------------------------------------------------------
271 sal_Bool
SmartContent::canCreateFolder( )
273 if ( !isBound() || isInvalid() )
276 sal_Bool bRet
= sal_False
;
279 Sequence
< ContentInfo
> aInfo
= m_pContent
->queryCreatableContentsInfo();
280 const ContentInfo
* pInfo
= aInfo
.getConstArray();
281 sal_Int32 nCount
= aInfo
.getLength();
282 for ( sal_Int32 i
= 0; i
< nCount
; ++i
, ++pInfo
)
284 // Simply look for the first KIND_FOLDER...
285 if ( pInfo
->Attributes
& ContentInfoAttribute::KIND_FOLDER
)
292 // now we're definitely valid
295 catch( const Exception
& )
297 // now we're definitely invalid
303 OUString
SmartContent::createFolder( const OUString
& _rTitle
)
305 OUString aCreatedUrl
;
308 OUString sFolderType
;
310 Sequence
< ContentInfo
> aInfo
= m_pContent
->queryCreatableContentsInfo();
311 const ContentInfo
* pInfo
= aInfo
.getConstArray();
312 sal_Int32 nCount
= aInfo
.getLength();
313 for ( sal_Int32 i
= 0; i
< nCount
; ++i
, ++pInfo
)
315 // Simply look for the first KIND_FOLDER...
316 if ( pInfo
->Attributes
& ContentInfoAttribute::KIND_FOLDER
)
318 sFolderType
= pInfo
->Type
;
323 if ( !sFolderType
.isEmpty() )
325 ucbhelper::Content aCreated
;
326 Sequence
< OUString
> aNames( 1 );
327 OUString
* pNames
= aNames
.getArray();
328 pNames
[0] = OUString( "Title" );
329 Sequence
< Any
> aValues( 1 );
330 Any
* pValues
= aValues
.getArray();
331 pValues
[0] = makeAny( _rTitle
);
332 m_pContent
->insertNewContent( sFolderType
, aNames
, aValues
, aCreated
);
334 aCreatedUrl
= aCreated
.getURL();
337 catch( const Exception
& )
343 //........................................................................
345 //........................................................................
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */