aboutsummaryrefslogtreecommitdiff
path: root/src/RoomNotificationsWindow.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/RoomNotificationsWindow.cpp')
-rw-r--r--src/RoomNotificationsWindow.cpp105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/RoomNotificationsWindow.cpp b/src/RoomNotificationsWindow.cpp
new file mode 100644
index 0000000..a491840
--- /dev/null
+++ b/src/RoomNotificationsWindow.cpp
@@ -0,0 +1,105 @@
+#include "../include/RoomNotificationsWindow.hpp"
+#include "../include/ChatWindow.hpp"
+#include "../include/Window.hpp"
+#include <gtkmm/treeview.h>
+#include <gtkmm/dialog.h>
+#include <assert.h>
+
+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);
+ 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;
+ }
+} \ No newline at end of file