X-Git-Url: http://git.maemo.org/git/?p=qtrapids;a=blobdiff_plain;f=src%2Fclient%2FDownloadView.cpp;h=cf04e51c70e55a8ebb22bcb16d9753985d56ab07;hp=d2201db6fc2d9f8c47009b2de1d8f813c901c5a6;hb=1d97c0a567b80cf9fc67791669debfe3aa9c4c84;hpb=06410091b1e07c443849f0fe71050654c6fb9710 diff --git a/src/client/DownloadView.cpp b/src/client/DownloadView.cpp index d2201db..cf04e51 100644 --- a/src/client/DownloadView.cpp +++ b/src/client/DownloadView.cpp @@ -1,11 +1,9 @@ /*************************************************************************** - * Copyright (C) 2009 by Lassi Väätämöinen * - * lassi.vaatamoinen@ixonos.com * + * Copyright (C) 2010 by Ixonos Plc * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * + * the Free Software Foundation; version 2 of the License. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * @@ -28,154 +26,219 @@ namespace qtrapids { - DownloadView::DownloadView(QWidget* parent) : - QTreeWidget(parent), - items_() - { - setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list) - setHeaderItem(DownloadViewItem::getHeaderItem()); - - connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)), - this, SLOT(on_itemClicked(QTreeWidgetItem*, int))); - - } - - - DownloadView::~DownloadView() - { - } - - void DownloadView::updateItem(TorrentState const& info, ParamsMap_t other_info) - { - DownloadItems_t::iterator p = items_.find(info.hash); - if (p != items_.end()) { - updateItem_(p.value(), info, other_info); - } else { - addItem_(info, other_info); - } - } - - void DownloadView::addItem_(TorrentState const& info, ParamsMap_t) - { - DownloadViewItem *item = new DownloadViewItem - ( info.hash, - QStringList() - << info.name - << formatSize(info.total_size) - << GetStatusString((TorrentStatus::Id)info.state) - << formatProgress(info.progress) - << QString::number(info.down_rate, 'f', 2) - << QString::number(info.up_rate, 'f', 2) - << QString::number(info.seeds) + "/" + QString::number(info.leeches) - << QString::number(info.ratio) - << "ETA" ); - - QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state)); - item->setForeground(2, brushTmp); - - addTopLevelItem(item); - items_[info.hash] = item; - } - - - void DownloadView::updateItem_(DownloadViewItem *item - , TorrentState const& info, ParamsMap_t) - { - item->setData(2, Qt::DisplayRole, - QVariant(GetStatusString((TorrentStatus::Id)info.state))); - item->setData(3, Qt::DisplayRole, - QVariant(formatProgress(info.progress))); - item->setData(4, Qt::DisplayRole, - QVariant(QString::number(info.down_rate))); - item->setData(5, Qt::DisplayRole, - QVariant(QString::number(info.up_rate))); - item->setData(6, Qt::DisplayRole, - QString::number(info.seeds) + "/" + QString::number(info.leeches)); - - QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state)); - item->setForeground(2, brushTmp); - } - - - QString DownloadView::removeSelected() - { - qDebug() << "DownloadView::removeSelected() " << topLevelItemCount() ; - - DownloadViewItem *item = dynamic_cast (currentItem()); - QString hash = item->getHash(); - - int removed = items_.remove(hash); - if (!removed) - qDebug() << "Inconsistent download view state on item removal"; - - int index = indexOfTopLevelItem(currentItem()); - if (index >= 0) { - takeTopLevelItem(index); - } - - qDebug() << "DownloadView::removeSelected() " << topLevelItemCount() ; - - return hash; - } - - - void DownloadView::on_itemClicked(QTreeWidgetItem * , int) - { - /* - qDebug() << "DownloadView::on_itemClicked(()" << item << "," << column; - qDebug() << "current item" << currentItem(); - - if (item == currentItem() && item->isSelected()) { - item->setSelected(false); - } - */ - } - - - QString DownloadView::GetStatusString(TorrentStatus::Id status) - { - switch (status) { - case TorrentStatus::QUEUED_FOR_CHECKING : - return tr("Queued"); - case TorrentStatus::CHECKING_FILES : - return tr("Checking"); - case TorrentStatus::DOWNLOADING_METADATA : - return tr("DL meta"); - case TorrentStatus::DOWNLOADING : - return tr("Downloading"); - case TorrentStatus::FINISHED : - return tr("Finished"); - case TorrentStatus::SEEDING : - return tr("Seeding"); - case TorrentStatus::ALLOCATING : - return tr("Allocating"); - case TorrentStatus::CHECKING_RESUME_DATA : - return tr("Checking resume"); - default: - return tr("N/A"); - } - } - - - QColor DownloadView::GetStatusColor(TorrentStatus::Id status) - { - QColor green(40,205,40); - QColor yellow(255,174,0); - - switch (status) { - case TorrentStatus::QUEUED_FOR_CHECKING : - case TorrentStatus::CHECKING_FILES : - case TorrentStatus::DOWNLOADING_METADATA : - case TorrentStatus::ALLOCATING : - case TorrentStatus::CHECKING_RESUME_DATA: - return yellow; - case TorrentStatus::DOWNLOADING : - case TorrentStatus::FINISHED : - case TorrentStatus::SEEDING : - return green; - default: - return QColor(); - } - } +DownloadView::DownloadView(QWidget* parent) : + QTreeWidget(parent), + items_(), + settings_() +{ + setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list) + setHeaderItem(DownloadViewItem::getHeaderItem()); + + connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)), + this, SLOT(on_itemClicked(QTreeWidgetItem*, int))); + +} + + +DownloadView::~DownloadView() +{ +} + +void DownloadView::updateItem(TorrentState const& info, ParamsMap_t other_info) +{ + DownloadItems_t::iterator p = items_.find(info.hash); + switch (info.action) { + case TorrentState::action_add : + if (p == items_.end()) { + addItem_(info, other_info); + } else { + qWarning() << "item with similar info hash marked as added"; + updateItem_(p.value(), info, other_info); + } + break; + case TorrentState::action_update : + if (p != items_.end()) { + updateItem_(p.value(), info, other_info); + } else { + qWarning() << "item does not exist in list but information update arrived"; + } + break; + case TorrentState::action_remove : + if (p != items_.end()) { + removeItem_(p.value(), info); + } else { + qWarning() << "item removal request arrived but there is no such item"; + } + break; + default: + qWarning() << "unknown action requested: " << info.action; + break; + } +} + +void DownloadView::removeItem_(DownloadViewItem *item, TorrentState const& info) +{ + QString hash = item->getHash(); + + int removed = items_.remove(hash); + if (!removed) + qDebug() << "Inconsistent download view state on item removal"; + + int index = indexOfTopLevelItem(item); + if (index >= 0) { + takeTopLevelItem(index); + } +} + +void DownloadView::addItem_(TorrentState const& info, ParamsMap_t) +{ + DownloadViewItem *item = new DownloadViewItem + ( info.hash, + QStringList() + << info.name + << formatSize(info.total_size) + << GetStatusString((TorrentStatus::Id)info.state) + << formatProgress(info.progress) + << formatSize(info.down_rate) + << formatSize(info.up_rate) + << QString::number(info.seeds) + "/" + QString::number(info.leeches) + << QString::number(info.ratio) + << "ETA" ); + + QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state)); + item->setForeground(2, brushTmp); + + addTopLevelItem(item); + items_[info.hash] = item; +} + + +void DownloadView::updateItem_(DownloadViewItem *item + , TorrentState const& info, ParamsMap_t) +{ + item->setData(2, Qt::DisplayRole, + QVariant(GetStatusString((TorrentStatus::Id)info.state))); + item->setData(3, Qt::DisplayRole, + QVariant(formatProgress(info.progress))); + item->setData(4, Qt::DisplayRole, + QVariant(formatSize(info.down_rate))); + item->setData(5, Qt::DisplayRole, + QVariant(formatSize(info.up_rate))); + item->setData(6, Qt::DisplayRole, + QString::number(info.seeds) + "/" + QString::number(info.leeches)); + item->setData(7, Qt::DisplayRole, QString::number(info.ratio)); + + // Calculate ETA + if (info.down_rate > 0) { + qulonglong eta = (info.total_size - info.total_done) / info.down_rate; + item->setData(8, Qt::DisplayRole, formatElapsedTime(eta)); + } else { + item->setData(8, Qt::DisplayRole, "N/A"); + } + + // Set color for status text + QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state)); + item->setForeground(2, brushTmp); +} + + +QString DownloadView::prepareRemoveSelected() +{ + qDebug() << "DownloadView::removeSelected() " << topLevelItemCount() ; + + DownloadViewItem *item = dynamic_cast (currentItem()); + QString hash = item->getHash(); + + item->setDisabled(true); + + qDebug() << "DownloadView::removeSelected() " << topLevelItemCount() ; + + return hash; +} + + +void DownloadView::on_itemClicked(QTreeWidgetItem * , int) +{ + /* + qDebug() << "DownloadView::on_itemClicked(()" << item << "," << column; + qDebug() << "current item" << currentItem(); + + if (item == currentItem() && item->isSelected()) { + item->setSelected(false); + } + */ +} + + +void DownloadView::saveView() +{ + QTreeWidgetItem *item = headerItem(); + QList columns; + + for (int i = 0; i < item->columnCount(); ++i) { + isColumnHidden(i) ? columns.push_back(QVariant(false)) : columns.push_back(QVariant(true)); + } + + settings_.setValue("downloadview_columns", QVariant(columns)); +} + + +void DownloadView::restoreView() +{ + QTreeWidgetItem *item = headerItem(); + QVariant columns(settings_.value("downloadview_columns")); + QList columnList = columns.toList(); + + for (int i = 0; i < columnList.size(); ++i) { + columnList.at(i).toBool() ? setColumnHidden(i, false) : setColumnHidden(i, true); + } +} + + +QString DownloadView::GetStatusString(TorrentStatus::Id status) +{ + switch (status) { + case TorrentStatus::QUEUED_FOR_CHECKING : + return tr("Queued"); + case TorrentStatus::CHECKING_FILES : + return tr("Checking"); + case TorrentStatus::DOWNLOADING_METADATA : + return tr("DL meta"); + case TorrentStatus::DOWNLOADING : + return tr("Downloading"); + case TorrentStatus::FINISHED : + return tr("Finished"); + case TorrentStatus::SEEDING : + return tr("Seeding"); + case TorrentStatus::ALLOCATING : + return tr("Allocating"); + case TorrentStatus::CHECKING_RESUME_DATA : + return tr("Checking resume"); + default: + return tr("N/A"); + } +} + + +QColor DownloadView::GetStatusColor(TorrentStatus::Id status) +{ + QColor green(40,205,40); + QColor yellow(255,174,0); + + switch (status) { + case TorrentStatus::QUEUED_FOR_CHECKING : + case TorrentStatus::CHECKING_FILES : + case TorrentStatus::DOWNLOADING_METADATA : + case TorrentStatus::ALLOCATING : + case TorrentStatus::CHECKING_RESUME_DATA: + return yellow; + case TorrentStatus::DOWNLOADING : + case TorrentStatus::FINISHED : + case TorrentStatus::SEEDING : + return green; + default: + return QColor(); + } +} } // namespace qtrapids