PageRenderTime 43ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/quassel-0.7.3/src/qtui/qtuimessageprocessor.cpp

#
C++ | 170 lines | 130 code | 20 blank | 20 comment | 31 complexity | cd19c195303af03bb34675a88786d071 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0
  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. #include "qtuimessageprocessor.h"
  21. #include "client.h"
  22. #include "clientsettings.h"
  23. #include "identity.h"
  24. #include "messagemodel.h"
  25. #include "network.h"
  26. const int progressUpdateDelay = 100; // ms between progress signal updates
  27. QtUiMessageProcessor::QtUiMessageProcessor(QObject *parent)
  28. : AbstractMessageProcessor(parent),
  29. _processing(false),
  30. _processMode(TimerBased)
  31. {
  32. NotificationSettings notificationSettings;
  33. _nicksCaseSensitive = notificationSettings.nicksCaseSensitive();
  34. _highlightNick = notificationSettings.highlightNick();
  35. highlightListChanged(notificationSettings.highlightList());
  36. notificationSettings.notify("Highlights/NicksCaseSensitive", this, SLOT(nicksCaseSensitiveChanged(const QVariant &)));
  37. notificationSettings.notify("Highlights/CustomList", this, SLOT(highlightListChanged(const QVariant &)));
  38. notificationSettings.notify("Highlights/HighlightNick", this, SLOT(highlightNickChanged(const QVariant &)));
  39. _processTimer.setInterval(0);
  40. connect(&_processTimer, SIGNAL(timeout()), this, SLOT(processNextMessage()));
  41. }
  42. void QtUiMessageProcessor::reset() {
  43. if(processMode() == TimerBased) {
  44. if(_processTimer.isActive()) _processTimer.stop();
  45. _processing = false;
  46. _currentBatch.clear();
  47. _processQueue.clear();
  48. }
  49. }
  50. void QtUiMessageProcessor::process(Message &msg) {
  51. checkForHighlight(msg);
  52. preProcess(msg);
  53. Client::messageModel()->insertMessage(msg);
  54. }
  55. void QtUiMessageProcessor::process(QList<Message> &msgs) {
  56. QList<Message>::iterator msgIter = msgs.begin();
  57. QList<Message>::iterator msgIterEnd = msgs.end();
  58. while(msgIter != msgIterEnd) {
  59. checkForHighlight(*msgIter);
  60. preProcess(*msgIter);
  61. msgIter++;
  62. }
  63. Client::messageModel()->insertMessages(msgs);
  64. return;
  65. if(msgs.isEmpty()) return;
  66. _processQueue.append(msgs);
  67. if(!isProcessing())
  68. startProcessing();
  69. }
  70. void QtUiMessageProcessor::startProcessing() {
  71. if(processMode() == TimerBased) {
  72. if(_currentBatch.isEmpty() && _processQueue.isEmpty())
  73. return;
  74. _processing = true;
  75. if(!_processTimer.isActive())
  76. _processTimer.start();
  77. }
  78. }
  79. void QtUiMessageProcessor::processNextMessage() {
  80. if(_currentBatch.isEmpty()) {
  81. if(_processQueue.isEmpty()) {
  82. _processTimer.stop();
  83. _processing = false;
  84. return;
  85. }
  86. _currentBatch = _processQueue.takeFirst();
  87. }
  88. Message msg = _currentBatch.takeFirst();
  89. process(msg);
  90. }
  91. void QtUiMessageProcessor::checkForHighlight(Message &msg) {
  92. if(!((msg.type() & (Message::Plain | Message::Notice | Message::Action)) && !(msg.flags() & Message::Self)))
  93. return;
  94. // TODO: Cache this (per network)
  95. const Network *net = Client::network(msg.bufferInfo().networkId());
  96. if(net && !net->myNick().isEmpty()) {
  97. QStringList nickList;
  98. if(_highlightNick == NotificationSettings::CurrentNick) {
  99. nickList << net->myNick();
  100. } else if(_highlightNick == NotificationSettings::AllNicks) {
  101. const Identity *myIdentity = Client::identity(net->identity());
  102. if(myIdentity)
  103. nickList = myIdentity->nicks();
  104. if(!nickList.contains(net->myNick()))
  105. nickList.prepend(net->myNick());
  106. }
  107. foreach(QString nickname, nickList) {
  108. QRegExp nickRegExp("(^|\\W)" + QRegExp::escape(nickname) + "(\\W|$)", _nicksCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
  109. if(nickRegExp.indexIn(msg.contents()) >= 0) {
  110. msg.setFlags(msg.flags() | Message::Highlight);
  111. return;
  112. }
  113. }
  114. for(int i = 0; i < _highlightRules.count(); i++) {
  115. const HighlightRule &rule = _highlightRules.at(i);
  116. if(!rule.isEnabled)
  117. continue;
  118. QRegExp rx;
  119. if(rule.isRegExp) {
  120. rx = QRegExp(rule.name, rule.caseSensitive? Qt::CaseSensitive : Qt::CaseInsensitive);
  121. } else {
  122. rx = QRegExp("(^|\\W)" + QRegExp::escape(rule.name) + "(\\W|$)", rule.caseSensitive? Qt::CaseSensitive : Qt::CaseInsensitive);
  123. }
  124. bool match = (rx.indexIn(msg.contents()) >= 0);
  125. if(match) {
  126. msg.setFlags(msg.flags() | Message::Highlight);
  127. return;
  128. }
  129. }
  130. }
  131. }
  132. void QtUiMessageProcessor::nicksCaseSensitiveChanged(const QVariant &variant) {
  133. _nicksCaseSensitive = variant.toBool();
  134. }
  135. void QtUiMessageProcessor::highlightListChanged(const QVariant &variant) {
  136. QVariantList varList = variant.toList();
  137. _highlightRules.clear();
  138. QVariantList::const_iterator iter = varList.constBegin();
  139. while(iter != varList.constEnd()) {
  140. QVariantMap rule = iter->toMap();
  141. _highlightRules << HighlightRule(rule["Name"].toString(),
  142. rule["Enable"].toBool(),
  143. rule["CS"].toBool() ? Qt::CaseSensitive : Qt::CaseInsensitive,
  144. rule["RegEx"].toBool());
  145. iter++;
  146. }
  147. }
  148. void QtUiMessageProcessor::highlightNickChanged(const QVariant &variant) {
  149. _highlightNick = (NotificationSettings::HighlightNickType)variant.toInt();
  150. }