Enable customizing the save statistics time interval
[qBittorrent.git] / src / gui / log / logmodel.cpp
blobe1bcb9fef9e9944299ea625f8df215346730ce73
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2024 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2020 Prince Gupta <jagannatharjun11@gmail.com>
5 * Copyright (C) 2019 sledgehammer999 <hammered999@gmail.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * In addition, as a special exception, the copyright holders give permission to
22 * link this program with the OpenSSL project's "OpenSSL" library (or with
23 * modified versions of it that use the same license as the "OpenSSL" library),
24 * and distribute the linked executables. You must obey the GNU General Public
25 * License in all respects for all of the code used other than "OpenSSL". If you
26 * modify file(s), you may extend this exception to your version of the file(s),
27 * but you are not obligated to do so. If you do not wish to do so, delete this
28 * exception statement from your version.
31 #include "logmodel.h"
33 #include <QApplication>
34 #include <QDateTime>
35 #include <QColor>
37 #include "base/global.h"
38 #include "gui/uithememanager.h"
40 const int MAX_VISIBLE_MESSAGES = 20000;
42 BaseLogModel::Message::Message(const QString &time, const QString &message, const Log::MsgType type)
43 : m_time {time}
44 , m_message {message}
45 , m_type {type}
49 QString BaseLogModel::Message::time() const
51 return m_time;
54 QString BaseLogModel::Message::message() const
56 return m_message;
59 Log::MsgType BaseLogModel::Message::type() const
61 return m_type;
64 BaseLogModel::BaseLogModel(QObject *parent)
65 : QAbstractListModel(parent)
66 , m_messages(MAX_VISIBLE_MESSAGES)
68 loadColors();
69 connect(UIThemeManager::instance(), &UIThemeManager::themeChanged, this, &BaseLogModel::onUIThemeChanged);
72 int BaseLogModel::rowCount(const QModelIndex &) const
74 return static_cast<int>(m_messages.size());
77 int BaseLogModel::columnCount(const QModelIndex &) const
79 return 1;
82 QVariant BaseLogModel::data(const QModelIndex &index, const int role) const
84 if (!index.isValid())
85 return {};
87 const int messageIndex = index.row();
88 if ((messageIndex < 0) || (messageIndex >= static_cast<int>(m_messages.size())))
89 return {};
91 const Message &message = m_messages[messageIndex];
92 switch (role)
94 case TimeRole:
95 return message.time();
96 case MessageRole:
97 return message.message();
98 case TimeForegroundRole:
99 return m_timeForeground;
100 case MessageForegroundRole:
101 return messageForeground(message);
102 case TypeRole:
103 return message.type();
104 default:
105 return {};
109 void BaseLogModel::addNewMessage(const BaseLogModel::Message &message)
111 // if row is inserted on filled up buffer, the size will not change
112 // but because of calling of beginInsertRows function we'll have ghost rows.
113 if (m_messages.size() == MAX_VISIBLE_MESSAGES)
115 const int lastMessage = static_cast<int>(m_messages.size()) - 1;
116 beginRemoveRows(QModelIndex(), lastMessage, lastMessage);
117 m_messages.pop_back();
118 endRemoveRows();
121 beginInsertRows(QModelIndex(), 0, 0);
122 m_messages.push_front(message);
123 endInsertRows();
126 void BaseLogModel::onUIThemeChanged()
128 loadColors();
129 emit dataChanged(index(0, 0), index((rowCount() - 1), (columnCount() - 1)), {TimeForegroundRole, MessageForegroundRole});
132 void BaseLogModel::loadColors()
134 m_timeForeground = UIThemeManager::instance()->getColor(u"Log.TimeStamp"_s);
137 void BaseLogModel::reset()
139 beginResetModel();
140 m_messages.clear();
141 endResetModel();
144 LogMessageModel::LogMessageModel(QObject *parent)
145 : BaseLogModel(parent)
147 loadColors();
149 for (const Log::Msg &msg : asConst(Logger::instance()->getMessages()))
150 handleNewMessage(msg);
151 connect(Logger::instance(), &Logger::newLogMessage, this, &LogMessageModel::handleNewMessage);
154 void LogMessageModel::handleNewMessage(const Log::Msg &message)
156 const QString time = QLocale::system().toString(QDateTime::fromSecsSinceEpoch(message.timestamp), QLocale::ShortFormat);
157 addNewMessage({time, message.message, message.type});
160 QColor LogMessageModel::messageForeground(const Message &message) const
162 return m_foregroundForMessageTypes.value(message.type());
165 void LogMessageModel::onUIThemeChanged()
167 loadColors();
168 BaseLogModel::onUIThemeChanged();
171 void LogMessageModel::loadColors()
173 const auto *themeManager = UIThemeManager::instance();
174 const QColor normalColor = themeManager->getColor(u"Log.Normal"_s);
175 m_foregroundForMessageTypes =
177 {Log::NORMAL, normalColor.isValid() ? normalColor : QApplication::palette().color(QPalette::Active, QPalette::WindowText)},
178 {Log::INFO, themeManager->getColor(u"Log.Info"_s)},
179 {Log::WARNING, themeManager->getColor(u"Log.Warning"_s)},
180 {Log::CRITICAL, themeManager->getColor(u"Log.Critical"_s)}
184 LogPeerModel::LogPeerModel(QObject *parent)
185 : BaseLogModel(parent)
187 loadColors();
189 for (const Log::Peer &peer : asConst(Logger::instance()->getPeers()))
190 handleNewMessage(peer);
191 connect(Logger::instance(), &Logger::newLogPeer, this, &LogPeerModel::handleNewMessage);
194 void LogPeerModel::handleNewMessage(const Log::Peer &peer)
196 const QString time = QLocale::system().toString(QDateTime::fromSecsSinceEpoch(peer.timestamp), QLocale::ShortFormat);
197 const QString message = peer.blocked
198 ? tr("%1 was blocked. Reason: %2.", "0.0.0.0 was blocked. Reason: reason for blocking.").arg(peer.ip, peer.reason)
199 : tr("%1 was banned", "0.0.0.0 was banned").arg(peer.ip);
201 addNewMessage({time, message, Log::NORMAL});
204 QColor LogPeerModel::messageForeground([[maybe_unused]] const Message &message) const
206 return m_bannedPeerForeground;
209 void LogPeerModel::onUIThemeChanged()
211 loadColors();
212 BaseLogModel::onUIThemeChanged();
215 void LogPeerModel::loadColors()
217 m_bannedPeerForeground = UIThemeManager::instance()->getColor(u"Log.BannedPeer"_s);