From 593413b6793c56fb90ef6dd067c9acb356e454ef Mon Sep 17 00:00:00 2001
From: Awad Mackie <firesock.serwalek@gmail.com>
Date: Sat, 24 Mar 2012 15:54:22 +0000
Subject: [PATCH] Add option for chat monitor to look in the backlog

---
 src/qtui/chatmonitorfilter.cpp                     |   16 +++++++++++++---
 src/qtui/chatmonitorfilter.h                       |    2 ++
 src/qtui/settingspages/chatmonitorsettingspage.cpp |    7 +++++++
 src/qtui/settingspages/chatmonitorsettingspage.ui  |   10 ++++++++++
 4 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/src/qtui/chatmonitorfilter.cpp b/src/qtui/chatmonitorfilter.cpp
index b46dd29..6ee9870 100644
--- a/src/qtui/chatmonitorfilter.cpp
+++ b/src/qtui/chatmonitorfilter.cpp
@@ -39,33 +39,39 @@ ChatMonitorFilter::ChatMonitorFilter(MessageModel *model, QObject *parent)
   QString showHighlightsSettingsId = "ShowHighlights";
   QString operationModeSettingsId = "OperationMode";
   QString buffersSettingsId = "Buffers";
+  QString showBacklogSettingsId = "ShowBacklog";
 
   _showHighlights = viewSettings.value(showHighlightsSettingsId, false).toBool();
   _operationMode = viewSettings.value(operationModeSettingsId, 0).toInt();
   // read configured list of buffers to monitor/ignore
   foreach(QVariant v, viewSettings.value(buffersSettingsId, QVariant()).toList())
     _bufferIds << v.value<BufferId>();
+  _showBacklog = viewSettings.value(showBacklogSettingsId, true).toBool();
 
   viewSettings.notify(showHighlightsSettingsId, this, SLOT(showHighlightsSettingChanged(const QVariant &)));
   viewSettings.notify(operationModeSettingsId, this, SLOT(operationModeSettingChanged(const QVariant &)));
   viewSettings.notify(buffersSettingsId, this, SLOT(buffersSettingChanged(const QVariant &)));
+  viewSettings.notify(showBacklogSettingsId, this, SLOT(showBacklogSettingChanged(const QVariant &)));
 }
 
 bool ChatMonitorFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const {
   Q_UNUSED(sourceParent)
 
   QModelIndex source_index = sourceModel()->index(sourceRow, 0);
+  BufferId bufferId = source_index.data(MessageModel::BufferIdRole).value<BufferId>();
 
   Message::Flags flags = (Message::Flags)source_index.data(MessageModel::FlagsRole).toInt();
-  if(flags & Message::Backlog || (!_showOwnMessages && flags & Message::Self))
+  if ((flags & Message::Backlog) && (!_showBacklog ||
+	 (Client::networkModel()->lastSeenMsgId(bufferId) >= sourceModel()->data(source_index, MessageModel::MsgIdRole).value<MsgId>()))) 
+	  return false;
+
+  if (!_showOwnMessages && flags & Message::Self)
     return false;
 
   Message::Type type = (Message::Type)source_index.data(MessageModel::TypeRole).toInt();
   if(!(type & (Message::Plain | Message::Notice | Message::Action)))
     return false;
 
-  BufferId bufferId = source_index.data(MessageModel::BufferIdRole).value<BufferId>();
-
   // ChatMonitorSettingsPage
   if(_operationMode == ChatViewSettings::OptOut
     && !(_showHighlights && flags & Message::Highlight)
@@ -167,3 +173,7 @@ void ChatMonitorFilter::buffersSettingChanged(const QVariant &newValue) {
   }
   invalidateFilter();
 }
+
+void ChatMonitorFilter::showBacklogSettingChanged(const QVariant &newValue) {
+	_showBacklog = newValue.toBool();
+}
diff --git a/src/qtui/chatmonitorfilter.h b/src/qtui/chatmonitorfilter.h
index d7f4c4b..ea37d94 100644
--- a/src/qtui/chatmonitorfilter.h
+++ b/src/qtui/chatmonitorfilter.h
@@ -57,6 +57,7 @@ private slots:
   void showHighlightsSettingChanged(const QVariant &newValue);
   void operationModeSettingChanged(const QVariant &newValue);
   void buffersSettingChanged(const QVariant &newValue);
+  void showBacklogSettingChanged(const QVariant &newValue);
 
 private:
   int _showFields;
@@ -64,6 +65,7 @@ private:
   QList<BufferId> _bufferIds;
   bool _showHighlights;
   int _operationMode;
+  bool _showBacklog;
 };
 
 #endif
diff --git a/src/qtui/settingspages/chatmonitorsettingspage.cpp b/src/qtui/settingspages/chatmonitorsettingspage.cpp
index 2a434c2..e2dbcbb 100644
--- a/src/qtui/settingspages/chatmonitorsettingspage.cpp
+++ b/src/qtui/settingspages/chatmonitorsettingspage.cpp
@@ -62,6 +62,7 @@ ChatMonitorSettingsPage::ChatMonitorSettingsPage(QWidget *parent)
   connect(ui.operationMode, SIGNAL(currentIndexChanged(int)), SLOT(switchOperationMode(int)));
   connect(ui.showHighlights, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
   connect(ui.showOwnMessages, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
+  connect(ui.showBacklog, SIGNAL(toggled(bool)), SLOT(widgetHasChanged()));
 }
 
 bool ChatMonitorSettingsPage::hasDefaults() const {
@@ -74,6 +75,7 @@ void ChatMonitorSettingsPage::defaults() {
   settings["ShowOwnMsgs"] = false;
   settings["Buffers"] = QVariant();
   settings["Default"] = true;
+  settings["ShowBacklog"] = true;
   load();
   widgetHasChanged();
 }
@@ -88,6 +90,7 @@ void ChatMonitorSettingsPage::load() {
   ui.operationMode->setCurrentIndex(settings["OperationMode"].toInt() - 1);
   ui.showHighlights->setChecked(settings["ShowHighlights"].toBool());
   ui.showOwnMessages->setChecked(settings["ShowOwnMsgs"].toBool());
+  ui.showBacklog->setChecked(settings["ShowBacklog"].toBool());
 
   // get all available buffer Ids
   QList<BufferId> allBufferIds = Client::networkModel()->allBufferIds();
@@ -118,6 +121,7 @@ void ChatMonitorSettingsPage::loadSettings() {
   settings["ShowHighlights"] = chatViewSettings.value("ShowHighlights", false);
   settings["ShowOwnMsgs"] = chatViewSettings.value("ShowOwnMsgs", false);
   settings["Buffers"] = chatViewSettings.value("Buffers", QVariantList());
+  settings["ShowBacklog"] = chatViewSettings.value("ShowBacklog", true);
 }
 
 void ChatMonitorSettingsPage::save() {
@@ -126,6 +130,7 @@ void ChatMonitorSettingsPage::save() {
   chatViewSettings.setValue("OperationMode", ui.operationMode->currentIndex() + 1);
   chatViewSettings.setValue("ShowHighlights", ui.showHighlights->isChecked());
   chatViewSettings.setValue("ShowOwnMsgs", ui.showOwnMessages->isChecked());
+  chatViewSettings.setValue("ShowBacklog", ui.showBacklog->isChecked());
 
   // save list of active buffers
   QVariantList saveableBufferIdList;
@@ -150,6 +155,8 @@ bool ChatMonitorSettingsPage::testHasChanged() {
     return true;
   if(settings["ShowOwnMsgs"].toBool() != ui.showOwnMessages->isChecked())
     return true;
+  if(settings["ShowBacklog"].toBool() != ui.showBacklog->isChecked())
+    return true;
 
   if(_configActive->bufferList().count() != settings["Buffers"].toList().count())
     return true;
diff --git a/src/qtui/settingspages/chatmonitorsettingspage.ui b/src/qtui/settingspages/chatmonitorsettingspage.ui
index e69524a..e630926 100644
--- a/src/qtui/settingspages/chatmonitorsettingspage.ui
+++ b/src/qtui/settingspages/chatmonitorsettingspage.ui
@@ -158,6 +158,16 @@ p, li { white-space: pre-wrap; }
      </property>
     </widget>
    </item>
+   <item>
+    <widget class="QCheckBox" name="showBacklog">
+     <property name="toolTip">
+      <string>Display messages from backlog on reconnect</string>
+     </property>
+     <property name="text">
+      <string>Show messages from backlog</string>
+     </property>
+    </widget>
+   </item>
   </layout>
  </widget>
  <customwidgets>
-- 
1.7.3.4

