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 +++++++++++++++++++++++++++++++++++++++++++++++-
archivebrowser.h | 7 ++++
archivebrowsermodel.cpp | 10 +++++
archivebrowsermodel.h | 1 +
copyworker.cpp | 59 +++++++++++++--------------
copyworker.h | 15 ++++---
6 files changed, 158 insertions(+), 38 deletions(-)
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();
diff --git a/archivebrowser.h b/archivebrowser.h
index f318eab..f895d21 100644
--- a/archivebrowser.h
+++ b/archivebrowser.h
@@ -17,11 +17,13 @@
class ArchiveBrowserModel;
class ArchiveBrowserModelProxy;
class SmTreeView;
+class CopyWorker;
class QSortFilterProxyModel;
class QComboBox;
class QCheckBox;
class QToolBar;
class QSplitter;
+class QProgressDialog;
class ArchiveBrowser : public QWidget {
Q_OBJECT
@@ -43,6 +45,9 @@ class ArchiveBrowser : public QWidget {
private slots:
void setupQualityFilter();
void resetAll();
+ void copyError(QString);
+ void copySuccess(QString);
+ void setCopyFile(QString);
signals:
void sizeChanged(qint64 size);
@@ -51,10 +56,12 @@ class ArchiveBrowser : public QWidget {
private:
QModelIndexList selectedRows(const QItemSelection &sel);
SmTreeView *mTree;
+ CopyWorker *mCopyWorker;
QComboBox *mQualityFilter;
QCheckBox *mSizeFilter;
QToolBar *mToolBar;
QSplitter *mTbSplitter;
+ QProgressDialog *mUSBProgress;
ArchiveBrowserModel *mModel;
ArchiveBrowserModelProxy *mProxy;
qint64 mSelectedSize;
diff --git a/archivebrowsermodel.cpp b/archivebrowsermodel.cpp
index 202a406..7bfa2df 100644
--- a/archivebrowsermodel.cpp
+++ b/archivebrowsermodel.cpp
@@ -122,6 +122,16 @@ void ArchiveBrowserModel::updateDVDNo(const QList fileNos) {
}
}
+int ArchiveBrowserModel::nextDVDNo() const {
+ int maxdvd = 0;
+ QSqlQuery maxDvdQ = QSqlQuery("SELECT max(idvd) FROM files", mDb);
+ while(maxDvdQ.next()){
+ maxdvd = maxDvdQ.value(0).toInt();
+ }
+ ++maxdvd;
+ return maxdvd;
+}
+
QModelIndexList ArchiveBrowserModel::children(const QModelIndex &idx){
if(!idx.isValid()){
return QModelIndexList();
diff --git a/archivebrowsermodel.h b/archivebrowsermodel.h
index 9a70d83..3150a5e 100644
--- a/archivebrowsermodel.h
+++ b/archivebrowsermodel.h
@@ -24,6 +24,7 @@ class ArchiveBrowserModel : public SmTreeModel {
virtual QVariant data(const QModelIndex &index, int role) const;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role);
void updateDVDNo(const QList fileNos);
+ int nextDVDNo() const;
virtual Qt::ItemFlags flags(const QModelIndex &index) const;
QList availableQualities() { return mAvailableQualities; }
QModelIndexList children(const QModelIndex &idx);
diff --git a/copyworker.cpp b/copyworker.cpp
index dbff451..0fbc802 100644
--- a/copyworker.cpp
+++ b/copyworker.cpp
@@ -13,27 +13,21 @@
CopyWorker::CopyWorker(QObject *parent) : QThread(parent), mMax(0) {}
-void CopyWorker::setDestination(const QString &destDir) {
- QFileInfo destFi(destDir);
- if(destFi.isDir()){
- mDest = destDir;
- }else{
- QString e = QString(tr("%1 is not a directory!")).arg(destFi.absoluteFilePath());
- emit error(e);
+void CopyWorker::enqueue(const QString &source, const QString &destination){
+ QMutexLocker l(&mSourceMx);
+ QPair x = qMakePair(source, destination);
+ if(!mFiles.contains(x)){
+ QFileInfo fi(source);
+ mFiles.append(x);
+ qint64 size = fi.size();
+ mMax += size / 1024 / 1024;
}
}
-void CopyWorker::addSource(const QString &path){
- QMutexLocker l(&mSourceMx);
- if(!mSources.contains(path)){
- QFileInfo fi(path);
- if(fi.exists() && fi.isFile()){
- mSources.append(path);
- qint64 size = fi.size();
- size = size / 1024 / 1024;
- mMax += size;
- }
- }
+void CopyWorker::appendData(const QString &source, const QVariant &data){
+ QList v = mData[source];
+ v << data;
+ mData.insert(source, v);
}
void CopyWorker::clear(){
@@ -41,46 +35,47 @@ void CopyWorker::clear(){
return;
}
QMutexLocker l(&mSourceMx);
- mSources.clear();
+ mFiles.clear();
mMax = 0;
}
void CopyWorker::run(){
- foreach(QString f, mSources){
- QFileInfo cur(f);
+ qint64 total = 0;
+ for(int i = 0; i < mFiles.size(); ++i){
+ QPair p = mFiles.at(i);
+ QFileInfo cur(p.first);
if(!cur.exists()){
QString e = QString(tr("%1 has gone away from under us!")).arg(cur.fileName());
emit error(e);
- continue;
+ return;
}
- QString destFile = QString("%1/%2").arg(mDest).arg(cur.fileName());
- QFileInfo destFi(destFile);
+ QFileInfo destFi(p.second);
if(destFi.exists()){
QString e = QString(tr("%1 already exists!")).arg(destFi.absoluteFilePath());
emit error(e);
- continue;
+ return;
}
- QFile sourceQF(f);
- QFile destQF(destFile);
+ QFile sourceQF(p.first);
+ QFile destQF(p.second);
bool openSource = sourceQF.open(QIODevice::ReadOnly);
bool openDest = destQF.open(QIODevice::WriteOnly);
if(!openSource || !openDest){
QString e = QString(tr("Failed to open source or destination on %1!")).arg(cur.fileName());
emit error(e);
- continue;
+ return;
}
char *buf = new char[32768];
qint64 len = 0;
- int total = 0;
+ emit file(cur.fileName());
while(!sourceQF.atEnd()){
len = sourceQF.read(buf, 32768);
destQF.write(buf, len);
- total +=len;
+ total += len;
qint64 cur = total / 1024 / 1024;
emit bytesRead(cur);
}
delete buf;
- QString s = QString(tr("Done copying %1")).arg(cur.fileName());
- emit success(s);
}
+ QString s = QString(tr("Done copying %1 file(s)")).arg(QString::number(mFiles.size()));
+ emit success(s);
}
diff --git a/copyworker.h b/copyworker.h
index fed3631..7413f49 100644
--- a/copyworker.h
+++ b/copyworker.h
@@ -10,16 +10,20 @@
#include
#include
-#include
+#include
+#include
#include
+#include
class CopyWorker : public QThread {
Q_OBJECT
public:
CopyWorker(QObject *parent = 0);
- void setDestination(const QString &destDir);
- void addSource(const QString &path);
+ void enqueue(const QString &source, const QString &destination);
+ void appendData(const QString &source, const QVariant &data);
+ const QHash > data() const { return mData; }
void clear();
+ qint64 max() const { return mMax; }
virtual void run();
signals:
@@ -27,10 +31,11 @@ class CopyWorker : public QThread {
void bytesRead(int);
void processint(QString);
void error(QString);
+ void file(QString);
private:
- QStringList mSources;
- QString mDest;
+ QList > mFiles;
+ QHash > mData;
QMutex mSourceMx;
int mMax;
};
--
cgit v1.2.3-70-g09d2