diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-11-01 07:17:49 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2018-11-01 07:17:52 +0100 |
commit | 0469c43a45310b6b92eb704773e3a34beb57f288 (patch) | |
tree | 3d79b563fdc99a18f29bf65a8b293fecca8b7a4a /src/Window.cpp | |
parent | a535703add6bf29878845cb270997514489cfc16 (diff) |
Move room code to dchat_core, add ability to send messages
Diffstat (limited to 'src/Window.cpp')
-rw-r--r-- | src/Window.cpp | 107 |
1 files changed, 45 insertions, 62 deletions
diff --git a/src/Window.cpp b/src/Window.cpp index 6a0ab64..b9e1490 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -1,15 +1,16 @@ #include "../include/Window.hpp" -#include "../include/ChannelDataType.hpp" +#include <dchat/RoomDataType.hpp> #include "../include/WindowNotification.hpp" #include <dchat/Cache.hpp> #include <sibs/SafeDeserializer.hpp> #include <math.h> #include <chrono> +#include <gtkmm.h> namespace dchat { - const int nodesPerColumn = 5; - const int nodesPerRow = 5; + const int nodesPerColumn = 10; + const int nodesPerRow = 10; Window::Window() { @@ -28,12 +29,12 @@ namespace dchat overlay.show(); windowNotification->show_all(); stack.show(); - chatWindow.show_all(); - loginWindow.show(); + //chatWindow.show_all(); + //loginWindow.show(); loginWindow.setLoginHandler([this, windowNotification](const Glib::ustring &username, const Glib::ustring &password) { - if(!database) + if(!rooms || !rooms->database) { windowNotification->show("You are not connected to the bootstrap node yet! please wait..."); return; @@ -42,15 +43,18 @@ namespace dchat try { fprintf(stderr, "Trying to login with username %s\n", username.raw().c_str()); - auto storedNodes = database->getStoredNodeUserInfoDecrypted(username.raw(), password.raw()); + rooms->loginUser(username.raw(), password.raw()); windowNotification->show(Glib::ustring("Successfully logged in as ") + username); drawBackgroundConnection.disconnect(); + chatWindow.show_all(); stack.set_visible_child(chatWindow); - for(auto &nodeInfo : storedNodes) + Glib::signal_timeout().connect([this] { - database->loadNode(nodeInfo.first); - } + printf("scroll to bottom!\n"); + chatWindow.scrollToBottom(); + return false; + }, 100); } catch(std::exception &e) { @@ -62,7 +66,7 @@ namespace dchat loginWindow.setRegisterHandler([this, windowNotification](const Glib::ustring &username, const Glib::ustring &password) { - if(!database) + if(!rooms || !rooms->database) { windowNotification->show("You are not connected to the bootstrap node yet! please wait..."); return; @@ -71,9 +75,10 @@ namespace dchat try { fprintf(stderr, "Trying to register username %s\n", username.raw().c_str()); - database->storeUserWithoutNodes(username.raw(), password.raw()); + rooms->registerUser(username.raw(), password.raw()); windowNotification->show(Glib::ustring("Successfully registered user ") + username); drawBackgroundConnection.disconnect(); + chatWindow.show_all(); stack.set_visible_child(chatWindow); } catch(std::exception &e) @@ -91,65 +96,43 @@ namespace dchat windowNotification->show("Passwords do not match"); }); - odhtdb::DatabaseCallbackFuncs callbackFuncs; - callbackFuncs.createNodeCallbackFunc = [this](const odhtdb::DatabaseCreateNodeRequest &request) + RoomCallbackFuncs roomCallbackFuncs; + roomCallbackFuncs.connectCallbackFunc = [this, windowNotification](std::shared_ptr<Rooms> rooms, const char *errMsg) { - std::lock_guard<std::mutex> lock(databaseCallbackMutex); - chatWindow.addChannel(*request.nodeHash); - chatWindow.addUser(*request.creatorPublicKey); - }; - - callbackFuncs.addNodeCallbackFunc = [this](const odhtdb::DatabaseAddNodeRequest &request) - { - std::lock_guard<std::mutex> lock(databaseCallbackMutex); - if(request.decryptedData.size == 0) - return; - - ChannelDataType channelDataType = (ChannelDataType)static_cast<const char*>(request.decryptedData.data)[0]; - try + this->rooms = rooms; + if(rooms) { - switch(channelDataType) - { - case ChannelDataType::ADD_MESSAGE: - { - Glib::ustring msg((const char*)request.decryptedData.data + 1, request.decryptedData.size - 1); - uint32_t timestampSeconds = ntp::NtpTimestamp::fromCombined(request.timestamp).seconds; - chatWindow.addLocalMessage(*request.nodeHash, *request.creatorPublicKey, timestampSeconds, std::move(msg)); - break; - } - case ChannelDataType::NICKNAME_CHANGE: - { - sibs::SafeDeserializer deserializer((const u8*)request.decryptedData.data + 1, request.decryptedData.size - 1); - u8 nameLength = deserializer.extract<u8>(); - if(nameLength > 0) - { - std::string nickname; - nickname.resize(nameLength); - deserializer.extract((u8*)&nickname[0], nameLength); - chatWindow.setUserNickname(*request.creatorPublicKey, nickname); - } - break; - } - default: - break; - } + loginWindow.show(); + stack.set_visible_child(loginWindow); + windowNotification->show("Connected to 83.252.53.188:27130"); + loginWindow.loginUsernameInput.grab_focus(); } - catch(std::exception &e) + else { - fprintf(stderr, "Failed to process add node request, reason: %s\n", e.what()); + std::string errMsgToShow = "Failed to connect to boostrap node, reason: "; + errMsgToShow += errMsg; + windowNotification->show(errMsgToShow); } }; - - callbackFuncs.addUserCallbackFunc = [this](const odhtdb::DatabaseAddUserRequest &request) + roomCallbackFuncs.createRoomCallbackFunc = [this](std::shared_ptr<Room> room) + { + chatWindow.addRoom(room); + }; + roomCallbackFuncs.addUserCallbackFunc = [this](std::shared_ptr<Room> room, std::shared_ptr<User> user) + { + chatWindow.addUser(room, user); + }; + roomCallbackFuncs.addMessageCallbackFunc = [this](const RoomAddMessageRequest &request) + { + chatWindow.addMessage(request); + }; + roomCallbackFuncs.userChangeNicknameCallbackFunc = [this](const UserChangeNicknameRequest &request) { - std::lock_guard<std::mutex> lock(databaseCallbackMutex); - chatWindow.addUser(*request.userToAddPublicKey); + chatWindow.setUserNickname(request); }; - fprintf(stderr, "Connecting...\n"); - database = odhtdb::Database::connect("83.252.53.188", 27130, Cache::getDchatDir(), callbackFuncs).get(); - stack.set_visible_child(loginWindow); - windowNotification->show("Connected to 83.252.53.188:27130"); + windowNotification->show("Connecting to 83.252.53.188:27130"); + Rooms::connect("83.252.53.188", 27130, roomCallbackFuncs); backgroundRng.seed(std::random_device()()); std::uniform_int_distribution<std::mt19937::result_type> sizeDeviationRand(0, 5); |