| 46 | } |
| 47 | |
| 48 | void GM_Downloader::scriptDownloaded() |
| 49 | { |
| 50 | Q_ASSERT(m_reply == qobject_cast<QNetworkReply*>(sender())); |
| 51 | |
| 52 | deleteLater(); |
| 53 | m_reply->deleteLater(); |
| 54 | |
| 55 | if (m_reply->error() != QNetworkReply::NoError) { |
| 56 | qWarning() << "GreaseMonkey: Cannot download script" << m_reply->errorString(); |
| 57 | Q_EMIT error(); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | QString contentType = m_reply->header(QNetworkRequest::ContentTypeHeader).toString(); |
| 62 | if (!(contentType.startsWith(QSL("text/plain")) || contentType.startsWith(QSL("text/javascript")))) { |
| 63 | qWarning() << "GreaseMonkey: Unsupported content type" << contentType; |
| 64 | Q_EMIT error(); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | const QByteArray response = QString::fromUtf8(m_reply->readAll()).toUtf8(); |
| 69 | |
| 70 | if (!response.contains(QByteArray("// ==UserScript=="))) { |
| 71 | qWarning() << "GreaseMonkey: Script does not contain UserScript header" << m_reply->request().url(); |
| 72 | Q_EMIT error(); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | if (m_fileName.isEmpty()) { |
| 77 | const QString filePath = QSL("%1/%2").arg(m_manager->scriptsDirectory(), QzTools::getFileNameFromUrl(m_reply->url())); |
| 78 | m_fileName = QzTools::ensureUniqueFilename(filePath); |
| 79 | } |
| 80 | |
| 81 | QFile file(m_fileName); |
| 82 | |
| 83 | if (!file.open(QFile::WriteOnly)) { |
| 84 | qWarning() << "GreaseMonkey: Cannot open file for writing" << m_fileName; |
| 85 | Q_EMIT error(); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | file.write(response); |
| 90 | file.close(); |
| 91 | |
| 92 | Q_EMIT finished(m_fileName); |
| 93 | } |
| 94 | |
| 95 | void GM_Downloader::requireDownloaded() |
| 96 | { |
nothing calls this directly
no test coverage detected