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 <svtools/PlaceEditDialog.hxx>
11 #include <svtools/ServerDetailsControls.hxx>
13 #include <com/sun/star/uno/Sequence.hxx>
14 #include <officecfg/Office/Common.hxx>
15 #include <svtools/svtresid.hxx>
16 #include <svtools/svtools.hrc>
17 #include <vcl/msgbox.hxx>
19 using namespace com::sun::star::uno
;
21 PlaceEditDialog::PlaceEditDialog(vcl::Window
* pParent
)
22 : ModalDialog(pParent
, "PlaceEditDialog", "svt/ui/placeedit.ui")
25 , bLabelChanged( false )
26 , m_bShowPassword( true )
28 get( m_pEDServerName
, "name" );
29 get( m_pLBServerType
, "type" );
30 get( m_pEDUsername
, "login" );
32 get( m_pBTCancel
, "cancel" );
33 get( m_pBTDelete
, "delete" );
34 get( m_pBTRepoRefresh
, "repositoriesRefresh" );
35 get( m_pCBPassword
, "rememberPassword" );
36 get( m_pEDPassword
, "password" );
37 get( m_pFTPasswordLabel
, "passwordLabel" );
39 m_pBTOk
->SetClickHdl( LINK( this, PlaceEditDialog
, OKHdl
) );
40 m_pBTOk
->Enable( false );
42 m_pEDServerName
->SetModifyHdl( LINK( this, PlaceEditDialog
, EditLabelHdl
) );
44 // This constructor is called when user request a place creation, so
45 // delete button is hidden.
48 m_pLBServerType
->SetSelectHdl( LINK( this, PlaceEditDialog
, SelectTypeHdl
) );
49 m_pEDUsername
->SetModifyHdl( LINK( this, PlaceEditDialog
, EditUsernameHdl
) );
50 m_pEDPassword
->SetModifyHdl( LINK( this, PlaceEditDialog
, EditUsernameHdl
) );
55 PlaceEditDialog::PlaceEditDialog(vcl::Window
* pParent
, const std::shared_ptr
<Place
>& rPlace
)
56 : ModalDialog(pParent
, "PlaceEditDialog", "svt/ui/placeedit.ui")
57 , m_xCurrentDetails( )
58 , bLabelChanged( true )
59 , m_bShowPassword( false )
61 get( m_pEDServerName
, "name" );
62 get( m_pLBServerType
, "type" );
63 get( m_pEDUsername
, "login" );
65 get( m_pBTCancel
, "cancel" );
66 get( m_pBTDelete
, "delete" );
67 get( m_pTypeGrid
, "TypeGrid" );
68 get( m_pCBPassword
, "rememberPassword" );
69 get( m_pEDPassword
, "password" );
70 get( m_pFTPasswordLabel
, "passwordLabel" );
72 m_pEDPassword
->Hide();
73 m_pFTPasswordLabel
->Hide();
74 m_pCBPassword
->Hide();
76 m_pBTOk
->SetClickHdl( LINK( this, PlaceEditDialog
, OKHdl
) );
77 m_pBTDelete
->SetClickHdl ( LINK( this, PlaceEditDialog
, DelHdl
) );
79 m_pEDServerName
->SetModifyHdl( LINK( this, PlaceEditDialog
, ModifyHdl
) );
80 m_pLBServerType
->SetSelectHdl( LINK( this, PlaceEditDialog
, SelectTypeHdl
) );
84 m_pEDServerName
->SetText(rPlace
->GetName());
86 // Fill the boxes with the URL parts
87 bool bSuccess
= false;
88 for (size_t i
= 0 ; i
< m_aDetailsContainers
.size( ) && !bSuccess
; ++i
)
90 INetURLObject
& rUrl
= rPlace
->GetUrlObject();
91 bSuccess
= m_aDetailsContainers
[i
]->setUrl( rUrl
);
94 m_pLBServerType
->SelectEntryPos( i
);
95 SelectTypeHdl( m_pLBServerType
);
97 // Fill the Username field
98 if ( rUrl
.HasUserData( ) )
99 m_pEDUsername
->SetText( INetURLObject::decode( rUrl
.GetUser( ),
100 INetURLObject::DECODE_WITH_CHARSET
) );
104 // In edit mode user can't change connection type
108 PlaceEditDialog::~PlaceEditDialog()
113 void PlaceEditDialog::dispose()
115 m_pEDServerName
.clear();
116 m_pLBServerType
.clear();
117 m_pEDUsername
.clear();
121 m_pEDPassword
.clear();
122 m_pFTPasswordLabel
.clear();
123 ModalDialog::dispose();
126 OUString
PlaceEditDialog::GetServerUrl()
129 if (m_xCurrentDetails
.get())
131 INetURLObject aUrl
= m_xCurrentDetails
->getUrl();
132 OUString sUsername
= OUString( m_pEDUsername
->GetText( ) ).trim( );
133 if ( !sUsername
.isEmpty( ) )
134 aUrl
.SetUser( sUsername
);
135 if ( !aUrl
.HasError( ) )
136 sUrl
= aUrl
.GetMainURL( INetURLObject::NO_DECODE
);
142 std::shared_ptr
<Place
> PlaceEditDialog::GetPlace()
144 return std::make_shared
<Place
>(m_pEDServerName
->GetText(), GetServerUrl(), true);
147 void PlaceEditDialog::InitDetails( )
149 // Create CMIS controls for each server type
151 Reference
< XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
153 // Load the ServerType entries
154 bool bSkipGDrive
= OUString( GDRIVE_CLIENT_ID
).isEmpty() ||
155 OUString( GDRIVE_CLIENT_SECRET
).isEmpty();
156 bool bSkipAlfresco
= OUString( ALFRESCO_CLOUD_CLIENT_ID
).isEmpty() ||
157 OUString( ALFRESCO_CLOUD_CLIENT_SECRET
).isEmpty();
158 bool bSkipOneDrive
= OUString( ONEDRIVE_CLIENT_ID
).isEmpty() ||
159 OUString( ONEDRIVE_CLIENT_SECRET
).isEmpty();
161 Sequence
< OUString
> aTypesUrlsList( officecfg::Office::Common::Misc::CmisServersUrls::get( xContext
) );
162 Sequence
< OUString
> aTypesNamesList( officecfg::Office::Common::Misc::CmisServersNames::get( xContext
) );
164 unsigned int nPos
= 0;
165 for ( sal_Int32 i
= 0; i
< aTypesUrlsList
.getLength( ) && aTypesNamesList
.getLength( ); ++i
)
167 OUString sUrl
= aTypesUrlsList
[i
];
168 nPos
= m_pLBServerType
->InsertEntry( aTypesNamesList
[i
], nPos
);
170 std::shared_ptr
<DetailsContainer
> xCmisDetails(std::make_shared
<CmisDetailsContainer
>(this, sUrl
));
171 xCmisDetails
->setChangeHdl( LINK( this, PlaceEditDialog
, EditHdl
) );
172 m_aDetailsContainers
.push_back(xCmisDetails
);
174 if ( ( sUrl
== GDRIVE_BASE_URL
&& bSkipGDrive
) ||
175 ( sUrl
.startsWith( ALFRESCO_CLOUD_BASE_URL
) && bSkipAlfresco
) ||
176 ( sUrl
== ONEDRIVE_BASE_URL
&& bSkipOneDrive
) )
178 xCmisDetails
->setActive( false );
184 // Create WebDAV / FTP / SSH details control
185 std::shared_ptr
<DetailsContainer
> xDavDetails(std::make_shared
<DavDetailsContainer
>(this));
186 xDavDetails
->setChangeHdl( LINK( this, PlaceEditDialog
, EditHdl
) );
187 m_aDetailsContainers
.push_back(xDavDetails
);
189 std::shared_ptr
<DetailsContainer
> xFtpDetails(std::make_shared
<HostDetailsContainer
>(this, 21, "ftp"));
190 xFtpDetails
->setChangeHdl( LINK( this, PlaceEditDialog
, EditHdl
) );
191 m_aDetailsContainers
.push_back(xFtpDetails
);
193 std::shared_ptr
<DetailsContainer
> xSshDetails(std::make_shared
<HostDetailsContainer
>(this, 22, "ssh"));
194 xSshDetails
->setChangeHdl( LINK( this, PlaceEditDialog
, EditHdl
) );
195 m_aDetailsContainers
.push_back(xSshDetails
);
197 // Create Windows Share control
198 std::shared_ptr
<DetailsContainer
> xSmbDetails(std::make_shared
<SmbDetailsContainer
>(this));
199 xSmbDetails
->setChangeHdl( LINK( this, PlaceEditDialog
, EditHdl
) );
200 m_aDetailsContainers
.push_back(xSmbDetails
);
202 // Set default to first value
203 m_pLBServerType
->SelectEntryPos( 0 );
205 if ( m_pLBServerType
->GetSelectEntry() == "--------------------" )
206 m_pLBServerType
->SelectEntryPos( 1 );
208 SelectTypeHdl( m_pLBServerType
);
211 void PlaceEditDialog::UpdateLabel( )
215 if( !m_pEDUsername
->GetText().isEmpty( ) )
217 OUString sLabel
= SvtResId( STR_SVT_DEFAULT_SERVICE_LABEL
);
218 OUString sUser
= m_pEDUsername
->GetText();
220 int nLength
= sUser
.indexOf( '@' );
222 nLength
= sUser
.getLength();
224 sLabel
= sLabel
.replaceFirst( "$user$", sUser
.copy( 0, nLength
) );
225 sLabel
= sLabel
.replaceFirst( "$service$", m_pLBServerType
->GetSelectEntry() );
227 m_pEDServerName
->SetText( sLabel
);
228 bLabelChanged
= false;
232 m_pEDServerName
->SetText( m_pLBServerType
->GetSelectEntry( ) );
237 IMPL_LINK ( PlaceEditDialog
, OKHdl
, Button
*, )
239 if ( m_xCurrentDetails
.get() )
241 OUString sUrl
= m_xCurrentDetails
->getUrl().GetHost( INetURLObject::DECODE_WITH_CHARSET
);
242 OUString
sGDriveHost( GDRIVE_BASE_URL
);
243 OUString
sAlfrescoHost( ALFRESCO_CLOUD_BASE_URL
);
244 OUString
sOneDriveHost( ONEDRIVE_BASE_URL
);
246 if ( sUrl
.compareTo( sGDriveHost
, sGDriveHost
.getLength() ) == 0
247 || sUrl
.compareTo( sAlfrescoHost
, sAlfrescoHost
.getLength() ) == 0
248 || sUrl
.compareTo( sOneDriveHost
, sOneDriveHost
.getLength() ) == 0 )
250 m_pBTRepoRefresh
->Click();
252 sUrl
= m_xCurrentDetails
->getUrl().GetHost( INetURLObject::DECODE_WITH_CHARSET
);
253 INetURLObject
aHostUrl( sUrl
);
254 OUString sRepoId
= aHostUrl
.GetMark();
256 if ( !sRepoId
.isEmpty() )
262 // TODO: repository id missing. Auth error?
274 IMPL_LINK ( PlaceEditDialog
, DelHdl
, Button
*, )
276 // ReUsing existing symbols...
281 IMPL_LINK_NOARG_TYPED( PlaceEditDialog
, EditHdl
, DetailsContainer
*, void )
285 OUString sUrl
= GetServerUrl( );
286 OUString sName
= OUString( m_pEDServerName
->GetText() ).trim( );
287 m_pBTOk
->Enable( !sName
.isEmpty( ) && !sUrl
.isEmpty( ) );
290 IMPL_LINK_NOARG( PlaceEditDialog
, ModifyHdl
)
296 IMPL_LINK_NOARG( PlaceEditDialog
, EditLabelHdl
)
298 bLabelChanged
= true;
304 IMPL_LINK_NOARG( PlaceEditDialog
, EditUsernameHdl
)
306 for ( std::vector
< std::shared_ptr
< DetailsContainer
> >::iterator it
= m_aDetailsContainers
.begin( );
307 it
!= m_aDetailsContainers
.end( ); ++it
)
309 ( *it
)->setUsername( OUString( m_pEDUsername
->GetText() ) );
310 ( *it
)->setPassword( m_pEDPassword
->GetText() );
318 IMPL_LINK_NOARG( PlaceEditDialog
, SelectTypeHdl
)
320 if ( m_pLBServerType
->GetSelectEntry() == "--------------------" )
322 if( !m_pLBServerType
->IsTravelSelect() )
323 m_pLBServerType
->SelectEntryPos( m_nCurrentType
);
325 m_pLBServerType
->SetNoSelection();
330 if (m_xCurrentDetails
.get())
331 m_xCurrentDetails
->show(false);
333 sal_uInt16 nPos
= m_pLBServerType
->GetSelectEntryPos( );
334 m_xCurrentDetails
= m_aDetailsContainers
[nPos
];
335 m_nCurrentType
= nPos
;
337 m_xCurrentDetails
->show();
339 m_pCBPassword
->Show( m_bShowPassword
);
340 m_pEDPassword
->Show( m_bShowPassword
);
341 m_pFTPasswordLabel
->Show( m_bShowPassword
);
343 SetSizePixel(GetOptimalSize());
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */