void AdBlockSubscription::subscriptionDownloaded() { if (m_reply != qobject_cast<FollowRedirectReply*>(sender())) { return; } bool error = false; const QByteArray response = QString::fromUtf8(m_reply->readAll()).toUtf8(); if (m_reply->error() != QNetworkReply::NoError || !response.startsWith(QByteArray("[Adblock")) || !saveDownloadedData(response) ) { error = true; } m_reply->deleteLater(); m_reply = 0; if (error) { emit subscriptionError(tr("Cannot load subscription!")); return; } loadSubscription(AdBlockManager::instance()->disabledRules()); emit subscriptionUpdated(); }
void AdBlockSubscription::subscriptionDownloaded() { if (m_reply != qobject_cast<FollowRedirectReply*>(sender())) { return; } QByteArray response = QString::fromUtf8(m_reply->readAll()).toUtf8(); if (m_reply->error() == QNetworkReply::NoError && response.startsWith("[Adblock")) { // Prepend subscription info response.prepend(QString("Title: %1\nUrl: %2\n").arg(title(), url().toString()).toUtf8()); saveDownloadedData(response); loadSubscription(AdBlockManager::instance()->disabledRules()); emit subscriptionUpdated(); } m_reply->deleteLater(); m_reply = 0; }
bool UpdateApplication::createFile() // ---------------------------------------------------------------------------- // Create the update file // ---------------------------------------------------------------------------- { XL_ASSERT(!file); // Keep the name of the remote file in the URL QString fileName = QFileInfo(url.path()).fileName(); // Choose folder #if QT_VERSION >= 0x050000 // Thank you Qt5 for this insanity, just in case we have several ~/Desktop QString desktop = ""; QStringList desktops = QStandardPaths::standardLocations( QStandardPaths::DesktopLocation); if (desktops.size() >= 1) desktop = desktops[0]; #else QString desktop = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation); #endif QString folder = QFileDialog::getExistingDirectory(NULL, tr("Select destination folder"), desktop); if (!folder.isEmpty()) { // Set complete filename QString completeFileName = folder + "/" + fileName; file = new QFile(completeFileName); if (file->exists()) { QString msg = tr("<h3>File exists</h3>"); QString info = tr("<p>The selected folder already contains a file " "called <b>%1</b>.</p>" "<p>Save anyway?</p>").arg(fileName); QMessageBox box(TaoApp->windowWidget()); setBoxMinimumWidth(box, 400); box.setIcon(QMessageBox::Warning); box.setWindowTitle(dialogTitle); box.setText(msg); box.setInformativeText(info); box.setStandardButtons(QMessageBox::Yes | QMessageBox::No); box.setDefaultButton(QMessageBox::No); if (box.exec() == QMessageBox::No) { delete file; file = NULL; return false; } } if (!file->open(QIODevice::WriteOnly | QIODevice::Truncate)) { IFTRACE(update) debug() << "Could not open file for writing: '" << +completeFileName << "' error: " << +file->errorString() << "\n"; QString msg = tr("<h3>Write error</h3>"); QString info = tr("<p>The upgrade cannot be saved to <b>%1</b>.</p>") .arg(fileName); QString details = file->errorString(); QMessageBox box(TaoApp->windowWidget()); setBoxMinimumWidth(box, 400); box.setIcon(QMessageBox::Critical); box.setWindowTitle(dialogTitle); box.setText(msg); box.setInformativeText(info); box.setDetailedText(details); box.setStandardButtons(QMessageBox::Ok); box.exec(); delete file; file = NULL; return false; } IFTRACE(update) debug() << "Save to: '" << +completeFileName << "'\n"; if (state == Downloaded) { // Download completed while user was still choosing file path saveDownloadedData(); } return true; } return false; }
void UpdateApplication::downloadFinished() // ---------------------------------------------------------------------------- // Process downloaded data reply // ---------------------------------------------------------------------------- { if (state == NetworkErrorCheck || state == NetworkErrorDownload) { QString details = reply->errorString(); reply->abort(); progress->close(); if (state == NetworkErrorDownload || show) { QString msg = tr("<h3>Network error</h3>"); QString info = tr("<p>Impossible to obtain update information.</p>" "<p>Please make sure that you are connected to " "the Internet and try again.</p>"); QMessageBox box(TaoApp->windowWidget()); setBoxMinimumWidth(box, 400); box.setIconPixmap(connectionErrorIcon); box.setWindowTitle(dialogTitle); box.setText(msg); box.setInformativeText(info); box.setDetailedText(details); box.setStandardButtons(QMessageBox::Ok); box.exec(); } return; } if (code != 200) return; bool updateAvailable = false; qint64 pid = 0; QString name; switch (state) { case WaitingForUpdate: // Save and process the update information we have just received progress->hide(); XL_ASSERT(!file); pid = QCoreApplication::applicationPid(); name = QString("tao_update.%1").arg(pid); file = new QFile(QDir::temp().filePath(name)); if (!file->open(QIODevice::WriteOnly | QIODevice::Truncate)) { IFTRACE(update) debug() << "Failed to open: " << +file->fileName() << "\n"; delete file; file = NULL; return; } file->write(reply->readAll()); file->close(); reply->deleteLater(); reply = NULL; readIniFile(); file->remove(); delete file; file = NULL; setLastChecked(QDateTime::currentDateTime()); // Propose to update if current version is older than the remote one updateAvailable = (version < remoteVersion) && !url.isEmpty(); IFTRACE(update) debug() << "Update available: " << updateAvailable << " (local " << version << " remote " << remoteVersion << ")\n"; if (updateAvailable) { bool ok = false; double min = QSettings().value("AppUpdatePromptMinVersion", -1.0) .toDouble(&ok); if (!ok) min = -1.0; if (show || remoteVersion > min) { if (remoteVersion > min) { // Remember that user was prompted to download this // version. He/she will not be prompted again on application // startup, unless a newer version exists. QSettings().setValue("AppUpdatePromptMinVersion", remoteVersion); IFTRACE2(update, settings) debug() << "Saving AppUpdatePromptMinVersion=" << remoteVersion << "\n"; } // Show update dialog QString msg = tr("<h3>Update available</h3>"); QString info = tr("<p>%1 version %2 is available." " Do you want to download it now?</p>") .arg(appName()).arg(remoteVer()); QMessageBox box(TaoApp->windowWidget()); setBoxMinimumWidth(box, 400); box.setIconPixmap(downloadIcon); box.setWindowTitle(dialogTitle); box.setText(msg); box.setInformativeText(info); if (!description.isEmpty()) box.setDetailedText(description); box.setStandardButtons(QMessageBox::Yes | QMessageBox::No); if (box.exec() == QMessageBox::Yes) { startDownload(); break; } } else { IFTRACE(update) debug() << "Update available dialog not shown due to " "AppUpdatePromptMinVersion=" << min << "\n"; } } else { if (show) showNoUpdateAvailable(); } progress->close(); break; case Downloading: IFTRACE(update) debug() << "Download finished\n"; progress->hide(); state = Downloaded; if (file) saveDownloadedData(); break; default: break; } }