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