Removing unnecessary instantiation to SettingsDialog.

This commit is contained in:
apoorv569 2021-04-25 03:44:11 +05:30
parent e38fef36de
commit e958eb995e
5 changed files with 106 additions and 27 deletions

View File

@ -29,7 +29,6 @@
#include "Tags.hpp" #include "Tags.hpp"
// #include "TreeItemDialog.hpp" // #include "TreeItemDialog.hpp"
#include "Serialize.hpp" #include "Serialize.hpp"
#include "wx/dataview.h"
#include <wx/fswatcher.h> #include <wx/fswatcher.h>
@ -172,9 +171,6 @@ Browser::Browser(wxWindow* window)
// Enable dragging a file from SampleListView // Enable dragging a file from SampleListView
m_SampleListView->EnableDragSource(wxDF_FILENAME); m_SampleListView->EnableDragSource(wxDF_FILENAME);
// Restore the data previously added to SampleListView
LoadDatabase();
// Initialize wxInfoBar for showing information inside application // Initialize wxInfoBar for showing information inside application
m_InfoBar = new wxInfoBar(m_BottomRightPanel); m_InfoBar = new wxInfoBar(m_BottomRightPanel);
@ -306,6 +302,13 @@ Browser::Browser(wxWindow* window)
m_BottomRightPanelMainSizer->Fit(m_BottomRightPanel); m_BottomRightPanelMainSizer->Fit(m_BottomRightPanel);
m_BottomRightPanelMainSizer->SetSizeHints(m_BottomRightPanel); m_BottomRightPanelMainSizer->SetSizeHints(m_BottomRightPanel);
m_BottomRightPanelMainSizer->Layout(); 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) void Browser::OnClickSettings(wxCommandEvent& event)
@ -335,7 +338,7 @@ wxString TagLibTowx(const TagLib::String& in)
void Browser::AddSamples(wxString file) void Browser::AddSamples(wxString file)
{ {
Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); Settings settings(m_ConfigFilepath, m_DatabaseFilepath);
Database db(*m_InfoBar); Database db(*m_InfoBar);
std::string path = file.ToStdString(); std::string path = file.ToStdString();
@ -442,7 +445,7 @@ void Browser::OnDragAndDropToSampleListView(wxDropFilesEvent& event)
void Browser::OnAutoImportDir() void Browser::OnAutoImportDir()
{ {
Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); Settings settings(m_ConfigFilepath, m_DatabaseFilepath);
wxBusyCursor busy_cursor; wxBusyCursor busy_cursor;
wxWindowDisabler window_disabler; wxWindowDisabler window_disabler;
@ -502,7 +505,7 @@ void Browser::OnDragFromDirCtrl(wxTreeEvent& event)
void Browser::OnDragFromSampleView(wxDataViewEvent& event) void Browser::OnDragFromSampleView(wxDataViewEvent& event)
{ {
Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); Settings settings(m_ConfigFilepath, m_DatabaseFilepath);
Database db(*m_InfoBar); Database db(*m_InfoBar);
int selected_row = m_SampleListView->ItemToRow(event.GetItem()); int selected_row = m_SampleListView->ItemToRow(event.GetItem());
@ -532,7 +535,7 @@ void Browser::OnClickPlay(wxCommandEvent& event)
{ {
bStopped = false; bStopped = false;
Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); Settings settings(m_ConfigFilepath, m_DatabaseFilepath);
Database db(*m_InfoBar); Database db(*m_InfoBar);
int selected_row = m_SampleListView->GetSelectedRow(); int selected_row = m_SampleListView->GetSelectedRow();
@ -653,7 +656,7 @@ void Browser::OnSlideVolume(wxScrollEvent& event)
void Browser::OnClickSampleView(wxDataViewEvent& event) void Browser::OnClickSampleView(wxDataViewEvent& event)
{ {
Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); Settings settings(m_ConfigFilepath, m_DatabaseFilepath);
Database db(*m_InfoBar); Database db(*m_InfoBar);
int selected_row = m_SampleListView->ItemToRow(event.GetItem()); int selected_row = m_SampleListView->ItemToRow(event.GetItem());
@ -684,7 +687,7 @@ void Browser::OnClickSampleView(wxDataViewEvent& event)
void Browser::OnShowSampleListViewContextMenu(wxDataViewEvent& event) void Browser::OnShowSampleListViewContextMenu(wxDataViewEvent& event)
{ {
TagEditor* tagEditor; TagEditor* tagEditor;
Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); Settings settings(m_ConfigFilepath, m_DatabaseFilepath);
Database db(*m_InfoBar); Database db(*m_InfoBar);
wxString msg; wxString msg;
@ -906,7 +909,7 @@ void Browser::OnShowSampleListViewContextMenu(wxDataViewEvent& event)
void Browser::LoadDatabase() void Browser::LoadDatabase()
{ {
Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); Settings settings(m_ConfigFilepath, m_DatabaseFilepath);
Database db(*m_InfoBar); Database db(*m_InfoBar);
try try
@ -1103,7 +1106,7 @@ void Browser::OnCancelSearch(wxCommandEvent& event)
void Browser::LoadConfigFile() void Browser::LoadConfigFile()
{ {
Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); Settings settings(m_ConfigFilepath, m_DatabaseFilepath);
Serializer serialize(m_ConfigFilepath); Serializer serialize(m_ConfigFilepath);
wxString font_face = serialize.DeserializeDisplaySettings().font_face; wxString font_face = serialize.DeserializeDisplaySettings().font_face;

View File

@ -9,6 +9,16 @@
Database::Database(wxInfoBar& infoBar) Database::Database(wxInfoBar& infoBar)
: m_InfoBar(infoBar) : m_InfoBar(infoBar)
{
}
Database::~Database()
{
}
void Database::CreateDatabase()
{ {
/* Create SQL statement */ /* Create SQL statement */
std::string sample = "CREATE TABLE IF NOT EXISTS SAMPLES(" std::string sample = "CREATE TABLE IF NOT EXISTS SAMPLES("
@ -26,7 +36,16 @@ Database::Database(wxInfoBar& infoBar)
try 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); rc = sqlite3_exec(m_Database, sample.c_str(), NULL, 0, &m_ErrMsg);
if (rc != SQLITE_OK) if (rc != SQLITE_OK)
@ -41,7 +60,12 @@ Database::Database(wxInfoBar& infoBar)
wxLogDebug("Table created successfully."); 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) catch (const std::exception &exception)
{ {
@ -49,11 +73,6 @@ Database::Database(wxInfoBar& infoBar)
} }
} }
Database::~Database()
{
}
void Database::InsertSample(int favorite, std::string filename, void Database::InsertSample(int favorite, std::string filename,
std::string fileExtension, std::string samplePack, std::string fileExtension, std::string samplePack,
std::string type, int channels, int length, std::string type, int channels, int length,
@ -62,7 +81,22 @@ void Database::InsertSample(int favorite, std::string filename,
{ {
try 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, \ std::string insert = "INSERT INTO SAMPLES (FAVORITE, FILENAME, \
EXTENSION, SAMPLEPACK, TYPE, CHANNELS, LENGTH, \ EXTENSION, SAMPLEPACK, TYPE, CHANNELS, LENGTH, \
@ -70,6 +104,8 @@ void Database::InsertSample(int favorite, std::string filename,
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
rc = sqlite3_prepare_v2(m_Database, insert.c_str(), insert.size(), &m_Stmt, NULL); 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_int(m_Stmt, 1, favorite);
rc = sqlite3_bind_text(m_Stmt, 2, filename.c_str(), filename.size(), SQLITE_STATIC); 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) 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); rc = sqlite3_finalize(m_Stmt);
if (rc != SQLITE_OK) if (rc != SQLITE_OK)
{ {
wxMessageDialog msgDialog(NULL, // wxMessageDialog msgDialog(NULL,
"Error! Cannot insert data into table.", // "Error! Cannot insert data into table.",
"Error", wxOK | wxICON_ERROR); // "Error", wxOK | wxICON_ERROR);
msgDialog.ShowModal(); // msgDialog.ShowModal();
wxLogDebug("Error! Cannot insert data into table. Error code: %d, Error Message: %s", rc, m_ErrMsg);
sqlite3_free(m_ErrMsg); sqlite3_free(m_ErrMsg);
} }
else 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); sqlite3_close(m_Database);
// IsOK = true;
m.unlock();
} }
catch (const std::exception &exception) catch (const std::exception &exception)
{ {
wxLogDebug(exception.what()); wxLogDebug(exception.what());
m.unlock();
} }
} }

View File

@ -1,3 +1,4 @@
#include <mutex>
#include <string> #include <string>
#include <wx/infobar.h> #include <wx/infobar.h>
@ -21,11 +22,17 @@ class Database
char* m_ErrMsg; char* m_ErrMsg;
sqlite3_stmt* m_Stmt; sqlite3_stmt* m_Stmt;
std::mutex m;
private: private:
// ------------------------------------------------------------------- // -------------------------------------------------------------------
wxInfoBar& m_InfoBar; wxInfoBar& m_InfoBar;
public: public:
// -------------------------------------------------------------------
// Create the table
void CreateDatabase();
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Insert into database // Insert into database
void InsertSample(int favorite, std::string filename, void InsertSample(int favorite, std::string filename,

View File

@ -8,6 +8,12 @@
#include "SettingsDialog.hpp" #include "SettingsDialog.hpp"
#include "Serialize.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) Settings::Settings(wxWindow* window, const std::string& configFilepath, const std::string& databaseFilepath)
: wxDialog(window, wxID_ANY, "Settings", wxDefaultPosition, : wxDialog(window, wxID_ANY, "Settings", wxDefaultPosition,
wxSize(720, 270), wxDEFAULT_DIALOG_STYLE | wxSTAY_ON_TOP), wxSize(720, 270), wxDEFAULT_DIALOG_STYLE | wxSTAY_ON_TOP),

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <string>
#include <wx/button.h> #include <wx/button.h>
#include <wx/checkbox.h> #include <wx/checkbox.h>
#include <wx/choice.h> #include <wx/choice.h>
@ -20,8 +22,8 @@
class Settings : public wxDialog class Settings : public wxDialog
{ {
public: public:
Settings(const std::string& configFilepath, const std::string& databaseFilepath);
Settings(wxWindow* window, const std::string& configFilepath, const std::string& databaseFilepath); Settings(wxWindow* window, const std::string& configFilepath, const std::string& databaseFilepath);
Settings();
~Settings(); ~Settings();