| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
|---|---|
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "wgetdialog.h" |
| 6 | |
| 7 | #include <QRegularExpression> |
| 8 | #include <QDebug> |
| 9 | |
| 10 | WGetDialog::WGetDialog(QWidget *parent, Qt::WindowFlags f) |
| 11 | : ProcessDialog (parent, f) |
| 12 | { |
| 13 | setWindowTitle("wget request download"); |
| 14 | process.setProgram("wget"); |
| 15 | } |
| 16 | |
| 17 | void WGetDialog::setWorkingDirectory(const QString &workDir) |
| 18 | { |
| 19 | process.setWorkingDirectory(workDir); |
| 20 | } |
| 21 | |
| 22 | QString WGetDialog::workDirectory() const |
| 23 | { |
| 24 | return process.workingDirectory(); |
| 25 | } |
| 26 | |
| 27 | void WGetDialog::setWgetArguments(const QStringList &list) |
| 28 | { |
| 29 | setWindowTitle(windowTitle() + " "+ list.join( " ")); |
| 30 | process.setArguments(list); |
| 31 | } |
| 32 | |
| 33 | QStringList WGetDialog::wgetArguments() const |
| 34 | { |
| 35 | return process.arguments(); |
| 36 | } |
| 37 | |
| 38 | void WGetDialog::doShowStdErr(const QByteArray &array) |
| 39 | { |
| 40 | static QString cacheData; |
| 41 | static QString headTitle; |
| 42 | static QString downloadLine; |
| 43 | cacheData += array; |
| 44 | auto datas = cacheData.split("\n\n"); |
| 45 | if (datas.size() >= 2) { |
| 46 | headTitle = datas.takeAt(0); |
| 47 | textBrowser->setText(headTitle + "\n"+ downloadLine); |
| 48 | cacheData = datas.join(""); |
| 49 | } |
| 50 | |
| 51 | if (!headTitle.isEmpty()) { |
| 52 | auto lines = cacheData.split("\n"); |
| 53 | if (lines.size() >= 2) { |
| 54 | downloadLine = lines.takeAt(0); |
| 55 | textBrowser->setText(headTitle + "\n"+ downloadLine); |
| 56 | QRegularExpression regExp("\\d+\\%"); |
| 57 | auto matchRes = regExp.match(downloadLine); |
| 58 | if (matchRes.hasMatch()) { |
| 59 | QString matched = matchRes.captured(); |
| 60 | matched = matched.remove(matched.size() - 1, 1); |
| 61 | doShowProgress(matched.toInt(), 100); |
| 62 | } |
| 63 | cacheData = lines.join(""); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void WGetDialog::doShowStdOut(const QByteArray &array) |
| 69 | { |
| 70 | textBrowser->append(array); |
| 71 | } |
| 72 | |
| 73 | void WGetDialog::doFinished(int exitCode, QProcess::ExitStatus status) |
| 74 | { |
| 75 | this->close(); |
| 76 | qInfo() << exitCode << status; |
| 77 | } |
| 78 | |
| 79 | void WGetDialog::showEvent(QShowEvent *event) |
| 80 | { |
| 81 | process.start(); |
| 82 | return QDialog::showEvent(event); |
| 83 | } |
| 84 |