From 905a9b962b1464cf2f293b21634d4aa665c009ab Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 4 Nov 2018 03:44:11 +0100 Subject: Fix crash when creating room when user is not a member of any other room --- include/ChatWindow.hpp | 4 +++- include/Window.hpp | 5 ++++- src/ChatWindow.cpp | 20 +++++++++++++++----- src/Window.cpp | 14 +++++++------- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/include/ChatWindow.hpp b/include/ChatWindow.hpp index 7a61d91..d457b7d 100644 --- a/include/ChatWindow.hpp +++ b/include/ChatWindow.hpp @@ -16,11 +16,12 @@ namespace dchat { class ChatMessage; + class Window; class ChatWindow : public Gtk::Grid { public: - ChatWindow(); + ChatWindow(Window *window); ~ChatWindow(); void addRoom(std::shared_ptr room); void addMessage(const RoomAddMessageRequest &request); @@ -59,5 +60,6 @@ namespace dchat int roomCount; RoomData *currentRoomData; std::shared_ptr currentRoom; + Window *window; }; } \ No newline at end of file diff --git a/include/Window.hpp b/include/Window.hpp index a29ec62..4355fb8 100644 --- a/include/Window.hpp +++ b/include/Window.hpp @@ -2,6 +2,7 @@ #include "ChatWindow.hpp" #include "LoginWindow.hpp" +#include "WindowNotification.hpp" #include #include #include @@ -17,6 +18,9 @@ namespace dchat public: Window(); virtual ~Window(); + + std::shared_ptr rooms; + WindowNotification *windowNotification; private: bool drawBackground(const Cairo::RefPtr &cairo); private: @@ -26,7 +30,6 @@ namespace dchat void draw(const Cairo::RefPtr &cairo) { Gtk::Overlay::draw(cairo); } }; - std::shared_ptr rooms; std::mutex databaseCallbackMutex; OverlayDrawable overlay; Gtk::Stack stack; diff --git a/src/ChatWindow.cpp b/src/ChatWindow.cpp index b53e337..f7cc5e2 100644 --- a/src/ChatWindow.cpp +++ b/src/ChatWindow.cpp @@ -1,22 +1,25 @@ #include "../include/ChatWindow.hpp" #include "../include/ChatMessage.hpp" #include "../include/InputDialog.hpp" +#include "../include/Window.hpp" #include #include #include #include -#include +#include namespace dchat { // Merge all messages that are written by the same user without interrupt within a timeframe const int MERGE_MESSAGE_TIMESTAMP_DIFF_SEC = 60; - ChatWindow::ChatWindow() : + ChatWindow::ChatWindow(Window *_window) : addRoomButton("images/add_button_small.png", " Add room"), roomCount(0), - currentRoomData(nullptr) + currentRoomData(nullptr), + window(_window) { + assert(window); leftPanelUsersStack.set_homogeneous(false); messageAreaStack.set_homogeneous(false); setupTopBar(); @@ -118,8 +121,12 @@ namespace dchat { case Gtk::RESPONSE_ACCEPT: { - // TODO: Do not allow if room name size is == 0 or > 32 - currentRoom->rooms->createRoom(createRoomDialog.getInput()); + // TODO: Show error inline in the create room dialog + Glib::ustring roomName = createRoomDialog.getInput(); + if(roomName.size() == 0 || roomName.size() > 32) + window->windowNotification->show("Room name has to be between 1 and 32 characters"); + else + window->rooms->createRoom(roomName); break; } default: @@ -151,6 +158,7 @@ namespace dchat chatArea->attach(*chatScrollWindow, 0, 0, 1, 1); chatInput.set_hexpand(true); + chatInput.set_editable(false); chatInput.set_name("chat-input"); chatInput.set_wrap_mode(Gtk::WrapMode::WRAP_WORD_CHAR); chatScrollWindow->add(chatInput); @@ -239,6 +247,7 @@ namespace dchat currentRoomData = new RoomData { leftPanelUsersLayout, messageAreaLayout, roomButton }; roomDataById[*room->id] = currentRoomData; currentRoom = room; + chatInput.set_editable(true); leftPanelUsersStack.set_visible_child(roomIdStr); messageAreaStack.set_visible_child(roomIdStr); @@ -252,6 +261,7 @@ namespace dchat currentChannelTitle.set_text(room->name); currentRoom = room; currentRoomData = roomDataById[*room->id]; + chatInput.set_editable(true); // TODO: Instead of scrolling to bottom, remember scroll position (even after restarting application). // We want to show oldest unread message first diff --git a/src/Window.cpp b/src/Window.cpp index b5a9f26..22e5501 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -1,6 +1,5 @@ #include "../include/Window.hpp" #include -#include "../include/WindowNotification.hpp" #include #include #include @@ -11,10 +10,11 @@ namespace dchat const int nodesPerColumn = 10; const int nodesPerRow = 10; - Window::Window() + Window::Window() : + chatWindow(this) { set_border_width(0); - WindowNotification *windowNotification = Gtk::manage(new WindowNotification()); + windowNotification = Gtk::manage(new WindowNotification()); overlay.add_overlay(*windowNotification); overlay.set_overlay_pass_through(*windowNotification); overlay.add(stack); @@ -31,7 +31,7 @@ namespace dchat //chatWindow.show_all(); //loginWindow.show(); - loginWindow.setLoginHandler([this, windowNotification](const Glib::ustring &username, const Glib::ustring &password) + loginWindow.setLoginHandler([this](const Glib::ustring &username, const Glib::ustring &password) { if(!rooms || !rooms->database) { @@ -57,7 +57,7 @@ namespace dchat } }); - loginWindow.setRegisterHandler([this, windowNotification](const Glib::ustring &username, const Glib::ustring &password) + loginWindow.setRegisterHandler([this](const Glib::ustring &username, const Glib::ustring &password) { if(!rooms || !rooms->database) { @@ -84,13 +84,13 @@ namespace dchat } }); - loginWindow.setRegisterPasswordMismatch([windowNotification] + loginWindow.setRegisterPasswordMismatch([this] { windowNotification->show("Passwords do not match"); }); RoomCallbackFuncs roomCallbackFuncs; - roomCallbackFuncs.connectCallbackFunc = [this, windowNotification](std::shared_ptr rooms, const char *errMsg) + roomCallbackFuncs.connectCallbackFunc = [this](std::shared_ptr rooms, const char *errMsg) { this->rooms = rooms; if(rooms) -- cgit v1.2.3