1 /***************************************************************************
2     Copyright (C) 2006-2009 Robby Stephenson <robby@periapsis.org>
3  ***************************************************************************/
4 
5 /***************************************************************************
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or         *
8  *   modify it under the terms of the GNU General Public License as        *
9  *   published by the Free Software Foundation; either version 2 of        *
10  *   the License or (at your option) version 3 or any later version        *
11  *   accepted by the membership of KDE e.V. (or its successor approved     *
12  *   by the membership of KDE e.V.), which shall act as a proxy            *
13  *   defined in Section 14 of version 3 of the license.                    *
14  *                                                                         *
15  *   This program is distributed in the hope that it will be useful,       *
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  *   GNU General Public License for more details.                          *
19  *                                                                         *
20  *   You should have received a copy of the GNU General Public License     *
21  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
22  *                                                                         *
23  ***************************************************************************/
24 
25 #include "netaccess.h"
26 #include "tellico_strings.h"
27 #include "../utils/guiproxy.h"
28 #include "../tellico_debug.h"
29 
30 #include <KIO/PreviewJob>
31 #include <KIO/StatJob>
32 #include <KIO/JobUiDelegate>
33 #include <KJobWidgets>
34 #include <KLocalizedString>
35 
36 #include <QUrl>
37 #include <QFileInfo>
38 #include <QTemporaryFile>
39 
40 static QStringList* tmpfiles = nullptr;
41 
42 QString Tellico::NetAccess::s_lastErrorMessage;
43 
44 using Tellico::NetAccess;
45 
download(const QUrl & url_,QString & target_,QWidget * window_,bool quiet_)46 bool NetAccess::download(const QUrl& url_, QString& target_, QWidget* window_, bool quiet_) {
47   // copied from KIO::NetAccess::download() apidox except for quiet part
48   if(url_.isLocalFile()) {
49     target_ = url_.toLocalFile();
50     const bool readable = QFileInfo(target_).isReadable();
51     if(!readable) {
52       s_lastErrorMessage = i18n(errorOpen, target_);
53     }
54     return readable;
55   }
56 
57   Q_ASSERT(target_.isEmpty());
58   if(target_.isEmpty()) {
59     QTemporaryFile tmpFile;
60     tmpFile.setAutoRemove(false);
61     tmpFile.open();
62     target_ = tmpFile.fileName();
63     if(!tmpfiles) {
64       tmpfiles = new QStringList();
65     }
66     tmpfiles->append(target_);
67   }
68 
69   KIO::JobFlags flags = KIO::Overwrite;
70   if(quiet_ || !window_) {
71     flags |= KIO::HideProgressInfo;
72   }
73 
74   // KIO::storedGet seems to handle Content-Encoding: gzip ok
75   KIO::StoredTransferJob* getJob = KIO::storedGet(url_, KIO::NoReload, flags);
76   KJobWidgets::setWindow(getJob, window_);
77   if(getJob->exec()) {
78     QFile f(target_);
79     if(f.open(QIODevice::WriteOnly)) {
80       if(f.write(getJob->data()) > -1) {
81         return true;
82       } else {
83         s_lastErrorMessage = i18n(errorWrite, target_);
84         myWarning() << "failed to write to" << target_;
85       }
86     } else {
87       s_lastErrorMessage = i18n(errorOpen, target_);
88     }
89   } else {
90     s_lastErrorMessage = QStringLiteral("Tellico was unable to download %1").arg(url_.url());
91     myWarning() << getJob->errorString();
92   }
93 
94   if(!quiet_ && getJob->uiDelegate()) {
95     getJob->uiDelegate()->showErrorMessage();
96   }
97   return false;
98 }
99 
filePreview(const QUrl & url,int size)100 QPixmap NetAccess::filePreview(const QUrl& url, int size) {
101   return filePreview(KFileItem(url), size);
102 }
103 
filePreview(const KFileItem & item,int size)104 QPixmap NetAccess::filePreview(const KFileItem& item, int size) {
105   NetAccess netaccess;
106 
107   // the default plugins are not used by default (what???)
108   // the default ones are in config settings instead, so ignore that
109   const QStringList plugins = KIO::PreviewJob::defaultPlugins();
110   KIO::PreviewJob* previewJob = KIO::filePreview(KFileItemList() << item, QSize(size, size),
111                                                  &plugins);
112   connect(previewJob, &KIO::PreviewJob::gotPreview,
113           &netaccess, &Tellico::NetAccess::slotPreview);
114 
115   if(GUI::Proxy::widget()) {
116     KJobWidgets::setWindow(previewJob, GUI::Proxy::widget());
117   }
118   if(!previewJob->exec()) {
119     myDebug() << "Preview job did not succeed";
120   }
121   if(previewJob->error() != 0) {
122     myDebug() << previewJob->errorString();
123   }
124   return netaccess.m_preview;
125 }
126 
slotPreview(const KFileItem &,const QPixmap & pix_)127 void NetAccess::slotPreview(const KFileItem&, const QPixmap& pix_) {
128   m_preview = pix_;
129 }
130 
removeTempFile(const QString & name)131 void NetAccess::removeTempFile(const QString& name) {
132   if(!tmpfiles) {
133     return;
134   }
135   if(tmpfiles->contains(name)) {
136     QFile::remove(name);
137     tmpfiles->removeAll(name);
138   }
139 }
140 
exists(const QUrl & url_,bool sourceSide_,QWidget * window_)141 bool NetAccess::exists(const QUrl& url_, bool sourceSide_, QWidget* window_) {
142   if(url_.isLocalFile()) {
143     return QFile::exists(url_.toLocalFile());
144   }
145 
146   KIO::StatJob* job = KIO::stat(url_);
147   KJobWidgets::setWindow(job, window_);
148   job->setSide(sourceSide_ ? KIO::StatJob::SourceSide : KIO::StatJob::DestinationSide);
149   return job->exec();
150 }
151 
lastErrorString()152 QString NetAccess::lastErrorString() {
153   return s_lastErrorMessage;
154 }
155