1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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
;
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()
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
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
;
67 catch ( const libcmis::Exception
& e
)
69 SAL_INFO( "cmisucp", "Exception thrown: " << e
.what() );
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() )
93 if ( getResult( nIndex
) )
96 vector
< string
> paths
= maResults
[nIndex
]->pObject
->getPaths( );
97 if ( paths
.size( ) > 0 )
98 sObjectPath
= paths
.front( );
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
;
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
;
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
;
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
;
154 uno::Reference
< ucb::XContentIdentifier
> xId
= queryContentIdentifier( nIndex
);
159 uno::Reference
< ucb::XContent
> xContent
= mxContent
->getProvider()->queryContent( xId
);
160 maResults
[ nIndex
]->xContent
= 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.
175 if ( getData() && maResults
.size() > nIndex
)
181 sal_uInt32
DataSupplier::totalCount()
184 return maResults
.size();
187 sal_uInt32
DataSupplier::currentCount()
189 return maResults
.size();
192 sal_Bool
DataSupplier::isCountFinal()
197 uno::Reference
< sdbc::XRow
> DataSupplier::queryPropertyValues( sal_uInt32 nIndex
)
199 if ( nIndex
< maResults
.size() )
201 uno::Reference
< sdbc::XRow
> xRow
= maResults
[ nIndex
]->xRow
;
209 if ( getResult( nIndex
) )
211 uno::Reference
< ucb::XContent
> xContent( queryContent( nIndex
) );
216 uno::Reference
< ucb::XCommandProcessor
> xCmdProc(
217 xContent
, uno::UNO_QUERY_THROW
);
218 sal_Int32
nCmdId( xCmdProc
->createCommandIdentifier() );
220 aCmd
.Name
= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("getPropertyValues"));
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
;
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: */