Put selections in both clipboard and X selection buffer on Linux again.
[quassel.git] / src / qtui / chatwidget.cpp
index 8a9b7a2..cf897b6 100644 (file)
 #include "chatline-old.h"
 #include "qtui.h"
 #include "uisettings.h"
-
-ChatWidget::ChatWidget(QWidget *parent) : QAbstractScrollArea(parent) {
+#include "client.h"
+#include "buffer.h"
+#include "clientbacklogmanager.h"
+
+ChatWidget::ChatWidget(BufferId bufid, QWidget *parent) : QAbstractScrollArea(parent), AbstractChatView(),
+    lastBacklogOffset(0),
+    lastBacklogSize(0)
+{
   //setAutoFillBackground(false);
   //QPalette palette;
   //palette.setColor(backgroundRole(), QColor(0, 0, 0, 50));
@@ -43,6 +49,8 @@ ChatWidget::ChatWidget(QWidget *parent) : QAbstractScrollArea(parent) {
   pointerPosition = QPoint(0,0);
   connect(verticalScrollBar(), SIGNAL(actionTriggered(int)), this, SLOT(scrollBarAction(int)));
   connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(scrollBarValChanged(int)));
+
+  init(bufid);
 }
 
 void ChatWidget::init(BufferId id) {
@@ -67,6 +75,9 @@ void ChatWidget::init(BufferId id) {
   mouseMode = Normal;
   selectionMode = NoSelection;
   connect(scrollTimer, SIGNAL(timeout()), this, SLOT(handleScrollTimer()));
+
+  if(bufferId.isValid())
+    connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(viewportChanged(int)));
 }
 
 ChatWidget::~ChatWidget() {
@@ -238,12 +249,14 @@ void ChatWidget::appendChatLines(QList<ChatLineOld *> list) {
   viewport()->update();
 }
 
-void ChatWidget::setContents(QList<ChatLineOld *> list) {
+void ChatWidget::setContents(const QList<AbstractUiMsg *> &list) {
   ycoords.clear();
   ycoords.append(0);
   height = 0;
   lines.clear();
-  appendChatLines(list);
+  QList<ChatLineOld *> cl;
+  foreach(AbstractUiMsg *msg, list) cl << dynamic_cast<ChatLineOld *>(msg);
+  appendChatLines(cl);
 }
 
 //!\brief Computes the different x position vars for given tsWidth and senderWidth.
@@ -371,7 +384,8 @@ void ChatWidget::mouseDoubleClickEvent(QMouseEvent *event) {
     len = end - start;
   }
   QString word = text.mid(start, len);
-  if(word.startsWith("http://")) {
+  QRegExp regex("^(h|f)t{1,2}ps?:\\/\\/");
+  if(regex.indexIn(word) != -1) {
     QDesktopServices::openUrl(QUrl(word));
   }
   
@@ -397,7 +411,11 @@ void ChatWidget::mouseReleaseEvent(QMouseEvent *event) {
         selectionStart = qMin(dragStartCursor, curCursor);
         selectionEnd = qMax(dragStartCursor, curCursor);
         // TODO Make X11SelectionMode configurable!
+#ifdef Q_WS_X11
+        QApplication::clipboard()->setText(selectionToString(), QClipboard::Selection);
+#else
         QApplication::clipboard()->setText(selectionToString());
+#endif
         break;
       case MarkLines:
         mouseMode = Normal;
@@ -405,7 +423,12 @@ void ChatWidget::mouseReleaseEvent(QMouseEvent *event) {
         selectionStart = qMin(dragStartLine, curLine);
         selectionEnd = qMax(dragStartLine, curLine);
         // TODO Make X11SelectionMode configurable!
+#ifdef Q_WS_X11
+        QApplication::clipboard()->setText(selectionToString(), QClipboard::Selection);
+#endif
+//#else
         QApplication::clipboard()->setText(selectionToString());
+//#endif
         break;
       default:
         mouseMode = Normal;
@@ -590,3 +613,26 @@ QString ChatWidget::selectionToString() {
   return lines[selectionLine]->text().mid(selectionStart, selectionEnd - selectionStart);
 }
 
+void ChatWidget::viewportChanged(int newPos) {
+  const int REQUEST_COUNT = 50;
+  QAbstractSlider *vbar = verticalScrollBar();
+  if(!vbar)
+    return;
+
+  int relativePos = 100;
+  if(vbar->maximum() - vbar->minimum() != 0)
+    relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
+
+  if(relativePos < 20) {
+    Buffer *buffer = Client::buffer(bufferId);
+    Q_CHECK_PTR(buffer);
+    if(buffer->contents().isEmpty())
+      return;
+    MsgId msgId = buffer->contents().first()->msgId();
+    if(!lastBacklogOffset.isValid() || (msgId < lastBacklogOffset && lastBacklogSize + REQUEST_COUNT <= buffer->contents().count())) {
+      Client::backlogManager()->requestBacklog(bufferId, REQUEST_COUNT, msgId.toInt());
+      lastBacklogOffset = msgId;
+      lastBacklogSize = buffer->contents().size();
+    }
+  }
+}