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