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(); 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 chat_id = getChatID(chat, ctSingle).first;
int sender_id = getAliasID(sender); int sender_id = getAliasID(sender);
int profile_id = getCurrentProfileID(); 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');") db.exec(QString("INSERT INTO history (profile_id, timestamp, chat_id, sender, mtype, message)") +
.arg(profile_id).arg(dt.toMSecsSinceEpoch()).arg(chat_id).arg(sender_id).arg(wrapMessage(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, QList<HistoryKeeper::HistMessage> HistoryKeeper::getChatHistory(HistoryKeeper::ChatType ct, const QString &profile,
@ -126,7 +128,7 @@ QList<HistoryKeeper::HistMessage> HistoryKeeper::getChatHistory(HistoryKeeper::C
QSqlQuery dbAnswer; QSqlQuery dbAnswer;
if (ct == ctSingle) 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)); QString("AND timestamp BETWEEN %1 AND %2 AND chat_id = %3;").arg(time64_from).arg(time64_to).arg(chat_id));
} else { } else {
// no groupchats yet // no groupchats yet
@ -138,8 +140,9 @@ QList<HistoryKeeper::HistMessage> HistoryKeeper::getChatHistory(HistoryKeeper::C
QString message = unWrapMessage(dbAnswer.value(2).toString()); QString message = unWrapMessage(dbAnswer.value(2).toString());
qint64 timeInt = dbAnswer.value(0).toLongLong(); qint64 timeInt = dbAnswer.value(0).toLongLong();
QDateTime time = QDateTime::fromMSecsSinceEpoch(timeInt); 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; return res;
@ -168,7 +171,7 @@ void HistoryKeeper::updateChatsID()
{ {
QString name = dbAnswer.value(1).toString(); QString name = dbAnswer.value(1).toString();
int id = dbAnswer.value(0).toInt(); 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}; chats[name] = {id, ctype};
} }
@ -263,3 +266,19 @@ int HistoryKeeper::getCurrentProfileID()
// for many profiles // for many profiles
return getAliasID(Core::getInstance()->getSelfId().publicKey); 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 class HistoryKeeper
{ {
public: public:
enum ChatType {ctSingle = 0, ctGroup};
enum MessageType {mtMessage = 0, mtAction};
struct HistMessage struct HistMessage
{ {
QString sender; QString sender;
QString message; QString message;
QDateTime timestamp; QDateTime timestamp;
MessageType mt;
}; };
enum ChatType {ctSingle = 0, ctGroup};
static HistoryKeeper* getInstance(); static HistoryKeeper* getInstance();
static void resetInstance(); static void resetInstance();
virtual ~HistoryKeeper(); 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); 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, QList<HistMessage> getChatHistory(ChatType ct, const QString &profile, const QString &chat,
const QDateTime &time_from, const QDateTime &time_to); const QDateTime &time_from, const QDateTime &time_to);
@ -58,6 +61,9 @@ private:
QString unWrapMessage(const QString &str); QString unWrapMessage(const QString &str);
bool dumpDBtoFile(const QString &fname); bool dumpDBtoFile(const QString &fname);
MessageType convertToMessageType(int);
ChatType convertToChatType(int);
QSqlDatabase db; QSqlDatabase db;
QMap<QString, int> aliases; QMap<QString, int> aliases;
QMap<QString, QPair<int, ChatType>> chats; QMap<QString, QPair<int, ChatType>> chats;

View File

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

View File

@ -501,7 +501,8 @@ void Widget::onFriendMessageReceived(int friendId, const QString& message, bool
QDateTime timestamp = QDateTime::currentDateTime(); QDateTime timestamp = QDateTime::currentDateTime();
f->chatForm->addMessage(f->getName(), message, isAction, timestamp); 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) if (activeChatroomWidget != nullptr)
{ {