1 /***************************************************************************
2 * Copyright (C) 2010 by Ixonos Plc *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; version 2 of the License. *
8 * This program is distributed in the hope that it will be useful, *
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11 * GNU General Public License for more details. *
13 * You should have received a copy of the GNU General Public License *
14 * along with this program; if not, write to the *
15 * Free Software Foundation, Inc., *
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17 ***************************************************************************/
18 #include <qtrapids/format.hpp>
20 #include "DownloadView.h"
29 DownloadView::DownloadView(QWidget* parent) :
34 setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list)
35 setHeaderItem(DownloadViewItem::getHeaderItem());
37 connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)),
38 this, SLOT(on_itemClicked(QTreeWidgetItem*, int)));
43 DownloadView::~DownloadView()
47 void DownloadView::updateItem(TorrentState const& info, ParamsMap_t other_info)
49 DownloadItems_t::iterator p = items_.find(info.hash);
50 switch (info.action) {
51 case TorrentState::action_add :
52 if (p == items_.end()) {
53 addItem_(info, other_info);
55 qWarning() << "item with similar info hash marked as added";
56 updateItem_(p.value(), info, other_info);
59 case TorrentState::action_update :
60 if (p != items_.end()) {
61 updateItem_(p.value(), info, other_info);
63 qWarning() << "item does not exist in list but information update arrived";
66 case TorrentState::action_remove :
67 if (p != items_.end()) {
68 removeItem_(p.value(), info);
70 qWarning() << "item removal request arrived but there is no such item";
74 qWarning() << "unknown action requested: " << info.action;
79 void DownloadView::removeItem_(DownloadViewItem *item, TorrentState const& info)
81 QString hash = item->getHash();
83 int removed = items_.remove(hash);
85 qDebug() << "Inconsistent download view state on item removal";
87 int index = indexOfTopLevelItem(item);
89 takeTopLevelItem(index);
93 void DownloadView::addItem_(TorrentState const& info, ParamsMap_t)
95 DownloadViewItem *item = new DownloadViewItem
99 << formatSize(info.total_size)
100 << GetStatusString((TorrentStatus::Id)info.state)
101 << formatProgress(info.progress)
102 << formatSize(info.down_rate)
103 << formatSize(info.up_rate)
104 << QString::number(info.seeds) + "/" + QString::number(info.leeches)
105 << QString::number(info.ratio)
108 QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state));
109 item->setForeground(2, brushTmp);
111 addTopLevelItem(item);
112 items_[info.hash] = item;
116 void DownloadView::updateItem_(DownloadViewItem *item
117 , TorrentState const& info, ParamsMap_t)
119 item->setData(2, Qt::DisplayRole,
120 QVariant(GetStatusString((TorrentStatus::Id)info.state)));
121 item->setData(3, Qt::DisplayRole,
122 QVariant(formatProgress(info.progress)));
123 item->setData(4, Qt::DisplayRole,
124 QVariant(formatSize(info.down_rate)));
125 item->setData(5, Qt::DisplayRole,
126 QVariant(formatSize(info.up_rate)));
127 item->setData(6, Qt::DisplayRole,
128 QString::number(info.seeds) + "/" + QString::number(info.leeches));
129 item->setData(7, Qt::DisplayRole, QString::number(info.ratio));
132 if (info.down_rate > 0) {
133 qulonglong eta = (info.total_size - info.total_done) / info.down_rate;
134 item->setData(8, Qt::DisplayRole, formatElapsedTime(eta));
136 item->setData(8, Qt::DisplayRole, "N/A");
139 // Set color for status text
140 QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state));
141 item->setForeground(2, brushTmp);
145 QString DownloadView::prepareRemoveSelected()
147 qDebug() << "DownloadView::removeSelected() " << topLevelItemCount() ;
149 DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
150 QString hash = item->getHash();
152 item->setDisabled(true);
154 qDebug() << "DownloadView::removeSelected() " << topLevelItemCount() ;
160 void DownloadView::on_itemClicked(QTreeWidgetItem * , int)
163 qDebug() << "DownloadView::on_itemClicked(()" << item << "," << column;
164 qDebug() << "current item" << currentItem();
166 if (item == currentItem() && item->isSelected()) {
167 item->setSelected(false);
173 void DownloadView::saveView()
175 QTreeWidgetItem *item = headerItem();
176 QList<QVariant> columns;
178 for (int i = 0; i < item->columnCount(); ++i) {
179 isColumnHidden(i) ? columns.push_back(QVariant(false)) : columns.push_back(QVariant(true));
182 settings_.setValue("downloadview_columns", QVariant(columns));
186 void DownloadView::restoreView()
188 QTreeWidgetItem *item = headerItem();
189 QVariant columns(settings_.value("downloadview_columns"));
190 QList<QVariant> columnList = columns.toList();
192 for (int i = 0; i < columnList.size(); ++i) {
193 columnList.at(i).toBool() ? setColumnHidden(i, false) : setColumnHidden(i, true);
198 QString DownloadView::GetStatusString(TorrentStatus::Id status)
201 case TorrentStatus::QUEUED_FOR_CHECKING :
203 case TorrentStatus::CHECKING_FILES :
204 return tr("Checking");
205 case TorrentStatus::DOWNLOADING_METADATA :
206 return tr("DL meta");
207 case TorrentStatus::DOWNLOADING :
208 return tr("Downloading");
209 case TorrentStatus::FINISHED :
210 return tr("Finished");
211 case TorrentStatus::SEEDING :
212 return tr("Seeding");
213 case TorrentStatus::ALLOCATING :
214 return tr("Allocating");
215 case TorrentStatus::CHECKING_RESUME_DATA :
216 return tr("Checking resume");
223 QColor DownloadView::GetStatusColor(TorrentStatus::Id status)
225 QColor green(40,205,40);
226 QColor yellow(255,174,0);
229 case TorrentStatus::QUEUED_FOR_CHECKING :
230 case TorrentStatus::CHECKING_FILES :
231 case TorrentStatus::DOWNLOADING_METADATA :
232 case TorrentStatus::ALLOCATING :
233 case TorrentStatus::CHECKING_RESUME_DATA:
235 case TorrentStatus::DOWNLOADING :
236 case TorrentStatus::FINISHED :
237 case TorrentStatus::SEEDING :
244 } // namespace qtrapids