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. 17
      src/gui/loglistwidget.cpp
  5. 2
      src/gui/loglistwidget.h

15
src/app/filelogger.cpp

@ -66,10 +66,9 @@ FileLogger::~FileLogger() @@ -66,10 +66,9 @@ FileLogger::~FileLogger()
void FileLogger::changePath(const QString &newPath)
{
QString tmpPath = Utils::Fs::fromNativePath(newPath);
QDir dir(tmpPath);
dir.mkpath(tmpPath);
tmpPath = dir.absoluteFilePath("qbittorrent.log");
const QDir dir(newPath);
dir.mkpath(newPath);
const QString tmpPath = dir.absoluteFilePath("qbittorrent.log");
if (tmpPath != m_path) {
m_path = tmpPath;
@ -87,8 +86,10 @@ void FileLogger::deleteOld(const int age, const FileLogAgeType ageType) @@ -87,8 +86,10 @@ void FileLogger::deleteOld(const int age, const FileLogAgeType ageType)
{
const QDateTime date = QDateTime::currentDateTime();
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();
switch (ageType) {
case DAYS:
@ -106,7 +107,7 @@ void FileLogger::deleteOld(const int age, const FileLogAgeType ageType) @@ -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;
}
@ -168,7 +169,7 @@ void FileLogger::openLogFile() @@ -168,7 +169,7 @@ void FileLogger::openLogFile()
|| !m_logFile->setPermissions(QFile::ReadOwner | QFile::WriteOwner)) {
delete m_logFile;
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 @@ @@ -40,7 +40,7 @@
ExecutionLogWidget::ExecutionLogWidget(QWidget *parent, const Log::MsgTypes &types)
: QWidget(parent)
, 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_ui->setupUi(this);
@ -75,38 +75,35 @@ void ExecutionLogWidget::showMsgTypes(const Log::MsgTypes &types) @@ -75,38 +75,35 @@ void ExecutionLogWidget::showMsgTypes(const Log::MsgTypes &types)
void ExecutionLogWidget::addLogMessage(const Log::Msg &msg)
{
QString text;
QDateTime time = QDateTime::fromMSecsSinceEpoch(msg.timestamp);
QColor color;
QString colorName;
switch (msg.type) {
case Log::INFO:
color.setNamedColor("blue");
colorName = QLatin1String("blue");
break;
case Log::WARNING:
color.setNamedColor("orange");
colorName = QLatin1String("orange");
break;
case Log::CRITICAL:
color.setNamedColor("red");
colorName = QLatin1String("red");
break;
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);
}
void ExecutionLogWidget::addPeerMessage(const Log::Peer &peer)
{
QString text;
QDateTime time = QDateTime::fromMSecsSinceEpoch(peer.timestamp);
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 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);
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);
}

3
src/gui/executionlogwidget.ui

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

17
src/gui/loglistwidget.cpp

@ -39,7 +39,7 @@ @@ -39,7 +39,7 @@
#include "base/global.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)
, m_maxLines(maxLines)
, m_types(types)
@ -47,8 +47,8 @@ LogListWidget::LogListWidget(int maxLines, const Log::MsgTypes &types, QWidget * @@ -47,8 +47,8 @@ LogListWidget::LogListWidget(int maxLines, const Log::MsgTypes &types, QWidget *
// Allow multiple selections
setSelectionMode(QAbstractItemView::ExtendedSelection);
// Context menu
QAction *copyAct = new QAction(GuiIconProvider::instance()->getIcon("edit-copy"), tr("Copy"), this);
QAction *clearAct = new QAction(GuiIconProvider::instance()->getIcon("edit-clear"), tr("Clear"), this);
auto *copyAct = new QAction(GuiIconProvider::instance()->getIcon("edit-copy"), tr("Copy"), this);
auto *clearAct = new QAction(GuiIconProvider::instance()->getIcon("edit-clear"), tr("Clear"), this);
connect(copyAct, &QAction::triggered, this, &LogListWidget::copySelection);
connect(clearAct, &QAction::triggered, this, &LogListWidget::clear);
addAction(copyAct);
@ -78,10 +78,12 @@ void LogListWidget::keyPressEvent(QKeyEvent *event) @@ -78,10 +78,12 @@ void LogListWidget::keyPressEvent(QKeyEvent *event)
void LogListWidget::appendLine(const QString &line, const Log::MsgType &type)
{
auto *item = new QListWidgetItem;
// 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);
auto *item = new QListWidgetItem;
item->setSizeHint(lbl->sizeHint());
item->setData(Qt::UserRole, type);
insertItem(0, item);
@ -96,9 +98,10 @@ void LogListWidget::appendLine(const QString &line, const Log::MsgType &type) @@ -96,9 +98,10 @@ void LogListWidget::appendLine(const QString &line, const Log::MsgType &type)
void LogListWidget::copySelection()
{
static const QRegularExpression htmlTag("<[^>]+>");
const QRegularExpression htmlTag("<[^>]+>");
QStringList strings;
for (QListWidgetItem* it : asConst(selectedItems()))
for (QListWidgetItem *it : asConst(selectedItems()))
strings << static_cast<QLabel*>(itemWidget(it))->text().remove(htmlTag);
QApplication::clipboard()->setText(strings.join('\n'));

2
src/gui/loglistwidget.h

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

Loading…
Cancel
Save