More crazy-fixes
[kdenetwork.git] / kget / transfer-plugins / bittorrent / scandlg.cpp
blobc21d5dd1c8a98dbd1a338954a7e82601eb725669
1 /***************************************************************************
2 * Copyright (C) 2007 by Joris Guisson and Ivan Vasic *
3 * joris.guisson@gmail.com *
4 * ivasic@gmail.com *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 ***************************************************************************/
21 #include "scandlg.h"
22 #include <QCloseEvent>
23 #include <klocale.h>
24 #include <kmessagebox.h>
25 #include <kstandardguiitem.h>
26 #include <util/error.h>
27 #include <util/log.h>
28 #include <interfaces/torrentinterface.h>
30 using namespace bt;
32 namespace kt
34 ScanDlg::ScanDlg(bool auto_import,QWidget* parent)
35 : QDialog(parent),bt::DataCheckerListener(auto_import),mutex(QMutex::Recursive)
37 setupUi(this);
38 m_cancel->setGuiItem(KStandardGuiItem::cancel());
39 connect(m_cancel,SIGNAL(clicked()),this,SLOT(onCancelPressed()));
40 connect(&timer,SIGNAL(timeout()),this,SLOT(update()));
41 tc = 0;
42 silently = false;
43 restart = false;
44 qm_controlled = false;
45 scanning = false;
46 num_chunks = 0;
47 total_chunks = 0;
48 num_downloaded = 0;
49 num_failed = 0;
50 m_progress->setMaximum(100);
51 m_progress->setValue(0);
54 ScanDlg::~ScanDlg()
58 void ScanDlg::scan()
60 try
62 tc->startDataCheck(this);
63 timer.start(500);
64 scanning = true;
66 catch (bt::Error & err)
68 KMessageBox::error(0,i18n("Error scanning data: %1",err.toString()));
72 void ScanDlg::execute(bt::TorrentInterface* tc,bool silently)
74 m_torrent_label->setText(i18n("Scanning data of <b>%1</b> :",tc->getStats().torrent_name));
75 adjustSize();
76 m_cancel->setEnabled(true);
77 this->silently = silently;
78 this->tc = tc;
79 num_chunks = 0;
80 total_chunks = 0;
81 num_downloaded = 0;
82 num_failed = 0;
83 if (auto_import || tc->getStats().running)
84 restart = true;
86 qm_controlled = !tc->getStats().user_controlled;
87 qm_priority = tc->getPriority();
89 if (tc->getStats().running)
91 tc->stop(true);
94 scan();
97 void ScanDlg::progress(bt::Uint32 num,bt::Uint32 total)
99 QMutexLocker lock(&mutex);
100 num_chunks = num;
101 total_chunks = total;
104 void ScanDlg::status(bt::Uint32 failed,bt::Uint32 downloaded)
106 QMutexLocker lock(&mutex);
107 num_failed = failed;
108 num_downloaded = downloaded;
111 void ScanDlg::finished()
113 QMutexLocker lock(&mutex);
114 scanning = false;
115 timer.stop();
116 progress(100,100);
117 update();
118 if (!isStopped())
120 if (restart)
122 tc->start();
125 if (silently)
126 accept();
127 else
129 // cancel now becomes a close button
130 m_cancel->setGuiItem(KStandardGuiItem::close());
131 disconnect(m_cancel,SIGNAL(clicked()),this,SLOT(onCancelPressed()));
132 connect(m_cancel,SIGNAL(clicked()),this,SLOT(accept()));
135 else
137 if (restart)
139 tc->start();
142 QDialog::reject();
146 void ScanDlg::closeEvent(QCloseEvent* )
148 if (scanning)
149 stop();
150 else
151 accept();
154 void ScanDlg::reject()
156 if (scanning)
157 stop();
158 else
160 QDialog::reject();
161 deleteLater();
165 void ScanDlg::accept()
167 QDialog::accept();
168 deleteLater();
171 void ScanDlg::onCancelPressed()
173 stop();
176 void ScanDlg::update()
178 QMutexLocker lock(&mutex);
179 m_progress->setMaximum(total_chunks);
180 m_progress->setValue(num_chunks);
181 m_chunks_found->setText(QString::number(num_downloaded));
182 m_chunks_failed->setText(QString::number(num_failed));
186 #include "scandlg.moc"