qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
toxfileprogress.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2021 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "toxfileprogress.h"
21 
22 #include <limits>
23 
24 ToxFileProgress::ToxFileProgress(uint64_t filesize, int samplePeriodMs)
25  : filesize(filesize)
26  , samplePeriodMs(samplePeriodMs)
27 {
28  if (samplePeriodMs < 0) {
29  qWarning("Invalid sample rate, healing to 1000ms");
30  this->samplePeriodMs = 1000;
31  }
32 }
33 
35 {
36  return samples[activeSample].timestamp;
37 }
38 
39 bool ToxFileProgress::addSample(uint64_t bytesSent, QTime now)
40 {
41  if (bytesSent > filesize) {
42  qWarning("Bytes sent exceeds file size, ignoring sample");
43  return false;
44  }
45 
46  auto* active = &samples[activeSample];
47  auto* inactive = &samples[!activeSample];
48 
49  if (bytesSent < active->bytesSent || bytesSent < inactive->bytesSent) {
50  qWarning("Bytes sent has decreased since last sample, ignoring sample");
51  return false;
52  }
53 
54  if (now < active->timestamp || now < inactive->timestamp) {
55  qWarning("Sample time has gone backwards, clearing progress buffer");
56  resetSpeed();
57  }
58 
59  // Ensure both samples are initialized
60  if (inactive->timestamp == QTime()) {
61  inactive->bytesSent = bytesSent;
62  inactive->timestamp = now;
63  }
64 
65  if (active->timestamp == QTime()) {
66  active->bytesSent = bytesSent;
67  active->timestamp = now;
68  }
69 
70  if (active->timestamp.msecsTo(now) >= samplePeriodMs) {
71  // Swap samples and set the newly active sample
73  std::swap(active, inactive);
74  }
75 
76  active->bytesSent = bytesSent;
77  active->timestamp = now;
78 
79  return true;
80 }
81 
83 {
84  for (auto& sample : samples) {
85  sample.timestamp = QTime();
86  }
87 }
88 
90 {
91  return samples[activeSample].bytesSent;
92 }
93 
95 {
96  return double(samples[activeSample].bytesSent) / filesize;
97 }
98 
100 {
101  if (samples.size() > 0
102  && samples[activeSample].bytesSent == filesize) {
103  return 0.0f;
104  }
105 
106  const auto sampleTimeInvalid = [](const Sample& sample) {
107  return sample.timestamp == QTime();
108  };
109 
110  if (std::any_of(samples.cbegin(), samples.cend(), sampleTimeInvalid)) {
111  return 0.0f;
112  }
113 
114  if (samples[0].timestamp == samples[1].timestamp) {
115  return 0.0f;
116  }
117 
118  const auto& active = samples[activeSample];
119  const auto& inactive = samples[!activeSample];
120 
121  return (active.bytesSent - inactive.bytesSent) / double(inactive.timestamp.msecsTo(active.timestamp)) * 1000.0;
122 }
123 
125 {
126  if (samples.size() > 0
127  && samples[activeSample].bytesSent == filesize) {
128  return 0;
129  }
130 
131  const auto speed = getSpeed();
132  if (speed == 0.0f) {
133  return std::numeric_limits<float>::infinity();
134  }
135 
136  return double(filesize - samples[activeSample].bytesSent) / getSpeed();
137 }
ToxFileProgress::getSpeed
double getSpeed() const
Definition: toxfileprogress.cpp:99
ToxFileProgress::Sample
Definition: toxfileprogress.h:47
toxfileprogress.h
ToxFileProgress::ToxFileProgress
ToxFileProgress(uint64_t filesize, int samplePeriodMs=4000)
Definition: toxfileprogress.cpp:24
ToxFileProgress::getProgress
double getProgress() const
Definition: toxfileprogress.cpp:94
ToxFileProgress::activeSample
uint8_t activeSample
Definition: toxfileprogress.h:54
ToxFileProgress::filesize
uint64_t filesize
Definition: toxfileprogress.h:43
ToxFileProgress::resetSpeed
void resetSpeed()
Definition: toxfileprogress.cpp:82
ToxFileProgress::samples
std::array< Sample, 2 > samples
Definition: toxfileprogress.h:53
ToxFileProgress::getTimeLeftSeconds
double getTimeLeftSeconds() const
Definition: toxfileprogress.cpp:124
ToxFileProgress::getBytesSent
uint64_t getBytesSent() const
Definition: toxfileprogress.cpp:89
ToxFileProgress::addSample
bool addSample(uint64_t bytesSent, QTime now=QTime::currentTime())
Definition: toxfileprogress.cpp:39
ToxFileProgress::lastSampleTime
QTime lastSampleTime() const
Definition: toxfileprogress.cpp:34
ToxFileProgress::samplePeriodMs
int samplePeriodMs
Definition: toxfileprogress.h:45