File indexing completed on 2024-02-18 04:58:17

0001 /***************************************************************************
0002  *   Copyright (C) 2011 Matthias Fuchs <mat69@gmx.net>                     *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0018  ***************************************************************************/
0019 
0020 #include "mostlocalurl.h"
0021 #include "kget.h"
0022 #include "plugin/transferfactory.h"
0023 
0024 #include "kget_debug.h"
0025 #include <KIO/StatJob>
0026 #include <QDebug>
0027 
0028 QUrl mostLocalUrl(const QUrl &url)
0029 {
0030     qCDebug(KGET_DEBUG);
0031     if (!url.isValid()) {
0032         qCDebug(KGET_DEBUG) << "Invalid URL: " << qUtf8Printable(url.toString());
0033         return url;
0034     }
0035 
0036     const QString protocol = url.scheme();
0037     if (protocol.isEmpty())
0038         return url;
0039 
0040     foreach (TransferFactory *factory, KGet::factories()) {
0041         if (factory->addsProtocols().contains(protocol)) {
0042             return url;
0043         }
0044     }
0045 
0046     qCDebug(KGET_DEBUG) << "Trying to find the most local URL for:" << url;
0047     KIO::StatJob *job = KIO::mostLocalUrl(url, KIO::HideProgressInfo);
0048     if (job->exec()) {
0049         return job->mostLocalUrl();
0050     };
0051     return url;
0052 }
0053 
0054 MostLocalUrlJob *mostLocalUrlJob(const QUrl &url)
0055 {
0056     return new MostLocalUrlJob(url);
0057 }
0058 
0059 MostLocalUrlJob::MostLocalUrlJob(const QUrl &url)
0060     : KIO::Job()
0061     , m_url(url)
0062 {
0063 }
0064 
0065 QUrl MostLocalUrlJob::url()
0066 {
0067     return m_url;
0068 }
0069 
0070 QUrl MostLocalUrlJob::mostLocalUrl() const
0071 {
0072     return m_mostLocalUrl;
0073 }
0074 
0075 void MostLocalUrlJob::start()
0076 {
0077     bool startJob = true;
0078     const QString protocol = m_url.scheme();
0079     foreach (TransferFactory *factory, KGet::factories()) {
0080         if (factory->addsProtocols().contains(protocol)) {
0081             startJob = false;
0082             break;
0083         }
0084     }
0085 
0086     if (startJob) {
0087         qCDebug(KGET_DEBUG) << "Starting KIO::mostLocalUrl for:" << m_url;
0088         KIO::Job *job = KIO::mostLocalUrl(m_url, KIO::HideProgressInfo);
0089         addSubjob(job);
0090     } else {
0091         m_mostLocalUrl = m_url;
0092         emitResult();
0093     }
0094 }
0095 
0096 void MostLocalUrlJob::slotResult(KJob *job)
0097 {
0098     if (job->error()) {
0099         qCWarning(KGET_DEBUG) << "Error" << job->error() << "happened for:" << m_url;
0100         m_mostLocalUrl = m_url;
0101     } else {
0102         m_mostLocalUrl = static_cast<KIO::StatJob *>(job)->mostLocalUrl();
0103     }
0104     qCDebug(KGET_DEBUG) << "Setting mostLocalUrl to" << m_mostLocalUrl;
0105     emitResult();
0106 }
0107 
0108 #include "moc_mostlocalurl.cpp"