From a369e2dac4e2ea79c6c7cfa145645fd95ed8c4ef Mon Sep 17 00:00:00 2001 From: Mark Watkins Date: Thu, 3 May 2018 19:59:31 +1000 Subject: [PATCH] Fix ResMed importer backup & gz glitch, remove quazip dependency --- .../SleepLib/loader_plugins/edfparser.cpp | 5 ++ .../SleepLib/loader_plugins/edfparser.h | 2 + .../SleepLib/loader_plugins/resmed_loader.cpp | 84 ++++++++++--------- sleepyhead/SleepLib/machine_loader.cpp | 2 + sleepyhead/UpdaterWindow.cpp | 40 ++++----- sleepyhead/UpdaterWindow.h | 28 +++---- sleepyhead/logger.cpp | 25 ++---- sleepyhead/mainwindow.cpp | 7 +- sleepyhead/sleepyhead.pro | 6 +- 9 files changed, 95 insertions(+), 104 deletions(-) diff --git a/sleepyhead/SleepLib/loader_plugins/edfparser.cpp b/sleepyhead/SleepLib/loader_plugins/edfparser.cpp index 45361564..8acc5b23 100644 --- a/sleepyhead/SleepLib/loader_plugins/edfparser.cpp +++ b/sleepyhead/SleepLib/loader_plugins/edfparser.cpp @@ -239,9 +239,11 @@ bool EDFParser::Open(const QString & name) goto badfile; } + EDFMutex.lock(); // Open gzip file for reading gzFile f = gzopen(name.toLatin1(), "rb"); if (!f) { + EDFMutex.unlock(); goto badfile; } @@ -250,6 +252,7 @@ bool EDFParser::Open(const QString & name) buffer = new char [datasize]; gzread(f, buffer, datasize); gzclose(f); + EDFMutex.unlock(); } else { // Open and read uncompressed file @@ -292,3 +295,5 @@ EDFSignal *EDFParser::lookupLabel(const QString & name, int index) return it.value()[index]; } + +QMutex EDFParser::EDFMutex; diff --git a/sleepyhead/SleepLib/loader_plugins/edfparser.h b/sleepyhead/SleepLib/loader_plugins/edfparser.h index 62534ea8..64aad2ae 100644 --- a/sleepyhead/SleepLib/loader_plugins/edfparser.h +++ b/sleepyhead/SleepLib/loader_plugins/edfparser.h @@ -13,6 +13,7 @@ #include #include #include +#include #include "SleepLib/common.h" @@ -163,6 +164,7 @@ class EDFParser qint64 startdate; qint64 enddate; QString reserved44; + static QMutex EDFMutex; }; diff --git a/sleepyhead/SleepLib/loader_plugins/resmed_loader.cpp b/sleepyhead/SleepLib/loader_plugins/resmed_loader.cpp index f8705a92..ee26c6e5 100644 --- a/sleepyhead/SleepLib/loader_plugins/resmed_loader.cpp +++ b/sleepyhead/SleepLib/loader_plugins/resmed_loader.cpp @@ -1467,8 +1467,9 @@ int ResmedLoader::scanFiles(Machine * mach, const QString & datalog_path) if (filename.endsWith(STR_ext_gz)) { filename.chop(3); } + QString fullpath = fi.filePath(); - QString newpath = create_backups ? backup(fi.canonicalFilePath(), backup_path) : fi.canonicalFilePath(); + QString newpath = create_backups ? backup(fullpath, backup_path) : fullpath; // Accept only .edf and .edf.gz files @@ -1495,6 +1496,10 @@ int ResmedLoader::scanFiles(Machine * mach, const QString & datalog_path) resday.files[filename] = newpath; } } +#ifdef DEBUG_EFFICIENCY + qDebug() << "Scanning EDF files took" << time.elapsed() << "ms"; +#endif + return resdayList.size(); } /*// Check for duplicates @@ -2114,7 +2119,7 @@ void ResDayTask::run() OverlappingEDF & B = next_oit.value(); int gap = B.start - A.end; if (gap < 60) { - qDebug() << "Only a" << gap << "s sgap between ResMed sessions on" << resday->date.toString(); +// qDebug() << "Only a" << gap << "s sgap between ResMed sessions on" << resday->date.toString(); } } } @@ -2452,7 +2457,10 @@ int ResmedLoader::Open(const QString & dirpath) QString backupfile = strBackupPath+"/"+newname; - if (compress_backups) backupfile += STR_ext_gz; + QString gzfile = backupfile + STR_ext_gz; + QString nongzfile = backupfile; + + backupfile = compress_backups ? gzfile : nongzfile; if (!QFile::exists(backupfile)) { if (filename.endsWith(STR_ext_gz,Qt::CaseInsensitive)) { @@ -2470,6 +2478,12 @@ int ResmedLoader::Open(const QString & dirpath) } } } + // Remove any duplicate compressed/uncompressed + if (compress_backups) { + QFile::exists(nongzfile) && QFile::remove(nongzfile); + } else { + QFile::exists(gzfile) && QFile::remove(gzfile); + } STRmap[date] = STRFile(backupfile, stredf); @@ -2543,6 +2557,8 @@ int ResmedLoader::Open(const QString & dirpath) } } + compress_backups ? compressFile(path + "STR.edf", backup_path + "STR.edf.gz") : QFile::copy(path + "STR.edf", backup_path + "STR.edf"); + // Copy Identification files to backup folder QFile::copy(path + RMS9_STR_idfile + STR_ext_TGT, backup_path + RMS9_STR_idfile + STR_ext_TGT); QFile::copy(path + RMS9_STR_idfile + STR_ext_CRC, backup_path + RMS9_STR_idfile + STR_ext_CRC); @@ -2758,13 +2774,16 @@ int ResmedLoader::Open(const QString & dirpath) QString ResmedLoader::backup(const QString & fullname, const QString & backup_path) { + QDir dir; + QString filename, yearstr, newname, oldname; + bool compress = p_profile->session->compressBackupData(); - QString filename, yearstr, newname, oldname; - bool ok, gz = (fullname.right(3).toLower() == STR_ext_gz); + bool ok; + bool gz = (fullname.right(3).toLower() == STR_ext_gz); // Input file is a .gz? + filename = fullname.section("/", -1); - if (gz) { filename.chop(3); } @@ -2772,61 +2791,48 @@ QString ResmedLoader::backup(const QString & fullname, const QString & backup_pa yearstr = filename.left(4); yearstr.toInt(&ok, 10); - if (!ok) { - qDebug() << "Invalid EDF filename given to ResMedLoader::backup()"; + qDebug() << "Invalid EDF filename given to ResMedLoader::backup()" << fullname; return ""; } - newname = backup_path + RMS9_STR_datalog + "/" + yearstr; - QDir dir; - dir.mkpath(newname); - newname += "/" + filename; + QString newpath = backup_path + RMS9_STR_datalog + "/" + yearstr; + !dir.exists(newpath) && dir.mkpath(newpath); + + newname = newpath+"/"+filename; QString tmpname = newname; - if (compress) { - newname += STR_ext_gz; - } + QString newnamegz = newname + STR_ext_gz; + QString newnamenogz = newname; - // First make sure the correct backup exists. + newname = compress ? newnamegz : newnamenogz; + + // First make sure the correct backup exists in the right place if (!QFile::exists(newname)) { if (compress) { - gz ? - QFile::copy(fullname, newname) // Already compressed.. copy it to the right location - : - compressFile(fullname, newname); + // If input file is already compressed.. copy it to the right location, otherwise compress it + gz ? QFile::copy(fullname, newname) : compressFile(fullname, newname); } else { - // dont really care if it's compressed and not meant to be, leave it that way - QFile::copy(fullname, newname); + // If inputs a gz, uncompress it, otherwise copy is raw + gz ? uncompressFile(fullname, newname) : QFile::copy(fullname, newname); } - } // else backup already exists... + } // else backup already exists... good. // Now the correct backup is in place, we can trash any if (compress) { // Remove any uncompressed duplicate - if (QFile::exists(tmpname)) { - QFile::remove(tmpname); - } + QFile::exists(newnamenogz) && QFile::remove(newnamenogz); } else { // Delete the non compressed copy and choose it instead. - if (QFile::exists(tmpname + STR_ext_gz)) { - QFile::remove(tmpname); - newname = tmpname + STR_ext_gz; - } - + QFile::exists(newnamegz) && QFile::remove(newnamegz); } + // Used to store it under Backup\Datalog // Remove any traces from old backup directory structure oldname = backup_path + RMS9_STR_datalog + "/" + filename; - - if (QFile::exists(oldname)) { - QFile::remove(oldname); - } - - if (QFile::exists(oldname + STR_ext_gz)) { - QFile::remove(oldname + STR_ext_gz); - } + QFile::exists(oldname) && QFile::remove(oldname); + QFile::exists(oldname + STR_ext_gz) && QFile::remove(oldname + STR_ext_gz); return newname; } diff --git a/sleepyhead/SleepLib/machine_loader.cpp b/sleepyhead/SleepLib/machine_loader.cpp index e84d6515..a516daeb 100644 --- a/sleepyhead/SleepLib/machine_loader.cpp +++ b/sleepyhead/SleepLib/machine_loader.cpp @@ -173,6 +173,7 @@ bool compressFile(QString infile, QString outfile) } if (QFile::exists(outfile)) { qDebug() << "compressFile()" << outfile << "already exists"; + return false; } QFile f(infile); @@ -198,6 +199,7 @@ bool compressFile(QString infile, QString outfile) } f.close(); + gzFile gz = gzopen(outfile.toLatin1(), "wb"); //gzbuffer(gz,65536*2); diff --git a/sleepyhead/UpdaterWindow.cpp b/sleepyhead/UpdaterWindow.cpp index c480af10..c7b77709 100644 --- a/sleepyhead/UpdaterWindow.cpp +++ b/sleepyhead/UpdaterWindow.cpp @@ -1,4 +1,4 @@ -/* UpdaterWindow +/* UpdaterWindow * * Copyright (c) 2011-2018 Mark Watkins * @@ -23,8 +23,8 @@ #include #include "SleepLib/profiles.h" -#include -#include +//#include +//#include #include "UpdaterWindow.h" #include "ui_UpdaterWindow.h" #include "version.h" @@ -67,8 +67,7 @@ UpdaterWindow::UpdaterWindow(QWidget *parent) : UpdaterWindow::~UpdaterWindow() { - disconnect(netmanager, SIGNAL(finished(QNetworkReply *)), this, - SLOT(replyFinished(QNetworkReply *))); + disconnect(netmanager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *))); delete ui; } @@ -93,7 +92,7 @@ void UpdaterWindow::checkForUpdates() { QString platform=platformStr(); -#ifdef Q_OS_WINDOWS +#ifdef Q_OS_WIN32 QString filename = QApplication::applicationDirPath() + "/Updates.xml"; #else QString filename = QApplication::applicationDirPath() + QString("/LatestVersion-%1").arg(platform); @@ -107,7 +106,7 @@ void UpdaterWindow::checkForUpdates() if (age < 900) { QFile file(filename); file.open(QFile::ReadOnly); -#ifdef Q_OS_WINDOWS +#ifdef Q_OS_WIN32 ParseUpdatesXML(&file); #else ParseLatestVersion(&file); @@ -119,7 +118,7 @@ void UpdaterWindow::checkForUpdates() mainwin->Notify(tr("Checking for SleepyHead Updates")); -#ifdef Q_OS_WINDOWS +#ifdef Q_OS_WIN32 update_url = QUrl(QString("http://sleepyhead.jedimark.net/packages/%1/Updates.xml").arg(platform)); #else update_url = QUrl(QString("http://sleepyhead.jedimark.net/releases/LatestVersion-%1").arg(platform)); @@ -135,7 +134,6 @@ void UpdaterWindow::downloadUpdateXML() req.setRawHeader("User-Agent", "Wget/1.12 (linux-gnu)"); reply = netmanager->get(req); ui->plainTextEdit->appendPlainText(tr("Requesting ") + update_url.toString()); - // netmanager->connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this,SLOT(downloadProgress(qint64, qint64))); connect(netmanager, SIGNAL(finished(QNetworkReply *)), this, SLOT(updateFinished(QNetworkReply *))); @@ -146,9 +144,6 @@ void UpdaterWindow::updateFinished(QNetworkReply *reply) { if (reply->error() != QNetworkReply::NoError) { qDebug() << "Update Check Error: "+reply->errorString(); -// netmanager->disconnect(reply, -// SIGNAL(downloadProgress(qint64, qint64)), this, -// SLOT(downloadProgress(qint64, qint64))); disconnect(netmanager, SIGNAL(finished(QNetworkReply *)), this, SLOT(updateFinished(QNetworkReply *))); mainwin->Notify(tr("SleepyHead Updates are currently unvailable for this platform"),tr("SleepyHead Updates")); } else { @@ -160,15 +155,12 @@ void UpdaterWindow::updateFinished(QNetworkReply *reply) QTimer::singleShot(100, this, SLOT(downloadUpdateXML())); return; } -// netmanager->disconnect(reply, -// SIGNAL(downloadProgress(qint64, qint64)), this, -// SLOT(downloadProgress(qint64, qint64))); disconnect(netmanager, SIGNAL(finished(QNetworkReply *)), this, SLOT(updateFinished(QNetworkReply *))); ui->plainTextEdit->appendPlainText(tr("%1 bytes received").arg(reply->size())); -#ifdef Q_OS_WINDOWS +#ifdef Q_OS_WIN32 QString filename = QApplication::applicationDirPath() + "/Updates.xml"; #else QString filename = QApplication::applicationDirPath() + QString("/LatestVersion-%1").arg(platformStr()); @@ -181,7 +173,7 @@ void UpdaterWindow::updateFinished(QNetworkReply *reply) file.close(); file.open(QFile::ReadOnly); -#ifdef Q_OS_WINDOWS +#ifdef Q_OS_WIN32 ParseUpdatesXML(&file); #else ParseLatestVersion(&file); @@ -193,7 +185,7 @@ void UpdaterWindow::updateFinished(QNetworkReply *reply) } } -void UpdaterWindow::dataReceived() +/*void UpdaterWindow::dataReceived() { QString rs = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString(); @@ -267,7 +259,7 @@ void UpdaterWindow::requestFile() connect(reply, SIGNAL(readyRead()), this, SLOT(dataReceived())); connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64))); -} +} */ int checkVersionStatus(QString statusstr) { @@ -435,7 +427,7 @@ void StartMaintenanceTool() { QString mt_path = QApplication::applicationDirPath()+"/MaintenanceTool.exe"; SpawnApp(mt_path); -#ifdef Q_OS_WINDOWS +#ifdef Q_OS_WIN32 #endif } @@ -516,7 +508,7 @@ void UpdaterWindow::ParseUpdatesXML(QIODevice *dev) } // Old -void UpdaterWindow::ParseUpdateXML(QIODevice *dev) +/*void UpdaterWindow::ParseUpdateXML(QIODevice *dev) { QXmlInputSource src(dev); QXmlSimpleReader reader; @@ -856,14 +848,14 @@ void UpdaterWindow::replyFinished(QNetworkReply *reply) mainwin->Notify(tr("There was an error completing a network request:\n\n(") + reply->errorString() + ")"); } -} +} */ void UpdaterWindow::on_CloseButton_clicked() { close(); } -void UpdaterWindow::upgradeNext() +/*void UpdaterWindow::upgradeNext() { QTableWidgetItem *item; bool fnd = false; @@ -950,7 +942,7 @@ void UpdaterWindow::on_upgradeButton_clicked() ui->stackedWidget->setCurrentIndex(1); upgradeNext(); -} +} */ void UpdaterWindow::on_FinishedButton_clicked() { diff --git a/sleepyhead/UpdaterWindow.h b/sleepyhead/UpdaterWindow.h index 1d5f9e66..6442ec79 100644 --- a/sleepyhead/UpdaterWindow.h +++ b/sleepyhead/UpdaterWindow.h @@ -1,4 +1,4 @@ -/* UpdaterWindow +/* UpdaterWindow * * Copyright (c) 2011-2018 Mark Watkins * @@ -50,24 +50,24 @@ class UpdaterWindow : public QMainWindow /*! \fn ParseUpdateXML(QIODevice * dev) \brief Parses the update.xml from either QFile or QNetworkReply source */ - void ParseUpdateXML(QIODevice *dev); + //void ParseUpdateXML(QIODevice *dev); void ParseUpdatesXML(QIODevice *dev); void ParseLatestVersion(QIODevice *dev); protected slots: void updateFinished(QNetworkReply *reply); - //! \brief Network reply completed - void replyFinished(QNetworkReply *reply); + // //! \brief Network reply completed + //void replyFinished(QNetworkReply *reply); - //! \brief Update the progress bars as data is received - void downloadProgress(qint64 bytesReceived, qint64 bytesTotal); + ////! \brief Update the progress bars as data is received + //void downloadProgress(qint64 bytesReceived, qint64 bytesTotal); - //! \brief Save incomming data - void dataReceived(); + ////! \brief Save incomming data + //void dataReceived(); - //! \brief Request a file to download - void requestFile(); +// //! \brief Request a file to download +// void requestFile(); //! \brief Request the update.xml file void downloadUpdateXML(); @@ -76,11 +76,11 @@ class UpdaterWindow : public QMainWindow //! \brief Just close the Updater window void on_CloseButton_clicked(); - //! \brief Start processing the download que, and applying the updates - void on_upgradeButton_clicked(); + // //! \brief Start processing the download que, and applying the updates +// void on_upgradeButton_clicked(); - //! \brief Selects the next file in the download queue - void upgradeNext(); +// //! \brief Selects the next file in the download queue +// void upgradeNext(); //! \brief Click on finished, restart if app has been upgraded, otherwise just close the window. void on_FinishedButton_clicked(); diff --git a/sleepyhead/logger.cpp b/sleepyhead/logger.cpp index e06eb4c5..6d2f4b88 100644 --- a/sleepyhead/logger.cpp +++ b/sleepyhead/logger.cpp @@ -1,4 +1,4 @@ -/* SleepyHead Logger module implementation +/* SleepyHead Logger module implementation * * Copyright (c) 2011-2018 Mark Watkins * @@ -10,22 +10,12 @@ QThreadPool * otherThreadPool = NULL; -#if QT_VERSION < QT_VERSION_CHECK(5,0,0) -void MyOutputHandler(QtMsgType type, const char *msgtxt) -{ - -#else void MyOutputHandler(QtMsgType type, const QMessageLogContext &context, const QString &msgtxt) { Q_UNUSED(context) -#endif if (!logger) { -#if QT_VERSION >= QT_VERSION_CHECK(5,0,0) fprintf(stderr, "Pre/Post: %s\n", msgtxt.toLocal8Bit().constData()); -#else - fprintf(stderr, "Pre/Post: %s\n", msgtxt); -#endif return; } @@ -49,18 +39,15 @@ void MyOutputHandler(QtMsgType type, const QMessageLogContext &context, const QS break; } -#if QT_VERSION >= QT_VERSION_CHECK(5,0,0) - msg = typestr + - msgtxt; //+QString(" (%1:%2, %3)").arg(context.file).arg(context.line).arg(context.function); -#else - msg = typestr + msgtxt; -#endif + msg = typestr + msgtxt; //+QString(" (%1:%2, %3)").arg(context.file).arg(context.line).arg(context.function); + if (logger && logger->isRunning()) { logger->append(msg); - } else { - fprintf(stderr, "%s\n", msg.toLocal8Bit().data()); } + //else { + fprintf(stderr, "%s\n", msg.toLocal8Bit().data()); +// } if (type == QtFatalMsg) { abort(); diff --git a/sleepyhead/mainwindow.cpp b/sleepyhead/mainwindow.cpp index 3d7bd93a..410d6242 100644 --- a/sleepyhead/mainwindow.cpp +++ b/sleepyhead/mainwindow.cpp @@ -1615,15 +1615,13 @@ void MainWindow::on_action_CycleTabs_triggered() void MainWindow::on_actionOnline_Users_Guide_triggered() { - ui->webView->load( - QUrl("http://sleepyhead.sourceforge.net/wiki/index.php?title=SleepyHead_Users_Guide")); + ui->webView->load(QUrl("http://sleepyhead.sourceforge.net/wiki/index.php?title=SleepyHead_Users_Guide")); ui->tabWidget->setCurrentWidget(ui->helpTab); } void MainWindow::on_action_Frequently_Asked_Questions_triggered() { - ui->webView->load( - QUrl("http://sleepyhead.sourceforge.net/wiki/index.php?title=Frequently_Asked_Questions")); + ui->webView->load(QUrl("http://sleepyhead.sourceforge.net/wiki/index.php?title=Frequently_Asked_Questions")); ui->tabWidget->setCurrentWidget(ui->helpTab); } @@ -1639,7 +1637,6 @@ void packEventList(EventList *el, EventDataType minval = 0) qint64 lasttime = 0; EventDataType min = 999, max = 0; - for (quint32 i = 0; i < el->count(); i++) { t = el->data(i); ti = el->time(i); diff --git a/sleepyhead/sleepyhead.pro b/sleepyhead/sleepyhead.pro index c4c35788..65f4bc78 100644 --- a/sleepyhead/sleepyhead.pro +++ b/sleepyhead/sleepyhead.pro @@ -293,9 +293,9 @@ mac { QMAKE_BUNDLE_DATA += TransFiles } -include(../3rdparty/quazip/quazip/quazip.pri) -INCLUDEPATH += $$PWD/../3rdparty/quazip -DEPENDPATH += $$PWD/../3rdparty/quazip +#include(../3rdparty/quazip/quazip/quazip.pri) +#INCLUDEPATH += $$PWD/../3rdparty/quazip +#DEPENDPATH += $$PWD/../3rdparty/quazip DISTFILES += \ ../README