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

History: retrieve history from save

This commit is contained in:
apprb 2014-10-11 02:32:10 +09:00
parent 6860790205
commit 625cb32b32
No known key found for this signature in database
GPG Key ID: B001911B5B22FB9B
12 changed files with 159 additions and 103 deletions

View File

@ -147,6 +147,7 @@ QTextTable *ChatAreaWidget::getMsgTable()
QTextCursor tc = textCursor(); QTextCursor tc = textCursor();
tc.movePosition(QTextCursor::End); tc.movePosition(QTextCursor::End);
QTextTable *chatTextTable = tc.insertTable(1, 5, *tableFrmt); QTextTable *chatTextTable = tc.insertTable(1, 5, *tableFrmt);
return chatTextTable; return chatTextTable;
@ -183,3 +184,8 @@ void ChatAreaWidget::clearChatArea()
insertMessage(message); insertMessage(message);
} }
} }
void ChatAreaWidget::rerenderContent()
{
}

View File

@ -30,6 +30,8 @@ public:
explicit ChatAreaWidget(QWidget *parent = 0); explicit ChatAreaWidget(QWidget *parent = 0);
virtual ~ChatAreaWidget(); virtual ~ChatAreaWidget();
void insertMessage(ChatAction *msgAction); void insertMessage(ChatAction *msgAction);
QList<ChatAction*>& getMesages() {return messages;}
void rerenderContent();
int nameColWidth() {return nameWidth;} int nameColWidth() {return nameWidth;}
void setNameColWidth(int w); void setNameColWidth(int w);

View File

@ -90,6 +90,7 @@ void ChatForm::onSendTriggered()
if (msg.isEmpty()) if (msg.isEmpty())
return; return;
QString name = Widget::getInstance()->getUsername(); QString name = Widget::getInstance()->getUsername();
HistoryKeeper::getInstance()->addChatEntry(f->userId, msg, Core::getInstance()->getSelfId().publicKey);
if (msg.startsWith("/me ")) if (msg.startsWith("/me "))
{ {
msg = msg.right(msg.length() - 4); msg = msg.right(msg.length() - 4);
@ -102,8 +103,6 @@ void ChatForm::onSendTriggered()
emit sendMessage(f->friendId, msg); emit sendMessage(f->friendId, msg);
} }
msgEdit->clear(); msgEdit->clear();
HistoryKeeper::getInstance()->addChatEntry(f->userId, msg, Core::getInstance()->getSelfId().publicKey);
} }
void ChatForm::onAttachClicked() void ChatForm::onAttachClicked()
@ -540,10 +539,52 @@ void ChatForm::onLoadHistory()
if (dlg.exec()) if (dlg.exec())
{ {
QDateTime fromTime = dlg.getFromDate(); QDateTime fromTime = dlg.getFromDate();
QDateTime toTime = dlg.getToDate(); QDateTime toTime = QDateTime::currentDateTime();
if (fromTime > toTime)
return;
if (earliestMessage)
{
if (*earliestMessage < fromTime)
return;
if (*earliestMessage < toTime)
toTime = *earliestMessage;
}
fromTime = fromTime.toUTC();
toTime = toTime.toUTC();
auto msgs = HistoryKeeper::getInstance()->getChatHistory(HistoryKeeper::ctSingle, Core::getInstance()->getSelfId().publicKey, auto msgs = HistoryKeeper::getInstance()->getChatHistory(HistoryKeeper::ctSingle, Core::getInstance()->getSelfId().publicKey,
f->userId, fromTime, toTime); f->userId, fromTime, toTime);
// do smth with messages
QString storedPrevName = previousName;
previousName = "";
QList<ChatAction*> historyMessages;
for (const auto &it : msgs)
{
bool isAction = false;
QString name = f->getName();
if (it.sender == Core::getInstance()->getSelfId().publicKey)
name = Core::getInstance()->getUsername();
ChatAction *ca = genMessageActionAction(name, it.message, isAction, it.timestamp.toLocalTime());
historyMessages.append(ca);
}
previousName = storedPrevName;
for (ChatAction *ca : chatWidget->getMesages())
historyMessages.append(ca);
int savedSliderPos = chatWidget->verticalScrollBar()->maximum() - chatWidget->verticalScrollBar()->value();
chatWidget->getMesages().clear();
chatWidget->clear();
for (ChatAction *ca : historyMessages)
chatWidget->insertMessage(ca);
savedSliderPos = chatWidget->verticalScrollBar()->maximum() - savedSliderPos;
chatWidget->verticalScrollBar()->setValue(savedSliderPos);
} }
} }

View File

@ -30,7 +30,8 @@
#include "widget/maskablepixmapwidget.h" #include "widget/maskablepixmapwidget.h"
GenericChatForm::GenericChatForm(QWidget *parent) : GenericChatForm::GenericChatForm(QWidget *parent) :
QWidget(parent) QWidget(parent),
earliestMessage(nullptr)
{ {
curRow = 0; curRow = 0;
@ -124,7 +125,7 @@ GenericChatForm::GenericChatForm(QWidget *parent) :
emoteButton->setAttribute(Qt::WA_LayoutUsesWidgetRect); emoteButton->setAttribute(Qt::WA_LayoutUsesWidgetRect);
menu.addAction(tr("Save chat log"), this, SLOT(onSaveLogClicked())); menu.addAction(tr("Save chat log"), this, SLOT(onSaveLogClicked()));
menu.addAction(tr("Clear displayed messages"), this, SLOT(clearChatArea())); menu.addAction(tr("Clear displayed messages"), this, SLOT(clearChatArea(bool)));
menu.addSeparator(); menu.addSeparator();
connect(emoteButton, SIGNAL(clicked()), this, SLOT(onEmoteButtonClicked())); connect(emoteButton, SIGNAL(clicked()), this, SLOT(onEmoteButtonClicked()));
@ -169,29 +170,10 @@ void GenericChatForm::onSaveLogClicked()
file.close(); file.close();
} }
void GenericChatForm::addMessage(QString author, QString message, bool isAction, QDateTime datetime) void GenericChatForm::addMessage(const QString &author, const QString &message, bool isAction, const QDateTime &datetime)
{ {
QString date = datetime.toString(Settings::getInstance().getTimestampFormat()); ChatAction *ca = genMessageActionAction(author, message, isAction, datetime);
bool isMe = (author == Widget::getInstance()->getUsername()); chatWidget->insertMessage(ca);
if (!isAction && message.startsWith("/me "))
{ // always render actions regardless of what core thinks
isAction = true;
message = message.right(message.length()-4);
}
if (isAction)
{
chatWidget->insertMessage(new ActionAction (getElidedName(author), message, date, isMe));
previousName = ""; // next msg has a name regardless
return;
}
else if (previousName == author)
chatWidget->insertMessage(new MessageAction("", message, date, isMe));
else
chatWidget->insertMessage(new MessageAction(getElidedName(author), message, date, isMe));
previousName = author;
} }
void GenericChatForm::onEmoteButtonClicked() void GenericChatForm::onEmoteButtonClicked()
@ -228,10 +210,8 @@ void GenericChatForm::focusInput()
void GenericChatForm::addSystemInfoMessage(const QString &message, const QString &type, const QDateTime &datetime) void GenericChatForm::addSystemInfoMessage(const QString &message, const QString &type, const QDateTime &datetime)
{ {
previousName = ""; ChatAction *ca = genSystemInfoAction(message, type, datetime);
QString date = datetime.toString(Settings::getInstance().getTimestampFormat()); chatWidget->insertMessage(ca);
chatWidget->insertMessage(new SystemMessageAction(message, type, date));
} }
QString GenericChatForm::getElidedName(const QString& name) QString GenericChatForm::getElidedName(const QString& name)
@ -242,9 +222,58 @@ QString GenericChatForm::getElidedName(const QString& name)
return fm.elidedText(name, Qt::ElideRight, chatWidget->nameColWidth()); return fm.elidedText(name, Qt::ElideRight, chatWidget->nameColWidth());
} }
void GenericChatForm::clearChatArea() void GenericChatForm::clearChatArea(bool notinform)
{ {
chatWidget->clearChatArea(); chatWidget->clearChatArea();
previousName = ""; previousName = "";
addSystemInfoMessage(tr("Cleared"), "green");
if (!notinform)
addSystemInfoMessage(tr("Cleared"), "green");
if (earliestMessage)
{
delete earliestMessage;
earliestMessage = nullptr;
}
}
ChatAction* GenericChatForm::genMessageActionAction(const QString &author, QString message,
bool isAction, const QDateTime &datetime)
{
if (earliestMessage == nullptr)
{
earliestMessage = new QDateTime(datetime);
}
QString date = datetime.toString(Settings::getInstance().getTimestampFormat());
bool isMe = (author == Widget::getInstance()->getUsername());
if (!isAction && message.startsWith("/me "))
{ // always render actions regardless of what core thinks
isAction = true;
message = message.right(message.length()-4);
}
if (isAction)
{
previousName = ""; // next msg has a name regardless
return (new ActionAction (getElidedName(author), message, date, isMe));
}
ChatAction *res;
if (previousName == author)
res = (new MessageAction("", message, date, isMe));
else
res = (new MessageAction(getElidedName(author), message, date, isMe));
previousName = author;
return res;
}
ChatAction* GenericChatForm::genSystemInfoAction(const QString &message, const QString &type, const QDateTime &datetime)
{
previousName = "";
QString date = datetime.toString(Settings::getInstance().getTimestampFormat());
return (new SystemMessageAction(message, type, date));
} }

View File

@ -32,6 +32,7 @@ class CroppingLabel;
class ChatTextEdit; class ChatTextEdit;
class ChatAreaWidget; class ChatAreaWidget;
class MaskablePixmapWidget; class MaskablePixmapWidget;
class ChatAction;
namespace Ui { namespace Ui {
class MainWindow; class MainWindow;
@ -45,7 +46,8 @@ public:
virtual void setName(const QString &newName); virtual void setName(const QString &newName);
virtual void show(Ui::MainWindow &ui); virtual void show(Ui::MainWindow &ui);
void addMessage(QString author, QString message, bool isAction = false, QDateTime datetime=QDateTime::currentDateTime()); void addMessage(const QString &author, const QString &message, bool isAction = false,
const QDateTime &datetime=QDateTime::currentDateTime());
void addSystemInfoMessage(const QString &message, const QString &type, const QDateTime &datetime=QDateTime::currentDateTime()); void addSystemInfoMessage(const QString &message, const QString &type, const QDateTime &datetime=QDateTime::currentDateTime());
signals: signals:
@ -60,10 +62,14 @@ protected slots:
void onSaveLogClicked(); void onSaveLogClicked();
void onEmoteButtonClicked(); void onEmoteButtonClicked();
void onEmoteInsertRequested(QString str); void onEmoteInsertRequested(QString str);
void clearChatArea(); void clearChatArea(bool);
protected: protected:
QString getElidedName(const QString& name); QString getElidedName(const QString& name);
ChatAction* genMessageActionAction(const QString &author, QString message, bool isAction = false,
const QDateTime &datetime=QDateTime::currentDateTime());
ChatAction* genSystemInfoAction(const QString &message, const QString &type,
const QDateTime &datetime=QDateTime::currentDateTime());
QMenu menu; QMenu menu;
CroppingLabel *nameLabel; CroppingLabel *nameLabel;
@ -76,6 +82,7 @@ protected:
QString previousName; QString previousName;
ChatAreaWidget *chatWidget; ChatAreaWidget *chatWidget;
int curRow; int curRow;
QDateTime *earliestMessage;
}; };
#endif // GENERICCHATFORM_H #endif // GENERICCHATFORM_H

View File

@ -34,9 +34,3 @@ QDateTime LoadHistoryDialog::getFromDate()
QDateTime res(ui->fromDate->selectedDate()); QDateTime res(ui->fromDate->selectedDate());
return res; return res;
} }
QDateTime LoadHistoryDialog::getToDate()
{
QDateTime res(ui->toDate->selectedDate().addDays(1));
return res;
}

View File

@ -33,7 +33,6 @@ public:
~LoadHistoryDialog(); ~LoadHistoryDialog();
QDateTime getFromDate(); QDateTime getFromDate();
QDateTime getToDate();
private: private:
Ui::LoadHistoryDialog *ui; Ui::LoadHistoryDialog *ui;

View File

@ -6,7 +6,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>574</width> <width>347</width>
<height>264</height> <height>264</height>
</rect> </rect>
</property> </property>
@ -15,40 +15,18 @@
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_3"> <layout class="QVBoxLayout" name="verticalLayout_3">
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout"> <widget class="QLabel" name="fromLabel">
<item> <property name="text">
<layout class="QVBoxLayout" name="fromLayout"> <string>From Date</string>
<item> </property>
<widget class="QLabel" name="fromLabel"> </widget>
<property name="text"> </item>
<string>From Date</string> <item>
</property> <widget class="QCalendarWidget" name="fromDate">
</widget> <property name="gridVisible">
</item> <bool>false</bool>
<item> </property>
<widget class="QCalendarWidget" name="fromDate"> </widget>
<property name="gridVisible">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="toLayout">
<item>
<widget class="QLabel" name="toLabel">
<property name="text">
<string>To Date</string>
</property>
</widget>
</item>
<item>
<widget class="QCalendarWidget" name="toDate"/>
</item>
</layout>
</item>
</layout>
</item> </item>
<item> <item>
<widget class="QDialogButtonBox" name="buttonBox"> <widget class="QDialogButtonBox" name="buttonBox">

View File

@ -30,12 +30,12 @@ void ActionAction::setup(QTextCursor cursor, QTextEdit *)
// Except we never udpate on our own, so we can safely free our resources // Except we never udpate on our own, so we can safely free our resources
(void) cursor; (void) cursor;
message.clear(); // message.clear();
message.squeeze(); // message.squeeze();
name.clear(); // name.clear();
name.squeeze(); // name.squeeze();
date.clear(); // date.clear();
date.squeeze(); // date.squeeze();
} }
QString ActionAction::getName() QString ActionAction::getName()

View File

@ -79,11 +79,11 @@ void FileTransferAction::updateHtml()
if (w->getState() == FileTransferInstance::TransfState::tsCanceled if (w->getState() == FileTransferInstance::TransfState::tsCanceled
|| w->getState() == FileTransferInstance::TransfState::tsFinished) || w->getState() == FileTransferInstance::TransfState::tsFinished)
{ {
name.clear(); // name.clear();
name.squeeze(); // name.squeeze();
date.clear(); // date.clear();
date.squeeze(); // date.squeeze();
cur = QTextCursor(); // cur = QTextCursor();
} }
} }

View File

@ -30,12 +30,12 @@ void MessageAction::setup(QTextCursor cursor, QTextEdit *)
// Except we never udpate on our own, so we can safely free our resources // Except we never udpate on our own, so we can safely free our resources
(void) cursor; (void) cursor;
message.clear(); // message.clear();
message.squeeze(); // message.squeeze();
name.clear(); // name.clear();
name.squeeze(); // name.squeeze();
date.clear(); // date.clear();
date.squeeze(); // date.squeeze();
} }
QString MessageAction::getMessage() QString MessageAction::getMessage()

View File

@ -35,12 +35,12 @@ void SystemMessageAction::setup(QTextCursor cursor, QTextEdit *)
// Except we never udpate on our own, so we can safely free our resources // Except we never udpate on our own, so we can safely free our resources
(void) cursor; (void) cursor;
message.clear(); // message.clear();
message.squeeze(); // message.squeeze();
name.clear(); // name.clear();
name.squeeze(); // name.squeeze();
date.clear(); // date.clear();
date.squeeze(); // date.squeeze();
type.clear(); // type.clear();
type.squeeze(); // type.squeeze();
} }