File indexing completed on 2024-04-28 04:57:32
0001 /* This file is part of the KDE project 0002 0003 Copyright (C) 2006 Manolo Valdes <nolis71cu@gmail.com> 0004 0005 This program is free software; you can redistribute it and/or 0006 modify it under the terms of the GNU General Public 0007 License as published by the Free Software Foundation; either 0008 version 2 of the License, or (at your option) any later version. 0009 */ 0010 0011 #include "mirrors.h" 0012 #include "mirrorsearchsettings.h" 0013 0014 #include "kget_debug.h" 0015 #include <QDebug> 0016 0017 mirror::mirror() 0018 { 0019 if (!MirrorSearchSettings::searchEnginesUrlList().isEmpty()) 0020 m_search_engine = MirrorSearchSettings::searchEnginesUrlList().takeFirst(); 0021 } 0022 0023 void mirror::search(const QUrl &url, QObject *receiver, const char *member) 0024 { 0025 qCDebug(KGET_DEBUG); 0026 0027 m_url = url; 0028 if (m_url.path() != m_url.fileName()) { 0029 m_Urls << m_url; 0030 } 0031 0032 search(m_url.fileName(), receiver, member); 0033 } 0034 0035 void mirror::search(const QString &fileName, QObject *receiver, const char *member) 0036 { 0037 qCDebug(KGET_DEBUG); 0038 0039 QUrl search(m_search_engine.replace("${filename}", fileName)); 0040 m_job = KIO::get(search, KIO::NoReload, KIO::HideProgressInfo); 0041 connect(m_job, &KIO::TransferJob::data, this, &mirror::slotData); 0042 connect(m_job, &KJob::result, this, &mirror::slotResult); 0043 connect(this, SIGNAL(urls(QList<QUrl> &)), receiver, member); 0044 } 0045 0046 void mirror::slotData(KIO::Job *, const QByteArray &data) 0047 { 0048 qCDebug(KGET_DEBUG); 0049 if (data.size() == 0) 0050 return; 0051 m_data.append(data); 0052 } 0053 0054 void mirror::slotResult(KJob *job) 0055 { 0056 qCDebug(KGET_DEBUG); 0057 m_job = nullptr; 0058 int minUrlsNeeded = static_cast<int>(!m_Urls.isEmpty()); 0059 0060 if (job->error()) { 0061 deleteLater(); 0062 return; 0063 } 0064 QString str(m_data); 0065 0066 int start = 0, posOfTagA = 0, posOfTagHref = 0, hrefEnd = 0; 0067 0068 while ((posOfTagA = str.indexOf("<a ", start, Qt::CaseInsensitive)) != -1) { 0069 posOfTagHref = str.indexOf("href=\"", posOfTagA, Qt::CaseInsensitive); 0070 hrefEnd = str.indexOf("\"", posOfTagHref + 6, Qt::CaseInsensitive); 0071 QString u = str.mid(posOfTagHref + 6, (hrefEnd - posOfTagHref - 6)); 0072 0073 start = hrefEnd + 1; 0074 if (u.endsWith('/' + m_url.fileName())) { 0075 m_Urls << QUrl(u); 0076 qCDebug(KGET_DEBUG) << "url: " << u; 0077 } 0078 } 0079 0080 if (m_Urls.size() > minUrlsNeeded) 0081 Q_EMIT urls(m_Urls); 0082 deleteLater(); 0083 } 0084 0085 void MirrorSearch(const QUrl &url, QObject *receiver, const char *member) 0086 { 0087 auto *searcher = new mirror(); 0088 searcher->search(url, receiver, member); 0089 } 0090 0091 void MirrorSearch(const QString &fileName, QObject *receiver, const char *member) 0092 { 0093 auto *searcher = new mirror(); 0094 searcher->search(fileName, receiver, member); 0095 } 0096 0097 #include "moc_mirrors.cpp"