File indexing completed on 2024-05-12 05:09:25

0001 /***************************************************************************
0002     Copyright (C) 2006-2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "netaccess.h"
0026 #include "tellico_strings.h"
0027 #include "../utils/guiproxy.h"
0028 #include "../tellico_debug.h"
0029 
0030 #include <KIO/PreviewJob>
0031 #include <KIO/StatJob>
0032 #include <KIO/JobUiDelegate>
0033 #include <KJobWidgets>
0034 #include <KLocalizedString>
0035 
0036 #include <QUrl>
0037 #include <QFileInfo>
0038 #include <QTemporaryFile>
0039 
0040 static QStringList* tmpfiles = nullptr;
0041 
0042 QString Tellico::NetAccess::s_lastErrorMessage;
0043 
0044 using Tellico::NetAccess;
0045 
0046 bool NetAccess::download(const QUrl& url_, QString& target_, QWidget* window_, bool quiet_) {
0047   // copied from KIO::NetAccess::download() apidox except for quiet part
0048   if(url_.isLocalFile()) {
0049     target_ = url_.toLocalFile();
0050     const bool readable = QFileInfo(target_).isReadable();
0051     if(!readable) {
0052       s_lastErrorMessage = i18n(errorOpen, target_);
0053     }
0054     return readable;
0055   }
0056 
0057   Q_ASSERT(target_.isEmpty());
0058   if(target_.isEmpty()) {
0059     QTemporaryFile tmpFile;
0060     tmpFile.setAutoRemove(false);
0061     tmpFile.open();
0062     target_ = tmpFile.fileName();
0063     if(!tmpfiles) {
0064       tmpfiles = new QStringList();
0065     }
0066     tmpfiles->append(target_);
0067   }
0068 
0069   KIO::JobFlags flags = KIO::Overwrite;
0070   if(quiet_ || !window_) {
0071     flags |= KIO::HideProgressInfo;
0072   }
0073 
0074   // KIO::storedGet seems to handle Content-Encoding: gzip ok
0075   KIO::StoredTransferJob* getJob = KIO::storedGet(url_, KIO::NoReload, flags);
0076   KJobWidgets::setWindow(getJob, window_);
0077   if(getJob->exec()) {
0078     QFile f(target_);
0079     if(f.open(QIODevice::WriteOnly)) {
0080       if(f.write(getJob->data()) > -1) {
0081         return true;
0082       } else {
0083         s_lastErrorMessage = i18n(errorWrite, target_);
0084         myWarning() << "failed to write to" << target_;
0085       }
0086     } else {
0087       s_lastErrorMessage = i18n(errorOpen, target_);
0088     }
0089   } else {
0090     s_lastErrorMessage = QStringLiteral("Tellico was unable to download %1").arg(url_.url());
0091     myWarning() << getJob->errorString();
0092   }
0093 
0094   if(!quiet_ && getJob->uiDelegate()) {
0095     getJob->uiDelegate()->showErrorMessage();
0096   }
0097   return false;
0098 }
0099 
0100 QPixmap NetAccess::filePreview(const QUrl& url, int size) {
0101   return filePreview(KFileItem(url), size);
0102 }
0103 
0104 QPixmap NetAccess::filePreview(const KFileItem& item, int size) {
0105   NetAccess netaccess;
0106 
0107   // the default plugins are not used by default (what???)
0108   // the default ones are in config settings instead, so ignore that
0109   const QStringList plugins = KIO::PreviewJob::defaultPlugins();
0110   KIO::PreviewJob* previewJob = KIO::filePreview(KFileItemList() << item, QSize(size, size),
0111                                                  &plugins);
0112   connect(previewJob, &KIO::PreviewJob::gotPreview,
0113           &netaccess, &Tellico::NetAccess::slotPreview);
0114 
0115   if(GUI::Proxy::widget()) {
0116     KJobWidgets::setWindow(previewJob, GUI::Proxy::widget());
0117   }
0118   if(!previewJob->exec()) {
0119     myDebug() << "Preview job did not succeed";
0120   }
0121   if(previewJob->error() != 0) {
0122     myDebug() << previewJob->errorString();
0123   }
0124   return netaccess.m_preview;
0125 }
0126 
0127 void NetAccess::slotPreview(const KFileItem&, const QPixmap& pix_) {
0128   m_preview = pix_;
0129 }
0130 
0131 void NetAccess::removeTempFile(const QString& name) {
0132   if(!tmpfiles) {
0133     return;
0134   }
0135   if(tmpfiles->contains(name)) {
0136     QFile::remove(name);
0137     tmpfiles->removeAll(name);
0138   }
0139 }
0140 
0141 bool NetAccess::exists(const QUrl& url_, bool sourceSide_, QWidget* window_) {
0142   if(url_.isLocalFile()) {
0143     return QFile::exists(url_.toLocalFile());
0144   }
0145 
0146   KIO::StatJob* job = KIO::stat(url_);
0147   KJobWidgets::setWindow(job, window_);
0148   job->setSide(sourceSide_ ? KIO::StatJob::SourceSide : KIO::StatJob::DestinationSide);
0149   return job->exec();
0150 }
0151 
0152 QString NetAccess::lastErrorString() {
0153   return s_lastErrorMessage;
0154 }