aboutsummaryrefslogtreecommitdiff
path: root/src/ChatWindow.cpp
blob: b0bb696ae179b47be3699dd1ae9b2bee16b9f51c (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "../include/ChatWindow.hpp"
#include "../include/ChatMessage.hpp"
#include <gtkmm/alignment.h>
#include <cassert>

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() : 
        channelCount(0),
        lastMessage(nullptr)
    {
        setupTopBar();

        Gtk::Paned *sidePanels = Gtk::manage(new Gtk::Paned());
        sidePanels->set_name("side-panels");
        sidePanels->set_border_width(0);
        attach(*sidePanels, 0, 1, 1, 2);

        setupLeftPanel(sidePanels);

        Gtk::Grid *rightPanel = Gtk::manage(new Gtk::Grid());
        rightPanel->set_hexpand(true);
        sidePanels->add2(*rightPanel);

        setupMessageArea(rightPanel);
        setupChatInput(rightPanel);

        set_vexpand(true);
        set_hexpand(true);
    }

    void ChatWindow::setupTopBar()
    {
        topbar.set_name("top-bar");
        topbar.set_hexpand(true);
        attach(topbar, 0, 0, 2, 1);

        topbarSearchBar.set_size_request(175);
        topbarSearchBar.set_name("top-bar-search");
        topbarSearchBar.set_placeholder_text("Search...");
        topbar.attach(topbarSearchBar, 0, 0, 1, 1);

        Gtk::Alignment *topbarSpacer = Gtk::manage(new Gtk::Alignment());
        topbarSpacer->set_size_request(50);
        topbar.attach_next_to(*topbarSpacer, topbarSearchBar, Gtk::POS_RIGHT, 1, 1);

        currentChannelTitle.set_text("Linux");
        currentChannelTitle.set_name("current-channel-title");
        topbar.attach_next_to(currentChannelTitle, *topbarSpacer, Gtk::POS_RIGHT, 1, 1);
    }

    void ChatWindow::setupLeftPanel(Gtk::Paned *sidePanels)
    {
        Gtk::Paned *leftPanel = Gtk::manage(new Gtk::Paned(Gtk::ORIENTATION_VERTICAL));
        leftPanel->set_size_request(200);
        leftPanel->set_name("left-panel");
        leftPanel->set_vexpand(true);
        sidePanels->add1(*leftPanel);

        leftPanelChannels.set_vexpand(true);
        leftPanel->add1(leftPanelChannels);

        Gtk::Label *channelsTitle = Gtk::manage(new Gtk::Label());
        channelsTitle->set_name("channels-title");
        channelsTitle->set_text("Channels");
        channelsTitle->set_halign(Gtk::ALIGN_START);
        leftPanelChannels.attach(*channelsTitle, 0, 0, 1, 1);

        ////
        leftPanelUsers.set_vexpand(true);
        leftPanel->add2(leftPanelUsers);

        Gtk::Label *usersTitle = Gtk::manage(new Gtk::Label());
        usersTitle->set_name("users-title");
        usersTitle->set_text("Users");
        usersTitle->set_halign(Gtk::ALIGN_START);
        leftPanelUsers.attach(*usersTitle, 0, 0, 1, 1);
    }

    void ChatWindow::setupMessageArea(Gtk::Grid *rightPanel)
    {
        chatArea.set_vexpand(true);
        chatArea.set_policy(Gtk::PolicyType::POLICY_NEVER, Gtk::PolicyType::POLICY_AUTOMATIC);
        rightPanel->attach(chatArea, 0, 0, 1, 2);

        chatAreaLayout.set_name("chat-area-layout");
        chatArea.add(chatAreaLayout);
    }

    void ChatWindow::setupChatInput(Gtk::Grid *rightPanel)
    {
        Gtk::ScrolledWindow *chatScrollWindow = Gtk::manage(new Gtk::ScrolledWindow());
        chatScrollWindow->set_name("chat-scroll-view");
        rightPanel->attach_next_to(*chatScrollWindow, chatArea, Gtk::POS_BOTTOM, 1, 1);

        chatInput.set_hexpand(true);
        chatInput.set_name("chat-input");
        chatInput.set_wrap_mode(Gtk::WrapMode::WRAP_WORD_CHAR);
        chatScrollWindow->add(chatInput);
    }

    void ChatWindow::addChannel(const odhtdb::Hash &nodeHash)
    {
        assert(channelDataById.find(nodeHash) == channelDataById.end());
        fprintf(stderr, "Added channel %s\n", nodeHash.toString().c_str());
        Gtk::ToggleButton *channelButton = Gtk::manage(new Gtk::ToggleButton("Channel name"));
        channelButton->set_active(true);
        channelButton->get_style_context()->add_class("channel-button");
        channelButton->set_hexpand(true);
        channelButton->get_child()->set_halign(Gtk::ALIGN_START);
        channelButton->show();
        leftPanelChannels.attach(*channelButton, 0, 1 + channelCount, 1, 1);
        ++channelCount;
        channelDataById[nodeHash] = { channelButton, 0 };
    }

    void ChatWindow::addLocalMessage(const odhtdb::Hash &channelId, const odhtdb::Signature::PublicKey &userPublicKey, uint32_t timestampSeconds, Glib::ustring msg)
    {
        auto it = channelDataById.find(channelId);
        assert(it != channelDataById.end());
        User *user = getUserByPublicKey(userPublicKey);
        if(!user)
        {
            fprintf(stderr, "Unable to add message to channel because the user %s doesn't exist in it\n", userPublicKey.toString().c_str());
            return;
        }

        if(lastMessage && lastMessage->user->publicKey == userPublicKey)
        {
            int64_t msgTimeDiff = (int64_t)timestampSeconds - (int64_t)lastMessage->timestampSeconds;
            if(msgTimeDiff <= MERGE_MESSAGE_TIMESTAMP_DIFF_SEC)
            {
                lastMessage->text.set_text(lastMessage->text.get_text() + "\n" + msg);
                return;
            }
        }

        ChatMessage *message = Gtk::manage(new ChatMessage(user->name, msg, timestampSeconds, user));
        lastMessage = message;
        message->set_valign(Gtk::Align::ALIGN_START);
        message->set_hexpand(true);
        message->show_all();
        chatAreaLayout.attach(*message, 0, it->second.messageCount, 1, 1);
        ++it->second.messageCount;
    }

    void ChatWindow::addUser(const odhtdb::Signature::PublicKey &userPublicKey)
    {
        User *existingUser = getUserByPublicKey(userPublicKey);
        if(existingUser)
        {
            fprintf(stderr, "ChatWindow::addUser: user %s was not added because the user has already been added\n", userPublicKey.toString().c_str());
            return;
        }

        Gtk::Label *username = Gtk::manage(new Gtk::Label("NoName"));
        username->set_halign(Gtk::ALIGN_START);
        username->show();
        username->get_style_context()->add_class("username-list-username");
        leftPanelUsers.attach(*username, 0, 1 + users.size(), 1, 1);
        users.push_back(new User { userPublicKey, "NoName" });
        fprintf(stderr, "Added user %s\n", userPublicKey.toString().c_str());
    }

    void ChatWindow::setUserNickname(const odhtdb::Signature::PublicKey &userPublicKey, const Glib::ustring &name)
    {
        for(size_t i = 0; i < users.size(); ++i)
        {
            User *user = users[i];
            if(user->publicKey == userPublicKey)
            {
                user->name = name;
                Gtk::Widget *usernameLabel = leftPanelUsers.get_child_at(0, 1 + i);
                assert(usernameLabel);
                //assert(usernameLabel->get_type() == Gtk::Label::get_type());
                static_cast<Gtk::Label*>(usernameLabel)->set_text(name);
                fprintf(stderr, "Set nickname for user %s to %s\n", userPublicKey.toString().c_str(), name.c_str());
                return;
            }
        }
        fprintf(stderr, "ChatWindow::setUserNickname: user %s doesn't exist\n", userPublicKey.toString().c_str());
    }

    User* ChatWindow::getUserByPublicKey(const odhtdb::Signature::PublicKey &publicKey) const
    {
        for(User *user : users)
        {
            if(user->publicKey == publicKey)
            {
                return user;
            }
        }
        return nullptr;
    }
}