fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / svtools / source / dialogs / ServerDetailsControls.cxx
blob07a2b0c268dab9486b1dad06e8db8e2efae058d2
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 "PlaceEditDialog.hxx"
24 #include "ServerDetailsControls.hxx"
26 using namespace std;
27 using namespace com::sun::star::lang;
28 using namespace com::sun::star::sdbc;
29 using namespace com::sun::star::task;
30 using namespace com::sun::star::ucb;
31 using namespace com::sun::star::uno;
33 DetailsContainer::DetailsContainer( VclBuilderContainer* pBuilder, const OString& rFrame )
35 pBuilder->get( m_pFrame, rFrame );
38 DetailsContainer::~DetailsContainer( )
42 void DetailsContainer::show( bool bShow )
44 m_pFrame->Show( bShow );
47 INetURLObject DetailsContainer::getUrl( )
49 // Don't use that class directly: make it smarter by subclassing it.
50 return INetURLObject( );
53 bool DetailsContainer::setUrl( const INetURLObject& )
55 // That class doesn't contain any logic... it defers the dirty work
56 // to the sub classes.
57 return false;
60 void DetailsContainer::notifyChange( )
62 m_aChangeHdl.Call( this );
65 IMPL_LINK( DetailsContainer, ValueChangeHdl, void *, EMPTYARG )
67 notifyChange( );
68 return 0;
71 HostDetailsContainer::HostDetailsContainer( VclBuilderContainer* pBuilder, sal_uInt16 nPort, OUString sScheme ) :
72 DetailsContainer( pBuilder, "HostDetails" ),
73 m_nDefaultPort( nPort ),
74 m_sScheme( sScheme )
76 pBuilder->get( m_pEDHost, "host" );
77 m_pEDHost->SetModifyHdl( LINK( this, DetailsContainer, ValueChangeHdl ) );
79 pBuilder->get( m_pEDPort, "port" );
80 m_pEDPort->SetModifyHdl( LINK( this, DetailsContainer, ValueChangeHdl ) );
82 pBuilder->get( m_pEDPath, "path" );
83 m_pEDPath->SetModifyHdl( LINK( this, DetailsContainer, ValueChangeHdl ) );
85 show( false );
88 void HostDetailsContainer::show( bool bShow )
90 DetailsContainer::show( bShow );
91 if ( bShow )
92 m_pEDPort->SetValue( m_nDefaultPort );
95 INetURLObject HostDetailsContainer::getUrl( )
97 OUString sHost = OUString( m_pEDHost->GetText() ).trim( );
98 sal_Int64 nPort = m_pEDPort->GetValue();
99 OUString sPath = OUString( m_pEDPath->GetText() ).trim( );
101 OUString sUrl;
102 if ( !sHost.isEmpty( ) )
104 sUrl = m_sScheme + "://" + sHost;
105 if ( nPort != m_nDefaultPort )
106 sUrl += ":" + OUString::valueOf( nPort );
107 if ( !sPath.isEmpty( ) )
108 if ( sPath.indexOf( '/' ) != 0 )
109 sUrl += "/";
110 sUrl += sPath;
113 return INetURLObject( sUrl );
116 bool HostDetailsContainer::setUrl( const INetURLObject& rUrl )
118 bool bSuccess = verifyScheme( INetURLObject::GetScheme( rUrl.GetProtocol( ) ) );
120 if ( bSuccess )
122 m_pEDHost->SetText( rUrl.GetHost( ) );
123 m_pEDPort->SetValue( rUrl.GetPort( ) );
124 m_pEDPath->SetText( rUrl.GetURLPath() );
127 return bSuccess;
130 bool HostDetailsContainer::verifyScheme( const OUString& sScheme )
132 return sScheme.equals( m_sScheme + "://" );
135 DavDetailsContainer::DavDetailsContainer( VclBuilderContainer* pBuilder ) :
136 HostDetailsContainer( pBuilder, 80, "http" )
138 pBuilder->get( m_pCBDavs, "webdavs" );
139 m_pCBDavs->SetToggleHdl( LINK( this, DavDetailsContainer, ToggledDavsHdl ) );
141 show( false );
144 void DavDetailsContainer::show( bool bShow )
146 HostDetailsContainer::show( bShow );
148 m_pCBDavs->Show( bShow );
150 if ( bShow )
151 m_pCBDavs->Check( false );
154 bool DavDetailsContainer::verifyScheme( const OUString& rScheme )
156 bool bValid = false;
157 if ( rScheme.equals( "http://" ) )
159 bValid = true;
160 m_pCBDavs->Check( false );
162 else if ( rScheme.equals( "https://" ) )
164 bValid = true;
165 m_pCBDavs->Check( true );
167 return bValid;
170 IMPL_LINK( DavDetailsContainer, ToggledDavsHdl, CheckBox*, pCheckBox )
172 // Change default port if needed
173 sal_Bool bCheckedDavs = pCheckBox->IsChecked();
174 if ( m_pEDPort->GetValue() == 80 && bCheckedDavs == sal_True)
175 m_pEDPort->SetValue( 443 );
176 else if ( m_pEDPort->GetValue() == 443 && bCheckedDavs == sal_False )
177 m_pEDPort->SetValue( 80 );
179 OUString sScheme( "http" );
180 if ( bCheckedDavs )
181 sScheme = "https";
182 setScheme( sScheme );
184 notifyChange( );
186 return 0;
189 SmbDetailsContainer::SmbDetailsContainer( VclBuilderContainer* pBuilder ) :
190 DetailsContainer( pBuilder, "SmbDetails" )
192 pBuilder->get( m_pEDHost, "smbHost" );
193 m_pEDHost->SetModifyHdl( LINK( this, DetailsContainer, ValueChangeHdl ) );
195 pBuilder->get( m_pEDShare, "smbShare" );
196 m_pEDShare->SetModifyHdl( LINK( this, DetailsContainer, ValueChangeHdl ) );
198 pBuilder->get( m_pEDPath, "smbPath" );
199 m_pEDPath->SetModifyHdl( LINK( this, DetailsContainer, ValueChangeHdl ) );
201 show( false );
204 INetURLObject SmbDetailsContainer::getUrl( )
206 OUString sHost = OUString( m_pEDHost->GetText() ).trim( );
207 OUString sShare = OUString( m_pEDShare->GetText() ).trim( );
208 OUString sPath = OUString( m_pEDPath->GetText() ).trim( );
210 OUString sUrl;
211 if ( !sHost.isEmpty( ) )
213 sUrl = "smb://" + sHost + "/";
214 if ( !sShare.isEmpty( ) )
215 sUrl += sShare;
216 if ( !sPath.isEmpty( ) )
217 if ( sPath.indexOf( '/' ) != 0 )
218 sUrl += "/";
219 sUrl += sPath;
222 return INetURLObject( sUrl );
225 bool SmbDetailsContainer::setUrl( const INetURLObject& rUrl )
227 bool bSuccess = rUrl.GetProtocol() == INET_PROT_SMB;
229 if ( bSuccess )
231 OUString sShare = rUrl.getName( 0 );
232 OUString sFullPath = rUrl.GetURLPath( );
233 OUString sPath;
234 if ( sFullPath.getLength( ) > sShare.getLength( ) )
236 sal_Int32 nPos = sShare.getLength( );
237 if ( nPos != 0 )
238 ++nPos;
239 sPath = sFullPath.copy( nPos );
242 m_pEDHost->SetText( rUrl.GetHost( ) );
243 m_pEDShare->SetText( sShare );
244 m_pEDPath->SetText( sPath );
247 return bSuccess;
250 CmisDetailsContainer::CmisDetailsContainer( VclBuilderContainer* pBuilder ) :
251 DetailsContainer( pBuilder, "CmisDetails" ),
252 m_sUsername( ),
253 m_xCmdEnv( ),
254 m_aServerTypesURLs( ),
255 m_aRepoIds( ),
256 m_sRepoId( )
258 Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
259 Reference< XInteractionHandler > xGlobalInteractionHandler(
260 InteractionHandler::createWithParent(xContext, 0), UNO_QUERY );
261 m_xCmdEnv = new ucbhelper::CommandEnvironment( xGlobalInteractionHandler, Reference< XProgressHandler >() );
263 pBuilder->get( m_pLBServerType, "serverType" );
264 m_pLBServerType->SetSelectHdl( LINK( this, CmisDetailsContainer, SelectServerTypeHdl ) );
266 pBuilder->get( m_pEDBinding, "binding" );
267 m_pEDBinding->SetModifyHdl( LINK( this, DetailsContainer, ValueChangeHdl ) );
269 pBuilder->get( m_pLBRepository, "repositories" );
270 m_pLBRepository->SetSelectHdl( LINK( this, CmisDetailsContainer, SelectRepoHdl ) );
272 pBuilder->get( m_pBTRepoRefresh, "repositoriesRefresh" );
273 m_pBTRepoRefresh->SetClickHdl( LINK( this, CmisDetailsContainer, RefreshReposHdl ) );
275 pBuilder->get( m_pEDPath, "cmisPath" );
276 m_pEDPath->SetModifyHdl( LINK( this, DetailsContainer, ValueChangeHdl ) );
278 show( false );
280 // Load the ServerType entries
281 Sequence< OUString > aTypesUrlsList( officecfg::Office::Common::Misc::CmisServersUrls::get( xContext ) );
282 Sequence< OUString > aTypesNamesList( officecfg::Office::Common::Misc::CmisServersNames::get( xContext ) );
283 for ( sal_Int32 i = 0; i < aTypesUrlsList.getLength( ) && aTypesNamesList.getLength( ); ++i )
285 m_pLBServerType->InsertEntry( aTypesNamesList[i] );
286 m_aServerTypesURLs.push_back( aTypesUrlsList[i] );
290 INetURLObject CmisDetailsContainer::getUrl( )
292 OUString sBindingUrl = OUString( m_pEDBinding->GetText() ).trim( );
293 OUString sPath = OUString( m_pEDPath->GetText() ).trim( );
295 OUString sUrl;
296 if ( !sBindingUrl.isEmpty( ) && !m_sRepoId.isEmpty() )
298 OUString sEncodedBinding = rtl::Uri::encode(
299 sBindingUrl + "#" + m_sRepoId,
300 rtl_UriCharClassRelSegment,
301 rtl_UriEncodeKeepEscapes,
302 RTL_TEXTENCODING_UTF8 );
303 sUrl = "vnd.libreoffice.cmis://" + sEncodedBinding;
305 sUrl += sPath;
307 return INetURLObject( sUrl );
310 bool CmisDetailsContainer::setUrl( const INetURLObject& rUrl )
312 bool bSuccess = rUrl.GetProtocol() == INET_PROT_CMIS;
314 if ( bSuccess )
316 OUString sBindingUrl;
317 OUString sRepositoryId;
319 OUString sDecodedHost = rUrl.GetHost( INetURLObject::DECODE_WITH_CHARSET );
320 INetURLObject aHostUrl( sDecodedHost );
321 sBindingUrl = aHostUrl.GetURLNoMark( );
322 sRepositoryId = aHostUrl.GetMark( );
324 m_pEDBinding->SetText( sBindingUrl );
325 m_pEDPath->SetText( rUrl.GetURLPath() );
327 return bSuccess;
330 void CmisDetailsContainer::setUsername( const OUString& rUsername )
332 m_sUsername = OUString( rUsername );
335 void CmisDetailsContainer::selectRepository( )
337 // Get the repo ID and call the Change listener
338 sal_uInt16 nPos = m_pLBRepository->GetSelectEntryPos( );
339 m_sRepoId = m_aRepoIds[nPos];
341 notifyChange( );
344 IMPL_LINK( CmisDetailsContainer, SelectServerTypeHdl, void *, EMPTYARG )
346 // Set a sample URL for the server
347 sal_uInt16 nId = m_pLBServerType->GetSelectEntryPos( );
348 m_pEDBinding->SetText( m_aServerTypesURLs[nId] );
349 return 0;
352 IMPL_LINK( CmisDetailsContainer, RefreshReposHdl, void *, EMPTYARG )
354 OUString sBindingUrl = OUString( m_pEDBinding->GetText() ).trim( );
356 // Clean the listbox
357 m_pLBRepository->Clear( );
358 m_aRepoIds.clear( );
360 // Compute the URL
361 OUString sUrl;
362 if ( !sBindingUrl.isEmpty( ) )
364 OUString sEncodedBinding = rtl::Uri::encode(
365 sBindingUrl,
366 rtl_UriCharClassRelSegment,
367 rtl_UriEncodeKeepEscapes,
368 RTL_TEXTENCODING_UTF8 );
369 sUrl = "vnd.libreoffice.cmis://" + sEncodedBinding;
372 // Get the Content
373 ::ucbhelper::Content aCnt( sUrl, m_xCmdEnv, comphelper::getProcessComponentContext() );
374 Sequence< OUString > aProps( 1 );
375 aProps[0] = OUString( "Title" );
379 Reference< XResultSet > xResultSet( aCnt.createCursor( aProps ), UNO_QUERY_THROW );
380 Reference< XContentAccess > xAccess( xResultSet, UNO_QUERY_THROW );
381 while ( xResultSet->next() )
383 OUString sURL = xAccess->queryContentIdentifierString( );
384 INetURLObject aURL( sURL );
385 OUString sId = aURL.GetURLPath( INetURLObject::DECODE_WITH_CHARSET );
386 sId = sId.copy( 1 );
387 m_aRepoIds.push_back( sId );
389 Reference< XRow > xRow( xResultSet, UNO_QUERY );
390 OUString sName = xRow->getString( 1 );
391 m_pLBRepository->InsertEntry( sName );
394 catch ( const Exception& )
398 // Auto-select the first one
399 if ( m_pLBRepository->GetEntryCount( ) > 0 )
401 m_pLBRepository->SelectEntryPos( 0 );
402 selectRepository( );
405 return 0;
408 IMPL_LINK( CmisDetailsContainer, SelectRepoHdl, void *, EMPTYARG )
410 selectRepository( );
411 return 0;
414 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */