aboutsummaryrefslogtreecommitdiff
path: root/src/RoomNotificationsWindow.cpp
blob: a491840c669b7a3c38b7b7c3832fc2ea56f228a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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;
    }
}