Move Clickable out of ContentsChatItem
[quassel.git] / src / uisupport / clickable.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "clickable.h"
22
23 // NOTE: This method is not threadsafe and not reentrant!
24 //       (RegExps are not constant while matching, and they are static here for efficiency)
25 ClickableList ClickableList::fromString(const QString &str) {
26   // For matching URLs
27   static QString urlEnd("(?:>|[,.;:\"]*\\s|\\b|$)");
28   static QString urlChars("(?:[,.;:]*[\\w\\-~@/?&=+$()!%#*|{}\\[\\]'^])");
29
30   static QRegExp regExp[] = {
31     // URL
32     // QRegExp(QString("((?:https?://|s?ftp://|irc://|mailto:|www\\.)%1+|%1+\\.[a-z]{2,4}(?:?=/%1+|\\b))%2").arg(urlChars, urlEnd)),
33     QRegExp(QString("((?:(?:mailto:|\\w+://)|www\\.)%1+)%2").arg(urlChars, urlEnd), Qt::CaseInsensitive),
34
35     // Channel name
36     // We don't match for channel names starting with + or &, because that gives us a lot of false positives.
37     QRegExp("((?:#|![A-Z0-9]{5})[^,:\\s]+(?::[^,:\\s]+)?)\\b", Qt::CaseInsensitive)
38
39     // TODO: Nicks, we'll need a filtering for only matching known nicknames further down if we do this
40   };
41
42   static const int regExpCount = 2;  // number of regexps in the array above
43
44   qint16 matches[] = { 0, 0, 0 };
45   qint16 matchEnd[] = { 0, 0, 0 };
46
47   ClickableList result;
48   //QString str = data(ChatLineModel::DisplayRole).toString();
49
50   qint16 idx = 0;
51   qint16 minidx;
52   int type = -1;
53
54   do {
55     type = -1;
56     minidx = str.length();
57     for(int i = 0; i < regExpCount; i++) {
58       if(matches[i] < 0 || matchEnd[i] > str.length()) continue;
59       if(idx >= matchEnd[i]) {
60         matches[i] = regExp[i].indexIn(str, qMax(matchEnd[i], idx));
61         if(matches[i] >= 0) matchEnd[i] = matches[i] + regExp[i].cap(1).length();
62       }
63       if(matches[i] >= 0 && matches[i] < minidx) {
64         minidx = matches[i];
65         type = i;
66       }
67     }
68     if(type >= 0) {
69       idx = matchEnd[type];
70       QString match = str.mid(matches[type], matchEnd[type] - matches[type]);
71       if(type == Clickable::Url && str.at(idx-1) == ')') {  // special case: closing paren only matches if we had an open one
72         if(!match.contains('(')) {
73           matchEnd[type]--;
74           match.chop(1);
75         }
76       }
77       if(type == Clickable::Channel) {
78         // don't make clickable if it could be a #number
79         if(QRegExp("^#\\d+$").exactMatch(match))
80           continue;
81       }
82       result.append(Clickable((Clickable::Type)type, matches[type], matchEnd[type] - matches[type]));
83     }
84   } while(type >= 0);
85   return result;
86 }