X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Fclient%2Fmodels%2FQDeclarativeDownloadListModel.cpp;fp=src%2Fclient%2Fmodels%2FQDeclarativeDownloadListModel.cpp;h=e990705a9c5a9c15c9b504bf3a8fbc1040da089a;hb=711f20225524f059da9296f814a06ab9332bb324;hp=0000000000000000000000000000000000000000;hpb=678f23ebb9d4b0f2ce713be2ce7bba9cb3c42bb5;p=qtrapids diff --git a/src/client/models/QDeclarativeDownloadListModel.cpp b/src/client/models/QDeclarativeDownloadListModel.cpp new file mode 100644 index 0000000..e990705 --- /dev/null +++ b/src/client/models/QDeclarativeDownloadListModel.cpp @@ -0,0 +1,304 @@ +/*************************************************************************** + * 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; 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 * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include +#include + +#include "QDeclarativeDownloadListModel.h" +#include "QDeclarativeDownloadListModel_p.h" +#include "qtrapids/format.hpp" + +namespace qtrapids +{ + +QDeclarativeDownloadListModel::QDeclarativeDownloadListModel(QObject *parent): + QAbstractListModel(parent) +{ + d = new QDeclarativeDownloadListModelPrivate; + + // These roles are the names that we use to access the model data on QML + // in the string format. So, use the value on the right-hand side of assignment operator in QML. + // The role enum value is passed from QML side to the C++ model for fetching the data. + QHash roles; + roles[DownloadRole] = "download"; + roles[SeedRole] = "seed"; + roles[HashRole] = "hash"; + roles[NameRole] = "name"; + roles[SizeRole] = "size"; + roles[StatusRole] = "status"; + roles[StateRole] = "state"; + roles[ProgressRole] = "progress"; + roles[DownRateRole] = "downRate"; + roles[UpRateRole] = "upRate"; + roles[SeedsRole] = "seeds"; + roles[LeechesRole] = "leeches"; + roles[RatioRole] = "ratio"; + roles[TotaSizeRole] = "totalSize"; + roles[TotalDoneRole] = "totalDone"; + setRoleNames(roles); +} + +QDeclarativeDownloadListModel::~QDeclarativeDownloadListModel() +{ +} + + +void +QDeclarativeDownloadListModel::updateItem(TorrentState const& info, + ParamsMap_t other_info) +{ + qDebug() << Q_FUNC_INFO << " enter"; + + DownloadItems_t::iterator p = d->items_.find(info.hash); + switch (info.action) { + case TorrentState::action_add : + if (p == d->items_.end()) { + addItem_(info, other_info); + } else { + qWarning() << "item with similar info hash marked as added"; + updateItem_(info, other_info); + } + break; + case TorrentState::action_update : + if (p != d->items_.end()) { + updateItem_(info, other_info); + } else { + qWarning() << "item does not exist in list but information update arrived"; + } + break; + case TorrentState::action_remove : + if (p != d->items_.end()) { + removeItem_(info); + } else { + qWarning() << "item removal request arrived but there is no such item"; + } + break; + default: + qWarning() << "unknown action requested: " << info.action; + break; + } +} + + + // Returns the hash of the torrent be removed. +QString QDeclarativeDownloadListModel::prepareRemoveSelected() +{ + return "DUMMY_HASH"; +} + +int QDeclarativeDownloadListModel::rowCount(const QModelIndex &parent) const +{ + return d->items_.count(); +} + +QVariant QDeclarativeDownloadListModel::data(const QModelIndex &index, int role) const +{ + + qDebug() << Q_FUNC_INFO << " enter"; + int rowIndex = index.row(); + QString hash; + TorrentState item; + + // Get the item hash corresponding to row number: + if (d->itemIndexes_.contains(rowIndex)) + hash = d->itemIndexes_[rowIndex]; + else + return QVariant(); + + // Get the torrent info for the hash: + if (d->items_.contains(hash)) + item = d->items_[hash]; + else + return QVariant(); + + switch (role) { +// case DownloadRole: +// // TODO: What to do here?? +// break; +// case SeedRole: +// // TODO: What to do here?? +// break; + case HashRole: + return QVariant(item.hash); + case NameRole: + return QVariant(item.name); +// case SizeRole: +// return QVariant(item.total_size); + case StatusRole: + return QVariant(); + case StateRole: + return QVariant(item.state); + case ProgressRole: + return QVariant(item.progress); + case DownRateRole: + return QVariant(item.down_rate); + case UpRateRole: + return QVariant(item.up_rate); + case SeedsRole: + return QVariant(item.seeds); + case LeechesRole: + return QVariant(item.leeches); + case RatioRole: + return QVariant(item.ratio); + case TotaSizeRole: + return QVariant(item.total_size); + case TotalDoneRole: + return QVariant(item.total_done); + default: + qWarning() << Q_FUNC_INFO << "Unknown role requested from model"; + return QVariant(); + } + return QVariant(); +} + +bool QDeclarativeDownloadListModel::insertRow(int row, const QModelIndex &parent) +{ + qDebug() << "QDeclarativeDownloadListModel::insertRow()"; + return false; +} + +bool QDeclarativeDownloadListModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + qDebug() << "QDeclarativeDownloadListModel::setData()"; + return false; +} + + +void QDeclarativeDownloadListModel::classBegin() +{ + qDebug() << "QDeclarativeDownloadListModel::classBegin()"; +} + +void QDeclarativeDownloadListModel::componentComplete() +{ + qDebug() << "QDeclarativeDownloadListModel::componentComplete()"; +} + + +void QDeclarativeDownloadListModel::removeItem_(TorrentState const& info) +{ + qDebug() << Q_FUNC_INFO << " enter"; + QString hash = info.hash; + + // TODO: Emit rows remove start + int removed = d->items_.remove(hash); + // TODO: Edit rows insert end + if (!removed) + qDebug() << "Inconsistent download view state on item removal"; + + /* + int index = indexOfTopLevelItem(item); + if (index >= 0) { + takeTopLevelItem(index); + } + */ +} + +void QDeclarativeDownloadListModel::addItem_(TorrentState const& info, + ParamsMap_t) +{ + qDebug() << Q_FUNC_INFO << " enter"; +/* + 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); + */ + + int itemsCount = d->items_.count(); + beginInsertRows(QModelIndex(), itemsCount, itemsCount+1); + d->items_[info.hash] = info; + d->itemIndexes_[itemsCount+1] = info.hash; + endInsertRows(); +} + + +void QDeclarativeDownloadListModel::updateItem_(TorrentState const& info, ParamsMap_t) +{ + qDebug() << Q_FUNC_INFO << " enter"; + /* + 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"); + } +*/ + // TODO: Update items data & Emit data changed. + +/* + // Set color for status text + QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state)); + item->setForeground(2, brushTmp); + */ +} + + + +QString QDeclarativeDownloadListModel::GetStatusString(TorrentStatus::Id status) +{ + qDebug() << Q_FUNC_INFO << " enter"; + 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"); + } +} + +} // namespace qtrapids