Esempio n. 1
0
void MainWindow::read() {
  // Массив для полученных данных
  QByteArray data;
  // Устанавливаем массиву размер соответствующий размеру полученного пакета данных
  data.resize(socket->pendingDatagramSize());
  QHostAddress* address = new QHostAddress();
  socket->readDatagram(data.data(), data.size(), address);
  qDebug() << "Message from IP: " << address->toString() << " " << data.size();

  // Разбор полученного пакета
  QDataStream in(&data, QIODevice::ReadOnly);

  // Получаем тип пакета
  qint8 type = 0;
  in >> type;

  if (type == USUAL_MESSAGE) {
    QString str;
    in >> str;

    if(str.length() == 0)
      return;

    // Записываем входящие сообщения в файл
    saveToLogFile(str);

    // Отображаем строчку в интерфейсе
    ui->plainTextEdit->appendPlainText(str);
  } else if (type == PERSON_ONLINE) {
Esempio n. 2
0
void ChatTab::chatLog(std::string line,
                      ChatMsgTypeT own,
                      const IgnoreRecord ignoreRecord,
                      const TryRemoveColors tryRemoveColors)
{
    // Trim whitespace
    trim(line);

    if (line.empty())
        return;

    if (tryRemoveColors == TryRemoveColors_true &&
        own == ChatMsgType::BY_OTHER &&
        config.getBoolValue("removeColors"))
    {
        line = removeColors(line);
        if (line.empty())
            return;
    }

    const unsigned lineLim = config.getIntValue("chatMaxCharLimit");
    if (lineLim > 0 && line.length() > lineLim)
        line = line.substr(0, lineLim);

    if (line.empty())
        return;

    CHATLOG tmp;
    tmp.own = own;
    tmp.nick.clear();
    tmp.text = line;

    const size_t pos = line.find(" : ");
    if (pos != std::string::npos)
    {
        if (line.length() <= pos + 3)
            return;

        tmp.nick = line.substr(0, pos);
        tmp.text = line.substr(pos + 3);
    }
    else
    {
        // Fix the owner of welcome message.
        if (line.length() > 7 && line.substr(0, 7) == "Welcome")
            own = ChatMsgType::BY_SERVER;
    }

    // *implements actions in a backwards compatible way*
    if ((own == ChatMsgType::BY_PLAYER || own == ChatMsgType::BY_OTHER) &&
        tmp.text.at(0) == '*' &&
        tmp.text.at(tmp.text.length()-1) == '*')
    {
        tmp.text[0] = ' ';
        tmp.text.erase(tmp.text.length() - 1);
        own = ChatMsgType::ACT_IS;
    }

    std::string lineColor("##C");
    switch (own)
    {
        case ChatMsgType::BY_GM:
            if (tmp.nick.empty())
            {
                // TRANSLATORS: chat message
                tmp.nick = std::string(_("Global announcement:")).append(" ");
                lineColor = "##G";
            }
            else
            {
                // TRANSLATORS: chat message
                tmp.nick = strprintf(_("Global announcement from %s:"),
                                     tmp.nick.c_str()).append(" ");
                lineColor = "##g";  // Equiv. to BrowserBox::RED
            }
            break;
        case ChatMsgType::BY_PLAYER:
            tmp.nick.append(": ");
            lineColor = "##Y";
            break;
        case ChatMsgType::BY_OTHER:
        case ChatMsgType::BY_UNKNOWN:
            tmp.nick.append(": ");
            lineColor = "##C";
            break;
        case ChatMsgType::BY_SERVER:
            // TRANSLATORS: chat message
            tmp.nick.clear();
            tmp.text = line;
            lineColor = "##S";
            break;
        case ChatMsgType::BY_CHANNEL:
            tmp.nick.clear();
            lineColor = "##2";  // Equiv. to BrowserBox::GREEN
            break;
        case ChatMsgType::ACT_WHISPER:
            // TRANSLATORS: chat message
            tmp.nick = strprintf(_("%s whispers: %s"), tmp.nick.c_str(), "");
            lineColor = "##W";
            break;
        case ChatMsgType::ACT_IS:
            lineColor = "##I";
            break;
        case ChatMsgType::BY_LOGGER:
            tmp.nick.clear();
            tmp.text = line;
            lineColor = "##L";
            break;
        default:
            break;
    }

    if (tmp.nick == ": ")
    {
        tmp.nick.clear();
        lineColor = "##S";
    }

    // if configured, move magic messages log to debug chat tab
    if (!serverFeatures->haveChatChannels()
        && localChatTab && this == localChatTab
        && ((config.getBoolValue("showMagicInDebug")
        && own == ChatMsgType::BY_PLAYER
        && tmp.text.length() > 1
        && tmp.text.at(0) == '#'
        && tmp.text.at(1) != '#')
        || (config.getBoolValue("serverMsgInDebug")
        && (own == ChatMsgType::BY_SERVER
        || tmp.nick.empty()))))
    {
        if (debugChatTab)
            debugChatTab->chatLog(line, own, ignoreRecord, tryRemoveColors);
        return;
    }

    // Get the current system time
    time_t t;
    time(&t);

    if (config.getBoolValue("useLocalTime"))
    {
        const tm *const timeInfo = localtime(&t);
        if (timeInfo)
        {
            line = strprintf("%s[%02d:%02d] %s%s", lineColor.c_str(),
                timeInfo->tm_hour, timeInfo->tm_min, tmp.nick.c_str(),
                tmp.text.c_str());
        }
        else
        {
            line = strprintf("%s %s%s", lineColor.c_str(),
                tmp.nick.c_str(), tmp.text.c_str());
        }
    }
    else
    {
        // Format the time string properly
        std::stringstream timeStr;
        timeStr << "[" << ((((t / 60) / 60) % 24 < 10) ? "0" : "")
            << CAST_S32(((t / 60) / 60) % 24)
            << ":" << (((t / 60) % 60 < 10) ? "0" : "")
            << CAST_S32((t / 60) % 60)
            << "] ";
        line = std::string(lineColor).append(timeStr.str()).append(
            tmp.nick).append(tmp.text);
    }

    if (config.getBoolValue("enableChatLog"))
        saveToLogFile(line);

    mTextOutput->setMaxRow(config.getIntValue("chatMaxLinesLimit"));

    // We look if the Vertical Scroll Bar is set at the max before
    // adding a row, otherwise the max will always be a row higher
    // at comparison.
    if (mScrollArea->getVerticalScrollAmount() + 2 >=
        mScrollArea->getVerticalMaxScroll())
    {
        addRow(line);
        mScrollArea->setVerticalScrollAmount(
            mScrollArea->getVerticalMaxScroll());
    }
    else
    {
        addRow(line);
    }

    if (chatWindow && this == localChatTab)
        chatWindow->addToAwayLog(line);

    mScrollArea->logic();
    if (own != ChatMsgType::BY_PLAYER)
    {
        if (own == ChatMsgType::BY_SERVER &&
            (getType() == ChatTabType::PARTY ||
            getType() == ChatTabType::CHANNEL ||
            getType() == ChatTabType::GUILD))
        {
            return;
        }

        const TabbedArea *const tabArea = getTabbedArea();
        if (!tabArea)
            return;

        const bool notFocused = WindowManager::getIsMinimized() ||
            (!settings.mouseFocused && !settings.inputFocused);

        if (this != tabArea->getSelectedTab() || notFocused)
        {
            if (getFlash() == 0)
            {
                if (chatWindow && chatWindow->findHighlight(tmp.text))
                {
                    setFlash(2);
                    soundManager.playGuiSound(SOUND_HIGHLIGHT);
                }
                else
                {
                    setFlash(1);
                }
            }
            else if (getFlash() == 2)
            {
                if (chatWindow && chatWindow->findHighlight(tmp.text))
                    soundManager.playGuiSound(SOUND_HIGHLIGHT);
            }
        }

        if ((getAllowHighlight() ||
            own == ChatMsgType::BY_GM) &&
            (this != tabArea->getSelectedTab() ||
            notFocused))
        {
            if (own == ChatMsgType::BY_GM)
            {
                if (chatWindow)
                    chatWindow->unHideWindow();
                soundManager.playGuiSound(SOUND_GLOBAL);
            }
            else if (own != ChatMsgType::BY_SERVER)
            {
                if (chatWindow)
                    chatWindow->unHideWindow();
                playNewMessageSound();
            }
            WindowManager::newChatMessage();
        }
    }
}
Esempio n. 3
0
void ChatTab::chatLog(std::string line, Own own, bool ignoreRecord)
{
    // Trim whitespace
    trim(line);

    if (line.empty())
        return;

    CHATLOG tmp;
    tmp.own = own;
    tmp.nick = "";
    tmp.text = line;

    std::string::size_type pos = line.find(" : ");
    if (pos != std::string::npos)
    {
        tmp.nick = line.substr(0, pos);
        tmp.text = line.substr(pos + 3);
    }
    else
    {
        // Fix the owner of welcome message.
        if (line.substr(0, 7) == "Welcome")
        {
            own = BY_SERVER;
        }
    }

    // *implements actions in a backwards compatible way*
    if ((own == BY_PLAYER || own == BY_OTHER) &&
        tmp.text.at(0) == '*' &&
        tmp.text.at(tmp.text.length()-1) == '*')
    {
        tmp.text[0] = ' ';
        tmp.text.erase(tmp.text.length() - 1);
        own = ACT_IS;
    }

    std::string lineColor = "##C";
    switch (own)
    {
        case BY_GM:
            if (tmp.nick.empty())
            {
                tmp.nick = std::string(_("Global announcement:"));
                tmp.nick += " ";
                lineColor = "##G";
            }
            else
            {
                tmp.nick = strprintf(_("Global announcement from %s:"),
                                     tmp.nick.c_str());
                tmp.nick += " ";
                lineColor = "##1"; // Equiv. to BrowserBox::RED
            }
            break;
        case BY_PLAYER:
            tmp.nick += ": ";
            lineColor = "##Y";
            break;
        case BY_OTHER:
            tmp.nick += ": ";
            lineColor = "##C";
            break;
        case BY_SERVER:
            tmp.nick = _("Server:");
            tmp.nick += " ";
            tmp.text = line;
            lineColor = "##S";
            break;
        case BY_CHANNEL:
            tmp.nick = "";
            // TODO: Use a predefined color
            lineColor = "##2"; // Equiv. to BrowserBox::GREEN
            break;
        case ACT_WHISPER:
            tmp.nick = strprintf(_("%s whispers: %s"), tmp.nick.c_str(), "");
            lineColor = "##W";
            break;
        case ACT_IS:
            lineColor = "##I";
            break;
        case BY_LOGGER:
            tmp.nick = "";
            tmp.text = line;
            lineColor = "##L";
            break;
    }

    if (tmp.nick == ": ")
    {
        tmp.nick = "";
        lineColor = "##S";
    }

    // Get the current system time
    time_t t;
    time(&t);

    // Format the time string properly
    std::stringstream timeStr;
    timeStr << "[" << ((((t / 60) / 60) % 24 < 10) ? "0" : "")
        << (int) (((t / 60) / 60) % 24)
        << ":" << (((t / 60) % 60 < 10) ? "0" : "")
        << (int) ((t / 60) % 60)
        << "] ";

    line = lineColor + timeStr.str() + tmp.nick + tmp.text;

    if (config.getBoolValue("enableChatLog"))
        saveToLogFile(line);

    // We look if the Vertical Scroll Bar is set at the max before
    // adding a row, otherwise the max will always be a row higher
    // at comparison.
    if (mScrollArea->getVerticalScrollAmount() >= mScrollArea->getVerticalMaxScroll())
    {
        addRow(line);
        mScrollArea->setVerticalScrollAmount(mScrollArea->getVerticalMaxScroll());
    }
    else
    {
        addRow(line);
    }

    mScrollArea->logic();
    chatWindow->mRecorder->record(line.substr(3));
    if (this != getTabbedArea()->getSelectedTab() &&
        own != BY_PLAYER)
        setFlash(true);
}