Skip to content

Commit 18e8597

Browse files
committed
modifiable paths, settings file created by default
1 parent 5ba7d7e commit 18e8597

File tree

4 files changed

+51
-22
lines changed

4 files changed

+51
-22
lines changed

README.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ qmake
2424
make
2525
```
2626

27-
Fig looks for the settings files in the bin directory.
27+
Fig looks for the schema file in the program's directory. The default filename is "settings.schema.json".
2828

29-
The file names should be:
29+
You can load a different schema by passing it in:
3030

31-
- settings.json (this is where it saves, should contain "{}" at first)
32-
- settings.schema.json (your schema here)
31+
```
32+
./fig schema.json
33+
```
34+
35+
The default save path is settings.json in the program's directory.
36+
See *Schema / Application Info* on how to change where it saves.
3337

3438
## How it Works
3539

@@ -204,6 +208,7 @@ To customize the dialog further, use these options. They are all optional.
204208
".icon-width": 64,
205209
".icon-height": 64,
206210
".footer": "Visit our website at : <a href=\"http://website.com\">website.com</a>",
211+
".settings": "settings.json"
207212
}
208213
```
209214

bin/settings.json

-1
This file was deleted.

src/FigApp.cpp

+40-14
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,58 @@
11
#include "FigApp.h"
22
#include "FigWindow.h"
33
#include <vector>
4+
#include <fstream>
45
#include "kit/kit.h"
56
#include "kit/log/log.h"
67
#include <QDialogButtonBox>
8+
#include <boost/filesystem.hpp>
79
using namespace std;
810
using kit::make_unique;
11+
namespace fs = boost::filesystem;
12+
using path = boost::filesystem::path;
913

1014
FigApp :: FigApp(int& argc, char* argv[]):
1115
QApplication(argc,argv),
1216
m_Args(argc, (const char**)argv),
1317
m_FigAppPath(argv[0])
1418
{
19+
path appPath = path(m_FigAppPath).parent_path();
20+
1521
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);
1723
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();
2927

3028
try{
3129
m_pSchema = make_shared<Meta>(m_SchemaFn);
32-
//m_pSchema = make_shared<Schema>(make_shared<Meta>(m_SchemaFn));
3330
}catch(Error& e){
3431
QMessageBox::critical(m_pWindow.get(), "Error", "Could not load schema file.");
3532
fail();
3633
goto return_ctor;
3734
}
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();
3841

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+
3956
m_Title = m_Args.value_or("title", "Settings");
4057
if(m_pSchema->has(".title"))
4158
m_Title = m_pSchema->at<string>(".title");
@@ -452,7 +469,16 @@ bool FigApp :: save()
452469
}
453470
}
454471
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+
}
456482
return not fail;
457483
}
458484

src/FigApp.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "FigWindow.h"
1010
#include "kit/args/args.h"
1111
#include "kit/meta/meta.h"
12-
#include "kit/meta/schema.h"
1312

1413
class FigApp:
1514
public QApplication
@@ -63,8 +62,8 @@ class FigApp:
6362

6463
std::string m_FigAppPath;
6564

66-
std::string m_SettingsFn;
67-
std::string m_SchemaFn;
65+
std::string m_SettingsFn = "settings.json";
66+
std::string m_SchemaFn = "settings.schema.json";
6867
std::shared_ptr<Meta> m_pSettings;
6968
std::shared_ptr<Meta> m_pSchema;
7069
std::string m_Title;

0 commit comments

Comments
 (0)