mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-07-16 02:03:07 -07:00
Corrections in RealProgressBarThread
- Fixed a bug related to the size of the array - Removed the much hated goto statements with loops :)
This commit is contained in:
parent
91328de30f
commit
8a9a23866f
1 changed files with 61 additions and 73 deletions
|
@ -53,84 +53,72 @@ void RealProgressBarThread::refresh()
|
||||||
condition.wakeOne();
|
condition.wakeOne();
|
||||||
}
|
}
|
||||||
|
|
||||||
//this is called inside run()
|
|
||||||
#define CHECK_INTERRUPT \
|
|
||||||
switch(ifInterrupted())\
|
|
||||||
{\
|
|
||||||
case stop:\
|
|
||||||
qDebug("RealProgressBarThread stop");\
|
|
||||||
return;\
|
|
||||||
case restart:\
|
|
||||||
qDebug("RealProgressBarThread restart");\
|
|
||||||
goto start;\
|
|
||||||
case ignore:\
|
|
||||||
break;\
|
|
||||||
}
|
|
||||||
|
|
||||||
void RealProgressBarThread::run(){
|
void RealProgressBarThread::run(){
|
||||||
//wait for refresh or rezise slot
|
forever
|
||||||
wait:
|
|
||||||
QMutexLocker locker(&mutex);
|
|
||||||
condition.wait(&mutex);
|
|
||||||
locker.unlock();
|
|
||||||
|
|
||||||
//start checking the torrent information
|
|
||||||
start:
|
|
||||||
CHECK_INTERRUPT
|
|
||||||
size_type total_size = thandle.total_size();
|
|
||||||
size_type piece_length = thandle.piece_length();
|
|
||||||
int num_pieces = thandle.num_pieces();
|
|
||||||
const std::vector<bool>* pieces = thandle.pieces();
|
|
||||||
//no vector returned
|
|
||||||
if (pieces == 0)
|
|
||||||
return;
|
|
||||||
//empty the array
|
|
||||||
locker.relock();
|
|
||||||
for(int i=0; i<size; i++)
|
|
||||||
array[i] = 0.;
|
|
||||||
locker.unlock();
|
|
||||||
qreal subfraction = size / (qreal) total_size;
|
|
||||||
qreal fraction = subfraction * piece_length;
|
|
||||||
//fill the array with complete pieces
|
|
||||||
for(int i=0; i<num_pieces; i++)
|
|
||||||
{
|
{
|
||||||
CHECK_INTERRUPT
|
//start checking the torrent information
|
||||||
qreal start = i * fraction;
|
if(ifInterrupted() == stop)
|
||||||
qreal end = start + fraction;
|
|
||||||
if((*pieces)[i])
|
|
||||||
mark(start, end);
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
//fill the array with incomplete pieces (from download queue)
|
|
||||||
std::vector<partial_piece_info> queue;
|
|
||||||
thandle.get_download_queue(queue);
|
|
||||||
for(unsigned int i=0; i<queue.size(); i++)
|
|
||||||
{
|
|
||||||
partial_piece_info ppi = queue[i];
|
|
||||||
qreal start = ppi.piece_index * fraction;
|
|
||||||
qreal end = start;
|
|
||||||
for(int j=0; j<ppi.blocks_in_piece; j++)
|
|
||||||
{
|
{
|
||||||
CHECK_INTERRUPT
|
qDebug("RealProgressBarThread stop");
|
||||||
block_info bi = ppi.blocks[j];
|
return;
|
||||||
end += bi.block_size * subfraction;
|
}
|
||||||
qreal progress = bi.bytes_progress / (qreal) bi.block_size;
|
size_type total_size = thandle.total_size();
|
||||||
mark(start, end, progress);
|
size_type piece_length = thandle.piece_length();
|
||||||
start = end;
|
int num_pieces = thandle.num_pieces();
|
||||||
|
const std::vector<bool>* pieces = thandle.pieces();
|
||||||
|
//pieces not returned
|
||||||
|
if (pieces == 0)
|
||||||
|
{
|
||||||
|
qDebug("pieces vector not returned");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//empty the array
|
||||||
|
mutex.lock();
|
||||||
|
for(int i=0; i<array.size(); i++)
|
||||||
|
array[i] = 0.;
|
||||||
|
mutex.unlock();
|
||||||
|
qreal subfraction = array.size() / (qreal) total_size;
|
||||||
|
qreal fraction = subfraction * piece_length;
|
||||||
|
bool success = true;
|
||||||
|
//fill the array with complete pieces
|
||||||
|
for(int i=0; i<num_pieces; i++)
|
||||||
|
{
|
||||||
|
Interrupt temp = ifInterrupted();
|
||||||
|
if (temp == stop)
|
||||||
|
{
|
||||||
|
qDebug("RealProgressBarThread stop");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (temp == restart)
|
||||||
|
{
|
||||||
|
qDebug("RealProgressBarThread restart");
|
||||||
|
success = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
qreal start = i * fraction;
|
||||||
|
qreal end = start + fraction;
|
||||||
|
if((*pieces)[i])
|
||||||
|
mark(start, end);
|
||||||
|
}
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
qreal sum = 0.;
|
||||||
|
mutex.lock();
|
||||||
|
for(int i=0; i<array.size(); i++)
|
||||||
|
sum += array[i];
|
||||||
|
qDebug()<<"progress:"<<sum*100./array.size();
|
||||||
|
mutex.unlock();*/
|
||||||
|
qDebug("refreshed emmitted");
|
||||||
|
emit refreshed(array);
|
||||||
|
//wait for refresh or rezise slot
|
||||||
|
mutex.lock();
|
||||||
|
condition.wait(&mutex);
|
||||||
|
mutex.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
qreal sum = 0.;
|
|
||||||
locker.relock();
|
|
||||||
for(int i=0; i<size; i++)
|
|
||||||
sum += array[i];
|
|
||||||
qDebug()<<"progress:"<<sum*100./size;
|
|
||||||
locker.unlock();*/
|
|
||||||
qDebug("refreshed emmitted");
|
|
||||||
emit refreshed(array);
|
|
||||||
goto wait;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//this is called by CHECK_INTERRUPT
|
|
||||||
Interrupt RealProgressBarThread::ifInterrupted()
|
Interrupt RealProgressBarThread::ifInterrupted()
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&mutex);
|
QMutexLocker locker(&mutex);
|
||||||
|
@ -150,8 +138,8 @@ Interrupt RealProgressBarThread::ifInterrupted()
|
||||||
|
|
||||||
void RealProgressBarThread::mark(qreal start, qreal end, qreal progress){
|
void RealProgressBarThread::mark(qreal start, qreal end, qreal progress){
|
||||||
QMutexLocker locker(&mutex);
|
QMutexLocker locker(&mutex);
|
||||||
if (end > size)
|
if (end > array.size())
|
||||||
end = size;
|
end = array.size();
|
||||||
int start_int, end_int;
|
int start_int, end_int;
|
||||||
qreal temp, start_frac, end_frac;
|
qreal temp, start_frac, end_frac;
|
||||||
start_frac = modf(start, &temp);
|
start_frac = modf(start, &temp);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue