bool Transfer::executeCustomCommands() { Logger::log("Transfer::executeCustomCommands()", Logger::LowVerbosity); m_commands.clear(); QString command = customCommand(); const QString defaultCommand = Settings::customTransferCommand(); const bool defaultEnabled = (!defaultCommand.isEmpty()) && (Settings::customTransferCommandEnabled()); if (!command.isEmpty()) { const QString workingDirectory = downloadPath(); command.replace("%f", fileName()); m_commands << Command(workingDirectory, command); Logger::log(QString("Transfer::executeCustomCommands(): Adding custom command: Working directory: %1, Command: %2") .arg(workingDirectory).arg(command), Logger::LowVerbosity); } if ((defaultEnabled) && ((command.isEmpty()) || (!customCommandOverrideEnabled()))) { const QString workingDirectory = downloadPath(); command = defaultCommand; command.replace("%f", fileName()); m_commands << Command(workingDirectory, command); Logger::log(QString("Transfer::executeCustomCommands(): Adding custom command: Working directory: %1, Command: %2") .arg(workingDirectory).arg(command), Logger::LowVerbosity); } if (!m_commands.isEmpty()) { setStatus(ExecutingCustomCommand); executeCustomCommand(m_commands.takeFirst()); return true; } return false; }
void Transfer::setDownloadPath(const QString &path) { if (path != downloadPath()) { m_downloadPath = path.endsWith("/") ? path : path + "/"; emit downloadPathChanged(); if (!fileName().isEmpty()) { m_file.setFileName(downloadPath() + fileName()); m_bytesTransferred = m_file.size(); if ((m_size > 0) && (m_bytesTransferred > 0)) { setProgress(m_bytesTransferred * 100 / m_size); } } } }
bool InternUpdater::download7zipIfNeeded(void) { if (this->_sevenZipReply != NULL) return false; // already downloading QDir downloadPath(QDir::current()); if (!downloadPath.exists(OutDir)) downloadPath.mkdir(OutDir); downloadPath.cd(OutDir); if (downloadPath.exists(SevenZipBinaryName)) { #ifndef QT_NO_DEBUG qDebug() << "[InternUpdater::download7zipIfNeeded]" << SevenZipBinaryName << "is already downloaded."; #endif // Check if 7z has exec rights QFile bin(downloadPath.path() +QDir::separator()+ SevenZipBinaryName); if (bin.open(QIODevice::ReadOnly)) { bin.setPermissions(bin.permissions() | QFile::ExeOwner); bin.close(); } return false; } QUrl url(ServerUrl + SevenZipBinaryName); this->_sevenZipReply = this->_netManager->get(QNetworkRequest(url)); #ifndef QT_NO_DEBUG qDebug() << "[InternUpdater::download7zipIfNeeded]" << "Downloading" << SevenZipBinaryName; #endif return true; }
void InternUpdater::replaceUpdaterBinaryIfNeeded(void) { static int attempts = 0; QDir downloadPath(QDir::current()); if (!downloadPath.exists(OutDir)) return; downloadPath.cd(OutDir); if (!downloadPath.exists(UpdaterBinaryName)) return; QDir destPath(QDir::current()); destPath.makeAbsolute(); destPath.remove(UpdaterBinaryName); const bool moveResult = downloadPath.rename(downloadPath.filePath(UpdaterBinaryName), destPath.path()+QDir::separator()+UpdaterBinaryName); #ifndef QT_NO_DEBUG qDebug() << "[InternUpdater::replaceUpdaterBinaryIfNeeded]" << "moveResult:" << moveResult; #endif if (moveResult == false && attempts++ < ReplaceBinaryAttempts) { #ifndef QT_NO_DEBUG qDebug() << "[InternUpdater::replaceUpdaterBinaryIfNeeded]" << "replaceUpdaterBinaryIfNeeded called again in five seconds."; qDebug() << "Remaining attempts:" << ReplaceBinaryAttempts - attempts; #endif QTimer::singleShot(5000, this, SLOT(replaceUpdaterBinaryIfNeeded())); } }
void InternUpdater::handleSevenZipReply(void) { QDir downloadPath(QDir::current()); if (!downloadPath.exists(OutDir)) downloadPath.mkdir(OutDir); downloadPath.cd(OutDir); QFile bin(downloadPath.path() + QDir::separator() + SevenZipBinaryName); if (bin.open(QIODevice::WriteOnly)) { bin.write(this->_sevenZipReply->readAll()); bin.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner); bin.close(); #ifndef QT_NO_DEBUG qDebug() << "[InternUpdater::handleSevenZipReply]" << SevenZipBinaryName << "has been successfully downloaded."; #endif } #ifndef QT_NO_DEBUG else { qDebug() << "[InternUpdater::handleSevenZipReply]" << "Open failed:" << bin.errorString(); } #endif }
void SettingsManager::save() { QString path = QDesktopServices::storageLocation(QDesktopServices::DataLocation) + "/"; QDir dir; if(!dir.mkpath(path)) { return; } QSettings settings("PayableOnDeath", "Surfer"); settings.setValue("webkit/images", isImagesEnabled()); settings.setValue("webkit/javascript", isJavascriptEnabled()); settings.setValue("webkit/java", isJavaEnabled()); settings.setValue("webkit/plugins", isPluginsEnabled()); settings.setValue("webkit/private_browsing", isPrivateBrowsingEnabled()); settings.setValue("geometry/main_window", mainWindowGeometry()); settings.setValue("geometry/history_dialog", historyDialogGeometry()); settings.setValue("geometry/bookmarks_dialog", bookmarksDialogGeometry()); settings.setValue("geometry/settings_dialog", settingsDialogGeometry()); settings.setValue("geometry/download_dialog", downloadDialogGeometry()); settings.setValue("extension/cache/maximum_size", maximumCacheSize()); settings.setValue("extension/history/expiration_days", historyExpirationDays()); settings.setValue("extension/download/path", downloadPath()); settings.setValue("font/sans/family", sansFontFamily()); settings.setValue("font/serif/family", serifFontFamily()); settings.setValue("font/mono/family", monoFontFamily()); settings.setValue("font/standard/size", standardFontSize()); settings.setValue("font/mono/size", monoFontSize()); }
void Transfer::followRedirect(const QUrl &u) { Logger::log("Transfer::followRedirect(). URL: " + u.toString(), Logger::LowVerbosity); QDir().mkpath(downloadPath()); if (!m_file.open(m_file.exists() ? QFile::Append : QFile::WriteOnly)) { setErrorString(m_file.errorString()); setStatus(Failed); return; } m_redirects++; if (!m_nam) { m_nam = new QNetworkAccessManager(this); m_ownNetworkAccessManager = true; } QNetworkRequest request(u); request.setRawHeader("User-Agent", USER_AGENT); if (m_bytesTransferred > 0) { request.setRawHeader("Range", "bytes=" + QByteArray::number(m_bytesTransferred) + "-"); } m_reply = m_nam->get(request); connect(m_reply, SIGNAL(metaDataChanged()), this, SLOT(onReplyMetaDataChanged())); connect(m_reply, SIGNAL(readyRead()), this, SLOT(onReplyReadyRead())); connect(m_reply, SIGNAL(finished()), this, SLOT(onReplyFinished())); }
void Transfer::setFileName(const QString &name) { if (name != fileName()) { m_fileName = name; switch (transferType()) { case Transfer::Download: m_fileName.replace(ILLEGAL_FILENAME_CHARS_RE, "_"); break; default: break; } emit fileNameChanged(); if (!downloadPath().isEmpty()) { m_file.setFileName(downloadPath() + fileName()); m_bytesTransferred = m_file.size(); if ((m_size > 0) && (m_bytesTransferred > 0)) { setProgress(m_bytesTransferred * 100 / m_size); } } } }
void PluginTransfer::onStreamsRequestFinished() { if (m_streamsRequest->status() == ResourcesRequest::Ready) { const QVariantMap result = m_streamsRequest->result().toMap(); if (result.contains("items")) { const QVariantList list = result.value("items").toList(); foreach (const QVariant &v, list) { const QVariantMap stream = v.toMap(); if (stream.value("id") == streamId()) { const QFileInfo info(downloadPath() + fileName()); QString suffix = info.suffix(); if (suffix.isEmpty()) { suffix = stream.value("ext").toString(); if (!suffix.isEmpty()) { if (!suffix.startsWith(".")) { suffix.prepend("."); } setFileName(info.completeBaseName() + suffix); } } startDownload(stream.value("url").toString()); return; } } } else if (result.contains("service")) { const QString service = result.value("service").toString(); if (service == Resources::SOUNDCLOUD) { soundcloudStreamsRequest()->get(result.value("id").toString()); } else { setErrorString(tr("Attempted redirect to unsupported service '%1'").arg(service)); Logger::log("PluginTransfer::onStreamsRequestFinished(). Error: " + errorString()); setStatus(Failed); } return; } }
void Transfer::cancel() { switch (status()) { case Canceled: case Completed: case ExecutingCustomCommand: return; default: break; } if ((m_reply) && (m_reply->isRunning())) { m_canceled = true; m_reply->abort(); } else { m_file.remove(); QDir().rmdir(downloadPath()); setStatus(Canceled); } }