2727#include < stdio.h>
2828// -----------------------------------------------------------------------
2929
30- static inline File getSavedStateDirectory () {
31- #if defined(__APPLE__)
32- File dir = File::getSpecialLocation (File::userApplicationDataDirectory).getChildFile (" Application Support/open-ephys" );
33- #elif _WIN32
34- File dir = File::getSpecialLocation (File::commonApplicationDataDirectory).getChildFile (" Open Ephys" );
35- #else
36- File dir = File::getSpecialLocation (File::userApplicationDataDirectory).getChildFile (" .open-ephys" );;
37- #endif
38- if (!dir.isDirectory ()) {
39- dir.createDirectory ();
40- }
41- return std::move (dir);
42- }
4330
4431 MainWindow::MainWindow (const File& fileToLoad)
4532: DocumentWindow(JUCEApplication::getInstance()->getApplicationName(),
@@ -91,11 +78,31 @@ static inline File getSavedStateDirectory() {
9178 }
9279 else if (shouldReloadOnStartup)
9380 {
94- File file = getSavedStateDirectory ().getChildFile (" lastConfig.xml" );
95- ui->getEditorViewport ()->loadState (file);
96- }
97-
81+ File lastConfig = CoreServices::getSavedStateDirectory ().getChildFile (" lastConfig.xml" );
82+ File recoveryConfig = CoreServices::getSavedStateDirectory ().getChildFile (" recoveryConfig.xml" );
9883
84+ if (lastConfig.existsAsFile ())
85+ {
86+ if (compareConfigFiles (lastConfig, recoveryConfig))
87+ {
88+ std::cout << " Loading last config.\n " << std::endl;
89+ ui->getEditorViewport ()->loadState (lastConfig);
90+ }
91+ else
92+ {
93+ bool loadRecovery = AlertWindow::showOkCancelBox (AlertWindow::WarningIcon, " Reloading Settings" ,
94+ " It looks like the GUI crashed during your last run, "
95+ " causing the configured settings to not save properly. "
96+ " Do you want to load the recovery config instead?" ,
97+ " Yes" , " No" );
98+
99+ if (loadRecovery)
100+ ui->getEditorViewport ()->loadState (recoveryConfig);
101+ else
102+ ui->getEditorViewport ()->loadState (lastConfig);
103+ }
104+ }
105+ }
99106
100107}
101108
@@ -114,8 +121,10 @@ MainWindow::~MainWindow()
114121 UIComponent* ui = (UIComponent*) getContentComponent ();
115122 ui->disableDataViewport ();
116123
117- File file = getSavedStateDirectory ().getChildFile (" lastConfig.xml" );
118- ui->getEditorViewport ()->saveState (file);
124+ File lastConfig = CoreServices::getSavedStateDirectory ().getChildFile (" lastConfig.xml" );
125+ File recoveryConfig = CoreServices::getSavedStateDirectory ().getChildFile (" recoveryConfig.xml" );
126+ ui->getEditorViewport ()->saveState (lastConfig);
127+ ui->getEditorViewport ()->saveState (recoveryConfig);
119128
120129 setMenuBar (0 );
121130
@@ -148,7 +157,7 @@ void MainWindow::saveWindowBounds()
148157 std::cout << " Saving window bounds." << std::endl;
149158 std::cout << std::endl;
150159
151- File file = getSavedStateDirectory ().getChildFile (" windowState.xml" );
160+ File file = CoreServices:: getSavedStateDirectory ().getChildFile (" windowState.xml" );
152161
153162 XmlElement* xml = new XmlElement (" MAINWINDOW" );
154163
@@ -194,7 +203,7 @@ void MainWindow::loadWindowBounds()
194203 std::cout << " Loading window bounds." << std::endl;
195204 std::cout << std::endl;
196205
197- File file = getSavedStateDirectory ().getChildFile (" windowState.xml" );
206+ File file = CoreServices:: getSavedStateDirectory ().getChildFile (" windowState.xml" );
198207
199208 XmlDocument doc (file);
200209 XmlElement* xml = doc.getDocumentElement ();
@@ -261,3 +270,43 @@ void MainWindow::loadWindowBounds()
261270 }
262271 // return "Everything went ok.";
263272}
273+
274+
275+ bool MainWindow::compareConfigFiles (File file1, File file2)
276+ {
277+ XmlDocument lcDoc (file1);
278+ XmlDocument rcDoc (file2);
279+
280+ std::unique_ptr<XmlElement> lcXml (lcDoc.getDocumentElement ());
281+ std::unique_ptr<XmlElement> rcXml (rcDoc.getDocumentElement ());
282+
283+ if (rcXml == 0 || ! rcXml->hasTagName (" SETTINGS" ))
284+ {
285+ std::cout << " Recovery config is inavlid. Loading lastConfig.xml" << std::endl;
286+ }
287+
288+ auto lcSig = lcXml->getChildByName (" SIGNALCHAIN" );
289+ auto rcSig = rcXml->getChildByName (" SIGNALCHAIN" );
290+
291+ if (lcSig == nullptr )
292+ {
293+ if (rcSig != nullptr )
294+ return false ;
295+ }
296+ else
297+ {
298+ if (rcSig != nullptr )
299+ {
300+ if (!lcSig->isEquivalentTo (rcSig, false ))
301+ return false ;
302+ }
303+ }
304+
305+ auto lcAudio = lcXml->getChildByName (" Audio" );
306+ auto rcAudio = rcXml->getChildByName (" Audio" );
307+
308+ if (!lcAudio->isEquivalentTo (rcAudio, false ))
309+ return false ;
310+
311+ return true ;
312+ }
0 commit comments