|
|
|
@ -53,84 +53,72 @@ void RealProgressBarThread::refresh()
@@ -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(); |
|
|
|
|
|
|
|
|
|
forever |
|
|
|
|
{ |
|
|
|
|
//start checking the torrent information
|
|
|
|
|
start: |
|
|
|
|
CHECK_INTERRUPT |
|
|
|
|
if(ifInterrupted() == stop) |
|
|
|
|
{ |
|
|
|
|
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(); |
|
|
|
|
//no vector returned
|
|
|
|
|
//pieces not returned
|
|
|
|
|
if (pieces == 0) |
|
|
|
|
{ |
|
|
|
|
qDebug("pieces vector not returned"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
//empty the array
|
|
|
|
|
locker.relock(); |
|
|
|
|
for(int i=0; i<size; i++) |
|
|
|
|
mutex.lock(); |
|
|
|
|
for(int i=0; i<array.size(); i++) |
|
|
|
|
array[i] = 0.; |
|
|
|
|
locker.unlock(); |
|
|
|
|
qreal subfraction = size / (qreal) total_size; |
|
|
|
|
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++) |
|
|
|
|
{ |
|
|
|
|
CHECK_INTERRUPT |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
/*
|
|
|
|
|
//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++) |
|
|
|
|
if (success) |
|
|
|
|
{ |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
/*
|
|
|
|
|
qreal sum = 0.; |
|
|
|
|
locker.relock(); |
|
|
|
|
for(int i=0; i<size; i++) |
|
|
|
|
mutex.lock(); |
|
|
|
|
for(int i=0; i<array.size(); i++) |
|
|
|
|
sum += array[i]; |
|
|
|
|
qDebug()<<"progress:"<<sum*100./size; |
|
|
|
|
locker.unlock();*/ |
|
|
|
|
qDebug()<<"progress:"<<sum*100./array.size(); |
|
|
|
|
mutex.unlock();*/ |
|
|
|
|
qDebug("refreshed emmitted"); |
|
|
|
|
emit refreshed(array); |
|
|
|
|
goto wait; |
|
|
|
|
//wait for refresh or rezise slot
|
|
|
|
|
mutex.lock(); |
|
|
|
|
condition.wait(&mutex); |
|
|
|
|
mutex.unlock(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//this is called by CHECK_INTERRUPT
|
|
|
|
|
Interrupt RealProgressBarThread::ifInterrupted() |
|
|
|
|
{ |
|
|
|
|
QMutexLocker locker(&mutex); |
|
|
|
@ -150,8 +138,8 @@ Interrupt RealProgressBarThread::ifInterrupted()
@@ -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); |
|
|
|
|