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 <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
, "svt/ui/placeedit.ui", "PlaceEditDialog")
28 , m_bLabelChanged( false )
29 , m_bShowPassword( true )
30 , m_xEDServerName(m_xBuilder
->weld_entry("name"))
31 , m_xLBServerType(m_xBuilder
->weld_combo_box("type"))
32 , m_xEDUsername(m_xBuilder
->weld_entry("login"))
33 , m_xFTUsernameLabel(m_xBuilder
->weld_label("loginLabel"))
34 , m_xBTOk(m_xBuilder
->weld_button("ok"))
35 , m_xBTDelete(m_xBuilder
->weld_button("delete"))
36 , m_xBTRepoRefresh(m_xBuilder
->weld_button("repositoriesRefresh"))
37 , m_xCBPassword(m_xBuilder
->weld_check_button("rememberPassword"))
38 , m_xEDPassword(m_xBuilder
->weld_entry("password"))
39 , m_xFTPasswordLabel(m_xBuilder
->weld_label("passwordLabel"))
40 , m_xTypeGrid(m_xBuilder
->weld_widget("TypeGrid"))
42 , m_xRepositoryBox(m_xBuilder
->weld_widget("RepositoryDetails"))
43 , m_xFTRepository(m_xBuilder
->weld_label("repositoryLabel"))
44 , m_xLBRepository(m_xBuilder
->weld_combo_box("repositories"))
46 , m_xEDShare(m_xBuilder
->weld_entry("share"))
47 , m_xFTShare(m_xBuilder
->weld_label("shareLabel"))
49 , m_xDetailsGrid(m_xBuilder
->weld_widget("Details"))
50 , m_xHostBox(m_xBuilder
->weld_widget("HostDetails"))
51 , m_xEDHost(m_xBuilder
->weld_entry("host"))
52 , m_xFTHost(m_xBuilder
->weld_label("hostLabel"))
53 , m_xEDPort(m_xBuilder
->weld_spin_button("port"))
54 , m_xFTPort(m_xBuilder
->weld_label("portLabel"))
55 , m_xEDRoot(m_xBuilder
->weld_entry("path"))
56 , m_xFTRoot(m_xBuilder
->weld_label("pathLabel"))
58 , m_xCBDavs(m_xBuilder
->weld_check_button("webdavs"))
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.
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
) );
76 PlaceEditDialog::PlaceEditDialog(weld::Window
* pParent
, const std::shared_ptr
<Place
>& rPlace
)
77 : GenericDialogController(pParent
, "svt/ui/placeedit.ui", "PlaceEditDialog")
78 , m_xCurrentDetails( )
79 , m_bLabelChanged( true )
80 , m_bShowPassword( false )
81 , m_xEDServerName(m_xBuilder
->weld_entry("name"))
82 , m_xLBServerType(m_xBuilder
->weld_combo_box("type"))
83 , m_xEDUsername(m_xBuilder
->weld_entry("login"))
84 , m_xFTUsernameLabel(m_xBuilder
->weld_label("loginLabel"))
85 , m_xBTOk(m_xBuilder
->weld_button("ok"))
86 , m_xBTDelete(m_xBuilder
->weld_button("delete"))
87 , m_xBTRepoRefresh(m_xBuilder
->weld_button("repositoriesRefresh"))
88 , m_xCBPassword(m_xBuilder
->weld_check_button("rememberPassword"))
89 , m_xEDPassword(m_xBuilder
->weld_entry("password"))
90 , m_xFTPasswordLabel(m_xBuilder
->weld_label("passwordLabel"))
91 , m_xTypeGrid(m_xBuilder
->weld_widget("TypeGrid"))
93 , m_xRepositoryBox(m_xBuilder
->weld_widget("RepositoryDetails"))
94 , m_xFTRepository(m_xBuilder
->weld_label("repositoryLabel"))
95 , m_xLBRepository(m_xBuilder
->weld_combo_box("repositories"))
97 , m_xEDShare(m_xBuilder
->weld_entry("share"))
98 , m_xFTShare(m_xBuilder
->weld_label("shareLabel"))
100 , m_xDetailsGrid(m_xBuilder
->weld_widget("Details"))
101 , m_xHostBox(m_xBuilder
->weld_widget("HostDetails"))
102 , m_xEDHost(m_xBuilder
->weld_entry("host"))
103 , m_xFTHost(m_xBuilder
->weld_label("hostLabel"))
104 , m_xEDPort(m_xBuilder
->weld_spin_button("port"))
105 , m_xFTPort(m_xBuilder
->weld_label("portLabel"))
106 , m_xEDRoot(m_xBuilder
->weld_entry("path"))
107 , m_xFTRoot(m_xBuilder
->weld_label("pathLabel"))
109 , m_xCBDavs(m_xBuilder
->weld_check_button("webdavs"))
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
) );
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
);
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
);
145 // In edit mode user can't change connection type
149 PlaceEditDialog::~PlaceEditDialog()
153 OUString
PlaceEditDialog::GetServerUrl()
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
);
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() );
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
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
);
212 // Create WebDAV / FTP / 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
> xFtpDetails(std::make_shared
<HostDetailsContainer
>(this, 21, "ftp"));
218 xFtpDetails
->setChangeHdl( LINK( this, PlaceEditDialog
, EditHdl
) );
219 m_aDetailsContainers
.push_back(xFtpDetails
);
221 std::shared_ptr
<DetailsContainer
> xSshDetails(std::make_shared
<HostDetailsContainer
>(this, 22, "sftp"));
222 xSshDetails
->setChangeHdl( LINK( this, PlaceEditDialog
, EditHdl
) );
223 m_aDetailsContainers
.push_back(xSshDetails
);
225 // Remove Windows Share entry from dialog on Windows OS, where it's non-functional
227 // nPos is the position of first item that is pre-defined in svtools/uiconfig/ui/placeedit.ui,
228 // after other CMIS types were inserted
229 m_xLBServerType
->remove(nPos
+ 3);
231 // Create Windows Share control
232 std::shared_ptr
<DetailsContainer
> xSmbDetails(std::make_shared
<SmbDetailsContainer
>(this));
233 xSmbDetails
->setChangeHdl( LINK( this, PlaceEditDialog
, EditHdl
) );
234 m_aDetailsContainers
.push_back(xSmbDetails
);
237 // Set default to first value
238 m_xLBServerType
->set_active(0);
240 if (m_xLBServerType
->get_active_text() == "--------------------" )
241 m_xLBServerType
->set_active(1);
246 IMPL_LINK( PlaceEditDialog
, OKHdl
, weld::Button
&, /*rBtn*/, void)
248 if ( !m_xCurrentDetails
)
251 OUString sUrl
= m_xCurrentDetails
->getUrl().GetHost( INetURLObject::DecodeMechanism::WithCharset
);
253 if ( sUrl
.startsWith( GDRIVE_BASE_URL
)
254 || sUrl
.startsWith( ALFRESCO_CLOUD_BASE_URL
)
255 || sUrl
.startsWith( ONEDRIVE_BASE_URL
) )
257 m_xBTRepoRefresh
->clicked();
259 sUrl
= m_xCurrentDetails
->getUrl().GetHost( INetURLObject::DecodeMechanism::WithCharset
);
260 INetURLObject
aHostUrl( sUrl
);
261 OUString sRepoId
= aHostUrl
.GetMark();
263 if ( !sRepoId
.isEmpty() )
265 m_xDialog
->response(RET_OK
);
269 // TODO: repository id missing. Auth error?
274 m_xDialog
->response(RET_OK
);
278 IMPL_LINK( PlaceEditDialog
, DelHdl
, weld::Button
&, /*rButton*/, void)
280 // ReUsing existing symbols...
281 m_xDialog
->response(RET_NO
);
284 IMPL_LINK_NOARG( PlaceEditDialog
, EditHdl
, DetailsContainer
*, void )
286 if( !m_bLabelChanged
)
288 if( !m_xEDUsername
->get_text().isEmpty( ) )
290 OUString sLabel
= SvtResId( STR_SVT_DEFAULT_SERVICE_LABEL
);
291 OUString sUser
= m_xEDUsername
->get_text();
293 int nLength
= sUser
.indexOf( '@' );
295 nLength
= sUser
.getLength();
297 sLabel
= sLabel
.replaceFirst( "$user$", sUser
.subView( 0, nLength
) );
298 sLabel
= sLabel
.replaceFirst( "$service$", m_xLBServerType
->get_active_text() );
300 m_xEDServerName
->set_text( sLabel
);
301 m_bLabelChanged
= false;
305 m_xEDServerName
->set_text( m_xLBServerType
->get_active_text( ) );
309 OUString sUrl
= GetServerUrl( );
310 OUString sName
= m_xEDServerName
->get_text().trim();
311 m_xBTOk
->set_sensitive( !sName
.isEmpty( ) && !sUrl
.isEmpty( ) );
314 IMPL_LINK_NOARG( PlaceEditDialog
, ModifyHdl
, weld::Entry
&, void )
319 IMPL_LINK_NOARG( PlaceEditDialog
, EditLabelHdl
, weld::Entry
&, void )
321 m_bLabelChanged
= true;
325 IMPL_LINK_NOARG( PlaceEditDialog
, EditUsernameHdl
, weld::Entry
&, void )
327 for ( auto& rxDetailsContainer
: m_aDetailsContainers
)
329 rxDetailsContainer
->setUsername( m_xEDUsername
->get_text() );
330 rxDetailsContainer
->setPassword( m_xEDPassword
->get_text() );
336 IMPL_LINK_NOARG( PlaceEditDialog
, SelectTypeHdl
, weld::ComboBox
&, void )
341 void PlaceEditDialog::SelectType(bool bSkipSeparator
)
343 if ( m_xLBServerType
->get_active_text() == "--------------------" )
346 m_xLBServerType
->set_active(m_nCurrentType
);
348 m_xLBServerType
->set_active(-1);
352 if (m_xCurrentDetails
)
353 m_xCurrentDetails
->set_visible(false);
355 const int nPos
= m_xLBServerType
->get_active( );
356 m_xCurrentDetails
= m_aDetailsContainers
[nPos
];
357 m_nCurrentType
= nPos
;
359 m_xCurrentDetails
->set_visible(true);
361 m_xCBPassword
->set_visible( m_bShowPassword
&& m_xCurrentDetails
->enableUserCredentials() );
362 m_xEDPassword
->set_visible( m_bShowPassword
&& m_xCurrentDetails
->enableUserCredentials() );
363 m_xFTPasswordLabel
->set_visible( m_bShowPassword
&& m_xCurrentDetails
->enableUserCredentials() );
364 m_xEDUsername
->set_visible( m_xCurrentDetails
->enableUserCredentials() );
365 m_xFTUsernameLabel
->set_visible( m_xCurrentDetails
->enableUserCredentials() );
367 m_xDialog
->resize_to_request();
372 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */