| 1 | #include "mount_widget.h" |
| 2 | #include "utils.h" |
| 3 | |
| 4 | MountWidget::MountWidget(QProcess *process, const QString &remote, |
| 5 | const QString &folder, QWidget *parent) |
| 6 | : QWidget(parent), mProcess(process) { |
| 7 | ui.setupUi(this); |
| 8 | |
| 9 | ui.remote->setText(remote); |
| 10 | ui.folder->setText(folder); |
| 11 | ui.info->setText(QString("%1 on %2").arg(remote).arg(folder)); |
| 12 | |
| 13 | ui.details->setVisible(false); |
| 14 | |
| 15 | ui.output->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); |
| 16 | ui.output->setVisible(false); |
| 17 | |
| 18 | QObject::connect( |
| 19 | ui.showDetails, &QToolButton::toggled, this, [=](bool checked) { |
| 20 | ui.details->setVisible(checked); |
| 21 | ui.showDetails->setArrowType(checked ? Qt::DownArrow : Qt::RightArrow); |
| 22 | }); |
| 23 | |
| 24 | QObject::connect( |
| 25 | ui.showOutput, &QToolButton::toggled, this, [=](bool checked) { |
| 26 | ui.output->setVisible(checked); |
| 27 | ui.showOutput->setArrowType(checked ? Qt::DownArrow : Qt::RightArrow); |
| 28 | }); |
| 29 | |
| 30 | ui.cancel->setIcon( |
| 31 | QApplication::style()->standardIcon(QStyle::SP_DialogCloseButton)); |
| 32 | |
| 33 | QObject::connect(ui.cancel, &QToolButton::clicked, this, [=]() { |
| 34 | if (mRunning) { |
| 35 | int button = QMessageBox::question( |
| 36 | this, "Unmount", |
| 37 | #if defined(Q_OS_WIN) |
| 38 | QString("Do you want to unmount %1 drive?").arg(folder), |
| 39 | #else |
| 40 | QString("Do you want to unmount %1 folder?").arg(folder), |
| 41 | #endif |
| 42 | QMessageBox::Yes | QMessageBox::No); |
| 43 | if (button == QMessageBox::Yes) { |
| 44 | cancel(); |
| 45 | } |
| 46 | } else { |
| 47 | emit closed(); |
| 48 | } |
| 49 | }); |
| 50 | |
| 51 | QObject::connect(mProcess, &QProcess::readyRead, this, [=]() { |
| 52 | while (mProcess->canReadLine()) { |
| 53 | ui.output->appendPlainText(mProcess->readLine().trimmed()); |
| 54 | } |
| 55 | }); |
| 56 | |
| 57 | QObject::connect(mProcess, |
| 58 | static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>( |
| 59 | &QProcess::finished), |
| 60 | this, [=](int status, QProcess::ExitStatus) { |
| 61 | mProcess->deleteLater(); |
| 62 | mRunning = false; |
| 63 | if (status == 0) { |
| 64 | ui.showDetails->setStyleSheet( |
| 65 | "QToolButton { border: 0; color: black; }"); |
| 66 | ui.showDetails->setText("Finished"); |
| 67 | } else { |
| 68 | ui.showDetails->setStyleSheet( |
| 69 | "QToolButton { border: 0; color: red; }"); |
| 70 | ui.showDetails->setText("Error"); |
| 71 | } |
| 72 | ui.cancel->setToolTip("Close"); |
| 73 | emit finished(); |
| 74 | }); |
| 75 | |
| 76 | ui.showDetails->setStyleSheet("QToolButton { border: 0; color: green; }"); |
| 77 | ui.showDetails->setText("Mounted"); |
| 78 | } |
| 79 | |
| 80 | MountWidget::~MountWidget() {} |
| 81 | |
| 82 | void MountWidget::cancel() { |
| 83 | if (!mRunning) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | QString cmd; |
| 88 | |
| 89 | #if defined(Q_OS_MACOS) || defined(Q_OS_FREEBSD) |
| 90 | QProcess::startDetached("umount", QStringList() << ui.folder->text()); |
| 91 | #else |
| 92 | #if defined(Q_OS_WIN32) |
| 93 | QProcess *p = new QProcess(); |
| 94 | QStringList args; |
| 95 | args << "rc"; |
| 96 | // requires rlone version at least 1.50 |
| 97 | args << "core/quit"; |
| 98 | |
| 99 | args << "--rc-addr"; |
| 100 | QString folder = ui.folder->text(); |
| 101 | |
| 102 | int port_offset = folder[0].toLatin1(); |
| 103 | unsigned short int rclone_rc_port_base = 19000; |
| 104 | unsigned short int rclone_rc_port = rclone_rc_port_base + port_offset; |
| 105 | args << "localhost:" + QVariant(rclone_rc_port).toString(); |
| 106 | |
| 107 | UseRclonePassword(p); |
| 108 | p->start(GetRclone(), args, QIODevice::ReadOnly); |
| 109 | #else |
| 110 | QProcess::startDetached("fusermount", QStringList() |
| 111 | << "-u" << ui.folder->text()); |
| 112 | #endif |
| 113 | #endif |
| 114 | |
| 115 | mProcess->waitForFinished(); |
| 116 | |
| 117 | emit closed(); |
| 118 | } |