1 //------------------------------------------------------------------------------
2 // emConfigModel.cpp
3 //
4 // Copyright (C) 2006-2008,2011,2014,2018-2020 Oliver Hamann.
5 //
6 // Homepage: http://eaglemode.sourceforge.net/
7 //
8 // This program is free software: you can redistribute it and/or modify it under
9 // the terms of the GNU General Public License version 3 as published by the
10 // Free Software Foundation.
11 //
12 // This program is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for
15 // more details.
16 //
17 // You should have received a copy of the GNU General Public License version 3
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 //------------------------------------------------------------------------------
20
21 #include <emCore/emConfigModel.h>
22
23
TrySave(bool force)24 void emConfigModel::TrySave(bool force)
25 {
26 if (Unsaved || force) {
27 GetRec().TrySave(InstallPath);
28 if (Unsaved) {
29 Unsaved=false;
30 Signal(ChangeSignal);
31 }
32 }
33 }
34
35
Save(bool force)36 void emConfigModel::Save(bool force)
37 {
38 try {
39 TrySave(force);
40 }
41 catch (const emException & exception) {
42 emFatalError("%s",exception.GetText().Get());
43 }
44 }
45
46
SetAutoSaveDelaySeconds(int seconds)47 void emConfigModel::SetAutoSaveDelaySeconds(int seconds)
48 {
49 AutoSaveDelaySeconds=seconds;
50 if (Unsaved && AutoSaveDelaySeconds>=0) {
51 AutoSaveTimer.Start(((emUInt64)AutoSaveDelaySeconds)*1000);
52 }
53 }
54
55
emConfigModel(emContext & context,const emString & name)56 emConfigModel::emConfigModel(emContext & context, const emString & name)
57 : emModel(context,name), Link(*this), AutoSaveTimer(GetScheduler())
58 {
59 Unsaved=false;
60 AutoSaveDelaySeconds=-1;
61 AddWakeUpSignal(AutoSaveTimer.GetSignal());
62 }
63
64
PostConstruct(emRec & rec,const emString & installPath)65 void emConfigModel::PostConstruct(emRec & rec, const emString & installPath)
66 {
67 Link.SetListenedRec(&rec);
68 InstallPath=installPath;
69 }
70
71
~emConfigModel()72 emConfigModel::~emConfigModel()
73 {
74 }
75
76
TryLoad()77 void emConfigModel::TryLoad()
78 {
79 GetRec().TryLoad(InstallPath);
80 if (Unsaved) {
81 Unsaved=false;
82 Signal(ChangeSignal);
83 }
84 }
85
86
Load()87 void emConfigModel::Load()
88 {
89 try {
90 TryLoad();
91 }
92 catch (const emException & exception) {
93 emFatalError("%s",exception.GetText().Get());
94 }
95 }
96
97
TryLoadOrInstall(const char * insSrcPath)98 void emConfigModel::TryLoadOrInstall(const char * insSrcPath)
99 {
100 if (emIsExistingPath(InstallPath)) {
101 TryLoad();
102 }
103 else {
104 emTryMakeDirectories(emGetParentPath(InstallPath));
105 if (insSrcPath) {
106 emTryCopyFileOrTree(InstallPath,insSrcPath);
107 TryLoad();
108 }
109 else {
110 GetRec().SetToDefault();
111 TrySave(true);
112 }
113 }
114 }
115
116
LoadOrInstall(const char * insSrcPath)117 void emConfigModel::LoadOrInstall(const char * insSrcPath)
118 {
119 try {
120 TryLoadOrInstall(insSrcPath);
121 }
122 catch (const emException & exception) {
123 emFatalError("%s",exception.GetText().Get());
124 }
125 }
126
127
Cycle()128 bool emConfigModel::Cycle()
129 {
130 bool busy;
131
132 busy=emModel::Cycle();
133 if (
134 IsSignaled(AutoSaveTimer.GetSignal()) &&
135 AutoSaveDelaySeconds>=0
136 ) {
137 Save();
138 }
139 return busy;
140 }
141
142
RecLink(emConfigModel & model)143 emConfigModel::RecLink::RecLink(emConfigModel & model)
144 : Model(model)
145 {
146 }
147
148
OnRecChanged()149 void emConfigModel::RecLink::OnRecChanged()
150 {
151 if (!Model.Unsaved) {
152 Model.Unsaved=true;
153 if (Model.AutoSaveDelaySeconds>=0) {
154 Model.AutoSaveTimer.Start(((emUInt64)Model.AutoSaveDelaySeconds)*1000);
155 }
156 }
157 Model.Signal(Model.ChangeSignal);
158 }
159