• 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
  • transfer-plugins
  • checksumsearch
checksumsearch.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 * Copyright (C) 2009 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 "checksumsearch.h"
21 
22 #include "core/verifier.h"
23 
24 #include <QFile>
25 #include <QFileInfo>
26 
27 #include <KDebug>
28 #include <KLocale>
29 
30 const QStringList ChecksumSearch::URLCHANGEMODES = (QStringList() << i18n("Append") << i18n("Replace file") << i18n("Replace file-ending"));
31 
32 ChecksumSearch::ChecksumSearch(const QList<KUrl> &srcs, const QString &fileName, const QStringList &types, QObject *parent)
33  : QObject(parent),
34  m_copyJob(0),
35  m_srcs(srcs),
36  m_fileName(fileName),
37  m_types(types)
38 {
39  createDownload();
40 }
41 
42 ChecksumSearch::~ChecksumSearch()
43 {
44  if (m_copyJob)
45  {
46  m_copyJob->kill(KJob::Quietly);
47  }
48 }
49 
50 void ChecksumSearch::createDownload()
51 {
52  if (m_srcs.isEmpty() || m_types.isEmpty()) {
53  deleteLater();
54  } else {
55  m_src = m_srcs.takeFirst();
56  m_type = m_types.takeFirst();
57  m_isEmpty = m_type.isEmpty();
58 
59  m_copyJob = KIO::get(m_src, KIO::Reload, KIO::HideProgressInfo);
60  m_copyJob->addMetaData("errorPage", "false");
61  connect(m_copyJob, SIGNAL(data(KIO::Job*,QByteArray)), SLOT(slotData(KIO::Job*,QByteArray)));
62  connect(m_copyJob, SIGNAL(result(KJob*)), SLOT(slotResult(KJob*)));
63  }
64 }
65 
66 void ChecksumSearch::slotData(KIO::Job *job, const QByteArray &data)
67 {
68  Q_UNUSED(job)
69 
70  if (m_dataBA.size() > 5 * 1024) {
71  m_copyJob->kill(KJob::EmitResult);
72  } else {
73  m_dataBA.append(data);
74  }
75 }
76 
77 void ChecksumSearch::slotResult(KJob *job)
78 {
79  kDebug(5001);
80 
81  m_data.clear();
82 
83  switch (job->error())
84  {
85  case 0://The download has finished
86  {
87  kDebug(5001) << "Correctly downloaded" << m_src.pathOrUrl();
88  m_data = QString(m_dataBA);
89  break;
90  }
91 
92  default:
93  kDebug(5001) << "There was error" << job->error() << "while downloading" << m_src.pathOrUrl();
94  break;
95  }
96 
97  m_copyJob = 0;
98  m_dataBA.clear();
99 
100  parseDownload();
101 }
102 
103 void ChecksumSearch::parseDownload()
104 {
105  if (!m_data.isEmpty()) {
106  kDebug(5001) << "*******Parse*******\n" << m_data << "*******************";
107  }
108 
109  //no type has been specified
110  if (m_type.isEmpty()) {
111  parseDownloadEmpty();
112  return;
113  }
114 
115  const int length = Verifier::diggestLength(m_type);
116 
117  const QString patternChecksum = QString("\\w{%1}").arg(length);
118  QRegExp rxChecksum(patternChecksum);
119  QString hash;
120 
121  //find the correct line
122  const QStringList lines = m_data.split('\n');
123  foreach (const QString &line, lines) {
124  if (line.contains(m_fileName, Qt::CaseInsensitive)) {
125  if (rxChecksum.indexIn(line) > -1) {
126  hash = rxChecksum.cap(0).toLower();
127  if (!m_fileName.contains(hash, Qt::CaseInsensitive)) {
128  kDebug(5001) << "Found hash: " << hash;
129  emit data(m_type, hash);
130  }
131  }
132  }
133  }
134 
135  //nothing found yet, so simply search for a word in the whole data that has the correct length
136  if (hash.isEmpty() && (rxChecksum.indexIn(m_data) > -1)) {
137  QString hash = rxChecksum.cap(0);
138  if (!m_fileName.contains(hash, Qt::CaseInsensitive)) {
139  kDebug(5001) << "Found hash:" << hash;
140  emit data(m_type, hash);
141  }
142  }
143 
144  //only create a download here if type was specified, otherwise parseDownloadEmpty has to handle this
145  if (!m_isEmpty) {
146  createDownload();
147  }
148 }
149 
150 void ChecksumSearch::parseDownloadEmpty()
151 {
152  const QStringList lines = m_data.split('\n');
153  const QStringList supportedTypes = Verifier::supportedVerficationTypes();
154  foreach (const QString &type, supportedTypes)
155  {
156  if (m_data.contains(type, Qt::CaseInsensitive))
157  {
158  m_type = type;
159  parseDownload();
160  }
161  }
162 
163  createDownload();
164 }
165 
166 KUrl ChecksumSearch::createUrl(const KUrl &src, const QString &change, ChecksumSearch::UrlChangeMode mode)
167 {
168  if (!src.isValid() || change.isEmpty())
169  {
170  return KUrl();
171  }
172 
173  KUrl url;
174  if (mode == kg_Append)
175  {
176  url = KUrl(src.pathOrUrl() + change);
177  }
178  else if (mode == kg_ReplaceFile)
179  {
180  KUrl temp = src.upUrl();
181  temp.addPath(change);
182  url = temp;
183  }
184  else if (mode == kg_ReplaceEnding)
185  {
186  QString fileName = src.fileName();
187  int index = fileName.lastIndexOf('.');
188  if (index > -1)
189  {
190  fileName = fileName.left(index) + change;
191  KUrl temp = src.upUrl();
192  temp.addPath(fileName);
193  url = temp;
194  }
195  }
196 
197  return url;
198 }
199 
200 #include "checksumsearch.moc"
ChecksumSearch::createUrl
static KUrl createUrl(const KUrl &src, const QString &change, UrlChangeMode mode)
Returns a modified url according to the parameters.
Definition: checksumsearch.cpp:166
ChecksumSearch::kg_ReplaceEnding
Replaces the file of the Url with QString –> "http://test.com/aFile.zip"; "MD5SUMS" "http://test...
Definition: checksumsearch.h:44
ChecksumSearch::UrlChangeMode
UrlChangeMode
Used to define in whiche way the url should be changed to try and find Checksums. ...
Definition: checksumsearch.h:40
checksumsearch.h
Verifier::diggestLength
static int diggestLength(const QString &type)
Returns the diggest length of type.
Definition: verifier.cpp:239
Verifier::supportedVerficationTypes
static QStringList supportedVerficationTypes()
Returns the supported verification types.
Definition: verifier.cpp:216
QObject
ChecksumSearch::kg_Append
Definition: checksumsearch.h:42
ChecksumSearch::data
void data(QString type, QString checksum)
ChecksumSearch::~ChecksumSearch
~ChecksumSearch()
Definition: checksumsearch.cpp:42
ChecksumSearch::ChecksumSearch
ChecksumSearch(const QList< KUrl > &srcs, const QString &fileName, const QStringList &types, QObject *parent=0)
Definition: checksumsearch.cpp:32
verifier.h
KJob
ChecksumSearch::kg_ReplaceFile
Appends the QString to the Url –> "http://test.com/aFile.zip"; ".md5" "http://test.com/aFile.zip.md5".
Definition: checksumsearch.h:43
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:17 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