update dev300-m58
[ooovba.git] / fpicker / source / office / fpsmartcontent.cxx
blob32a699895f69974bcaadf7a4769722fc8e88e34a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: fpsmartcontent.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_fpicker.hxx"
33 #include "fpsmartcontent.hxx"
35 /** === begin UNO includes === **/
36 #include <com/sun/star/ucb/XContentCreator.hpp>
37 #include <com/sun/star/container/XChild.hpp>
38 #include <com/sun/star/ucb/ContentInfoAttribute.hpp>
39 /** === end UNO includes === **/
41 #ifndef _UNOTOOLS_PROCESSFACTORY_HXX
42 #include <comphelper/processfactory.hxx>
43 #endif
44 #include <ucbhelper/commandenvironment.hxx>
45 #include <tools/solar.h>
46 #include <tools/debug.hxx>
47 #include <tools/string.hxx>
49 //........................................................................
50 namespace svt
52 //........................................................................
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star::task;
56 using namespace ::com::sun::star::ucb;
57 using namespace ::com::sun::star::lang;
58 using namespace ::com::sun::star::container;
60 //====================================================================
61 //= SmartContent
62 //====================================================================
63 //--------------------------------------------------------------------
64 SmartContent::SmartContent()
65 :m_pContent( NULL )
66 ,m_eState( NOT_BOUND )
67 ,m_pOwnInteraction( NULL )
71 //--------------------------------------------------------------------
72 SmartContent::SmartContent( const ::rtl::OUString& _rInitialURL )
73 :m_pContent( NULL )
74 ,m_eState( NOT_BOUND )
76 bindTo( _rInitialURL );
79 //--------------------------------------------------------------------
80 SmartContent::~SmartContent()
82 //Do not delete the content. Because the content will be used by the cache.
83 //DELETEZ( m_pContent );
86 //--------------------------------------------------------------------
87 void SmartContent::enableOwnInteractionHandler(::svt::OFilePickerInteractionHandler::EInterceptedInteractions eInterceptions)
89 Reference< XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
90 Reference< XInteractionHandler > xGlobalInteractionHandler = Reference< XInteractionHandler >(
91 xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.task.InteractionHandler") ) ), UNO_QUERY );
93 m_pOwnInteraction = new ::svt::OFilePickerInteractionHandler(xGlobalInteractionHandler);
94 m_pOwnInteraction->enableInterceptions(eInterceptions);
95 m_xOwnInteraction = m_pOwnInteraction;
97 m_xCmdEnv = new ::ucbhelper::CommandEnvironment( m_xOwnInteraction, Reference< XProgressHandler >() );
100 //--------------------------------------------------------------------
101 void SmartContent::enableDefaultInteractionHandler()
103 // Don't free the memory here! It will be done by the next
104 // call automaticly - releasing of the uno reference ...
105 m_pOwnInteraction = NULL;
106 m_xOwnInteraction = Reference< XInteractionHandler >();
108 Reference< XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
109 Reference< XInteractionHandler > xGlobalInteractionHandler = Reference< XInteractionHandler >(
110 xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.task.InteractionHandler") ) ), UNO_QUERY );
111 m_xCmdEnv = new ucbhelper::CommandEnvironment( xGlobalInteractionHandler, Reference< XProgressHandler >() );
114 //--------------------------------------------------------------------
115 ::svt::OFilePickerInteractionHandler* SmartContent::getOwnInteractionHandler() const
117 if (!m_xOwnInteraction.is())
118 return NULL;
119 return m_pOwnInteraction;
122 //--------------------------------------------------------------------
123 SmartContent::InteractionHandlerType SmartContent::queryCurrentInteractionHandler() const
125 if (m_xOwnInteraction.is())
126 return IHT_OWN;
128 if (!m_xCmdEnv.is())
129 return IHT_NONE;
131 return IHT_DEFAULT;
134 //--------------------------------------------------------------------
135 void SmartContent::disableInteractionHandler()
137 // Don't free the memory here! It will be done by the next
138 // call automaticly - releasing of the uno reference ...
139 m_pOwnInteraction = NULL;
140 m_xOwnInteraction.clear();
142 m_xCmdEnv.clear();
145 //--------------------------------------------------------------------
146 void SmartContent::bindTo( const ::rtl::OUString& _rURL )
148 if ( getURL() == _rURL )
149 // nothing to do, regardless of the state
150 return;
152 DELETEZ( m_pContent );
153 m_eState = INVALID; // default to INVALID
154 m_sURL = _rURL;
156 if ( m_sURL.getLength() )
160 m_pContent = new ::ucbhelper::Content( _rURL, m_xCmdEnv );
161 m_eState = UNKNOWN;
162 // from now on, the state is unknown -> we cannot know for sure if the content
163 // is really valid (some UCP's only tell this when asking for properties, not upon
164 // creation)
166 catch( ContentCreationException& )
169 catch( Exception& )
171 DBG_ERROR( "SmartContent::bindTo: unexpected exception caught!" );
174 else
176 m_eState = NOT_BOUND;
180 // don't forget to reset the may internal used interaction handler ...
181 // But do it only for our own specialized interaction helper!
182 ::svt::OFilePickerInteractionHandler* pHandler = getOwnInteractionHandler();
183 if (pHandler)
185 pHandler->resetUseState();
186 pHandler->forgetRequest();
190 //--------------------------------------------------------------------
191 sal_Bool SmartContent::implIs( const ::rtl::OUString& _rURL, Type _eType )
193 // bind to this content
194 bindTo( _rURL );
196 // did we survive this?
197 if ( isInvalid() || !isBound() )
198 return sal_False;
200 DBG_ASSERT( m_pContent, "SmartContent::implIs: inconsistence!" );
201 // if, after an bindTo, we don't have a content, then we should be INVALID, or at least
202 // NOT_BOUND (the latter happens, for example, if somebody tries to ask for an empty URL)
204 sal_Bool bIs = sal_False;
207 if ( Folder == _eType )
208 bIs = m_pContent->isFolder();
209 else
210 bIs = m_pContent->isDocument();
212 // from here on, we definately know that the content is valid
213 m_eState = VALID;
215 catch( Exception& )
217 // now we're definately invalid
218 m_eState = INVALID;
220 return bIs;
223 //--------------------------------------------------------------------
224 void SmartContent::getTitle( ::rtl::OUString& /* [out] */ _rTitle )
226 if ( !isBound() || isInvalid() )
227 return;
231 ::rtl::OUString sTitle;
232 m_pContent->getPropertyValue( ::rtl::OUString::createFromAscii( "Title" ) ) >>= sTitle;
233 _rTitle = sTitle;
235 // from here on, we definately know that the content is valid
236 m_eState = VALID;
238 catch( ::com::sun::star::uno::Exception& )
240 // now we're definately invalid
241 m_eState = INVALID;
245 //--------------------------------------------------------------------
246 sal_Bool SmartContent::hasParentFolder( )
248 if ( !isBound() || isInvalid() )
249 return sal_False;
251 sal_Bool bRet = sal_False;
254 Reference< XChild > xChild( m_pContent->get(), UNO_QUERY );
255 if ( xChild.is() )
257 Reference< XContent > xParent( xChild->getParent(), UNO_QUERY );
258 if ( xParent.is() )
260 String aParentURL = String( xParent->getIdentifier()->getContentIdentifier() );
261 bRet = ( aParentURL.Len() > 0 && aParentURL != (String)(m_pContent->getURL()) );
263 // now we're definately valid
264 m_eState = VALID;
268 catch( const Exception& )
270 // now we're definately invalid
271 m_eState = INVALID;
273 return bRet;
276 //--------------------------------------------------------------------
277 sal_Bool SmartContent::canCreateFolder( )
279 if ( !isBound() || isInvalid() )
280 return sal_False;
282 sal_Bool bRet = sal_False;
285 Reference< XContentCreator > xCreator = Reference< XContentCreator >( m_pContent->get(), UNO_QUERY );
286 if ( xCreator.is() )
288 Sequence< ContentInfo > aInfo = xCreator->queryCreatableContentsInfo();
289 const ContentInfo* pInfo = aInfo.getConstArray();
290 sal_Int32 nCount = aInfo.getLength();
291 for ( sal_Int32 i = 0; i < nCount; ++i, ++pInfo )
293 // Simply look for the first KIND_FOLDER...
294 if ( pInfo->Attributes & ContentInfoAttribute::KIND_FOLDER )
296 bRet = sal_True;
297 break;
301 // now we're definately valid
302 m_eState = VALID;
305 catch( Exception& )
307 // now we're definately invalid
308 m_eState = INVALID;
310 return bRet;
313 //........................................................................
314 } // namespace svt
315 //........................................................................