diff framework/MainWindowBase.cpp @ 140:9ccaa8fd9b9f

* Add code to identify (usually) the type of an XML file that contains either SV session or layer data, and use it to permit loading files with plain .xml extension that contain complete uncompression sessions
author Chris Cannam
date Thu, 20 Nov 2008 10:59:14 +0000
parents 2cf711ed89e2
children 9a8c73ffdce0
line wrap: on
line diff
--- a/framework/MainWindowBase.cpp	Wed Nov 19 14:55:17 2008 +0000
+++ b/framework/MainWindowBase.cpp	Thu Nov 20 10:59:14 2008 +0000
@@ -1157,7 +1157,10 @@
 
         return FileOpenFailed;
 
-    } else if (source.getExtension() == "svl" || source.getExtension() == "xml") {
+    } else if (source.getExtension() == "svl" ||
+               (source.getExtension() == "xml" &&
+                (SVFileReader::identifyXmlFile(source.getLocalFilename())
+                 == SVFileReader::SVLayerFile))) {
 
         PaneCallback callback(this);
         QFile file(path);
@@ -1314,13 +1317,45 @@
     std::cerr << "MainWindowBase::openSession(" << source.getLocation().toStdString() << ")" << std::endl;
 
     if (!source.isAvailable()) return FileOpenFailed;
-    if (source.getExtension() != "sv") return FileOpenFailed;
+
+    if (source.getExtension() != "sv") {
+        if (source.getExtension() == "xml") {
+            source.waitForData();
+            if (SVFileReader::identifyXmlFile(source.getLocalFilename()) ==
+                SVFileReader::SVSessionFile) {
+                std::cerr << "This XML file looks like a session file, attempting to open it as a session" << std::endl;
+            } else {
+                return FileOpenFailed;
+            }
+        } else {
+            return FileOpenFailed;
+        }
+    }
     source.waitForData();
 
-    BZipFileDevice bzFile(source.getLocalFilename());
-    if (!bzFile.open(QIODevice::ReadOnly)) return FileOpenFailed;
-
-    if (!checkSaveModified()) return FileOpenCancelled;
+    QXmlInputSource *inputSource = 0;
+    BZipFileDevice *bzFile = 0;
+    QFile *rawFile = 0;
+
+    if (source.getExtension() == "sv") {
+        bzFile = new BZipFileDevice(source.getLocalFilename());
+        if (!bzFile->open(QIODevice::ReadOnly)) {
+            delete bzFile;
+            return FileOpenFailed;
+        }
+        inputSource = new QXmlInputSource(bzFile);
+    } else {
+        rawFile = new QFile(source.getLocalFilename());
+        inputSource = new QXmlInputSource(rawFile);
+    }
+
+    if (!checkSaveModified()) {
+        if (bzFile) bzFile->close();
+        delete inputSource;
+        delete bzFile;
+        delete rawFile;
+        return FileOpenCancelled;
+    }
 
     QString error;
     closeSession();
@@ -1336,14 +1371,18 @@
     connect
         (&reader, SIGNAL(modelRegenerationWarning(QString, QString, QString)),
          this, SLOT(modelRegenerationWarning(QString, QString, QString)));
-    QXmlInputSource inputSource(&bzFile);
-    reader.parse(inputSource);
+
+    reader.parse(*inputSource);
     
     if (!reader.isOK()) {
         error = tr("SV XML file read error:\n%1").arg(reader.getErrorString());
     }
     
-    bzFile.close();
+    if (bzFile) bzFile->close();
+
+    delete inputSource;
+    delete bzFile;
+    delete rawFile;
 
     bool ok = (error == "");