diff --git a/src/Lib/Utils.cc b/src/Lib/Utils.cc index a46a86786d87a5e3ec5ccdcbfb2f46665bebdd31..db198f789cf09611fbcccc303d29639985f0967d 100644 --- a/src/Lib/Utils.cc +++ b/src/Lib/Utils.cc @@ -6,7 +6,7 @@ using namespace Vivy; bool -Utils::detectDocumentType(const QFileInfo &file, DocumentType *type) +Utils::detectDocumentType(const QFileInfo &file, DocumentType *const type) { const QString suffix = file.suffix(); @@ -14,15 +14,27 @@ Utils::detectDocumentType(const QFileInfo &file, DocumentType *type) *type = DocumentType::Audio; return true; } - if (Utils::videoFileSuffix.contains(suffix, Qt::CaseInsensitive)) { + + else if (Utils::videoFileSuffix.contains(suffix, Qt::CaseInsensitive)) { *type = DocumentType::Video; return true; } - if (Utils::assFileSuffix.contains(suffix, Qt::CaseInsensitive)) { + + else if (Utils::assFileSuffix.contains(suffix, Qt::CaseInsensitive)) { *type = DocumentType::ASS; return true; } + else if (Utils::vivyFileSuffix.contains(suffix, Qt::CaseInsensitive)) { + *type = DocumentType::Vivy; + return true; + } + + else if (Utils::scriptFileSuffix.contains(suffix, Qt::CaseInsensitive)) { + *type = DocumentType::VivyScript; + return true; + } + return false; } diff --git a/src/Lib/Utils.hh b/src/Lib/Utils.hh index 6246bf4d2809a37abe65ab4b1b510b63b98d0989..7e75014ad3010e694ee5170830f9ba958a8014b9 100644 --- a/src/Lib/Utils.hh +++ b/src/Lib/Utils.hh @@ -108,7 +108,7 @@ struct Time final { quint64 toUInt() const noexcept; }; -bool detectDocumentType(const QFileInfo &, DocumentType *); +bool detectDocumentType(const QFileInfo &, DocumentType *const); bool decodeLineToBoolean(const QString &item, const QString &error); int decodeLineToInteger(const QString &item, const QString &error); float decodeLineToFloating(const QString &item, const QString &error); diff --git a/src/UI/MainWindow.cc b/src/UI/MainWindow.cc index 805489ec830b647f2f25a4e4cd6ec6d863ac2cbb..32e6bbf9ec5bc28c14548593ddc126b6df28136a 100644 --- a/src/UI/MainWindow.cc +++ b/src/UI/MainWindow.cc @@ -247,40 +247,31 @@ MainWindow::newDocument() noexcept void MainWindow::openDocument() noexcept { - const QString separator = QStringLiteral(";;"); - const QString filename = dialogOpenFileName("Select a document to open", QDir::homePath(), + const QString filename = dialogOpenFileName("Select a document to open", QDir::homePath(), Utils::getAnyDocumentFileSuffixFilter()); if (filename.isEmpty()) { qWarning() << "Found an empty filename, don't open a file"; return; } - // TODO: Better implementation - // Load script document - if (filename.endsWith(".lua") || filename.endsWith(".vvs")) { - qDebug() << "Try to load script" << filename; - try { - addTab(new ScriptDocumentView(filename, documents)); - } catch (const std::runtime_error &e) { - qCritical() << "Failed to load script" << filename << "with error:" << e.what(); - } + const QFileInfo fileInfo(filename); + Utils::DocumentType fileType; + + if (!Utils::detectDocumentType(fileInfo, &fileType)) { + qWarning() << "Failed to detect file type for" << filename; + return; } - // TODO: Better implementation - // Load Vivy document - else { - qDebug() << "Try to load document" << filename; - std::shared_ptr<VivyDocument> document = vivyApp->documentStore.loadDocument(filename); - - try { - qDebug() << "Add the view to the MainWindow for" << filename; - addTab(new VivyDocumentView(document)); - } catch (const std::runtime_error &e) { - qCritical() - << "Failed to create the document view for" << QFileInfo(filename).baseName() - << "with path" << filename << "and error:" << e.what(); - vivyApp->documentStore.closeDocument(document->getUuid()); - } + // Handle the different types here + try { + if ((fileType == Utils::DocumentType::Video) || (fileType == Utils::DocumentType::Vivy) || + (fileType == Utils::DocumentType::Audio) || (fileType == Utils::DocumentType::ASS)) + addTab(new VivyDocumentView(vivyApp->documentStore.loadDocument(filename))); + + else if (fileType == Utils::DocumentType::VivyScript) + addTab(new ScriptDocumentView(filename, documents)); + } catch (const std::runtime_error &e) { + qCritical() << "Failed to load document" << filename << "with error:" << e.what(); } }