|
1 | 1 | #include "FigApp.h"
|
2 | 2 | #include "FigWindow.h"
|
3 | 3 | #include <vector>
|
| 4 | +#include <fstream> |
4 | 5 | #include "kit/kit.h"
|
5 | 6 | #include "kit/log/log.h"
|
6 | 7 | #include <QDialogButtonBox>
|
| 8 | +#include <boost/filesystem.hpp> |
7 | 9 | using namespace std;
|
8 | 10 | using kit::make_unique;
|
| 11 | +namespace fs = boost::filesystem; |
| 12 | +using path = boost::filesystem::path; |
9 | 13 |
|
10 | 14 | FigApp :: FigApp(int& argc, char* argv[]):
|
11 | 15 | QApplication(argc,argv),
|
12 | 16 | m_Args(argc, (const char**)argv),
|
13 | 17 | m_FigAppPath(argv[0])
|
14 | 18 | {
|
| 19 | + path appPath = path(m_FigAppPath).parent_path(); |
| 20 | + |
15 | 21 | if(m_Args.size()>0)
|
16 |
| - m_SettingsFn = m_Args.get(m_Args.size()-1); |
| 22 | + m_SchemaFn = m_Args.get(m_Args.size()-1); |
17 | 23 | else
|
18 |
| - m_SettingsFn = "settings.json"; |
19 |
| - |
20 |
| - m_SchemaFn = m_Args.value_or("schema", "settings.schema.json"); |
21 |
| - |
22 |
| - try{ |
23 |
| - m_pSettings = make_shared<Meta>(m_SettingsFn); |
24 |
| - }catch(Error& e){ |
25 |
| - QMessageBox::critical(m_pWindow.get(), "Error", "Could not load settings file."); |
26 |
| - fail(); |
27 |
| - goto return_ctor; |
28 |
| - } |
| 24 | + m_SchemaFn = (appPath / m_SchemaFn).string(); |
| 25 | + |
| 26 | + path schemaPath = path(m_SchemaFn).parent_path(); |
29 | 27 |
|
30 | 28 | try{
|
31 | 29 | m_pSchema = make_shared<Meta>(m_SchemaFn);
|
32 |
| - //m_pSchema = make_shared<Schema>(make_shared<Meta>(m_SchemaFn)); |
33 | 30 | }catch(Error& e){
|
34 | 31 | QMessageBox::critical(m_pWindow.get(), "Error", "Could not load schema file.");
|
35 | 32 | fail();
|
36 | 33 | goto return_ctor;
|
37 | 34 | }
|
| 35 | + |
| 36 | + // use .settings path or default to same path as schema |
| 37 | + if(m_pSchema->has(".settings")) |
| 38 | + m_SettingsFn = m_pSchema->at<string>(".settings"); |
| 39 | + else |
| 40 | + m_SettingsFn = (schemaPath / m_SettingsFn).string(); |
38 | 41 |
|
| 42 | + if(fs::exists(m_SettingsFn)) |
| 43 | + { |
| 44 | + try{ |
| 45 | + m_pSettings = make_shared<Meta>(m_SettingsFn); |
| 46 | + }catch(...){ |
| 47 | + QMessageBox::critical(m_pWindow.get(), "Error", |
| 48 | + "Unable to load settings. The file may be corrupt." |
| 49 | + ); |
| 50 | + m_pSettings = make_shared<Meta>(); |
| 51 | + } |
| 52 | + } |
| 53 | + else |
| 54 | + m_pSettings = make_shared<Meta>(); |
| 55 | + |
39 | 56 | m_Title = m_Args.value_or("title", "Settings");
|
40 | 57 | if(m_pSchema->has(".title"))
|
41 | 58 | m_Title = m_pSchema->at<string>(".title");
|
@@ -452,7 +469,16 @@ bool FigApp :: save()
|
452 | 469 | }
|
453 | 470 | }
|
454 | 471 | if(not fail)
|
455 |
| - m_pSettings->serialize(); |
| 472 | + { |
| 473 | + try{ |
| 474 | + m_pSettings->serialize(m_SettingsFn); |
| 475 | + }catch(...){ |
| 476 | + QMessageBox::critical(m_pWindow.get(), "Error", |
| 477 | + "Unable to save settings." |
| 478 | + ); |
| 479 | + fail = true; |
| 480 | + } |
| 481 | + } |
456 | 482 | return not fail;
|
457 | 483 | }
|
458 | 484 |
|
|
0 commit comments