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

chatlog: save chat log

This commit is contained in:
krepa098 2015-01-02 12:04:04 +01:00
parent 1c653c5f65
commit b0c3b4032f
4 changed files with 41 additions and 24 deletions

View File

@ -32,6 +32,8 @@
#include <QApplication>
#include <QMenu>
#include <QClipboard>
#include <QFile>
#include <QFileDialog>
template<class T>
T clamp(T x, T min, T max)
@ -459,6 +461,27 @@ QString ChatLog::getSelectedText() const
return QString();
}
QString ChatLog::toPlainText() const
{
QString out;
QString lastSender;
for(ChatLine* l : lines)
{
if(lastSender != l->content[0]->getText() && !l->content[0]->getText().isEmpty())
{
//author changed
out += l->content[0]->getText() + ":\n";
lastSender = l->content[0]->getText();
}
out += l->content[1]->getText();
out += "\n\n";
}
return out;
}
bool ChatLog::isEmpty() const
{
return lines.isEmpty();
@ -469,9 +492,10 @@ void ChatLog::showContextMenu(const QPoint& globalPos, const QPointF& scenePos)
QMenu menu;
// populate
QAction* copyAction = menu.addAction(QIcon::fromTheme("edit-copy"), "Copy");
QAction* copyAction = menu.addAction(QIcon::fromTheme("edit-copy"), tr("Copy"));
menu.addSeparator();
QAction* clearAction = menu.addAction("Clear log");
QAction* clearAction = menu.addAction(QIcon::fromTheme("edit-clear") ,tr("Clear chat log"));
QAction* saveAction = menu.addAction(QIcon::fromTheme("document-save") ,tr("Save chat log"));
if(!isOverSelection(scenePos))
copyAction->setDisabled(true);
@ -484,6 +508,20 @@ void ChatLog::showContextMenu(const QPoint& globalPos, const QPointF& scenePos)
if(action == clearAction)
clear();
if(action == saveAction)
{
QString path = QFileDialog::getSaveFileName(0, tr("Save chat log"));
if (path.isEmpty())
return;
QFile file(path);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
file.write(toPlainText().toUtf8());
file.close();
}
}
void ChatLog::clear()

View File

@ -49,6 +49,7 @@ public:
void clear();
void copySelectedText() const;
QString getSelectedText() const;
QString toPlainText() const;
bool isEmpty() const;

View File

@ -143,10 +143,6 @@ GenericChatForm::GenericChatForm(QWidget *parent) :
fileButton->setAttribute(Qt::WA_LayoutUsesWidgetRect);
emoteButton->setAttribute(Qt::WA_LayoutUsesWidgetRect);
menu.addAction(tr("Save chat log"), this, SLOT(onSaveLogClicked()));
menu.addAction(tr("Clear displayed messages"), this, SLOT(clearChatArea(bool)));
menu.addSeparator();
connect(emoteButton, SIGNAL(clicked()), this, SLOT(onEmoteButtonClicked()));
connect(chatWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint)));
connect(chatWidget, SIGNAL(onClick()), this, SLOT(onChatWidgetClicked()));
@ -189,23 +185,6 @@ void GenericChatForm::onChatContextMenuRequested(QPoint pos)
menu.exec(pos);
}
void GenericChatForm::onSaveLogClicked()
{
// QString path = QFileDialog::getSaveFileName(0, tr("Save chat log"));
// if (path.isEmpty())
// return;
// QFile file(path);
// if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
// return;
// QString log;
// log = chatWidget->toPlainText();
// file.write(log.toUtf8());
// file.close();
}
ChatMessage* GenericChatForm::addMessage(const ToxID& author, const QString &message, bool isAction,
const QDateTime &datetime, bool isSent)
{

View File

@ -69,7 +69,6 @@ public slots:
protected slots:
void onChatContextMenuRequested(QPoint pos);
void onSaveLogClicked();
void onEmoteButtonClicked();
void onEmoteInsertRequested(QString str);
void clearChatArea(bool);