PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/uisupport/clickable.cpp

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