| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2014 Eran Ifrah |
| 5 |
// file name : DbSettingDialog.cpp |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
|
| 26 |
#include "DbSettingDialog.h" |
| 27 |
#include "db_explorer_settings.h" |
| 28 |
#include "editor_config.h" |
| 29 |
#include "globals.h" |
| 30 |
#include "windowattrmanager.h" |
| 31 |
|
| 32 |
#include <wx/dblayer/include/DatabaseLayer.h> |
| 33 |
|
| 34 |
#ifdef DBL_USE_MYSQL |
| 35 |
#include <wx/dblayer/include/MysqlDatabaseLayer.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#ifdef DBL_USE_SQLITE |
| 39 |
#include <wx/dblayer/include/SqliteDatabaseLayer.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
#include <wx/dblayer/include/DatabaseErrorCodes.h> |
| 43 |
#include <wx/dblayer/include/DatabaseLayerException.h> |
| 44 |
|
| 45 |
DbSettingDialog::DbSettingDialog(DbViewerPanel* parent, wxWindow* pWindowParent) |
| 46 |
: _DBSettingsDialog(pWindowParent) |
| 47 |
{ |
| 48 |
|
| 49 |
m_pParent = parent; |
| 50 |
m_listCtrlRecentFiles->InsertColumn(0, _("File name")); |
| 51 |
m_listCtrlRecentFiles->SetColumnWidth(0, 600); |
| 52 |
m_filePickerSqlite->SetFocus(); |
| 53 |
|
| 54 |
LoadHistory(); |
| 55 |
#ifndef DBL_USE_MYSQL |
| 56 |
m_MySqlPanel->Enable(false); |
| 57 |
#endif |
| 58 |
#ifndef DBL_USE_POSTGRES |
| 59 |
m_PostgrePanel->Enable(false); |
| 60 |
#endif |
| 61 |
SetName("DbSettingDialog"); |
| 62 |
WindowAttrManager::Load(this); |
| 63 |
CentreOnParent(); |
| 64 |
} |
| 65 |
|
| 66 |
DbSettingDialog::~DbSettingDialog() {} |
| 67 |
|
| 68 |
void DbSettingDialog::OnCancelClick(wxCommandEvent& event) { event.Skip(); } |
| 69 |
|
| 70 |
void DbSettingDialog::OnMySqlOkClick(wxCommandEvent& event) |
| 71 |
{ |
| 72 |
#ifdef DBL_USE_MYSQL |
| 73 |
try { |
| 74 |
// MysqlDatabaseLayer *DbLayer = new |
| 75 |
// MysqlDatabaseLayer(m_txServer->GetValue(),wxT(""),m_txUserName->GetValue(),m_txPassword->GetValue()); |
| 76 |
IDbAdapter* adapt = |
| 77 |
new MySqlDbAdapter(m_txServer->GetValue(), m_txUserName->GetValue(), m_txPassword->GetValue()); |
| 78 |
|
| 79 |
wxString serverName = m_txServer->GetValue(); |
| 80 |
m_pParent->AddDbConnection(new DbConnection(adapt, serverName)); |
| 81 |
m_pParent->SetServer(serverName); |
| 82 |
event.Skip(); |
| 83 |
} catch(DatabaseLayerException& e) { |
| 84 |
wxString errorMessage = wxString::Format(_("Error (%d): %s"), e.GetErrorCode(), e.GetErrorMessage().c_str()); |
| 85 |
wxMessageDialog dlg(this, errorMessage, _("DB Error"), wxOK | wxCENTER | wxICON_ERROR); |
| 86 |
dlg.ShowModal(); |
| 87 |
} catch(...) { |
| 88 |
wxMessageDialog dlg(this, _("Unknown error."), _("DB Error"), wxOK | wxCENTER | wxICON_ERROR); |
| 89 |
dlg.ShowModal(); |
| 90 |
} |
| 91 |
#else |
| 92 |
wxMessageBox(_("MySQL connection is not supported."), _("DB Error"), wxOK | wxICON_WARNING); |
| 93 |
#endif |
| 94 |
} |
| 95 |
|
| 96 |
void DbSettingDialog::OnSqliteOkClick(wxCommandEvent& event) |
| 97 |
{ |
| 98 |
if(DoSQLiteItemActivated()) { |
| 99 |
event.Skip(); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
void DbSettingDialog::OnHistoryClick(wxCommandEvent& event) |
| 104 |
{ |
| 105 |
wxUnusedVar(event); |
| 106 |
#ifdef DBL_USE_MYSQL |
| 107 |
DoFindConnectionByName(DoLoadMySQLHistory(), m_listBox2->GetStringSelection()); |
| 108 |
#endif |
| 109 |
} |
| 110 |
|
| 111 |
void DbSettingDialog::OnHistoryDClick(wxCommandEvent& event) { event.Skip(); } |
| 112 |
|
| 113 |
void DbSettingDialog::LoadHistory() |
| 114 |
{ |
| 115 |
// recent sqlite files |
| 116 |
wxArrayString files = DoLoadSqliteHistory(); |
| 117 |
|
| 118 |
m_listCtrlRecentFiles->DeleteAllItems(); |
| 119 |
for(size_t i = 0; i < files.Count(); i++) { |
| 120 |
int idx = AppendListCtrlRow(m_listCtrlRecentFiles); |
| 121 |
SetColumnText(m_listCtrlRecentFiles, idx, 0, files.Item(i)); |
| 122 |
} |
| 123 |
#ifdef DBL_USE_MYSQL |
| 124 |
DbConnectionInfoVec mySqlConns = DoLoadMySQLHistory(); |
| 125 |
m_listBox2->Clear(); |
| 126 |
for(size_t i = 0; i < mySqlConns.size(); i++) { |
| 127 |
m_listBox2->Append(mySqlConns.at(i).GetConnectionName()); |
| 128 |
} |
| 129 |
#endif |
| 130 |
|
| 131 |
#ifdef DBL_USE_POSTGRES |
| 132 |
DbConnectionInfoVec pgSqlConns = DoLoadPgSQLHistory(); |
| 133 |
m_listBoxPg->Clear(); |
| 134 |
for(size_t i = 0; i < pgSqlConns.size(); i++) { |
| 135 |
m_listBoxPg->Append(pgSqlConns.at(i).GetConnectionName()); |
| 136 |
} |
| 137 |
#endif |
| 138 |
} |
| 139 |
|
| 140 |
void DbSettingDialog::OnPgOkClick(wxCommandEvent& event) |
| 141 |
{ |
| 142 |
#ifdef DBL_USE_POSTGRES |
| 143 |
try { |
| 144 |
// MysqlDatabaseLayer *DbLayer = new |
| 145 |
// MysqlDatabaseLayer(m_txServer->GetValue(),wxT(""),m_txUserName->GetValue(),m_txPassword->GetValue()); |
| 146 |
long portNumber = 0; |
| 147 |
m_txPgPort->GetValue().ToLong(&portNumber); |
| 148 |
IDbAdapter* adapt = new PostgreSqlDbAdapter(m_txPgServer->GetValue(), portNumber, m_txPgDatabase->GetValue(), |
| 149 |
m_txPgUserName->GetValue(), m_txPgPassword->GetValue()); |
| 150 |
|
| 151 |
wxString serverName = m_txPgServer->GetValue(); |
| 152 |
m_pParent->AddDbConnection(new DbConnection(adapt, serverName)); |
| 153 |
|
| 154 |
m_pParent->SetServer(serverName); |
| 155 |
} catch(DatabaseLayerException& e) { |
| 156 |
wxString errorMessage = wxString::Format(_("Error (%d): %s"), e.GetErrorCode(), e.GetErrorMessage().c_str()); |
| 157 |
wxMessageDialog dlg(this, errorMessage, _("DB Error"), wxOK | wxCENTER | wxICON_ERROR); |
| 158 |
dlg.ShowModal(); |
| 159 |
event.Skip(); |
| 160 |
} catch(...) { |
| 161 |
wxMessageDialog dlg(this, _("Unknown error."), _("DB Error"), wxOK | wxCENTER | wxICON_ERROR); |
| 162 |
dlg.ShowModal(); |
| 163 |
} |
| 164 |
#else |
| 165 |
wxMessageBox(_("PostgreSQL connection is not supported."), _("DB Error"), wxOK | wxICON_WARNING); |
| 166 |
#endif |
| 167 |
} |
| 168 |
|
| 169 |
void DbSettingDialog::OnPgHistoryClick(wxCommandEvent& event) |
| 170 |
{ |
| 171 |
wxUnusedVar(event); |
| 172 |
#ifdef DBL_USE_POSTGRES |
| 173 |
DoFindConnectionByName(DoLoadPgSQLHistory(), m_listBoxPg->GetStringSelection()); |
| 174 |
#endif |
| 175 |
} |
| 176 |
|
| 177 |
void DbSettingDialog::OnPgHistoryDClick(wxCommandEvent& event) { event.Skip(); } |
| 178 |
|
| 179 |
void DbSettingDialog::OnItemActivated(wxListEvent& event) |
| 180 |
{ |
| 181 |
long selecteditem = -1; |
| 182 |
selecteditem = m_listCtrlRecentFiles->GetNextItem(selecteditem, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED); |
| 183 |
m_filePickerSqlite->SetPath(GetColumnText(m_listCtrlRecentFiles, (int)selecteditem, 0)); |
| 184 |
if(DoSQLiteItemActivated()) { |
| 185 |
Close(); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
void DbSettingDialog::OnItemKeyDown(wxListEvent& event) |
| 190 |
{ |
| 191 |
if(event.GetKeyCode() == WXK_DELETE || event.GetKeyCode() == WXK_NUMPAD_DELETE) { |
| 192 |
m_listCtrlRecentFiles->DeleteItem(event.GetItem()); |
| 193 |
DoSaveSqliteHistory(); |
| 194 |
|
| 195 |
} else { |
| 196 |
event.Skip(); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
void DbSettingDialog::OnItemSelected(wxListEvent& event) |
| 201 |
{ |
| 202 |
long selecteditem = -1; |
| 203 |
selecteditem = m_listCtrlRecentFiles->GetNextItem(selecteditem, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED); |
| 204 |
|
| 205 |
m_filePickerSqlite->SetPath(GetColumnText(m_listCtrlRecentFiles, (int)selecteditem, 0)); |
| 206 |
} |
| 207 |
|
| 208 |
void DbSettingDialog::OnDlgOK(wxCommandEvent& event) |
| 209 |
{ |
| 210 |
// Dispatch the OK click |
| 211 |
int selection = m_notebook2->GetSelection(); |
| 212 |
switch(selection) { |
| 213 |
case 0: // Sqlite |
| 214 |
OnSqliteOkClick(event); |
| 215 |
break; |
| 216 |
case 1: // MySQL |
| 217 |
OnMySqlOkClick(event); |
| 218 |
break; |
| 219 |
case 2: // Pg |
| 220 |
OnPgOkClick(event); |
| 221 |
break; |
| 222 |
default: |
| 223 |
break; |
| 224 |
} |
| 225 |
|
| 226 |
// Save all the connections |
| 227 |
DoSaveSqliteHistory(); |
| 228 |
|
| 229 |
#ifdef DBL_USE_MYSQL |
| 230 |
DoSaveMySQLHistory(); |
| 231 |
#endif |
| 232 |
|
| 233 |
#ifdef DBL_USE_POSTGRES |
| 234 |
DoSavePgSQLHistory(); |
| 235 |
#endif |
| 236 |
} |
| 237 |
|
| 238 |
void DbSettingDialog::DoSaveSqliteHistory() |
| 239 |
{ |
| 240 |
// Save the recent opened files |
| 241 |
clConfig config(DBE_CONFIG_FILE); |
| 242 |
DbExplorerSettings settings; |
| 243 |
config.ReadItem(&settings); |
| 244 |
|
| 245 |
wxArrayString files = settings.GetRecentFiles(); |
| 246 |
|
| 247 |
wxString filename = m_filePickerSqlite->GetPath(); |
| 248 |
filename.Trim().Trim(false); |
| 249 |
if(filename.IsEmpty()) return; |
| 250 |
|
| 251 |
files.Insert(filename, 0); |
| 252 |
settings.SetRecentFiles(files); |
| 253 |
config.WriteItem(&settings); |
| 254 |
} |
| 255 |
|
| 256 |
wxArrayString DbSettingDialog::DoLoadSqliteHistory() |
| 257 |
{ |
| 258 |
clConfig config(DBE_CONFIG_FILE); |
| 259 |
DbExplorerSettings settings; |
| 260 |
config.ReadItem(&settings); |
| 261 |
return settings.GetRecentFiles(); |
| 262 |
} |
| 263 |
|
| 264 |
DbConnectionInfoVec DbSettingDialog::DoLoadMySQLHistory() |
| 265 |
{ |
| 266 |
clConfig config(DBE_CONFIG_FILE); |
| 267 |
DbExplorerSettings settings; |
| 268 |
config.ReadItem(&settings); |
| 269 |
return settings.GetMySQLConnections(); |
| 270 |
} |
| 271 |
|
| 272 |
DbConnectionInfoVec DbSettingDialog::DoLoadPgSQLHistory() |
| 273 |
{ |
| 274 |
clConfig config(DBE_CONFIG_FILE); |
| 275 |
DbExplorerSettings settings; |
| 276 |
config.ReadItem(&settings); |
| 277 |
return settings.GetPgSQLConnections(); |
| 278 |
} |
| 279 |
|
| 280 |
void DbSettingDialog::DoSaveMySQLHistory() |
| 281 |
{ |
| 282 |
clConfig config(DBE_CONFIG_FILE); |
| 283 |
DbExplorerSettings settings; |
| 284 |
config.ReadItem(&settings); |
| 285 |
DbConnectionInfoVec mysql = settings.GetMySQLConnections(); |
| 286 |
|
| 287 |
DbConnectionInfo conn; |
| 288 |
conn.SetConnectionType(DbConnectionInfo::DbConnTypeMySQL); |
| 289 |
conn.SetDefaultDatabase(wxT("")); |
| 290 |
conn.SetConnectionName(m_txName->GetValue()); |
| 291 |
conn.SetPassword(m_txPassword->GetValue()); |
| 292 |
conn.SetServer(m_txServer->GetValue()); |
| 293 |
conn.SetUsername(m_txUserName->GetValue()); |
| 294 |
|
| 295 |
if(!conn.IsValid()) return; |
| 296 |
|
| 297 |
// remove any connection with this name |
| 298 |
DbConnectionInfoVec::iterator iter = mysql.begin(); |
| 299 |
for(; iter != mysql.end(); ++iter) { |
| 300 |
if(iter->GetConnectionName() == conn.GetConnectionName()) { |
| 301 |
mysql.erase(iter); |
| 302 |
break; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
mysql.insert(mysql.begin(), conn); |
| 307 |
settings.SetMySQLConnections(mysql); |
| 308 |
config.WriteItem(&settings); |
| 309 |
} |
| 310 |
|
| 311 |
void DbSettingDialog::DoSavePgSQLHistory() |
| 312 |
{ |
| 313 |
clConfig config(DBE_CONFIG_FILE); |
| 314 |
DbExplorerSettings settings; |
| 315 |
config.ReadItem(&settings); |
| 316 |
DbConnectionInfoVec pgconns = settings.GetPgSQLConnections(); |
| 317 |
|
| 318 |
long port = 0; |
| 319 |
DbConnectionInfo conn; |
| 320 |
conn.SetConnectionType(DbConnectionInfo::DbConnTypePgSQL); |
| 321 |
conn.SetConnectionName(m_txPgName->GetValue()); |
| 322 |
conn.SetDefaultDatabase(m_txPgDatabase->GetValue()); |
| 323 |
conn.SetPassword(m_txPgPassword->GetValue()); |
| 324 |
conn.SetServer(m_txPgServer->GetValue()); |
| 325 |
m_txPgPort->GetValue().ToLong(&port); |
| 326 |
conn.SetPort(port); |
| 327 |
conn.SetUsername(m_txPgUserName->GetValue()); |
| 328 |
|
| 329 |
if(!conn.IsValid()) return; |
| 330 |
|
| 331 |
// remove any connection with this name |
| 332 |
DbConnectionInfoVec::iterator iter = pgconns.begin(); |
| 333 |
for(; iter != pgconns.end(); iter++) { |
| 334 |
if(iter->GetConnectionName() == conn.GetConnectionName()) { |
| 335 |
pgconns.erase(iter); |
| 336 |
break; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
pgconns.insert(pgconns.begin(), conn); |
| 341 |
settings.SetPgSQLConnections(pgconns); |
| 342 |
config.WriteItem(&settings); |
| 343 |
} |
| 344 |
|
| 345 |
void DbSettingDialog::DoFindConnectionByName(const DbConnectionInfoVec& conns, const wxString& name) |
| 346 |
{ |
| 347 |
for(size_t i = 0; i < conns.size(); i++) { |
| 348 |
if(conns.at(i).GetConnectionName() == name) { |
| 349 |
// we found the selected connection |
| 350 |
if(conns.at(i).GetConnectionType() == DbConnectionInfo::DbConnTypeMySQL) { |
| 351 |
// populate the MySQL fields |
| 352 |
m_txName->SetValue(conns.at(i).GetConnectionName()); |
| 353 |
m_txServer->SetValue(conns.at(i).GetServer()); |
| 354 |
m_txUserName->SetValue(conns.at(i).GetUsername()); |
| 355 |
m_txPassword->SetValue(conns.at(i).GetPassword()); |
| 356 |
|
| 357 |
} else { |
| 358 |
// populate the PgSQL fields |
| 359 |
m_txPgName->SetValue(conns.at(i).GetConnectionName()); |
| 360 |
m_txPgServer->SetValue(conns.at(i).GetServer()); |
| 361 |
m_txPgPort->SetValue(wxString::Format(wxT("%i"), conns.at(i).GetPort())); |
| 362 |
m_txPgUserName->SetValue(conns.at(i).GetUsername()); |
| 363 |
m_txPgPassword->SetValue(conns.at(i).GetPassword()); |
| 364 |
m_txPgDatabase->SetValue(conns.at(i).GetDefaultDatabase()); |
| 365 |
} |
| 366 |
return; |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
bool DbSettingDialog::DoSQLiteItemActivated() |
| 372 |
{ |
| 373 |
#ifdef DBL_USE_SQLITE |
| 374 |
try { |
| 375 |
IDbAdapter* pAdapt = new SQLiteDbAdapter(m_filePickerSqlite->GetPath()); |
| 376 |
|
| 377 |
// m_pParent->SetDbLayer(DbLayer); |
| 378 |
wxString serverName = m_filePickerSqlite->GetPath(); |
| 379 |
m_pParent->AddDbConnection(new DbConnection(pAdapt, serverName)); |
| 380 |
m_pParent->SetServer(serverName); |
| 381 |
return true; |
| 382 |
} catch(DatabaseLayerException& e) { |
| 383 |
wxString errorMessage = wxString::Format(_("Error (%d): %s"), e.GetErrorCode(), e.GetErrorMessage().c_str()); |
| 384 |
wxMessageDialog dlg(this, errorMessage, _("DB Error"), wxOK | wxCENTER | wxICON_ERROR); |
| 385 |
dlg.ShowModal(); |
| 386 |
return false; |
| 387 |
} catch(...) { |
| 388 |
wxMessageDialog dlg(this, _("Unknown error."), _("DB Error"), wxOK | wxCENTER | wxICON_ERROR); |
| 389 |
dlg.ShowModal(); |
| 390 |
return false; |
| 391 |
} |
| 392 |
#else |
| 393 |
wxMessageBox(_("SQLite connection is not supported."), _("DB Error"), wxOK | wxICON_WARNING); |
| 394 |
return false; |
| 395 |
#endif |
| 396 |
} |