| 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 |
|
| 10 |
#include <sal/config.h> |
| 11 |
|
| 12 |
#include <boost/make_shared.hpp> |
| 13 |
|
| 14 |
#include <com/sun/star/beans/PropertyAttribute.hpp> |
| 15 |
#include <com/sun/star/beans/PropertyValue.hpp> |
| 16 |
#include <com/sun/star/beans/XPropertySetInfo.hpp> |
| 17 |
#include <com/sun/star/lang/IllegalArgumentException.hpp> |
| 18 |
#include <com/sun/star/ucb/InteractiveNetworkResolveNameException.hpp> |
| 19 |
#include <com/sun/star/ucb/InteractiveNetworkConnectException.hpp> |
| 20 |
#include <com/sun/star/ucb/InteractiveNetworkReadException.hpp> |
| 21 |
#include <com/sun/star/ucb/XCommandInfo.hpp> |
| 22 |
#include <com/sun/star/ucb/XDynamicResultSet.hpp> |
| 23 |
|
| 24 |
#include <config_oauth2.h> |
| 25 |
#include <rtl/uri.hxx> |
| 26 |
#include <sal/log.hxx> |
| 27 |
#include <tools/urlobj.hxx> |
| 28 |
#include <ucbhelper/cancelcommandexecution.hxx> |
| 29 |
#include <ucbhelper/contentidentifier.hxx> |
| 30 |
#include <ucbhelper/propertyvalueset.hxx> |
| 31 |
#include <ucbhelper/proxydecider.hxx> |
| 32 |
#include <ucbhelper/macros.hxx> |
| 33 |
|
| 34 |
#include "auth_provider.hxx" |
| 35 |
#include "cmis_content.hxx" |
| 36 |
#include "cmis_provider.hxx" |
| 37 |
#include "cmis_repo_content.hxx" |
| 38 |
#include "cmis_resultset.hxx" |
| 39 |
#include <memory> |
| 40 |
|
| 41 |
#define OUSTR_TO_STDSTR(s) std::string( OUStringToOString( s, RTL_TEXTENCODING_UTF8 ) ) |
| 42 |
#define STD_TO_OUSTR( str ) OUString( str.c_str(), str.length( ), RTL_TEXTENCODING_UTF8 ) |
| 43 |
|
| 44 |
using namespace com::sun::star; |
| 45 |
|
| 46 |
namespace cmis |
| 47 |
{ |
| 48 |
RepoContent::RepoContent( const uno::Reference< uno::XComponentContext >& rxContext, |
| 49 |
ContentProvider *pProvider, const uno::Reference< ucb::XContentIdentifier >& Identifier, |
| 50 |
std::vector< libcmis::RepositoryPtr > && aRepos ) |
| 51 |
: ContentImplHelper( rxContext, pProvider, Identifier ), |
| 52 |
m_pProvider( pProvider ), |
| 53 |
m_aURL( Identifier->getContentIdentifier( ) ), |
| 54 |
m_aRepositories( std::move(aRepos) ) |
| 55 |
{ |
| 56 |
// Split the URL into bits |
| 57 |
OUString sURL = m_xIdentifier->getContentIdentifier( ); |
| 58 |
SAL_INFO( "ucb.ucp.cmis", "RepoContent::RepoContent() " << sURL ); |
| 59 |
|
| 60 |
m_sRepositoryId = m_aURL.getObjectPath(); |
| 61 |
if (!m_sRepositoryId.isEmpty() && m_sRepositoryId[0] == '/') |
| 62 |
m_sRepositoryId = m_sRepositoryId.copy(1); |
| 63 |
} |
| 64 |
|
| 65 |
RepoContent::~RepoContent() |
| 66 |
{ |
| 67 |
} |
| 68 |
|
| 69 |
uno::Any RepoContent::getBadArgExcept() |
| 70 |
{ |
| 71 |
return uno::Any( lang::IllegalArgumentException( |
| 72 |
u"Wrong argument type!"_ustr, |
| 73 |
getXWeak(), -1) ); |
| 74 |
} |
| 75 |
|
| 76 |
uno::Reference< sdbc::XRow > RepoContent::getPropertyValues( |
| 77 |
const uno::Sequence< beans::Property >& rProperties, |
| 78 |
const uno::Reference< ucb::XCommandEnvironment >& xEnv ) |
| 79 |
{ |
| 80 |
rtl::Reference< ::ucbhelper::PropertyValueSet > xRow = new ::ucbhelper::PropertyValueSet( m_xContext ); |
| 81 |
|
| 82 |
for( const beans::Property& rProp : rProperties ) |
| 83 |
{ |
| 84 |
try |
| 85 |
{ |
| 86 |
if ( rProp.Name == "IsDocument" ) |
| 87 |
{ |
| 88 |
xRow->appendBoolean( rProp, false ); |
| 89 |
} |
| 90 |
else if ( rProp.Name == "IsFolder" ) |
| 91 |
{ |
| 92 |
xRow->appendBoolean( rProp, true ); |
| 93 |
} |
| 94 |
else if ( rProp.Name == "Title" ) |
| 95 |
{ |
| 96 |
xRow->appendString( rProp, STD_TO_OUSTR( getRepository( xEnv )->getName( ) ) ); |
| 97 |
} |
| 98 |
else if ( rProp.Name == "IsReadOnly" ) |
| 99 |
{ |
| 100 |
xRow->appendBoolean( rProp, true ); |
| 101 |
} |
| 102 |
else |
| 103 |
{ |
| 104 |
xRow->appendVoid( rProp ); |
| 105 |
SAL_INFO( "ucb.ucp.cmis", "Looking for unsupported property " << rProp.Name ); |
| 106 |
} |
| 107 |
} |
| 108 |
catch (const libcmis::Exception&) |
| 109 |
{ |
| 110 |
xRow->appendVoid( rProp ); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return xRow; |
| 115 |
} |
| 116 |
|
| 117 |
void RepoContent::getRepositories( const uno::Reference< ucb::XCommandEnvironment > & xEnv ) |
| 118 |
{ |
| 119 |
// Set the proxy if needed. We are doing that all times as the proxy data shouldn't be cached. |
| 120 |
ucbhelper::InternetProxyDecider aProxyDecider( m_xContext ); |
| 121 |
INetURLObject aBindingUrl( m_aURL.getBindingUrl( ) ); |
| 122 |
const OUString sProxy = aProxyDecider.getProxy( |
| 123 |
INetURLObject::GetScheme( aBindingUrl.GetProtocol( ) ), aBindingUrl.GetHost(), aBindingUrl.GetPort() ); |
| 124 |
libcmis::SessionFactory::setProxySettings( OUSTR_TO_STDSTR( sProxy ), std::string(), std::string(), std::string() ); |
| 125 |
|
| 126 |
if ( !m_aRepositories.empty() ) |
| 127 |
return; |
| 128 |
|
| 129 |
libcmis::Session* session = createSession(xEnv, *m_pProvider, m_aURL, |
| 130 |
m_xIdentifier->getContentIdentifier(), this); |
| 131 |
assert(session); |
| 132 |
m_aRepositories = session->getRepositories(); |
| 133 |
} |
| 134 |
|
| 135 |
libcmis::RepositoryPtr RepoContent::getRepository( const uno::Reference< ucb::XCommandEnvironment > & xEnv ) |
| 136 |
{ |
| 137 |
// Ensure we have the repositories extracted |
| 138 |
getRepositories( xEnv ); |
| 139 |
|
| 140 |
libcmis::RepositoryPtr repo; |
| 141 |
|
| 142 |
if ( !m_sRepositoryId.isEmpty() ) |
| 143 |
{ |
| 144 |
auto it = std::find_if(m_aRepositories.begin(), m_aRepositories.end(), |
| 145 |
[&](const libcmis::RepositoryPtr& rRepo) { return STD_TO_OUSTR(rRepo->getId()) == m_sRepositoryId; }); |
| 146 |
if (it != m_aRepositories.end()) |
| 147 |
repo = *it; |
| 148 |
} |
| 149 |
else |
| 150 |
repo = m_aRepositories.front( ); |
| 151 |
return repo; |
| 152 |
} |
| 153 |
|
| 154 |
uno::Sequence< beans::Property > RepoContent::getProperties( |
| 155 |
const uno::Reference< ucb::XCommandEnvironment > & /*xEnv*/ ) |
| 156 |
{ |
| 157 |
static const beans::Property aGenericProperties[] = |
| 158 |
{ |
| 159 |
beans::Property( u"IsDocument"_ustr, |
| 160 |
-1, cppu::UnoType<bool>::get(), |
| 161 |
beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ), |
| 162 |
beans::Property( u"IsFolder"_ustr, |
| 163 |
-1, cppu::UnoType<bool>::get(), |
| 164 |
beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ), |
| 165 |
beans::Property( u"Title"_ustr, |
| 166 |
-1, cppu::UnoType<OUString>::get(), |
| 167 |
beans::PropertyAttribute::BOUND ), |
| 168 |
beans::Property( u"IsReadOnly"_ustr, |
| 169 |
-1, cppu::UnoType<bool>::get(), |
| 170 |
beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY ), |
| 171 |
}; |
| 172 |
|
| 173 |
const int nProps = SAL_N_ELEMENTS(aGenericProperties); |
| 174 |
return uno::Sequence< beans::Property > ( aGenericProperties, nProps ); |
| 175 |
} |
| 176 |
|
| 177 |
uno::Sequence< ucb::CommandInfo > RepoContent::getCommands( |
| 178 |
const uno::Reference< ucb::XCommandEnvironment > & /*xEnv*/ ) |
| 179 |
{ |
| 180 |
static const ucb::CommandInfo aCommandInfoTable[] = |
| 181 |
{ |
| 182 |
// Required commands |
| 183 |
ucb::CommandInfo |
| 184 |
( u"getCommandInfo"_ustr, |
| 185 |
-1, cppu::UnoType<void>::get() ), |
| 186 |
ucb::CommandInfo |
| 187 |
( u"getPropertySetInfo"_ustr, |
| 188 |
-1, cppu::UnoType<void>::get() ), |
| 189 |
ucb::CommandInfo |
| 190 |
( u"getPropertyValues"_ustr, |
| 191 |
-1, cppu::UnoType<uno::Sequence< beans::Property >>::get() ), |
| 192 |
ucb::CommandInfo |
| 193 |
( u"setPropertyValues"_ustr, |
| 194 |
-1, cppu::UnoType<uno::Sequence< beans::PropertyValue >>::get() ), |
| 195 |
|
| 196 |
// Optional standard commands |
| 197 |
ucb::CommandInfo |
| 198 |
( u"open"_ustr, |
| 199 |
-1, cppu::UnoType<ucb::OpenCommandArgument2>::get() ), |
| 200 |
}; |
| 201 |
|
| 202 |
const int nProps = SAL_N_ELEMENTS(aCommandInfoTable); |
| 203 |
return uno::Sequence< ucb::CommandInfo >(aCommandInfoTable, nProps ); |
| 204 |
} |
| 205 |
|
| 206 |
OUString RepoContent::getParentURL( ) |
| 207 |
{ |
| 208 |
SAL_INFO( "ucb.ucp.cmis", "RepoContent::getParentURL()" ); |
| 209 |
|
| 210 |
// TODO Implement me |
| 211 |
|
| 212 |
return OUString(); |
| 213 |
} |
| 214 |
|
| 215 |
XTYPEPROVIDER_COMMON_IMPL( RepoContent ); |
| 216 |
|
| 217 |
OUString SAL_CALL RepoContent::getImplementationName() |
| 218 |
{ |
| 219 |
return u"com.sun.star.comp.CmisRepoContent"_ustr; |
| 220 |
} |
| 221 |
|
| 222 |
uno::Sequence< OUString > SAL_CALL RepoContent::getSupportedServiceNames() |
| 223 |
{ |
| 224 |
return { u"com.sun.star.ucb.Content"_ustr }; |
| 225 |
} |
| 226 |
|
| 227 |
OUString SAL_CALL RepoContent::getContentType() |
| 228 |
{ |
| 229 |
return CMIS_REPO_TYPE; |
| 230 |
} |
| 231 |
|
| 232 |
uno::Any SAL_CALL RepoContent::execute( |
| 233 |
const ucb::Command& aCommand, |
| 234 |
sal_Int32 /*CommandId*/, |
| 235 |
const uno::Reference< ucb::XCommandEnvironment >& xEnv ) |
| 236 |
{ |
| 237 |
SAL_INFO( "ucb.ucp.cmis", "RepoContent::execute( ) - " << aCommand.Name ); |
| 238 |
|
| 239 |
uno::Any aRet; |
| 240 |
|
| 241 |
if ( aCommand.Name == "getPropertyValues" ) |
| 242 |
{ |
| 243 |
uno::Sequence< beans::Property > Properties; |
| 244 |
if ( !( aCommand.Argument >>= Properties ) ) |
| 245 |
ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv ); |
| 246 |
aRet <<= getPropertyValues( Properties, xEnv ); |
| 247 |
} |
| 248 |
else if ( aCommand.Name == "getPropertySetInfo" ) |
| 249 |
aRet <<= getPropertySetInfo( xEnv, false ); |
| 250 |
else if ( aCommand.Name == "getCommandInfo" ) |
| 251 |
aRet <<= getCommandInfo( xEnv, false ); |
| 252 |
else if ( aCommand.Name == "open" ) |
| 253 |
{ |
| 254 |
ucb::OpenCommandArgument2 aOpenCommand; |
| 255 |
if ( !( aCommand.Argument >>= aOpenCommand ) ) |
| 256 |
ucbhelper::cancelCommandExecution ( getBadArgExcept (), xEnv ); |
| 257 |
const ucb::OpenCommandArgument2& rOpenCommand = aOpenCommand; |
| 258 |
|
| 259 |
getRepositories( xEnv ); |
| 260 |
uno::Reference< ucb::XDynamicResultSet > xSet |
| 261 |
= new DynamicResultSet(m_xContext, this, rOpenCommand, xEnv ); |
| 262 |
aRet <<= xSet; |
| 263 |
} |
| 264 |
else |
| 265 |
{ |
| 266 |
SAL_INFO( "ucb.ucp.cmis", "Command not allowed" ); |
| 267 |
} |
| 268 |
|
| 269 |
return aRet; |
| 270 |
} |
| 271 |
|
| 272 |
void SAL_CALL RepoContent::abort( sal_Int32 /*CommandId*/ ) |
| 273 |
{ |
| 274 |
SAL_INFO( "ucb.ucp.cmis", "TODO - RepoContent::abort()" ); |
| 275 |
// TODO Implement me |
| 276 |
} |
| 277 |
|
| 278 |
uno::Sequence< uno::Type > SAL_CALL RepoContent::getTypes() |
| 279 |
{ |
| 280 |
static cppu::OTypeCollection s_aFolderCollection |
| 281 |
(CPPU_TYPE_REF( lang::XTypeProvider ), |
| 282 |
CPPU_TYPE_REF( lang::XServiceInfo ), |
| 283 |
CPPU_TYPE_REF( lang::XComponent ), |
| 284 |
CPPU_TYPE_REF( ucb::XContent ), |
| 285 |
CPPU_TYPE_REF( ucb::XCommandProcessor ), |
| 286 |
CPPU_TYPE_REF( beans::XPropertiesChangeNotifier ), |
| 287 |
CPPU_TYPE_REF( ucb::XCommandInfoChangeNotifier ), |
| 288 |
CPPU_TYPE_REF( beans::XPropertyContainer ), |
| 289 |
CPPU_TYPE_REF( beans::XPropertySetInfoChangeNotifier ), |
| 290 |
CPPU_TYPE_REF( container::XChild ) ); |
| 291 |
return s_aFolderCollection.getTypes(); |
| 292 |
} |
| 293 |
|
| 294 |
std::vector< uno::Reference< ucb::XContent > > RepoContent::getChildren( ) |
| 295 |
{ |
| 296 |
std::vector< uno::Reference< ucb::XContent > > result; |
| 297 |
|
| 298 |
// TODO Cache the results somehow |
| 299 |
SAL_INFO( "ucb.ucp.cmis", "RepoContent::getChildren" ); |
| 300 |
|
| 301 |
if ( m_sRepositoryId.isEmpty( ) ) |
| 302 |
{ |
| 303 |
for ( const auto& rRepo : m_aRepositories ) |
| 304 |
{ |
| 305 |
URL aUrl( m_aURL ); |
| 306 |
aUrl.setObjectPath( STD_TO_OUSTR( rRepo->getId( ) ) ); |
| 307 |
|
| 308 |
uno::Reference< ucb::XContentIdentifier > xId = new ucbhelper::ContentIdentifier( aUrl.asString( ) ); |
| 309 |
uno::Reference< ucb::XContent > xContent = new RepoContent( m_xContext, m_pProvider, xId, std::vector(m_aRepositories) ); |
| 310 |
|
| 311 |
result.push_back( xContent ); |
| 312 |
} |
| 313 |
} |
| 314 |
else |
| 315 |
{ |
| 316 |
// Return the repository root as child |
| 317 |
OUString sUrl; |
| 318 |
OUString sEncodedBinding = rtl::Uri::encode( |
| 319 |
m_aURL.getBindingUrl( ) + "#" + m_sRepositoryId, |
| 320 |
rtl_UriCharClassRelSegment, |
| 321 |
rtl_UriEncodeKeepEscapes, |
| 322 |
RTL_TEXTENCODING_UTF8 ); |
| 323 |
sUrl = "vnd.libreoffice.cmis://" + sEncodedBinding; |
| 324 |
|
| 325 |
uno::Reference< ucb::XContentIdentifier > xId = new ucbhelper::ContentIdentifier( sUrl ); |
| 326 |
uno::Reference< ucb::XContent > xContent = new Content( m_xContext, m_pProvider, xId ); |
| 327 |
|
| 328 |
result.push_back( xContent ); |
| 329 |
} |
| 330 |
return result; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |