2018-12-23 12:03:08 -05:00
/*******************************************************************************
* gui / settings / WebuiPage . cpp *
* *
* Copyright ( c ) 2014 Retroshare Team < retroshare . project @ gmail . com > *
* *
* This program is free software : you can redistribute it and / or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation , either version 3 of the *
* License , or ( at your option ) any later version . *
* *
* This program is distributed in the hope that it will be useful , *
* but WITHOUT ANY WARRANTY ; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the *
* GNU Affero General Public License for more details . *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program . If not , see < https : //www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2015-04-24 10:31:13 -04:00
# include "WebuiPage.h"
# include <iostream>
# include <QMessageBox>
# include <QDesktopServices>
# include <QUrl>
2017-07-30 12:29:03 -04:00
# include <QSpinBox>
2015-04-24 10:31:13 -04:00
2017-02-27 18:10:54 -05:00
# include "util/misc.h"
2019-11-10 11:38:16 -05:00
# include "retroshare/rswebui.h"
2019-11-16 12:19:07 -05:00
# include "retroshare/rsjsonapi.h"
2015-04-24 10:31:13 -04:00
# include "rsharesettings.h"
resource_api : : ApiServer * WebuiPage : : apiServer = 0 ;
resource_api : : ApiServerMHD * WebuiPage : : apiServerMHD = 0 ;
2016-07-28 08:08:49 -04:00
// TODO: LIBRESAPI_LOCAL_SERVER Put indipendent option for libresapilocalserver in appropriate place
2016-07-14 16:48:44 -04:00
# ifdef LIBRESAPI_LOCAL_SERVER
2016-07-05 10:11:37 -04:00
resource_api : : ApiServerLocal * WebuiPage : : apiServerLocal = 0 ;
2016-07-14 16:48:44 -04:00
# endif
2015-04-24 10:31:13 -04:00
resource_api : : RsControlModule * WebuiPage : : controlModule = 0 ;
2018-09-04 18:08:56 -04:00
2015-04-24 10:31:13 -04:00
WebuiPage : : WebuiPage ( QWidget */ * parent */ , Qt : : WindowFlags /*flags*/ )
{
ui . setupUi ( this ) ;
connect ( ui . enableWebUI_CB , SIGNAL ( clicked ( bool ) ) , this , SLOT ( onEnableCBClicked ( bool ) ) ) ;
2017-07-30 12:29:03 -04:00
connect ( ui . allIp_CB , SIGNAL ( clicked ( bool ) ) , this , SLOT ( onAllIPCBClicked ( bool ) ) ) ;
2019-11-10 16:39:21 -05:00
connect ( ui . apply_PB , SIGNAL ( clicked ( ) ) , this , SLOT ( onApplyClicked ( ) ) ) ;
connect ( ui . password_LE , SIGNAL ( textChanged ( QString ) ) , this , SLOT ( onPasswordValueChanged ( QString ) ) ) ;
connect ( ui . startWebBrowser_PB , SIGNAL ( clicked ( ) ) , this , SLOT ( onStartWebBrowserClicked ( ) ) ) ;
2019-11-10 15:29:26 -05:00
connect ( ui . webInterfaceFilesDirectory_PB , SIGNAL ( clicked ( ) ) , this , SLOT ( selectWebInterfaceDirectory ( ) ) ) ;
2015-04-24 10:31:13 -04:00
}
WebuiPage : : ~ WebuiPage ( )
{
}
2019-11-10 11:38:16 -05:00
void WebuiPage : : selectWebInterfaceDirectory ( )
{
QString dirname = QFileDialog : : getExistingDirectory ( NULL , tr ( " Please select the directory were to find retroshare webinterface files " ) , ui . webInterfaceFiles_LE - > text ( ) ) ;
2019-11-10 15:29:26 -05:00
if ( dirname . isNull ( ) )
return ;
whileBlocking ( ui . webInterfaceFiles_LE ) - > setText ( dirname ) ;
2019-11-10 11:38:16 -05:00
}
2017-01-30 15:38:06 -05:00
bool WebuiPage : : updateParams ( QString & errmsg )
2015-04-24 10:31:13 -04:00
{
std : : cerr < < " WebuiPage::save() " < < std : : endl ;
bool ok = true ;
bool changed = false ;
if ( ui . enableWebUI_CB - > isChecked ( ) ! = Settings - > getWebinterfaceEnabled ( ) )
changed = true ;
2019-11-10 15:29:26 -05:00
if ( ui . webInterfaceFiles_LE - > text ( ) ! = Settings - > getWebinterfaceFilesDirectory ( ) )
changed = true ;
2015-04-24 10:31:13 -04:00
if ( changed )
{
// store config
Settings - > setWebinterfaceEnabled ( ui . enableWebUI_CB - > isChecked ( ) ) ;
2019-11-10 15:29:26 -05:00
Settings - > setWebinterfaceFilesDirectory ( ui . webInterfaceFiles_LE - > text ( ) ) ;
2015-04-24 10:31:13 -04:00
}
return ok ;
}
2019-11-16 12:19:07 -05:00
void WebuiPage : : onPasswordValueChanged ( QString password )
{
QColor color ;
bool valid = password . length ( ) > = 1 ;
if ( ! valid )
color = QApplication : : palette ( ) . color ( QPalette : : Disabled , QPalette : : Base ) ;
else
color = QApplication : : palette ( ) . color ( QPalette : : Active , QPalette : : Base ) ;
/* unpolish widget to clear the stylesheet's palette cache */
//ui.searchLineFrame->style()->unpolish(ui.searchLineFrame);
QPalette palette = ui . password_LE - > palette ( ) ;
palette . setColor ( ui . password_LE - > backgroundRole ( ) , color ) ;
ui . password_LE - > setPalette ( palette ) ;
}
2019-11-10 16:05:55 -05:00
bool WebuiPage : : restart ( )
{
return checkStartWebui ( ) ;
}
2019-11-23 12:40:31 -05:00
2015-04-24 10:31:13 -04:00
void WebuiPage : : load ( )
{
2017-07-30 12:29:03 -04:00
std : : cerr < < " WebuiPage::load() " < < std : : endl ;
whileBlocking ( ui . enableWebUI_CB ) - > setChecked ( Settings - > getWebinterfaceEnabled ( ) ) ;
2019-11-10 11:38:16 -05:00
whileBlocking ( ui . webInterfaceFiles_LE ) - > setText ( Settings - > getWebinterfaceFilesDirectory ( ) ) ;
2019-11-16 12:19:07 -05:00
# ifdef RS_JSONAPI
2019-11-27 12:44:10 -05:00
auto smap = rsJsonApi - > getAuthorizedTokens ( ) ;
2019-11-16 12:19:07 -05:00
auto it = smap . find ( " webui " ) ;
if ( it ! = smap . end ( ) )
whileBlocking ( ui . password_LE ) - > setText ( QString : : fromStdString ( it - > second ) ) ;
# endif
2015-04-24 10:31:13 -04:00
}
2019-11-13 15:06:20 -05:00
2015-04-24 10:31:13 -04:00
QString WebuiPage : : helpText ( ) const
{
2015-06-30 12:44:58 -04:00
return tr ( " <h1><img width= \" 24 \" src= \" :/icons/help_64.png \" > Webinterface</h1> \
2015-09-10 05:29:15 -04:00
< p > The webinterface allows you to control Retroshare from the browser . Multiple devices can share control over one Retroshare instance . So you could start a conversation on a tablet computer and later use a desktop computer to continue it . < / p > \
2015-04-24 10:31:13 -04:00
< p > Warning : don ' t expose the webinterface to the internet , because there is no access control and no encryption . If you want to use the webinterface over the internet , use a SSH tunnel or a proxy to secure the connection . < / p > " );
}
/*static*/ bool WebuiPage : : checkStartWebui ( )
{
if ( ! Settings - > getWebinterfaceEnabled ( ) )
2019-11-13 15:06:20 -05:00
return false ;
2015-04-24 10:31:13 -04:00
2019-11-27 17:32:38 -05:00
rsWebUi - > setHtmlFilesDirectory ( Settings - > getWebinterfaceFilesDirectory ( ) . toStdString ( ) ) ;
rsWebUi - > restart ( ) ;
2015-04-24 10:31:13 -04:00
2019-11-10 11:38:16 -05:00
return true ;
2015-04-24 10:31:13 -04:00
}
/*static*/ void WebuiPage : : checkShutdownWebui ( )
{
2019-11-27 17:32:38 -05:00
rsWebUi - > stop ( ) ;
2015-04-24 10:31:13 -04:00
}
2015-04-28 10:55:13 -04:00
/*static*/ void WebuiPage : : showWebui ( )
{
2019-11-27 12:44:10 -05:00
if ( Settings - > getWebinterfaceEnabled ( ) )
{
QUrl webuiUrl ;
webuiUrl . setScheme ( " http " ) ;
webuiUrl . setHost ( QString : : fromStdString ( rsJsonApi - > getBindingAddress ( ) ) ) ;
webuiUrl . setPort ( rsJsonApi - > listeningPort ( ) ) ;
QDesktopServices : : openUrl ( webuiUrl ) ;
}
2015-04-28 10:55:13 -04:00
else
{
2015-07-29 09:02:10 -04:00
QMessageBox : : warning ( 0 , tr ( " Webinterface not enabled " ) , tr ( " The webinterface is not enabled. Enable it in Settings -> Webinterface. " ) ) ;
2015-04-28 10:55:13 -04:00
}
}
2015-04-24 10:31:13 -04:00
void WebuiPage : : onEnableCBClicked ( bool checked )
{
2017-07-30 12:29:03 -04:00
ui . params_GB - > setEnabled ( checked ) ;
2019-11-10 16:39:21 -05:00
ui . apply_PB - > setEnabled ( checked ) ;
ui . startWebBrowser_PB - > setEnabled ( checked ) ;
2017-07-30 12:29:03 -04:00
QString S ;
2019-11-13 15:06:20 -05:00
2019-11-13 15:35:57 -05:00
Settings - > setWebinterfaceEnabled ( checked ) ;
2019-11-13 15:06:20 -05:00
if ( checked )
checkStartWebui ( ) ;
else
checkShutdownWebui ( ) ;
2017-07-30 12:29:03 -04:00
}
2017-01-31 17:07:59 -05:00
2017-07-30 12:29:03 -04:00
void WebuiPage : : onPortValueChanged ( int /*value*/ )
{
QString S ;
updateParams ( S ) ;
}
void WebuiPage : : onAllIPCBClicked ( bool /*checked*/ )
{
QString S ;
updateParams ( S ) ;
2015-04-24 10:31:13 -04:00
}
void WebuiPage : : onApplyClicked ( )
{
2019-11-27 17:32:38 -05:00
rsWebUi - > setUserPassword ( ui . password_LE - > text ( ) . toStdString ( ) ) ;
2019-11-16 12:19:07 -05:00
2019-11-10 16:05:55 -05:00
if ( ! restart ( ) )
2015-04-24 10:31:13 -04:00
{
QMessageBox : : warning ( 0 , tr ( " failed to start Webinterface " ) , " Failed to start the webinterface. " ) ;
return ;
}
2019-11-16 12:19:07 -05:00
emit passwordChanged ( ) ;
2019-11-10 16:39:21 -05:00
}
2019-11-27 12:44:10 -05:00
void WebuiPage : : onStartWebBrowserClicked ( ) { showWebui ( ) ; }