aboutsummaryrefslogtreecommitdiff
path: root/src/Window.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Window.cpp')
-rw-r--r--src/Window.cpp63
1 files changed, 61 insertions, 2 deletions
diff --git a/src/Window.cpp b/src/Window.cpp
index c912130..5f27000 100644
--- a/src/Window.cpp
+++ b/src/Window.cpp
@@ -1,4 +1,6 @@
#include "../include/Window.hpp"
+#include "../include/Cache.hpp"
+#include "../include/ChannelDataType.hpp"
namespace dchat
{
@@ -12,10 +14,67 @@ namespace dchat
show_all_children();
- loginWindow.setLoginHandler([]
+ loginWindow.setLoginHandler([this](const Glib::ustring &username, const Glib::ustring &password)
{
- printf("login!\n");
+ if(!database)
+ {
+ fprintf(stderr, "You are not connected to the bootstrap node yet! please wait...\n");
+ return;
+ }
+
+ try
+ {
+ printf("Trying to login with username %s\n", username.raw().c_str());
+ auto storedNodes = database->getStoredNodeUserInfoDecrypted(username.raw(), password.raw());
+ printf("Successfully logged in as %s\n", username.raw().c_str());
+ stack.set_visible_child(chatWindow);
+
+ for(auto &nodeInfo : storedNodes)
+ {
+ database->loadNode(nodeInfo.first);
+ }
+ }
+ catch(std::exception &e)
+ {
+ fprintf(stderr, "Failed to login, reason: %s\n", e.what());
+ }
});
+
+ odhtdb::DatabaseCallbackFuncs callbackFuncs;
+ callbackFuncs.createNodeCallbackFunc = [this](const odhtdb::DatabaseCreateNodeRequest &request)
+ {
+ std::lock_guard<std::mutex> lock(databaseCallbackMutex);
+ chatWindow.addChannel(*request.nodeHash);
+ };
+
+ 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];
+ switch(channelDataType)
+ {
+ case ChannelDataType::ADD_MESSAGE:
+ {
+ Glib::ustring msg((const char*)request.decryptedData.data + 1, request.decryptedData.size - 1);
+ chatWindow.addLocalMessage(*request.nodeHash, std::move(msg));
+ break;
+ }
+ default:
+ break;
+ }
+ };
+
+ callbackFuncs.addUserCallbackFunc = [this](const odhtdb::DatabaseAddUserRequest &request)
+ {
+ std::lock_guard<std::mutex> lock(databaseCallbackMutex);
+ };
+
+ printf("Connecting...\n");
+ database = odhtdb::Database::connect("83.252.53.188", 27130, Cache::getDchatDir(), callbackFuncs).get();
+ stack.set_visible_child(loginWindow);
}
Window::~Window()