- Scrollbar added to column selector
[qtrapids] / src / client / DownloadView.cpp
1 /***************************************************************************
2  *   Copyright (C) 2009 by Lassi Väätämöinen   *
3  *   lassi.vaatamoinen@ixonos.com   *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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>
21
22 #include "DownloadView.h"
23
24 #include <QDebug>
25 #include <QVariant>
26 #include <QColor>
27
28 namespace qtrapids
29 {
30
31 DownloadView::DownloadView(QWidget* parent) :
32                 QTreeWidget(parent),
33                 items_(),
34                 settings_()
35 {
36         setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list)
37         setHeaderItem(DownloadViewItem::getHeaderItem());
38
39         connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)),
40                 this, SLOT(on_itemClicked(QTreeWidgetItem*, int)));
41
42 }
43
44
45 DownloadView::~DownloadView()
46 {
47 }
48
49 void DownloadView::updateItem(TorrentState const& info, ParamsMap_t other_info)
50 {
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);
56                 } else {
57                         qWarning() << "item with similar info hash marked as added";
58                         updateItem_(p.value(), info, other_info);
59                 }
60                 break;
61         case TorrentState::action_update :
62                 if (p != items_.end()) {
63                         updateItem_(p.value(), info, other_info);
64                 } else {
65                         qWarning() << "item does not exist in list but information update arrived";
66                 }
67                 break;
68         case TorrentState::action_remove :
69                 if (p != items_.end()) {
70                         removeItem_(p.value(), info);
71                 } else {
72                         qWarning() << "item removal request arrived but there is no such item";
73                 }
74                 break;
75         default:
76                 qWarning() << "unknown action requested: " << info.action;
77                 break;
78         }
79 }
80
81 void DownloadView::removeItem_(DownloadViewItem *item, TorrentState const& info)
82 {
83         QString hash = item->getHash();
84
85         int removed = items_.remove(hash);
86         if (!removed)
87                 qDebug() << "Inconsistent download view state on item removal";
88
89         int index = indexOfTopLevelItem(item);
90         if (index >= 0) {
91                 takeTopLevelItem(index);
92         }
93 }
94
95 void DownloadView::addItem_(TorrentState const& info, ParamsMap_t)
96 {
97         DownloadViewItem *item = new DownloadViewItem
98         ( info.hash,
99           QStringList()
100           << info.name
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)
108           << "ETA" );
109
110         QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state));
111         item->setForeground(2, brushTmp);
112
113         addTopLevelItem(item);
114         items_[info.hash] = item;
115 }
116
117
118 void DownloadView::updateItem_(DownloadViewItem *item
119                                , TorrentState const& info, ParamsMap_t)
120 {
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));
132         
133         // Calculate ETA
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));
137         }       else {
138                 item->setData(8, Qt::DisplayRole, "N/A");
139         }
140         
141         // Set color for status text
142         QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state));
143         item->setForeground(2, brushTmp);
144 }
145
146
147 QString DownloadView::prepareRemoveSelected()
148 {
149         qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
150
151         DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
152         QString hash = item->getHash();
153
154         item->setDisabled(true);
155
156         qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
157
158         return hash;
159 }
160
161
162 void DownloadView::on_itemClicked(QTreeWidgetItem * , int)
163 {
164         /*
165           qDebug() << "DownloadView::on_itemClicked(()" << item << "," << column;
166           qDebug() << "current item" << currentItem();
167
168           if (item == currentItem() && item->isSelected()) {
169           item->setSelected(false);
170           }
171         */
172 }
173
174
175 void DownloadView::saveView()
176 {
177                 QTreeWidgetItem *item = headerItem();
178                 QList<QVariant> columns;
179                 
180                 for (int i = 0; i < item->columnCount(); ++i) {
181                         isColumnHidden(i) ? columns.push_back(QVariant(false)) : columns.push_back(QVariant(true));
182                 }
183                 
184         settings_.setValue("downloadview_columns", QVariant(columns));
185 }
186
187
188 void DownloadView::restoreView()
189 {
190         QTreeWidgetItem *item = headerItem();
191         QVariant columns(settings_.value("downloadview_columns"));
192         QList<QVariant> columnList = columns.toList();
193         
194         for (int i = 0; i < columnList.size(); ++i) {
195                 columnList.at(i).toBool() ? setColumnHidden(i, false) : setColumnHidden(i, true);
196         }
197 }
198
199
200 QString DownloadView::GetStatusString(TorrentStatus::Id status)
201 {
202         switch (status) {
203                 case TorrentStatus::QUEUED_FOR_CHECKING :
204                         return tr("Queued");
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");
219                 default:
220                         return tr("N/A");
221         }
222 }
223
224
225 QColor DownloadView::GetStatusColor(TorrentStatus::Id status)
226 {
227         QColor green(40,205,40);
228         QColor yellow(255,174,0);
229
230         switch (status) {
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:
236                         return yellow;
237                 case TorrentStatus::DOWNLOADING :
238                 case TorrentStatus::FINISHED :
239                 case TorrentStatus::SEEDING :
240                         return green;
241                 default:
242                         return QColor();
243         }
244 }
245
246 } // namespace qtrapids