From 8951068895eadeab592083e9e66332af80911796 Mon Sep 17 00:00:00 2001 From: sawinglogz <3787776-sawinglogz@users.noreply.gitlab.com> Date: Sun, 26 Jan 2020 18:13:09 -0500 Subject: [PATCH] Add support for importing a whole folder of Viatom files. --- .../SleepLib/loader_plugins/viatom_loader.cpp | 48 +++++++++++-------- oscar/mainwindow.cpp | 11 +++-- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/oscar/SleepLib/loader_plugins/viatom_loader.cpp b/oscar/SleepLib/loader_plugins/viatom_loader.cpp index 52be4f15..9f54774a 100644 --- a/oscar/SleepLib/loader_plugins/viatom_loader.cpp +++ b/oscar/SleepLib/loader_plugins/viatom_loader.cpp @@ -34,35 +34,45 @@ ViatomLoader::Detect(const QString & path) int ViatomLoader::Open(const QString & dirpath) { - // I don't know under what circumstances this is called... qDebug() << "ViatomLoader::Open(" << dirpath << ")"; - return 0; + int imported = 0; + int found = 0; + + if (QFileInfo(dirpath).isDir()) { + QDir dir(dirpath); + dir.setFilter(QDir::NoDotAndDotDot | QDir::Files | QDir::Hidden); + dir.setNameFilters(getNameFilter()); + dir.setSorting(QDir::Name); + + for (auto & fi : dir.entryInfoList()) { + imported += OpenFile(fi.canonicalFilePath()); + found++; + } + } + else { + // This filename has already been filtered by QFileDialog. + imported = OpenFile(dirpath); + found++; + } + + if (!found) { + return -1; + } + return imported; } int ViatomLoader::OpenFile(const QString & filename) { - bool ok = false; + int imported = 0; s_unexpectedMessages.clear(); - qDebug() << "ViatomLoader::OpenFile(" << filename << ")"; - Session* sess = ParseFile(filename); - if (sess == nullptr) { - // TODO: This should not be an error condition, at least because we skip sessions that are already imported, - // and also since we may eventually scan a directory containing non-Viatom files, and those should simply - // be skipped. - /* - QMessageBox::information(QApplication::activeWindow(), - QObject::tr("Unrecognized File"), - QObject::tr("This file does not appear to be a Viatom data file.") +"\n\n"+ - QObject::tr("If it is, the developers will need a copy of this file so that future versions of OSCAR will be able to read it.") - ,QMessageBox::Ok); - */ - } else { + if (sess) { SaveSessionToDatabase(sess); - ok = true; + imported = 1; + // TODO: Move this to Open() if (s_unexpectedMessages.count() > 0 && p_profile->session->warnOnUnexpectedData()) { // Compare this to the list of messages previously seen for this machine // and only alert if there are new ones. @@ -80,7 +90,7 @@ ViatomLoader::OpenFile(const QString & filename) } } - return ok; + return imported; } Session* ViatomLoader::ParseFile(const QString & filename) diff --git a/oscar/mainwindow.cpp b/oscar/mainwindow.cpp index ac049790..0468c71c 100644 --- a/oscar/mainwindow.cpp +++ b/oscar/mainwindow.cpp @@ -2410,12 +2410,15 @@ void MainWindow::on_actionImport_Viatom_Data_triggered() if (w.exec() == QFileDialog::Accepted) { QString filename = w.selectedFiles()[0]; - if (!viatom.OpenFile(filename)) { - Notify(tr("There was a problem opening Viatom data file: ") + filename); - return; + int c = viatom.Open(filename); + if (c > 0) { + Notify(tr("Imported %1 oximetry session(s) from\n\n%2").arg(c).arg(filename), tr("Import Success")); + } else if (c == 0) { + Notify(tr("Already up to date with oximetry data at\n\n%1").arg(filename), tr("Up to date")); + } else { + Notify(tr("Couldn't find any valid data at\n\n%1").arg(filename),tr("Import Problem")); } - Notify(tr("Viatom Data Import complete")); daily->LoadDate(daily->getDate()); } }