-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlconversationmodel.cpp
173 lines (149 loc) · 5.34 KB
/
sqlconversationmodel.cpp
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
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "sqlconversationmodel.h"
#include <QDateTime>
#include <QDebug>
#include <QSqlError>
#include <QSqlRecord>
#include <QSqlQuery>
#include <QEventLoop>
static const char *conversationsTableName = "Conversations";
static void createTable()
{
if (QSqlDatabase::database().tables().contains(conversationsTableName)) {
// The table already exists; we don't need to do anything.
return;
}
QSqlQuery query;
if (!query.exec(
"CREATE TABLE IF NOT EXISTS 'Conversations' ("
// "'id' INTEGER PRIMARY KEY,"
"'author' TEXT NOT NULL,"
"'recipient' TEXT NOT NULL,"
"'timestamp' TEXT NOT NULL,"
"'message' TEXT NOT NULL,"
"FOREIGN KEY('author') REFERENCES Contacts ( name ),"
"FOREIGN KEY('recipient') REFERENCES Contacts ( name )"
")")) {
qFatal("Failed to query database: %s", qPrintable(query.lastError().text()));
}
// query.exec("INSERT INTO Conversations VALUES(1, 'Me', 'huahua', '2016-01-01T11:24:53', 'Hi!')");
query.exec("INSERT INTO Conversations VALUES('huahua', 'Me', '2016-01-07T14:36:16', '我是Qwen。')");
}
SqlConversationModel::SqlConversationModel(QObject *parent) :
QSqlTableModel(parent)
{
m_human_assets = HumanAssets::GetInstance();
createTable();
setTable(conversationsTableName);
setSort(2, Qt::DescendingOrder);
// Ensures that the model is sorted correctly after submitting a new row.
setEditStrategy(QSqlTableModel::OnManualSubmit);
// m_timer = new QTimer(this);
// connect(m_timer, &QTimer::timeout, this, &SqlConversationModel::updateMsg);
connect(this->m_human_assets,SIGNAL(ChatUpdatedSignal()),SLOT(updateMsg()));
connect(this->m_human_assets->m_chat_model, SIGNAL(SignalNewAnswer(QString, bool)), this, SLOT(SlotNewAnswer(QString, bool)));
}
void SqlConversationModel::SlotNewAnswer(QString str, bool end)
{
qDebug() << "SlotNewAnswer end" << end;
m_chatEnded = end;
if (m_chatEnded)
{
return;
}
m_timestamp = QDateTime::currentDateTime().toString(Qt::ISODateWithMs);
qDebug() << "SlotNewAnswer" << str << rowCount() << m_timestamp;
QSqlRecord newRecord = record();
// newRecord.setValue("id", m_chat_round + 1);
newRecord.setValue("author", "huahua");
newRecord.setValue("recipient", "Me");
newRecord.setValue("timestamp", m_timestamp);
if (sub_text_index == 0)
{
m_message_receive = str;
newRecord.setValue("message", m_message_receive);
if (!insertRecord(rowCount(), newRecord)) {
qWarning() << "Failed to send message:" << lastError().text();
return;
}
}
else
{
m_message_receive += str;
newRecord.setValue("message", m_message_receive);
if (!setRecord(0, newRecord)) {
qWarning() << "Failed to send message:" << lastError().text();
return;
}
}
qDebug() << "sssss" << rowCount() << str ;
submitAll();
sub_text_index ++;
}
QString SqlConversationModel::recipient() const
{
return m_recipient;
}
void SqlConversationModel::setRecipient(const QString &recipient)
{
if (recipient == m_recipient)
return;
m_recipient = recipient;
const QString filterString = QString::fromLatin1(
"(recipient = '%1' AND author = 'Me') OR (recipient = 'Me' AND author='%1')").arg(m_recipient);
setFilter(filterString);
select();
emit recipientChanged();
}
QVariant SqlConversationModel::data(const QModelIndex &index, int role) const
{
if (role < Qt::UserRole)
return QSqlTableModel::data(index, role);
const QSqlRecord sqlRecord = record(index.row());
return sqlRecord.value(role - Qt::UserRole);
}
QHash<int, QByteArray> SqlConversationModel::roleNames() const
{
QHash<int, QByteArray> names;
names[Qt::UserRole + 0] = "author";
names[Qt::UserRole + 1] = "recipient";
names[Qt::UserRole + 2] = "timestamp";
names[Qt::UserRole + 3] = "message";
return names;
}
void SqlConversationModel::sendMessage(const QString &recipient, const QString &message)
{
m_chatEnded = false;
m_chat_round = rowCount() + 1;
qDebug() << "sendMessage" << recipient << message << rowCount();
m_timestamp = QDateTime::currentDateTime().toString(Qt::ISODateWithMs);
m_recipient = recipient;
m_message = message;
QSqlRecord newRecord = record();
// newRecord.setValue("id", m_chat_round);
newRecord.setValue("author", "Me");
newRecord.setValue("recipient", recipient);
newRecord.setValue("timestamp", m_timestamp);
newRecord.setValue("message", m_message);
if (!insertRecord(rowCount(), newRecord)) {
qWarning() << "Failed to send message:" << lastError().text();
return;
}
qDebug() << "TTTTTTTT" << recipient << message;
submitAll();
m_human_assets->SlotNewChat(m_message);
sub_text_index = 0;
}
void SqlConversationModel::updateMsg()
{
}
void SqlConversationModel::removeAllMessage()
{
qDebug() << "removeAllMessage" << m_chatEnded;
if (m_chatEnded)
{
removeRows(0, rowCount() - 1);
}
submitAll();
}