Skip to content

Commit

Permalink
Merge pull request #2176 from zonkmachine/recoverFileFix
Browse files Browse the repository at this point in the history
Recover file work over
  • Loading branch information
tresf committed Jan 19, 2016
2 parents 613b01c + 9e80207 commit d30a7df
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 22 deletions.
30 changes: 27 additions & 3 deletions include/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,29 @@ class MainWindow : public QMainWindow
///
bool mayChangeProject(bool stopPlayback);

void autoSaveTimerStart()
{
m_autoSaveTimer.start( 1000 * 60 ); // 1 minute
}

enum SessionState
{
Normal,
Recover,
Limited,
};

void setSession( SessionState session )
{
m_session = session;
}

SessionState getSession()
{
return m_session;
}

void sessionCleanup();

void clearKeyModifiers();

Expand Down Expand Up @@ -131,6 +154,8 @@ public slots:
void undo();
void redo();

void autoSave();
void runAutoSave();

protected:
virtual void closeEvent( QCloseEvent * _ce );
Expand All @@ -150,7 +175,6 @@ public slots:
void toggleWindow( QWidget *window, bool forceShow = false );
void refocus();


QMdiArea * m_workspace;

QWidget * m_toolBar;
Expand Down Expand Up @@ -187,6 +211,8 @@ public slots:

ToolButton * m_metronomeToggle;

SessionState m_session;

private slots:
void browseHelp();
void fillTemplatesMenu();
Expand All @@ -198,8 +224,6 @@ private slots:
void onToggleMetronome();


void autoSave();

signals:
void periodicUpdate();
void initProgress(const QString &msg);
Expand Down
121 changes: 112 additions & 9 deletions src/core/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <QTranslator>
#include <QApplication>
#include <QMessageBox>
#include <QPushButton>
#include <QTextStream>

#ifdef LMMS_BUILD_WIN32
Expand All @@ -61,6 +62,7 @@
#include "MemoryManager.h"
#include "ConfigManager.h"
#include "NotePlayHandle.h"
#include "embed.h"
#include "Engine.h"
#include "GuiApplication.h"
#include "ImportFilter.h"
Expand Down Expand Up @@ -644,13 +646,102 @@ int main( int argc, char * * argv )
// recover a file?
QString recoveryFile = ConfigManager::inst()->recoveryFile();

if( QFileInfo(recoveryFile).exists() &&
QMessageBox::question( gui->mainWindow(), MainWindow::tr( "Project recovery" ),
MainWindow::tr( "It looks like the last session did not end properly. "
"Do you want to recover the project of this session?" ),
QMessageBox::Yes | QMessageBox::No ) == QMessageBox::Yes )
{
fileToLoad = recoveryFile;
bool recoveryFilePresent = QFileInfo( recoveryFile ).exists() &&
QFileInfo( recoveryFile ).isFile();
bool autoSaveEnabled =
ConfigManager::inst()->value( "ui", "enableautosave" ).toInt();
if( recoveryFilePresent )
{
QMessageBox mb;
mb.setWindowTitle( MainWindow::tr( "Project recovery" ) );
mb.setText( QString(
"<html>"
"<p style=\"margin-left:6\">%1</p>"
"<table cellpadding=\"3\">"
" <tr>"
" <td><b>%2</b></td>"
" <td>%3</td>"
" </tr>"
" <tr>"
" <td><b>%4</b></td>"
" <td>%5</td>"
" </tr>"
" <tr>"
" <td><b>%6</b></td>"
" <td>%7</td>"
" </tr>"
" <tr>"
" <td><b>%8</b></td>"
" <td>%9</td>"
" </tr>"
"</table>"
"</html>" ).arg(
MainWindow::tr( "There is a recovery file present. "
"It looks like the last session did not end "
"properly or another instance of LMMS is "
"already running. Do you want to recover the "
"project of this session?" ),
MainWindow::tr( "Recover" ),
MainWindow::tr( "Recover the file. Please don't run "
"multiple instances of LMMS when you do this." ),
MainWindow::tr( "Ignore" ),
MainWindow::tr( "Launch LMMS as usual but with "
"automatic backup disabled to prevent the "
"present recover file from being overwritten." ),
MainWindow::tr( "Discard" ),
MainWindow::tr( "Launch a default session and delete "
"the restored files. This is not reversible." ),
MainWindow::tr( "Quit" ),
MainWindow::tr( "Shut down LMMS with no further action." )
) );

mb.setIcon( QMessageBox::Warning );
mb.setWindowIcon( embed::getIconPixmap( "icon" ) );

mb.setStandardButtons( QMessageBox::Ok |
QMessageBox::Discard );

mb.setButtonText( QMessageBox::Ok,
MainWindow::tr( "Recover" ) );

QAbstractButton * recover;
QAbstractButton * discard;
QPushButton * ignore;
QPushButton * exit;

recover = mb.QMessageBox::button( QMessageBox::Ok );
discard = mb.QMessageBox::button( QMessageBox::Discard );
ignore = mb.addButton( MainWindow::tr( "Ignore" ),
QMessageBox::NoRole );
ignore->setIcon( embed::getIconPixmap( "no_entry" ) );
exit = mb.addButton( MainWindow::tr( "Exit" ),
QMessageBox::RejectRole );
exit->setIcon( embed::getIconPixmap( "exit" ) );

mb.setDefaultButton( QMessageBox::Ok );
mb.setEscapeButton( exit );

mb.exec();
if( mb.clickedButton() == discard )
{
gui->mainWindow()->sessionCleanup();
}
else if( mb.clickedButton() == recover ) // ::Recover
{
fileToLoad = recoveryFile;
gui->mainWindow()->setSession( MainWindow::SessionState::Recover );
}
else if( mb.clickedButton() == ignore )
{
if( autoSaveEnabled )
{
gui->mainWindow()->setSession( MainWindow::SessionState::Limited );
}
}
else // Exit
{
return 0;
}
}

// we try to load given file
Expand Down Expand Up @@ -689,10 +780,13 @@ int main( int argc, char * * argv )
{

// If enabled, open last project if there is one. Else, create
// a new one.
// a new one. Also skip recently opened file if limited session to
// lower the chance of opening an already opened file.
if( ConfigManager::inst()->
value( "app", "openlastproject" ).toInt() &&
!ConfigManager::inst()->recentlyOpenedProjects().isEmpty() )
!ConfigManager::inst()->recentlyOpenedProjects().isEmpty() &&
gui->mainWindow()->getSession()
!= MainWindow::SessionState::Limited )
{
QString f = ConfigManager::inst()->
recentlyOpenedProjects().first();
Expand Down Expand Up @@ -720,6 +814,15 @@ int main( int argc, char * * argv )
gui->mainWindow()->showMaximized();
}
}
// Finally we start the auto save timer and also trigger the
// autosave one time as recover.mmp is a signal to possible other
// instances of LMMS.
if( autoSaveEnabled &&
gui->mainWindow()->getSession() != MainWindow::SessionState::Limited )
{
gui->mainWindow()->runAutoSave();
gui->mainWindow()->autoSaveTimerStart();
}
}

const int ret = app->exec();
Expand Down
Loading

0 comments on commit d30a7df

Please sign in to comment.