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 "PlaceEditDialog.hxx"
24 #include "ServerDetailsControls.hxx"
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.
60 void DetailsContainer::notifyChange( )
62 m_aChangeHdl
.Call( this );
65 IMPL_LINK( DetailsContainer
, ValueChangeHdl
, void *, EMPTYARG
)
71 HostDetailsContainer::HostDetailsContainer( VclBuilderContainer
* pBuilder
, sal_uInt16 nPort
, OUString sScheme
) :
72 DetailsContainer( pBuilder
, "HostDetails" ),
73 m_nDefaultPort( nPort
),
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
) );
88 void HostDetailsContainer::show( bool bShow
)
90 DetailsContainer::show( 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( );
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 )
113 return INetURLObject( sUrl
);
116 bool HostDetailsContainer::setUrl( const INetURLObject
& rUrl
)
118 bool bSuccess
= verifyScheme( INetURLObject::GetScheme( rUrl
.GetProtocol( ) ) );
122 m_pEDHost
->SetText( rUrl
.GetHost( ) );
123 m_pEDPort
->SetValue( rUrl
.GetPort( ) );
124 m_pEDPath
->SetText( rUrl
.GetURLPath() );
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
) );
144 void DavDetailsContainer::show( bool bShow
)
146 HostDetailsContainer::show( bShow
);
148 m_pCBDavs
->Show( bShow
);
151 m_pCBDavs
->Check( false );
154 bool DavDetailsContainer::verifyScheme( const OUString
& rScheme
)
157 if ( rScheme
.equals( "http://" ) )
160 m_pCBDavs
->Check( false );
162 else if ( rScheme
.equals( "https://" ) )
165 m_pCBDavs
->Check( true );
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" );
182 setScheme( sScheme
);
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
) );
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( );
211 if ( !sHost
.isEmpty( ) )
213 sUrl
= "smb://" + sHost
+ "/";
214 if ( !sShare
.isEmpty( ) )
216 if ( !sPath
.isEmpty( ) )
217 if ( sPath
.indexOf( '/' ) != 0 )
222 return INetURLObject( sUrl
);
225 bool SmbDetailsContainer::setUrl( const INetURLObject
& rUrl
)
227 bool bSuccess
= rUrl
.GetProtocol() == INET_PROT_SMB
;
231 OUString sShare
= rUrl
.getName( 0 );
232 OUString sFullPath
= rUrl
.GetURLPath( );
234 if ( sFullPath
.getLength( ) > sShare
.getLength( ) )
236 sal_Int32 nPos
= sShare
.getLength( );
239 sPath
= sFullPath
.copy( nPos
);
242 m_pEDHost
->SetText( rUrl
.GetHost( ) );
243 m_pEDShare
->SetText( sShare
);
244 m_pEDPath
->SetText( sPath
);
250 CmisDetailsContainer::CmisDetailsContainer( VclBuilderContainer
* pBuilder
) :
251 DetailsContainer( pBuilder
, "CmisDetails" ),
254 m_aServerTypesURLs( ),
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
) );
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( );
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
;
307 return INetURLObject( sUrl
);
310 bool CmisDetailsContainer::setUrl( const INetURLObject
& rUrl
)
312 bool bSuccess
= rUrl
.GetProtocol() == INET_PROT_CMIS
;
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() );
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
];
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
] );
352 IMPL_LINK( CmisDetailsContainer
, RefreshReposHdl
, void *, EMPTYARG
)
354 OUString sBindingUrl
= OUString( m_pEDBinding
->GetText() ).trim( );
357 m_pLBRepository
->Clear( );
362 if ( !sBindingUrl
.isEmpty( ) )
364 OUString sEncodedBinding
= rtl::Uri::encode(
366 rtl_UriCharClassRelSegment
,
367 rtl_UriEncodeKeepEscapes
,
368 RTL_TEXTENCODING_UTF8
);
369 sUrl
= "vnd.libreoffice.cmis://" + sEncodedBinding
;
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
);
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 );
408 IMPL_LINK( CmisDetailsContainer
, SelectRepoHdl
, void *, EMPTYARG
)
414 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */