1 /*
2 	Actiona
3 	Copyright (C) 2005 Jonathan Mercier-Ganady
4 
5 	Actiona is free software: you can redistribute it and/or modify
6 	it under the terms of the GNU General Public License as published by
7 	the Free Software Foundation, either version 3 of the License, or
8 	(at your option) any later version.
9 
10 	Actiona is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 	Contact : jmgr@jmgr.info
19 */
20 
21 #include "webdownloadinstance.h"
22 
23 #include <QNetworkAccessManager>
24 #include <QNetworkReply>
25 #include <QNetworkRequest>
26 #include <QProgressDialog>
27 
28 namespace Actions
29 {
30     Tools::StringListPair WebDownloadInstance::destinations =
31     {
32         {
33             QStringLiteral("variable"),
34             QStringLiteral("file")
35         },
36         {
37             QStringLiteral(QT_TRANSLATE_NOOP("WebDownloadInstance::destinations", "Variable")),
38             QStringLiteral(QT_TRANSLATE_NOOP("WebDownloadInstance::destinations", "File"))
39         }
40     };
41 
WebDownloadInstance(const ActionTools::ActionDefinition * definition,QObject * parent)42 	WebDownloadInstance::WebDownloadInstance(const ActionTools::ActionDefinition *definition, QObject *parent)
43 		: ActionTools::ActionInstance(definition, parent),
44 		  mNetworkAccessManager(new QNetworkAccessManager(this)),
45 		  mReply(nullptr),
46 		  mDestination(Variable),
47 		  mProgressDialog(new QProgressDialog)
48 	{
49         connect(mProgressDialog, &QProgressDialog::canceled, this, &WebDownloadInstance::canceled);
50 	}
51 
~WebDownloadInstance()52 	WebDownloadInstance::~WebDownloadInstance()
53 	{
54 		delete mProgressDialog;
55 	}
56 
startExecution()57 	void WebDownloadInstance::startExecution()
58 	{
59 		bool ok = true;
60 
61 		QString urlString = evaluateString(ok, QStringLiteral("url"));
62 		mDestination = evaluateListElement<Destination>(ok, destinations, QStringLiteral("destination"));
63 		mVariable = evaluateVariable(ok, QStringLiteral("variable"));
64 		QString file = evaluateString(ok, QStringLiteral("file"));
65 
66 
67 		if(!ok)
68 			return;
69 
70 		QUrl url(urlString);
71 		if(url.scheme() == QString())
72 			url = QUrl(QStringLiteral("http://") + urlString, QUrl::TolerantMode);
73 
74 		if(!url.isValid())
75 		{
76 			setCurrentParameter(QStringLiteral("url"));
77 			emit executionException(ActionTools::ActionException::InvalidParameterException, tr("Invalid URL"));
78 			return;
79 		}
80 
81 		if(mDestination == File)
82 		{
83 			mFile.setFileName(file);
84 			if(!mFile.open(QIODevice::WriteOnly))
85 			{
86 				setCurrentParameter(QStringLiteral("file"));
87 				emit executionException(CannotOpenFileException, tr("Cannot write to file"));
88 				return;
89 			}
90 		}
91 
92 		mReply = mNetworkAccessManager->get(QNetworkRequest(url));
93 
94         connect(mReply, &QNetworkReply::finished, this, &WebDownloadInstance::finished);
95         connect(mReply, &QNetworkReply::downloadProgress, this, &WebDownloadInstance::downloadProgress);
96         connect(mReply, &QNetworkReply::readyRead, this, &WebDownloadInstance::readyRead);
97 
98 		mProgressDialog->setModal(false);
99 		mProgressDialog->setWindowTitle(tr("Downloading"));
100 		mProgressDialog->setLabelText(tr("Downloading..."));
101 		mProgressDialog->setMaximum(100);
102 		mProgressDialog->show();
103 	}
104 
stopExecution()105 	void WebDownloadInstance::stopExecution()
106 	{
107 		if(mReply)
108 			mReply->abort();
109 	}
110 
finished()111 	void WebDownloadInstance::finished()
112 	{
113 		mFile.close();
114 
115 		switch(mReply->error())
116 		{
117 		case QNetworkReply::NoError:
118 			if(mDestination == Variable)
119                 setVariable(mVariable, QString::fromUtf8(mReply->readAll()));
120 
121             executionEnded();
122 			break;
123 		case QNetworkReply::OperationCanceledError:
124 			if(mDestination == File)
125 				mFile.remove();
126 
127             executionEnded();
128 			break;
129 		default:
130 			{
131 				if(mDestination == File)
132 					mFile.remove();
133 
134 				setCurrentParameter(QStringLiteral("url"));
135 
136 				emit executionException(DownloadException, tr("Download error: %1").arg(mReply->errorString()));
137 			}
138 			break;
139 		}
140 
141 		mProgressDialog->close();
142 
143 		mReply->deleteLater();
144 		mReply = nullptr;
145 	}
146 
downloadProgress(qint64 bytesReceived,qint64 bytesTotal)147 	void WebDownloadInstance::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
148 	{
149 		if(bytesTotal > 0)
150 			mProgressDialog->setValue((bytesReceived * 100) / bytesTotal);
151 	}
152 
readyRead()153 	void WebDownloadInstance::readyRead()
154 	{
155 		if(mDestination == File)
156 			mFile.write(mReply->readAll());
157 	}
158 
canceled()159 	void WebDownloadInstance::canceled()
160 	{
161 		if(mReply)
162 			mReply->abort();
163 	}
164 }
165