constify
[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 <QDesktopServices>
22 #include <QModelIndex>
23 #include <QUrl>
24
25 #include "buffermodel.h"
26 #include "clickable.h"
27 #include "client.h"
28
29 void Clickable::activate(NetworkId networkId, const QString &text) const {
30   if(!isValid())
31     return;
32
33   QString str = text.mid(start(), length());
34
35   switch(type()) {
36     case Clickable::Url:
37       if(!str.contains("://"))
38         str = "http://" + str;
39       QDesktopServices::openUrl(QUrl::fromEncoded(str.toUtf8(), QUrl::TolerantMode));
40       break;
41     case Clickable::Channel:
42       Client::bufferModel()->switchToOrJoinBuffer(networkId, str);
43       break;
44     default:
45       break;
46   }
47 }
48
49 // NOTE: This method is not threadsafe and not reentrant!
50 //       (RegExps are not constant while matching, and they are static here for efficiency)
51 ClickableList ClickableList::fromString(const QString &str) {
52   // For matching URLs
53   static QString scheme("(?:(?:mailto:|(?:[+.-]?\\w)+://)|www(?=\\.\\S+\\.))");
54   static QString authority("(?:(?:[,.;@:]?[-\\w]+)+\\.?|\\[[0-9a-f:.]+\\])(?::\\d+)?");
55   static QString urlChars("(?:[,.;:]*[\\w~@/?&=+$()!%#*{}\\[\\]\\|'^-])");
56   static QString urlEnd("(?:>|[,.;:\"]*\\s|\\b|$)");
57
58   static QRegExp regExp[] = {
59     // URL
60     // QRegExp(QString("((?:https?://|s?ftp://|irc://|mailto:|www\\.)%1+|%1+\\.[a-z]{2,4}(?:?=/%1+|\\b))%2").arg(urlChars, urlEnd)),
61     QRegExp(QString("\\b(%1%2(?:/%3*)?)%4").arg(scheme, authority, urlChars, urlEnd), Qt::CaseInsensitive),
62
63     // Channel name
64     // We don't match for channel names starting with + or &, because that gives us a lot of false positives.
65     QRegExp("((?:#|![A-Z0-9]{5})[^,:\\s]+(?::[^,:\\s]+)?)\\b", Qt::CaseInsensitive)
66
67     // TODO: Nicks, we'll need a filtering for only matching known nicknames further down if we do this
68   };
69
70   static const int regExpCount = 2;  // number of regexps in the array above
71
72   qint16 matches[] = { 0, 0, 0 };
73   qint16 matchEnd[] = { 0, 0, 0 };
74
75   ClickableList result;
76   //QString str = data(ChatLineModel::DisplayRole).toString();
77
78   qint16 idx = 0;
79   qint16 minidx;
80   int type = -1;
81
82   do {
83     type = -1;
84     minidx = str.length();
85     for(int i = 0; i < regExpCount; i++) {
86       if(matches[i] < 0 || matchEnd[i] > str.length()) continue;
87       if(idx >= matchEnd[i]) {
88         matches[i] = regExp[i].indexIn(str, qMax(matchEnd[i], idx));
89         if(matches[i] >= 0) matchEnd[i] = matches[i] + regExp[i].cap(1).length();
90       }
91       if(matches[i] >= 0 && matches[i] < minidx) {
92         minidx = matches[i];
93         type = i;
94       }
95     }
96     if(type >= 0) {
97       idx = matchEnd[type];
98       QString match = str.mid(matches[type], matchEnd[type] - matches[type]);
99       if(type == Clickable::Url && str.at(idx-1) == ')') {  // special case: closing paren only matches if we had an open one
100         if(!match.contains('(')) {
101           matchEnd[type]--;
102           match.chop(1);
103         }
104       }
105       if(type == Clickable::Channel) {
106         // don't make clickable if it could be a #number
107         if(QRegExp("^#\\d+$").exactMatch(match))
108           continue;
109       }
110       result.append(Clickable((Clickable::Type)type, matches[type], matchEnd[type] - matches[type]));
111     }
112   } while(type >= 0);
113   return result;
114 }
115
116 Clickable ClickableList::atCursorPos(int idx) {
117   foreach(const Clickable &click, *this) {
118     if(idx >= click.start() && idx < click.start() + click.length())
119       return click;
120   }
121   return Clickable();
122 }