| 1 | #include "remote_widget.h" |
| 2 | #include "export_dialog.h" |
| 3 | #include "icon_cache.h" |
| 4 | #include "item_model.h" |
| 5 | #include "list_of_job_options.h" |
| 6 | #include "progress_dialog.h" |
| 7 | #include "transfer_dialog.h" |
| 8 | #include "utils.h" |
| 9 | |
| 10 | RemoteWidget::RemoteWidget(IconCache *iconCache, const QString &remote, |
| 11 | bool isLocal, bool isGoogle, QWidget *parent) |
| 12 | : QWidget(parent) { |
| 13 | ui.setupUi(this); |
| 14 | |
| 15 | QString root = isLocal ? "/" : QString(); |
| 16 | |
| 17 | #ifndef Q_OS_WIN |
| 18 | isLocal = false; |
| 19 | #endif |
| 20 | |
| 21 | auto settings = GetSettings(); |
| 22 | QString rcloneVersion = settings->value("Settings/rcloneVersion").toString(); |
| 23 | settings->setValue("Settings/driveShared", Qt::Unchecked); |
| 24 | ui.tree->setAlternatingRowColors( |
| 25 | settings->value("Settings/rowColors", false).toBool()); |
| 26 | ui.checkBoxShared->setChecked(false); |
| 27 | ui.checkBoxShared->setDisabled(!isGoogle); |
| 28 | // hide checkBoxShared for non Google remotes |
| 29 | if (!isGoogle) { |
| 30 | ui.checkBoxShared->hide(); |
| 31 | } |
| 32 | |
| 33 | QStyle *style = QApplication::style(); |
| 34 | ui.refresh->setIcon(style->standardIcon(QStyle::SP_BrowserReload)); |
| 35 | ui.mkdir->setIcon(style->standardIcon(QStyle::SP_FileDialogNewFolder)); |
| 36 | ui.rename->setIcon(style->standardIcon(QStyle::SP_FileIcon)); |
| 37 | ui.move->setIcon(style->standardIcon(QStyle::SP_DirOpenIcon)); |
| 38 | ui.purge->setIcon(style->standardIcon(QStyle::SP_TrashIcon)); |
| 39 | ui.mount->setIcon(style->standardIcon(QStyle::SP_DriveNetIcon)); |
| 40 | ui.stream->setIcon(style->standardIcon(QStyle::SP_MediaPlay)); |
| 41 | ui.upload->setIcon(style->standardIcon(QStyle::SP_ArrowUp)); |
| 42 | ui.download->setIcon(style->standardIcon(QStyle::SP_ArrowDown)); |
| 43 | ui.download->setIcon(style->standardIcon(QStyle::SP_ArrowDown)); |
| 44 | ui.getSize->setIcon(style->standardIcon(QStyle::SP_FileDialogInfoView)); |
| 45 | ui.getTree->setIcon(style->standardIcon(QStyle::SP_FileDialogListView)); |
| 46 | ui.export_->setIcon(style->standardIcon(QStyle::SP_FileDialogDetailedView)); |
| 47 | ui.link->setIcon(style->standardIcon(QStyle::SP_FileLinkIcon)); |
| 48 | |
| 49 | ui.buttonRefresh->setDefaultAction(ui.refresh); |
| 50 | ui.buttonMkdir->setDefaultAction(ui.mkdir); |
| 51 | ui.buttonRename->setDefaultAction(ui.rename); |
| 52 | ui.buttonMove->setDefaultAction(ui.move); |
| 53 | ui.buttonPurge->setDefaultAction(ui.purge); |
| 54 | ui.buttonMount->setDefaultAction(ui.mount); |
| 55 | ui.buttonStream->setDefaultAction(ui.stream); |
| 56 | ui.buttonUpload->setDefaultAction(ui.upload); |
| 57 | ui.buttonDownload->setDefaultAction(ui.download); |
| 58 | ui.buttonTree->setDefaultAction(ui.getTree); |
| 59 | ui.buttonLink->setDefaultAction(ui.link); |
| 60 | ui.buttonSize->setDefaultAction(ui.getSize); |
| 61 | ui.buttonExport->setDefaultAction(ui.export_); |
| 62 | |
| 63 | ui.tree->sortByColumn(0, Qt::AscendingOrder); |
| 64 | ui.tree->header()->setSectionsMovable(false); |
| 65 | |
| 66 | ItemModel *model = new ItemModel(iconCache, remote, this); |
| 67 | ui.tree->setModel(model); |
| 68 | QTimer::singleShot(0, ui.tree, SLOT(setFocus())); |
| 69 | |
| 70 | QObject::connect(model, &QAbstractItemModel::layoutChanged, this, [=]() { |
| 71 | ui.tree->header()->setSectionResizeMode(0, QHeaderView::Stretch); |
| 72 | ui.tree->resizeColumnToContents(1); |
| 73 | ui.tree->resizeColumnToContents(2); |
| 74 | }); |
| 75 | |
| 76 | QObject::connect( |
| 77 | ui.tree->selectionModel(), &QItemSelectionModel::selectionChanged, this, |
| 78 | [=](const QItemSelection &selection) { |
| 79 | for (auto child : findChildren<QAction *>()) { |
| 80 | child->setDisabled(selection.isEmpty()); |
| 81 | } |
| 82 | |
| 83 | if (selection.isEmpty()) { |
| 84 | ui.path->clear(); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | QModelIndex index = selection.indexes().front(); |
| 89 | |
| 90 | bool topLevel = model->isTopLevel(index); |
| 91 | bool isFolder = model->isFolder(index); |
| 92 | |
| 93 | QDir path; |
| 94 | if (model->isLoading(index)) { |
| 95 | ui.refresh->setDisabled(true); |
| 96 | ui.move->setDisabled(true); |
| 97 | ui.rename->setDisabled(true); |
| 98 | ui.purge->setDisabled(true); |
| 99 | ui.mount->setDisabled(true); |
| 100 | ui.stream->setDisabled(true); |
| 101 | ui.upload->setDisabled(true); |
| 102 | ui.download->setDisabled(true); |
| 103 | ui.checkBoxShared->setDisabled(true); |
| 104 | path = model->path(model->parent(index)); |
| 105 | } else { |
| 106 | ui.refresh->setDisabled(false); |
| 107 | bool driveShared = ui.checkBoxShared->checkState(); |
| 108 | ui.mkdir->setDisabled(driveShared); |
| 109 | ui.rename->setDisabled(topLevel || driveShared); |
| 110 | ui.move->setDisabled(topLevel || driveShared); |
| 111 | ui.purge->setDisabled(topLevel || driveShared); |
| 112 | ui.upload->setDisabled(driveShared); |
| 113 | |
| 114 | #if defined(Q_OS_WIN32) |
| 115 | // check if required version |
| 116 | unsigned int result = |
| 117 | compareVersion(rcloneVersion.toStdString(), "1.50"); |
| 118 | if (result == 2) { |
| 119 | ui.mount->setDisabled(true); |
| 120 | } else { |
| 121 | ui.mount->setDisabled(!isFolder); |
| 122 | }; |
| 123 | #else |
| 124 | // mount is not supported by rclone on these systems |
| 125 | #if defined(Q_OS_OPENBSD) || defined(Q_OS_NETBSD) |
| 126 | ui.mount->setDisabled(true); |
| 127 | #else |
| 128 | ui.mount->setDisabled(!isFolder); |
| 129 | #endif |
| 130 | #endif |
| 131 | |
| 132 | ui.stream->setDisabled(isFolder); |
| 133 | ui.checkBoxShared->setDisabled(!isGoogle); |
| 134 | path = model->path(index); |
| 135 | } |
| 136 | |
| 137 | ui.getSize->setDisabled(!isFolder); |
| 138 | ui.getTree->setDisabled(!isFolder); |
| 139 | ui.export_->setDisabled(!isFolder); |
| 140 | ui.path->setText(isLocal ? QDir::toNativeSeparators(path.path()) |
| 141 | : path.path()); |
| 142 | }); |
| 143 | |
| 144 | QObject::connect(ui.refresh, &QAction::triggered, this, [=]() { |
| 145 | auto settings = GetSettings(); |
| 146 | bool driveShared = ui.checkBoxShared->checkState(); |
| 147 | (driveShared ? settings->setValue("Settings/driveShared", Qt::Checked) |
| 148 | : settings->setValue("Settings/driveShared", Qt::Unchecked)); |
| 149 | |
| 150 | QModelIndex index = ui.tree->selectionModel()->selectedRows().front(); |
| 151 | model->refresh(index); |
| 152 | }); |
| 153 | |
| 154 | QObject::connect(ui.mkdir, &QAction::triggered, this, [=]() { |
| 155 | auto settings = GetSettings(); |
| 156 | bool driveShared = ui.checkBoxShared->checkState(); |
| 157 | (driveShared ? settings->setValue("Settings/driveShared", Qt::Checked) |
| 158 | : settings->setValue("Settings/driveShared", Qt::Unchecked)); |
| 159 | |
| 160 | QModelIndex index = ui.tree->selectionModel()->selectedRows().front(); |
| 161 | |
| 162 | if (!model->isFolder(index)) { |
| 163 | index = index.parent(); |
| 164 | } |
| 165 | QDir path = model->path(index); |
| 166 | QString pathMsg = |
| 167 | isLocal ? QDir::toNativeSeparators(path.path()) : path.path(); |
| 168 | QString name = QInputDialog::getText( |
| 169 | this, "New Folder", QString("Create folder in %1").arg(pathMsg)); |
| 170 | if (!name.isEmpty()) { |
| 171 | QString folder = path.filePath(name); |
| 172 | QString folderMsg = isLocal ? QDir::toNativeSeparators(folder) : folder; |
| 173 | |
| 174 | QProcess process; |
| 175 | UseRclonePassword(&process); |
| 176 | process.setProgram(GetRclone()); |
| 177 | process.setArguments(QStringList() << "mkdir" << GetRcloneConf() |
| 178 | << GetDriveSharedWithMe() |
| 179 | << GetDefaultRcloneOptionsList() |
| 180 | << remote + ":" + folder); |
| 181 | process.setProcessChannelMode(QProcess::MergedChannels); |
| 182 | |
| 183 | ProgressDialog progress("New Folder", "Creating...", folderMsg, &process, |
| 184 | this); |
| 185 | if (progress.exec() == QDialog::Accepted) { |
| 186 | model->refresh(index); |
| 187 | } |
| 188 | } |
| 189 | }); |
| 190 | |
| 191 | QObject::connect(ui.rename, &QAction::triggered, this, [=]() { |
| 192 | auto settings = GetSettings(); |
| 193 | bool driveShared = ui.checkBoxShared->checkState(); |
| 194 | (driveShared ? settings->setValue("Settings/driveShared", Qt::Checked) |
| 195 | : settings->setValue("Settings/driveShared", Qt::Unchecked)); |
| 196 | |
| 197 | QModelIndex index = ui.tree->selectionModel()->selectedRows().front(); |
| 198 | |
| 199 | QString path = model->path(index).path(); |
| 200 | QString pathMsg = isLocal ? QDir::toNativeSeparators(path) : path; |
| 201 | |
| 202 | QString name = model->data(index, Qt::DisplayRole).toString(); |
| 203 | name = QInputDialog::getText(this, "Rename", |
| 204 | QString("New name for %1").arg(pathMsg), |
| 205 | QLineEdit::Normal, name); |
| 206 | if (!name.isEmpty()) { |
| 207 | QProcess process; |
| 208 | UseRclonePassword(&process); |
| 209 | process.setProgram(GetRclone()); |
| 210 | process.setArguments( |
| 211 | QStringList() << "moveto" << GetRcloneConf() << GetDriveSharedWithMe() |
| 212 | << GetDefaultRcloneOptionsList() << remote + ":" + path |
| 213 | << remote + ":" + |
| 214 | model->path(index.parent()).filePath(name)); |
| 215 | process.setProcessChannelMode(QProcess::MergedChannels); |
| 216 | |
| 217 | ProgressDialog progress("Rename", "Renaming...", pathMsg, &process, this); |
| 218 | if (progress.exec() == QDialog::Accepted) { |
| 219 | model->rename(index, name); |
| 220 | } |
| 221 | } |
| 222 | }); |
| 223 | |
| 224 | QObject::connect(ui.move, &QAction::triggered, this, [=]() { |
| 225 | auto settings = GetSettings(); |
| 226 | bool driveShared = ui.checkBoxShared->checkState(); |
| 227 | (driveShared ? settings->setValue("Settings/driveShared", Qt::Checked) |
| 228 | : settings->setValue("Settings/driveShared", Qt::Unchecked)); |
| 229 | |
| 230 | QModelIndex index = ui.tree->selectionModel()->selectedRows().front(); |
| 231 | |
| 232 | QString path = model->path(index).path(); |
| 233 | QString pathMsg = isLocal ? QDir::toNativeSeparators(path) : path; |
| 234 | |
| 235 | QString name = model->path(index.parent()).path() + "/"; |
| 236 | name = QInputDialog::getText(this, "Move", |
| 237 | QString("New location for %1").arg(pathMsg), |
| 238 | QLineEdit::Normal, name); |
| 239 | if (!name.isEmpty()) { |
| 240 | QProcess process; |
| 241 | UseRclonePassword(&process); |
| 242 | process.setProgram(GetRclone()); |
| 243 | process.setArguments( |
| 244 | QStringList() << "move" << GetRcloneConf() << GetDriveSharedWithMe() |
| 245 | << GetDefaultRcloneOptionsList() << remote + ":" + path |
| 246 | << remote + ":" + name); |
| 247 | process.setProcessChannelMode(QProcess::MergedChannels); |
| 248 | |
| 249 | ProgressDialog progress("Move", "Moving...", pathMsg, &process, this); |
| 250 | if (progress.exec() == QDialog::Accepted) { |
| 251 | model->refresh(index); |
| 252 | } |
| 253 | } |
| 254 | }); |
| 255 | |
| 256 | QObject::connect(ui.purge, &QAction::triggered, this, [=]() { |
| 257 | auto settings = GetSettings(); |
| 258 | bool driveShared = ui.checkBoxShared->checkState(); |
| 259 | (driveShared ? settings->setValue("Settings/driveShared", Qt::Checked) |
| 260 | : settings->setValue("Settings/driveShared", Qt::Unchecked)); |
| 261 | |
| 262 | QModelIndex index = ui.tree->selectionModel()->selectedRows().front(); |
| 263 | |
| 264 | QString path = model->path(index).path(); |
| 265 | QString pathMsg = isLocal ? QDir::toNativeSeparators(path) : path; |
| 266 | |
| 267 | int button = QMessageBox::question( |
| 268 | this, "Delete", |
| 269 | QString("Are you sure you want to delete %1 ?").arg(pathMsg), |
| 270 | QMessageBox::Yes | QMessageBox::No); |
| 271 | if (button == QMessageBox::Yes) { |
| 272 | QProcess process; |
| 273 | UseRclonePassword(&process); |
| 274 | process.setProgram(GetRclone()); |
| 275 | process.setArguments(QStringList() |
| 276 | << (model->isFolder(index) ? "purge" : "delete") |
| 277 | << GetRcloneConf() << GetDriveSharedWithMe() |
| 278 | << GetDefaultRcloneOptionsList() |
| 279 | << remote + ":" + path); |
| 280 | process.setProcessChannelMode(QProcess::MergedChannels); |
| 281 | |
| 282 | ProgressDialog progress("Delete", "Deleting...", pathMsg, &process, this); |
| 283 | if (progress.exec() == QDialog::Accepted) { |
| 284 | QModelIndex parent = index.parent(); |
| 285 | QModelIndex next = parent.model()->index(index.row() + 1, 0); |
| 286 | ui.tree->selectionModel()->select(next.isValid() ? next : parent, |
| 287 | QItemSelectionModel::SelectCurrent); |
| 288 | model->removeRow(index.row(), parent); |
| 289 | } |
| 290 | } |
| 291 | }); |
| 292 | |
| 293 | QObject::connect(ui.mount, &QAction::triggered, this, [=]() { |
| 294 | auto settings = GetSettings(); |
| 295 | bool driveShared = ui.checkBoxShared->checkState(); |
| 296 | (driveShared ? settings->setValue("Settings/driveShared", Qt::Checked) |
| 297 | : settings->setValue("Settings/driveShared", Qt::Unchecked)); |
| 298 | |
| 299 | QModelIndex index = ui.tree->selectionModel()->selectedRows().front(); |
| 300 | |
| 301 | QString path = model->path(index).path(); |
| 302 | QString pathMsg = isLocal ? QDir::toNativeSeparators(path) : path; |
| 303 | |
| 304 | #if defined(Q_OS_WIN32) |
| 305 | QString folder = |
| 306 | QInputDialog::getText(this, "Mount", |
| 307 | QString("(Make sure you have WinFsp-FUSE " |
| 308 | "installed)\n\nDrive to mount %1 to") |
| 309 | .arg(remote), |
| 310 | QLineEdit::Normal, "Z:"); |
| 311 | #else |
| 312 | QString folder = QFileDialog::getExistingDirectory(this, QString("Mount %1").arg(remote)); |
| 313 | #endif |
| 314 | |
| 315 | if (!folder.isEmpty()) { |
| 316 | emit addMount(remote + ":" + path, folder); |
| 317 | } |
| 318 | }); |
| 319 | |
| 320 | QObject::connect(ui.stream, &QAction::triggered, this, [=]() { |
| 321 | auto settings = GetSettings(); |
| 322 | bool driveShared = ui.checkBoxShared->checkState(); |
| 323 | (driveShared ? settings->setValue("Settings/driveShared", Qt::Checked) |
| 324 | : settings->setValue("Settings/driveShared", Qt::Unchecked)); |
| 325 | |
| 326 | QModelIndex index = ui.tree->selectionModel()->selectedRows().front(); |
| 327 | QString path = model->path(index).path(); |
| 328 | |
| 329 | bool streamConfirmed = |
| 330 | settings->value("Settings/streamConfirmed", false).toBool(); |
| 331 | QString stream = settings->value("Settings/stream", "mpv -").toString(); |
| 332 | if (!streamConfirmed) { |
| 333 | QString result = QInputDialog::getText( |
| 334 | this, "Stream", |
| 335 | "Enter stream command (file will be passed in STDIN):", |
| 336 | QLineEdit::Normal, stream); |
| 337 | if (result.isEmpty()) { |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | stream = result; |
| 342 | |
| 343 | settings->setValue("Settings/stream", stream); |
| 344 | settings->setValue("Settings/streamConfirmed", true); |
| 345 | } |
| 346 | |
| 347 | emit addStream(remote + ":" + path, stream); |
| 348 | }); |
| 349 | |
| 350 | QObject::connect(ui.checkBoxShared, &QCheckBox::toggled, ui.shared, |
| 351 | &QAction::toggled); |
| 352 | |
| 353 | QObject::connect(ui.shared, &QAction::toggled, this, [=](const bool checked) { |
| 354 | auto settings = GetSettings(); |
| 355 | settings->setValue("Settings/driveShared", checked); |
| 356 | ui.checkBoxShared->setChecked(checked); |
| 357 | |
| 358 | QModelIndex index = ui.tree->selectionModel()->selectedRows().front(); |
| 359 | QModelIndex top = index; |
| 360 | while (!model->isTopLevel(top)) { |
| 361 | top = top.parent(); |
| 362 | } |
| 363 | ui.tree->selectionModel()->clear(); |
| 364 | ui.tree->selectionModel()->select(top, QItemSelectionModel::Select | |
| 365 | QItemSelectionModel::Rows); |
| 366 | model->refresh(top); |
| 367 | }); |
| 368 | |
| 369 | QObject::connect(ui.link, &QAction::triggered, this, [=]() { |
| 370 | auto settings = GetSettings(); |
| 371 | bool driveShared = ui.checkBoxShared->checkState(); |
| 372 | (driveShared ? settings->setValue("Settings/driveShared", Qt::Checked) |
| 373 | : settings->setValue("Settings/driveShared", Qt::Unchecked)); |
| 374 | |
| 375 | QModelIndex index = ui.tree->selectionModel()->selectedRows().front(); |
| 376 | |
| 377 | QString path = model->path(index).path(); |
| 378 | QString pathMsg = isLocal ? QDir::toNativeSeparators(path) : path; |
| 379 | |
| 380 | QProcess process; |
| 381 | UseRclonePassword(&process); |
| 382 | process.setProgram(GetRclone()); |
| 383 | process.setArguments( |
| 384 | QStringList() << "link" << GetRcloneConf() << GetDriveSharedWithMe() |
| 385 | << GetDefaultRcloneOptionsList() << remote + ":" + path); |
| 386 | process.setProcessChannelMode(QProcess::MergedChannels); |
| 387 | ProgressDialog progress("Fetch Public Link", "Fetching link for...", |
| 388 | pathMsg, &process, this, false, true); |
| 389 | progress.expand(); |
| 390 | progress.allowToClose(); |
| 391 | progress.exec(); |
| 392 | }); |
| 393 | |
| 394 | QObject::connect(ui.upload, &QAction::triggered, this, [=]() { |
| 395 | auto settings = GetSettings(); |
| 396 | bool driveShared = ui.checkBoxShared->checkState(); |
| 397 | (driveShared ? settings->setValue("Settings/driveShared", Qt::Checked) |
| 398 | : settings->setValue("Settings/driveShared", Qt::Unchecked)); |
| 399 | |
| 400 | QModelIndex index = ui.tree->selectionModel()->selectedRows().front(); |
| 401 | |
| 402 | if (!model->isFolder(index)) { |
| 403 | index = index.parent(); |
| 404 | } |
| 405 | QDir path = model->path(index); |
| 406 | |
| 407 | TransferDialog t(false, false, remote, path, true, this); |
| 408 | if (t.exec() == QDialog::Accepted) { |
| 409 | QString src = t.getSource(); |
| 410 | QString dst = t.getDest(); |
| 411 | |
| 412 | QStringList args = t.getOptions(); |
| 413 | emit addTransfer(QString("%1 from %2").arg(t.getMode()).arg(src), src, |
| 414 | dst, args); |
| 415 | } |
| 416 | }); |
| 417 | |
| 418 | QObject::connect(ui.download, &QAction::triggered, this, [=]() { |
| 419 | auto settings = GetSettings(); |
| 420 | bool driveShared = ui.checkBoxShared->checkState(); |
| 421 | (driveShared ? settings->setValue("Settings/driveShared", Qt::Checked) |
| 422 | : settings->setValue("Settings/driveShared", Qt::Unchecked)); |
| 423 | |
| 424 | QModelIndex index = ui.tree->selectionModel()->selectedRows().front(); |
| 425 | QDir path = model->path(index); |
| 426 | |
| 427 | TransferDialog t(true, false, remote, path, model->isFolder(index), this); |
| 428 | if (t.exec() == QDialog::Accepted) { |
| 429 | QString src = t.getSource(); |
| 430 | QString dst = t.getDest(); |
| 431 | |
| 432 | QStringList args = t.getOptions(); |
| 433 | emit addTransfer(QString("%1 %2").arg(t.getMode()).arg(src), src, dst, |
| 434 | args); |
| 435 | } |
| 436 | }); |
| 437 | |
| 438 | QObject::connect(ui.getTree, &QAction::triggered, this, [=]() { |
| 439 | auto settings = GetSettings(); |
| 440 | bool driveShared = ui.checkBoxShared->checkState(); |
| 441 | (driveShared ? settings->setValue("Settings/driveShared", Qt::Checked) |
| 442 | : settings->setValue("Settings/driveShared", Qt::Unchecked)); |
| 443 | QModelIndex index = ui.tree->selectionModel()->selectedRows().front(); |
| 444 | |
| 445 | QString path = model->path(index).path(); |
| 446 | QString pathMsg = isLocal ? QDir::toNativeSeparators(path) : path; |
| 447 | |
| 448 | QProcess process; |
| 449 | UseRclonePassword(&process); |
| 450 | process.setProgram(GetRclone()); |
| 451 | process.setArguments( |
| 452 | QStringList() << "tree" |
| 453 | << "-d" << GetRcloneConf() << GetDriveSharedWithMe() |
| 454 | << GetDefaultRcloneOptionsList() << remote + ":" + path); |
| 455 | process.setProcessChannelMode(QProcess::MergedChannels); |
| 456 | ProgressDialog progress("Show directories tree", "Processing...", pathMsg, |
| 457 | &process, this, false); |
| 458 | progress.expand(); |
| 459 | progress.allowToClose(); |
| 460 | progress.resize(1000, 600); |
| 461 | progress.exec(); |
| 462 | }); |
| 463 | |
| 464 | QObject::connect(ui.getSize, &QAction::triggered, this, [=]() { |
| 465 | auto settings = GetSettings(); |
| 466 | bool driveShared = ui.checkBoxShared->checkState(); |
| 467 | (driveShared ? settings->setValue("Settings/driveShared", Qt::Checked) |
| 468 | : settings->setValue("Settings/driveShared", Qt::Unchecked)); |
| 469 | QModelIndex index = ui.tree->selectionModel()->selectedRows().front(); |
| 470 | |
| 471 | QString path = model->path(index).path(); |
| 472 | QString pathMsg = isLocal ? QDir::toNativeSeparators(path) : path; |
| 473 | |
| 474 | QProcess process; |
| 475 | UseRclonePassword(&process); |
| 476 | process.setProgram(GetRclone()); |
| 477 | process.setArguments( |
| 478 | QStringList() << "size" << GetRcloneConf() << GetDriveSharedWithMe() |
| 479 | << GetDefaultRcloneOptionsList() << remote + ":" + path); |
| 480 | process.setProcessChannelMode(QProcess::MergedChannels); |
| 481 | ProgressDialog progress("Get Size", "Calculating...", pathMsg, &process, |
| 482 | this, false); |
| 483 | progress.expand(); |
| 484 | progress.allowToClose(); |
| 485 | progress.exec(); |
| 486 | }); |
| 487 | |
| 488 | QObject::connect(ui.export_, &QAction::triggered, this, [=]() { |
| 489 | auto settings = GetSettings(); |
| 490 | bool driveShared = ui.checkBoxShared->checkState(); |
| 491 | (driveShared ? settings->setValue("Settings/driveShared", Qt::Checked) |
| 492 | : settings->setValue("Settings/driveShared", Qt::Unchecked)); |
| 493 | |
| 494 | QModelIndex index = ui.tree->selectionModel()->selectedRows().front(); |
| 495 | QDir path = model->path(index); |
| 496 | ExportDialog e(remote, path, this); |
| 497 | if (e.exec() == QDialog::Accepted) { |
| 498 | QString dst = e.getDestination(); |
| 499 | bool txt = e.onlyFilenames(); |
| 500 | |
| 501 | QFile *file = new QFile(dst); |
| 502 | if (!file->open(QFile::WriteOnly)) { |
| 503 | QMessageBox::warning( |
| 504 | this, "Error", |
| 505 | QString("Cannot open file '%1' for writing!").arg(dst)); |
| 506 | delete file; |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | QRegExp re(R"(^(\d+) (\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d)\.\d+ (.+)$)"); |
| 511 | |
| 512 | QProcess process; |
| 513 | UseRclonePassword(&process); |
| 514 | process.setProgram(GetRclone()); |
| 515 | process.setArguments(QStringList() |
| 516 | << GetRcloneConf() << GetDriveSharedWithMe() |
| 517 | << GetDefaultRcloneOptionsList() << e.getOptions()); |
| 518 | process.setProcessChannelMode(QProcess::MergedChannels); |
| 519 | |
| 520 | ProgressDialog progress("Export", "Exporting...", dst, &process, this); |
| 521 | file->setParent(&progress); |
| 522 | |
| 523 | QObject::connect(&progress, &ProgressDialog::outputAvailable, this, |
| 524 | [=](const QString &output) { |
| 525 | QTextStream out(file); |
| 526 | |
| 527 | for (const auto &line : output.split('\n')) { |
| 528 | if (re.exactMatch(line.trimmed())) { |
| 529 | QStringList cap = re.capturedTexts(); |
| 530 | |
| 531 | if (txt) { |
| 532 | out << cap[3] << '\n'; |
| 533 | } else { |
| 534 | QString name = cap[3]; |
| 535 | if (name.contains(' ') || name.contains(',') || |
| 536 | name.contains('"')) { |
| 537 | name = '"' + name.replace("\"", "\"\"") + '"'; |
| 538 | } |
| 539 | out << name << ',' << '"' << cap[2] << '"' << ',' |
| 540 | << cap[1].toULongLong() << '\n'; |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | }); |
| 545 | |
| 546 | progress.exec(); |
| 547 | } |
| 548 | }); |
| 549 | |
| 550 | QObject::connect( |
| 551 | model, &ItemModel::drop, this, |
| 552 | [=](const QDir &path, const QModelIndex &parent) { |
| 553 | auto settings = GetSettings(); |
| 554 | bool driveShared = ui.checkBoxShared->checkState(); |
| 555 | (driveShared |
| 556 | ? settings->setValue("Settings/driveShared", Qt::Checked) |
| 557 | : settings->setValue("Settings/driveShared", Qt::Unchecked)); |
| 558 | |
| 559 | qApp->setActiveWindow(this); |
| 560 | QDir destPath = model->path(parent); |
| 561 | QString dest = QFileInfo(path.path()).isDir() |
| 562 | ? destPath.filePath(path.dirName()) |
| 563 | : destPath.path(); |
| 564 | |
| 565 | TransferDialog t(false, true, remote, dest, true, this); |
| 566 | t.setSource(path.path()); |
| 567 | |
| 568 | if (t.exec() == QDialog::Accepted) { |
| 569 | QString src = t.getSource(); |
| 570 | QString dst = t.getDest(); |
| 571 | |
| 572 | QStringList args = t.getOptions(); |
| 573 | emit addTransfer(QString("%1 from %2").arg(t.getMode()).arg(src), src, |
| 574 | dst, args); |
| 575 | } |
| 576 | }); |
| 577 | |
| 578 | QObject::connect( |
| 579 | ui.tree, &QWidget::customContextMenuRequested, this, |
| 580 | [=](const QPoint &pos) { |
| 581 | auto settings = GetSettings(); |
| 582 | bool driveShared = ui.checkBoxShared->checkState(); |
| 583 | (driveShared |
| 584 | ? settings->setValue("Settings/driveShared", Qt::Checked) |
| 585 | : settings->setValue("Settings/driveShared", Qt::Unchecked)); |
| 586 | |
| 587 | QMenu menu; |
| 588 | menu.addAction(ui.refresh); |
| 589 | menu.addAction(ui.getSize); |
| 590 | menu.addAction(ui.getTree); |
| 591 | menu.addAction(ui.export_); |
| 592 | menu.addSeparator(); |
| 593 | menu.addAction(ui.mkdir); |
| 594 | menu.addAction(ui.rename); |
| 595 | menu.addAction(ui.move); |
| 596 | menu.addAction(ui.purge); |
| 597 | menu.addSeparator(); |
| 598 | menu.addAction(ui.mount); |
| 599 | menu.addAction(ui.stream); |
| 600 | menu.addAction(ui.upload); |
| 601 | menu.addAction(ui.download); |
| 602 | menu.addAction(ui.link); |
| 603 | menu.exec(ui.tree->viewport()->mapToGlobal(pos)); |
| 604 | }); |
| 605 | |
| 606 | if (isLocal) { |
| 607 | QHash<QString, QPersistentModelIndex> drives; |
| 608 | |
| 609 | // QDir::drives is fast |
| 610 | for (const auto &drive : QDir::drives()) { |
| 611 | QString path = drive.path(); |
| 612 | QModelIndex index = model->addRoot(QDir::toNativeSeparators(path), path); |
| 613 | drives.insert(path, index); |
| 614 | } |
| 615 | |
| 616 | #if (QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)) && !(defined Q_OS_WIN) |
| 617 | QThread *thread = new QThread(this); |
| 618 | thread->start(); |
| 619 | |
| 620 | QObject *worker = new QObject(); |
| 621 | worker->moveToThread(thread); |
| 622 | |
| 623 | QTimer::singleShot(0, worker, [=]() { |
| 624 | QStorageInfo info; |
| 625 | info.refresh(); |
| 626 | |
| 627 | // QStorageInfo::mountedVolumes is slow :( |
| 628 | for (const auto &volume : info.mountedVolumes()) { |
| 629 | QString name = volume.name(); |
| 630 | if (!name.isEmpty()) { |
| 631 | QString path = volume.rootPath(); |
| 632 | QString item = |
| 633 | QString("%1 (%2)").arg(QDir::toNativeSeparators(path)).arg(name); |
| 634 | QTimer::singleShot(0, this, |
| 635 | [=]() { model->rename(drives[path], item); }); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | thread->quit(); |
| 640 | thread->deleteLater(); |
| 641 | worker->deleteLater(); |
| 642 | }); |
| 643 | #endif |
| 644 | |
| 645 | ui.tree->selectionModel()->selectionChanged(QItemSelection(), |
| 646 | QItemSelection()); |
| 647 | } else { |
| 648 | QModelIndex index = model->addRoot("/", root); |
| 649 | ui.tree->selectionModel()->select( |
| 650 | index, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows); |
| 651 | ui.tree->expand(index); |
| 652 | } |
| 653 | |
| 654 | QShortcut *close = new QShortcut(QKeySequence::Close, this); |
| 655 | QObject::connect(close, &QShortcut::activated, this, [=]() { |
| 656 | auto tabs = qobject_cast<QTabWidget *>(parent); |
| 657 | tabs->removeTab(tabs->indexOf(this)); |
| 658 | }); |
| 659 | } |
| 660 | |
| 661 | RemoteWidget::~RemoteWidget() {} |