Bump for 3.6-28
[LibreOffice.git] / ucb / source / ucp / cmis / cmis_datasupplier.cxx
blob29458b27ff1b4a91b6e73891845156956b46fcea
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Copyright 2012 LibreOffice contributors.
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 */
10 #include <vector>
12 #include <ucbhelper/contentidentifier.hxx>
13 #include <ucbhelper/providerhelper.hxx>
15 #include <com/sun/star/ucb/OpenMode.hpp>
17 #include "cmis_datasupplier.hxx"
18 #include "cmis_content.hxx"
19 #include "cmis_provider.hxx"
21 #define STD_TO_OUSTR( str ) rtl::OUString( str.c_str(), str.length( ), RTL_TEXTENCODING_UTF8 )
23 using namespace com::sun::star;
24 using namespace std;
26 namespace cmis
29 typedef std::vector< ResultListEntry* > ResultList;
31 DataSupplier::DataSupplier( const uno::Reference< lang::XMultiServiceFactory >& rxSMgr,
32 const uno::Reference< ::cmis::Content >& rContent, sal_Int32 nOpenMode )
33 : mxContent(rContent), m_xSMgr(rxSMgr), mnOpenMode(nOpenMode), mbCountFinal(false)
37 bool DataSupplier::getData()
39 if ( mbCountFinal )
40 return true;
42 libcmis::ObjectPtr pObject = mxContent->getObject();
43 libcmis::Folder* pFolder = dynamic_cast< libcmis::Folder* >( pObject.get( ) );
44 if ( NULL != pFolder )
46 // Get the children from pObject
47 try
49 vector< libcmis::ObjectPtr > children = pFolder->getChildren( );
51 // Loop over the results and filter them
52 for ( vector< libcmis::ObjectPtr >::iterator it = children.begin();
53 it != children.end(); ++it )
55 bool bIsFolder = ( *it )->getBaseType( ) == "cmis:folder";
56 if ( ( mnOpenMode == ucb::OpenMode::FOLDERS && bIsFolder ) ||
57 ( mnOpenMode == ucb::OpenMode::DOCUMENTS && !bIsFolder ) ||
58 ( mnOpenMode == ucb::OpenMode::ALL ) )
60 maResults.push_back( new ResultListEntry( *it ) );
63 mbCountFinal = sal_True;
65 return true;
67 catch ( const libcmis::Exception& e )
69 SAL_INFO( "cmisucp", "Exception thrown: " << e.what() );
70 return false;
74 return false;
77 DataSupplier::~DataSupplier()
81 ::rtl::OUString DataSupplier::queryContentIdentifierString( sal_uInt32 nIndex )
83 if ( nIndex < maResults.size() )
85 ::rtl::OUString aId = maResults[ nIndex ]->aId;
86 if ( aId.getLength() )
88 // Already cached.
89 return aId;
93 if ( getResult( nIndex ) )
95 string sObjectPath;
96 vector< string > paths = maResults[nIndex]->pObject->getPaths( );
97 if ( paths.size( ) > 0 )
98 sObjectPath = paths.front( );
99 else
101 // Handle the unfiled objects with their id...
102 // They manage to sneak here if we don't have the permission to get the object
103 // parents (and then the path)
104 sObjectPath += "#" + maResults[nIndex]->pObject->getId( );
107 // Get the URL from the Path
108 URL aUrl( mxContent->getIdentifier( )->getContentIdentifier( ) );
109 aUrl.setObjectPath( STD_TO_OUSTR( sObjectPath ) );
110 rtl::OUString aId = aUrl.asString( );
112 maResults[ nIndex ]->aId = aId;
113 return aId;
116 return ::rtl::OUString();
119 uno::Reference< ucb::XContentIdentifier > DataSupplier::queryContentIdentifier( sal_uInt32 nIndex )
121 if ( nIndex < maResults.size() )
123 uno::Reference< ucb::XContentIdentifier > xId = maResults[ nIndex ]->xId;
124 if ( xId.is() )
126 // Already cached.
127 return xId;
131 ::rtl::OUString aId = queryContentIdentifierString( nIndex );
132 if ( aId.getLength() )
134 uno::Reference< ucb::XContentIdentifier > xId = new ucbhelper::ContentIdentifier( aId );
135 maResults[ nIndex ]->xId = xId;
136 return xId;
139 return uno::Reference< ucb::XContentIdentifier >();
142 uno::Reference< ucb::XContent > DataSupplier::queryContent( sal_uInt32 nIndex )
144 if ( nIndex < maResults.size() )
146 uno::Reference< ucb::XContent > xContent = maResults[ nIndex ]->xContent;
147 if ( xContent.is() )
149 // Already cached.
150 return xContent;
154 uno::Reference< ucb::XContentIdentifier > xId = queryContentIdentifier( nIndex );
155 if ( xId.is() )
159 uno::Reference< ucb::XContent > xContent = mxContent->getProvider()->queryContent( xId );
160 maResults[ nIndex ]->xContent = xContent;
161 return xContent;
163 catch ( ucb::IllegalIdentifierException& )
167 return uno::Reference< ucb::XContent >();
170 sal_Bool DataSupplier::getResult( sal_uInt32 nIndex )
172 if ( maResults.size() > nIndex ) // Result already present.
173 return sal_True;
175 if ( getData() && maResults.size() > nIndex )
176 return sal_True;
178 return sal_False;
181 sal_uInt32 DataSupplier::totalCount()
183 getData();
184 return maResults.size();
187 sal_uInt32 DataSupplier::currentCount()
189 return maResults.size();
192 sal_Bool DataSupplier::isCountFinal()
194 return mbCountFinal;
197 uno::Reference< sdbc::XRow > DataSupplier::queryPropertyValues( sal_uInt32 nIndex )
199 if ( nIndex < maResults.size() )
201 uno::Reference< sdbc::XRow > xRow = maResults[ nIndex ]->xRow;
202 if ( xRow.is() )
204 // Already cached.
205 return xRow;
209 if ( getResult( nIndex ) )
211 uno::Reference< ucb::XContent > xContent( queryContent( nIndex ) );
212 if ( xContent.is() )
216 uno::Reference< ucb::XCommandProcessor > xCmdProc(
217 xContent, uno::UNO_QUERY_THROW );
218 sal_Int32 nCmdId( xCmdProc->createCommandIdentifier() );
219 ucb::Command aCmd;
220 aCmd.Name = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("getPropertyValues"));
221 aCmd.Handle = -1;
222 aCmd.Argument <<= getResultSet()->getProperties();
223 uno::Any aResult( xCmdProc->execute(
224 aCmd, nCmdId, getResultSet()->getEnvironment() ) );
225 uno::Reference< sdbc::XRow > xRow;
226 if ( aResult >>= xRow )
228 maResults[ nIndex ]->xRow = xRow;
229 return xRow;
232 catch ( uno::Exception const & )
237 return uno::Reference< sdbc::XRow >();
240 void DataSupplier::releasePropertyValues( sal_uInt32 nIndex )
242 if ( nIndex < maResults.size() )
243 maResults[ nIndex ]->xRow = uno::Reference< sdbc::XRow >();
246 void DataSupplier::close()
250 void DataSupplier::validate() throw( ucb::ResultSetException )
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */