Browse Source

no reason to keep action flag in the logfile

pull/423/head
apprb 11 years ago
parent
commit
e3d5853ca3
No known key found for this signature in database
GPG Key ID: B001911B5B22FB9B
  1. 25
      src/historykeeper.cpp
  2. 5
      src/historykeeper.h
  3. 9
      src/widget/form/chatform.cpp
  4. 7
      src/widget/widget.cpp

25
src/historykeeper.cpp

@ -82,12 +82,11 @@ HistoryKeeper::HistoryKeeper(const QString &path, bool encr) :
profile_id -- profile ID (resolves from aliases table) profile_id -- profile ID (resolves from aliases table)
chat_id -- current chat ID (resolves from chats table) chat_id -- current chat ID (resolves from chats table)
sender -- sender's ID (resolves from aliases table) sender -- sender's ID (resolves from aliases table)
mtype -- type of message (message, action, etc) (UNUSED now)
message message
*/ */
db.exec(QString("CREATE TABLE IF NOT EXISTS history (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER NOT NULL, ") + db.exec(QString("CREATE TABLE IF NOT EXISTS history (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER NOT NULL, ") +
QString("profile_id INTEGER NOT NULL, chat_id INTEGER NOT NULL, sender INTERGER NOT NULL, mtype INTEGER NOT NULL, message TEXT NOT NULL);")); QString("profile_id INTEGER NOT NULL, chat_id INTEGER NOT NULL, sender INTERGER NOT NULL, message TEXT NOT NULL);"));
db.exec(QString("CREATE TABLE IF NOT EXISTS aliases (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id TEXT UNIQUE NOT NULL);")); db.exec(QString("CREATE TABLE IF NOT EXISTS aliases (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id TEXT UNIQUE NOT NULL);"));
db.exec(QString("CREATE TABLE IF NOT EXISTS chats (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE NOT NULL, ctype INTEGER NOT NULL);")); db.exec(QString("CREATE TABLE IF NOT EXISTS chats (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE NOT NULL, ctype INTEGER NOT NULL);"));
@ -100,16 +99,15 @@ HistoryKeeper::~HistoryKeeper()
db.close(); db.close();
} }
void HistoryKeeper::addChatEntry(const QString& chat, MessageType mt, const QString& message, void HistoryKeeper::addChatEntry(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt)
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)") + db.exec(QString("INSERT INTO history (profile_id, timestamp, chat_id, sender, message)") +
QString("VALUES (%1, %2, %3, %4, %5, '%6');") QString("VALUES (%1, %2, %3, %4, '%5');")
.arg(profile_id).arg(dt.toMSecsSinceEpoch()).arg(chat_id).arg(sender_id).arg(mt).arg(wrapMessage(message))); .arg(profile_id).arg(dt.toMSecsSinceEpoch()).arg(chat_id).arg(sender_id).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,
@ -127,7 +125,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, mtype FROM history INNER JOIN aliases ON history.sender = aliases.id ") + dbAnswer = db.exec(QString("SELECT timestamp, user_id, message FROM history INNER JOIN aliases ON history.sender = aliases.id ") +
QString("AND timestamp BETWEEN %1 AND %2 AND chat_id = %3 AND profile_id = %4;") QString("AND timestamp BETWEEN %1 AND %2 AND chat_id = %3 AND profile_id = %4;")
.arg(time64_from).arg(time64_to).arg(chat_id).arg(profile_id)); .arg(time64_from).arg(time64_to).arg(chat_id).arg(profile_id));
} else { } else {
@ -140,9 +138,8 @@ 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, mt}); res.push_back({sender,message,time});
} }
return res; return res;
@ -267,14 +264,6 @@ int HistoryKeeper::getCurrentProfileID()
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) HistoryKeeper::ChatType HistoryKeeper::convertToChatType(int ct)
{ {
if (ct < 0 || ct > 1) if (ct < 0 || ct > 1)

5
src/historykeeper.h

@ -26,21 +26,19 @@ class HistoryKeeper
{ {
public: public:
enum ChatType {ctSingle = 0, ctGroup}; 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;
}; };
static HistoryKeeper* getInstance(); static HistoryKeeper* getInstance();
static void resetInstance(); static void resetInstance();
virtual ~HistoryKeeper(); virtual ~HistoryKeeper();
void addChatEntry(const QString& chat, MessageType mt, const QString& message, const QString& sender, const QDateTime &dt); void addChatEntry(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); 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);
@ -61,7 +59,6 @@ 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); ChatType convertToChatType(int);
QSqlDatabase db; QSqlDatabase db;

9
src/widget/form/chatform.cpp

@ -91,19 +91,17 @@ 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();
@ -568,12 +566,11 @@ void ChatForm::onLoadHistory()
for (const auto &it : msgs) for (const auto &it : msgs)
{ {
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();
ChatAction *ca = genMessageActionAction(name, it.message, isAction, it.timestamp.toLocalTime()); ChatAction *ca = genMessageActionAction(name, it.message, false, it.timestamp.toLocalTime());
historyMessages.append(ca); historyMessages.append(ca);
} }
previousName = storedPrevName; previousName = storedPrevName;

7
src/widget/widget.cpp

@ -501,8 +501,11 @@ 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::MessageType mt = isAction ? HistoryKeeper::mtAction : HistoryKeeper::mtMessage;
HistoryKeeper::getInstance()->addChatEntry(f->userId, mt, message, f->userId, timestamp); if (isAction)
HistoryKeeper::getInstance()->addChatEntry(f->userId, "/me " + message, f->userId, timestamp);
else
HistoryKeeper::getInstance()->addChatEntry(f->userId, message, f->userId, timestamp);
if (activeChatroomWidget != nullptr) if (activeChatroomWidget != nullptr)
{ {

Loading…
Cancel
Save