- Added buildconf.pri to hold the master-level build options.
[qtrapids] / src / gui / 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 <QDebug>
21 #include <QTimer>
22 #include <QVariant>
23 #include <QColor>
24 #include "DownloadView.h"
25
26
27 DownloadView::DownloadView(QWidget* parent) :
28                 QTreeWidget(parent),
29                 items_(),
30                 timer_(NULL)
31 {
32   setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list)
33   setHeaderItem(DownloadViewItem::getHeaderItem());
34
35   connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)),
36           this, SLOT(on_itemClicked(QTreeWidgetItem*, int)));
37
38         timer_ = new QTimer(this);
39         connect(timer_, SIGNAL(timeout()), this, SLOT(on_timeout()));
40         timer_->start(5000);
41
42 }
43
44
45 DownloadView::~DownloadView()
46 {
47 }
48
49
50 void DownloadView::newItem(QTorrentHandle handle)
51 {
52 #ifdef QTRAPIDS_DEBUG
53         qDebug() << "DownloadView::newItem() " << items_.count(handle);
54 #endif
55
56         DownloadViewItem *item = new DownloadViewItem(QStringList()
57                         << handle.name()
58                         << QString::number(handle.getTotalSize())
59                         << GetStatusString(handle.state())
60                         << QString::number(100*handle.progress() + '%')
61                         << QString::number(handle.downloadRate(), 'f', 2) 
62                         << QString::number(handle.uploadRate(),  'f', 2) 
63                         << QString::number(handle.numSeeds()) + "/"
64                                         + QString::number(handle.numLeeches())
65                         << QString::number(handle.ratio())
66                         << "ETA" );
67         
68         // Set text color for status:
69         QBrush brushTmp(GetStatusColor(handle.state()));
70         item->setForeground(2, brushTmp);
71                         
72         addTopLevelItem(item);
73         items_[handle] = item;
74 }
75
76
77 void DownloadView::updateItem(QTorrentHandle handle)
78 {
79         //qDebug() << "DownloadView::updateItem() "  << items_.count(handle);
80                 
81         static float lastProg = 0;
82         
83         // If there are items currently downloading, update:
84         if (items_.count(handle) > 0) {
85                 
86                 DownloadViewItem *item = items_[handle];
87                 
88                 // Only the changing fields are being updated:
89                 item->setData(2, Qt::DisplayRole,
90                                                                         QVariant(GetStatusString(handle.state())));
91                 item->setData(4, Qt::DisplayRole,
92                                                                         QVariant(QString::number(handle.downloadRate(), 'f', 2)));
93                 item->setData(5, Qt::DisplayRole,
94                                                                         QVariant(QString::number(handle.uploadRate(), 'f', 2)));
95                 item->setData(6, Qt::DisplayRole, 
96                                                                         QString::number(handle.numSeeds()) + "/" 
97                                                                                 + QString::number(handle.numLeeches()));
98                                                                                 
99                 // Set progress if increment is 1 percent.                      
100                 float prog = handle.progress();
101                 if ((prog-lastProg) >= 0.01 || prog >= 1.0) {
102                         item->setData(3, Qt::DisplayRole,
103                                                                                 QVariant(QString::number(100*prog) + '%'));
104                         lastProg = prog;
105                 }
106                 
107                 /// @TODO: ETA-counter adjusting,if ETA is to be implemented
108                                                                                 
109                 // Adjust color
110                 QBrush brushTmp(GetStatusColor(handle.state()));
111                 item->setForeground(2, brushTmp);
112         }
113
114 }
115
116
117 QTorrentHandle DownloadView::removeSelected()
118 {
119 #ifdef QTRAPIDS_DEBUG
120         qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
121 #endif
122
123         DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
124         
125         std::map<QTorrentHandle, DownloadViewItem*>::iterator listIter
126                         = items_.begin();
127         std::map<QTorrentHandle, DownloadViewItem*>::iterator listEnd
128                         = items_.end();
129         
130         while (listIter != listEnd) {
131                 if (listIter->second == item) {
132                         break;
133                 }
134                 ++listIter;
135         }
136         
137         QTorrentHandle handle = listIter->first;
138         items_.erase(listIter);
139
140         
141         int index = indexOfTopLevelItem(currentItem());
142         if (index >= 0) {
143                 takeTopLevelItem(index);
144         }
145
146 #ifdef QTRAPIDS_DEBUG
147         qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
148 #endif
149
150         return handle;
151 }
152
153
154 void DownloadView::removeItem(QTorrentHandle handle)
155 {
156 }
157
158
159 void DownloadView::setRefreshInterval(int msec)
160 {
161         timer_->setInterval(msec);
162 }
163
164
165 void DownloadView::on_itemClicked(QTreeWidgetItem * item, int column)
166 {
167         /*
168         qDebug() << "DownloadView::on_itemClicked(()" << item << "," << column;
169         qDebug() << "current item" << currentItem();
170         
171         if (item == currentItem() && item->isSelected()) {
172                 item->setSelected(false);
173         }
174         */
175 }
176
177 void DownloadView::on_timeout() {
178 #ifdef QTRAPIDS_DEBUG
179         qDebug() << "DownloadView::on_timeout()";
180 #endif
181         UpdateView();
182 }
183
184
185
186 QString DownloadView::GetStatusString(QTorrentHandle::State const& status) const
187 {
188         switch (status) {
189                 case QTorrentHandle::QUEUED_FOR_CHECKING :
190                         return tr("Queued");
191                 case QTorrentHandle::CHECKING_FILES :
192                         return tr("Checking");
193                 case QTorrentHandle::DOWNLOADING_METADATA :
194                         return tr("DL meta");
195                 case QTorrentHandle::DOWNLOADING :
196                         return tr("Downloading");
197                 case QTorrentHandle::FINISHED :
198                         return tr("Finished");
199                 case QTorrentHandle::SEEDING :
200                         return tr("Seeding"); 
201                 case QTorrentHandle::ALLOCATING :
202                         return tr("Allocating");
203                 default:
204                         return tr("N/A");
205         }
206 }
207
208
209 QColor DownloadView::GetStatusColor(QTorrentHandle::State const& status) const
210 {
211         QColor green(40,205,40);
212         QColor yellow(255,174,0);
213
214         switch (status) {
215                 case QTorrentHandle::QUEUED_FOR_CHECKING :
216                 case QTorrentHandle::CHECKING_FILES :
217                 case QTorrentHandle::DOWNLOADING_METADATA :
218                 case QTorrentHandle::ALLOCATING :
219                         return yellow;
220                 case QTorrentHandle::DOWNLOADING :
221                 case QTorrentHandle::FINISHED :
222                 case QTorrentHandle::SEEDING :
223                                 return green;
224                 default:
225                         return QColor();
226         }
227 }
228
229 void DownloadView::UpdateView()
230 {
231         DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
232         
233         std::map<QTorrentHandle, DownloadViewItem*>::iterator listIter
234                         = items_.begin();
235         std::map<QTorrentHandle, DownloadViewItem*>::iterator listEnd
236                         = items_.end();
237         
238         while (listIter != listEnd) {
239                 updateItem(listIter->first);
240                 ++listIter;
241         }
242 }