update emoji autocorrect entries from po-files
[LibreOffice.git] / fpicker / source / office / fpsmartcontent.cxx
blob3774e8bcdc2bce44c1e8413d252d18152c270c32
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/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>
35 namespace svt
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;
46 //= SmartContent
49 SmartContent::SmartContent()
50 :m_pContent( NULL )
51 ,m_eState( NOT_BOUND )
52 ,m_pOwnInteraction( NULL )
57 SmartContent::SmartContent( const OUString& _rInitialURL )
58 :m_pContent( NULL )
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
74 just leaking memory.
76 TODO: If there is real need for caching the content, it must
77 be done here.
79 delete m_pContent;
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())
114 return NULL;
115 return m_pOwnInteraction;
119 SmartContent::InteractionHandlerType SmartContent::queryCurrentInteractionHandler() const
121 if (m_xOwnInteraction.is())
122 return IHT_OWN;
124 if (!m_xCmdEnv.is())
125 return IHT_NONE;
127 return IHT_DEFAULT;
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();
138 m_xCmdEnv.clear();
142 void SmartContent::bindTo( const OUString& _rURL )
144 if ( getURL() == _rURL )
145 // nothing to do, regardless of the state
146 return;
148 DELETEZ( m_pContent );
149 m_eState = INVALID; // default to INVALID
150 m_sURL = _rURL;
152 if ( !m_sURL.isEmpty() )
156 m_pContent = new ::ucbhelper::Content( _rURL, m_xCmdEnv, comphelper::getProcessComponentContext() );
157 m_eState = UNKNOWN;
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
160 // creation)
162 catch( const ContentCreationException& )
165 catch( const Exception& )
167 OSL_FAIL( "SmartContent::bindTo: unexpected exception caught!" );
170 else
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();
179 if (pHandler)
181 pHandler->resetUseState();
182 pHandler->forgetRequest();
187 bool SmartContent::implIs( const OUString& _rURL, Type _eType )
189 // bind to this content
190 bindTo( _rURL );
192 // did we survive this?
193 if ( isInvalid() || !isBound() )
194 return false;
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)
200 bool bIs = false;
203 if ( Folder == _eType )
204 bIs = m_pContent->isFolder();
205 else
206 bIs = m_pContent->isDocument();
208 // from here on, we definitely know that the content is valid
209 m_eState = VALID;
211 catch( const Exception& )
213 // now we're definitely invalid
214 m_eState = INVALID;
216 return bIs;
220 void SmartContent::getTitle( OUString& /* [out] */ _rTitle )
222 if ( !isBound() || isInvalid() )
223 return;
227 OUString sTitle;
228 m_pContent->getPropertyValue("Title") >>= sTitle;
229 _rTitle = sTitle;
231 // from here on, we definitely know that the content is valid
232 m_eState = VALID;
234 catch( const ::com::sun::star::uno::Exception& )
236 // now we're definitely invalid
237 m_eState = INVALID;
242 bool SmartContent::hasParentFolder( )
244 if ( !isBound() || isInvalid() )
245 return false;
247 bool bRet = false;
250 Reference< XChild > xChild( m_pContent->get(), UNO_QUERY );
251 if ( xChild.is() )
253 Reference< XContent > xParent( xChild->getParent(), UNO_QUERY );
254 if ( xParent.is() )
256 const OUString aParentURL( xParent->getIdentifier()->getContentIdentifier() );
257 bRet = ( !aParentURL.isEmpty() && aParentURL != m_pContent->getURL() );
259 // now we're definitely valid
260 m_eState = VALID;
264 catch( const Exception& )
266 // now we're definitely invalid
267 m_eState = INVALID;
269 return bRet;
273 bool SmartContent::canCreateFolder( )
275 if ( !isBound() || isInvalid() )
276 return false;
278 bool bRet = false;
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 )
289 bRet = true;
290 break;
294 // now we're definitely valid
295 m_eState = VALID;
297 catch( const Exception& )
299 // now we're definitely invalid
300 m_eState = INVALID;
302 return bRet;
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;
321 break;
325 if ( !sFolderType.isEmpty() )
327 ucbhelper::Content aCreated;
328 Sequence< OUString > aNames( 1 );
329 OUString* pNames = aNames.getArray();
330 pNames[0] = "Title";
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& )
342 return aCreatedUrl;
346 } // namespace svt
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */