Skip to content

Commit 6443f43

Browse files
committed
Add support to save and update recovery config
1 parent c4297fe commit 6443f43

7 files changed

Lines changed: 103 additions & 23 deletions

File tree

Source/CoreServices.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,20 @@ namespace CoreServices
241241
return std::move(dir);
242242
}
243243

244+
File getSavedStateDirectory() {
245+
#if defined(__APPLE__)
246+
File dir = File::getSpecialLocation(File::userApplicationDataDirectory).getChildFile("Application Support/open-ephys");
247+
#elif _WIN32
248+
File dir = File::getSpecialLocation(File::commonApplicationDataDirectory).getChildFile("Open Ephys");
249+
#else
250+
File dir = File::getSpecialLocation(File::userApplicationDataDirectory).getChildFile(".open-ephys");;
251+
#endif
252+
if (!dir.isDirectory()) {
253+
dir.createDirectory();
254+
}
255+
return std::move(dir);
256+
}
257+
244258
String getGUIVersion()
245259
{
246260
#define XSTR_DEF(s) #s

Source/CoreServices.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ PLUGIN_API const char* getApplicationResource(const char* name, int& size);
129129
/** Gets the default directory for user-initiated file saving/loading */
130130
PLUGIN_API File getDefaultUserSaveDirectory();
131131

132+
/** Gets the save directory for GUI-related file saving/loading */
133+
PLUGIN_API File getSavedStateDirectory();
134+
132135
/** Gets the GUI version */
133136
PLUGIN_API String getGUIVersion();
134137

Source/MainWindow.cpp

Lines changed: 70 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,6 @@
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+
}

Source/MainWindow.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ class MainWindow : public DocumentWindow
7474
from which the GUI is run. */
7575
void loadWindowBounds();
7676

77+
/** Checks whether the signal chains of both the config files (lastConfig.xml & recoveryConfig.xml)
78+
* match or not. */
79+
bool compareConfigFiles(File file1, File file2);
80+
7781
/** A pointer to the application's AudioComponent (owned by the MainWindow). */
7882
ScopedPointer<AudioComponent> audioComponent;
7983

Source/Processors/AudioNode/AudioEditor.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "AudioEditor.h"
2525
#include "../../Audio/AudioComponent.h"
2626
#include "../../AccessClass.h"
27+
#include "../../UI/EditorViewport.h"
2728
#include "../../UI/LookAndFeel/MaterialSliderLookAndFeel.h"
2829

2930

@@ -325,6 +326,9 @@ AudioConfigurationWindow::~AudioConfigurationWindow()
325326

326327
void AudioConfigurationWindow::closeButtonPressed()
327328
{
329+
File recoveryFile = CoreServices::getSavedStateDirectory().getChildFile("recoveryConfig.xml");
330+
AccessClass::getEditorViewport()->saveState(recoveryFile);
331+
328332
controlButton->setToggleState (false, dontSendNotification);
329333
setVisible (false);
330334
}

Source/Processors/Editors/GenericEditor.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,11 +552,12 @@ void GenericEditor::update()
552552
drawerButton->setVisible(true);
553553
}
554554

555-
556-
557555
updateVisualizer(); // does nothing unless this method
558556
// has been implemented
559557

558+
File recoveryFile = CoreServices::getSavedStateDirectory().getChildFile("recoveryConfig.xml");
559+
AccessClass::getEditorViewport()->saveState(recoveryFile);
560+
560561
}
561562

562563
const DataChannel* GenericEditor::getChannel(int chan) const

Source/UI/SignalChainManager.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
#include "EditorViewport.h"
2727

28+
#include "../AccessClass.h"
29+
2830
#include <iostream>
2931

3032
SignalChainManager::SignalChainManager
@@ -557,4 +559,7 @@ void SignalChainManager::updateProcessorSettings()
557559
}
558560
}
559561
}
562+
563+
File recoveryFile = CoreServices::getSavedStateDirectory().getChildFile("recoveryConfig.xml");
564+
AccessClass::getEditorViewport()->saveState(recoveryFile);
560565
}

0 commit comments

Comments
 (0)