fixed a few bugs. First running version.

This commit is contained in:
csoler 2019-11-10 21:29:26 +01:00
parent 0103a741d6
commit 3446f0b677
No known key found for this signature in database
GPG Key ID: 7BCA522266C0804C
5 changed files with 73 additions and 25 deletions

View File

@ -47,6 +47,13 @@ static constexpr char *mime_types[3] = {
"image/svg+xml"
};
#ifdef WINDOWS_SYS
static std::string _base_directory = "data/webui";
#else
static std::string _base_directory = "/usr/share/retroshare/webui/";
#endif
template<int MIME_TYPE_INDEX> class handler
{
public:
@ -79,16 +86,8 @@ template<int MIME_TYPE_INDEX> class handler
session->close( restbed::NOT_FOUND );
}
}
static std::string _base_directory ;
};
#ifdef WINDOWS_SYS
template<int MIME_TYPE_INDEX> std::string handler<MIME_TYPE_INDEX>::_base_directory = "data/webui";
#else
template<int MIME_TYPE_INDEX> std::string handler<MIME_TYPE_INDEX>::_base_directory = "/usr/share/retroshare/webui/";
#endif
static void service_ready_handler( restbed::Service& )
{
fprintf( stderr, "Hey! The service is up and running." );
@ -100,7 +99,7 @@ public:
WebUIThread()
{
_service = std::make_shared<restbed::Service>();
_listening_port = 1984;
}
void runloop() override
@ -128,7 +127,7 @@ public:
resource3->set_method_handler( "GET", handler<TEXT_SVG>::get_handler );
auto settings = std::make_shared< restbed::Settings >( );
settings->set_port( 1984 );
settings->set_port( _listening_port );
settings->set_default_header( "Connection", "close" );
_service->publish( resource1 );
@ -136,7 +135,16 @@ public:
_service->publish( resource3 );
_service->set_ready_handler( service_ready_handler );
_service->start( settings );
try
{
_service->start( settings );
}
catch(std::exception& e)
{
RsErr() << "Could not start web interface: " << e.what() << std::endl;
return;
}
}
void stop()
{
@ -148,8 +156,12 @@ public:
sleep(1);
}
void setListeningPort(uint16_t p) { _listening_port = p ; }
uint16_t listeningPort() const { return _listening_port;}
private:
std::shared_ptr<restbed::Service> _service;
uint16_t _listening_port;
};
p3WebUI::p3WebUI()
@ -169,7 +181,7 @@ p3WebUI::~p3WebUI()
bool p3WebUI::restart()
{
RsDbg() << "Restarting web interface listening on port " << _listening_port << std::endl;
RsDbg() << "Restarting web interface listening on port " << _webui_thread->listeningPort() << std::endl;
if(_webui_thread->isRunning())
_webui_thread->stop();
@ -186,11 +198,11 @@ bool p3WebUI::stop()
void p3WebUI::setHtmlFilesDirectory(const std::string& html_dir)
{
_html_files_directory = html_dir;
_base_directory = html_dir;
}
void p3WebUI::setListeningPort(uint16_t port)
{
_listening_port = port;
_webui_thread->setListeningPort(port);
if(_webui_thread->isRunning())
restart();

View File

@ -41,9 +41,6 @@ public:
private:
WebUIThread *_webui_thread;
std::string _html_files_directory;
uint16_t _listening_port;
};

View File

@ -47,7 +47,7 @@ WebuiPage::WebuiPage(QWidget */*parent*/, Qt::WindowFlags /*flags*/)
connect(ui.port_SB, SIGNAL(valueChanged(int)), this, SLOT(onPortValueChanged(int)));
connect(ui.allIp_CB, SIGNAL(clicked(bool)), this, SLOT(onAllIPCBClicked(bool)));
connect(ui.applyStartBrowser_PB, SIGNAL(clicked()), this, SLOT(onApplyClicked()));
connect(ui.webInterfaceFiles_LE, SIGNAL(clicked()), this, SLOT(selectWebInterfaceDirectory()));
connect(ui.webInterfaceFilesDirectory_PB, SIGNAL(clicked()), this, SLOT(selectWebInterfaceDirectory()));
}
WebuiPage::~WebuiPage()
@ -58,6 +58,11 @@ WebuiPage::~WebuiPage()
void WebuiPage::selectWebInterfaceDirectory()
{
QString dirname = QFileDialog::getExistingDirectory(NULL,tr("Please select the directory were to find retroshare webinterface files"),ui.webInterfaceFiles_LE->text());
if(dirname.isNull())
return;
whileBlocking(ui.webInterfaceFiles_LE)->setText(dirname);
}
bool WebuiPage::updateParams(QString &errmsg)
@ -71,12 +76,16 @@ bool WebuiPage::updateParams(QString &errmsg)
changed = true;
if(ui.allIp_CB->isChecked() != Settings->getWebinterfaceAllowAllIps())
changed = true;
if(ui.webInterfaceFiles_LE->text() != Settings->getWebinterfaceFilesDirectory())
changed = true;
if(changed)
{
// store config
Settings->setWebinterfaceEnabled(ui.enableWebUI_CB->isChecked());
Settings->setWebinterfacePort(ui.port_SB->value());
Settings->setWebinterfaceAllowAllIps(ui.allIp_CB->isChecked());
Settings->setWebinterfaceFilesDirectory(ui.webInterfaceFiles_LE->text());
// apply config
checkShutdownWebui();

View File

@ -60,7 +60,29 @@
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="webInterfaceFiles_LE"/>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="webInterfaceFiles_LE">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="webInterfaceFilesDirectory_PB">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Select directory for webinterface files (advanced)&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../images.qrc">
<normaloff>:/images/folder-draft24-pressed.png</normaloff>:/images/folder-draft24-pressed.png</iconset>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
@ -106,6 +128,8 @@
</item>
</layout>
</widget>
<resources/>
<resources>
<include location="../images.qrc"/>
</resources>
<connections/>
</ui>

View File

@ -1150,11 +1150,6 @@ void RshareSettings::setWebinterfaceEnabled(bool enabled)
setValueToGroup("Webinterface", "enabled", enabled);
}
uint16_t RshareSettings::getWebinterfacePort()
{
return valueFromGroup("Webinterface", "port", 1984).toUInt();
}
QString RshareSettings::getWebinterfaceFilesDirectory()
{
#ifdef WINDOWS_SYS
@ -1163,6 +1158,17 @@ QString RshareSettings::getWebinterfaceFilesDirectory()
return valueFromGroup("Webinterface","directory","/usr/share/retroshare/webui/").toString();
}
void RshareSettings::setWebinterfaceFilesDirectory(const QString& s)
{
setValueToGroup("Webinterface","directory",s);
}
uint16_t RshareSettings::getWebinterfacePort()
{
return valueFromGroup("Webinterface", "port", 1984).toUInt();
}
void RshareSettings::setWebinterfacePort(uint16_t port)
{
setValueToGroup("Webinterface", "port", port);