#351: "WorkState" is now observable

This commit is contained in:
Andrey Prygunkov
2019-01-26 18:16:17 +01:00
parent a665dc5375
commit 89427f42ce
7 changed files with 34 additions and 17 deletions

View File

@@ -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();

View File

@@ -1,7 +1,7 @@
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2007-2019 Andrey Prygunkov <hugbug@users.sourceforge.net>
* Copyright (C) 2019 Andrey Prygunkov <hugbug@users.sourceforge.net>
*
* 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);
}

View File

@@ -1,7 +1,7 @@
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2007-2019 Andrey Prygunkov <hugbug@users.sourceforge.net>
* Copyright (C) 2019 Andrey Prygunkov <hugbug@users.sourceforge.net>
*
* 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;

View File

@@ -345,6 +345,9 @@ void NZBGet::CreateGlobals()
g_WinConsole = m_winConsole.get();
#endif
m_workState = std::make_unique<WorkState>();
g_WorkState = m_workState.get();
m_serviceCoordinator = std::make_unique<ServiceCoordinator>();
g_ServiceCoordinator = m_serviceCoordinator.get();
@@ -393,9 +396,6 @@ void NZBGet::CreateGlobals()
m_commandScriptLog = std::make_unique<CommandScriptLog>();
g_CommandScriptLog = m_commandScriptLog.get();
m_workState = std::make_unique<WorkState>();
g_WorkState = m_workState.get();
m_scheduler = std::make_unique<Scheduler>();
m_diskService = std::make_unique<DiskService>();

View File

@@ -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();

View File

@@ -240,6 +240,7 @@ void QueueCoordinator::Run()
if (standBy != wasStandBy)
{
g_StatMeter->EnterLeaveStandBy(standBy);
g_WorkState->SetDownloading(!standBy);
wasStandBy = standBy;
if (standBy)
{

View File

@@ -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()));