From d4f7e96578ad4d9a580858ae1e0d7157acefd163 Mon Sep 17 00:00:00 2001 From: MuldeR Date: Tue, 21 May 2013 01:22:35 +0200 Subject: [PATCH] Create a lock file in Temp folder, so it cannot be deleted before MediaInfo is extracted. --- src/Main.cpp | 31 ++++++++++++++++++++----------- src/MainWindow.cpp | 4 ++++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/Main.cpp b/src/Main.cpp index 409e1a7..a262b9c 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -150,12 +150,13 @@ QString mixp_getAppDataFolder(void) return folder; } -QString mixp_getTempFolder() +QString mixp_getTempFolder(QFile **lockfile) { + *lockfile = NULL; QString tempFolder; static const char *TEMP_STR = "Temp"; - const QString WRITE_TEST_DATA = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua."; + const QByteArray WRITE_TEST_DATA = QByteArray("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua."); const QString SUB_FOLDER = QUuid::createUuid().toString(); //Try the %TMP% or %TEMP% directory first @@ -165,14 +166,16 @@ QString mixp_getTempFolder() temp.mkdir(SUB_FOLDER); if(temp.cd(SUB_FOLDER) && temp.exists()) { - QFile testFile(QString("%1/~test.tmp").arg(temp.canonicalPath())); - if(testFile.open(QIODevice::ReadWrite)) + QFile *testFile = new QFile(QString("%1/~lock.tmp").arg(temp.canonicalPath())); + if(testFile->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered)) { - if(testFile.write(WRITE_TEST_DATA.toLatin1().constData()) >= strlen(WRITE_TEST_DATA.toLatin1().constData())) + if(testFile->write(WRITE_TEST_DATA) >= WRITE_TEST_DATA.size()) { + *lockfile = testFile; testFile = NULL; tempFolder = temp.canonicalPath(); } - testFile.remove(); + if(testFile) testFile->remove(); + MIXP_DELETE_OBJ(testFile); } } if(!tempFolder.isEmpty()) @@ -200,14 +203,16 @@ QString mixp_getTempFolder() localAppData.mkdir(SUB_FOLDER); if(localAppData.cd(SUB_FOLDER) && localAppData.exists()) { - QFile testFile(QString("%1/~test.tmp").arg(localAppData.canonicalPath())); - if(testFile.open(QIODevice::ReadWrite)) + QFile *testFile = new QFile(QString("%1/~lock.tmp").arg(localAppData.canonicalPath())); + if(testFile->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered)) { - if(testFile.write(WRITE_TEST_DATA.toLatin1().constData()) >= strlen(WRITE_TEST_DATA.toLatin1().constData())) + if(testFile->write(WRITE_TEST_DATA) >= WRITE_TEST_DATA.size()) { + *lockfile = testFile; testFile = NULL; tempFolder = localAppData.canonicalPath(); } - testFile.remove(); + if(testFile) testFile->remove(); + MIXP_DELETE_OBJ(testFile); } } } @@ -268,8 +273,10 @@ int mixp_main(int argc, char* argv[]) qDebug("Copyright (c) 2004-%s LoRd_MuldeR . Some rights reserved.", &mixp_buildDate[7]); qDebug("Built with Qt v%s, running with Qt v%s.\n", QT_VERSION_STR, qVersion()); + QFile *lockFile = NULL; + //Get temp folder - const QString tempFolder = mixp_getTempFolder(); + const QString tempFolder = mixp_getTempFolder(&lockFile); qDebug("TEMP folder is:\n%s\n", QDir::toNativeSeparators(tempFolder).toUtf8().constData()); //Create application @@ -287,6 +294,8 @@ int mixp_main(int argc, char* argv[]) //Clean up MIXP_DELETE_OBJ(mainWindow); MIXP_DELETE_OBJ(application); + if(lockFile) lockFile->remove(); + MIXP_DELETE_OBJ(lockFile); mixp_clean_folder(tempFolder); return exit_code; diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 6dd8fb4..43b6b5d 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -579,6 +579,10 @@ bool CMainWindow::analyzeFile(const QString &filePath) if(mediaInfoPath.isEmpty()) { QMessageBox::critical(this, tr("Failure"), tr("Error: Failed to extract MediaInfo binary!"), QMessageBox::Ok); + m_floatingLabel->hide(); + ui->actionOpen->setEnabled(true); + ui->analyzeButton->setEnabled(true); + ui->exitButton->setEnabled(true); return false; }