| 1 | #include "job_widget.h" |
| 2 | #include "utils.h" |
| 3 | |
| 4 | JobWidget::JobWidget(QProcess *process, const QString &info, |
| 5 | const QStringList &args, const QString &source, |
| 6 | const QString &dest, QWidget *parent) |
| 7 | : QWidget(parent), mProcess(process) { |
| 8 | ui.setupUi(this); |
| 9 | |
| 10 | mArgs.append(QDir::toNativeSeparators(GetRclone())); |
| 11 | mArgs.append(GetRcloneConf()); |
| 12 | mArgs.append(args); |
| 13 | |
| 14 | ui.source->setText(source); |
| 15 | ui.dest->setText(dest); |
| 16 | ui.info->setText(info); |
| 17 | |
| 18 | ui.details->setVisible(false); |
| 19 | |
| 20 | ui.output->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); |
| 21 | ui.output->setVisible(false); |
| 22 | |
| 23 | QObject::connect( |
| 24 | ui.showDetails, &QToolButton::toggled, this, [=](bool checked) { |
| 25 | ui.details->setVisible(checked); |
| 26 | ui.showDetails->setArrowType(checked ? Qt::DownArrow : Qt::RightArrow); |
| 27 | }); |
| 28 | |
| 29 | QObject::connect( |
| 30 | ui.showOutput, &QToolButton::toggled, this, [=](bool checked) { |
| 31 | ui.output->setVisible(checked); |
| 32 | ui.showOutput->setArrowType(checked ? Qt::DownArrow : Qt::RightArrow); |
| 33 | }); |
| 34 | |
| 35 | ui.cancel->setIcon( |
| 36 | QApplication::style()->standardIcon(QStyle::SP_DialogCloseButton)); |
| 37 | |
| 38 | QObject::connect(ui.cancel, &QToolButton::clicked, this, [=]() { |
| 39 | if (mRunning) { |
| 40 | int button = QMessageBox::question( |
| 41 | this, "Transfer", |
| 42 | QString("rclone process is still running. Do you want to cancel it?"), |
| 43 | QMessageBox::Yes | QMessageBox::No); |
| 44 | if (button == QMessageBox::Yes) { |
| 45 | cancel(); |
| 46 | } |
| 47 | } else { |
| 48 | emit closed(); |
| 49 | } |
| 50 | }); |
| 51 | |
| 52 | ui.copy->setIcon( |
| 53 | QApplication::style()->standardIcon(QStyle::SP_FileLinkIcon)); |
| 54 | |
| 55 | QObject::connect(ui.copy, &QToolButton::clicked, this, [=]() { |
| 56 | QClipboard *clipboard = QGuiApplication::clipboard(); |
| 57 | clipboard->setText(mArgs.join(" ")); |
| 58 | }); |
| 59 | |
| 60 | QObject::connect(mProcess, &QProcess::readyRead, this, [=]() { |
| 61 | QRegExp rxSize( |
| 62 | R"(^Transferred:\s+(\S+ \S+) \(([^)]+)\)$)"); // Until rclone 1.42 |
| 63 | QRegExp rxSize2( |
| 64 | R"(^Transferred:\s+([0-9.]+)(\S)? \/ (\S+) (\S+), ([0-9%-]+), (\S+ \S+), (\S+) (\S+)$)"); // Starting with rclone 1.43 |
| 65 | QRegExp rxErrors(R"(^Errors:\s+(\S+)$)"); |
| 66 | QRegExp rxChecks(R"(^Checks:\s+(\S+)$)"); // Until rclone 1.42 |
| 67 | QRegExp rxChecks2( |
| 68 | R"(^Checks:\s+(\S+) \/ (\S+), ([0-9%-]+)$)"); // Starting with |
| 69 | // rclone 1.43 |
| 70 | QRegExp rxTransferred(R"(^Transferred:\s+(\S+)$)"); // Until rclone 1.42 |
| 71 | QRegExp rxTransferred2( |
| 72 | R"(^Transferred:\s+(\S+) \/ (\S+), ([0-9%-]+)$)"); // Starting with |
| 73 | // rclone 1.43 |
| 74 | QRegExp rxTime(R"(^Elapsed time:\s+(\S+)$)"); |
| 75 | QRegExp rxProgress( |
| 76 | R"(^\*([^:]+):\s*([^%]+)% done.+(ETA: [^)]+)$)"); // Until rclone 1.38 |
| 77 | QRegExp rxProgress2( |
| 78 | R"(\*([^:]+):\s*([^%]+)% \/[a-zA-z0-9.]+, [a-zA-z0-9.]+\/s, (\w+)$)"); // Starting with rclone 1.39 |
| 79 | |
| 80 | while (mProcess->canReadLine()) { |
| 81 | QString line = mProcess->readLine().trimmed(); |
| 82 | if (++mLines == 10000) { |
| 83 | ui.output->clear(); |
| 84 | mLines = 1; |
| 85 | } |
| 86 | ui.output->appendPlainText(line); |
| 87 | |
| 88 | if (line.isEmpty()) { |
| 89 | for (auto it = mActive.begin(), eit = mActive.end(); it != eit; |
| 90 | /* empty */) { |
| 91 | auto label = it.value(); |
| 92 | if (mUpdated.contains(label)) { |
| 93 | ++it; |
| 94 | } else { |
| 95 | it = mActive.erase(it); |
| 96 | ui.progress->removeWidget(label->buddy()); |
| 97 | ui.progress->removeWidget(label); |
| 98 | delete label->buddy(); |
| 99 | delete label; |
| 100 | } |
| 101 | } |
| 102 | mUpdated.clear(); |
| 103 | continue; |
| 104 | } |
| 105 | |
| 106 | if (rxSize.exactMatch(line)) { |
| 107 | ui.size->setText(rxSize.cap(1)); |
| 108 | ui.bandwidth->setText(rxSize.cap(2)); |
| 109 | } else if (rxSize2.exactMatch(line)) { |
| 110 | ui.size->setText(rxSize2.cap(1) + " " + rxSize2.cap(2) + "B" + ", " + |
| 111 | rxSize2.cap(5)); |
| 112 | ui.bandwidth->setText(rxSize2.cap(6)); |
| 113 | ui.eta->setText(rxSize2.cap(8)); |
| 114 | ui.totalsize->setText(rxSize2.cap(3) + " " + rxSize2.cap(4)); |
| 115 | } else if (rxErrors.exactMatch(line)) { |
| 116 | ui.errors->setText(rxErrors.cap(1)); |
| 117 | } else if (rxChecks.exactMatch(line)) { |
| 118 | ui.checks->setText(rxChecks.cap(1)); |
| 119 | } else if (rxChecks2.exactMatch(line)) { |
| 120 | ui.checks->setText(rxChecks2.cap(1) + " / " + rxChecks2.cap(2) + ", " + |
| 121 | rxChecks2.cap(3)); |
| 122 | } else if (rxTransferred.exactMatch(line)) { |
| 123 | ui.transferred->setText(rxTransferred.cap(1)); |
| 124 | } else if (rxTransferred2.exactMatch(line)) { |
| 125 | ui.transferred->setText(rxTransferred2.cap(1) + " / " + |
| 126 | rxTransferred2.cap(2) + ", " + |
| 127 | rxTransferred2.cap(3)); |
| 128 | } else if (rxTime.exactMatch(line)) { |
| 129 | ui.elapsed->setText(rxTime.cap(1)); |
| 130 | } else if (rxProgress.exactMatch(line)) { |
| 131 | QString name = rxProgress.cap(1).trimmed(); |
| 132 | |
| 133 | auto it = mActive.find(name); |
| 134 | |
| 135 | QLabel *label; |
| 136 | QProgressBar *bar; |
| 137 | if (it == mActive.end()) { |
| 138 | label = new QLabel(); |
| 139 | label->setText(name); |
| 140 | |
| 141 | bar = new QProgressBar(); |
| 142 | bar->setMinimum(0); |
| 143 | bar->setMaximum(100); |
| 144 | bar->setTextVisible(true); |
| 145 | |
| 146 | label->setBuddy(bar); |
| 147 | |
| 148 | ui.progress->addRow(label, bar); |
| 149 | |
| 150 | mActive.insert(name, label); |
| 151 | } else { |
| 152 | label = it.value(); |
| 153 | bar = static_cast<QProgressBar *>(label->buddy()); |
| 154 | } |
| 155 | |
| 156 | bar->setValue(rxProgress.cap(2).toInt()); |
| 157 | bar->setToolTip(rxProgress.cap(3)); |
| 158 | |
| 159 | mUpdated.insert(label); |
| 160 | } else if (rxProgress2.exactMatch(line)) { |
| 161 | QString name = rxProgress2.cap(1).trimmed(); |
| 162 | |
| 163 | auto it = mActive.find(name); |
| 164 | |
| 165 | QLabel *label; |
| 166 | QProgressBar *bar; |
| 167 | if (it == mActive.end()) { |
| 168 | label = new QLabel(); |
| 169 | |
| 170 | QString nameTrimmed; |
| 171 | |
| 172 | if (name.length() > 47) { |
| 173 | nameTrimmed = name.left(25) + "..." + name.right(19); |
| 174 | } else { |
| 175 | nameTrimmed = name; |
| 176 | } |
| 177 | |
| 178 | label->setText(nameTrimmed); |
| 179 | |
| 180 | bar = new QProgressBar(); |
| 181 | bar->setMinimum(0); |
| 182 | bar->setMaximum(100); |
| 183 | bar->setTextVisible(true); |
| 184 | |
| 185 | label->setBuddy(bar); |
| 186 | |
| 187 | ui.progress->addRow(label, bar); |
| 188 | |
| 189 | mActive.insert(name, label); |
| 190 | } else { |
| 191 | label = it.value(); |
| 192 | bar = static_cast<QProgressBar *>(label->buddy()); |
| 193 | } |
| 194 | |
| 195 | bar->setValue(rxProgress2.cap(2).toInt()); |
| 196 | bar->setToolTip("File name: " + name + "\nFile stats" + rxProgress2.cap(0).mid(rxProgress2.cap(0).indexOf(':'))); |
| 197 | |
| 198 | mUpdated.insert(label); |
| 199 | } |
| 200 | } |
| 201 | }); |
| 202 | |
| 203 | QObject::connect(mProcess, |
| 204 | static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>( |
| 205 | &QProcess::finished), |
| 206 | this, [=](int status, QProcess::ExitStatus) { |
| 207 | mProcess->deleteLater(); |
| 208 | for (auto label : mActive) { |
| 209 | ui.progress->removeWidget(label->buddy()); |
| 210 | ui.progress->removeWidget(label); |
| 211 | delete label->buddy(); |
| 212 | delete label; |
| 213 | } |
| 214 | |
| 215 | mRunning = false; |
| 216 | if (status == 0) { |
| 217 | ui.showDetails->setStyleSheet( |
| 218 | "QToolButton { border: 0; color: black; }"); |
| 219 | ui.showDetails->setText("Finished"); |
| 220 | } else { |
| 221 | ui.showDetails->setStyleSheet( |
| 222 | "QToolButton { border: 0; color: red; }"); |
| 223 | ui.showDetails->setText("Error"); |
| 224 | } |
| 225 | |
| 226 | ui.cancel->setToolTip("Close"); |
| 227 | |
| 228 | emit finished(ui.info->text()); |
| 229 | }); |
| 230 | |
| 231 | ui.showDetails->setStyleSheet("QToolButton { border: 0; color: green; }"); |
| 232 | ui.showDetails->setText("Running"); |
| 233 | } |
| 234 | |
| 235 | JobWidget::~JobWidget() {} |
| 236 | |
| 237 | void JobWidget::showDetails() { ui.showDetails->setChecked(true); } |
| 238 | |
| 239 | void JobWidget::cancel() { |
| 240 | if (!mRunning) { |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | mProcess->kill(); |
| 245 | mProcess->waitForFinished(); |
| 246 | |
| 247 | emit closed(); |
| 248 | } |