~ubuntu-branches/ubuntu/oneiric/libktorrent/oneiric

« back to all changes in this revision

Viewing changes to src/datachecker/datacheckerjob.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Modestas Vainius
  • Date: 2011-05-26 00:33:38 UTC
  • mfrom: (5.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110526003338-2r4zwyzn8bovsxay
Tags: 1.1.1-2
Release to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 ***************************************************************************/
20
20
 
21
21
#include "datacheckerjob.h"
22
 
#include "datacheckerlistener.h"
23
22
#include "datacheckerthread.h"
24
23
#include "multidatachecker.h"
25
24
#include "singledatachecker.h"
26
25
#include <torrent/torrentcontrol.h>
27
26
#include <util/functions.h>
 
27
#include <KLocale>
28
28
 
29
29
namespace bt
30
30
{
 
31
        static ResourceManager data_checker_slot(1);
31
32
        
32
 
        DataCheckerJob::DataCheckerJob(bt::DataCheckerListener* lst, bt::TorrentControl* tc): Job(true,tc),listener(lst)
 
33
        DataCheckerJob::DataCheckerJob(bool auto_import,bt::TorrentControl* tc)
 
34
                : Job(true,tc),
 
35
                Resource(&data_checker_slot,tc->getInfoHash().toString()),
 
36
                dcheck_thread(0),
 
37
                killed(false),
 
38
                auto_import(auto_import),
 
39
                started(false)
33
40
        {
34
 
                dcheck_thread = 0;
35
 
                killed = false;
36
41
        }
37
42
        
38
43
        
42
47
 
43
48
        void DataCheckerJob::start()
44
49
        {
 
50
                registerWithTracker();
45
51
                DataChecker* dc = 0;
46
52
                const TorrentStats & stats = torrent()->getStats();
47
53
                if (stats.multi_file_torrent)
49
55
                else
50
56
                        dc = new SingleDataChecker();
51
57
                
52
 
                dc->setListener(listener);
 
58
                connect(dc,SIGNAL(progress(quint32,quint32)),
 
59
                                this,SLOT(progress(quint32,quint32)),Qt::QueuedConnection);
 
60
                connect(dc,SIGNAL(status(quint32,quint32,quint32,quint32)),
 
61
                                this,SLOT(status(quint32,quint32,quint32,quint32)),Qt::QueuedConnection);
 
62
                
53
63
                TorrentControl* tor = torrent();
54
64
                dcheck_thread = new DataCheckerThread(
55
65
                                dc,tor->downloadedChunksBitSet(),
56
66
                                stats.output_path,tor->getTorrent(),
57
67
                                tor->getTorDir() + "dnd" + bt::DirSeparator());
58
68
                                
59
 
                connect(dcheck_thread,SIGNAL(finished()),this,SLOT(finished()),Qt::QueuedConnection);
 
69
                connect(dcheck_thread,SIGNAL(finished()),this,SLOT(threadFinished()),Qt::QueuedConnection);
60
70
                
61
71
                torrent()->beforeDataCheck();
62
72
                
63
 
                // dc->check(stats.output_path,*tor,tordir + "dnd" + bt::DirSeparator());
 
73
                setTotalAmount(Bytes,stats.total_chunks);
 
74
                data_checker_slot.add(this);
 
75
                if (!started)
 
76
                        infoMessage(this,i18n("Waiting for other data checks to finish"));
 
77
        }
 
78
        
 
79
        void DataCheckerJob::acquired()
 
80
        {
 
81
                started = true;
 
82
                description(this,i18n("Checking data"));
64
83
                dcheck_thread->start(QThread::IdlePriority);
65
84
        }
66
85
 
69
88
                killed = true;
70
89
                if (dcheck_thread && dcheck_thread->isRunning())
71
90
                {
72
 
                        listener->stop();
 
91
                        dcheck_thread->getDataChecker()->stop();
73
92
                        dcheck_thread->wait();
74
93
                        dcheck_thread->deleteLater();
75
94
                        dcheck_thread = 0;
78
97
        }
79
98
        
80
99
        
81
 
        void DataCheckerJob::finished()
 
100
        void DataCheckerJob::threadFinished()
82
101
        {
83
102
                if (!killed)
84
103
                {
85
104
                        DataChecker* dc = dcheck_thread->getDataChecker();
86
 
                        torrent()->afterDataCheck(listener,dc->getResult(),dcheck_thread->getError());
 
105
                        torrent()->afterDataCheck(this,dc->getResult());
 
106
                        if (!dcheck_thread->getError().isEmpty())
 
107
                        {
 
108
                                setErrorText(dcheck_thread->getError());
 
109
                                setError(KIO::ERR_UNKNOWN);
 
110
                        }
 
111
                        else
 
112
                                setError(0);
87
113
                }
 
114
                else
 
115
                        setError(0);
 
116
                
88
117
                dcheck_thread->deleteLater();
89
118
                dcheck_thread = 0;
90
 
                setError(0);
91
 
                emitResult();
92
 
        }
93
 
 
94
 
 
 
119
                if (!killed) // Job::kill already emitted the result
 
120
                        emitResult();
 
121
                
 
122
                release();
 
123
        }
 
124
        
 
125
        void DataCheckerJob::progress(quint32 num, quint32 total)
 
126
        {
 
127
                Q_UNUSED(total);
 
128
                setProcessedAmount(Bytes,num);
 
129
        }
 
130
        
 
131
        void DataCheckerJob::status(quint32 num_failed, quint32 num_found, quint32 num_downloaded, quint32 num_not_downloaded)
 
132
        {
 
133
                QPair<QString,QString> field1 = qMakePair(QString::number(num_failed),QString::number(num_found));
 
134
                QPair<QString,QString> field2 = qMakePair(QString::number(num_downloaded),QString::number(num_not_downloaded));
 
135
                description(this,i18n("Checking Data"),field1,field2);
 
136
        }
95
137
}
 
 
b'\\ No newline at end of file'