Code formatting/indentation unified in trunk
[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 {
35     setRootIsDecorated(false); // Hide branch lines, making one-level treeview (similar to list)
36     setHeaderItem(DownloadViewItem::getHeaderItem());
37
38     connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)),
39             this, SLOT(on_itemClicked(QTreeWidgetItem*, int)));
40
41 }
42
43
44 DownloadView::~DownloadView()
45 {
46 }
47
48 void DownloadView::updateItem(TorrentState const& info, ParamsMap_t other_info)
49 {
50     DownloadItems_t::iterator p = items_.find(info.hash);
51     switch (info.action)
52     {
53     case TorrentState::action_add :
54         if (p == items_.end())
55         {
56             addItem_(info, other_info);
57         }
58         else
59         {
60             qWarning() << "item with similar info hash marked as added";
61             updateItem_(p.value(), info, other_info);
62         }
63         break;
64     case TorrentState::action_update :
65         if (p != items_.end())
66         {
67             updateItem_(p.value(), info, other_info);
68         }
69         else
70         {
71             qWarning() << "item does not exist in list but information update arrived";
72         }
73         break;
74     case TorrentState::action_remove :
75         if (p != items_.end())
76         {
77             removeItem_(p.value(), info);
78         }
79         else
80         {
81             qWarning() << "item removal request arrived but there is no such item";
82         }
83         break;
84     default:
85         qWarning() << "unknown action requested: " << info.action;
86         break;
87     }
88 }
89
90 void DownloadView::removeItem_(DownloadViewItem *item, TorrentState const& info)
91 {
92     QString hash = item->getHash();
93
94     int removed = items_.remove(hash);
95     if (!removed)
96         qDebug() << "Inconsistent download view state on item removal";
97
98     int index = indexOfTopLevelItem(item);
99     if (index >= 0)
100     {
101         takeTopLevelItem(index);
102     }
103 }
104
105 void DownloadView::addItem_(TorrentState const& info, ParamsMap_t)
106 {
107     DownloadViewItem *item = new DownloadViewItem
108     ( info.hash,
109       QStringList()
110       << info.name
111       << formatSize(info.total_size)
112       << GetStatusString((TorrentStatus::Id)info.state)
113       << formatProgress(info.progress)
114       << QString::number(info.down_rate, 'f', 2)
115       << QString::number(info.up_rate, 'f', 2)
116       << QString::number(info.seeds) + "/" + QString::number(info.leeches)
117       << QString::number(info.ratio)
118       << "ETA" );
119
120     QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state));
121     item->setForeground(2, brushTmp);
122
123     addTopLevelItem(item);
124     items_[info.hash] = item;
125 }
126
127
128 void DownloadView::updateItem_(DownloadViewItem *item
129                                , TorrentState const& info, ParamsMap_t)
130 {
131     item->setData(2, Qt::DisplayRole,
132                   QVariant(GetStatusString((TorrentStatus::Id)info.state)));
133     item->setData(3, Qt::DisplayRole,
134                   QVariant(formatProgress(info.progress)));
135     item->setData(4, Qt::DisplayRole,
136                   QVariant(QString::number(info.down_rate)));
137     item->setData(5, Qt::DisplayRole,
138                   QVariant(QString::number(info.up_rate)));
139     item->setData(6, Qt::DisplayRole,
140                   QString::number(info.seeds) + "/" + QString::number(info.leeches));
141
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 QString DownloadView::GetStatusString(TorrentStatus::Id status)
176 {
177     switch (status)
178     {
179     case TorrentStatus::QUEUED_FOR_CHECKING :
180         return tr("Queued");
181     case TorrentStatus::CHECKING_FILES :
182         return tr("Checking");
183     case TorrentStatus::DOWNLOADING_METADATA :
184         return tr("DL meta");
185     case TorrentStatus::DOWNLOADING :
186         return tr("Downloading");
187     case TorrentStatus::FINISHED :
188         return tr("Finished");
189     case TorrentStatus::SEEDING :
190         return tr("Seeding");
191     case TorrentStatus::ALLOCATING :
192         return tr("Allocating");
193     case TorrentStatus::CHECKING_RESUME_DATA :
194         return tr("Checking resume");
195     default:
196         return tr("N/A");
197     }
198 }
199
200
201 QColor DownloadView::GetStatusColor(TorrentStatus::Id status)
202 {
203     QColor green(40,205,40);
204     QColor yellow(255,174,0);
205
206     switch (status)
207     {
208     case TorrentStatus::QUEUED_FOR_CHECKING :
209     case TorrentStatus::CHECKING_FILES :
210     case TorrentStatus::DOWNLOADING_METADATA :
211     case TorrentStatus::ALLOCATING :
212     case TorrentStatus::CHECKING_RESUME_DATA:
213         return yellow;
214     case TorrentStatus::DOWNLOADING :
215     case TorrentStatus::FINISHED :
216     case TorrentStatus::SEEDING :
217         return green;
218     default:
219         return QColor();
220     }
221 }
222
223 } // namespace qtrapids