1 /*******************************************************
2  Copyright (C) 2013,2014 Guan Lisheng (guanlisheng@gmail.com)
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  ********************************************************/
18 
19 #include "Model_Setting.h"
20 #include "constants.h"
21 #include "mmOption.h"
22 #include "paths.h"
23 
Model_Setting()24 Model_Setting::Model_Setting()
25 : Model<DB_Table_SETTING_V1>()
26 {
27 }
28 
~Model_Setting()29 Model_Setting::~Model_Setting()
30 {
31 }
32 
33 /**
34 * Initialize the global Model_Setting table.
35 * Reset the Model_Setting table or create the table if it does not exist.
36 */
instance(wxSQLite3Database * db)37 Model_Setting& Model_Setting::instance(wxSQLite3Database* db)
38 {
39     Model_Setting& ins = Singleton<Model_Setting>::instance();
40     ins.db_ = db;
41     ins.destroy_cache();
42     ins.ensure(db);
43     ins.preload();
44 
45     return ins;
46 }
47 
48 /** Return the static instance of Model_Setting table. */
instance()49 Model_Setting& Model_Setting::instance()
50 {
51     return Singleton<Model_Setting>::instance();
52 }
53 
54 // Setter
Set(const wxString & key,int value)55 void Model_Setting::Set(const wxString& key, int value)
56 {
57     this->Set(key, wxString::Format("%d", value));
58 }
59 
Set(const wxString & key,bool value)60 void Model_Setting::Set(const wxString& key, bool value)
61 {
62     this->Set(key, wxString::Format("%s", value ? "TRUE" : "FALSE"));
63 }
64 
Set(const wxString & key,const wxColour & value)65 void Model_Setting::Set(const wxString& key, const wxColour& value)
66 {
67     this->Set(key, wxString::Format("%d,%d,%d", value.Red(), value.Green(), value.Blue()));
68 }
69 
Set(const wxString & key,const wxString & value)70 void Model_Setting::Set(const wxString& key, const wxString& value)
71 {
72     Data* setting = this->get_one(SETTINGNAME(key));
73     if (!setting) // not cached
74     {
75         Data_Set items = this->find(SETTINGNAME(key));
76         if (!items.empty()) setting = this->get(items[0].SETTINGID, this->db_);
77     }
78     if (setting)
79     {
80         setting->SETTINGVALUE = value;
81         setting->save(this->db_);
82     }
83     else
84     {
85         setting = this->create();
86         setting->SETTINGNAME = key;
87         setting->SETTINGVALUE = value;
88         setting->save(this->db_);
89     }
90 }
91 
92 // Getter
GetBoolSetting(const wxString & key,bool default_value)93 bool Model_Setting::GetBoolSetting(const wxString& key, bool default_value)
94 {
95     wxString value = this->GetStringSetting(key, "");
96     if (value == "TRUE") return true;
97     if (value == "FALSE") return false;
98 
99     return default_value;
100 }
101 
GetIntSetting(const wxString & key,int default_value)102 int Model_Setting::GetIntSetting(const wxString& key, int default_value)
103 {
104     wxString value = this->GetStringSetting(key, "");
105     if (!value.IsEmpty() && value.IsNumber()) return wxAtoi(value);
106 
107     return default_value;
108 }
109 
GetStringSetting(const wxString & key,const wxString & default_value)110 wxString Model_Setting::GetStringSetting(const wxString& key, const wxString& default_value)
111 {
112     Data* setting = this->get_one(SETTINGNAME(key));
113     if (!setting) // not cached
114     {
115         Data_Set items = this->find(SETTINGNAME(key));
116         if (!items.empty()) return items[0].SETTINGVALUE;
117     }
118     else
119     {
120         return setting->SETTINGVALUE;
121     }
122     return default_value;
123 }
124 
getLastDbPath()125 wxString Model_Setting::getLastDbPath()
126 {
127     wxString path = this->GetStringSetting("LASTFILENAME", "");
128 
129     if (!mmex::isPortableMode()) return path;
130 
131     wxString vol = wxFileName(wxStandardPaths::Get().GetExecutablePath()).GetVolume();
132 
133     if (!vol.IsEmpty())
134     {
135 
136         wxFileName fname(path);
137         fname.SetVolume(vol); // database should be on portable device
138 
139         if (fname.FileExists()) {
140             path = fname.GetFullPath();
141         }
142     }
143 
144     return path;
145 }
146 
147 /* Returns true if key setting found */
ContainsSetting(const wxString & key)148 bool Model_Setting::ContainsSetting(const wxString& key)
149 {
150     return !this->find(SETTINGNAME(key)).empty();
151 }
152 
to_row_t()153 row_t Model_Setting::to_row_t()
154 {
155     row_t row;
156     for (const auto &r: instance().all())
157         row(r.SETTINGNAME.ToStdWstring()) = r.SETTINGVALUE;
158     return row;
159 }
160 //-------------------------------------------------------------------
161 
DisplayInternetNews()162 bool Model_Setting::DisplayInternetNews()
163 {
164     return GetBoolSetting(INIDB_DISPLAY_INTERNET_NEWS, true);
165 }
166 
SetDisplayInternetNews(bool display)167 void Model_Setting::SetDisplayInternetNews(bool display)
168 {
169     Set(INIDB_DISPLAY_INTERNET_NEWS, display);
170 }
171 
172 //-------------------------------------------------------------------
ViewAccounts()173 wxString Model_Setting::ViewAccounts()
174 {
175     return GetStringSetting("VIEWACCOUNTS", VIEW_ACCOUNTS_ALL_STR);
176 }
177 
SetViewAccounts(const wxString & value)178 void Model_Setting::SetViewAccounts(const wxString& value)
179 {
180     Set("VIEWACCOUNTS", value);
181 }
182 
183 //-------------------------------------------------------------------
ViewTransactions()184 wxString Model_Setting::ViewTransactions()
185 {
186     return GetStringSetting("VIEWTRANSACTIONS", VIEW_TRANS_ALL_STR);
187 }
188 
SetViewTransactions(const wxString & value)189 void Model_Setting::SetViewTransactions(const wxString& value)
190 {
191     Set("VIEWTRANSACTIONS", value);
192 }
193 
194 //-------------------------------------------------------------------
HtmlFontSize()195 int Model_Setting::HtmlFontSize()
196 {
197     return GetIntSetting("HTMLFONTSIZE", 3);
198 }
199 
SetHtmlFontSize(const int & size)200 void Model_Setting::SetHtmlFontSize(const int& size)
201 {
202     Set("HTMLFONTSIZE", size);
203     mmIniOptions::instance().html_font_size_ = size;
204 }
205 
206 //-------------------------------------------------------------------
BudgetFinancialYears()207 bool Model_Setting::BudgetFinancialYears()
208 {
209     return GetBoolSetting(INIDB_BUDGET_FINANCIAL_YEARS, false);
210 }
211 
SetBudgetFinancialYears(bool value)212 void Model_Setting::SetBudgetFinancialYears(bool value)
213 {
214     Set(INIDB_BUDGET_FINANCIAL_YEARS, value);
215     mmIniOptions::instance().budgetFinancialYears_ = value;
216 }
217 
218 //-------------------------------------------------------------------
BudgetIncludeTransfers()219 bool Model_Setting::BudgetIncludeTransfers()
220 {
221     return GetBoolSetting(INIDB_BUDGET_INCLUDE_TRANSFERS, false);
222 }
223 
SetBudgetIncludeTransfers(bool value)224 void Model_Setting::SetBudgetIncludeTransfers(bool value)
225 {
226     Set(INIDB_BUDGET_INCLUDE_TRANSFERS, value);
227     mmIniOptions::instance().budgetIncludeTransfers_ = value;
228 }
229 
230 //-------------------------------------------------------------------
BudgetSetupWithoutSummary()231 bool Model_Setting::BudgetSetupWithoutSummary()
232 {
233     return GetBoolSetting(INIDB_BUDGET_SETUP_WITHOUT_SUMMARY, false);
234 }
235 
SetBudgetSetupWithoutSummary(bool value)236 void Model_Setting::SetBudgetSetupWithoutSummary(bool value)
237 {
238     Set(INIDB_BUDGET_SETUP_WITHOUT_SUMMARY, value);
239     mmIniOptions::instance().budgetSetupWithoutSummaries_ = value;
240 }
241 
242 //-------------------------------------------------------------------
BudgetSummaryWithoutCategory()243 bool Model_Setting::BudgetSummaryWithoutCategory()
244 {
245     return GetBoolSetting(INIDB_BUDGET_SUMMARY_WITHOUT_CATEG, true);
246 }
247 
SetBudgetSummaryWithoutCategory(bool value)248 void Model_Setting::SetBudgetSummaryWithoutCategory(bool value)
249 {
250     Set(INIDB_BUDGET_SUMMARY_WITHOUT_CATEG, value);
251     mmIniOptions::instance().budgetReportWithSummaries_ = value;
252 }
253 
254 //-------------------------------------------------------------------
IgnoreFutureTransactions()255 bool Model_Setting::IgnoreFutureTransactions()
256 {
257     return GetBoolSetting(INIDB_IGNORE_FUTURE_TRANSACTIONS, false);
258 }
259 
SetIgnoreFutureTransactions(bool value)260 void Model_Setting::SetIgnoreFutureTransactions(bool value)
261 {
262     Set(INIDB_IGNORE_FUTURE_TRANSACTIONS, value);
263     mmIniOptions::instance().ignoreFutureTransactions_ = value;
264 }
265