]> git.sesse.net Git - kdenlive/blobdiff - src/kdenlivedoc.cpp
Fix massive project corruption when opening a document with frame rate different...
[kdenlive] / src / kdenlivedoc.cpp
index 7a4b9482698dc0d23adadadaf291e3848e554dc9..eebaf6f721ba2df2a28cbaf24211d94ff0ba85c5 100644 (file)
@@ -54,7 +54,7 @@
 
 const double DOCUMENTVERSION = 0.86;
 
-KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup *undoGroup, QString profileName, QMap <QString, QString> properties, const QPoint tracks, Render *render, KTextEdit *notes, MainWindow *parent, KProgressDialog *progressDialog) :
+KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup *undoGroup, QString profileName, QMap <QString, QString> properties, const QPoint tracks, Render *render, KTextEdit *notes, bool *openBackup, MainWindow *parent, KProgressDialog *progressDialog) :
     QObject(parent),
     m_autosave(NULL),
     m_url(url),
@@ -81,19 +81,32 @@ KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup
     m_documentProperties["proxyminsize"] = QString::number(KdenliveSettings::proxyminsize());
     m_documentProperties["generateimageproxy"] = QString::number((int) KdenliveSettings::generateimageproxy());
     m_documentProperties["proxyimageminsize"] = QString::number(KdenliveSettings::proxyimageminsize());
-    
+#if QT_VERSION >= 0x040700
+    m_documentProperties["documentid"] = QString::number(QDateTime::currentMSecsSinceEpoch());
+#else
+    QDateTime date = QDateTime::currentDateTime();
+    m_documentProperties["documentid"] = QString::number(date.toTime_t());
+#endif
+
     // Load properties
     QMapIterator<QString, QString> i(properties);
     while (i.hasNext()) {
         i.next();
         m_documentProperties[i.key()] = i.value();
     }
+
+    *openBackup = false;
     
     if (!url.isEmpty()) {
         QString tmpFile;
         success = KIO::NetAccess::download(url.path(), tmpFile, parent);
-        if (!success) // The file cannot be opened
-            KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
+        if (!success) {
+            // The file cannot be opened
+            if (KMessageBox::warningContinueCancel(parent, i18n("Cannot open the project file, error is:\n%1\nDo you want to open a backup file?", KIO::NetAccess::lastErrorString()), i18n("Error opening file"), KGuiItem(i18n("Open Backup"))) == KMessageBox::Continue) {
+                *openBackup = true;
+            }
+            //KMessageBox::error(parent, KIO::NetAccess::lastErrorString());
+        }
         else {
             QFile file(tmpFile);
             QString errorMsg;
@@ -103,8 +116,13 @@ KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup
             file.close();
             KIO::NetAccess::removeTempFile(tmpFile);
 
-            if (!success) // It is corrupted
-                KMessageBox::error(parent, errorMsg);
+            if (!success) {
+                // It is corrupted
+                if (KMessageBox::warningContinueCancel(parent, i18n("Cannot open the project file, error is:\n%1\nDo you want to open a backup file?", errorMsg), i18n("Error opening file"), KGuiItem(i18n("Open Backup"))) == KMessageBox::Continue) {
+                *openBackup = true;
+            }
+                //KMessageBox::error(parent, errorMsg);
+            }
             else {
                 parent->slotGotProgressInfo(i18n("Validating"), 0);
                 qApp->processEvents();
@@ -113,6 +131,9 @@ KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup
                 if (!success) {
                     // It is not a project file
                     parent->slotGotProgressInfo(i18n("File %1 is not a Kdenlive project file", m_url.path()), 100);
+                    if (KMessageBox::warningContinueCancel(parent, i18n("File %1 is not a valid project file.\nDo you want to open a backup file?", m_url.path()), i18n("Error opening file"), KGuiItem(i18n("Open Backup"))) == KMessageBox::Continue) {
+                        *openBackup = true;
+                    }
                 } else {
                     /*
                      * Validate the file against the current version (upgrade
@@ -131,6 +152,9 @@ KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup
                             QDomElement mlt = m_document.firstChildElement("mlt");
                             QDomElement infoXml = mlt.firstChildElement("kdenlivedoc");
 
+                            // Set profile, fps, etc for the document
+                            setProfilePath(infoXml.attribute("profile"));
+
                             // Check embedded effects
                             QDomElement customeffects = infoXml.firstChildElement("customeffects");
                             if (!customeffects.isNull() && customeffects.hasChildNodes()) {
@@ -236,7 +260,7 @@ KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup
                                 QDomNamedNodeMap props = docproperties.attributes();
                                 for (int i = 0; i < props.count(); i++)
                                     m_documentProperties.insert(props.item(i).nodeName(), props.item(i).nodeValue());
-                                setProfilePath(infoXml.attribute("profile"));
+
                                 if (validator.isModified()) setModified(true);
                                 kDebug() << "Reading file: " << url.path() << ", found clips: " << producers.count();
                             }
@@ -249,7 +273,7 @@ KdenliveDoc::KdenliveDoc(const KUrl &url, const KUrl &projectFolder, QUndoGroup
 
     // Something went wrong, or a new file was requested: create a new project
     if (!success) {
-        m_url = KUrl();
+        m_url.clear();
         setProfilePath(profileName);
         m_document = createEmptyDocument(tracks.x(), tracks.y());
     }
@@ -688,6 +712,15 @@ bool KdenliveDoc::saveSceneList(const QString &path, const QString &scene, const
     file.close();
     if (!autosave) {
         cleanupBackupFiles();
+        QFileInfo info(file);
+        QString fileName = KUrl(path).fileName().section('.', 0, -2);   
+        fileName.append("-" + m_documentProperties.value("documentid"));
+        fileName.append(info.lastModified().toString("-yyyy-MM-dd-hh-mm"));
+        fileName.append(".kdenlive.png");
+        KUrl backupFile = m_projectFolder;
+        backupFile.addPath(".backup/");
+        backupFile.addPath(fileName);
+        emit saveTimelinePreview(backupFile.path());
     }
     return true;
 }
@@ -844,10 +877,10 @@ void KdenliveDoc::setRenderer(Render *render) {
     emit progressInfo(QString(), -1);
 }*/
 
-void KdenliveDoc::checkProjectClips(bool displayRatioChanged)
+void KdenliveDoc::checkProjectClips(bool displayRatioChanged, bool fpsChanged)
 {
     if (m_render == NULL) return;
-    m_clipManager->resetProducersList(m_render->producersList(), displayRatioChanged);
+    m_clipManager->resetProducersList(m_render->producersList(), displayRatioChanged, fpsChanged);
 }
 
 Render *KdenliveDoc::renderer()
@@ -1327,7 +1360,7 @@ QString KdenliveDoc::getLadspaFile() const
 bool KdenliveDoc::checkDocumentClips(QDomNodeList infoproducers)
 {
     DocumentChecker d(infoproducers, m_document);
-    return (d.hasMissingClips() == false);
+    return (d.hasErrorInClips() == false);
 
     /*    int clipType;
         QDomElement e;
@@ -1585,18 +1618,18 @@ double KdenliveDoc::getDisplayRatio(const QString &path)
 void KdenliveDoc::backupLastSavedVersion(const QString &path)
 {
     // Ensure backup folder exists
+    if (path.isEmpty()) return;
     QFile file(path);
     KUrl backupFile = m_projectFolder;
     backupFile.addPath(".backup/");
     KIO::NetAccess::mkdir(backupFile, kapp->activeWindow());
     QString fileName = KUrl(path).fileName().section('.', 0, -2);
     QFileInfo info(file);
+    fileName.append("-" + m_documentProperties.value("documentid"));
     fileName.append(info.lastModified().toString("-yyyy-MM-dd-hh-mm"));
     fileName.append(".kdenlive");
     backupFile.addPath(fileName);
 
-    emit saveTimelinePreview(backupFile.path() + ".png");
-
     if (file.exists()) {
         // delete previous backup if it was done less than 60 seconds ago
         QFile::remove(backupFile.path());
@@ -1612,6 +1645,7 @@ void KdenliveDoc::cleanupBackupFiles()
     backupFile.addPath(".backup/");
     QDir dir(backupFile.path());
     QString projectFile = url().fileName().section('.', 0, -2);
+    projectFile.append("-" + m_documentProperties.value("documentid"));
     projectFile.append("-??");
     projectFile.append("??");
     projectFile.append("-??");