Implemented fileserver example.

This commit is contained in:
Nathan Osman
2015-07-02 19:48:22 -07:00
parent 7ee24f5ac4
commit 2c90fcf0fe
5 changed files with 326 additions and 4 deletions

View File

@@ -1,8 +1,17 @@
set(SRC
main.cpp
set(INCLUDE
mainwindow.h
)
add_executable(fileserver WIN32 ${SRC})
set(SRC
main.cpp
mainwindow.cpp
)
qt5_wrap_ui(UI
mainwindow.ui
)
add_executable(fileserver WIN32 ${SRC} ${UI})
qt5_use_modules(fileserver Widgets)
target_link_libraries(fileserver qhttpengine)
@@ -14,6 +23,6 @@ install(TARGETS fileserver
RUNTIME DESTINATION ${EXAMPLES_INSTALL_DIR}/fileserver
)
install(FILES ${SRC}
install(FILES ${INCLUDE} ${SRC} ${UI}
DESTINATION ${EXAMPLES_INSTALL_DIR}/fileserver
)

View File

@@ -22,9 +22,14 @@
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char * argv[])
{
QApplication a(argc, argv);
MainWindow mainWindow;
mainWindow.show();
return a.exec();
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright (c) 2015 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <QDir>
#include <QFileDialog>
#include <QMessageBox>
#include "mainwindow.h"
MainWindow::MainWindow()
: mServer(&mHandler)
{
setupUi(this);
// Obtain the user's current home directory
documentRoot->setText(QDir::homePath());
}
void MainWindow::onBrowseClicked()
{
QString dir = QFileDialog::getExistingDirectory(this, tr("Browse"), documentRoot->text());
if(!dir.isNull()) {
documentRoot->setText(dir);
}
}
void MainWindow::onStartServerClicked()
{
mHandler.setDocumentRoot(documentRoot->text());
if(mServer.listen(QHostAddress::Any, port->value())) {
toggleWidgets(true);
} else {
QMessageBox::critical(this, tr("Error"), tr("Unable to listen on port."));
}
}
void MainWindow::onStopServerClicked()
{
mServer.close();
toggleWidgets(false);
}
void MainWindow::toggleWidgets(bool running)
{
documentRoot->setEnabled(!running);
browse->setEnabled(!running);
portLabel->setEnabled(!running);
port->setEnabled(!running);
startServer->setEnabled(!running);
stopServer->setEnabled(running);
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright (c) 2015 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef FILESERVER_MAINWINDOW_H
#define FILESERVER_MAINWINDOW_H
#include <QMainWindow>
#include <QFilesystemHandler>
#include <QHttpServer>
#include "ui_mainwindow.h"
class MainWindow : public QMainWindow, public Ui::MainWindow
{
Q_OBJECT
public:
MainWindow();
private Q_SLOTS:
void onBrowseClicked();
void onStartServerClicked();
void onStopServerClicked();
private:
void toggleWidgets(bool running);
QFilesystemHandler mHandler;
QHttpServer mServer;
};
#endif // FILESERVER_MAINWINDOW_H

View File

@@ -0,0 +1,182 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>200</height>
</rect>
</property>
<property name="windowTitle">
<string>File Server</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Select a directory below and choose a port in order to serve files from the directory:</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="documentRootLayout">
<item>
<widget class="QLineEdit" name="documentRoot"/>
</item>
<item>
<widget class="QPushButton" name="browse">
<property name="text">
<string>Browse...</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="portLayout">
<item>
<widget class="QLabel" name="portLabel">
<property name="text">
<string>Port:</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="port">
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>65535</number>
</property>
<property name="value">
<number>8000</number>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="buttonLayout">
<item>
<widget class="QPushButton" name="startServer">
<property name="text">
<string>Start Server</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="stopServer">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Stop Server</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>browse</sender>
<signal>clicked()</signal>
<receiver>MainWindow</receiver>
<slot>onBrowseClicked()</slot>
<hints>
<hint type="sourcelabel">
<x>347</x>
<y>63</y>
</hint>
<hint type="destinationlabel">
<x>199</x>
<y>99</y>
</hint>
</hints>
</connection>
<connection>
<sender>startServer</sender>
<signal>clicked()</signal>
<receiver>MainWindow</receiver>
<slot>onStartServerClicked()</slot>
<hints>
<hint type="sourcelabel">
<x>103</x>
<y>176</y>
</hint>
<hint type="destinationlabel">
<x>199</x>
<y>99</y>
</hint>
</hints>
</connection>
<connection>
<sender>stopServer</sender>
<signal>clicked()</signal>
<receiver>MainWindow</receiver>
<slot>onStopServerClicked()</slot>
<hints>
<hint type="sourcelabel">
<x>296</x>
<y>176</y>
</hint>
<hint type="destinationlabel">
<x>199</x>
<y>99</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>onBrowseClicked()</slot>
<slot>onStartServerClicked()</slot>
<slot>onStopServerClicked()</slot>
</slots>
</ui>