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