Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FbParams.cpp « MyRuLib - github.com/lintest/myrulib.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 70f7adcc5549a532151c7a1c6f68c5ba3017559a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <wx/stdpaths.h>
#include <wx/filename.h>
#include "FbParams.h"
#include "MyRuLibApp.h"

WX_DEFINE_OBJARRAY(ParamArray);

ParamItem::ParamItem(wxSQLite3ResultSet & result):
	id(result.GetInt(wxT("id"))),
	value(result.GetInt(wxT("value"))),
	text(result.GetString(wxT("text")))
{
}

ParamArray FbParams::sm_params;

wxCriticalSection FbParams::sm_queue;

FbParams::FbParams()
{
	m_database.AttachConfig();
}

void FbParams::LoadParams()
{
	wxCriticalSectionLocker enter(sm_queue);

	sm_params.Empty();

	wxString sql = wxT("SELECT id, value, text FROM config WHERE id>=100 UNION ALL SELECT id, value, text FROM params WHERE id<100");
	wxSQLite3ResultSet result = m_database.ExecuteQuery(sql);
	while (result.NextRow()) sm_params.Add(new ParamItem(result));
}

int FbParams::GetValue(const int param)
{
	wxCriticalSectionLocker enter(sm_queue);
	for (size_t i=0; i<sm_params.Count(); i++) {
		if (sm_params[i].id == param) {
			return sm_params[i].value;
		}
	}
	return DefaultValue(param);
};

wxString FbParams::GetText(const int param)
{
	wxCriticalSectionLocker enter(sm_queue);
	for (size_t i=0; i<sm_params.Count(); i++) {
		if (sm_params[i].id == param) {
			return sm_params[i].text;
		}
	}
	return DefaultText(param);
};

void FbParams::SetValue(const int param, int value)
{
	wxCriticalSectionLocker enter(sm_queue);

	const wchar_t * table = param < 100 ? wxT("params") : wxT("config");

	if (value == DefaultValue(param)) {
		wxString sql = wxString::Format( wxT("DELETE FROM %s WHERE id=?"), table);
		wxSQLite3Statement stmt = m_database.PrepareStatement(sql);
		stmt.Bind(1, param);
		stmt.ExecuteUpdate();
	} else {
		wxString sql = wxString::Format( wxT("INSERT OR REPLACE INTO %s (value, id) VALUES (?,?)"), table);
		wxSQLite3Statement stmt = m_database.PrepareStatement(sql);
		stmt.Bind(1, value);
		stmt.Bind(2, param);
		stmt.ExecuteUpdate();
	}

	for (size_t i=0; i<sm_params.Count(); i++) {
		if (sm_params[i].id == param) {
			sm_params[i].value = value;
			return;
		}
	}

	ParamItem * item = new ParamItem(param);
	item->value = value;
	sm_params.Add(item);
}

void FbParams::SetText(const int param, wxString text)
{
	wxCriticalSectionLocker enter(sm_queue);

	const wchar_t * table = param < 100 ? wxT("params") : wxT("config");

	if (text == DefaultValue(param)) {
		wxString sql = wxString::Format( wxT("DELETE FROM %s WHERE id=?"), table);
		wxSQLite3Statement stmt = m_database.PrepareStatement(sql);
		stmt.Bind(1, param);
		stmt.ExecuteUpdate();
	} else {
		wxString sql = wxString::Format( wxT("INSERT OR REPLACE INTO %s (text, id) VALUES (?,?)"), table);
		wxSQLite3Statement stmt = m_database.PrepareStatement(sql);
		stmt.Bind(1, text);
		stmt.Bind(2, param);
		stmt.ExecuteUpdate();
	}

	for (size_t i=0; i<sm_params.Count(); i++) {
		if (sm_params[i].id == param) {
			sm_params[i].text = text;
			return;
		}
	}

	ParamItem * item = new ParamItem(param);
	item->text = text;
	sm_params.Add(item);
}

int FbParams::DefaultValue(int param)
{
	switch (param) {
		case FB_MODE_AUTHOR: return 1;
		case FB_TRANSLIT_FOLDER: return 0;
		case FB_TRANSLIT_FILE: return 1;
		case FB_USE_PROXY: return 0;
		case FB_AUTO_DOWNLD: return 1;
		default: return 0;
	}
};

wxString FbParams::DefaultText(int param)
{
	switch (param) {
		case DB_LIBRARY_DIR:
			return wxGetApp().GetAppPath();
		case DB_WANRAIK_DIR:
			return wxGetApp().GetAppPath();
		case FB_LIBRUSEC_URL:
			return wxT("http://lib.rus.ec");
		case FB_DOWNLOAD_DIR:
			return FbStandardPaths().GetUserConfigDir() + wxFileName::GetPathSeparator() + wxT("download");
		default:
			return wxEmptyString;
	}
};