From 8b968ddcb1613af383d5a0530500573a1816711b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Poizl?= Date: Thu, 21 Apr 2022 16:47:03 +0200 Subject: [PATCH] Add move marked files --- database.cpp | 5 +++ database.h | 1 + mainwindow.cpp | 92 +++++++++++++++++++++++++++++++------------------- mainwindow.h | 2 ++ 4 files changed, 65 insertions(+), 35 deletions(-) diff --git a/database.cpp b/database.cpp index a4b46cf..0ed0b51 100644 --- a/database.cpp +++ b/database.cpp @@ -98,6 +98,11 @@ QStringList Database::getMarkedFiles() return files; } +void Database::clearMarkedFiles() +{ + QSqlDatabase::database().exec("DELETE FROM files"); +} + bool Database::checkError(QSqlQuery &query) { QSqlError error = query.lastError(); diff --git a/database.h b/database.h index 888d216..e034770 100644 --- a/database.h +++ b/database.h @@ -28,6 +28,7 @@ public: bool unmark(const QString &filename); bool isMarked(const QString &filename); QStringList getMarkedFiles(); + void clearMarkedFiles(); void indexDir(const QDir &dir, QProgressDialog *progress); QStringList getFitsKeywords(); diff --git a/mainwindow.cpp b/mainwindow.cpp index c854ae1..a935456 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -87,6 +87,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) QMenu *fileMenu = new QMenu(tr("File"), this); fileMenu->addAction(tr("Open"), this, SLOT(loadFile()), QKeySequence("Ctrl+O")); fileMenu->addAction(tr("Copy marked files"), this, SLOT(copyMarked())); + fileMenu->addAction(tr("Move marked files"), this, SLOT(moveMarked())); fileMenu->addAction(tr("Save as"), this, SLOT(saveAs()), QKeySequence("Ctrl+S")); fileMenu->addAction(tr("Index directory"), this, SLOT(indexDir())); QAction *liveModeAction = fileMenu->addAction(tr("Live mode"), this, SLOT(liveMode(bool))); @@ -244,6 +245,57 @@ void MainWindow::closeEvent(QCloseEvent *event) QMainWindow::closeEvent(event); } +void MainWindow::copyOrMove(bool copy) +{ + QString dest = QFileDialog::getExistingDirectory(this, tr("Select destination"), _lastDir); + QDir dir(dest); + if(!dest.isEmpty() && dir.exists()) + { + int i = 0; + QStringList files = m_database->getMarkedFiles(); + QProgressDialog progress(copy ? tr("Copying") : tr("Moving"), tr("Cancel"), 0, files.size(), this); + progress.setWindowModality(Qt::WindowModal); + progress.show(); + foreach(const QString &file, files) + { + QFileInfo info(file); + QFile srcFile(file); + QFile dstFile(dir.absoluteFilePath(info.fileName())); + + if(dstFile.exists()) + continue; + + if(progress.wasCanceled()) + break; +#ifndef __linux__ + if(copy) + { + srcFile.open(QIODevice::ReadOnly); + dstFile.open(QIODevice::WriteOnly); + if(ioctl(dstFile.handle(), BTRFS_IOC_CLONE, srcFile.handle()) < 0) + { + dstFile.remove(); + dstFile.close(); + qDebug() << dstFile.fileName(); + srcFile.copy(dstFile.fileName()); + } + } + else + { + srcFile.rename(dstFile.fileName()); + } +#else + if(copy) + srcFile.copy(dstFile.fileName()); + else + srcFile.rename(dstFile.fileName()); +#endif + progress.setValue(i++); + } + } + m_database->clearMarkedFiles(); +} + void MainWindow::socketNotify() { socketNotifier->setEnabled(false); @@ -377,42 +429,12 @@ void MainWindow::unmarkAndNext() void MainWindow::copyMarked() { - QString dest = QFileDialog::getExistingDirectory(this, tr("Select destination")); - QDir dir(dest); - if(!dest.isEmpty() && dir.exists()) - { - int i = 0; - QStringList files = m_database->getMarkedFiles(); - QProgressDialog progress(tr("Copying"), tr("Cancel"), 0, files.size(), this); - progress.setWindowModality(Qt::WindowModal); - progress.show(); - foreach(const QString &file, files) - { - QFileInfo info(file); - QFile srcFile(file); - QFile dstFile(dir.absoluteFilePath(info.fileName())); + copyOrMove(true); +} - if(dstFile.exists()) - continue; - - if(progress.wasCanceled()) - break; -#ifdef __linux__ - srcFile.open(QIODevice::ReadOnly); - dstFile.open(QIODevice::WriteOnly); - if(ioctl(dstFile.handle(), BTRFS_IOC_CLONE, srcFile.handle()) < 0) - { - dstFile.remove(); - dstFile.close(); - qDebug() << dstFile.fileName(); - srcFile.copy(dstFile.fileName()); - } -#else - srcFile.copy(dstFile.fileName()); -#endif - progress.setValue(i++); - } - } +void MainWindow::moveMarked() +{ + copyOrMove(false); } void MainWindow::toggleFullScreen() diff --git a/mainwindow.h b/mainwindow.h index f0689e2..0413956 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -36,6 +36,7 @@ protected: void setupSigterm(); static void signalHandler(int); void closeEvent(QCloseEvent *event); + void copyOrMove(bool copy); protected slots: void socketNotify(); void updateWindowTitle(); @@ -51,6 +52,7 @@ protected slots: void markAndNext(); void unmarkAndNext(); void copyMarked(); + void moveMarked(); void toggleFullScreen(); void liveMode(bool active); void imageStats(bool imageStats);