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