#include "../include/RoomNotificationsWindow.hpp" #include "../include/ChatWindow.hpp" #include "../include/Window.hpp" #include #include #include namespace dchat { RoomNotificationsWindow::RoomNotificationsWindow(ChatWindow *_chatWindow) : chatWindow(_chatWindow) { set_vexpand(true); set_hexpand(true); set_border_width(25); } RoomNotificationsWindow::RoomNotifications* RoomNotificationsWindow::createRoomNotifications() { RoomNotifications *roomNotifications = Gtk::manage(new RoomNotifications()); roomNotifications->columns.add(roomNotifications->userPublicKeyColumn); roomNotifications->columns.add(roomNotifications->messageColumn); roomNotifications->listStore = Gtk::ListStore::create(roomNotifications->columns); Gtk::TreeView *treeView = Gtk::manage(new Gtk::TreeView(roomNotifications->listStore)); treeView->append_column("Public key", roomNotifications->userPublicKeyColumn); treeView->append_column("Message", roomNotifications->messageColumn); treeView->set_reorderable(); //treeView->set_rules_hint(); treeView->set_headers_visible(); treeView->set_headers_clickable(); treeView->set_activate_on_single_click(false); treeView->get_selection()->set_mode(Gtk::SelectionMode::SELECTION_SINGLE); treeView->signal_row_activated().connect([roomNotifications, treeView, this](const Gtk::TreeModel::Path &path, Gtk::TreeViewColumn *column) { if(path.empty()) return; auto selectedRow = treeView->get_selection()->get_selected(); Glib::ustring userPublicKeyStr = selectedRow->get_value(roomNotifications->userPublicKeyColumn); assert(roomNotifications->inviteRequests.find(userPublicKeyStr) != roomNotifications->inviteRequests.end()); InviteUserRequest &request = roomNotifications->inviteRequests[userPublicKeyStr]; Gtk::Dialog dialog; dialog.set_title("Add user to room"); Glib::ustring msg = "Are you sure you want to add the user "; msg += userPublicKeyStr; msg += " to the room "; msg += request.room->name + " (" + request.room->id->toString() + ") ?"; dialog.get_content_area()->pack_start(*Gtk::manage(new Gtk::Label(msg))); dialog.add_button("Yes", Gtk::RESPONSE_YES); dialog.add_button("No", Gtk::RESPONSE_NO); dialog.show_all(); switch(dialog.run()) { case Gtk::RESPONSE_YES: { assert(!request.room->groups.empty()); // TODO: Add user to a guest group instead try { request.room->addUser(request.userPublicKey, request.room->groups[0]); roomNotifications->listStore->erase(selectedRow); } catch(std::exception &e) { chatWindow->window->windowNotification->show(Glib::ustring("Failed to add user to room, reason: ") + e.what()); } } default: break; } }); roomNotifications->add(*treeView); roomNotifications->set_overlay_scrolling(false); //roomNotifications->set_size_request(640, 480); //roomNotifications->set_valign(Gtk::ALIGN_START); //roomNotifications->set_halign(Gtk::ALIGN_CENTER); return roomNotifications; } void RoomNotificationsWindow::addInviteRequest(const InviteUserRequest &request) { RoomNotifications *roomNotifications = nullptr; auto it = roomNotificationsMap.find(request.room.get()); if(it == roomNotificationsMap.end()) { roomNotifications = createRoomNotifications(); add(*roomNotifications, request.room->id->toString()); roomNotificationsMap[request.room.get()] = roomNotifications; } else roomNotifications = it->second; std::string userPublicKeyStr = request.userPublicKey.toString(); if(roomNotifications->inviteRequests.find(userPublicKeyStr) != roomNotifications->inviteRequests.end()) { fprintf(stderr, "Got duplicate invite request from user %s for room %s\n", userPublicKeyStr.c_str(), request.room->id->toString().c_str()); return; } roomNotifications->inviteRequests[userPublicKeyStr] = request; Gtk::TreeModel::Row row = *roomNotifications->listStore->append(); row[roomNotifications->userPublicKeyColumn] = userPublicKeyStr; row[roomNotifications->messageColumn] = request.message; } }