1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
11 #include <com/sun/star/task/InteractionHandler.hpp>
12 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
13 #include <com/sun/star/ucb/XContentAccess.hpp>
14 #include <com/sun/star/sdbc/XResultSet.hpp>
15 #include <com/sun/star/sdbc/XRow.hpp>
17 #include <comphelper/processfactory.hxx>
18 #include <officecfg/Office/Common.hxx>
19 #include <rtl/uri.hxx>
20 #include <ucbhelper/content.hxx>
21 #include <ucbhelper/commandenvironment.hxx>
23 #include <svtools/PlaceEditDialog.hxx>
24 #include <svtools/ServerDetailsControls.hxx>
25 #include <config_oauth2.h>
28 using namespace com::sun::star::lang
;
29 using namespace com::sun::star::sdbc
;
30 using namespace com::sun::star::task
;
31 using namespace com::sun::star::ucb
;
32 using namespace com::sun::star::uno
;
34 DetailsContainer::DetailsContainer( VclBuilderContainer
* pBuilder
, const OString
& rFrame
)
36 pBuilder
->get( m_pFrame
, rFrame
);
39 DetailsContainer::~DetailsContainer( )
43 void DetailsContainer::show( bool bShow
)
45 m_pFrame
->Show( bShow
);
48 INetURLObject
DetailsContainer::getUrl( )
50 // Don't use that class directly: make it smarter by subclassing it.
51 return INetURLObject( );
54 bool DetailsContainer::setUrl( const INetURLObject
& )
56 // That class doesn't contain any logic... it defers the dirty work
57 // to the sub classes.
61 void DetailsContainer::notifyChange( )
63 m_aChangeHdl
.Call( this );
66 IMPL_LINK( DetailsContainer
, ValueChangeHdl
, void *, EMPTYARG
)
72 HostDetailsContainer::HostDetailsContainer( VclBuilderContainer
* pBuilder
, sal_uInt16 nPort
, OUString sScheme
) :
73 DetailsContainer( pBuilder
, "HostDetails" ),
74 m_nDefaultPort( nPort
),
77 pBuilder
->get( m_pEDHost
, "host" );
78 m_pEDHost
->SetModifyHdl( LINK( this, DetailsContainer
, ValueChangeHdl
) );
80 pBuilder
->get( m_pEDPort
, "port" );
81 m_pEDPort
->SetModifyHdl( LINK( this, DetailsContainer
, ValueChangeHdl
) );
83 pBuilder
->get( m_pEDPath
, "path" );
84 m_pEDPath
->SetModifyHdl( LINK( this, DetailsContainer
, ValueChangeHdl
) );
89 void HostDetailsContainer::show( bool bShow
)
91 DetailsContainer::show( bShow
);
93 m_pEDPort
->SetValue( m_nDefaultPort
);
96 INetURLObject
HostDetailsContainer::getUrl( )
98 OUString sHost
= m_pEDHost
->GetText().trim( );
99 sal_Int64 nPort
= m_pEDPort
->GetValue();
100 OUString sPath
= m_pEDPath
->GetText().trim( );
103 if ( !sHost
.isEmpty( ) )
105 sUrl
= m_sScheme
+ "://" + sHost
;
106 if ( nPort
!= m_nDefaultPort
)
107 sUrl
+= ":" + OUString::number( nPort
);
108 if ( !sPath
.isEmpty( ) )
109 if ( sPath
.indexOf( '/' ) != 0 )
114 return INetURLObject( sUrl
);
117 bool HostDetailsContainer::setUrl( const INetURLObject
& rUrl
)
119 bool bSuccess
= verifyScheme( INetURLObject::GetScheme( rUrl
.GetProtocol( ) ) );
123 m_pEDHost
->SetText( rUrl
.GetHost( ) );
124 m_pEDPort
->SetValue( rUrl
.GetPort( ) );
125 m_pEDPath
->SetText( rUrl
.GetURLPath() );
131 bool HostDetailsContainer::verifyScheme( const OUString
& sScheme
)
133 return sScheme
.equals( m_sScheme
+ "://" );
136 DavDetailsContainer::DavDetailsContainer( VclBuilderContainer
* pBuilder
) :
137 HostDetailsContainer( pBuilder
, 80, "http" )
139 pBuilder
->get( m_pCBDavs
, "webdavs" );
140 m_pCBDavs
->SetToggleHdl( LINK( this, DavDetailsContainer
, ToggledDavsHdl
) );
145 void DavDetailsContainer::show( bool bShow
)
147 HostDetailsContainer::show( bShow
);
149 m_pCBDavs
->Show( bShow
);
152 m_pCBDavs
->Check( false );
155 bool DavDetailsContainer::verifyScheme( const OUString
& rScheme
)
158 if ( rScheme
.equals( "http://" ) )
161 m_pCBDavs
->Check( false );
163 else if ( rScheme
.equals( "https://" ) )
166 m_pCBDavs
->Check( true );
171 IMPL_LINK( DavDetailsContainer
, ToggledDavsHdl
, CheckBox
*, pCheckBox
)
173 // Change default port if needed
174 sal_Bool bCheckedDavs
= pCheckBox
->IsChecked();
175 if ( m_pEDPort
->GetValue() == 80 && bCheckedDavs
== sal_True
)
176 m_pEDPort
->SetValue( 443 );
177 else if ( m_pEDPort
->GetValue() == 443 && bCheckedDavs
== sal_False
)
178 m_pEDPort
->SetValue( 80 );
180 OUString
sScheme( "http" );
183 setScheme( sScheme
);
190 SmbDetailsContainer::SmbDetailsContainer( VclBuilderContainer
* pBuilder
) :
191 DetailsContainer( pBuilder
, "SmbDetails" )
193 pBuilder
->get( m_pEDHost
, "smbHost" );
194 m_pEDHost
->SetModifyHdl( LINK( this, DetailsContainer
, ValueChangeHdl
) );
196 pBuilder
->get( m_pEDShare
, "smbShare" );
197 m_pEDShare
->SetModifyHdl( LINK( this, DetailsContainer
, ValueChangeHdl
) );
199 pBuilder
->get( m_pEDPath
, "smbPath" );
200 m_pEDPath
->SetModifyHdl( LINK( this, DetailsContainer
, ValueChangeHdl
) );
205 INetURLObject
SmbDetailsContainer::getUrl( )
207 OUString sHost
= m_pEDHost
->GetText().trim( );
208 OUString sShare
= m_pEDShare
->GetText().trim( );
209 OUString sPath
= m_pEDPath
->GetText().trim( );
212 if ( !sHost
.isEmpty( ) )
214 sUrl
= "smb://" + sHost
+ "/";
215 if ( !sShare
.isEmpty( ) )
217 if ( !sPath
.isEmpty( ) )
218 if ( sPath
.indexOf( '/' ) != 0 )
223 return INetURLObject( sUrl
);
226 bool SmbDetailsContainer::setUrl( const INetURLObject
& rUrl
)
228 bool bSuccess
= rUrl
.GetProtocol() == INET_PROT_SMB
;
232 OUString sShare
= rUrl
.getName( 0 );
233 OUString sFullPath
= rUrl
.GetURLPath( );
235 if ( sFullPath
.getLength( ) > sShare
.getLength( ) )
237 sal_Int32 nPos
= sShare
.getLength( );
240 sPath
= sFullPath
.copy( nPos
);
243 m_pEDHost
->SetText( rUrl
.GetHost( ) );
244 m_pEDShare
->SetText( sShare
);
245 m_pEDPath
->SetText( sPath
);
251 CmisDetailsContainer::CmisDetailsContainer( VclBuilderContainer
* pBuilder
) :
252 DetailsContainer( pBuilder
, "CmisDetails" ),
255 m_aServerTypesURLs( ),
259 Reference
< XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
260 Reference
< XInteractionHandler
> xGlobalInteractionHandler(
261 InteractionHandler::createWithParent(xContext
, 0), UNO_QUERY
);
262 m_xCmdEnv
= new ucbhelper::CommandEnvironment( xGlobalInteractionHandler
, Reference
< XProgressHandler
>() );
264 pBuilder
->get( m_pLBServerType
, "serverType" );
265 m_pLBServerType
->SetSelectHdl( LINK( this, CmisDetailsContainer
, SelectServerTypeHdl
) );
267 pBuilder
->get( m_pEDBinding
, "binding" );
268 m_pEDBinding
->SetModifyHdl( LINK( this, DetailsContainer
, ValueChangeHdl
) );
270 pBuilder
->get( m_pLBRepository
, "repositories" );
271 m_pLBRepository
->SetSelectHdl( LINK( this, CmisDetailsContainer
, SelectRepoHdl
) );
273 pBuilder
->get( m_pBTRepoRefresh
, "repositoriesRefresh" );
274 m_pBTRepoRefresh
->SetClickHdl( LINK( this, CmisDetailsContainer
, RefreshReposHdl
) );
276 pBuilder
->get( m_pEDPath
, "cmisPath" );
277 m_pEDPath
->SetModifyHdl( LINK( this, DetailsContainer
, ValueChangeHdl
) );
281 // Load the ServerType entries
282 bool bSkipGDrive
= OUString( GDRIVE_CLIENT_ID
).isEmpty() ||
283 OUString( GDRIVE_CLIENT_SECRET
).isEmpty();
284 bool bSkipAlfresco
= OUString( ALFRESCO_CLOUD_CLIENT_ID
).isEmpty() ||
285 OUString( ALFRESCO_CLOUD_CLIENT_SECRET
).isEmpty();
287 Sequence
< OUString
> aTypesUrlsList( officecfg::Office::Common::Misc::CmisServersUrls::get( xContext
) );
288 Sequence
< OUString
> aTypesNamesList( officecfg::Office::Common::Misc::CmisServersNames::get( xContext
) );
289 for ( sal_Int32 i
= 0; i
< aTypesUrlsList
.getLength( ) && aTypesNamesList
.getLength( ); ++i
)
291 OUString sUrl
= aTypesUrlsList
[i
];
292 if ( !( sUrl
== GDRIVE_BASE_URL
&& bSkipGDrive
) &&
293 !( sUrl
.startsWith( ALFRESCO_CLOUD_BASE_URL
) && bSkipAlfresco
) )
295 m_pLBServerType
->InsertEntry( aTypesNamesList
[i
] );
296 m_aServerTypesURLs
.push_back( sUrl
);
301 INetURLObject
CmisDetailsContainer::getUrl( )
303 OUString sBindingUrl
= m_pEDBinding
->GetText().trim( );
304 OUString sPath
= m_pEDPath
->GetText().trim( );
307 if ( !sBindingUrl
.isEmpty( ) && !m_sRepoId
.isEmpty() )
309 OUString sEncodedBinding
= rtl::Uri::encode(
310 sBindingUrl
+ "#" + m_sRepoId
,
311 rtl_UriCharClassRelSegment
,
312 rtl_UriEncodeKeepEscapes
,
313 RTL_TEXTENCODING_UTF8
);
314 sUrl
= "vnd.libreoffice.cmis://" + sEncodedBinding
;
318 return INetURLObject( sUrl
);
321 bool CmisDetailsContainer::setUrl( const INetURLObject
& rUrl
)
323 bool bSuccess
= rUrl
.GetProtocol() == INET_PROT_CMIS
;
327 OUString sBindingUrl
;
328 OUString sRepositoryId
;
330 OUString sDecodedHost
= rUrl
.GetHost( INetURLObject::DECODE_WITH_CHARSET
);
331 INetURLObject
aHostUrl( sDecodedHost
);
332 sBindingUrl
= aHostUrl
.GetURLNoMark( );
333 sRepositoryId
= aHostUrl
.GetMark( );
335 m_pEDBinding
->SetText( sBindingUrl
);
336 m_pEDPath
->SetText( rUrl
.GetURLPath() );
341 void CmisDetailsContainer::setUsername( const OUString
& rUsername
)
343 m_sUsername
= rUsername
;
346 void CmisDetailsContainer::selectRepository( )
348 // Get the repo ID and call the Change listener
349 sal_uInt16 nPos
= m_pLBRepository
->GetSelectEntryPos( );
350 m_sRepoId
= m_aRepoIds
[nPos
];
355 IMPL_LINK( CmisDetailsContainer
, SelectServerTypeHdl
, void *, EMPTYARG
)
357 // Set a sample URL for the server
358 sal_uInt16 nId
= m_pLBServerType
->GetSelectEntryPos( );
359 m_pEDBinding
->SetText( m_aServerTypesURLs
[nId
] );
363 IMPL_LINK( CmisDetailsContainer
, RefreshReposHdl
, void *, EMPTYARG
)
365 OUString sBindingUrl
= m_pEDBinding
->GetText().trim( );
368 m_pLBRepository
->Clear( );
373 if ( !sBindingUrl
.isEmpty( ) )
375 OUString sEncodedBinding
= rtl::Uri::encode(
377 rtl_UriCharClassRelSegment
,
378 rtl_UriEncodeKeepEscapes
,
379 RTL_TEXTENCODING_UTF8
);
380 sUrl
= "vnd.libreoffice.cmis://" + sEncodedBinding
;
384 ::ucbhelper::Content
aCnt( sUrl
, m_xCmdEnv
, comphelper::getProcessComponentContext() );
385 Sequence
< OUString
> aProps( 1 );
390 Reference
< XResultSet
> xResultSet( aCnt
.createCursor( aProps
), UNO_QUERY_THROW
);
391 Reference
< XContentAccess
> xAccess( xResultSet
, UNO_QUERY_THROW
);
392 while ( xResultSet
->next() )
394 OUString sURL
= xAccess
->queryContentIdentifierString( );
395 INetURLObject
aURL( sURL
);
396 OUString sId
= aURL
.GetURLPath( INetURLObject::DECODE_WITH_CHARSET
);
398 m_aRepoIds
.push_back( sId
);
400 Reference
< XRow
> xRow( xResultSet
, UNO_QUERY
);
401 OUString sName
= xRow
->getString( 1 );
402 m_pLBRepository
->InsertEntry( sName
);
405 catch ( const Exception
& )
409 // Auto-select the first one
410 if ( m_pLBRepository
->GetEntryCount( ) > 0 )
412 m_pLBRepository
->SelectEntryPos( 0 );
419 IMPL_LINK( CmisDetailsContainer
, SelectRepoHdl
, void *, EMPTYARG
)
425 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */