#include "../include/Window.hpp" #include "../include/Cache.hpp" #include "../include/ChannelDataType.hpp" #include "../include/WindowNotification.hpp" namespace dchat { Window::Window() { set_border_width(0); Gtk::Overlay *overlay = Gtk::manage(new Gtk::Overlay()); WindowNotification *windowNotification = Gtk::manage(new WindowNotification()); overlay->add_overlay(*windowNotification); overlay->add(stack); add(*overlay); stack.set_transition_type(Gtk::StackTransitionType::STACK_TRANSITION_TYPE_SLIDE_LEFT_RIGHT); stack.set_transition_duration(250); stack.add(loginWindow, "login"); stack.add(chatWindow, "chat"); show_all(); loginWindow.setLoginHandler([this, windowNotification](const Glib::ustring &username, const Glib::ustring &password) { if(!database) { windowNotification->show("You are not connected to the bootstrap node yet! please wait..."); return; } try { fprintf(stderr, "Trying to login with username %s\n", username.raw().c_str()); auto storedNodes = database->getStoredNodeUserInfoDecrypted(username.raw(), password.raw()); fprintf(stderr, "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) { Glib::ustring errMsg = "Failed to login, reason: "; errMsg += e.what(); windowNotification->show(errMsg); } }); odhtdb::DatabaseCallbackFuncs callbackFuncs; callbackFuncs.createNodeCallbackFunc = [this](const odhtdb::DatabaseCreateNodeRequest &request) { std::lock_guard lock(databaseCallbackMutex); chatWindow.addChannel(*request.nodeHash); }; 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]; 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 lock(databaseCallbackMutex); }; 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"); } Window::~Window() { } }