From ff0fb0bef10963a20dffdc0c2bfcb065fbe90034 Mon Sep 17 00:00:00 2001 From: Mark Liversedge Date: Thu, 7 Mar 2013 15:23:43 +0000 Subject: [PATCH] Add support for deleting videos .. but only deletes references and db entry .. we do not delete from the filesystem -- need to add a dialog to ask the user to confirm deleting the file from disk. --- src/Library.cpp | 11 +++++++++++ src/Library.h | 1 + src/TrainDB.cpp | 11 +++++++++++ src/TrainDB.h | 1 + src/TrainTool.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ src/TrainTool.h | 1 + 6 files changed, 68 insertions(+) diff --git a/src/Library.cpp b/src/Library.cpp index 2e4356d9f..3d2eec803 100644 --- a/src/Library.cpp +++ b/src/Library.cpp @@ -196,6 +196,17 @@ Library::importFiles(MainWindow *mainWindow, QStringList files) } } +void +Library::removeRef(MainWindow *mainWindow, QString ref) +{ + // remove a previous reference + int index = refs.indexOf(ref); + if (index >= 0) { + refs.removeAt(index); + LibraryParser::serialize(mainWindow->home); + } +} + // // SEARCHDIALOG -- user select paths and files and run a search // diff --git a/src/Library.h b/src/Library.h index 7880ff8d2..add633587 100644 --- a/src/Library.h +++ b/src/Library.h @@ -41,6 +41,7 @@ class Library : QObject static void initialise(QDir); // init static Library *findLibrary(QString); static void importFiles(MainWindow *mainWindow, QStringList files); + void removeRef(MainWindow *mainWindow, QString ref); }; extern QList libraries; // keep track of all Library search paths for all users diff --git a/src/TrainDB.cpp b/src/TrainDB.cpp index a6c444519..ab02079d0 100644 --- a/src/TrainDB.cpp +++ b/src/TrainDB.cpp @@ -350,6 +350,17 @@ bool TrainDB::importWorkout(QString pathname, ErgFile *ergFile) return rc; } +bool TrainDB::deleteVideo(QString pathname) +{ + QSqlQuery query(dbconn); + QDateTime timestamp = QDateTime::currentDateTime(); + + // zap the current row - if there is one + query.prepare("DELETE FROM videos WHERE filepath = ?;"); + query.addBindValue(pathname); + return query.exec(); +} + bool TrainDB::importVideo(QString pathname) { QSqlQuery query(dbconn); diff --git a/src/TrainDB.h b/src/TrainDB.h index f2964b594..93c0b101d 100644 --- a/src/TrainDB.h +++ b/src/TrainDB.h @@ -54,6 +54,7 @@ class TrainDB : public QObject bool deleteWorkout(QString pathname); bool importVideo(QString pathname); + bool deleteVideo(QString pathname); // drop and recreate tables void rebuildDB(); diff --git a/src/TrainTool.cpp b/src/TrainTool.cpp index 16be927ab..008fdd141 100644 --- a/src/TrainTool.cpp +++ b/src/TrainTool.cpp @@ -52,6 +52,7 @@ #include // isnan and isinf #include "TrainDB.h" +#include "Library.h" TrainTool::TrainTool(MainWindow *parent, const QDir &home) : GcWindow(parent), home(home), main(parent) { @@ -461,6 +462,15 @@ TrainTool::mediaPopup() connect(import, SIGNAL(triggered(void)), main, SLOT(importWorkout(void))); connect(scan, SIGNAL(triggered(void)), main, SLOT(manageLibrary(void))); + QModelIndex current = mediaTree->currentIndex(); + QModelIndex target = vsortModel->mapToSource(current); + QString filename = videoModel->data(videoModel->index(target.row(), 0), Qt::DisplayRole).toString(); + if (QFileInfo(filename).exists()) { + QAction *del = new QAction(tr("Delete selected video"), workoutTree); + menu.addAction(del); + connect(del, SIGNAL(triggered(void)), this, SLOT(deleteVideos(void))); + } + // execute the menu menu.exec(trainSplitter->mapToGlobal(QPoint(mediaItem->pos().x()+mediaItem->width()-20, mediaItem->pos().y()))); @@ -654,6 +664,39 @@ TrainTool::listWorkoutFiles(const QDir &dir) const return dir.entryList(filters, QDir::Files, QDir::Name); } +void +TrainTool::deleteVideos() +{ + QModelIndex current = mediaTree->currentIndex(); + QModelIndex target = vsortModel->mapToSource(current); + QString filename = videoModel->data(videoModel->index(target.row(), 0), Qt::DisplayRole).toString(); + + if (QFileInfo(filename).exists()) { + // are you sure? + QMessageBox msgBox; + msgBox.setText(tr("Are you sure you want to delete this video?")); + msgBox.setInformativeText(filename); + QPushButton *deleteButton = msgBox.addButton(tr("Delete"),QMessageBox::YesRole); + msgBox.setStandardButtons(QMessageBox::Cancel); + msgBox.setDefaultButton(QMessageBox::Cancel); + msgBox.setIcon(QMessageBox::Critical); + msgBox.exec(); + + if(msgBox.clickedButton() != deleteButton) return; + + // delete from disk + //XXX QFile(filename).remove(); // lets not for now.. + + // remove any reference (from drag and drop) + Library *l = Library::findLibrary("Media Library"); + if (l) l->removeRef(main, filename); + + // delete from DB + trainDB->startLUW(); + trainDB->deleteVideo(filename); + trainDB->endLUW(); + } +} void TrainTool::deleteWorkouts() { diff --git a/src/TrainTool.h b/src/TrainTool.h index 648c64a19..0c01b3d23 100644 --- a/src/TrainTool.h +++ b/src/TrainTool.h @@ -135,6 +135,7 @@ class TrainTool : public GcWindow public slots: void configChanged(); void deleteWorkouts(); // deletes selected workouts + void deleteVideos(); // deletes selected workouts void Start(); // when start button is pressed void Pause(); // when Paude is pressed