Revision 1788 trunk/client/gui_qt/DownloadQueueModel.cpp
| DownloadQueueModel.cpp (revision 1788) | ||
|---|---|---|
| 29 | 29 |
|
| 30 | 30 |
int DownloadQueueModel::columnCount(const QModelIndex &parent) const |
| 31 | 31 |
{
|
| 32 |
if (parent.isValid()) |
|
| 33 |
return static_cast |
|
| 34 |
else |
|
| 35 |
return rootItem->columnCount(); |
|
| 32 |
return parent.isValid() && parent.column() != 0 ? 0 : DownloadQueueItem::NUM_FIELDS; |
|
| 36 | 33 |
} |
| 37 | 34 |
|
| 38 | 35 |
QVariant DownloadQueueModel::data(const QModelIndex &index, int role) const |
| ... | ... | |
| 123 | 120 |
if (!index.isValid()) |
| 124 | 121 |
return QModelIndex(); |
| 125 | 122 |
|
| 126 |
DownloadQueueItem *childItem = static_cast |
|
| 127 |
DownloadQueueItem *parentItem = childItem->parent(); |
|
| 123 |
if (DownloadQueueItem *childItem = itemForIndex(index)) {
|
|
| 124 |
if (DownloadQueueItem *parentItem = childItem->parent()) {
|
|
| 125 |
if (parentItem == rootItem) |
|
| 126 |
return QModelIndex(); |
|
| 127 |
if (DownloadQueueItem *grandParentItem = parentItem->parent()) {
|
|
| 128 |
int row = grandParentItem->rowOfChild(parentItem); |
|
| 129 |
return createIndex(row, 0, parentItem); |
|
| 130 |
} |
|
| 131 |
} |
|
| 132 |
} |
|
| 128 | 133 |
|
| 129 |
if (parentItem == rootItem || !parentItem) |
|
| 130 |
return QModelIndex(); |
|
| 134 |
return QModelIndex(); |
|
| 135 |
} |
|
| 131 | 136 |
|
| 132 |
return createIndex(parentItem->row(), 0, parentItem); |
|
| 137 |
DownloadQueueItem *DownloadQueueModel::itemForIndex(const QModelIndex &index) const |
|
| 138 |
{
|
|
| 139 |
if (index.isValid()) {
|
|
| 140 |
if (DownloadQueueItem *item = static_cast |
|
| 141 |
return item; |
|
| 142 |
} |
|
| 143 |
} |
|
| 144 |
|
|
| 145 |
return rootItem; |
|
| 133 | 146 |
} |
| 134 | 147 |
|
| 135 | 148 |
int DownloadQueueModel::rowCount(const QModelIndex &parent) const |
| 136 | 149 |
{
|
| 137 |
DownloadQueueItem *parentItem; |
|
| 138 |
if (parent.column() > 0) |
|
| 150 |
if (parent.isValid() && parent.column() != 0) |
|
| 139 | 151 |
return 0; |
| 140 | 152 |
|
| 141 |
if (!parent.isValid()) |
|
| 142 |
parentItem = rootItem; |
|
| 143 |
else |
|
| 144 |
parentItem = static_cast |
|
| 145 |
|
|
| 146 |
return parentItem->childCount(); |
|
| 153 |
DownloadQueueItem *parentItem = itemForIndex(parent); |
|
| 154 |
return parentItem ? parentItem->childCount() : 0; |
|
| 147 | 155 |
} |
| 148 | 156 |
|
| 149 | 157 |
void DownloadQueueModel::loadDownloadQueue(QNNTPGrabGlue *glue) |
| ... | ... | |
| 244 | 252 |
int ret = (((double)size - sizeRemaining) / size) * 100; |
| 245 | 253 |
|
| 246 | 254 |
if (ret < 0 || ret > 100) {
|
| 247 |
qWarning("Invalid progress detected, size = %lli, size_remaining = %lli", size, sizeRemaining);
|
|
| 255 |
qDebug("Invalid progress detected, size = %lli, size_remaining = %lli", size, sizeRemaining);
|
|
| 248 | 256 |
} |
| 249 | 257 |
|
| 250 | 258 |
return qMin(100, ret); |
| ... | ... | |
| 409 | 417 |
|
| 410 | 418 |
idx = createIndex(subject_node->row(), (int) DownloadQueueItem::FIELD_FILE_SIZE_REMAINING_STR, subject_node); |
| 411 | 419 |
emit dataChanged(idx, idx); |
| 420 |
|
|
| 421 |
emit requestColumnResize(DownloadQueueItem::FIELD_STATE_STR); |
|
| 412 | 422 |
} |
| 413 | 423 |
|
| 414 | 424 |
void DownloadQueueModel::onFileAdded(QString collection_name, QString subject, QString poster, QDateTime stamp, quint64 file_size, quint64 total_size, quint64 total_size_remaining, QNNTPGrabGlue::TaskState state, int num_parts, QList |
| ... | ... | |
| 421 | 431 |
return; |
| 422 | 432 |
} |
| 423 | 433 |
|
| 434 |
beginInsertRows(createIndex(collection_node->row(), 0, collection_node), collection_node->childCount(), collection_node->childCount()); |
|
| 435 |
|
|
| 424 | 436 |
DownloadQueueItem *file_node = new DownloadQueueItem(collection_node); |
| 425 | 437 |
file_node->subject = subject; |
| 426 | 438 |
file_node->poster = poster; |
| ... | ... | |
| 447 | 459 |
file_node->sizeRemainingStr = QNNTPGrabGlue::utilsCalculateFileSize(0); |
| 448 | 460 |
} |
| 449 | 461 |
|
| 450 |
QModelIndex idx_start = createIndex(file_node->row(), 0, file_node); |
|
| 451 |
QModelIndex idx_end = createIndex(file_node->row(), DownloadQueueItem::NUM_FIELDS - 1, file_node); |
|
| 452 |
emit dataChanged(idx_start, idx_end); |
|
| 453 |
|
|
| 454 | 462 |
/* Update the parent */ |
| 455 | 463 |
collection_node->appendChild(file_node); |
| 456 | 464 |
|
| 465 |
endInsertRows(); |
|
| 466 |
|
|
| 457 | 467 |
collection_node->size = total_size; |
| 458 | 468 |
collection_node->sizeStr = QNNTPGrabGlue::utilsCalculateFileSize(total_size); |
| 459 | 469 |
collection_node->sizeRemaining = total_size_remaining; |
| ... | ... | |
| 468 | 478 |
|
| 469 | 479 |
idx = createIndex(collection_node->row(), (int) DownloadQueueItem::FIELD_FILE_SIZE_REMAINING_STR, collection_node); |
| 470 | 480 |
emit dataChanged(idx, idx); |
| 481 |
|
|
| 482 |
emit requestColumnResize(DownloadQueueItem::FIELD_STATE_STR); |
|
| 471 | 483 |
} |
| 472 | 484 |
|
| 473 | 485 |
void DownloadQueueModel::onFileRemoved(QString collection_name, QString subject, quint64 total_size, quint64 total_size_remaining) |
| ... | ... | |
| 482 | 494 |
return; |
| 483 | 495 |
} |
| 484 | 496 |
|
| 497 |
beginRemoveRows(createIndex(collection_node->row(), 0, collection_node), subject_node->row(), subject_node->row()); |
|
| 498 |
|
|
| 485 | 499 |
collection_node->size = total_size; |
| 486 | 500 |
collection_node->sizeStr = QNNTPGrabGlue::utilsCalculateFileSize(total_size); |
| 487 | 501 |
collection_node->sizeRemaining = total_size_remaining; |
| ... | ... | |
| 489 | 503 |
collection_node->progress = calculateProgress(total_size, total_size_remaining); |
| 490 | 504 |
|
| 491 | 505 |
collection_node->removeChild(subject_node); |
| 506 |
delete subject_node; |
|
| 492 | 507 |
|
| 493 |
QModelIndex idx_start = createIndex(subject_node->row(), 0, subject_node); |
|
| 494 |
QModelIndex idx_end = createIndex(subject_node->row(), DownloadQueueItem::NUM_FIELDS - 1, subject_node); |
|
| 495 |
emit dataChanged(idx_start, idx_end); |
|
| 508 |
endRemoveRows(); |
|
| 496 | 509 |
|
| 497 |
delete subject_node; |
|
| 498 |
|
|
| 499 | 510 |
QModelIndex idx = createIndex(collection_node->row(), (int) DownloadQueueItem::FIELD_STATE_STR, collection_node); |
| 500 | 511 |
emit dataChanged(idx, idx); |
| 501 | 512 |
|
| ... | ... | |
| 541 | 552 |
|
| 542 | 553 |
void DownloadQueueModel::onCollectionAdded(QString collection_name, QString poster) |
| 543 | 554 |
{
|
| 555 |
beginInsertRows(QModelIndex(), rootItem->childCount(), rootItem->childCount()); |
|
| 556 |
|
|
| 544 | 557 |
DownloadQueueItem *collection_node = new DownloadQueueItem(rootItem); |
| 545 | 558 |
collection_node->subject = collection_name; |
| 546 | 559 |
collection_node->poster = poster; |
| 560 |
|
|
| 547 | 561 |
rootItem->appendChild(collection_node); |
| 548 | 562 |
|
| 549 |
QModelIndex idx_start = createIndex(collection_node->row(), 0, collection_node); |
|
| 550 |
QModelIndex idx_end = createIndex(collection_node->row(), DownloadQueueItem::NUM_FIELDS - 1, collection_node); |
|
| 551 |
emit dataChanged(idx_start, idx_end); |
|
| 563 |
endInsertRows(); |
|
| 564 |
|
|
| 565 |
QModelIndex idx_begin = createIndex(collection_node->row(), (int) DownloadQueueItem::FIELD_STATE_STR, collection_node); |
|
| 566 |
QModelIndex idx_end = createIndex(collection_node->row(), (int) DownloadQueueItem::NUM_FIELDS, collection_node); |
|
| 567 |
emit dataChanged(idx_begin, idx_end); |
|
| 568 |
|
|
| 569 |
emit requestColumnResize(DownloadQueueItem::FIELD_SUBJECT); |
|
| 552 | 570 |
} |
| 553 | 571 |
|
| 554 | 572 |
void DownloadQueueModel::onCollectionRemoved(QString collection_name) |
| ... | ... | |
| 560 | 578 |
return; |
| 561 | 579 |
} |
| 562 | 580 |
|
| 581 |
/* Does the collection still contain files? */ |
|
| 582 |
if (collection_node->childCount() != 0) {
|
|
| 583 |
qWarning("Request received to remove collection, but collection is not empty");
|
|
| 584 |
return; |
|
| 585 |
} |
|
| 586 |
|
|
| 587 |
beginRemoveRows(createIndex(rootItem->row(), 0, rootItem), collection_node->row(), collection_node->row()); |
|
| 588 |
|
|
| 563 | 589 |
rootItem->removeChild(collection_node); |
| 590 |
//delete collection_node; /* Disabled for now as this causes a segfault later on */ |
|
| 564 | 591 |
|
| 565 |
QModelIndex idx_start = createIndex(collection_node->row(), 0, collection_node); |
|
| 566 |
QModelIndex idx_end = createIndex(collection_node->row(), DownloadQueueItem::NUM_FIELDS - 1, collection_node); |
|
| 567 |
emit dataChanged(idx_start, idx_end); |
|
| 568 |
|
|
| 569 |
delete collection_node; |
|
| 592 |
endRemoveRows(); |
|
| 570 | 593 |
} |
| 571 | 594 |
|
| 572 | 595 |
void DownloadQueueModel::onCollectionModified(QString collection_name, QString poster) |
| ... | ... | |
| 603 | 626 |
|
| 604 | 627 |
QModelIndex parent_idx = createIndex(rootItem->row(), 0, rootItem); |
| 605 | 628 |
int new_pos_in_model = qMin(rootItem->childCount(), ((old_position < new_position) ? new_position + 1 : new_position)); |
| 606 |
if (!beginMoveRows(parent_idx, old_position, old_position, parent_idx, new_pos_in_model)) {
|
|
| 629 |
if (!beginMoveRows(QModelIndex(), old_position, old_position, QModelIndex(), new_pos_in_model)) {
|
|
| 607 | 630 |
qWarning("beginMoveRows failed\n");
|
| 608 | 631 |
return; |
| 609 | 632 |
} |
Also available in: Unified diff
NNTPGrab

