• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kget

  • sources
  • kde-4.12
  • kdenetwork
  • kget
  • core
verificationthread.cpp
Go to the documentation of this file.
1 /**************************************************************************
2 * Copyright (C) 2009-2011 Matthias Fuchs <mat69@gmx.net> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18 ***************************************************************************/
19 
20 #include "verificationthread.h"
21 #include "verifier.h"
22 
23 #include <KDebug>
24 
25 #include <QtCore/QFile>
26 
27 VerificationThread::VerificationThread(QObject *parent)
28  : QThread(parent),
29  m_abort(false),
30  m_length(0),
31  m_type(Nothing)
32 {
33 }
34 
35 VerificationThread::~VerificationThread()
36 {
37  m_mutex.lock();
38  m_abort = true;
39  m_mutex.unlock();
40 
41  wait();
42 }
43 
44 void VerificationThread::verifiy(const QString &type, const QString &checksum, const KUrl &file)
45 {
46  QMutexLocker locker(&m_mutex);
47  m_types.append(type);
48  m_checksums.append(checksum);
49  m_files.append(file);
50  m_type = Verify;
51 
52  if (!isRunning())
53  {
54  start();
55  }
56 }
57 
58 void VerificationThread::findBrokenPieces(const QString &type, const QList<QString> checksums, KIO::filesize_t length, const KUrl &file)
59 {
60  QMutexLocker locker(&m_mutex);
61  m_types.clear();
62  m_types.append(type);
63  m_checksums = checksums;
64  m_files.clear();
65  m_files.append(file);
66  m_length = length;
67  m_type = BrokenPieces;
68 
69  if (!isRunning())
70  {
71  start();
72  }
73 }
74 
75 void VerificationThread::run()
76 {
77  if (m_type == Nothing)
78  {
79  return;
80  }
81 
82  if (m_type == Verify)
83  {
84  doVerify();
85  }
86  else if (m_type == BrokenPieces)
87  {
88  doBrokenPieces();
89  }
90 }
91 
92 void VerificationThread::doVerify()
93 {
94  m_mutex.lock();
95  bool run = m_files.count();
96  m_mutex.unlock();
97 
98  while (run && !m_abort)
99  {
100  m_mutex.lock();
101  const QString type = m_types.takeFirst();
102  const QString checksum = m_checksums.takeFirst();
103  const KUrl url = m_files.takeFirst();
104  m_mutex.unlock();
105 
106  if (type.isEmpty() || checksum.isEmpty())
107  {
108  m_mutex.lock();
109  run = m_files.count();
110  m_mutex.unlock();
111  continue;
112  }
113 
114  const QString hash = Verifier::checksum(url, type, &m_abort);
115  kDebug(5001) << "Type:" << type << "Calculated checksum:" << hash << "Entered checksum:" << checksum;
116  const bool fileVerified = (hash == checksum);
117 
118  if (m_abort)
119  {
120  return;
121  }
122 
123  m_mutex.lock();
124  if (!m_abort)
125  {
126  emit verified(type, fileVerified, url);
127  emit verified(fileVerified);
128  }
129  run = m_files.count();
130  m_mutex.unlock();
131  }
132 }
133 
134 void VerificationThread::doBrokenPieces()
135 {
136  m_mutex.lock();
137  const QString type = m_types.takeFirst();
138  const QStringList checksums = m_checksums;
139  m_checksums.clear();
140  const KUrl url = m_files.takeFirst();
141  const KIO::filesize_t length = m_length;
142  m_mutex.unlock();
143 
144  QList<KIO::fileoffset_t> broken;
145 
146  if (QFile::exists(url.pathOrUrl()))
147  {
148  QFile file(url.pathOrUrl());
149  if (!file.open(QIODevice::ReadOnly))
150  {
151  emit brokenPieces(broken, length);
152  return;
153  }
154 
155  const KIO::filesize_t fileSize = file.size();
156  if (!length || !fileSize)
157  {
158  emit brokenPieces(broken, length);
159  return;
160  }
161 
162  const QStringList fileChecksums = Verifier::partialChecksums(url, type, length, &m_abort).checksums();
163  if (m_abort)
164  {
165  emit brokenPieces(broken, length);
166  return;
167  }
168 
169  if (fileChecksums.size() != checksums.size())
170  {
171  kDebug(5001) << "Number of checksums differs!";
172  emit brokenPieces(broken, length);
173  return;
174  }
175 
176  for (int i = 0; i < checksums.size(); ++i)
177  {
178  if (fileChecksums.at(i) != checksums.at(i))
179  {
180  const int brokenStart = length * i;
181  kDebug(5001) << url << "broken segment" << i << "start" << brokenStart << "length" << length;
182  broken.append(brokenStart);
183  }
184  }
185  }
186 
187  emit brokenPieces(broken, length);
188 }
VerificationThread::verifiy
void verifiy(const QString &type, const QString &checksum, const KUrl &file)
Definition: verificationthread.cpp:44
QObject
VerificationThread::verified
void verified(bool verified)
Emitted when the verification of a file finishes, connect to this signal if you do the verification f...
VerificationThread::brokenPieces
void brokenPieces(const QList< KIO::fileoffset_t > &offsets, KIO::filesize_t length)
PartialChecksums::checksums
QStringList checksums() const
Definition: verifier.h:58
verificationthread.h
VerificationThread::findBrokenPieces
void findBrokenPieces(const QString &type, const QList< QString > checksums, KIO::filesize_t length, const KUrl &file)
Definition: verificationthread.cpp:58
verifier.h
VerificationThread::VerificationThread
VerificationThread(QObject *parent=0)
Definition: verificationthread.cpp:27
Verifier::partialChecksums
static PartialChecksums partialChecksums(const KUrl &dest, const QString &type, KIO::filesize_t length=0, bool *abortPtr=0)
Create partial checksums of type for file dest.
Definition: verifier.cpp:448
VerificationThread::~VerificationThread
~VerificationThread()
Definition: verificationthread.cpp:35
VerificationThread::run
void run()
Definition: verificationthread.cpp:75
QThread
Verifier::checksum
static QString checksum(const KUrl &dest, const QString &type, bool *abortPtr)
Creates the checksum type of the file dest.
Definition: verifier.cpp:398
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:18 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kget

Skip menu "kget"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal