[vlmc-devel] commit: ProjectManager: Better support for backup files. ( Hugo Beauzée-Luyssen )

git at videolan.org git at videolan.org
Tue Jun 1 02:20:00 CEST 2010


vlmc | branch: master | Hugo Beauzée-Luyssen <beauze.h at gmail.com> | Tue Jun  1 01:24:26 2010 +0200| [72c8a27776fce62a5cc84e31bdfb051c5f2478b8] | committer: Hugo Beauzée-Luyssen 

ProjectManager: Better support for backup files.

If a backup file is more recent than the actual project file, the user
is asked if she wants to load the backup.
If an outdated backup is found, it can be deleted.
Code has been cleaned up a bit.

> http://git.videolan.org/gitweb.cgi/vlmc.git/?a=commit;h=72c8a27776fce62a5cc84e31bdfb051c5f2478b8
---

 src/Gui/MainWindow.cpp                |    3 +-
 src/Gui/project/GuiProjectManager.cpp |   58 +++++++++++++++++++++++++--------
 src/Gui/project/GuiProjectManager.h   |   14 ++++----
 src/Gui/wizard/WelcomePage.cpp        |    7 +++-
 src/Main/guimain.cpp                  |    2 +-
 src/Project/ProjectManager.cpp        |   28 ++++++++-------
 src/Project/ProjectManager.h          |    2 +
 7 files changed, 76 insertions(+), 38 deletions(-)

diff --git a/src/Gui/MainWindow.cpp b/src/Gui/MainWindow.cpp
index 1a1a9c5..7192673 100644
--- a/src/Gui/MainWindow.cpp
+++ b/src/Gui/MainWindow.cpp
@@ -301,8 +301,7 @@ void    MainWindow::on_actionSave_As_triggered()
 
 void    MainWindow::on_actionLoad_Project_triggered()
 {
-    GUIProjectManager* pm = GUIProjectManager::getInstance();
-    pm->loadProject( pm->acquireProjectFileName() );
+    GUIProjectManager::getInstance()->loadProject();
 }
 
 void MainWindow::createStatusBar()
diff --git a/src/Gui/project/GuiProjectManager.cpp b/src/Gui/project/GuiProjectManager.cpp
index cb99b84..86261e9 100644
--- a/src/Gui/project/GuiProjectManager.cpp
+++ b/src/Gui/project/GuiProjectManager.cpp
@@ -22,6 +22,7 @@
 
 #include "GuiProjectManager.h"
 
+#include <QDateTime>
 #include <QFileDialog>
 #include <QMessageBox>
 #include <QTimer>
@@ -89,16 +90,6 @@ GUIProjectManager::askForSaveIfModified()
     return true;
 }
 
-QString
-GUIProjectManager::acquireProjectFileName()
-{
-    QString fileName =
-            QFileDialog::getOpenFileName( NULL, "Enter the output file name",
-                                          VLMC_PROJECT_GET_STRING( "general/VLMCWorkspace" ),
-                                          "VLMC project file(*.vlmc)" );
-    return fileName;
-}
-
 bool
 GUIProjectManager::createNewProjectFile( bool saveAs )
 {
@@ -154,7 +145,7 @@ GUIProjectManager::autoSaveRequired()
 {
     if ( m_projectFile == NULL )
         return ;
-    ProjectManager::__saveProject( createAutoSaveOutputFileName() );
+    ProjectManager::__saveProject( createAutoSaveOutputFileName( m_projectFile->fileName() ) );
 }
 
 void
@@ -220,8 +211,47 @@ GUIProjectManager::loadTimeline( const QDomElement &root )
     Timeline::getInstance()->load( root );
 }
 
-QString
-GUIProjectManager::createAutoSaveOutputFileName() const
+void
+GUIProjectManager::loadProject()
 {
-    return m_projectFile->fileName() + "~";
+    QString fileName =
+            QFileDialog::getOpenFileName( NULL, "Enter the output file name",
+                                          VLMC_PROJECT_GET_STRING( "general/VLMCWorkspace" ),
+                                          "VLMC project file(*.vlmc)" );
+    if ( fileName.length() <= 0 ) //If the user canceled.
+        return ;
+    QFile   projectFile( fileName );
+    //If for some reason this happens... better safe than sorry
+    if ( projectFile.exists() == false )
+        return ;
+
+    QString backupFilename = createAutoSaveOutputFileName( fileName );
+    QFile   autoBackup( backupFilename );
+    if ( autoBackup.exists() == true )
+    {
+        QFileInfo       projectFileInfo( projectFile );
+        QFileInfo       autobackupFileInfo( autoBackup );
+
+        if ( autobackupFileInfo.lastModified() > projectFileInfo.lastModified() )
+        {
+            if ( QMessageBox::question( NULL, tr( "Backup file" ),
+                                        tr( "A backup file exists for this project. "
+                                        "Do you want to load it ?" ),
+                                        QMessageBox::Ok | QMessageBox::No ) == QMessageBox::Ok )
+            {
+                fileName = backupFilename;
+            }
+        }
+        else
+        {
+            if ( QMessageBox::question( NULL, tr( "Backup file" ),
+                                        tr( "An outdated backup file was found. "
+                                       "Do you want to erase it ?" ),
+                                        QMessageBox::Ok | QMessageBox::No ) == QMessageBox::Ok )
+            {
+                autoBackup.remove();
+            }
+        }
+    }
+    ProjectManager::loadProject( fileName );
 }
diff --git a/src/Gui/project/GuiProjectManager.h b/src/Gui/project/GuiProjectManager.h
index e24c113..a202f07 100644
--- a/src/Gui/project/GuiProjectManager.h
+++ b/src/Gui/project/GuiProjectManager.h
@@ -37,12 +37,6 @@ public:
     bool            askForSaveIfModified();
     void            newProject( const QString& projectName );
     /**
-     *  \brief      Ask the user for the project file she wants to load.
-     *
-     *  \return     The project to load.
-     */
-    QString         acquireProjectFileName();
-    /**
      *  \brief      Save the project using the current project file.
      */
     void            saveProject( bool saveAs = false );
@@ -55,6 +49,13 @@ public:
      */
     bool            closeProject();
     bool            needSave() const;
+    /**
+     *  \brief      Display the open file name dialog, and call the actual project loading
+     *              method.
+     *
+     *  \warning    This is not an overload for the ProjectManager::loadProject() method.
+     */
+    void            loadProject();
 
 protected:
     virtual void    failedToLoad( const QString &reason ) const;
@@ -63,7 +64,6 @@ protected:
 
 private:
     bool            createNewProjectFile( bool saveAs );
-    QString         createAutoSaveOutputFileName() const;
 
 private:
     QTimer*         m_timer;
diff --git a/src/Gui/wizard/WelcomePage.cpp b/src/Gui/wizard/WelcomePage.cpp
index 1b4062b..acaddb2 100644
--- a/src/Gui/wizard/WelcomePage.cpp
+++ b/src/Gui/wizard/WelcomePage.cpp
@@ -23,7 +23,9 @@
 #include "WelcomePage.h"
 
 #include "project/GuiProjectManager.h"
+#include "SettingsManager.h"
 
+#include <QFileDialog>
 #include <QLabel>
 #include <QPushButton>
 #include <QVBoxLayout>
@@ -130,7 +132,10 @@ void WelcomePage::loadRecentsProjects()
 
 void WelcomePage::loadProject()
 {
-    QString projectPath = GUIProjectManager::getInstance()->acquireProjectFileName();
+    QString projectPath =
+            QFileDialog::getOpenFileName( NULL, "Enter the output file name",
+                                          VLMC_PROJECT_GET_STRING( "general/VLMCWorkspace" ),
+                                          "VLMC project file(*.vlmc)" );
 
     if ( projectPath.isEmpty() ) return;
 
diff --git a/src/Main/guimain.cpp b/src/Main/guimain.cpp
index 2bb72ce..f6520c6 100644
--- a/src/Main/guimain.cpp
+++ b/src/Main/guimain.cpp
@@ -102,7 +102,7 @@ VLMCmain( int argc, char **argv )
 
     MainWindow w;
     if ( argc > 1 )
-        GUIProjectManager::getInstance()->loadProject( argv[argc - 1] );
+        GUIProjectManager::getInstance()->ProjectManager::loadProject( argv[argc - 1] );
     w.show();
     return app.exec();
 }
diff --git a/src/Project/ProjectManager.cpp b/src/Project/ProjectManager.cpp
index cae08cf..a7f78e8 100644
--- a/src/Project/ProjectManager.cpp
+++ b/src/Project/ProjectManager.cpp
@@ -38,6 +38,7 @@
 
 const QString   ProjectManager::unNamedProject = tr( "<Unnamed project>" );
 const QString   ProjectManager::unSavedProject = tr( "<Unsaved project>" );
+const QString   ProjectManager::backupSuffix = "~";
 
 ProjectManager::ProjectManager() : m_projectFile( NULL ), m_needSave( false )
 {
@@ -100,6 +101,7 @@ void    ProjectManager::loadWorkflow()
 
 void    ProjectManager::loadProject( const QString& fileName )
 {
+    //FIXME:this is probably useless, as this is handled by the gui part now.
     //Don't print an error. The user most likely canceled the open project dialog.
     if ( fileName.isEmpty() == true )
         return ;
@@ -110,15 +112,14 @@ void    ProjectManager::loadProject( const QString& fileName )
     if ( m_projectFile->open( QFile::ReadOnly ) == false )
     {
         failedToLoad( tr( "Can't open project file. (%1)" ).arg( m_projectFile->errorString() ) );
+        delete m_projectFile;
+        m_projectFile = NULL;
         return ;
     }
     m_projectFile->close();
 
     m_domDocument = new QDomDocument;
     m_domDocument->setContent( m_projectFile );
-#ifdef WITH_GUI
-    m_needSave = false;
-#endif
     if ( ProjectManager::isBackupFile( fileName ) == true )
     {
         //Delete the project file representation, so the next time the user
@@ -160,16 +161,10 @@ void    ProjectManager::emergencyBackup()
     QString     name;
 
     if ( m_projectFile != NULL )
-    {
-        name = m_projectFile->fileName();
-        name += "backup";
-        __saveProject( name );
-    }
+        name = createAutoSaveOutputFileName( m_projectFile->fileName() );
     else
-    {
-       name = QDir::currentPath() + "/unsavedproject.vlmcbackup";
-        __saveProject( name );
-    }
+       name = createAutoSaveOutputFileName( QDir::currentPath() + "/unsavedproject" );
+    __saveProject( name );
     QSettings   s;
     s.setValue( "EmergencyBackup", name );
     s.sync();
@@ -177,9 +172,16 @@ void    ProjectManager::emergencyBackup()
 
 bool    ProjectManager::isBackupFile( const QString& projectFile )
 {
-    return projectFile.endsWith( "backup" );
+    return projectFile.endsWith( ProjectManager::backupSuffix );
 }
 
+QString
+ProjectManager::createAutoSaveOutputFileName( const QString& baseName ) const
+{
+    return baseName + ProjectManager::backupSuffix;
+}
+
+
 void    ProjectManager::appendToRecentProject( const QString& projectFile )
 {
     // Append the item to the recents list
diff --git a/src/Project/ProjectManager.h b/src/Project/ProjectManager.h
index b0e6d4e..75461a8 100644
--- a/src/Project/ProjectManager.h
+++ b/src/Project/ProjectManager.h
@@ -45,6 +45,7 @@ class   ProjectManager : public QObject , public Singleton<ProjectManager>
 public:
     static const QString    unNamedProject;
     static const QString    unSavedProject;
+    static const QString    backupSuffix;
 
     void            loadProject( const QString& fileName );
     QStringList     recentsProjects() const;
@@ -80,6 +81,7 @@ protected:
 
     virtual void    failedToLoad( const QString& reason ) const;
     virtual void    loadTimeline( const QDomElement& ){};
+    QString         createAutoSaveOutputFileName( const QString& baseName ) const;
 
     ProjectManager();
     ~ProjectManager();



More information about the Vlmc-devel mailing list