is_new attribute added to the torrent state information
[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         if (p != items_.end()) {
52             if (info.is_new) {
53                 qWarning() << "item with similar info hash marked as new";
54             }
55             updateItem_(p.value(), info, other_info);
56         } else {
57             if (!info.is_new) {
58                 qDebug() << "torrent info arrived but there is no torrent for it";
59                 return;
60             }
61             addItem_(info, other_info);
62         }
63     }
64
65     void DownloadView::addItem_(TorrentState const& info, ParamsMap_t)
66     {
67         DownloadViewItem *item = new DownloadViewItem
68             ( info.hash, 
69               QStringList()
70               << info.name
71               << formatSize(info.total_size)
72               << GetStatusString((TorrentStatus::Id)info.state)
73               << formatProgress(info.progress)
74               << QString::number(info.down_rate, 'f', 2)
75               << QString::number(info.up_rate, 'f', 2)
76               << QString::number(info.seeds) + "/" + QString::number(info.leeches)
77               << QString::number(info.ratio)
78               << "ETA" );
79
80         QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state));
81         item->setForeground(2, brushTmp);
82
83         addTopLevelItem(item);
84         items_[info.hash] = item;
85     }
86
87
88     void DownloadView::updateItem_(DownloadViewItem *item
89                                    , TorrentState const& info, ParamsMap_t)
90     {
91         item->setData(2, Qt::DisplayRole,
92                       QVariant(GetStatusString((TorrentStatus::Id)info.state)));
93         item->setData(3, Qt::DisplayRole,
94                       QVariant(formatProgress(info.progress)));
95         item->setData(4, Qt::DisplayRole,
96                       QVariant(QString::number(info.down_rate)));
97         item->setData(5, Qt::DisplayRole,
98                       QVariant(QString::number(info.up_rate)));
99         item->setData(6, Qt::DisplayRole,
100                       QString::number(info.seeds) + "/" + QString::number(info.leeches));
101     
102         QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state));
103         item->setForeground(2, brushTmp);
104     }
105
106
107     QString DownloadView::removeSelected()
108     {
109         qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
110
111         DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
112         QString hash = item->getHash();
113
114         int removed = items_.remove(hash);
115         if (!removed)
116             qDebug() << "Inconsistent download view state on item removal";
117
118         int index = indexOfTopLevelItem(currentItem());
119         if (index >= 0) {
120             takeTopLevelItem(index);
121         }
122
123         qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
124
125         return hash;
126     }
127
128
129     void DownloadView::on_itemClicked(QTreeWidgetItem * , int)
130     {
131         /*
132           qDebug() << "DownloadView::on_itemClicked(()" << item << "," << column;
133           qDebug() << "current item" << currentItem();
134
135           if (item == currentItem() && item->isSelected()) {
136           item->setSelected(false);
137           }
138         */
139     }
140
141
142     QString DownloadView::GetStatusString(TorrentStatus::Id status)
143     {
144         switch (status) {
145             case TorrentStatus::QUEUED_FOR_CHECKING :
146                 return tr("Queued");
147             case TorrentStatus::CHECKING_FILES :
148                 return tr("Checking");
149             case TorrentStatus::DOWNLOADING_METADATA :
150                 return tr("DL meta");
151             case TorrentStatus::DOWNLOADING :
152                 return tr("Downloading");
153             case TorrentStatus::FINISHED :
154                 return tr("Finished");
155             case TorrentStatus::SEEDING :
156                 return tr("Seeding");
157             case TorrentStatus::ALLOCATING :
158                 return tr("Allocating");
159             case TorrentStatus::CHECKING_RESUME_DATA :
160                 return tr("Checking resume");
161             default:
162                 return tr("N/A");
163         }
164     }
165
166
167     QColor DownloadView::GetStatusColor(TorrentStatus::Id status)
168     {
169         QColor green(40,205,40);
170         QColor yellow(255,174,0);
171
172         switch (status) {
173             case TorrentStatus::QUEUED_FOR_CHECKING :
174             case TorrentStatus::CHECKING_FILES :
175             case TorrentStatus::DOWNLOADING_METADATA :
176             case TorrentStatus::ALLOCATING :
177             case TorrentStatus::CHECKING_RESUME_DATA:
178                 return yellow;
179             case TorrentStatus::DOWNLOADING :
180             case TorrentStatus::FINISHED :
181             case TorrentStatus::SEEDING :
182                                 return green;
183             default:
184                 return QColor();
185         }
186     }
187
188 } // namespace qtrapids