1 // Copyright 2008 Brian Caine
3 // This file is part of Potpourri.
5 // Potpourri is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Potpourri is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTIBILITY of FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Potpourri. If not, see <http://www.gnu.org/licenses/>.
21 // The source for the event pipeline
27 #include "../../include/core/SettingsDB.h"
29 using namespace fragrant
;
31 GameInfo
fragrant::makeGameInfo(std::string name
,
32 std::string url
, std::string last
, std::string controls
)
37 results
.last_presentation
= last
;
38 results
.controls
= controls
;
43 SettingsDB::SettingsDB(std::string dblocation
)
45 int result
= sqlite3_open(dblocation
.c_str(), &db
);
49 std::cerr
<< "SettingsDB::SettingsDB(): Error opening db, "
50 << sqlite3_errmsg(db
) << std::endl
;
52 throw std::runtime_error("");
55 std::string query
= "select * from sqlite_master;";
57 to_search
= "general";
58 sqlite3_exec(db
, query
.c_str(), mainquery
, this, 0);
62 std::string query
= "create table general(key varchar[10], "
63 "value varchar[20]);";
64 sqlite3_exec(db
, query
.c_str(), mainquery
, this, 0);
68 to_search
= "game_info";
70 sqlite3_exec(db
, query
.c_str(), mainquery
, this, 0);
74 std::string query
= "create table game_info(gamename varchar[10],"
75 " lastmode varchar[20], url varchar[40], cont"
76 "rols varchar[100]);";
77 sqlite3_exec(db
, query
.c_str(), mainquery
, this, 0);
80 plugin_folder
= getValueFromGeneral("plugin_folder");
81 game_presentations
= getGamePresentations();
84 SettingsDB::~SettingsDB()
86 setValueFromGeneral("plugin_folder", plugin_folder
);
87 setGamePresentations(game_presentations
);
92 std::string
& SettingsDB::pluginFolder()
97 std::vector
<GameInfo
> SettingsDB::getGameList()
99 std::vector
<GameInfo
> results
;
101 std::map
<std::string
, GameInfo
>::iterator iter
;
102 for (iter
= game_presentations
.begin();
103 iter
!= game_presentations
.end();
105 results
.push_back(iter
->second
);
110 GameInfo
& SettingsDB::getPresentationName(std::string game
)
112 if (game_presentations
.find(game
) == game_presentations
.end())
113 game_presentations
[game
] = makeGameInfo("", "", "");
115 return game_presentations
[game
];
118 std::string
SettingsDB::getValueFromGeneral(std::string value
)
120 std::string query
= "select * from general where key='" +
121 sanitizeString(value
) + "';";
122 general_callback_called
= false;
123 sqlite3_exec(db
, query
.c_str(), generalcallback
, this, 0);
124 if (general_callback_called
)
125 return general_callback_result
;
127 setValueFromGeneral(value
, "");
132 void SettingsDB::setValueFromGeneral(std::string key
, std::string newvalue
)
134 std::string query
= "select * from general where key='" +
135 sanitizeString(key
) + "';";
136 general_callback_called
= false;
137 sqlite3_exec(db
, query
.c_str(), generalcallback
, this, 0);
139 if (general_callback_called
)
141 std::string query
= "update general set value='" +
142 sanitizeString(newvalue
) + "' where key='" + sanitizeString(key
) + "';";
143 general_callback_called
= false;
144 sqlite3_exec(db
, query
.c_str(), generalcallback
, this, 0);
148 std::string query
= "insert into general values('" +
149 sanitizeString(key
) + "', '" + sanitizeString(newvalue
) +"');";
150 general_callback_called
= false;
151 sqlite3_exec(db
, query
.c_str(), generalcallback
, this, 0);
157 std::map
<std::string
, GameInfo
> SettingsDB::getGamePresentations()
160 std::string query
= "select * from game_info;";
162 sqlite3_exec(db
, query
.c_str(), content_callback
, this, 0);
167 void SettingsDB::setGamePresentations(
168 std::map
<std::string
, GameInfo
> presentations
)
170 std::map
<std::string
, GameInfo
> current_contents
= getGamePresentations();
172 std::map
<std::string
, GameInfo
>::iterator iter
;
173 for (iter
= presentations
.begin(); iter
!= presentations
.end(); iter
++)
176 if (current_contents
.find(iter
->first
) == current_contents
.end())
177 query
= "insert into game_info values('" +
178 sanitizeString(iter
->first
) + "', '" +
179 sanitizeString(iter
->second
.last_presentation
) + "', '" +
180 sanitizeString(iter
->second
.url
) + "', '" +
181 sanitizeString(iter
->second
.controls
) + "');";
184 query
= "update game_info set lastmode='" +
185 sanitizeString(iter
->second
.last_presentation
) +"', url='" +
186 sanitizeString(iter
->second
.url
) + "', controls='" +
187 sanitizeString(iter
->second
.controls
) + "' where gamename='" +
188 sanitizeString(iter
->first
) + "';";
190 sqlite3_exec(db
, query
.c_str(), generalcallback
, this, 0);
196 std::string
SettingsDB::sanitizeString(std::string input
)
198 pcrecpp::RE
replace_stuff("'");
199 replace_stuff
.GlobalReplace("''", &input
);
203 // my original code was buggy and obnoxious and longer than the
206 // note to self: don't try and reinvent the wheel
208 // unless the existing wheel is shitty
213 int SettingsDB::mainquery(void* sdb
, int argc
, char** argv
,
216 SettingsDB
* db
= static_cast<SettingsDB
*>(sdb
);
218 if (std::string(argv
[1]) == db
->to_search
)
219 db
->table_exists
= true;
224 int SettingsDB::generalcallback(void* sdb
, int argc
, char** argv
,
227 SettingsDB
* db
= static_cast<SettingsDB
*>(sdb
);
229 db
->general_callback_called
= true;
230 db
->general_callback_result
= argv
[1];
235 int SettingsDB::content_callback(void* sdb
, int argc
, char** argv
,
238 SettingsDB
* db
= static_cast<SettingsDB
*>(sdb
);
240 db
->contents
[argv
[0]] = makeGameInfo(argv
[0], argv
[2], argv
[1]);