You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kaffeine/kaffeine/src/player-parts/libmpv-part/libmpv_log.cpp

145 lines
4.2 KiB

/*
* Kaffeine libmpv part
* Copyright (C) 2023 Mavridis Philippe <mavridisf@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
*/
// TQt
#include <tqapplication.h>
#include <tqclipboard.h>
#include <tqlistview.h>
#include <tqtextstream.h>
// TDE
#include <tdemessagebox.h>
#include <kiconloader.h>
#include <tdeglobal.h>
#include <tdelocale.h>
// Part
#include "libmpv_event.h"
#include "libmpv_part.h"
#include "libmpv_log.h"
MpvLogViewerDlg::MpvLogViewerDlg(MpvPart *part)
: KDialogBase(0, 0, false, i18n("MPV log"),
KDialogBase::User1 | KDialogBase::User2 | KDialogBase::User3 | KDialogBase::Close,
KDialogBase::Close, false,
KGuiItem(i18n("Clear"), "clear_left", "Clear MPV event log"),
KGuiItem(i18n("Refresh"), "reload", "Refresh MPV event log"),
KGuiItem(i18n("Copy"), "edit-copy", "Copy selected line")),
m_part(part)
{
m_logView = new TQListView(this);
m_logView->addColumn("Level");
m_logView->addColumn("Timestamp");
m_logView->addColumn("Prefix");
m_logView->addColumn("Message");
m_logView->setResizeMode(TQListView::LastColumn);
m_logView->setSorting(1);
m_logView->setAllColumnsShowFocus(true);
setMainWidget(m_logView);
resize(500, 250);
refreshLog();
}
MpvLogViewerDlg::~MpvLogViewerDlg() {
delete m_logView;
}
void MpvLogViewerDlg::refreshLog() {
TDEIconLoader *icoLoad = TDEGlobal::iconLoader();
m_logView->clear();
TQValueList<struct MpvEventData>::iterator it;
for (it = m_part->m_eventLog.begin();
it != m_part->m_eventLog.end();
++it)
{
TQString timestamp ((*it).timestamp.toString()),
level ((*it).level),
prefix ((*it).prefix),
message ((*it).text.replace("\n", ""));
TQListViewItem *li = new TQListViewItem(m_logView, level, timestamp,
prefix, message);
TQString icon;
if (level == "error" || level == "fatal") {
icon = "messagebox_critical";
}
else if (level == "warn") {
icon = "messagebox_warning";
}
else {
icon = "messagebox_info";
}
li->setPixmap(0, icoLoad->loadIcon(icon, TDEIcon::Small));
}
}
/*** Clear ***/
void MpvLogViewerDlg::slotUser1() {
int ret = KMessageBox::questionYesNo(this,
i18n("Would you like to clear the MPV log?"),
i18n("Clear log?"));
if (ret == KMessageBox::Yes) {
m_part->m_eventLog.clear();
refreshLog();
}
}
/*** Refresh ***/
void MpvLogViewerDlg::slotUser2() {
refreshLog();
}
/*** Copy selection ***/
void MpvLogViewerDlg::slotUser3() {
TQClipboard *clip = TQApplication::clipboard();
TQString logLineTemplate = TQString("[%1] (%2 %3) %4");
TQListViewItem *sel = m_logView->selectedItem();
if (!sel) {
int ret = KMessageBox::warningYesNo(this,
i18n("Would you like to copy the whole MPV log?"),
i18n("Copy whole log?"));
if (ret != KMessageBox::Yes) {
return;
}
}
TQString out;
TQTextStream ts(&out, IO_WriteOnly);
TQListViewItemIterator it(m_logView);
while (it.current()) {
if (!sel || it.current()->isSelected()) {
ts << logLineTemplate.arg(
(*it)->text(1), (*it)->text(0),
(*it)->text(2), (*it)->text(3))
<< endl;
}
++it;
}
clip->setText(out);
}