1 /***************************************************************************
2 * Copyright (C) 2009 by Lassi Väätämöinen *
3 * lassi.vaatamoinen@ixonos.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #include <qtrapids/format.hpp>
22 #include "DownloadView.h"
31 DownloadView::DownloadView(QWidget* parent) :
35 setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list)
36 setHeaderItem(DownloadViewItem::getHeaderItem());
38 connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)),
39 this, SLOT(on_itemClicked(QTreeWidgetItem*, int)));
44 DownloadView::~DownloadView()
48 void DownloadView::updateItem(TorrentState const& info, ParamsMap_t other_info)
50 DownloadItems_t::iterator p = items_.find(info.hash);
51 switch (info.action) {
52 case TorrentState::action_add :
53 if (p == items_.end()) {
54 addItem_(info, other_info);
56 qWarning() << "item with similar info hash marked as added";
57 updateItem_(p.value(), info, other_info);
60 case TorrentState::action_update :
61 if (p != items_.end()) {
62 updateItem_(p.value(), info, other_info);
64 qWarning() << "item does not exist in list but information update arrived";
67 case TorrentState::action_remove :
68 if (p != items_.end()) {
69 removeItem_(p.value(), info);
71 qWarning() << "item removal request arrived but there is no such item";
75 qWarning() << "unknown action requested: " << info.action;
80 void DownloadView::removeItem_(DownloadViewItem *item, TorrentState const& info)
82 QString hash = item->getHash();
84 int removed = items_.remove(hash);
86 qDebug() << "Inconsistent download view state on item removal";
88 int index = indexOfTopLevelItem(item);
90 takeTopLevelItem(index);
94 void DownloadView::addItem_(TorrentState const& info, ParamsMap_t)
96 DownloadViewItem *item = new DownloadViewItem
100 << formatSize(info.total_size)
101 << GetStatusString((TorrentStatus::Id)info.state)
102 << formatProgress(info.progress)
103 << formatSize(info.down_rate)
104 << formatSize(info.up_rate)
105 << QString::number(info.seeds) + "/" + QString::number(info.leeches)
106 << QString::number(info.ratio)
109 QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state));
110 item->setForeground(2, brushTmp);
112 addTopLevelItem(item);
113 items_[info.hash] = item;
117 void DownloadView::updateItem_(DownloadViewItem *item
118 , TorrentState const& info, ParamsMap_t)
120 item->setData(2, Qt::DisplayRole,
121 QVariant(GetStatusString((TorrentStatus::Id)info.state)));
122 item->setData(3, Qt::DisplayRole,
123 QVariant(formatProgress(info.progress)));
124 item->setData(4, Qt::DisplayRole,
125 QVariant(formatSize(info.down_rate)));
126 item->setData(5, Qt::DisplayRole,
127 QVariant(formatSize(info.up_rate)));
128 item->setData(6, Qt::DisplayRole,
129 QString::number(info.seeds) + "/" + QString::number(info.leeches));
131 QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state));
132 item->setForeground(2, brushTmp);
136 QString DownloadView::prepareRemoveSelected()
138 qDebug() << "DownloadView::removeSelected() " << topLevelItemCount() ;
140 DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
141 QString hash = item->getHash();
143 item->setDisabled(true);
145 qDebug() << "DownloadView::removeSelected() " << topLevelItemCount() ;
151 void DownloadView::on_itemClicked(QTreeWidgetItem * , int)
154 qDebug() << "DownloadView::on_itemClicked(()" << item << "," << column;
155 qDebug() << "current item" << currentItem();
157 if (item == currentItem() && item->isSelected()) {
158 item->setSelected(false);
164 QString DownloadView::GetStatusString(TorrentStatus::Id status)
167 case TorrentStatus::QUEUED_FOR_CHECKING :
169 case TorrentStatus::CHECKING_FILES :
170 return tr("Checking");
171 case TorrentStatus::DOWNLOADING_METADATA :
172 return tr("DL meta");
173 case TorrentStatus::DOWNLOADING :
174 return tr("Downloading");
175 case TorrentStatus::FINISHED :
176 return tr("Finished");
177 case TorrentStatus::SEEDING :
178 return tr("Seeding");
179 case TorrentStatus::ALLOCATING :
180 return tr("Allocating");
181 case TorrentStatus::CHECKING_RESUME_DATA :
182 return tr("Checking resume");
189 QColor DownloadView::GetStatusColor(TorrentStatus::Id status)
191 QColor green(40,205,40);
192 QColor yellow(255,174,0);
195 case TorrentStatus::QUEUED_FOR_CHECKING :
196 case TorrentStatus::CHECKING_FILES :
197 case TorrentStatus::DOWNLOADING_METADATA :
198 case TorrentStatus::ALLOCATING :
199 case TorrentStatus::CHECKING_RESUME_DATA:
201 case TorrentStatus::DOWNLOADING :
202 case TorrentStatus::FINISHED :
203 case TorrentStatus::SEEDING :
210 } // namespace qtrapids