Browse Source

Clean up code

adaptive-webui-19844
Chocobo1 6 years ago
parent
commit
a3019f56b0
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 15
      src/app/filelogger.cpp
  2. 33
      src/gui/executionlogwidget.cpp
  3. 3
      src/gui/executionlogwidget.ui
  4. 15
      src/gui/loglistwidget.cpp
  5. 2
      src/gui/loglistwidget.h

15
src/app/filelogger.cpp

@ -66,10 +66,9 @@ FileLogger::~FileLogger()
void FileLogger::changePath(const QString &newPath) void FileLogger::changePath(const QString &newPath)
{ {
QString tmpPath = Utils::Fs::fromNativePath(newPath); const QDir dir(newPath);
QDir dir(tmpPath); dir.mkpath(newPath);
dir.mkpath(tmpPath); const QString tmpPath = dir.absoluteFilePath("qbittorrent.log");
tmpPath = dir.absoluteFilePath("qbittorrent.log");
if (tmpPath != m_path) { if (tmpPath != m_path) {
m_path = tmpPath; m_path = tmpPath;
@ -87,8 +86,10 @@ void FileLogger::deleteOld(const int age, const FileLogAgeType ageType)
{ {
const QDateTime date = QDateTime::currentDateTime(); const QDateTime date = QDateTime::currentDateTime();
const QDir dir(Utils::Fs::branchPath(m_path)); const QDir dir(Utils::Fs::branchPath(m_path));
const QFileInfoList fileList = dir.entryInfoList(QStringList("qbittorrent.log.bak*")
, (QDir::Files | QDir::Writable), (QDir::Time | QDir::Reversed));
for (const QFileInfo &file : asConst(dir.entryInfoList(QStringList("qbittorrent.log.bak*"), QDir::Files | QDir::Writable, QDir::Time | QDir::Reversed))) { for (const QFileInfo &file : fileList) {
QDateTime modificationDate = file.lastModified(); QDateTime modificationDate = file.lastModified();
switch (ageType) { switch (ageType) {
case DAYS: case DAYS:
@ -106,7 +107,7 @@ void FileLogger::deleteOld(const int age, const FileLogAgeType ageType)
} }
} }
void FileLogger::setBackup(bool value) void FileLogger::setBackup(const bool value)
{ {
m_backup = value; m_backup = value;
} }
@ -168,7 +169,7 @@ void FileLogger::openLogFile()
|| !m_logFile->setPermissions(QFile::ReadOwner | QFile::WriteOwner)) { || !m_logFile->setPermissions(QFile::ReadOwner | QFile::WriteOwner)) {
delete m_logFile; delete m_logFile;
m_logFile = nullptr; m_logFile = nullptr;
Logger::instance()->addMessage(tr("An error occurred while trying to open the log file. Logging to file is disabled."), Log::CRITICAL); LogMsg(tr("An error occurred while trying to open the log file. Logging to file is disabled."), Log::CRITICAL);
} }
} }

33
src/gui/executionlogwidget.cpp

@ -40,7 +40,7 @@
ExecutionLogWidget::ExecutionLogWidget(QWidget *parent, const Log::MsgTypes &types) ExecutionLogWidget::ExecutionLogWidget(QWidget *parent, const Log::MsgTypes &types)
: QWidget(parent) : QWidget(parent)
, m_ui(new Ui::ExecutionLogWidget) , m_ui(new Ui::ExecutionLogWidget)
, m_msgList(new LogListWidget(MAX_LOG_MESSAGES, Log::MsgTypes(types))) , m_msgList(new LogListWidget(MAX_LOG_MESSAGES, types))
, m_peerList(new LogListWidget(MAX_LOG_MESSAGES)) , m_peerList(new LogListWidget(MAX_LOG_MESSAGES))
{ {
m_ui->setupUi(this); m_ui->setupUi(this);
@ -75,38 +75,35 @@ void ExecutionLogWidget::showMsgTypes(const Log::MsgTypes &types)
void ExecutionLogWidget::addLogMessage(const Log::Msg &msg) void ExecutionLogWidget::addLogMessage(const Log::Msg &msg)
{ {
QString text; QString colorName;
QDateTime time = QDateTime::fromMSecsSinceEpoch(msg.timestamp);
QColor color;
switch (msg.type) { switch (msg.type) {
case Log::INFO: case Log::INFO:
color.setNamedColor("blue"); colorName = QLatin1String("blue");
break; break;
case Log::WARNING: case Log::WARNING:
color.setNamedColor("orange"); colorName = QLatin1String("orange");
break; break;
case Log::CRITICAL: case Log::CRITICAL:
color.setNamedColor("red"); colorName = QLatin1String("red");
break; break;
default: default:
color = QApplication::palette().color(QPalette::WindowText); colorName = QApplication::palette().color(QPalette::WindowText).name();
} }
text = "<font color='grey'>" + time.toString(Qt::SystemLocaleShortDate) + "</font> - <font color='" + color.name() + "'>" + msg.message + "</font>"; const QDateTime time = QDateTime::fromMSecsSinceEpoch(msg.timestamp);
const QString text = QString(QLatin1String("<font color='grey'>%1</font> - <font color='%2'>%3</font>"))
.arg(time.toString(Qt::SystemLocaleShortDate), colorName, msg.message);
m_msgList->appendLine(text, msg.type); m_msgList->appendLine(text, msg.type);
} }
void ExecutionLogWidget::addPeerMessage(const Log::Peer &peer) void ExecutionLogWidget::addPeerMessage(const Log::Peer &peer)
{ {
QString text; const QDateTime time = QDateTime::fromMSecsSinceEpoch(peer.timestamp);
QDateTime time = QDateTime::fromMSecsSinceEpoch(peer.timestamp); const QString msg = QString(QLatin1String("<font color='grey'>%1</font> - <font color='red'>%2</font>"))
.arg(time.toString(Qt::SystemLocaleShortDate), peer.ip);
if (peer.blocked)
text = "<font color='grey'>" + time.toString(Qt::SystemLocaleShortDate) + "</font> - "
+ tr("<font color='red'>%1</font> was blocked %2", "x.y.z.w was blocked").arg(peer.ip, peer.reason);
else
text = "<font color='grey'>" + time.toString(Qt::SystemLocaleShortDate) + "</font> - " + tr("<font color='red'>%1</font> was banned", "x.y.z.w was banned").arg(peer.ip);
const QString text = peer.blocked
? tr("%1 was blocked %2", "0.0.0.0 was blocked due to reason").arg(msg, peer.reason)
: tr("%1 was banned", "0.0.0.0 was banned").arg(msg);
m_peerList->appendLine(text, Log::NORMAL); m_peerList->appendLine(text, Log::NORMAL);
} }

3
src/gui/executionlogwidget.ui

@ -10,9 +10,6 @@
<height>300</height> <height>300</height>
</rect> </rect>
</property> </property>
<property name="windowTitle">
<string notr="true">Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin"> <property name="leftMargin">
<number>0</number> <number>0</number>

15
src/gui/loglistwidget.cpp

@ -39,7 +39,7 @@
#include "base/global.h" #include "base/global.h"
#include "guiiconprovider.h" #include "guiiconprovider.h"
LogListWidget::LogListWidget(int maxLines, const Log::MsgTypes &types, QWidget *parent) LogListWidget::LogListWidget(const int maxLines, const Log::MsgTypes &types, QWidget *parent)
: QListWidget(parent) : QListWidget(parent)
, m_maxLines(maxLines) , m_maxLines(maxLines)
, m_types(types) , m_types(types)
@ -47,8 +47,8 @@ LogListWidget::LogListWidget(int maxLines, const Log::MsgTypes &types, QWidget *
// Allow multiple selections // Allow multiple selections
setSelectionMode(QAbstractItemView::ExtendedSelection); setSelectionMode(QAbstractItemView::ExtendedSelection);
// Context menu // Context menu
QAction *copyAct = new QAction(GuiIconProvider::instance()->getIcon("edit-copy"), tr("Copy"), this); auto *copyAct = new QAction(GuiIconProvider::instance()->getIcon("edit-copy"), tr("Copy"), this);
QAction *clearAct = new QAction(GuiIconProvider::instance()->getIcon("edit-clear"), tr("Clear"), this); auto *clearAct = new QAction(GuiIconProvider::instance()->getIcon("edit-clear"), tr("Clear"), this);
connect(copyAct, &QAction::triggered, this, &LogListWidget::copySelection); connect(copyAct, &QAction::triggered, this, &LogListWidget::copySelection);
connect(clearAct, &QAction::triggered, this, &LogListWidget::clear); connect(clearAct, &QAction::triggered, this, &LogListWidget::clear);
addAction(copyAct); addAction(copyAct);
@ -78,10 +78,12 @@ void LogListWidget::keyPressEvent(QKeyEvent *event)
void LogListWidget::appendLine(const QString &line, const Log::MsgType &type) void LogListWidget::appendLine(const QString &line, const Log::MsgType &type)
{ {
auto *item = new QListWidgetItem;
// We need to use QLabel here to support rich text // We need to use QLabel here to support rich text
QLabel *lbl = new QLabel(line); auto *lbl = new QLabel(line);
lbl->setTextFormat(Qt::RichText);
lbl->setContentsMargins(4, 2, 4, 2); lbl->setContentsMargins(4, 2, 4, 2);
auto *item = new QListWidgetItem;
item->setSizeHint(lbl->sizeHint()); item->setSizeHint(lbl->sizeHint());
item->setData(Qt::UserRole, type); item->setData(Qt::UserRole, type);
insertItem(0, item); insertItem(0, item);
@ -96,7 +98,8 @@ void LogListWidget::appendLine(const QString &line, const Log::MsgType &type)
void LogListWidget::copySelection() void LogListWidget::copySelection()
{ {
static const QRegularExpression htmlTag("<[^>]+>"); const QRegularExpression htmlTag("<[^>]+>");
QStringList strings; QStringList strings;
for (QListWidgetItem *it : asConst(selectedItems())) for (QListWidgetItem *it : asConst(selectedItems()))
strings << static_cast<QLabel*>(itemWidget(it))->text().remove(htmlTag); strings << static_cast<QLabel*>(itemWidget(it))->text().remove(htmlTag);

2
src/gui/loglistwidget.h

@ -53,7 +53,7 @@ protected:
void keyPressEvent(QKeyEvent *event) override; void keyPressEvent(QKeyEvent *event) override;
private: private:
int m_maxLines; const int m_maxLines;
Log::MsgTypes m_types; Log::MsgTypes m_types;
}; };

Loading…
Cancel
Save