forked from foldynl/QLog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKSTChat.h
184 lines (148 loc) · 4.12 KB
/
KSTChat.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
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
#ifndef QLOG_CORE_KSTCHAT_H
#define QLOG_CORE_KSTCHAT_H
#include <QObject>
#include <QTcpSocket>
#include "core/Gridsquare.h"
#include "data/Dxcc.h"
#include "ui/NewContactWidget.h"
struct KSTChatMsg
{
QString time;
QString sender;
QString message;
Gridsquare grid;
QStringList matchedHighlightRules;
};
Q_DECLARE_METATYPE(KSTChatMsg);
struct KSTUsersInfo
{
QString callsign; // do not use Callsign class because KST users a free text here
Gridsquare grid;
QString stationComment;
DxccEntity dxcc;
DxccStatus status;
qulonglong dupeCount = 0;
};
Q_DECLARE_METATYPE(KSTUsersInfo);
class chatHighlightRule : public QObject
{
Q_OBJECT
public:
enum InfoSource {
SENDER = 0,
MESSAGE = 1,
GRIDSQUARE = 2
};
enum Operator {
OPERATOR_CONTAINS = 0,
OPERATOR_STARTWITH = 1
};
enum InterConditionOperand{
OPERAND_AND = 0,
OPERAND_OR = 1
};
struct Condition
{
InfoSource source;
Operator operatorID;
QString value;
};
public:
explicit chatHighlightRule(QObject *parent = nullptr);
~chatHighlightRule(){};
bool save();
bool load(const QString &);
bool match(const int roomIndex,
const KSTChatMsg &msg) const;
QString ruleName;
bool enabled;
int ruleRoomIndex;
InterConditionOperand interConditionOperand;
QList<Condition> conditions;
bool ruleValid;
private:
void fromJson(const QJsonDocument &ruleDefinition);
QByteArray toJson();
};
class chatHighlightEvaluator : public QObject
{
Q_OBJECT
public:
explicit chatHighlightEvaluator(const int roomIndex,
QObject *parent = nullptr);
~chatHighlightEvaluator() { clearRules();}
void clearRules();
static QStringList getAllRuleNames();
public slots:
void loadRules();
bool shouldHighlight(const KSTChatMsg &msg,
QStringList &matchedRules);
private:
QList<chatHighlightRule *>ruleList;
int roomIndex;
};
class KSTChat : public QObject
{
Q_OBJECT
public:
const static QStringList chatRooms;
explicit KSTChat(int chatRoomIndex,
const QString &username,
const QString &password,
const NewContactWidget *contact,
QObject *parent = nullptr);
~KSTChat();
QList<KSTUsersInfo> getUsersList() const;
KSTUsersInfo getUserInfo(const QString& username) const;
static const QString getUsername();
static const QString getPassword();
static void saveUsernamePassword(const QString&, const QString&);
public slots:
void connectChat();
void disconnectChat();
void sendMessage(const QString&);
void reloadStationProfile();
void resetDupe();
void recalculateDupe();
void recalculateDxccStatus();
void updateSpotsStatusWhenQSOAdded(const QSqlRecord &record);
void updateSpotsStatusWhenQSODeleted(const QSqlRecord &record);
void updateSpotsDxccStatusWhenQSODeleted(const QSet<uint> &entities);
private slots:
void receiveData();
void socketConnected();
void socketError(QAbstractSocket::SocketError socker_error);
signals:
void chatConnected();
void chatDisconnected();
void chatError(QString);
void chatMsg(KSTChatMsg);
void usersListUpdated();
private:
enum Command
{
NO_CMD = 0,
LOGIN_CMD = 1,
USER_CMD = 2,
SHOW_USERS_CMD = 3,
SET_GRID_CMD = 4
};
int chatRoomIdx;
QString userName;
QString password;
QTcpSocket* socket;
Command currCommand;
QString receiveBuffer;
QStringList commandLineBuffer;
void sendShowUsersCommand();
void sendCommand(const Command&, const QString&);
void sendSetGridCommand();
void finalizeShowUsersCommand(const QStringList&);
QStringList joinLines(const QByteArray &data);
QList<KSTUsersInfo> userList;
QList<QPair<Command, QString>> commandQueue;
const NewContactWidget *contact;
static const QString SECURE_STORAGE_KEY;
static const QString CONFIG_USERNAME_KEY;
};
#endif // QLOG_CORE_KSTCHAT_H