URL recognition (WiP)
[quassel.git] / src / qtui / chatitem.cpp
index 127d3ba..aea98db 100644 (file)
@@ -63,6 +63,7 @@ qreal ChatItem::setGeometry(qreal w, qreal h) {
   prepareGeometryChange();
   _boundingRect.setWidth(w);
   if(h < 0) h = computeHeight();
+  //if(h < 0) h = fontMetrics()->lineSpacing(); // only contents can be multi-line
   _boundingRect.setHeight(h);
   if(haveLayout()) updateLayout();
   return h;
@@ -294,6 +295,28 @@ void ContentsChatItem::updateLayout() {
     h += line.height() + fontMetrics()->leading();
   }
   layout()->endLayout();
+
+  analyze();
+}
+
+void ContentsChatItem::analyze() {
+  // Match an URL
+  static QString urlEnd("(?:>|[,.;:]?\\s|\\b)");
+  static QString urlChars("(?:[\\w\\-~@/?&=+$()!%#]|[,.;:]\\w)");
+  static QRegExp urlExp(QString("((?:(?:https?://|ftp://|irc://|mailto:)|www)%1+)%2").arg(urlChars, urlEnd));
+
+  // Match a channel name
+  // We don't match for channel names starting with + or &, because that gives us a lot of false positives.
+  static QRegExp chanExp("((?:#|![A-Z0-9]{5})[^,:\\s]+(?::[^,:\\s]+)?)\\b");
+  QString str = data(ChatLineModel::DisplayRole).toString();
+  quint16 idx = 0;
+  // first, we split on characters that might be URL separators
+  int i = str.indexOf(chanExp);
+  if(i >= 0) {
+    qDebug() << i << chanExp.cap(1);
+  }
+
+
 }
 
 void ContentsChatItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
@@ -332,13 +355,15 @@ void ContentsChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
 
 /*************************************************************************************************/
 
-ContentsChatItem::WrapColumnFinder::WrapColumnFinder(ChatItem *_item) : item(_item) {
-  wrapList = item->data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>();
-  wordidx = 0;
-  layout = 0;
-  lastwrapcol = 0;
-  lastwrappos = 0;
-  w = 0;
+ContentsChatItem::WrapColumnFinder::WrapColumnFinder(ChatItem *_item)
+  : item(_item),
+    layout(0),
+    wrapList(item->data(ChatLineModel::WrapListRole).value<ChatLineModel::WrapList>()),
+    wordidx(0),
+    lastwrapcol(0),
+    lastwrappos(0),
+    w(0)
+{
 }
 
 ContentsChatItem::WrapColumnFinder::~WrapColumnFinder() {