OSDN Git Service

フォルダ名変更
[wordring-tm/wordring-tm.git] / proxy / tmsocket.cpp
index 2fbde6e..2650e4a 100644 (file)
@@ -1,6 +1,7 @@
 #include "tmsocket.h"
-#include "tmwidget.h"
+#include "tmeditorwidget.h"
 
+#include "settings.h"
 #include "html.h"
 
 #include <QMutex>
@@ -9,8 +10,6 @@
 #include <QJsonDocument>
 #include <QJsonObject>
 
-#include <QSettings>
-
 #include "debug.h"
 
 
@@ -116,9 +115,9 @@ bool TM::TextConverter::is_white_space(QChar const &ch)
 
 // SocketConnection -----------------------------------------------------------
 
-TM::SocketConnection::SocketConnection(Widget *widget, QWebSocket *socket)
+TM::SocketConnection::SocketConnection(EditorWidget *editor_widget, QWebSocket *socket)
        : QObject(socket)
-       , m_widget(widget)
+       , m_editor_widget(editor_widget)
        , m_mutex(QMutex::Recursive)
        , m_edit_mode(false)
 {
@@ -130,7 +129,7 @@ TM::SocketConnection::SocketConnection(Widget *widget, QWebSocket *socket)
 
 TM::SocketConnection::~SocketConnection()
 {
-       m_widget->detach(this);
+       m_editor_widget->detach(this);
 }
 
 QWebSocket* TM::SocketConnection::socket()
@@ -179,7 +178,7 @@ void TM::SocketConnection::do_edit(QJsonObject const &json)
 
        TextConverter tc;
        m_text = tc.to_text(HtmlRange(body, body));
-       m_widget->set_string(m_text->to_string(), json["target"].toString());
+       m_editor_widget->set_string(m_text->to_string(), json["target"].toString());
        //emit editCmd(json["id"].toInt(), m_document.to_string());
        /*
        QJsonObject reply;
@@ -198,12 +197,12 @@ void TM::SocketConnection::do_edit(QJsonObject const &json)
  */
 void TM::SocketConnection::do_focus(QJsonObject const &json)
 {
-       m_widget->attach(this);
+       m_editor_widget->attach(this);
        bool edit_mode = json["edit_mode"].toBool();
        m_edit_mode = edit_mode;
-       m_widget->set_edit_mode(edit_mode);
+       m_editor_widget->set_edit_mode(edit_mode);
 
-       if(m_text) m_widget->set_string(m_text->to_string(), "");
+       if(m_text) m_editor_widget->set_string(m_text->to_string(), "");
 }
 
 void TM::SocketConnection::do_blur(QJsonObject const &)
@@ -213,10 +212,10 @@ void TM::SocketConnection::do_blur(QJsonObject const &)
 
 void TM::SocketConnection::do_load(const QJsonObject &json)
 {
-       m_widget->attach(this);
+       m_editor_widget->attach(this);
        bool edit_mode = json["edit_mode"].toBool();
        m_edit_mode = edit_mode;
-       m_widget->set_edit_mode(edit_mode);
+       m_editor_widget->set_edit_mode(edit_mode);
 
        //set_edit_mode(m_widget->edit_mode());
 }
@@ -236,16 +235,14 @@ void TM::SocketConnection::onBinaryMessageReceived(QByteArray const &message)
        qDebug() << message;
 }
 
-
-
 // SocketServer ---------------------------------------------------------------
 
-TM::SocketServer::SocketServer(QSettings *settings, Widget *widget, QObject *parent)
+TM::SocketServer::SocketServer(Settings *settings, EditorWidget *editor_widget, QObject *parent)
        : QObject(parent)
        , m_settings(settings)
        , m_server(new QWebSocketServer(QStringLiteral("wordring websocket"),
                                                                        QWebSocketServer::NonSecureMode, this))
-       , m_widget(widget)
+       , m_editor_widget(editor_widget)
 {
        connect(m_server, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
 
@@ -255,21 +252,37 @@ TM::SocketServer::SocketServer(QSettings *settings, Widget *widget, QObject *par
        if(!result) qFatal("An error occured in SocketServer().");
 }
 
+TM::SocketServer::~SocketServer()
+{
+}
+
 quint16 TM::SocketServer::port() const
 {
        return m_server->serverPort();
 }
 
+void TM::SocketServer::abort()
+{
+       for(QWebSocket *ws : m_sockets)
+       {
+               ws->abort();
+               ws->deleteLater();
+       }
+       m_sockets.clear();
+}
+
 void TM::SocketServer::onNewConnection()
 {
        QWebSocket *socket = m_server->nextPendingConnection();
        connect(socket, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
-       new SocketConnection(m_widget, socket);
+       new SocketConnection(m_editor_widget, socket);
+       m_sockets.push_back(socket);
 }
 
 void TM::SocketServer::onDisconnected()
 {
        QWebSocket *socket = qobject_cast<QWebSocket*>(sender());
+       m_sockets.removeOne(socket);
        socket->deleteLater();
 }