From 2aea235e343f3c1b3d00273872d5944b25ed6f1e Mon Sep 17 00:00:00 2001 From: Thomas Piccirello Date: Sat, 22 Sep 2018 20:21:04 -0400 Subject: [PATCH] Add free disk space to WebUI status bar Closes #6829. --- src/webui/CMakeLists.txt | 2 ++ src/webui/api/freediskspacechecker.cpp | 38 ++++++++++++++++++++ src/webui/api/freediskspacechecker.h | 46 +++++++++++++++++++++++++ src/webui/api/synccontroller.cpp | 42 ++++++++++++++++++++++ src/webui/api/synccontroller.h | 18 ++++++++++ src/webui/webui.pri | 2 ++ src/webui/www/private/index.html | 2 ++ src/webui/www/private/scripts/client.js | 1 + 8 files changed, 151 insertions(+) create mode 100644 src/webui/api/freediskspacechecker.cpp create mode 100644 src/webui/api/freediskspacechecker.h diff --git a/src/webui/CMakeLists.txt b/src/webui/CMakeLists.txt index 5c4ae7f71..2bdc1678b 100644 --- a/src/webui/CMakeLists.txt +++ b/src/webui/CMakeLists.txt @@ -5,6 +5,7 @@ api/apierror.h api/appcontroller.h api/isessionmanager.h api/authcontroller.h +api/freediskspacechecker.h api/logcontroller.h api/rsscontroller.h api/synccontroller.h @@ -19,6 +20,7 @@ api/apicontroller.cpp api/apierror.cpp api/appcontroller.cpp api/authcontroller.cpp +api/freediskspacechecker.cpp api/logcontroller.cpp api/rsscontroller.cpp api/synccontroller.cpp diff --git a/src/webui/api/freediskspacechecker.cpp b/src/webui/api/freediskspacechecker.cpp new file mode 100644 index 000000000..9be812c53 --- /dev/null +++ b/src/webui/api/freediskspacechecker.cpp @@ -0,0 +1,38 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2018 Thomas Piccirello + * + * 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. + * + * In addition, as a special exception, the copyright holders give permission to + * link this program with the OpenSSL project's "OpenSSL" library (or with + * modified versions of it that use the same license as the "OpenSSL" library), + * and distribute the linked executables. You must obey the GNU General Public + * License in all respects for all of the code used other than "OpenSSL". If you + * modify file(s), you may extend this exception to your version of the file(s), + * but you are not obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + */ + +#include "freediskspacechecker.h" + +#include "base/bittorrent/session.h" +#include "base/utils/fs.h" + +void FreeDiskSpaceChecker::check() +{ + const qint64 freeDiskSpace = Utils::Fs::freeDiskSpaceOnPath(BitTorrent::Session::instance()->defaultSavePath()); + emit checked(freeDiskSpace); +} diff --git a/src/webui/api/freediskspacechecker.h b/src/webui/api/freediskspacechecker.h new file mode 100644 index 000000000..9e5aecfb7 --- /dev/null +++ b/src/webui/api/freediskspacechecker.h @@ -0,0 +1,46 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2018 Thomas Piccirello + * + * 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. + * + * In addition, as a special exception, the copyright holders give permission to + * link this program with the OpenSSL project's "OpenSSL" library (or with + * modified versions of it that use the same license as the "OpenSSL" library), + * and distribute the linked executables. You must obey the GNU General Public + * License in all respects for all of the code used other than "OpenSSL". If you + * modify file(s), you may extend this exception to your version of the file(s), + * but you are not obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + */ + +#pragma once + +#include + +class FreeDiskSpaceChecker : public QObject +{ + Q_OBJECT + Q_DISABLE_COPY(FreeDiskSpaceChecker) + +public: + FreeDiskSpaceChecker() = default; + +public slots: + void check(); + +signals: + void checked(qint64 freeSpaceSize); +}; diff --git a/src/webui/api/synccontroller.cpp b/src/webui/api/synccontroller.cpp index 597a207e3..25ed90302 100644 --- a/src/webui/api/synccontroller.cpp +++ b/src/webui/api/synccontroller.cpp @@ -29,6 +29,8 @@ #include "synccontroller.h" #include +#include +#include #include "base/bittorrent/peerinfo.h" #include "base/bittorrent/session.h" @@ -38,6 +40,7 @@ #include "base/utils/fs.h" #include "base/utils/string.h" #include "apierror.h" +#include "freediskspacechecker.h" #include "isessionmanager.h" #include "serialize/serialize_torrent.h" @@ -75,6 +78,7 @@ const char KEY_TRANSFER_UPDATA[] = "up_info_data"; const char KEY_TRANSFER_UPRATELIMIT[] = "up_rate_limit"; const char KEY_TRANSFER_DHT_NODES[] = "dht_nodes"; const char KEY_TRANSFER_CONNECTION_STATUS[] = "connection_status"; +const char KEY_TRANSFER_FREESPACEONDISK[] = "free_space_on_disk"; // Statistics keys const char KEY_TRANSFER_ALLTIME_DL[] = "alltime_dl"; @@ -94,6 +98,8 @@ const char KEY_FULL_UPDATE[] = "full_update"; const char KEY_RESPONSE_ID[] = "rid"; const char KEY_SUFFIX_REMOVED[] = "_removed"; +const int FREEDISKSPACE_CHECK_TIMEOUT = 30000; + namespace { void processMap(const QVariantMap &prevData, const QVariantMap &data, QVariantMap &syncData); @@ -312,6 +318,25 @@ namespace } } +SyncController::SyncController(ISessionManager *sessionManager, QObject *parent) + : APIController(sessionManager, parent) +{ + m_freeDiskSpaceThread = new QThread(this); + m_freeDiskSpaceChecker = new FreeDiskSpaceChecker(); + m_freeDiskSpaceChecker->moveToThread(m_freeDiskSpaceThread); + + connect(m_freeDiskSpaceThread, &QThread::finished, m_freeDiskSpaceChecker, &QObject::deleteLater); + connect(m_freeDiskSpaceChecker, &FreeDiskSpaceChecker::checked, this, &SyncController::freeDiskSpaceSizeUpdated); + + m_freeDiskSpaceThread->start(); +} + +SyncController::~SyncController() +{ + m_freeDiskSpaceThread->quit(); + m_freeDiskSpaceThread->wait(); +} + // The function returns the changed data from the server to synchronize with the web client. // Return value is map in JSON format. // Map contain the key: @@ -369,6 +394,7 @@ namespace // - "up_rate_limit: upload speed limit // - "queueing": priority system usage flag // - "refresh_interval": torrents table refresh interval +// - "free_space_on_disk": Free space on the default save path // GET param: // - rid (int): last response id void SyncController::maindataAction() @@ -412,6 +438,7 @@ void SyncController::maindataAction() data["categories"] = categories; QVariantMap serverState = getTranserInfo(); + serverState[KEY_TRANSFER_FREESPACEONDISK] = getFreeDiskSpace(); serverState[KEY_SYNC_MAINDATA_QUEUEING] = session->isQueueingSystemEnabled(); serverState[KEY_SYNC_MAINDATA_USE_ALT_SPEED_LIMITS] = session->isAltGlobalSpeedLimitEnabled(); serverState[KEY_SYNC_MAINDATA_REFRESH_INTERVAL] = session->refreshInterval(); @@ -482,3 +509,18 @@ void SyncController::torrentPeersAction() sessionManager()->session()->setData(QLatin1String("syncTorrentPeersLastResponse"), lastResponse); sessionManager()->session()->setData(QLatin1String("syncTorrentPeersLastAcceptedResponse"), lastAcceptedResponse); } + +qint64 SyncController::getFreeDiskSpace() +{ + const qint64 now = QDateTime::currentMSecsSinceEpoch(); + if ((now - m_freeDiskSpaceLastUpdate) >= FREEDISKSPACE_CHECK_TIMEOUT) { + QTimer::singleShot(0, m_freeDiskSpaceChecker, &FreeDiskSpaceChecker::check); + m_freeDiskSpaceLastUpdate = now; + } + return m_freeDiskSpace; +} + +void SyncController::freeDiskSpaceSizeUpdated(qint64 freeSpaceSize) +{ + m_freeDiskSpace = freeSpaceSize; +} diff --git a/src/webui/api/synccontroller.h b/src/webui/api/synccontroller.h index 0e5e79e45..e839acc64 100644 --- a/src/webui/api/synccontroller.h +++ b/src/webui/api/synccontroller.h @@ -30,6 +30,12 @@ #include "apicontroller.h" +struct ISessionManager; + +class QThread; + +class FreeDiskSpaceChecker; + class SyncController : public APIController { Q_OBJECT @@ -38,7 +44,19 @@ class SyncController : public APIController public: using APIController::APIController; + explicit SyncController(ISessionManager *sessionManager, QObject *parent = nullptr); + ~SyncController() override; + private slots: void maindataAction(); void torrentPeersAction(); + void freeDiskSpaceSizeUpdated(qint64 freeSpaceSize); + +private: + qint64 getFreeDiskSpace(); + + qint64 m_freeDiskSpace = 0; + qint64 m_freeDiskSpaceLastUpdate = 0; + FreeDiskSpaceChecker *m_freeDiskSpaceChecker = nullptr; + QThread *m_freeDiskSpaceThread = nullptr; }; diff --git a/src/webui/webui.pri b/src/webui/webui.pri index 73a146005..70f47ba87 100644 --- a/src/webui/webui.pri +++ b/src/webui/webui.pri @@ -3,6 +3,7 @@ HEADERS += \ $$PWD/api/apierror.h \ $$PWD/api/appcontroller.h \ $$PWD/api/authcontroller.h \ + $$PWD/api/freediskspacechecker.h \ $$PWD/api/isessionmanager.h \ $$PWD/api/logcontroller.h \ $$PWD/api/rsscontroller.h \ @@ -18,6 +19,7 @@ SOURCES += \ $$PWD/api/apierror.cpp \ $$PWD/api/appcontroller.cpp \ $$PWD/api/authcontroller.cpp \ + $$PWD/api/freediskspacechecker.cpp \ $$PWD/api/logcontroller.cpp \ $$PWD/api/rsscontroller.cpp \ $$PWD/api/synccontroller.cpp \ diff --git a/src/webui/www/private/index.html b/src/webui/www/private/index.html index f3f8148af..b4c2645a0 100644 --- a/src/webui/www/private/index.html +++ b/src/webui/www/private/index.html @@ -155,6 +155,8 @@ + + diff --git a/src/webui/www/private/scripts/client.js b/src/webui/www/private/scripts/client.js index 15ac6ac13..f8fdd9044 100644 --- a/src/webui/www/private/scripts/client.js +++ b/src/webui/www/private/scripts/client.js @@ -421,6 +421,7 @@ window.addEvent('load', function() { } else document.title = "qBittorrent ${VERSION} QBT_TR(Web UI)QBT_TR[CONTEXT=OptionsDialog]"; + $('freeSpaceOnDisk').set('html', 'QBT_TR(Free space: %1)QBT_TR[CONTEXT=HttpServer]'.replace("%1", friendlyUnit(serverState.free_space_on_disk))); $('DHTNodes').set('html', 'QBT_TR(DHT: %1 nodes)QBT_TR[CONTEXT=StatusBar]'.replace("%1", serverState.dht_nodes)); // Statistics dialog
Connection Status