366 |
366 |
367 // unlinked nick (if own one) |
367 // unlinked nick (if own one) |
368 return QString("<span class=\"nick\">%1</span>").arg(Qt::escape(nickname)); |
368 return QString("<span class=\"nick\">%1</span>").arg(Qt::escape(nickname)); |
369 } |
369 } |
370 |
370 |
371 |
371 const QRegExp HWChatWidget::URLREGEXP = QRegExp("(http://)?(www\\.)?((hedgewars\\.org|code\\.google\\.com|googlecode\\.com|hh\\.unit22\\.org)(/[^ ]*)?)"); |
372 void HWChatWidget::onChatString(const QString& str) |
372 |
373 { |
373 bool HWChatWidget::containsHighlight(const QString & sender, const QString & message) |
374 onChatString("", str); |
374 { |
375 } |
375 if ((sender != m_userNick) && (!m_userNick.isEmpty())) |
376 |
376 { |
377 const QRegExp HWChatWidget::URLREGEXP = QRegExp("(http://)?(www\\.)?(hedgewars\\.org(/[^ ]*)?)"); |
377 QString lcStr = message.toLower(); |
378 |
|
379 void HWChatWidget::onChatString(const QString& nick, const QString& str) |
|
380 { |
|
381 QSortFilterProxyModel * playersSortFilterModel = qobject_cast<QSortFilterProxyModel *>(chatNicks->model()); |
|
382 if(!playersSortFilterModel) |
|
383 return; |
|
384 |
|
385 PlayersListModel * players = qobject_cast<PlayersListModel *>(playersSortFilterModel->sourceModel()); |
|
386 |
|
387 if(!players) |
|
388 return; |
|
389 |
|
390 if (!nick.isEmpty()) |
|
391 { |
|
392 // don't show chat lines that are from ignored nicks |
|
393 if (players->isFlagSet(nick, PlayersListModel::Ignore)) |
|
394 return; |
|
395 } |
|
396 |
|
397 bool isFriend = (!nick.isEmpty()) && players->isFlagSet(nick, PlayersListModel::Friend); |
|
398 |
|
399 QString formattedStr = Qt::escape(str.mid(1)); |
|
400 // make hedgewars.org urls actual links |
|
401 formattedStr = formattedStr.replace(URLREGEXP, "<a href=\"http://\\3\">\\3</a>"); |
|
402 |
|
403 // link the nick |
|
404 if(!nick.isEmpty()) |
|
405 formattedStr.replace("|nick|", linkedNick(nick)); |
|
406 |
|
407 QString cssClass("msg_UserChat"); |
|
408 |
|
409 // check first character for color code and set color properly |
|
410 char c = str[0].toAscii(); |
|
411 switch (c) |
|
412 { |
|
413 case 3: |
|
414 cssClass = (isFriend ? "msg_FriendJoin" : "msg_UserJoin"); |
|
415 break; |
|
416 case 2: |
|
417 cssClass = (isFriend ? "msg_FriendAction" : "msg_UserAction"); |
|
418 break; |
|
419 default: |
|
420 if (isFriend) |
|
421 cssClass = "msg_FriendChat"; |
|
422 } |
|
423 |
|
424 bool isHL = false; |
|
425 |
|
426 if ((c != 3) && (!nick.isEmpty()) && |
|
427 (nick != m_userNick) && (!m_userNick.isEmpty())) |
|
428 { |
|
429 QString lcStr = str.toLower(); |
|
430 |
378 |
431 foreach (const QRegExp & hl, m_highlights) |
379 foreach (const QRegExp & hl, m_highlights) |
432 { |
380 { |
433 if (lcStr.contains(hl)) |
381 if (lcStr.contains(hl)) |
434 { |
382 return true; |
435 isHL = true; |
|
436 break; |
|
437 } |
|
438 } |
383 } |
439 } |
384 } |
440 |
385 return false; |
441 addLine(cssClass, formattedStr, isHL); |
386 } |
|
387 |
|
388 QString HWChatWidget::messageToHTML(const QString & message) |
|
389 { |
|
390 QString formattedStr = Qt::escape(message); |
|
391 // link some urls |
|
392 formattedStr = formattedStr.replace(URLREGEXP, "<a href=\"http://\\3\">\\3</a>"); |
|
393 return formattedStr; |
|
394 } |
|
395 |
|
396 void HWChatWidget::onChatAction(const QString & nick, const QString & action) |
|
397 { |
|
398 printChatString(nick, "* " + linkedNick(nick) + " " + messageToHTML(action), "Action", containsHighlight(nick, action)); |
|
399 } |
|
400 |
|
401 void HWChatWidget::onChatMessage(const QString & nick, const QString & message) |
|
402 { |
|
403 printChatString(nick, linkedNick(nick) + ": " + messageToHTML(message), "Chat", containsHighlight(nick, message)); |
|
404 } |
|
405 |
|
406 void HWChatWidget::printChatString( |
|
407 const QString & nick, const QString & str, const QString & cssClassPart, bool highlight) |
|
408 { |
|
409 QSortFilterProxyModel * playersSortFilterModel = qobject_cast<QSortFilterProxyModel *>(chatNicks->model()); |
|
410 if(!playersSortFilterModel) |
|
411 return; |
|
412 |
|
413 PlayersListModel * players = qobject_cast<PlayersListModel *>(playersSortFilterModel->sourceModel()); |
|
414 |
|
415 if(!players) |
|
416 return; |
|
417 |
|
418 // don't show chat lines that are from ignored nicks |
|
419 if (players->isFlagSet(nick, PlayersListModel::Ignore)) |
|
420 return; |
|
421 |
|
422 bool isFriend = (!nick.isEmpty()) && players->isFlagSet(nick, PlayersListModel::Friend); |
|
423 |
|
424 QString cssClass = (isFriend ? "msg_Friend" : "msg_User") + cssClassPart; |
|
425 |
|
426 addLine(cssClass, str, highlight); |
442 } |
427 } |
443 |
428 |
444 void HWChatWidget::addLine(const QString & cssClass, QString line, bool isHighlight) |
429 void HWChatWidget::addLine(const QString & cssClass, QString line, bool isHighlight) |
445 { |
430 { |
446 if (s_displayNone->contains(cssClass)) |
431 if (s_displayNone->contains(cssClass)) |
510 if ((!isIgnored) && (nick != m_userNick)) // don't auto-complete own name |
495 if ((!isIgnored) && (nick != m_userNick)) // don't auto-complete own name |
511 chatEditLine->addNickname(nick); |
496 chatEditLine->addNickname(nick); |
512 |
497 |
513 emit nickCountUpdate(chatNicks->model()->rowCount()); |
498 emit nickCountUpdate(chatNicks->model()->rowCount()); |
514 |
499 |
|
500 if (!isIgnored) |
|
501 printChatString(nick, QString("*** ") + tr("%1 has joined").arg(linkedNick(nick)), "Join", false); |
|
502 |
515 if (notifyNick && notify && (m_helloSounds.size() > 0)) |
503 if (notifyNick && notify && (m_helloSounds.size() > 0)) |
516 { |
504 { |
517 SDLInteraction::instance().playSoundFile( |
505 SDLInteraction::instance().playSoundFile( |
518 m_helloSounds.at(rand() % m_helloSounds.size())); |
506 m_helloSounds.at(rand() % m_helloSounds.size())); |
519 } |
507 } |
520 } |
508 } |
521 |
509 |
522 void HWChatWidget::nickRemoved(const QString& nick) |
510 void HWChatWidget::nickRemoved(const QString& nick) |
523 { |
511 { |
|
512 nickRemoved(nick, ""); |
|
513 } |
|
514 |
|
515 void HWChatWidget::nickRemoved(const QString& nick, const QString & message) |
|
516 { |
524 chatEditLine->removeNickname(nick); |
517 chatEditLine->removeNickname(nick); |
525 |
518 |
526 emit nickCountUpdate(chatNicks->model()->rowCount()); |
519 emit nickCountUpdate(chatNicks->model()->rowCount()); |
|
520 |
|
521 if (message.isEmpty()) |
|
522 printChatString(nick, QString("*** ") + tr("%1 has left").arg(linkedNick(nick)), "Leave", false); |
|
523 else |
|
524 printChatString(nick, QString("*** ") + tr("%1 has left (%2)").arg(linkedNick(nick)).arg(messageToHTML(message)), "Leave", false); |
527 } |
525 } |
528 |
526 |
529 void HWChatWidget::clear() |
527 void HWChatWidget::clear() |
530 { |
528 { |
531 chatEditLine->reset(); |
529 chatEditLine->reset(); |