diff --git a/CMakeLists.txt b/CMakeLists.txt index 56dd9d7..9d851b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,9 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -pthread") find_package(Boost COMPONENTS system REQUIRED) include_directories(${Boost_INCLUDE_DIR}) -set(SOURCE_FILES commandline_chat.cpp chat_messages.hpp chat_networking.hpp) +find_package(Qt5Widgets) +set(CMAKE_AUTOMOC ON) + +set(SOURCE_FILES gui_chat.cpp chat_window.hpp chat_messages.hpp chat_networking.hpp check_policy.hpp) add_executable(cpp11NHF2_chat ${SOURCE_FILES}) -target_link_libraries(cpp11NHF2_chat ${Boost_LIBRARIES}) +target_link_libraries(cpp11NHF2_chat ${Boost_LIBRARIES} Qt5::Widgets) \ No newline at end of file diff --git a/chat_window.hpp b/chat_window.hpp index 0dce1ac..ca7aca7 100644 --- a/chat_window.hpp +++ b/chat_window.hpp @@ -3,9 +3,31 @@ #include #include #include +#include +#include +#include "chat_networking.hpp" +struct receive_policy_ignore +{ + static void message_do_what(const chat::chat_message& msg) {} + static void handshake_do_what(const chat::chat_message& msg) {} + static void serverdirection_do_what(const chat::chat_message& msg) {} + static void login_do_what(const chat::chat_message& msg) {} + static void logout_do_what(const chat::chat_message& msg) {} + static void invalid_msg_do_what(const chat::chat_message& msg) + { throw std::runtime_error("Invalid message received!"); } +}; + +struct send_policy_ignore +{ + /* this is handled in the gui: QLineEdit.setMaxLength() */ + static bool check_msg_length(const chat::chat_message& msg) + { return true; } +}; + + class chat_window : public QWidget { Q_OBJECT @@ -15,15 +37,47 @@ class chat_window : public QWidget QListWidget* _people; QLineEdit* _msg; + + boost::asio::io_service _ios; + boost::asio::ip::tcp::resolver::query _query; + chat::client_network_manager _cnm; + std::thread _iosthread; + + public: explicit chat_window(QWidget* parent = 0) - :QWidget(parent) - { setupUi(); } + :QWidget(parent), _ios(), _query("infoc.eet.bme.hu", "8888"), + _cnm(_ios, boost::asio::ip::tcp::resolver(_ios).resolve(_query), "BATMAN"), + _iosthread([this]{ _ios.run(); }) + { + _cnm.subscribe(std::bind(&chat_window::receive_handler, this, std::placeholders::_1)); + setupUi(); + } + + void receive_handler(chat::chat_message msg) + { + switch (msg.get_header()) + { + case chat::message::MESSAGE: + case chat::message::SERVER_DIRECTION: + _chat->addItem(msg.get_content().c_str()); + break; + case chat::message::LOGIN: + _people->addItem(msg.get_content().c_str()); + break; + case chat::message::LOGOUT: + auto list = _people->findItems(msg.get_content().c_str(), Qt::MatchExactly); + if (list.size() > 0) + delete list[0]; + break; + } + } public slots: void return_handler() { _chat->addItem(_msg->text()); + _cnm.send(chat::chat_message(chat::message::MESSAGE, _msg->text().toStdString())); _msg->clear(); }; @@ -38,6 +92,7 @@ private: _people = new QListWidget(); _msg = new QLineEdit(); + _msg->setMaxLength(220); connect(_msg, &QLineEdit::returnPressed, this, &chat_window::return_handler); _layout->addWidget(_chat, 0, 0, 1, 1); @@ -46,4 +101,10 @@ private: this->setLayout(_layout); } -}; \ No newline at end of file + + void closeEvent(QCloseEvent* event) + { + _cnm.close_connection(); + _iosthread.join(); + } +}; diff --git a/gui_chat.cpp b/gui_chat.cpp new file mode 100644 index 0000000..a89b3ca --- /dev/null +++ b/gui_chat.cpp @@ -0,0 +1,13 @@ +#include +#include "chat_window.hpp" + + + +int main(int argc, char* argv[]) +{ + QApplication a(argc, argv); + chat_window w; + w.show(); + + return a.exec(); +} \ No newline at end of file