mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-03-10 04:11:16 +00: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
@ -53,84 +53,72 @@ void RealProgressBarThread::refresh()
|
||||
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(){
|
||||
//wait for refresh or rezise slot
|
||||
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++)
|
||||
forever
|
||||
{
|
||||
CHECK_INTERRUPT
|
||||
qreal start = i * fraction;
|
||||
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++)
|
||||
//start checking the torrent information
|
||||
if(ifInterrupted() == stop)
|
||||
{
|
||||
CHECK_INTERRUPT
|
||||
block_info bi = ppi.blocks[j];
|
||||
end += bi.block_size * subfraction;
|
||||
qreal progress = bi.bytes_progress / (qreal) bi.block_size;
|
||||
mark(start, end, progress);
|
||||
start = end;
|
||||
qDebug("RealProgressBarThread stop");
|
||||
return;
|
||||
}
|
||||
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();
|
||||
//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()
|
||||
{
|
||||
QMutexLocker locker(&mutex);
|
||||
@ -150,8 +138,8 @@ Interrupt RealProgressBarThread::ifInterrupted()
|
||||
|
||||
void RealProgressBarThread::mark(qreal start, qreal end, qreal progress){
|
||||
QMutexLocker locker(&mutex);
|
||||
if (end > size)
|
||||
end = size;
|
||||
if (end > array.size())
|
||||
end = array.size();
|
||||
int start_int, end_int;
|
||||
qreal temp, start_frac, end_frac;
|
||||
start_frac = modf(start, &temp);
|
||||
|
Loading…
x
Reference in New Issue
Block a user