Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #include "fpsmartcontent.hxx"
21 : :
22 : : #include <com/sun/star/container/XChild.hpp>
23 : : #include <com/sun/star/ucb/ContentInfo.hpp>
24 : : #include <com/sun/star/ucb/ContentInfoAttribute.hpp>
25 : : #include <com/sun/star/ucb/XContent.hpp>
26 : :
27 : : #include <comphelper/processfactory.hxx>
28 : : #include <ucbhelper/commandenvironment.hxx>
29 : : #include <tools/solar.h>
30 : : #include <tools/debug.hxx>
31 : :
32 : : //........................................................................
33 : : namespace svt
34 : : {
35 : : //........................................................................
36 : :
37 : : using namespace ::com::sun::star::uno;
38 : : using namespace ::com::sun::star::task;
39 : : using namespace ::com::sun::star::ucb;
40 : : using namespace ::com::sun::star::lang;
41 : : using namespace ::com::sun::star::container;
42 : :
43 : : //====================================================================
44 : : //= SmartContent
45 : : //====================================================================
46 : : //--------------------------------------------------------------------
47 : 0 : SmartContent::SmartContent()
48 : : :m_pContent( NULL )
49 : : ,m_eState( NOT_BOUND )
50 : 0 : ,m_pOwnInteraction( NULL )
51 : : {
52 : 0 : }
53 : :
54 : : //--------------------------------------------------------------------
55 : 0 : SmartContent::SmartContent( const ::rtl::OUString& _rInitialURL )
56 : : :m_pContent( NULL )
57 : 0 : ,m_eState( NOT_BOUND )
58 : : {
59 : 0 : bindTo( _rInitialURL );
60 : 0 : }
61 : :
62 : : //--------------------------------------------------------------------
63 : 0 : SmartContent::~SmartContent()
64 : : {
65 : : /* This destructor originally contained the following blurb: "Do
66 : : not delete the content. Because the content will be used by
67 : : the cache." This is just plain silly, because it relies on
68 : : the provider caching created contents (which is done by
69 : : ucbhelper::ContentProviderImplHelper, but we do not actually
70 : : expect all providers to use that, right?) Otherwise we are
71 : : just leaking memory.
72 : :
73 : : TODO: If there is real need for caching the content, it must
74 : : be done here.
75 : : */
76 : 0 : delete m_pContent;
77 : 0 : }
78 : :
79 : : //--------------------------------------------------------------------
80 : 0 : void SmartContent::enableOwnInteractionHandler(::svt::OFilePickerInteractionHandler::EInterceptedInteractions eInterceptions)
81 : : {
82 : 0 : Reference< XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
83 : : Reference< XInteractionHandler > xGlobalInteractionHandler = Reference< XInteractionHandler >(
84 : 0 : xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.task.InteractionHandler") ) ), UNO_QUERY );
85 : :
86 : 0 : m_pOwnInteraction = new ::svt::OFilePickerInteractionHandler(xGlobalInteractionHandler);
87 : 0 : m_pOwnInteraction->enableInterceptions(eInterceptions);
88 : 0 : m_xOwnInteraction = m_pOwnInteraction;
89 : :
90 : 0 : m_xCmdEnv = new ::ucbhelper::CommandEnvironment( m_xOwnInteraction, Reference< XProgressHandler >() );
91 : 0 : }
92 : :
93 : : //--------------------------------------------------------------------
94 : 0 : void SmartContent::enableDefaultInteractionHandler()
95 : : {
96 : : // Don't free the memory here! It will be done by the next
97 : : // call automaticly - releasing of the uno reference ...
98 : 0 : m_pOwnInteraction = NULL;
99 : 0 : m_xOwnInteraction = Reference< XInteractionHandler >();
100 : :
101 : 0 : Reference< XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory();
102 : : Reference< XInteractionHandler > xGlobalInteractionHandler = Reference< XInteractionHandler >(
103 : 0 : xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.task.InteractionHandler") ) ), UNO_QUERY );
104 : 0 : m_xCmdEnv = new ucbhelper::CommandEnvironment( xGlobalInteractionHandler, Reference< XProgressHandler >() );
105 : 0 : }
106 : :
107 : : //--------------------------------------------------------------------
108 : 0 : ::svt::OFilePickerInteractionHandler* SmartContent::getOwnInteractionHandler() const
109 : : {
110 : 0 : if (!m_xOwnInteraction.is())
111 : 0 : return NULL;
112 : 0 : return m_pOwnInteraction;
113 : : }
114 : :
115 : : //--------------------------------------------------------------------
116 : 0 : SmartContent::InteractionHandlerType SmartContent::queryCurrentInteractionHandler() const
117 : : {
118 : 0 : if (m_xOwnInteraction.is())
119 : 0 : return IHT_OWN;
120 : :
121 : 0 : if (!m_xCmdEnv.is())
122 : 0 : return IHT_NONE;
123 : :
124 : 0 : return IHT_DEFAULT;
125 : : }
126 : :
127 : : //--------------------------------------------------------------------
128 : 0 : void SmartContent::disableInteractionHandler()
129 : : {
130 : : // Don't free the memory here! It will be done by the next
131 : : // call automaticly - releasing of the uno reference ...
132 : 0 : m_pOwnInteraction = NULL;
133 : 0 : m_xOwnInteraction.clear();
134 : :
135 : 0 : m_xCmdEnv.clear();
136 : 0 : }
137 : :
138 : : //--------------------------------------------------------------------
139 : 0 : void SmartContent::bindTo( const ::rtl::OUString& _rURL )
140 : : {
141 : 0 : if ( getURL() == _rURL )
142 : : // nothing to do, regardless of the state
143 : 0 : return;
144 : :
145 : 0 : DELETEZ( m_pContent );
146 : 0 : m_eState = INVALID; // default to INVALID
147 : 0 : m_sURL = _rURL;
148 : :
149 : 0 : if ( !m_sURL.isEmpty() )
150 : : {
151 : : try
152 : : {
153 : 0 : m_pContent = new ::ucbhelper::Content( _rURL, m_xCmdEnv );
154 : 0 : m_eState = UNKNOWN;
155 : : // from now on, the state is unknown -> we cannot know for sure if the content
156 : : // is really valid (some UCP's only tell this when asking for properties, not upon
157 : : // creation)
158 : : }
159 : 0 : catch( const ContentCreationException& )
160 : : {
161 : : }
162 : 0 : catch( const Exception& )
163 : : {
164 : : OSL_FAIL( "SmartContent::bindTo: unexpected exception caught!" );
165 : : }
166 : : }
167 : : else
168 : : {
169 : 0 : m_eState = NOT_BOUND;
170 : : }
171 : :
172 : :
173 : : // don't forget to reset the may internal used interaction handler ...
174 : : // But do it only for our own specialized interaction helper!
175 : 0 : ::svt::OFilePickerInteractionHandler* pHandler = getOwnInteractionHandler();
176 : 0 : if (pHandler)
177 : : {
178 : 0 : pHandler->resetUseState();
179 : 0 : pHandler->forgetRequest();
180 : : }
181 : : }
182 : :
183 : : //--------------------------------------------------------------------
184 : 0 : sal_Bool SmartContent::implIs( const ::rtl::OUString& _rURL, Type _eType )
185 : : {
186 : : // bind to this content
187 : 0 : bindTo( _rURL );
188 : :
189 : : // did we survive this?
190 : 0 : if ( isInvalid() || !isBound() )
191 : 0 : return sal_False;
192 : :
193 : : DBG_ASSERT( m_pContent, "SmartContent::implIs: inconsistence!" );
194 : : // if, after an bindTo, we don't have a content, then we should be INVALID, or at least
195 : : // NOT_BOUND (the latter happens, for example, if somebody tries to ask for an empty URL)
196 : :
197 : 0 : sal_Bool bIs = sal_False;
198 : : try
199 : : {
200 : 0 : if ( Folder == _eType )
201 : 0 : bIs = m_pContent->isFolder();
202 : : else
203 : 0 : bIs = m_pContent->isDocument();
204 : :
205 : : // from here on, we definately know that the content is valid
206 : 0 : m_eState = VALID;
207 : : }
208 : 0 : catch( const Exception& )
209 : : {
210 : : // now we're definately invalid
211 : 0 : m_eState = INVALID;
212 : : }
213 : 0 : return bIs;
214 : : }
215 : :
216 : : //--------------------------------------------------------------------
217 : 0 : void SmartContent::getTitle( ::rtl::OUString& /* [out] */ _rTitle )
218 : : {
219 : 0 : if ( !isBound() || isInvalid() )
220 : 0 : return;
221 : :
222 : : try
223 : : {
224 : 0 : ::rtl::OUString sTitle;
225 : 0 : m_pContent->getPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Title" )) ) >>= sTitle;
226 : 0 : _rTitle = sTitle;
227 : :
228 : : // from here on, we definately know that the content is valid
229 : 0 : m_eState = VALID;
230 : : }
231 : 0 : catch( const ::com::sun::star::uno::Exception& )
232 : : {
233 : : // now we're definately invalid
234 : 0 : m_eState = INVALID;
235 : : }
236 : : }
237 : :
238 : : //--------------------------------------------------------------------
239 : 0 : sal_Bool SmartContent::hasParentFolder( )
240 : : {
241 : 0 : if ( !isBound() || isInvalid() )
242 : 0 : return sal_False;
243 : :
244 : 0 : sal_Bool bRet = sal_False;
245 : : try
246 : : {
247 : 0 : Reference< XChild > xChild( m_pContent->get(), UNO_QUERY );
248 : 0 : if ( xChild.is() )
249 : : {
250 : 0 : Reference< XContent > xParent( xChild->getParent(), UNO_QUERY );
251 : 0 : if ( xParent.is() )
252 : : {
253 : 0 : const ::rtl::OUString aParentURL( xParent->getIdentifier()->getContentIdentifier() );
254 : 0 : bRet = ( !aParentURL.isEmpty() && aParentURL != m_pContent->getURL() );
255 : :
256 : : // now we're definately valid
257 : 0 : m_eState = VALID;
258 : 0 : }
259 : 0 : }
260 : : }
261 : 0 : catch( const Exception& )
262 : : {
263 : : // now we're definately invalid
264 : 0 : m_eState = INVALID;
265 : : }
266 : 0 : return bRet;
267 : : }
268 : :
269 : : //--------------------------------------------------------------------
270 : 0 : sal_Bool SmartContent::canCreateFolder( )
271 : : {
272 : 0 : if ( !isBound() || isInvalid() )
273 : 0 : return sal_False;
274 : :
275 : 0 : sal_Bool bRet = sal_False;
276 : : try
277 : : {
278 : 0 : Sequence< ContentInfo > aInfo = m_pContent->queryCreatableContentsInfo();
279 : 0 : const ContentInfo* pInfo = aInfo.getConstArray();
280 : 0 : sal_Int32 nCount = aInfo.getLength();
281 : 0 : for ( sal_Int32 i = 0; i < nCount; ++i, ++pInfo )
282 : : {
283 : : // Simply look for the first KIND_FOLDER...
284 : 0 : if ( pInfo->Attributes & ContentInfoAttribute::KIND_FOLDER )
285 : : {
286 : 0 : bRet = sal_True;
287 : 0 : break;
288 : : }
289 : : }
290 : :
291 : : // now we're definately valid
292 : 0 : m_eState = VALID;
293 : : }
294 : 0 : catch( const Exception& )
295 : : {
296 : : // now we're definately invalid
297 : 0 : m_eState = INVALID;
298 : : }
299 : 0 : return bRet;
300 : : }
301 : :
302 : 0 : rtl::OUString SmartContent::createFolder( const rtl::OUString& _rTitle )
303 : : {
304 : 0 : rtl::OUString aCreatedUrl;
305 : : try
306 : : {
307 : 0 : rtl::OUString sFolderType;
308 : :
309 : 0 : Sequence< ContentInfo > aInfo = m_pContent->queryCreatableContentsInfo();
310 : 0 : const ContentInfo* pInfo = aInfo.getConstArray();
311 : 0 : sal_Int32 nCount = aInfo.getLength();
312 : 0 : for ( sal_Int32 i = 0; i < nCount; ++i, ++pInfo )
313 : : {
314 : : // Simply look for the first KIND_FOLDER...
315 : 0 : if ( pInfo->Attributes & ContentInfoAttribute::KIND_FOLDER )
316 : : {
317 : 0 : sFolderType = pInfo->Type;
318 : 0 : break;
319 : : }
320 : : }
321 : :
322 : 0 : if ( !sFolderType.isEmpty() )
323 : : {
324 : 0 : ucbhelper::Content aCreated;
325 : 0 : Sequence< rtl::OUString > aNames( 1 );
326 : 0 : rtl::OUString* pNames = aNames.getArray();
327 : 0 : pNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Title" ) );
328 : 0 : Sequence< Any > aValues( 1 );
329 : 0 : Any* pValues = aValues.getArray();
330 : 0 : pValues[0] = makeAny( _rTitle );
331 : 0 : m_pContent->insertNewContent( sFolderType, aNames, aValues, aCreated );
332 : :
333 : 0 : aCreatedUrl = aCreated.getURL();
334 : 0 : }
335 : : }
336 : 0 : catch( const Exception& )
337 : : {
338 : : }
339 : 0 : return aCreatedUrl;
340 : : }
341 : :
342 : : //........................................................................
343 : : } // namespace svt
344 : : //........................................................................
345 : :
346 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|