From dbe1e9b2fb16ebf25dcb829e066fd9f8690d4283 Mon Sep 17 00:00:00 2001 From: Arno Date: Fri, 4 Mar 2016 20:06:35 +0100 Subject: Now MoveToUSB does something! Make it work. Lessons learned: Don't keep a QProgressDialog around. Use it and delete it later. Otherwise it will show up spontaneously. --- archivebrowser.cpp | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) (limited to 'archivebrowser.cpp') diff --git a/archivebrowser.cpp b/archivebrowser.cpp index 6b83d99..fbeb50d 100644 --- a/archivebrowser.cpp +++ b/archivebrowser.cpp @@ -18,9 +18,11 @@ #include #include #include +#include #include "archivebrowser.h" #include "archivebrowsermodel.h" +#include "copyworker.h" #include "smtreeview.h" #include "smglobals.h" #include "pictureviewer2.h" @@ -74,6 +76,11 @@ ArchiveBrowser::ArchiveBrowser(QWidget *parent) : QWidget(parent), mSelectedSize mainLayout->addWidget(mTree); setLayout(mainLayout); mTree->setSortingEnabled(true); + + //copyworker + mUSBProgress = 0; + mCopyWorker = new CopyWorker(this); + } void ArchiveBrowser::browserSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected) { @@ -154,7 +161,67 @@ void ArchiveBrowser::moveToBurn() { } void ArchiveBrowser::moveToUSB(){ - + QModelIndexList sel = mTree->selectionModel()->selectedRows(); + if(sel.isEmpty()){ + return; + } + QSettings s; + QString destDirS = s.value("paths/usb").toString(); + QFileInfo destFi(destDirS); + if(!destFi.exists() || !destFi.isDir()){ + QString msg = QString(tr("%1 does not exist or isn't a directory!")).arg(destFi.absoluteFilePath()); + QMessageBox::critical(this, tr("Error"), msg); + return; + } + int nextDVDNo = mModel->nextDVDNo(); + QString dvdDirS = QString("DVD_%1").arg(QString::number(nextDVDNo)); + QDir dest(destDirS); + if(dest.exists(dvdDirS)){ + QString msg = QString(tr("Something fishy is going on: %1 already exists in %2!")).arg(dvdDirS).arg(destDirS); + QMessageBox::critical(this, tr("Error"), msg); + return; + } + bool mkdir = dest.mkdir(dvdDirS); + if(!mkdir){ + QString msg = QString(tr("Failed to create %1 in %2!")).arg(dvdDirS).arg(destDirS); + QMessageBox::critical(this, tr("Error"), msg); + return; + } + // this one is .../DVD_123 + QString finalDir = QString("%1/%2").arg(destDirS).arg(dvdDirS); + QString msg = QString(tr("

This will do the following:

  • Move %1 file(s) to %2
  • Update the DVD no. for %1 files

Continue?

")).arg(sel.size()).arg(finalDir); + int retval = QMessageBox::question(this, tr("Question"), msg, QMessageBox::Yes | QMessageBox::No); + if(retval == QMessageBox::Yes){ + mCopyWorker->clear(); + QDir finalD(finalDir); + foreach(QModelIndex idx, sel){ + QString dirName = idx.data(ArchiveBrowserModel::NameRole).toString(); + dirName.replace(' ', '.'); + // this one .../DVD_123/series_name + // don't check for success, it may already exist! + finalD.mkdir(dirName); + QModelIndex real = mProxy->mapToSource(idx); + QModelIndexList children = mModel->children(real); + foreach(QModelIndex child, children){ + QString source = child.data(ArchiveBrowserModel::FullPathRole).toString(); + QFileInfo sFi(source); + QString destination = QString("%1/%2/%3").arg(finalDir).arg(dirName).arg(sFi.fileName()); + mCopyWorker->enqueue(source, destination); + mCopyWorker->appendData(source, child.data(ArchiveBrowserModel::FileTypeRole)); + mCopyWorker->appendData(source, child.data(ArchiveBrowserModel::GenericIdRole)); + } + } + mUSBProgress = new QProgressDialog(this); + connect(mCopyWorker, SIGNAL(bytesRead(int)), mUSBProgress, SLOT(setValue(int))); + connect(mCopyWorker, SIGNAL(error(QString)), this, SLOT(copyError(QString))); + connect(mCopyWorker, SIGNAL(success(QString)), this, SLOT(copySuccess(QString))); + connect(mCopyWorker, SIGNAL(file(QString)), this, SLOT(setCopyFile(QString))); + mUSBProgress->setLabelText(tr("Copying files...")); + mUSBProgress->setMinimum(0); + mUSBProgress->setMaximum(mCopyWorker->max()); + mUSBProgress->show(); + mCopyWorker->start(); + } } void ArchiveBrowser::refresh() { @@ -219,6 +286,41 @@ void ArchiveBrowser::resetAll() { emit itemCountChanged(0); } +void ArchiveBrowser::copyError(QString error){ + mUSBProgress->hide(); + QMessageBox::critical(this, tr("Copy Error"), error); + mUSBProgress->disconnect(); + mUSBProgress->deleteLater(); +} + +void ArchiveBrowser::copySuccess(QString success){ + mUSBProgress->hide(); + QMessageBox::information(this, tr("Copy Success"), success); + mUSBProgress->disconnect(); + mUSBProgress->deleteLater(); + QString msg = QString(tr("Delete source files and update database?")); + int q = QMessageBox::question(this, tr("Question"), msg); + if(q == QMessageBox::Yes){ + QHash > data = mCopyWorker->data(); + QList filesToUpdate; + foreach(QString source, data.keys()){ + int ft = data.value(source).at(0).toInt(); + if(ft == FT_MOVIE){ + QFile::remove(source); + filesToUpdate << data.value(source).at(1).toInt(); + } + } + mModel->updateDVDNo(filesToUpdate); + mModel->refresh(); + mProxy->setBytesRemaining(0); + } +} + +void ArchiveBrowser::setCopyFile(QString file){ + QString msg = QString(tr("Copying %1")).arg(file); + mUSBProgress->setLabelText(msg); +} + QModelIndexList ArchiveBrowser::selectedRows(const QItemSelection &sel){ QModelIndexList retval; QModelIndexList selIdx = sel.indexes(); -- cgit v1.2.3-70-g09d2