/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   joris.guisson@gmail.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/

#include "logviewer.h"

#include <QApplication>
#include <QBoxLayout>
#include <QIcon>
#include <QMenu>

#include <KConfig>
#include <KLocalizedString>

#include "logflags.h"
#include "logviewerpluginsettings.h"


namespace kt
{

    LogViewer::LogViewer(LogFlags* flags, QWidget* parent) : Activity(i18n("Log"), QStringLiteral("utilities-log-viewer"), 100, parent), use_rich_text(true), flags(flags), suspended(false), menu(nullptr), max_block_count(200)
    {
        setToolTip(i18n("View the logging output generated by KTorrent"));
        QVBoxLayout* layout = new QVBoxLayout(this);
        output = new QTextBrowser(this);
        layout->setMargin(0);
        layout->setSpacing(0);
        layout->addWidget(output);
        output->document()->setMaximumBlockCount(max_block_count);
        output->setContextMenuPolicy(Qt::CustomContextMenu);
        connect(output, &QTextBrowser::customContextMenuRequested, this, &LogViewer::showMenu);

        suspend_action = new QAction(QIcon::fromTheme(QStringLiteral("media-playback-pause")), i18n("Suspend Output"), this);
        suspend_action->setCheckable(true);
        connect(suspend_action, &QAction::toggled, this, &LogViewer::suspend);
    }


    LogViewer::~LogViewer()
    {
    }


    void LogViewer::message(const QString& line, unsigned int arg)
    {
        if (suspended)
            return;

        /*
            IMPORTANT: because QTextBrowser is not thread safe, we must use the Qt event mechanism
            to add strings to it, this will ensure that strings will only be added in the main application
            thread.
        */
        if (arg == 0x00 || flags->checkFlags(arg))
        {
            if (!mutex.tryLock()) // Drop the message if we cannot acquire the lock, no deadlocks
                return;

            if (use_rich_text)
            {
                pending.append(flags->getFormattedMessage(arg, line));
            }
            else
            {
                pending.append(line);
            }

            while (pending.size() > max_block_count)
                pending.pop_front();
            mutex.unlock();
        }
    }

    void LogViewer::processPending()
    {
        // Copy to tmp list so that we do not get a deadlock when Qt tries to print something when we add lines to the output
        QStringList tmp;
        {
            if (!mutex.tryLock()) // No deadlocks
                return;

            tmp = pending;
            pending.clear();
            mutex.unlock();
        }

        foreach (const QString& line, tmp)
        {
            QTextCharFormat fm = output->currentCharFormat();
            output->append(line);
            output->setCurrentCharFormat(fm);
        }
    }

    void LogViewer::setRichText(bool val)
    {
        use_rich_text = val;
    }

    void LogViewer::setMaxBlockCount(int max)
    {
        max_block_count = max;
        output->document()->setMaximumBlockCount(max);
    }


    void LogViewer::showMenu(const QPoint& pos)
    {
        if (!menu)
        {
            menu = output->createStandardContextMenu();
            QAction* first = menu->actions().at(0);
            QAction* sep = menu->insertSeparator(first);
            menu->insertAction(sep, suspend_action);
        }
        menu->popup(output->viewport()->mapToGlobal(pos));
    }

    void LogViewer::suspend(bool on)
    {
        suspended = on;
        QTextCharFormat fm = output->currentCharFormat();
        if (on)
            output->append(i18n("<font color=\"#FF0000\">Logging output suspended</font>"));
        else
            output->append(i18n("<font color=\"#00FF00\">Logging output resumed</font>"));
        output->setCurrentCharFormat(fm);
    }


}
