-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClientList.h
156 lines (146 loc) · 4.24 KB
/
ClientList.h
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
#ifndef CLIENTLIST_H
#define CLIENTLIST_H
#include <QObject>
#include <QString>
#include <QStringList>
#include <QtNetwork/QTcpSocket>
#include <QtSql>
#include <QTableView>
#include "ChatMessages.h"
#include "logger.h"
/*
TODO for future:
*/
class ChatClient
{
private:
QString m_username;
QString m_userInfo;
QString m_userPassword;
QString m_userState;
QTcpSocket *m_userSocket;
public:
ChatClient(): m_userSocket(NULL) {}
QString &username();
QString &userInfo();
QString &password();
QString &userState();
QTcpSocket *userSocket() const;
void setUsername(QString name);
void setUserInfo(QString info);
void setPassword(QString pass);
void setUserState(QString state);
void setUserSocket(QTcpSocket *socket);
};
class ChatChannel
{
private:
QString m_name;
QString m_description;
QString m_topic;
public:
QStringList userList;
// should be private. but i couldn't find a nice way to provide
// an interface to iterate all the clients in the channel
ChatChannel() {}
QString &name();
void setName(QString name);
QString &description();
void setDescription(QString desc);
QString &topic();
void setTopic(QString topic);
void addClient(QString username);
void deleteClient(QString username);
bool hasClient(QString username);
void deleteAllClients();
};
class DBManager: public QObject
{
Q_OBJECT
private:
QSqlDatabase m_DB;
QString m_DBName;
const QString m_clientTableName;
const QString m_channelTableName;
const QString m_membershipTableName;
const QString m_DBFileName;
bool createClientsTable();
bool createChannelsTable();
bool createMembershipTable();
public:
explicit DBManager(QObject *parent = 0);
~DBManager();
void connectToDB();
void disconnectDB();
//general methods
void createDB();
// client table
bool hasClient(QString username);
ChatClient getClient(QString username);
void setClient(ChatClient &client);
// channel table
bool hasChannel(QString channelName);
ChatChannel getChannel(QString channelName);
void setChannel(ChatChannel &channel);
//membership table
bool isMembership(QString username, QString channelName);
void addMembership(QString username, QString channelName);
void deleteMembership(QString username, QString channel);
// methods for initializing channels in general client list
QVector<ChatChannel> getChannelList();
signals:
void logMessage(ErrorStatus, QString&);
};
class GeneralClientList: public QObject
{
Q_OBJECT
private:
QMap<QString, ChatClient> m_generalClientList;
QVector<ChatChannel> m_channelList;
DBManager m_DB;
public:
typedef QVector<QTcpSocket *> userSocketsList_t;
typedef QVectorIterator<QTcpSocket *> userSocketsListIterator_t;
enum AuthResult
{
arAllreadyAuthorized,
arAuthSuccess,
arWrongAuthData
};
enum RegResult
{
rrBadUsername,
rrOccupiedUsername,
rrRegSuccess
};
enum CreateChannelResult
{
ccrTooManyChannels,
ccrBadName,
ccrSuccess
};
explicit GeneralClientList(QObject *parent = 0);
void readChannelsFromDB();
ChatChannel getChannel(QString &channelName);
ChatClient getClient(const QString &username);
bool hasClient(QString username);
bool hasChannel(QString channelName);
QMap<QString, QString> getChannelsForClient(QString username);
QStringList getClientsForChannel(QString channelName);
QMap<QString, QString> getAllChanells();
RegResult registrate(QString username, QString password);
AuthResult authorize(QString username, QString password, QTcpSocket *socket);
void disconnect(QString username);
void joinChannel(QString username, QString channelName);
void leaveChannel(QString username, QString channelName);
void disconnectAll();
userSocketsList_t *getAllSockets();
CreateChannelResult createChannel(QString channelName, QString description, QString topic);
void updateClient(ChatClient &client);
void updateChannel(ChatChannel &channel);
signals:
void logMessage(ErrorStatus, QString &);
private slots:
void replyLog(ErrorStatus, QString ¶m);
};
#endif // CLIENTLIST_H