OSDN Git Service

ソケットの終了処理追加。
[wordring-tm/wordring-tm.git] / proxy / tmsocket.h
1 #ifndef TMSOCKET_H
2 #define TMSOCKET_H
3
4 #include "html.h"
5 #include "text.h"
6
7 #include <QObject>
8
9 #include <QString>
10 #include <QList>
11 #include <QJsonArray>
12
13 #include <QThread>
14 #include <QMutex>
15
16 #include <QWebSocket>
17 #include <QWebSocketServer>
18
19 QT_BEGIN_NAMESPACE
20 //class QSettings;
21 QT_END_NAMESPACE
22
23 class Settings;
24
25 namespace TM
26 {
27
28 class Service;
29 class EditorWidget;
30
31 class HtmlData : public RangeData
32 {
33 public:
34         typedef std::shared_ptr<HtmlData> pointer;
35
36         enum : int { Type = UserData::User + 1, };
37
38 private:
39         HtmlData(HtmlNode node_, int begin_, int tail_);
40
41 public:
42         int type() const;
43
44         static pointer create(HtmlNode node_, int begin_, int tail_);
45
46 private:
47         HtmlNode m_node;
48 };
49
50 /*!
51  * \brief HtmlRangeから構造化テキストに変換するためのクラスです。
52  */
53 class TextConverter
54 {
55 public:
56         Text::pointer to_text(HtmlRange range);
57         Text::pointer stuff_text(Text::pointer parent, QString const &string);
58
59         bool is_ignorable_white_space(QChar const &ch);
60
61         static bool is_white_space(QChar const &ch);
62
63 private:
64         int m_state;
65 };
66
67 class TextSentence
68 {
69
70 private:
71         Text::pointer m_source_sentence;
72         Text::pointer m_target_sentence;
73         QJsonArray m_link_data;
74 };
75
76 class TextSegment
77 {
78
79 };
80
81 class SocketConnection : public QObject
82 {
83         Q_OBJECT
84
85 public:
86         SocketConnection(Settings *settings, Service *service,
87                                         EditorWidget *editor_widget, QWebSocket *socket);
88         virtual ~SocketConnection();
89
90         //quint16 port() const;
91         QWebSocket* socket();
92         void send_message(QString const &message);
93         void send_message(QJsonObject const &json);
94
95         void set_edit_mode(bool edit_mode);
96         //void set_source_language(int code);
97         //void set_target_language(int code);
98
99 signals:
100         void editCmd(int id, QString html);
101
102 public slots:
103         void changeEditMode(bool edit_mode);
104
105 private:
106         void do_edit(QJsonObject const &json);
107         void do_focus(QJsonObject const &json);
108         void do_blur(QJsonObject const &json);
109         void do_load(QJsonObject const &json);
110
111         Text::pointer to_text(HtmlRange range) const;
112
113 private slots:
114         void onTextMessageReceived(QString const &message);
115         void onBinaryMessageReceived(QByteArray const &message);
116
117 private:
118         Settings *m_settings;
119         Service *m_service;
120
121         QMutex m_mutex;
122
123         bool m_edit_mode;
124         int m_source_language;
125         int m_target_language;
126
127         EditorWidget *m_editor_widget;
128
129         HtmlDocument m_document; /*!< 現在処理中のHTML文書パラグラフを保持 */
130         Text::pointer m_text; /*!< HtmlData付のパラグラフをテキストとして保持 */
131         //QJsonArray m_json; /*!<  */
132 };
133
134 class SocketServer : public QObject
135 {
136         Q_OBJECT
137 public:
138         SocketServer(Settings *settings, Service* service,
139                                 EditorWidget *editor_widget, QObject *parent);
140         ~SocketServer();
141
142         quint16 port() const;
143
144 signals:
145
146 public slots:
147         void abort();
148         void onNewConnection();
149         void onDisconnected();
150
151 private:
152         void attachConnections(QWebSocket* socket);
153         void detachConnections(QWebSocket* socket);
154 signals:
155
156 private:
157         Settings *m_settings;
158         Service *m_service;
159
160         QWebSocketServer *m_server;
161         QList<QWebSocket*> m_sockets;
162
163         EditorWidget *m_editor_widget;
164 };
165
166 } // namespace TM
167
168 #endif // TMSOCKET_H