From 0469c43a45310b6b92eb704773e3a34beb57f288 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 1 Nov 2018 07:17:49 +0100 Subject: Move room code to dchat_core, add ability to send messages --- src/Window.cpp | 107 ++++++++++++++++++++++++--------------------------------- 1 file changed, 45 insertions(+), 62 deletions(-) (limited to 'src/Window.cpp') 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 #include "../include/WindowNotification.hpp" #include #include #include #include +#include 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, const char *errMsg) { - std::lock_guard lock(databaseCallbackMutex); - chatWindow.addChannel(*request.nodeHash); - chatWindow.addUser(*request.creatorPublicKey); - }; - - callbackFuncs.addNodeCallbackFunc = [this](const odhtdb::DatabaseAddNodeRequest &request) - { - std::lock_guard lock(databaseCallbackMutex); - if(request.decryptedData.size == 0) - return; - - ChannelDataType channelDataType = (ChannelDataType)static_cast(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(); - 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) + { + chatWindow.addRoom(room); + }; + roomCallbackFuncs.addUserCallbackFunc = [this](std::shared_ptr room, std::shared_ptr user) + { + chatWindow.addUser(room, user); + }; + roomCallbackFuncs.addMessageCallbackFunc = [this](const RoomAddMessageRequest &request) + { + chatWindow.addMessage(request); + }; + roomCallbackFuncs.userChangeNicknameCallbackFunc = [this](const UserChangeNicknameRequest &request) { - std::lock_guard 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 sizeDeviationRand(0, 5); -- cgit v1.2.3