bump product version to 5.0.4.1
[LibreOffice.git] / xmlhelp / source / cxxhelp / provider / content.cxx
blob2e180d8314a79e89f722d6eb54484cdaf151a9be
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 /**************************************************************************
21 TODO
22 **************************************************************************
24 *************************************************************************/
25 #include <com/sun/star/beans/PropertyAttribute.hpp>
26 #include <com/sun/star/beans/XPropertyAccess.hpp>
27 #include <com/sun/star/sdbc/XRow.hpp>
28 #include <com/sun/star/ucb/OpenCommandArgument2.hpp>
29 #include <com/sun/star/ucb/OpenMode.hpp>
30 #include <com/sun/star/ucb/XCommandInfo.hpp>
31 #include <com/sun/star/io/XActiveDataSink.hpp>
32 #include <com/sun/star/io/XOutputStream.hpp>
33 #include <com/sun/star/lang/IllegalAccessException.hpp>
34 #include <com/sun/star/ucb/UnsupportedDataSinkException.hpp>
35 #include <com/sun/star/io/XActiveDataStreamer.hpp>
36 #include <com/sun/star/ucb/XPersistentPropertySet.hpp>
37 #include <osl/diagnose.h>
38 #include <comphelper/processfactory.hxx>
39 #include <ucbhelper/contentidentifier.hxx>
40 #include <ucbhelper/propertyvalueset.hxx>
41 #include <ucbhelper/cancelcommandexecution.hxx>
42 #include "content.hxx"
43 #include "provider.hxx"
44 #include "resultset.hxx"
45 #include "databases.hxx"
46 #include "resultsetfactory.hxx"
47 #include "resultsetbase.hxx"
48 #include "resultsetforroot.hxx"
49 #include "resultsetforquery.hxx"
51 using namespace com::sun::star;
52 using namespace chelp;
54 // Content Implementation.
56 Content::Content( const uno::Reference< uno::XComponentContext >& rxContext,
57 ::ucbhelper::ContentProviderImplHelper* pProvider,
58 const uno::Reference< ucb::XContentIdentifier >&
59 Identifier,
60 Databases* pDatabases )
61 : ContentImplHelper( rxContext, pProvider, Identifier ),
62 m_aURLParameter( Identifier->getContentIdentifier(),pDatabases ),
63 m_pDatabases( pDatabases ) // not owner
67 // virtual
68 Content::~Content()
72 // XInterface methods.
74 // virtual
75 void SAL_CALL Content::acquire()
76 throw( )
78 ContentImplHelper::acquire();
81 // virtual
82 void SAL_CALL Content::release()
83 throw( )
85 ContentImplHelper::release();
88 // virtual
89 uno::Any SAL_CALL Content::queryInterface( const uno::Type & rType )
90 throw ( uno::RuntimeException, std::exception )
92 uno::Any aRet;
93 return aRet.hasValue() ? aRet : ContentImplHelper::queryInterface( rType );
96 // XTypeProvider methods.
98 XTYPEPROVIDER_COMMON_IMPL( Content );
100 // virtual
101 uno::Sequence< uno::Type > SAL_CALL Content::getTypes()
102 throw( uno::RuntimeException, std::exception )
104 static cppu::OTypeCollection* pCollection = NULL;
106 if ( !pCollection )
108 osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() );
109 if ( !pCollection )
111 static cppu::OTypeCollection aCollection(
112 CPPU_TYPE_REF( lang::XTypeProvider ),
113 CPPU_TYPE_REF( lang::XServiceInfo ),
114 CPPU_TYPE_REF( lang::XComponent ),
115 CPPU_TYPE_REF( ucb::XContent ),
116 CPPU_TYPE_REF( ucb::XCommandProcessor ),
117 CPPU_TYPE_REF( beans::XPropertiesChangeNotifier ),
118 CPPU_TYPE_REF( ucb::XCommandInfoChangeNotifier ),
119 CPPU_TYPE_REF( beans::XPropertyContainer ),
120 CPPU_TYPE_REF( beans::XPropertySetInfoChangeNotifier ),
121 CPPU_TYPE_REF( container::XChild ) );
122 pCollection = &aCollection;
126 return (*pCollection).getTypes();
129 // XServiceInfo methods.
131 // virtual
132 OUString SAL_CALL Content::getImplementationName()
133 throw( uno::RuntimeException, std::exception )
135 return OUString( "CHelpContent" );
138 // virtual
139 uno::Sequence< OUString > SAL_CALL Content::getSupportedServiceNames()
140 throw( uno::RuntimeException, std::exception )
142 uno::Sequence< OUString > aSNS( 1 );
143 aSNS.getArray()[ 0 ] = MYUCP_CONTENT_SERVICE_NAME ;
144 return aSNS;
147 // XContent methods.
149 // virtual
150 OUString SAL_CALL Content::getContentType()
151 throw( uno::RuntimeException, std::exception )
153 return OUString( MYUCP_CONTENT_TYPE );
156 // XCommandProcessor methods.
158 //virtual
159 void SAL_CALL Content::abort( sal_Int32 /*CommandId*/ )
160 throw( uno::RuntimeException, std::exception )
164 class ResultSetForRootFactory
165 : public ResultSetFactory
167 private:
169 uno::Reference< uno::XComponentContext > m_xContext;
170 uno::Reference< ucb::XContentProvider > m_xProvider;
171 sal_Int32 m_nOpenMode;
172 uno::Sequence< beans::Property > m_seq;
173 uno::Sequence< ucb::NumberedSortingInfo > m_seqSort;
174 URLParameter m_aURLParameter;
175 Databases* m_pDatabases;
177 public:
179 ResultSetForRootFactory(
180 const uno::Reference< uno::XComponentContext >& xContext,
181 const uno::Reference< ucb::XContentProvider >& xProvider,
182 sal_Int32 nOpenMode,
183 const uno::Sequence< beans::Property >& seq,
184 const uno::Sequence< ucb::NumberedSortingInfo >& seqSort,
185 const URLParameter& rURLParameter,
186 Databases* pDatabases )
187 : m_xContext( xContext ),
188 m_xProvider( xProvider ),
189 m_nOpenMode( nOpenMode ),
190 m_seq( seq ),
191 m_seqSort( seqSort ),
192 m_aURLParameter( rURLParameter ),
193 m_pDatabases( pDatabases )
197 ResultSetBase* createResultSet() SAL_OVERRIDE
199 return new ResultSetForRoot( m_xContext,
200 m_xProvider,
201 m_nOpenMode,
202 m_seq,
203 m_seqSort,
204 m_aURLParameter,
205 m_pDatabases );
209 class ResultSetForQueryFactory
210 : public ResultSetFactory
212 private:
214 uno::Reference< uno::XComponentContext > m_xContext;
215 uno::Reference< ucb::XContentProvider > m_xProvider;
216 sal_Int32 m_nOpenMode;
217 uno::Sequence< beans::Property > m_seq;
218 uno::Sequence< ucb::NumberedSortingInfo > m_seqSort;
219 URLParameter m_aURLParameter;
220 Databases* m_pDatabases;
222 public:
224 ResultSetForQueryFactory(
225 const uno::Reference< uno::XComponentContext >& rxContext,
226 const uno::Reference< ucb::XContentProvider >& xProvider,
227 sal_Int32 nOpenMode,
228 const uno::Sequence< beans::Property >& seq,
229 const uno::Sequence< ucb::NumberedSortingInfo >& seqSort,
230 const URLParameter& rURLParameter,
231 Databases* pDatabases )
232 : m_xContext( rxContext ),
233 m_xProvider( xProvider ),
234 m_nOpenMode( nOpenMode ),
235 m_seq( seq ),
236 m_seqSort( seqSort ),
237 m_aURLParameter( rURLParameter ),
238 m_pDatabases( pDatabases )
242 ResultSetBase* createResultSet() SAL_OVERRIDE
244 return new ResultSetForQuery( m_xContext,
245 m_xProvider,
246 m_nOpenMode,
247 m_seq,
248 m_seqSort,
249 m_aURLParameter,
250 m_pDatabases );
254 // virtual
255 uno::Any SAL_CALL Content::execute(
256 const ucb::Command& aCommand,
257 sal_Int32 CommandId,
258 const uno::Reference< ucb::XCommandEnvironment >& Environment )
259 throw( uno::Exception,
260 ucb::CommandAbortedException,
261 uno::RuntimeException, std::exception )
263 uno::Any aRet;
265 if ( aCommand.Name == "getPropertyValues" )
267 uno::Sequence< beans::Property > Properties;
268 if ( !( aCommand.Argument >>= Properties ) )
270 aRet <<= lang::IllegalArgumentException();
271 ucbhelper::cancelCommandExecution(aRet,Environment);
274 aRet <<= getPropertyValues( Properties );
276 else if ( aCommand.Name == "setPropertyValues" )
278 uno::Sequence<beans::PropertyValue> propertyValues;
280 if( ! ( aCommand.Argument >>= propertyValues ) ) {
281 aRet <<= lang::IllegalArgumentException();
282 ucbhelper::cancelCommandExecution(aRet,Environment);
285 uno::Sequence< uno::Any > ret(propertyValues.getLength());
286 uno::Sequence< beans::Property > props(getProperties(Environment));
287 // No properties can be set
288 for(sal_Int32 i = 0; i < ret.getLength(); ++i) {
289 ret[i] <<= beans::UnknownPropertyException();
290 for(sal_Int32 j = 0; j < props.getLength(); ++j)
291 if(props[j].Name == propertyValues[i].Name) {
292 ret[i] <<= lang::IllegalAccessException();
293 break;
297 aRet <<= ret;
299 else if ( aCommand.Name == "getPropertySetInfo" )
301 // Note: Implemented by base class.
302 aRet <<= getPropertySetInfo( Environment );
304 else if ( aCommand.Name == "getCommandInfo" )
306 // Note: Implemented by base class.
307 aRet <<= getCommandInfo( Environment );
309 else if ( aCommand.Name == "open" )
311 ucb::OpenCommandArgument2 aOpenCommand;
312 if ( !( aCommand.Argument >>= aOpenCommand ) )
314 aRet <<= lang::IllegalArgumentException();
315 ucbhelper::cancelCommandExecution(aRet,Environment);
318 uno::Reference< io::XActiveDataSink > xActiveDataSink(
319 aOpenCommand.Sink, uno::UNO_QUERY);
321 if(xActiveDataSink.is())
322 m_aURLParameter.open(aCommand,
323 CommandId,
324 Environment,
325 xActiveDataSink);
327 uno::Reference< io::XActiveDataStreamer > xActiveDataStreamer(
328 aOpenCommand.Sink, uno::UNO_QUERY);
330 if(xActiveDataStreamer.is()) {
331 aRet <<= ucb::UnsupportedDataSinkException();
332 ucbhelper::cancelCommandExecution(aRet,Environment);
335 uno::Reference< io::XOutputStream > xOutputStream(
336 aOpenCommand.Sink, uno::UNO_QUERY);
338 if(xOutputStream.is() )
339 m_aURLParameter.open(aCommand,
340 CommandId,
341 Environment,
342 xOutputStream);
344 if( m_aURLParameter.isRoot() )
346 uno::Reference< ucb::XDynamicResultSet > xSet
347 = new DynamicResultSet(
348 m_xContext,
349 this,
350 aOpenCommand,
351 Environment,
352 new ResultSetForRootFactory(
353 m_xContext,
354 m_xProvider.get(),
355 aOpenCommand.Mode,
356 aOpenCommand.Properties,
357 aOpenCommand.SortingInfo,
358 m_aURLParameter,
359 m_pDatabases));
360 aRet <<= xSet;
362 else if( m_aURLParameter.isQuery() )
364 uno::Reference< ucb::XDynamicResultSet > xSet
365 = new DynamicResultSet(
366 m_xContext,
367 this,
368 aOpenCommand,
369 Environment,
370 new ResultSetForQueryFactory(
371 m_xContext,
372 m_xProvider.get(),
373 aOpenCommand.Mode,
374 aOpenCommand.Properties,
375 aOpenCommand.SortingInfo,
376 m_aURLParameter,
377 m_pDatabases ) );
378 aRet <<= xSet;
381 else
383 // Unsupported command
384 aRet <<= ucb::UnsupportedCommandException();
385 ucbhelper::cancelCommandExecution(aRet,Environment);
388 return aRet;
391 uno::Reference< sdbc::XRow > Content::getPropertyValues(
392 const uno::Sequence< beans::Property >& rProperties )
394 osl::MutexGuard aGuard( m_aMutex );
396 rtl::Reference< ::ucbhelper::PropertyValueSet > xRow =
397 new ::ucbhelper::PropertyValueSet( m_xContext );
399 for ( sal_Int32 n = 0; n < rProperties.getLength(); ++n )
401 const beans::Property& rProp = rProperties[n];
403 if ( rProp.Name == "ContentType" )
404 xRow->appendString(
405 rProp,
406 OUString(
407 "application/vnd.sun.star.help" ) );
408 else if ( rProp.Name == "Title" )
409 xRow->appendString ( rProp,m_aURLParameter.get_title() );
410 else if ( rProp.Name == "IsReadOnly" )
411 xRow->appendBoolean( rProp,true );
412 else if ( rProp.Name == "IsDocument" )
413 xRow->appendBoolean(
414 rProp,
415 m_aURLParameter.isFile() || m_aURLParameter.isRoot() );
416 else if ( rProp.Name == "IsFolder" )
417 xRow->appendBoolean(
418 rProp,
419 ! m_aURLParameter.isFile() || m_aURLParameter.isRoot() );
420 else if ( rProp.Name == "IsErrorDocument" )
421 xRow->appendBoolean( rProp, m_aURLParameter.isErrorDocument() );
422 else if ( rProp.Name == "MediaType" )
423 if( m_aURLParameter.isPicture() )
424 xRow->appendString(
425 rProp,
426 OUString( "image/gif" ) );
427 else if( m_aURLParameter.isActive() )
428 xRow->appendString(
429 rProp,
430 OUString( "text/plain" ) );
431 else if( m_aURLParameter.isFile() )
432 xRow->appendString(
433 rProp,OUString( "text/html" ) );
434 else if( m_aURLParameter.isRoot() )
435 xRow->appendString(
436 rProp,
437 OUString( "text/css" ) );
438 else
439 xRow->appendVoid( rProp );
440 else if( m_aURLParameter.isModule() )
441 if ( rProp.Name == "KeywordList" )
443 KeywordInfo *inf =
444 m_pDatabases->getKeyword( m_aURLParameter.get_module(),
445 m_aURLParameter.get_language() );
447 uno::Any aAny;
448 if( inf )
449 aAny <<= inf->getKeywordList();
450 xRow->appendObject( rProp,aAny );
452 else if ( rProp.Name == "KeywordRef" )
454 KeywordInfo *inf =
455 m_pDatabases->getKeyword( m_aURLParameter.get_module(),
456 m_aURLParameter.get_language() );
458 uno::Any aAny;
459 if( inf )
460 aAny <<= inf->getIdList();
461 xRow->appendObject( rProp,aAny );
463 else if ( rProp.Name == "KeywordAnchorForRef" )
465 KeywordInfo *inf =
466 m_pDatabases->getKeyword( m_aURLParameter.get_module(),
467 m_aURLParameter.get_language() );
469 uno::Any aAny;
470 if( inf )
471 aAny <<= inf->getAnchorList();
472 xRow->appendObject( rProp,aAny );
474 else if ( rProp.Name == "KeywordTitleForRef" )
476 KeywordInfo *inf =
477 m_pDatabases->getKeyword( m_aURLParameter.get_module(),
478 m_aURLParameter.get_language() );
480 uno::Any aAny;
481 if( inf )
482 aAny <<= inf->getTitleList();
483 xRow->appendObject( rProp,aAny );
485 else if ( rProp.Name == "SearchScopes" )
487 uno::Sequence< OUString > seq( 2 );
488 seq[0] = "Heading";
489 seq[1] = "FullText";
490 uno::Any aAny;
491 aAny <<= seq;
492 xRow->appendObject( rProp,aAny );
494 else if ( rProp.Name == "Order" )
496 StaticModuleInformation *inf =
497 m_pDatabases->getStaticInformationForModule(
498 m_aURLParameter.get_module(),
499 m_aURLParameter.get_language() );
501 uno::Any aAny;
502 if( inf )
503 aAny <<= sal_Int32( inf->get_order() );
504 xRow->appendObject( rProp,aAny );
506 else
507 xRow->appendVoid( rProp );
508 else if( "AnchorName" == rProp.Name &&
509 m_aURLParameter.isFile() )
510 xRow->appendString( rProp,m_aURLParameter.get_tag() );
511 else
512 xRow->appendVoid( rProp );
515 return uno::Reference< sdbc::XRow >( xRow.get() );
518 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */