cid#1606940 Check of thread-shared field evades lock acquisition
[LibreOffice.git] / svtools / source / dialogs / PlaceEditDialog.cxx
blob2905f502b8575aa4e598f84c1da9614240a1bbdd
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 <config_oauth2.h>
12 #include <svtools/PlaceEditDialog.hxx>
14 #include <com/sun/star/uno/Sequence.hxx>
15 #include <officecfg/Office/Common.hxx>
16 #include <svtools/svtresid.hxx>
17 #include <svtools/strings.hrc>
18 #include <svtools/place.hxx>
20 #include "ServerDetailsControls.hxx"
22 using namespace com::sun::star::uno;
24 PlaceEditDialog::PlaceEditDialog(weld::Window* pParent)
25 : GenericDialogController(pParent, u"svt/ui/placeedit.ui"_ustr, u"PlaceEditDialog"_ustr)
26 , m_xCurrentDetails()
27 , m_nCurrentType( 0 )
28 , m_bLabelChanged( false )
29 , m_bShowPassword( true )
30 , m_xEDServerName(m_xBuilder->weld_entry(u"name"_ustr))
31 , m_xLBServerType(m_xBuilder->weld_combo_box(u"type"_ustr))
32 , m_xEDUsername(m_xBuilder->weld_entry(u"login"_ustr))
33 , m_xFTUsernameLabel(m_xBuilder->weld_label(u"loginLabel"_ustr))
34 , m_xBTOk(m_xBuilder->weld_button(u"ok"_ustr))
35 , m_xBTDelete(m_xBuilder->weld_button(u"delete"_ustr))
36 , m_xBTRepoRefresh(m_xBuilder->weld_button(u"repositoriesRefresh"_ustr))
37 , m_xCBPassword(m_xBuilder->weld_check_button(u"rememberPassword"_ustr))
38 , m_xEDPassword(m_xBuilder->weld_entry(u"password"_ustr))
39 , m_xFTPasswordLabel(m_xBuilder->weld_label(u"passwordLabel"_ustr))
40 , m_xTypeGrid(m_xBuilder->weld_widget(u"TypeGrid"_ustr))
42 , m_xRepositoryBox(m_xBuilder->weld_widget(u"RepositoryDetails"_ustr))
43 , m_xFTRepository(m_xBuilder->weld_label(u"repositoryLabel"_ustr))
44 , m_xLBRepository(m_xBuilder->weld_combo_box(u"repositories"_ustr))
46 , m_xEDShare(m_xBuilder->weld_entry(u"share"_ustr))
47 , m_xFTShare(m_xBuilder->weld_label(u"shareLabel"_ustr))
49 , m_xDetailsGrid(m_xBuilder->weld_widget(u"Details"_ustr))
50 , m_xHostBox(m_xBuilder->weld_widget(u"HostDetails"_ustr))
51 , m_xEDHost(m_xBuilder->weld_entry(u"host"_ustr))
52 , m_xFTHost(m_xBuilder->weld_label(u"hostLabel"_ustr))
53 , m_xEDPort(m_xBuilder->weld_spin_button(u"port"_ustr))
54 , m_xFTPort(m_xBuilder->weld_label(u"portLabel"_ustr))
55 , m_xEDRoot(m_xBuilder->weld_entry(u"path"_ustr))
56 , m_xFTRoot(m_xBuilder->weld_label(u"pathLabel"_ustr))
58 , m_xCBDavs(m_xBuilder->weld_check_button(u"webdavs"_ustr))
60 m_xBTOk->connect_clicked( LINK( this, PlaceEditDialog, OKHdl) );
61 m_xBTOk->set_sensitive( false );
63 m_xEDServerName->connect_changed( LINK( this, PlaceEditDialog, EditLabelHdl) );
65 // This constructor is called when user request a place creation, so
66 // delete button is hidden.
67 m_xBTDelete->hide();
69 m_xLBServerType->connect_changed( LINK( this, PlaceEditDialog, SelectTypeHdl ) );
70 m_xEDUsername->connect_changed( LINK( this, PlaceEditDialog, EditUsernameHdl ) );
71 m_xEDPassword->connect_changed( LINK( this, PlaceEditDialog, EditUsernameHdl ) );
73 InitDetails( );
76 PlaceEditDialog::PlaceEditDialog(weld::Window* pParent, const std::shared_ptr<Place>& rPlace)
77 : GenericDialogController(pParent, u"svt/ui/placeedit.ui"_ustr, u"PlaceEditDialog"_ustr)
78 , m_xCurrentDetails( )
79 , m_bLabelChanged( true )
80 , m_bShowPassword( false )
81 , m_xEDServerName(m_xBuilder->weld_entry(u"name"_ustr))
82 , m_xLBServerType(m_xBuilder->weld_combo_box(u"type"_ustr))
83 , m_xEDUsername(m_xBuilder->weld_entry(u"login"_ustr))
84 , m_xFTUsernameLabel(m_xBuilder->weld_label(u"loginLabel"_ustr))
85 , m_xBTOk(m_xBuilder->weld_button(u"ok"_ustr))
86 , m_xBTDelete(m_xBuilder->weld_button(u"delete"_ustr))
87 , m_xBTRepoRefresh(m_xBuilder->weld_button(u"repositoriesRefresh"_ustr))
88 , m_xCBPassword(m_xBuilder->weld_check_button(u"rememberPassword"_ustr))
89 , m_xEDPassword(m_xBuilder->weld_entry(u"password"_ustr))
90 , m_xFTPasswordLabel(m_xBuilder->weld_label(u"passwordLabel"_ustr))
91 , m_xTypeGrid(m_xBuilder->weld_widget(u"TypeGrid"_ustr))
93 , m_xRepositoryBox(m_xBuilder->weld_widget(u"RepositoryDetails"_ustr))
94 , m_xFTRepository(m_xBuilder->weld_label(u"repositoryLabel"_ustr))
95 , m_xLBRepository(m_xBuilder->weld_combo_box(u"repositories"_ustr))
97 , m_xEDShare(m_xBuilder->weld_entry(u"share"_ustr))
98 , m_xFTShare(m_xBuilder->weld_label(u"shareLabel"_ustr))
100 , m_xDetailsGrid(m_xBuilder->weld_widget(u"Details"_ustr))
101 , m_xHostBox(m_xBuilder->weld_widget(u"HostDetails"_ustr))
102 , m_xEDHost(m_xBuilder->weld_entry(u"host"_ustr))
103 , m_xFTHost(m_xBuilder->weld_label(u"hostLabel"_ustr))
104 , m_xEDPort(m_xBuilder->weld_spin_button(u"port"_ustr))
105 , m_xFTPort(m_xBuilder->weld_label(u"portLabel"_ustr))
106 , m_xEDRoot(m_xBuilder->weld_entry(u"path"_ustr))
107 , m_xFTRoot(m_xBuilder->weld_label(u"pathLabel"_ustr))
109 , m_xCBDavs(m_xBuilder->weld_check_button(u"webdavs"_ustr))
111 m_xEDPassword->hide();
112 m_xFTPasswordLabel->hide();
113 m_xCBPassword->hide();
115 m_xBTOk->connect_clicked( LINK( this, PlaceEditDialog, OKHdl) );
116 m_xBTDelete->connect_clicked( LINK( this, PlaceEditDialog, DelHdl) );
118 m_xEDServerName->connect_changed( LINK( this, PlaceEditDialog, ModifyHdl) );
119 m_xLBServerType->connect_changed( LINK( this, PlaceEditDialog, SelectTypeHdl ) );
121 InitDetails( );
123 m_xEDServerName->set_text(rPlace->GetName());
125 // Fill the boxes with the URL parts
126 bool bSuccess = false;
127 for (size_t i = 0 ; i < m_aDetailsContainers.size( ) && !bSuccess; ++i)
129 INetURLObject& rUrl = rPlace->GetUrlObject();
130 bSuccess = m_aDetailsContainers[i]->setUrl( rUrl );
131 if ( bSuccess )
133 // Fill the Username field
134 if ( rUrl.HasUserData( ) )
136 m_xEDUsername->set_text( rUrl.GetUser(INetURLObject::DecodeMechanism::WithCharset) );
137 m_aDetailsContainers[i]->setUsername( rUrl.GetUser(INetURLObject::DecodeMechanism::WithCharset) );
140 m_xLBServerType->set_active(i);
141 SelectType(true);
145 // In edit mode user can't change connection type
146 m_xTypeGrid->hide();
149 PlaceEditDialog::~PlaceEditDialog()
153 OUString PlaceEditDialog::GetServerUrl()
155 OUString sUrl;
156 if (m_xCurrentDetails)
158 INetURLObject aUrl = m_xCurrentDetails->getUrl();
159 OUString sUsername = m_xEDUsername->get_text().trim();
160 if ( !sUsername.isEmpty( ) )
161 aUrl.SetUser( sUsername );
162 if ( !aUrl.HasError( ) )
163 sUrl = aUrl.GetMainURL( INetURLObject::DecodeMechanism::NONE );
166 return sUrl;
169 std::shared_ptr<Place> PlaceEditDialog::GetPlace()
171 return std::make_shared<Place>(m_xEDServerName->get_text(), GetServerUrl(), true);
174 void PlaceEditDialog::InitDetails( )
176 // Create CMIS controls for each server type
178 // Load the ServerType entries
179 bool bSkipGDrive = std::string_view( GDRIVE_CLIENT_ID ).empty() ||
180 std::string_view( GDRIVE_CLIENT_SECRET ).empty();
181 bool bSkipAlfresco = std::string_view( ALFRESCO_CLOUD_CLIENT_ID ).empty() ||
182 std::string_view( ALFRESCO_CLOUD_CLIENT_SECRET ).empty();
183 bool bSkipOneDrive= std::string_view( ONEDRIVE_CLIENT_ID ).empty() ||
184 std::string_view( ONEDRIVE_CLIENT_SECRET ).empty();
186 Sequence< OUString > aTypesUrlsList( officecfg::Office::Common::Misc::CmisServersUrls::get() );
187 Sequence< OUString > aTypesNamesList( officecfg::Office::Common::Misc::CmisServersNames::get() );
189 int nPos = 0;
190 auto nSize = std::min(aTypesUrlsList.getLength(), aTypesNamesList.getLength());
191 for ( sal_Int32 i = 0; i < nSize; ++i )
193 OUString sUrl = aTypesUrlsList[i].replaceFirst("<host", Concat2View("<" + SvtResId(STR_SVT_HOST))).replaceFirst("port>", Concat2View(SvtResId(STR_SVT_PORT) + ">"));
195 if ((sUrl == GDRIVE_BASE_URL && bSkipGDrive) ||
196 (sUrl.startsWith( ALFRESCO_CLOUD_BASE_URL) && bSkipAlfresco) ||
197 (sUrl == ONEDRIVE_BASE_URL && bSkipOneDrive))
199 // this service is not supported
200 continue;
203 m_xLBServerType->insert_text(nPos, aTypesNamesList[i].replaceFirst("Other CMIS", SvtResId(STR_SVT_OTHER_CMIS)));
205 std::shared_ptr<DetailsContainer> xCmisDetails(std::make_shared<CmisDetailsContainer>(this, sUrl));
206 xCmisDetails->setChangeHdl( LINK( this, PlaceEditDialog, EditHdl ) );
207 m_aDetailsContainers.push_back(xCmisDetails);
209 ++nPos;
212 // Create WebDAV / SSH details control
213 std::shared_ptr<DetailsContainer> xDavDetails(std::make_shared<DavDetailsContainer>(this));
214 xDavDetails->setChangeHdl( LINK( this, PlaceEditDialog, EditHdl ) );
215 m_aDetailsContainers.push_back(xDavDetails);
217 std::shared_ptr<DetailsContainer> xSshDetails(std::make_shared<HostDetailsContainer>(this, 22, "sftp"));
218 xSshDetails->setChangeHdl( LINK( this, PlaceEditDialog, EditHdl ) );
219 m_aDetailsContainers.push_back(xSshDetails);
221 // Remove Windows Share entry from dialog on Windows OS, where it's non-functional
222 #if defined(_WIN32)
223 // nPos is the position of first item that is pre-defined in svtools/uiconfig/ui/placeedit.ui,
224 // after other CMIS types were inserted
225 m_xLBServerType->remove(nPos + 2);
226 #else
227 // Create Windows Share control
228 std::shared_ptr<DetailsContainer> xSmbDetails(std::make_shared<SmbDetailsContainer>(this));
229 xSmbDetails->setChangeHdl( LINK( this, PlaceEditDialog, EditHdl ) );
230 m_aDetailsContainers.push_back(xSmbDetails);
231 #endif
233 // Set default to first value
234 m_xLBServerType->set_active(0);
236 if (m_xLBServerType->get_active_text() == "--------------------" )
237 m_xLBServerType->set_active(1);
239 SelectType(true);
242 IMPL_LINK( PlaceEditDialog, OKHdl, weld::Button&, /*rBtn*/, void)
244 if ( !m_xCurrentDetails )
245 return;
247 OUString sUrl = m_xCurrentDetails->getUrl().GetHost( INetURLObject::DecodeMechanism::WithCharset );
249 if ( sUrl.startsWith( GDRIVE_BASE_URL )
250 || sUrl.startsWith( ALFRESCO_CLOUD_BASE_URL )
251 || sUrl.startsWith( ONEDRIVE_BASE_URL ) )
253 m_xBTRepoRefresh->clicked();
255 sUrl = m_xCurrentDetails->getUrl().GetHost( INetURLObject::DecodeMechanism::WithCharset );
256 INetURLObject aHostUrl( sUrl );
257 OUString sRepoId = aHostUrl.GetMark();
259 if ( !sRepoId.isEmpty() )
261 m_xDialog->response(RET_OK);
263 else
265 // TODO: repository id missing. Auth error?
268 else
270 m_xDialog->response(RET_OK);
274 IMPL_LINK( PlaceEditDialog, DelHdl, weld::Button&, /*rButton*/, void)
276 // ReUsing existing symbols...
277 m_xDialog->response(RET_NO);
280 IMPL_LINK_NOARG( PlaceEditDialog, EditHdl, DetailsContainer*, void )
282 if( !m_bLabelChanged )
284 if( !m_xEDUsername->get_text().isEmpty( ) )
286 OUString sLabel = SvtResId( STR_SVT_DEFAULT_SERVICE_LABEL );
287 OUString sUser = m_xEDUsername->get_text();
289 int nLength = sUser.indexOf( '@' );
290 if( nLength < 0 )
291 nLength = sUser.getLength();
293 sLabel = sLabel.replaceFirst( "$user$", sUser.subView( 0, nLength ) );
294 sLabel = sLabel.replaceFirst( "$service$", m_xLBServerType->get_active_text() );
296 m_xEDServerName->set_text( sLabel );
297 m_bLabelChanged = false;
299 else
301 m_xEDServerName->set_text( m_xLBServerType->get_active_text( ) );
305 OUString sUrl = GetServerUrl( );
306 OUString sName = m_xEDServerName->get_text().trim();
307 m_xBTOk->set_sensitive( !sName.isEmpty( ) && !sUrl.isEmpty( ) );
310 IMPL_LINK_NOARG( PlaceEditDialog, ModifyHdl, weld::Entry&, void )
312 EditHdl(nullptr);
315 IMPL_LINK_NOARG( PlaceEditDialog, EditLabelHdl, weld::Entry&, void )
317 m_bLabelChanged = true;
318 EditHdl(nullptr);
321 IMPL_LINK_NOARG( PlaceEditDialog, EditUsernameHdl, weld::Entry&, void )
323 for ( auto& rxDetailsContainer : m_aDetailsContainers )
325 rxDetailsContainer->setUsername( m_xEDUsername->get_text() );
326 rxDetailsContainer->setPassword( m_xEDPassword->get_text() );
329 EditHdl(nullptr);
332 IMPL_LINK_NOARG( PlaceEditDialog, SelectTypeHdl, weld::ComboBox&, void )
334 SelectType(false);
337 void PlaceEditDialog::SelectType(bool bSkipSeparator)
339 if ( m_xLBServerType->get_active_text() == "--------------------" )
341 if (bSkipSeparator)
342 m_xLBServerType->set_active(m_nCurrentType);
343 else
344 m_xLBServerType->set_active(-1);
345 return;
348 if (m_xCurrentDetails)
349 m_xCurrentDetails->set_visible(false);
351 const int nPos = m_xLBServerType->get_active( );
352 m_xCurrentDetails = m_aDetailsContainers[nPos];
353 m_nCurrentType = nPos;
355 m_xCurrentDetails->set_visible(true);
357 m_xCBPassword->set_visible( m_bShowPassword && m_xCurrentDetails->enableUserCredentials() );
358 m_xEDPassword->set_visible( m_bShowPassword && m_xCurrentDetails->enableUserCredentials() );
359 m_xFTPasswordLabel->set_visible( m_bShowPassword && m_xCurrentDetails->enableUserCredentials() );
360 m_xEDUsername->set_visible( m_xCurrentDetails->enableUserCredentials() );
361 m_xFTUsernameLabel->set_visible( m_xCurrentDetails->enableUserCredentials() );
363 m_xDialog->resize_to_request();
365 EditHdl(nullptr);
368 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */