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