diff --git a/daemon/main/DiskService.cpp b/daemon/main/DiskService.cpp index e00d5364..1a5358e4 100644 --- a/daemon/main/DiskService.cpp +++ b/daemon/main/DiskService.cpp @@ -41,6 +41,7 @@ void DiskService::ServiceWork() if (!g_WorkState->GetPauseDownload() && g_Options->GetDiskSpace() > 0 && !g_StatMeter->GetStandBy()) + if (g_Options->GetDiskSpace() > 0 && g_WorkState->GetDownloading()) { // check free disk space every 1 second CheckDiskSpace(); diff --git a/daemon/main/WorkState.cpp b/daemon/main/WorkState.cpp index 69a197b7..2578b978 100644 --- a/daemon/main/WorkState.cpp +++ b/daemon/main/WorkState.cpp @@ -1,7 +1,7 @@ /* * This file is part of nzbget. See . * - * Copyright (C) 2007-2019 Andrey Prygunkov + * Copyright (C) 2019 Andrey Prygunkov * * 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 @@ -21,3 +21,7 @@ #include "nzbget.h" #include "WorkState.h" +void WorkState::Changed() +{ + Notify(nullptr); +} diff --git a/daemon/main/WorkState.h b/daemon/main/WorkState.h index 52bdd084..145980d5 100644 --- a/daemon/main/WorkState.h +++ b/daemon/main/WorkState.h @@ -1,7 +1,7 @@ /* * This file is part of nzbget. See . * - * Copyright (C) 2007-2019 Andrey Prygunkov + * Copyright (C) 2019 Andrey Prygunkov * * 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 @@ -21,27 +21,36 @@ #ifndef WORKSTATE_H #define WORKSTATE_H -class WorkState +#include "Observer.h" + +// WorkState is observable but notifications are not 100% reliable. +// The changes via Set-methods and readings via Get-methods are not synchronized throughout the program. +// As result race conditions may occur and some changes may go unnoticed. +// When waiting for changes don't wait too long to avoid lock ups. + +class WorkState : public Subject { public: - void SetPauseDownload(bool pauseDownload) { m_pauseDownload = pauseDownload; } + void SetPauseDownload(bool pauseDownload) { m_pauseDownload = pauseDownload; Changed(); } bool GetPauseDownload() const { return m_pauseDownload; } - void SetPausePostProcess(bool pausePostProcess) { m_pausePostProcess = pausePostProcess; } + void SetPausePostProcess(bool pausePostProcess) { m_pausePostProcess = pausePostProcess; Changed(); } bool GetPausePostProcess() const { return m_pausePostProcess; } - void SetPauseScan(bool pauseScan) { m_pauseScan = pauseScan; } + void SetPauseScan(bool pauseScan) { m_pauseScan = pauseScan; Changed(); } bool GetPauseScan() const { return m_pauseScan; } - void SetTempPauseDownload(bool tempPauseDownload) { m_tempPauseDownload = tempPauseDownload; } + void SetTempPauseDownload(bool tempPauseDownload) { m_tempPauseDownload = tempPauseDownload; Changed(); } bool GetTempPauseDownload() const { return m_tempPauseDownload; } + void SetTempPausePostprocess(bool tempPausePostprocess) { m_tempPausePostprocess = tempPausePostprocess; Changed(); } bool GetTempPausePostprocess() const { return m_tempPausePostprocess; } - void SetTempPausePostprocess(bool tempPausePostprocess) { m_tempPausePostprocess = tempPausePostprocess; } - void SetSpeedLimit(int speedLimit) { m_speedLimit = speedLimit; } + void SetSpeedLimit(int speedLimit) { m_speedLimit = speedLimit; Changed(); } int GetSpeedLimit() const { return m_speedLimit; } - void SetResumeTime(time_t resumeTime) { m_resumeTime = resumeTime; } + void SetResumeTime(time_t resumeTime) { m_resumeTime = resumeTime; Changed(); } time_t GetResumeTime() const { return m_resumeTime; } - void SetLocalTimeOffset(int localTimeOffset) { m_localTimeOffset = localTimeOffset; } + void SetLocalTimeOffset(int localTimeOffset) { m_localTimeOffset = localTimeOffset; Changed(); } int GetLocalTimeOffset() { return m_localTimeOffset; } - void SetQuotaReached(bool quotaReached) { m_quotaReached = quotaReached; } + void SetQuotaReached(bool quotaReached) { m_quotaReached = quotaReached; Changed(); } bool GetQuotaReached() { return m_quotaReached; } + void SetDownloading(bool downloading) { m_downloading = downloading; Changed(); } + bool GetDownloading() { return m_downloading; } private: bool m_pauseDownload = false; @@ -54,6 +63,9 @@ private: int m_localTimeOffset = 0; bool m_quotaReached = false; int m_speedLimit = 0; + bool m_downloading = false; + + void Changed(); }; extern WorkState* g_WorkState; diff --git a/daemon/main/nzbget.cpp b/daemon/main/nzbget.cpp index 44887981..ad4bfa73 100644 --- a/daemon/main/nzbget.cpp +++ b/daemon/main/nzbget.cpp @@ -345,6 +345,9 @@ void NZBGet::CreateGlobals() g_WinConsole = m_winConsole.get(); #endif + m_workState = std::make_unique(); + g_WorkState = m_workState.get(); + m_serviceCoordinator = std::make_unique(); g_ServiceCoordinator = m_serviceCoordinator.get(); @@ -393,9 +396,6 @@ void NZBGet::CreateGlobals() m_commandScriptLog = std::make_unique(); g_CommandScriptLog = m_commandScriptLog.get(); - m_workState = std::make_unique(); - g_WorkState = m_workState.get(); - m_scheduler = std::make_unique(); m_diskService = std::make_unique(); diff --git a/daemon/nntp/StatMeter.h b/daemon/nntp/StatMeter.h index 8d03c5c0..7180d928 100644 --- a/daemon/nntp/StatMeter.h +++ b/daemon/nntp/StatMeter.h @@ -84,7 +84,6 @@ public: void AddServerData(int bytes, int serverId); void CalcTotalStat(int* upTimeSec, int* dnTimeSec, int64* allBytes, bool* standBy); void CalcQuotaUsage(int64& monthBytes, int64& dayBytes); - bool GetStandBy() { return m_standBy; } void IntervalCheck(); void EnterLeaveStandBy(bool enter); GuardedServerVolumes GuardServerVolumes(); diff --git a/daemon/queue/QueueCoordinator.cpp b/daemon/queue/QueueCoordinator.cpp index 585c7764..aa8bc0ed 100644 --- a/daemon/queue/QueueCoordinator.cpp +++ b/daemon/queue/QueueCoordinator.cpp @@ -240,6 +240,7 @@ void QueueCoordinator::Run() if (standBy != wasStandBy) { g_StatMeter->EnterLeaveStandBy(standBy); + g_WorkState->SetDownloading(!standBy); wasStandBy = standBy; if (standBy) { diff --git a/daemon/windows/WinConsole.cpp b/daemon/windows/WinConsole.cpp index d3761971..57fc1e8a 100644 --- a/daemon/windows/WinConsole.cpp +++ b/daemon/windows/WinConsole.cpp @@ -764,7 +764,7 @@ void WinConsole::UpdateTrayIcon() m_iconData->hIcon = m_pausedIcon; strncpy(m_iconData->szTip, "NZBGet - paused", sizeof(m_iconData->szTip)); } - else if (!g_StatMeter->GetStandBy()) + else if (g_WorkState->GetDownloading()) { m_iconData->hIcon = m_workingIcon; BString<100> tip("NZBGet - downloading at %s", *Util::FormatSpeed(g_StatMeter->CalcCurrentDownloadSpeed()));