Commit 72c8a277 authored by Hugo Beauzée-Luyssen's avatar 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.
parent 10326928
......@@ -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()
......
......@@ -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 );
}
......@@ -36,12 +36,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.
*/
......@@ -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;
......
......@@ -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;
......
......@@ -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();
}
......@@ -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
......
......@@ -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();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment