Added remove, copy to clipboard and resend of messages in History Browser.

Clear history in PeersDialog don't remove messages from the History Keeper.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3447 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2010-09-05 20:50:34 +00:00
parent c63905fefc
commit bacf1f5a80
13 changed files with 379 additions and 45 deletions

View File

@ -1385,7 +1385,6 @@ void PeersDialog::setChatInfo(QString info, QColor color)
void PeersDialog::on_actionClearChat_triggered()
{
ui.msgText->clear();
historyKeeper.clear();
}
void PeersDialog::displayInfoChatMenu(const QPoint& pos)
@ -1756,6 +1755,6 @@ void PeersDialog::statusColumn()
void PeersDialog::on_actionMessageHistory_triggered()
{
ImHistoryBrowser imBrowser(false, historyKeeper, this);
ImHistoryBrowser imBrowser(false, historyKeeper, ui.lineEdit, this);
imBrowser.exec();
}

View File

@ -25,12 +25,14 @@
IMHistoryItem::IMHistoryItem()
{
hiid = 0;
}
//============================================================================
IMHistoryItem::IMHistoryItem(bool incomingIn, std::string &idIn, const QString &nameIn, const QDateTime &sendTimeIn, const QString &messageTextIn)
IMHistoryItem::IMHistoryItem(int hiidIn, bool incomingIn, std::string &idIn, const QString &nameIn, const QDateTime &sendTimeIn, const QString &messageTextIn)
{
hiid = hiidIn;
incoming = incomingIn;
id = idIn;
name = nameIn;

View File

@ -30,8 +30,9 @@ class IMHistoryItem
public:
IMHistoryItem();
IMHistoryItem(bool incoming, std::string &id, const QString &name, const QDateTime &sendTime, const QString &messageText);
IMHistoryItem(int hiid, bool incoming, std::string &id, const QString &name, const QDateTime &sendTime, const QString &messageText);
int hiid;
bool incoming;
std::string id;
QString name;

View File

@ -44,6 +44,8 @@ IMHistoryKeeper::IMHistoryKeeper()
saveTimer->connect(saveTimer, SIGNAL(timeout()), this, SLOT(saveHistory()));
saveTimer->setInterval(10000);
saveTimer->start();
lasthiid = 0;
};
//=============================================================================
@ -57,6 +59,8 @@ IMHistoryKeeper::~IMHistoryKeeper()
void IMHistoryKeeper::init(QString historyFileName)
{
lasthiid = 0;
hfName = historyFileName;
loadHistoryFile();
}
@ -65,7 +69,7 @@ void IMHistoryKeeper::init(QString historyFileName)
void IMHistoryKeeper::addMessage(bool incoming, std::string &id, const QString &name, const QDateTime &sendTime, const QString &messageText)
{
IMHistoryItem item(incoming, id, name, sendTime, messageText);
IMHistoryItem item(++lasthiid, incoming, id, name, sendTime, messageText);
hitems.append(item);
@ -97,7 +101,7 @@ bool IMHistoryKeeper::loadHistoryFile()
}
IMHistoryReader hreader;
if (!hreader.read(hitems, hfName)) {
if (!hreader.read(hitems, hfName, lasthiid)) {
lastErrorMessage = hreader.errorMessage();
return false;
}
@ -145,16 +149,73 @@ bool IMHistoryKeeper::getMessages(QList<IMHistoryItem> &historyItems, const int
//=============================================================================
bool IMHistoryKeeper::getMessage(int hiid, IMHistoryItem &item)
{
QList<IMHistoryItem>::iterator it;
for (it = hitems.begin(); it != hitems.end(); it++) {
if (it->hiid == hiid) {
item = *it;
return true;
}
}
return false;
}
//=============================================================================
void IMHistoryKeeper::clear()
{
hitems.clear();
historyChanged = true;
lasthiid = 0;
emit historyClear();
}
//=============================================================================
void IMHistoryKeeper::removeMessage(int hiid)
{
QList<IMHistoryItem>::iterator it;
for (it = hitems.begin(); it != hitems.end(); it++) {
if (it->hiid == hiid) {
emit historyRemove(*it);
hitems.erase(it);
historyChanged = true;
break;
}
}
}
//=============================================================================
void IMHistoryKeeper::removeMessages(QList<int> &hiids)
{
bool changed = false;
QList<IMHistoryItem>::iterator it = hitems.begin();
while (it != hitems.end()) {
if (qFind(hiids, it->hiid) != hiids.end()) {
emit historyRemove(*it);
hitems.erase(it);
changed = true;
continue;
}
it++;
}
if (changed) {
historyChanged = true;
}
}
//=============================================================================
void IMHistoryKeeper::saveHistory()
{
if (historyChanged && hfName.isEmpty() == false) {

View File

@ -71,6 +71,9 @@ public:
//! Fills given list with items
bool getMessages(QList<IMHistoryItem> &historyItems, const int messagesCount);
//! Get message
bool getMessage(int hiid, IMHistoryItem &item);
//! Adds new message to the history
//! Adds new message to the history, but the message will be saved to
@ -80,6 +83,12 @@ public:
//! Clear the history
void clear();
//! Remove item
void removeMessage(int hiid);
//! Remove items
void removeMessages(QList<int> &hiids);
private:
bool loadHistoryFile();
@ -88,12 +97,14 @@ private:
bool historyChanged;
QString lastErrorMessage;
QTimer *saveTimer;
int lasthiid;
private slots:
void saveHistory();
signals:
void historyAdd(IMHistoryItem item) const;
void historyRemove(IMHistoryItem item) const;
void historyClear() const;
};

View File

@ -35,12 +35,11 @@ IMHistoryReader::IMHistoryReader()
//=============================================================================
bool IMHistoryReader::read(QList<IMHistoryItem>& resultList,
const QString fileName)
bool IMHistoryReader::read(QList<IMHistoryItem>& resultList, const QString fileName, int &lasthiid)
{
errMess = "No error";
QList<IMHistoryItem> result;
resultList.clear();
//==== check for file and open it
QFile fl(fileName);
@ -64,7 +63,7 @@ bool IMHistoryReader::read(QList<IMHistoryItem>& resultList,
readNext();
if (isStartElement()) {
if (name() == "history_file" && attributes().value("format_version") == "1.0") {
readHistory(result);
readHistory(resultList, lasthiid);
break;
} else {
errMess = "The file is not a history file with format version 1.0";
@ -75,13 +74,11 @@ bool IMHistoryReader::read(QList<IMHistoryItem>& resultList,
if (error()) {
errMess = errorString();
} else {
resultList.clear();
QList<IMHistoryItem>::const_iterator hii;//history items iterator
for (hii = result.constBegin(); hii != result.constEnd(); ++hii) {
resultList << *hii;
}
// } else {
// QList<IMHistoryItem>::const_iterator hii;//history items iterator
// for (hii = result.constBegin(); hii != result.constEnd(); ++hii) {
// resultList << *hii;
// }
}
return !error();
@ -118,13 +115,16 @@ void IMHistoryReader::readUnknownElement()
//=============================================================================
void IMHistoryReader::readHistory(QList<IMHistoryItem> &historyItems)
void IMHistoryReader::readHistory(QList<IMHistoryItem> &historyItems, int &lasthiid)
{
Q_ASSERT(isStartElement());
// qDebug()<< " " << "node with message " << name() ;
historyItems.clear();
lasthiid = 0;
bool recalculate = false;
while (!atEnd()) {
readNext();
@ -136,13 +136,29 @@ void IMHistoryReader::readHistory(QList<IMHistoryItem> &historyItems)
if ( name() == "message") {
IMHistoryItem item;
readMessage(item);
if (item.hiid == 0) {
recalculate = true;
} else {
if (item.hiid > lasthiid) {
lasthiid = item.hiid;
}
}
historyItems.append(item);
}
else {
} else {
readUnknownElement();
}
}
}
if (recalculate) {
// calculate hiid
QList<IMHistoryItem>::iterator item = historyItems.begin();
for (item = historyItems.begin(); item != historyItems.end(); item++) {
if (item->hiid == 0) {
item->hiid = ++lasthiid;
}
}
}
}
//=============================================================================
@ -154,6 +170,7 @@ void IMHistoryReader::readMessage(IMHistoryItem &historyItem)
if (isStartElement() && (name() == "message")) {
//=== process attributes
historyItem.hiid = attributes().value("hiid").toString().toInt();
historyItem.incoming = (attributes().value("incoming").toString().toInt() == 1);
historyItem.id = attributes().value("id").toString().toStdString();
historyItem.name = attributes().value("name").toString();
@ -180,6 +197,3 @@ void IMHistoryReader::readMessage(IMHistoryItem &historyItem)
}
}
//=============================================================================
//=============================================================================
//=============================================================================

View File

@ -34,14 +34,13 @@ class IMHistoryReader : public QXmlStreamReader
public:
IMHistoryReader();
bool read(QList<IMHistoryItem>& resultList,
const QString fileName );
bool read(QList<IMHistoryItem>& resultList, const QString fileName, int &lasthiid);
QString errorMessage();
private:
void readUnknownElement();
void readHistory(QList<IMHistoryItem> &historyItems);
void readHistory(QList<IMHistoryItem> &historyItems, int &lasthiid);
void readMessage(IMHistoryItem &historyItem);
QString errMess;

View File

@ -59,6 +59,7 @@ bool IMHistoryWriter::write(QList<IMHistoryItem>& itemList, const QString fileNa
foreach(IMHistoryItem item, itemList) {
writeStartElement("message");
writeAttribute("hiid", QString::number(item.hiid));
writeAttribute("incoming", QString::number(item.incoming ? 1 : 0));
writeAttribute("id", QString::fromStdString(item.id));
writeAttribute("name", item.name);

View File

@ -24,6 +24,9 @@
#include <QMenu>
#include <QClipboard>
#include <QTextDocument>
#include <QTextEdit>
#include <QClipboard>
#include <QKeyEvent>
#include "ImHistoryBrowser.h"
#include "IMHistoryItemDelegate.h"
@ -32,24 +35,33 @@
#include "rshare.h"
#include "gui/settings/rsharesettings.h"
#define ROLE_PLAINTEXT Qt::UserRole
#define ROLE_HIID Qt::UserRole
#define ROLE_PLAINTEXT Qt::UserRole + 1
/** Default constructor */
ImHistoryBrowser::ImHistoryBrowser(bool isPrivateChatIn, IMHistoryKeeper &histKeeper, QWidget *parent, Qt::WFlags flags)
ImHistoryBrowser::ImHistoryBrowser(bool isPrivateChatIn, IMHistoryKeeper &histKeeper, QTextEdit *edit, QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags), historyKeeper(histKeeper)
{
/* Invoke Qt Designer generated QObject setup routine */
ui.setupUi(this);
isPrivateChat = isPrivateChatIn;
textEdit = edit;
connect(&historyKeeper, SIGNAL(historyAdd(IMHistoryItem)), this, SLOT(historyAdd(IMHistoryItem)));
connect(&historyKeeper, SIGNAL(historyRemove(IMHistoryItem)), this, SLOT(historyRemove(IMHistoryItem)));
connect(&historyKeeper, SIGNAL(historyClear()), this, SLOT(historyClear()));
connect(ui.clearButton, SIGNAL(clicked()), this, SLOT(clearFilter()));
connect(ui.clearFilterButton, SIGNAL(clicked()), this, SLOT(clearFilter()));
connect(ui.filterPatternLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(filterRegExpChanged()));
ui.clearButton->hide();
connect(ui.copyButton, SIGNAL(clicked()), SLOT(copyMessage()));
connect(ui.removeButton, SIGNAL(clicked()), SLOT(removeMessages()));
connect(ui.listWidget, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelectionChanged()));
connect(ui.listWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
ui.clearFilterButton->hide();
// embed smileys ?
if (isPrivateChat) {
@ -73,6 +85,12 @@ ImHistoryBrowser::ImHistoryBrowser(bool isPrivateChatIn, IMHistoryKeeper &histKe
if (geometry.isEmpty() == false) {
restoreGeometry(geometry);
}
// dummy call for set butons
itemSelectionChanged();
ui.listWidget->installEventFilter(this);
}
ImHistoryBrowser::~ImHistoryBrowser()
@ -80,6 +98,22 @@ ImHistoryBrowser::~ImHistoryBrowser()
Settings->setValueToGroup("HistorieBrowser", "Geometry", saveGeometry());
}
bool ImHistoryBrowser::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui.listWidget) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent && keyEvent->key() == Qt::Key_Delete) {
// Delete pressed
removeMessages();
return true; // eat event
}
}
}
// pass the event on to the parent class
return QDialog::eventFilter(obj, event);
}
void ImHistoryBrowser::historyAdd(IMHistoryItem item)
{
QListWidgetItem *itemWidget = addItem(item);
@ -88,6 +122,18 @@ void ImHistoryBrowser::historyAdd(IMHistoryItem item)
}
}
void ImHistoryBrowser::historyRemove(IMHistoryItem item)
{
int count = ui.listWidget->count();
for (int i = 0; i < count; i++) {
QListWidgetItem *itemWidget = ui.listWidget->item(i);
if (itemWidget->data(ROLE_HIID).toString().toInt() == item.hiid) {
delete(ui.listWidget->takeItem(i));
break;
}
}
}
void ImHistoryBrowser::historyClear()
{
ui.listWidget->clear();
@ -112,6 +158,7 @@ QListWidgetItem *ImHistoryBrowser::addItem(IMHistoryItem &item)
QListWidgetItem *itemWidget = new QListWidgetItem;
itemWidget->setData(Qt::DisplayRole, qVariantFromValue(IMHistoryItemPainter(formatMsg)));
itemWidget->setData(ROLE_HIID, item.hiid);
/* calculate plain text */
QTextDocument doc;
@ -127,9 +174,9 @@ void ImHistoryBrowser::filterRegExpChanged()
QString text = ui.filterPatternLineEdit->text();
if (text.isEmpty()) {
ui.clearButton->hide();
ui.clearFilterButton->hide();
} else {
ui.clearButton->show();
ui.clearFilterButton->show();
}
filterItems();
@ -145,7 +192,6 @@ void ImHistoryBrowser::filterItems(QListWidgetItem *item)
{
QString text = ui.filterPatternLineEdit->text();
QRegExp regExp(text);
if (item == NULL) {
int count = ui.listWidget->count();
for (int i = 0; i < count; i++) {
@ -153,7 +199,7 @@ void ImHistoryBrowser::filterItems(QListWidgetItem *item)
if (text.isEmpty()) {
item->setHidden(false);
} else {
if (item->data(ROLE_PLAINTEXT).toString().contains(regExp)) {
if (item->data(ROLE_PLAINTEXT).toString().contains(text, Qt::CaseInsensitive)) {
item->setHidden(false);
} else {
item->setHidden(true);
@ -164,7 +210,7 @@ void ImHistoryBrowser::filterItems(QListWidgetItem *item)
if (text.isEmpty()) {
item->setHidden(false);
} else {
if (item->data(ROLE_PLAINTEXT).toString().contains(regExp)) {
if (item->data(ROLE_PLAINTEXT).toString().contains(text, Qt::CaseInsensitive)) {
item->setHidden(false);
} else {
item->setHidden(true);
@ -172,3 +218,129 @@ void ImHistoryBrowser::filterItems(QListWidgetItem *item)
}
}
}
void ImHistoryBrowser::getSelectedItems(QList<int> &items)
{
QList<QListWidgetItem*> itemWidgets = ui.listWidget->selectedItems();
QList<QListWidgetItem*>::iterator it;
for (it = itemWidgets.begin(); it != itemWidgets.end(); it++) {
QListWidgetItem *item = *it;
if (item->isHidden()) {
continue;
}
items.append(item->data(ROLE_HIID).toString().toInt());
}
}
void ImHistoryBrowser::itemSelectionChanged()
{
QList<int> hiids;
getSelectedItems(hiids);
if (hiids.size()) {
// activate buttons
ui.copyButton->setEnabled(true);
ui.removeButton->setEnabled(true);
} else {
// deactivate buttons
ui.copyButton->setDisabled(true);
ui.removeButton->setDisabled(true);
}
}
void ImHistoryBrowser::customContextMenuRequested(QPoint pos)
{
QList<int> hiids;
getSelectedItems(hiids);
QListWidgetItem *currentItem = ui.listWidget->currentItem();
QMenu contextMnu(this);
QAction *selectAll = new QAction(tr("Mark all"), &contextMnu);
QAction *copyMessage = new QAction(tr("Copy"), &contextMnu);
QAction *removeMessages = new QAction(tr("Delete"), &contextMnu);
QAction *clearHistory = new QAction(tr("Clear history"), &contextMnu);
QAction *sendItem = NULL;
if (textEdit) {
sendItem = new QAction(tr("Send"), &contextMnu);
if (currentItem) {
connect(sendItem, SIGNAL(triggered()), this, SLOT(sendMessage()));
} else {
sendItem->setDisabled(true);
}
}
if (hiids.size()) {
connect(selectAll, SIGNAL(triggered()), ui.listWidget, SLOT(selectAll()));
connect(copyMessage, SIGNAL(triggered()), this, SLOT(copyMessage()));
connect(removeMessages, SIGNAL(triggered()), this, SLOT(removeMessages()));
connect(clearHistory, SIGNAL(triggered()), this, SLOT(clearHistory()));
} else {
selectAll->setDisabled(true);
copyMessage->setDisabled(true);
removeMessages->setDisabled(true);
clearHistory->setDisabled(true);
}
contextMnu.addAction(selectAll);
contextMnu.addSeparator();
contextMnu.addAction(copyMessage);
contextMnu.addAction(removeMessages);
contextMnu.addAction(clearHistory);
if (sendItem) {
contextMnu.addSeparator();
contextMnu.addAction(sendItem);
}
contextMnu.exec(QCursor::pos());
}
void ImHistoryBrowser::copyMessage()
{
QListWidgetItem *currentItem = ui.listWidget->currentItem();
if (currentItem) {
int hiid = currentItem->data(ROLE_HIID).toString().toInt();
IMHistoryItem item;
if (historyKeeper.getMessage(hiid, item)) {
QTextDocument doc;
doc.setHtml(item.messageText);
QApplication::clipboard()->setText(doc.toPlainText());
}
}
}
void ImHistoryBrowser::removeMessages()
{
QList<int> hiids;
getSelectedItems(hiids);
historyKeeper.removeMessages(hiids);
}
void ImHistoryBrowser::clearHistory()
{
historyKeeper.clear();
}
void ImHistoryBrowser::sendMessage()
{
if (textEdit) {
QListWidgetItem *currentItem = ui.listWidget->currentItem();
if (currentItem) {
int hiid = currentItem->data(ROLE_HIID).toString().toInt();
IMHistoryItem item;
if (historyKeeper.getMessage(hiid, item)) {
textEdit->clear();
textEdit->setText(item.messageText);
textEdit->setFocus();
QTextCursor cursor = textEdit->textCursor();
cursor.movePosition(QTextCursor::End);
textEdit->setTextCursor(cursor);
close();
}
}
}
}

View File

@ -30,28 +30,45 @@
#include "ui_ImHistoryBrowser.h"
class QTextEdit;
class ImHistoryBrowser : public QDialog
{
Q_OBJECT
public:
/** Default constructor */
ImHistoryBrowser(bool isPrivateChat, IMHistoryKeeper &histKeeper, QWidget *parent = 0, Qt::WFlags flags = 0);
ImHistoryBrowser(bool isPrivateChat, IMHistoryKeeper &histKeeper, QTextEdit *edit, QWidget *parent = 0, Qt::WFlags flags = 0);
/** Default destructor */
virtual ~ImHistoryBrowser();
protected:
bool eventFilter(QObject *obj, QEvent *ev);
private slots:
void historyAdd(IMHistoryItem item);
void historyRemove(IMHistoryItem item);
void historyClear();
void filterRegExpChanged();
void clearFilter();
void itemSelectionChanged();
void customContextMenuRequested(QPoint pos);
void copyMessage();
void removeMessages();
void clearHistory();
void sendMessage();
private:
QListWidgetItem *addItem(IMHistoryItem &item);
void filterItems(QListWidgetItem *item = NULL);
void getSelectedItems(QList<int> &items);
bool isPrivateChat;
QTextEdit *textEdit;
bool embedSmileys;
IMHistoryKeeper &historyKeeper;
ChatStyle style;

View File

@ -14,7 +14,7 @@
<string>Message History</string>
</property>
<property name="windowIcon">
<iconset resource="../images.qrc">
<iconset>
<normaloff>:/images/rstray3.png</normaloff>:/images/rstray3.png</iconset>
</property>
<property name="sizeGripEnabled">
@ -23,7 +23,7 @@
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QGridLayout" name="mainLayout">
<item row="1" column="0">
<item row="7" column="0">
<layout class="QHBoxLayout" name="bottomLine">
<item>
<layout class="QHBoxLayout" name="filterLayout">
@ -36,7 +36,7 @@
<string/>
</property>
<property name="pixmap">
<pixmap resource="../images.qrc">:/images/find-16.png</pixmap>
<pixmap>:/images/find-16.png</pixmap>
</property>
</widget>
</item>
@ -44,7 +44,7 @@
<widget class="QLineEdit" name="filterPatternLineEdit"/>
</item>
<item>
<widget class="QPushButton" name="clearButton">
<widget class="QPushButton" name="clearFilterButton">
<property name="minimumSize">
<size>
<width>16</width>
@ -97,16 +97,72 @@ border-image: url(:/images/closepressed.png)
</item>
</layout>
</item>
<item row="6" column="0">
<widget class="QListWidget" name="listWidget">
<property name="contextMenuPolicy">
<enum>Qt::CustomContextMenu</enum>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QListWidget" name="listWidget"/>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QToolButton" name="copyButton">
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="text">
<string>Copy</string>
</property>
<property name="icon">
<iconset>
<normaloff>:/images/copy.png</normaloff>:/images/copy.png</iconset>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextBesideIcon</enum>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="removeButton">
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="text">
<string>Remove</string>
</property>
<property name="icon">
<iconset>
<normaloff>:/images/delete.png</normaloff>:/images/delete.png</iconset>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextBesideIcon</enum>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
<resources>
<include location="../images.qrc"/>
</resources>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>

View File

@ -107,6 +107,7 @@
<file>images/console-small-hover.png</file>
<file>images/console-small-up.png</file>
<file>images/contact_new128.png</file>
<file>images/copy.png</file>
<file>images/delete.png</file>
<file>images/deleteall.png</file>
<file>images/deletemail-pressed.png</file>

Binary file not shown.

After

Width:  |  Height:  |  Size: 590 B