1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

correct action saving into the logfile

This commit is contained in:
apprb 2014-10-14 22:43:03 +09:00
parent 14a71a0f3f
commit ec3d8146c6
No known key found for this signature in database
GPG Key ID: B001911B5B22FB9B
4 changed files with 40 additions and 11 deletions

View File

@ -100,14 +100,16 @@ HistoryKeeper::~HistoryKeeper()
db.close();
}
void HistoryKeeper::addChatEntry(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt)
void HistoryKeeper::addChatEntry(const QString& chat, MessageType mt, const QString& message,
const QString& sender, const QDateTime &dt)
{
int chat_id = getChatID(chat, ctSingle).first;
int sender_id = getAliasID(sender);
int profile_id = getCurrentProfileID();
db.exec(QString("INSERT INTO history (profile_id, timestamp, chat_id, sender, mtype, message) VALUES (%1, %2, %3, %4, 0, '%5');")
.arg(profile_id).arg(dt.toMSecsSinceEpoch()).arg(chat_id).arg(sender_id).arg(wrapMessage(message)));
db.exec(QString("INSERT INTO history (profile_id, timestamp, chat_id, sender, mtype, message)") +
QString("VALUES (%1, %2, %3, %4, %5, '%6');")
.arg(profile_id).arg(dt.toMSecsSinceEpoch()).arg(chat_id).arg(sender_id).arg(mt).arg(wrapMessage(message)));
}
QList<HistoryKeeper::HistMessage> HistoryKeeper::getChatHistory(HistoryKeeper::ChatType ct, const QString &profile,
@ -126,7 +128,7 @@ QList<HistoryKeeper::HistMessage> HistoryKeeper::getChatHistory(HistoryKeeper::C
QSqlQuery dbAnswer;
if (ct == ctSingle)
{
dbAnswer = db.exec(QString("SELECT timestamp, user_id, message FROM history INNER JOIN aliases ON history.sender = aliases.id ") +
dbAnswer = db.exec(QString("SELECT timestamp, user_id, message, mtype FROM history INNER JOIN aliases ON history.sender = aliases.id ") +
QString("AND timestamp BETWEEN %1 AND %2 AND chat_id = %3;").arg(time64_from).arg(time64_to).arg(chat_id));
} else {
// no groupchats yet
@ -138,8 +140,9 @@ QList<HistoryKeeper::HistMessage> HistoryKeeper::getChatHistory(HistoryKeeper::C
QString message = unWrapMessage(dbAnswer.value(2).toString());
qint64 timeInt = dbAnswer.value(0).toLongLong();
QDateTime time = QDateTime::fromMSecsSinceEpoch(timeInt);
MessageType mt = convertToMessageType(dbAnswer.value(3).toInt());
res.push_back({sender,message,time});
res.push_back({sender,message,time, mt});
}
return res;
@ -168,7 +171,7 @@ void HistoryKeeper::updateChatsID()
{
QString name = dbAnswer.value(1).toString();
int id = dbAnswer.value(0).toInt();
ChatType ctype = static_cast<ChatType>(dbAnswer.value(2).toInt());
ChatType ctype = convertToChatType(dbAnswer.value(2).toInt());
chats[name] = {id, ctype};
}
@ -263,3 +266,19 @@ int HistoryKeeper::getCurrentProfileID()
// for many profiles
return getAliasID(Core::getInstance()->getSelfId().publicKey);
}
HistoryKeeper::MessageType HistoryKeeper::convertToMessageType(int mt)
{
if (mt < 0 || mt > 1)
return mtMessage;
return static_cast<MessageType>(mt);
}
HistoryKeeper::ChatType HistoryKeeper::convertToChatType(int ct)
{
if (ct < 0 || ct > 1)
return ctSingle;
return static_cast<ChatType>(ct);
}

View File

@ -25,19 +25,22 @@
class HistoryKeeper
{
public:
enum ChatType {ctSingle = 0, ctGroup};
enum MessageType {mtMessage = 0, mtAction};
struct HistMessage
{
QString sender;
QString message;
QDateTime timestamp;
MessageType mt;
};
enum ChatType {ctSingle = 0, ctGroup};
static HistoryKeeper* getInstance();
static void resetInstance();
virtual ~HistoryKeeper();
void addChatEntry(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt);
void addChatEntry(const QString& chat, MessageType mt, const QString& message, const QString& sender, const QDateTime &dt);
void addGroupChatEntry(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt);
QList<HistMessage> getChatHistory(ChatType ct, const QString &profile, const QString &chat,
const QDateTime &time_from, const QDateTime &time_to);
@ -58,6 +61,9 @@ private:
QString unWrapMessage(const QString &str);
bool dumpDBtoFile(const QString &fname);
MessageType convertToMessageType(int);
ChatType convertToChatType(int);
QSqlDatabase db;
QMap<QString, int> aliases;
QMap<QString, QPair<int, ChatType>> chats;

View File

@ -91,16 +91,19 @@ void ChatForm::onSendTriggered()
return;
QString name = Widget::getInstance()->getUsername();
QDateTime timestamp = QDateTime::currentDateTime();
HistoryKeeper::getInstance()->addChatEntry(f->userId, msg, Core::getInstance()->getSelfId().publicKey, timestamp);
if (msg.startsWith("/me "))
{
msg = msg.right(msg.length() - 4);
addMessage(name, msg, true, timestamp);
HistoryKeeper::getInstance()->addChatEntry(f->userId, HistoryKeeper::mtAction, msg,
Core::getInstance()->getSelfId().publicKey, timestamp);
emit sendAction(f->friendId, msg);
}
else
{
addMessage(name, msg, false, timestamp);
HistoryKeeper::getInstance()->addChatEntry(f->userId, HistoryKeeper::mtMessage, msg,
Core::getInstance()->getSelfId().publicKey, timestamp);
emit sendMessage(f->friendId, msg);
}
msgEdit->clear();
@ -565,7 +568,7 @@ void ChatForm::onLoadHistory()
for (const auto &it : msgs)
{
bool isAction = false;
bool isAction = (it.mt == HistoryKeeper::mtAction);
QString name = f->getName();
if (it.sender == Core::getInstance()->getSelfId().publicKey)
name = Core::getInstance()->getUsername();

View File

@ -501,7 +501,8 @@ void Widget::onFriendMessageReceived(int friendId, const QString& message, bool
QDateTime timestamp = QDateTime::currentDateTime();
f->chatForm->addMessage(f->getName(), message, isAction, timestamp);
HistoryKeeper::getInstance()->addChatEntry(f->userId, message, f->userId, timestamp);
HistoryKeeper::MessageType mt = isAction ? HistoryKeeper::mtAction : HistoryKeeper::mtMessage;
HistoryKeeper::getInstance()->addChatEntry(f->userId, mt, message, f->userId, timestamp);
if (activeChatroomWidget != nullptr)
{