GPU-Calc: remove Alloc_Host_Ptr for clmem of NAN vector
[LibreOffice.git] / svtools / source / dialogs / ServerDetailsControls.cxx
bloba270a5dfe7b3fbf1257b6e3a699e2382f1a0291a
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/.
8 */
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>
27 using namespace std;
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.
58 return false;
61 void DetailsContainer::notifyChange( )
63 m_aChangeHdl.Call( this );
66 IMPL_LINK( DetailsContainer, ValueChangeHdl, void *, EMPTYARG )
68 notifyChange( );
69 return 0;
72 HostDetailsContainer::HostDetailsContainer( VclBuilderContainer* pBuilder, sal_uInt16 nPort, OUString sScheme ) :
73 DetailsContainer( pBuilder, "HostDetails" ),
74 m_nDefaultPort( nPort ),
75 m_sScheme( sScheme )
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 ) );
86 show( false );
89 void HostDetailsContainer::show( bool bShow )
91 DetailsContainer::show( bShow );
92 if ( 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( );
102 OUString sUrl;
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 )
110 sUrl += "/";
111 sUrl += sPath;
114 return INetURLObject( sUrl );
117 bool HostDetailsContainer::setUrl( const INetURLObject& rUrl )
119 bool bSuccess = verifyScheme( INetURLObject::GetScheme( rUrl.GetProtocol( ) ) );
121 if ( bSuccess )
123 m_pEDHost->SetText( rUrl.GetHost( ) );
124 m_pEDPort->SetValue( rUrl.GetPort( ) );
125 m_pEDPath->SetText( rUrl.GetURLPath() );
128 return bSuccess;
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 ) );
142 show( false );
145 void DavDetailsContainer::show( bool bShow )
147 HostDetailsContainer::show( bShow );
149 m_pCBDavs->Show( bShow );
151 if ( bShow )
152 m_pCBDavs->Check( false );
155 bool DavDetailsContainer::verifyScheme( const OUString& rScheme )
157 bool bValid = false;
158 if ( rScheme.equals( "http://" ) )
160 bValid = true;
161 m_pCBDavs->Check( false );
163 else if ( rScheme.equals( "https://" ) )
165 bValid = true;
166 m_pCBDavs->Check( true );
168 return bValid;
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" );
181 if ( bCheckedDavs )
182 sScheme = "https";
183 setScheme( sScheme );
185 notifyChange( );
187 return 0;
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 ) );
202 show( false );
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( );
211 OUString sUrl;
212 if ( !sHost.isEmpty( ) )
214 sUrl = "smb://" + sHost + "/";
215 if ( !sShare.isEmpty( ) )
216 sUrl += sShare;
217 if ( !sPath.isEmpty( ) )
218 if ( sPath.indexOf( '/' ) != 0 )
219 sUrl += "/";
220 sUrl += sPath;
223 return INetURLObject( sUrl );
226 bool SmbDetailsContainer::setUrl( const INetURLObject& rUrl )
228 bool bSuccess = rUrl.GetProtocol() == INET_PROT_SMB;
230 if ( bSuccess )
232 OUString sShare = rUrl.getName( 0 );
233 OUString sFullPath = rUrl.GetURLPath( );
234 OUString sPath;
235 if ( sFullPath.getLength( ) > sShare.getLength( ) )
237 sal_Int32 nPos = sShare.getLength( );
238 if ( nPos != 0 )
239 ++nPos;
240 sPath = sFullPath.copy( nPos );
243 m_pEDHost->SetText( rUrl.GetHost( ) );
244 m_pEDShare->SetText( sShare );
245 m_pEDPath->SetText( sPath );
248 return bSuccess;
251 CmisDetailsContainer::CmisDetailsContainer( VclBuilderContainer* pBuilder ) :
252 DetailsContainer( pBuilder, "CmisDetails" ),
253 m_sUsername( ),
254 m_xCmdEnv( ),
255 m_aServerTypesURLs( ),
256 m_aRepoIds( ),
257 m_sRepoId( )
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 ) );
279 show( false );
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( );
306 OUString sUrl;
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;
316 sUrl += sPath;
318 return INetURLObject( sUrl );
321 bool CmisDetailsContainer::setUrl( const INetURLObject& rUrl )
323 bool bSuccess = rUrl.GetProtocol() == INET_PROT_CMIS;
325 if ( bSuccess )
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() );
338 return bSuccess;
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];
352 notifyChange( );
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] );
360 return 0;
363 IMPL_LINK( CmisDetailsContainer, RefreshReposHdl, void *, EMPTYARG )
365 OUString sBindingUrl = m_pEDBinding->GetText().trim( );
367 // Clean the listbox
368 m_pLBRepository->Clear( );
369 m_aRepoIds.clear( );
371 // Compute the URL
372 OUString sUrl;
373 if ( !sBindingUrl.isEmpty( ) )
375 OUString sEncodedBinding = rtl::Uri::encode(
376 sBindingUrl,
377 rtl_UriCharClassRelSegment,
378 rtl_UriEncodeKeepEscapes,
379 RTL_TEXTENCODING_UTF8 );
380 sUrl = "vnd.libreoffice.cmis://" + sEncodedBinding;
383 // Get the Content
384 ::ucbhelper::Content aCnt( sUrl, m_xCmdEnv, comphelper::getProcessComponentContext() );
385 Sequence< OUString > aProps( 1 );
386 aProps[0] = "Title";
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 );
397 sId = sId.copy( 1 );
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 );
413 selectRepository( );
416 return 0;
419 IMPL_LINK( CmisDetailsContainer, SelectRepoHdl, void *, EMPTYARG )
421 selectRepository( );
422 return 0;
425 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */