chatroom.h 1.84 KB
#ifndef CHATROOM_H
#define CHATROOM_H

#include <QMainWindow>
#include <QModelIndex>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <map>
#include "loginscreen.h"
#include "chatwindow.h"
#include "Socket.h"

#define MSG_USER   0
#define MSG_STATUS 1
#define MSG_SERVER 2

namespace Ui {
class Chatroom;
}

class Chatroom : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit Chatroom(QWidget *parent = 0);
    bool getConnected();
    QString getNickname();
    void setNickname(QString nick);
    void updateUserList(QString userlist);
    void relayMsg(string msg);
    void removeChat(QString &nickname);
    void putMsgToPrintQueue(string& msg, int type);
    void putMsgToSendQueue(QString& msg);
    ~Chatroom();

private:
    Ui::Chatroom *ui;
    Socket s;
    bool connected;
    std::thread *send;
    std::thread *recv;
    QString nickname;
    list<string> printQueue;
    list<string> printServerQueue;
    list<string> printStatusQueue;
    map<string,void*> activeChats;
    map<string,void*> inactiveChats;
    std::mutex chatMutex;
    unique_lock<std::mutex>* chatLock;
    std::condition_variable chatCreated;
    string getSender(string msg);
    void freeChats(bool active);

protected:
    void closeEvent(QCloseEvent *event);

public slots:
    void startSession();
    void sendMsg();
    void disconnectChatroom();
    void launchChatWindow(QModelIndex index);
    void setConnected(bool status);

private slots:
    void printMsg();
    void printServerMsg();
    void printStatusMsg();
    void newChat(QString peerNick);

signals:
    void messagesToPrint();
    void serverMessagesToPrint();
    void statusMessagesToPrint();
    void createChat(QString sender);
    void toggleConnected(bool status);

};

void sendThread(Socket* s, Chatroom *chat);
void recvThread(Socket *s, Chatroom *chat);

#endif // CHATROOM_H