From e958eb995e273019a697eb008d8c1b48405fe7ce Mon Sep 17 00:00:00 2001 From: apoorv569 Date: Sun, 25 Apr 2021 03:44:11 +0530 Subject: [PATCH] Removing unnecessary instantiation to SettingsDialog. --- src/Browser.cpp | 27 +++++++------ src/Database.cpp | 89 +++++++++++++++++++++++++++++++++++------- src/Database.hpp | 7 ++++ src/SettingsDialog.cpp | 6 +++ src/SettingsDialog.hpp | 4 +- 5 files changed, 106 insertions(+), 27 deletions(-) diff --git a/src/Browser.cpp b/src/Browser.cpp index 731bb8a..23baa0a 100644 --- a/src/Browser.cpp +++ b/src/Browser.cpp @@ -29,7 +29,6 @@ #include "Tags.hpp" // #include "TreeItemDialog.hpp" #include "Serialize.hpp" -#include "wx/dataview.h" #include @@ -172,9 +171,6 @@ Browser::Browser(wxWindow* window) // Enable dragging a file from SampleListView m_SampleListView->EnableDragSource(wxDF_FILENAME); - // Restore the data previously added to SampleListView - LoadDatabase(); - // Initialize wxInfoBar for showing information inside application m_InfoBar = new wxInfoBar(m_BottomRightPanel); @@ -306,6 +302,13 @@ Browser::Browser(wxWindow* window) m_BottomRightPanelMainSizer->Fit(m_BottomRightPanel); m_BottomRightPanelMainSizer->SetSizeHints(m_BottomRightPanel); m_BottomRightPanelMainSizer->Layout(); + + // Initialize the database + Database db(*m_InfoBar); + db.CreateDatabase(); + + // Restore the data previously added to SampleListView + LoadDatabase(); } void Browser::OnClickSettings(wxCommandEvent& event) @@ -335,7 +338,7 @@ wxString TagLibTowx(const TagLib::String& in) void Browser::AddSamples(wxString file) { - Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); + Settings settings(m_ConfigFilepath, m_DatabaseFilepath); Database db(*m_InfoBar); std::string path = file.ToStdString(); @@ -442,7 +445,7 @@ void Browser::OnDragAndDropToSampleListView(wxDropFilesEvent& event) void Browser::OnAutoImportDir() { - Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); + Settings settings(m_ConfigFilepath, m_DatabaseFilepath); wxBusyCursor busy_cursor; wxWindowDisabler window_disabler; @@ -502,7 +505,7 @@ void Browser::OnDragFromDirCtrl(wxTreeEvent& event) void Browser::OnDragFromSampleView(wxDataViewEvent& event) { - Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); + Settings settings(m_ConfigFilepath, m_DatabaseFilepath); Database db(*m_InfoBar); int selected_row = m_SampleListView->ItemToRow(event.GetItem()); @@ -532,7 +535,7 @@ void Browser::OnClickPlay(wxCommandEvent& event) { bStopped = false; - Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); + Settings settings(m_ConfigFilepath, m_DatabaseFilepath); Database db(*m_InfoBar); int selected_row = m_SampleListView->GetSelectedRow(); @@ -653,7 +656,7 @@ void Browser::OnSlideVolume(wxScrollEvent& event) void Browser::OnClickSampleView(wxDataViewEvent& event) { - Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); + Settings settings(m_ConfigFilepath, m_DatabaseFilepath); Database db(*m_InfoBar); int selected_row = m_SampleListView->ItemToRow(event.GetItem()); @@ -684,7 +687,7 @@ void Browser::OnClickSampleView(wxDataViewEvent& event) void Browser::OnShowSampleListViewContextMenu(wxDataViewEvent& event) { TagEditor* tagEditor; - Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); + Settings settings(m_ConfigFilepath, m_DatabaseFilepath); Database db(*m_InfoBar); wxString msg; @@ -906,7 +909,7 @@ void Browser::OnShowSampleListViewContextMenu(wxDataViewEvent& event) void Browser::LoadDatabase() { - Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); + Settings settings(m_ConfigFilepath, m_DatabaseFilepath); Database db(*m_InfoBar); try @@ -1103,7 +1106,7 @@ void Browser::OnCancelSearch(wxCommandEvent& event) void Browser::LoadConfigFile() { - Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); + Settings settings(m_ConfigFilepath, m_DatabaseFilepath); Serializer serialize(m_ConfigFilepath); wxString font_face = serialize.DeserializeDisplaySettings().font_face; diff --git a/src/Database.cpp b/src/Database.cpp index bb30a43..ade08a4 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -9,6 +9,16 @@ Database::Database(wxInfoBar& infoBar) : m_InfoBar(infoBar) +{ + +} + +Database::~Database() +{ + +} + +void Database::CreateDatabase() { /* Create SQL statement */ std::string sample = "CREATE TABLE IF NOT EXISTS SAMPLES(" @@ -26,7 +36,16 @@ Database::Database(wxInfoBar& infoBar) try { - rc = sqlite3_open("sample.hive", &m_Database); + if (sqlite3_open("sample.hive", &m_Database) != SQLITE_OK && IsOK) + { + wxLogDebug("Error opening DB"); + throw sqlite3_errmsg(m_Database); + } + else + { + wxLogDebug("Opening DB.."); + } + rc = sqlite3_exec(m_Database, sample.c_str(), NULL, 0, &m_ErrMsg); if (rc != SQLITE_OK) @@ -41,7 +60,12 @@ Database::Database(wxInfoBar& infoBar) wxLogDebug("Table created successfully."); } - sqlite3_close(m_Database); + rc = sqlite3_close(m_Database); + + if (rc == SQLITE_OK) + wxLogDebug("DB Closed.."); + else + wxLogDebug("Error! Cannot close DB, Error code: %d, Error message: %s", rc, m_ErrMsg); } catch (const std::exception &exception) { @@ -49,11 +73,6 @@ Database::Database(wxInfoBar& infoBar) } } -Database::~Database() -{ - -} - void Database::InsertSample(int favorite, std::string filename, std::string fileExtension, std::string samplePack, std::string type, int channels, int length, @@ -62,7 +81,22 @@ void Database::InsertSample(int favorite, std::string filename, { try { - rc = sqlite3_open("sample.hive", &m_Database); + // while (!IsOK) + // { + // wxLogDebug("Waiting On Previous Transaction"); + // } + m.lock(); + + // rc = sqlite3_open("sample.hive", &m_Database); + if (sqlite3_open("sample.hive", &m_Database) != SQLITE_OK) + { + wxLogDebug("Error opening DB"); + throw sqlite3_errmsg(m_Database); + } + else + { + wxLogDebug("Opening DB.."); + } std::string insert = "INSERT INTO SAMPLES (FAVORITE, FILENAME, \ EXTENSION, SAMPLEPACK, TYPE, CHANNELS, LENGTH, \ @@ -70,6 +104,8 @@ void Database::InsertSample(int favorite, std::string filename, VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; rc = sqlite3_prepare_v2(m_Database, insert.c_str(), insert.size(), &m_Stmt, NULL); + if (rc != SQLITE_OK) + wxLogDebug("Cannot prepare sql statement.."); rc = sqlite3_bind_int(m_Stmt, 1, favorite); rc = sqlite3_bind_text(m_Stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); @@ -85,29 +121,54 @@ void Database::InsertSample(int favorite, std::string filename, if (sqlite3_step(m_Stmt) != SQLITE_DONE) { - wxLogWarning("No data inserted."); + wxLogDebug("No data inserted. Error code: %d", sqlite3_step(m_Stmt)); } rc = sqlite3_finalize(m_Stmt); if (rc != SQLITE_OK) { - wxMessageDialog msgDialog(NULL, - "Error! Cannot insert data into table.", - "Error", wxOK | wxICON_ERROR); - msgDialog.ShowModal(); + // wxMessageDialog msgDialog(NULL, + // "Error! Cannot insert data into table.", + // "Error", wxOK | wxICON_ERROR); + // msgDialog.ShowModal(); + wxLogDebug("Error! Cannot insert data into table. Error code: %d, Error Message: %s", rc, m_ErrMsg); sqlite3_free(m_ErrMsg); } else { - wxLogInfo("Data inserted successfully. %s", m_ErrMsg); + wxLogDebug("Data inserted successfully. %s", m_ErrMsg); } + if (rc == SQLITE_BUSY) + wxLogDebug("SQLITE_BUSY"); + if (rc == SQLITE_ABORT) + wxLogDebug("SQLITE_ABORT"); + if (rc == SQLITE_NOMEM) + wxLogDebug("SQLITE_NOMEM"); + if (rc == SQLITE_LOCKED) + wxLogDebug("SQLITE_LOCKED"); + if (rc == SQLITE_IOERR) + wxLogDebug("SQLITE_IOERR"); + if (rc == SQLITE_CORRUPT) + wxLogDebug("SQLITE_CORRUPT"); + if (rc == SQLITE_READONLY) + wxLogDebug("SQLITE_READONLY"); + if (rc == SQLITE_ERROR) + wxLogDebug("SQLITE_ERROR"); + if (rc == SQLITE_PERM) + wxLogDebug("SQLITE_PERM"); + if (rc == SQLITE_INTERNAL) + wxLogDebug("SQLITE_INTERNAL"); + sqlite3_close(m_Database); + // IsOK = true; + m.unlock(); } catch (const std::exception &exception) { wxLogDebug(exception.what()); + m.unlock(); } } diff --git a/src/Database.hpp b/src/Database.hpp index 711ee4f..4c981dd 100644 --- a/src/Database.hpp +++ b/src/Database.hpp @@ -1,3 +1,4 @@ +#include #include #include @@ -21,11 +22,17 @@ class Database char* m_ErrMsg; sqlite3_stmt* m_Stmt; + std::mutex m; + private: // ------------------------------------------------------------------- wxInfoBar& m_InfoBar; public: + // ------------------------------------------------------------------- + // Create the table + void CreateDatabase(); + // ------------------------------------------------------------------- // Insert into database void InsertSample(int favorite, std::string filename, diff --git a/src/SettingsDialog.cpp b/src/SettingsDialog.cpp index 170a5b2..aaeceb5 100644 --- a/src/SettingsDialog.cpp +++ b/src/SettingsDialog.cpp @@ -8,6 +8,12 @@ #include "SettingsDialog.hpp" #include "Serialize.hpp" +Settings::Settings(const std::string& configFilepath, const std::string& databaseFilepath) + : m_ConfigFilepath(configFilepath), m_DatabaseFilepath(databaseFilepath) +{ + +} + Settings::Settings(wxWindow* window, const std::string& configFilepath, const std::string& databaseFilepath) : wxDialog(window, wxID_ANY, "Settings", wxDefaultPosition, wxSize(720, 270), wxDEFAULT_DIALOG_STYLE | wxSTAY_ON_TOP), diff --git a/src/SettingsDialog.hpp b/src/SettingsDialog.hpp index 688250b..7c6e6b5 100644 --- a/src/SettingsDialog.hpp +++ b/src/SettingsDialog.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include #include #include @@ -20,8 +22,8 @@ class Settings : public wxDialog { public: + Settings(const std::string& configFilepath, const std::string& databaseFilepath); Settings(wxWindow* window, const std::string& configFilepath, const std::string& databaseFilepath); - Settings(); ~Settings();