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) :
36 setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list)
37 setHeaderItem(DownloadViewItem::getHeaderItem());
39 connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)),
40 this, SLOT(on_itemClicked(QTreeWidgetItem*, int)));
45 DownloadView::~DownloadView()
49 void DownloadView::updateItem(TorrentState const& info, ParamsMap_t other_info)
51 DownloadItems_t::iterator p = items_.find(info.hash);
52 switch (info.action) {
53 case TorrentState::action_add :
54 if (p == items_.end()) {
55 addItem_(info, other_info);
57 qWarning() << "item with similar info hash marked as added";
58 updateItem_(p.value(), info, other_info);
61 case TorrentState::action_update :
62 if (p != items_.end()) {
63 updateItem_(p.value(), info, other_info);
65 qWarning() << "item does not exist in list but information update arrived";
68 case TorrentState::action_remove :
69 if (p != items_.end()) {
70 removeItem_(p.value(), info);
72 qWarning() << "item removal request arrived but there is no such item";
76 qWarning() << "unknown action requested: " << info.action;
81 void DownloadView::removeItem_(DownloadViewItem *item, TorrentState const& info)
83 QString hash = item->getHash();
85 int removed = items_.remove(hash);
87 qDebug() << "Inconsistent download view state on item removal";
89 int index = indexOfTopLevelItem(item);
91 takeTopLevelItem(index);
95 void DownloadView::addItem_(TorrentState const& info, ParamsMap_t)
97 DownloadViewItem *item = new DownloadViewItem
101 << formatSize(info.total_size)
102 << GetStatusString((TorrentStatus::Id)info.state)
103 << formatProgress(info.progress)
104 << formatSize(info.down_rate)
105 << formatSize(info.up_rate)
106 << QString::number(info.seeds) + "/" + QString::number(info.leeches)
107 << QString::number(info.ratio)
110 QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state));
111 item->setForeground(2, brushTmp);
113 addTopLevelItem(item);
114 items_[info.hash] = item;
118 void DownloadView::updateItem_(DownloadViewItem *item
119 , TorrentState const& info, ParamsMap_t)
121 item->setData(2, Qt::DisplayRole,
122 QVariant(GetStatusString((TorrentStatus::Id)info.state)));
123 item->setData(3, Qt::DisplayRole,
124 QVariant(formatProgress(info.progress)));
125 item->setData(4, Qt::DisplayRole,
126 QVariant(formatSize(info.down_rate)));
127 item->setData(5, Qt::DisplayRole,
128 QVariant(formatSize(info.up_rate)));
129 item->setData(6, Qt::DisplayRole,
130 QString::number(info.seeds) + "/" + QString::number(info.leeches));
131 item->setData(7, Qt::DisplayRole, QString::number(info.ratio));
134 if (info.down_rate > 0) {
135 qulonglong eta = (info.total_size - info.total_done) / info.down_rate;
136 item->setData(8, Qt::DisplayRole, formatElapsedTime(eta));
138 item->setData(8, Qt::DisplayRole, "N/A");
141 // Set color for status text
142 QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state));
143 item->setForeground(2, brushTmp);
147 QString DownloadView::prepareRemoveSelected()
149 qDebug() << "DownloadView::removeSelected() " << topLevelItemCount() ;
151 DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
152 QString hash = item->getHash();
154 item->setDisabled(true);
156 qDebug() << "DownloadView::removeSelected() " << topLevelItemCount() ;
162 void DownloadView::on_itemClicked(QTreeWidgetItem * , int)
165 qDebug() << "DownloadView::on_itemClicked(()" << item << "," << column;
166 qDebug() << "current item" << currentItem();
168 if (item == currentItem() && item->isSelected()) {
169 item->setSelected(false);
175 void DownloadView::saveView()
177 QTreeWidgetItem *item = headerItem();
178 QList<QVariant> columns;
180 for (int i = 0; i < item->columnCount(); ++i) {
181 isColumnHidden(i) ? columns.push_back(QVariant(false)) : columns.push_back(QVariant(true));
184 settings_.setValue("downloadview_columns", QVariant(columns));
188 void DownloadView::restoreView()
190 QTreeWidgetItem *item = headerItem();
191 QVariant columns(settings_.value("downloadview_columns"));
192 QList<QVariant> columnList = columns.toList();
194 for (int i = 0; i < columnList.size(); ++i) {
195 columnList.at(i).toBool() ? setColumnHidden(i, false) : setColumnHidden(i, true);
200 QString DownloadView::GetStatusString(TorrentStatus::Id status)
203 case TorrentStatus::QUEUED_FOR_CHECKING :
205 case TorrentStatus::CHECKING_FILES :
206 return tr("Checking");
207 case TorrentStatus::DOWNLOADING_METADATA :
208 return tr("DL meta");
209 case TorrentStatus::DOWNLOADING :
210 return tr("Downloading");
211 case TorrentStatus::FINISHED :
212 return tr("Finished");
213 case TorrentStatus::SEEDING :
214 return tr("Seeding");
215 case TorrentStatus::ALLOCATING :
216 return tr("Allocating");
217 case TorrentStatus::CHECKING_RESUME_DATA :
218 return tr("Checking resume");
225 QColor DownloadView::GetStatusColor(TorrentStatus::Id status)
227 QColor green(40,205,40);
228 QColor yellow(255,174,0);
231 case TorrentStatus::QUEUED_FOR_CHECKING :
232 case TorrentStatus::CHECKING_FILES :
233 case TorrentStatus::DOWNLOADING_METADATA :
234 case TorrentStatus::ALLOCATING :
235 case TorrentStatus::CHECKING_RESUME_DATA:
237 case TorrentStatus::DOWNLOADING :
238 case TorrentStatus::FINISHED :
239 case TorrentStatus::SEEDING :
246 } // namespace qtrapids