Cleanup Database interface

This commit is contained in:
Mathias Buhr 2021-07-20 21:39:15 +02:00
parent 63173df9ad
commit e00d753302
5 changed files with 125 additions and 235 deletions

View File

@ -53,17 +53,6 @@ public:
sqlite3_stmt *stmt = nullptr; sqlite3_stmt *stmt = nullptr;
}; };
std::unique_ptr<IDatabase> createSqlDatabase(DatabaseType type, wxInfoBar &infoBar, const std::string &dbPath)
{
switch (type)
{
case DatabaseType::SqlLite:
return std::make_unique<Database>(infoBar, dbPath);
default:
return std::unique_ptr<Database>();
}
}
Database::Database(wxInfoBar &infoBar, const std::string &dbPath) Database::Database(wxInfoBar &infoBar, const std::string &dbPath)
: m_InfoBar(infoBar) : m_InfoBar(infoBar)
{ {
@ -142,7 +131,7 @@ void Database::CreateTableHives()
} }
//Loops through a Sample array and adds them to the database //Loops through a Sample array and adds them to the database
void Database::InsertIntoSamples(std::vector<Sample> samples) void Database::InsertIntoSamples(const std::vector<Sample> &samples)
{ {
try try
{ {
@ -780,13 +769,13 @@ std::string Database::GetSampleFileExtension(const std::string &filename)
return extension; return extension;
} }
wxVector<wxVector<wxVariant>> wxVector<wxVector<wxVariant>> Database::LoadSamplesDatabase(
Database::LoadSamplesDatabase(wxVector<wxVector<wxVariant>> &vecSet,
// wxTreeCtrl& favorite_tree, wxTreeItemId& favorite_item, // wxTreeCtrl& favorite_tree, wxTreeItemId& favorite_item,
wxDataViewTreeCtrl &favorite_tree, wxDataViewItem &favorite_item, wxDataViewTreeCtrl &favorite_tree, wxDataViewItem &favorite_item,
wxTreeCtrl &trash_tree, wxTreeItemId &trash_item, bool show_extension, wxTreeCtrl &trash_tree, wxTreeItemId &trash_item, bool show_extension,
const std::string &icon_star_filled, const std::string &icon_star_empty) const std::string &icon_star_filled, const std::string &icon_star_empty)
{ {
wxVector<wxVector<wxVariant>> vecSet;
wxVariant icon_filled, icon_empty; wxVariant icon_filled, icon_empty;
icon_filled = wxVariant(wxBitmap(icon_star_filled)); icon_filled = wxVariant(wxBitmap(icon_star_filled));
icon_empty = wxVariant(wxBitmap(icon_star_empty)); icon_empty = wxVariant(wxBitmap(icon_star_empty));
@ -932,10 +921,10 @@ Database::LoadSamplesDatabase(wxVector<wxVector<wxVariant>> &vecSet,
} }
wxVector<wxVector<wxVariant>> wxVector<wxVector<wxVariant>>
Database::FilterDatabaseBySampleName(wxVector<wxVector<wxVariant>> &sampleVec, Database::FilterDatabaseBySampleName(const std::string &sampleName, bool show_extension,
const std::string &sampleName, bool show_extension,
const std::string &icon_star_filled, const std::string &icon_star_empty) const std::string &icon_star_filled, const std::string &icon_star_empty)
{ {
wxVector<wxVector<wxVariant>> sampleVec;
wxVariant icon_filled, icon_empty; wxVariant icon_filled, icon_empty;
icon_filled = wxVariant(wxBitmap(icon_star_filled)); icon_filled = wxVariant(wxBitmap(icon_star_filled));
icon_empty = wxVariant(wxBitmap(icon_star_empty)); icon_empty = wxVariant(wxBitmap(icon_star_empty));
@ -1026,10 +1015,10 @@ Database::FilterDatabaseBySampleName(wxVector<wxVector<wxVariant>> &sampleVec,
} }
wxVector<wxVector<wxVariant>> wxVector<wxVector<wxVariant>>
Database::FilterDatabaseByHiveName(wxVector<wxVector<wxVariant>> &sampleVec, Database::FilterDatabaseByHiveName(const std::string &hiveName, bool show_extension,
const std::string &hiveName, bool show_extension,
const std::string &icon_star_filled, const std::string &icon_star_empty) const std::string &icon_star_filled, const std::string &icon_star_empty)
{ {
wxVector<wxVector<wxVariant>> sampleVec;
wxVariant icon_filled, icon_empty; wxVariant icon_filled, icon_empty;
icon_filled = wxVariant(wxBitmap(icon_star_filled)); icon_filled = wxVariant(wxBitmap(icon_star_filled));
icon_empty = wxVariant(wxBitmap(icon_star_empty)); icon_empty = wxVariant(wxBitmap(icon_star_empty));
@ -1382,3 +1371,4 @@ void Database::close()
{ {
throw_on_sqlite3_error(sqlite3_close(m_Database)); throw_on_sqlite3_error(sqlite3_close(m_Database));
} }

View File

@ -17,6 +17,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#pragma once
#include "Sample.hpp" #include "Sample.hpp"
@ -33,9 +34,7 @@
#include <sqlite3.h> #include <sqlite3.h>
#include "IDatabase.hpp" class Database
class Database : public IDatabase
{ {
public: public:
Database(wxInfoBar& infoBar, const std::string& dbPath); Database(wxInfoBar& infoBar, const std::string& dbPath);
@ -56,61 +55,58 @@ class Database : public IDatabase
public: public:
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Create the table // Create the table
void CreateTableSamples() override; void CreateTableSamples();
void CreateTableHives() override; void CreateTableHives();
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Insert into database // Insert into database
void InsertIntoSamples(std::vector<Sample>) override; void InsertIntoSamples(const std::vector<Sample>&);
void InsertIntoHives(const std::string& hiveName) override; void InsertIntoHives(const std::string& hiveName);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Update database // Update database
void UpdateFavoriteColumn(const std::string& filename, int value) override; void UpdateFavoriteColumn(const std::string& filename, int value);
void UpdateHive(const std::string& hiveOldName, const std::string& hiveNewName) override; void UpdateHive(const std::string& hiveOldName, const std::string& hiveNewName);
void UpdateHiveName(const std::string& filename, const std::string& hiveName) override; void UpdateHiveName(const std::string& filename, const std::string& hiveName);
void UpdateTrashColumn(const std::string& filename, int value) override; void UpdateTrashColumn(const std::string& filename, int value);
void UpdateSamplePack(const std::string& filename, const std::string& samplePack) override; void UpdateSamplePack(const std::string& filename, const std::string& samplePack);
void UpdateSampleType(const std::string& filename, const std::string& type) override; void UpdateSampleType(const std::string& filename, const std::string& type);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Get from database // Get from database
int GetFavoriteColumnValueByFilename(const std::string& filename) override; int GetFavoriteColumnValueByFilename(const std::string& filename);
std::string GetHiveByFilename(const std::string& filename) override; std::string GetHiveByFilename(const std::string& filename);
std::string GetSamplePathByFilename(const std::string& filename) override; std::string GetSamplePathByFilename(const std::string& filename);
std::string GetSampleFileExtension(const std::string& filename) override; std::string GetSampleFileExtension(const std::string& filename);
std::string GetSampleType(const std::string& filename) override; std::string GetSampleType(const std::string& filename);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Check database // Check database
bool IsTrashed(const std::string& filename) override; bool IsTrashed(const std::string& filename);
wxArrayString CheckDuplicates(const wxArrayString& files) override; wxArrayString CheckDuplicates(const wxArrayString& files);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Remove from database // Remove from database
void RemoveSampleFromDatabase(const std::string& filename) override; void RemoveSampleFromDatabase(const std::string& filename);
void RemoveHiveFromDatabase(const std::string& hiveName) override; void RemoveHiveFromDatabase(const std::string& hiveName);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
wxVector<wxVector<wxVariant>>
// LoadDatabase(wxVector<wxVector<wxVariant>> &vecSet, // LoadDatabase(wxVector<wxVector<wxVariant>> &vecSet,
// wxTreeCtrl& favorite_tree, wxTreeItemId& favorite_item, // wxTreeCtrl& favorite_tree, wxTreeItemId& favorite_item,
// wxTreeCtrl& trash_tree, wxTreeItemId& trash_item, bool show_extension) override; // wxTreeCtrl& trash_tree, wxTreeItemId& trash_item, bool show_extension);
LoadSamplesDatabase(wxVector<wxVector<wxVariant>>& vecSet, wxVector<wxVector<wxVariant>>
wxDataViewTreeCtrl& favorite_tree, wxDataViewItem& favorite_item, LoadSamplesDatabase(wxDataViewTreeCtrl& favorite_tree, wxDataViewItem& favorite_item,
wxTreeCtrl& trash_tree, wxTreeItemId& trash_item, bool show_extension, wxTreeCtrl& trash_tree, wxTreeItemId& trash_item, bool show_extension,
const std::string& icon_star_filled, const std::string& icon_star_emtpy) override; const std::string& icon_star_filled, const std::string& icon_star_emtpy);
void LoadHivesDatabase(wxDataViewTreeCtrl& favorite_tree) override; void LoadHivesDatabase(wxDataViewTreeCtrl& favorite_tree);
wxVector<wxVector<wxVariant>> wxVector<wxVector<wxVariant>>
RestoreFromTrashByFilename(const std::string& filename, RestoreFromTrashByFilename(const std::string& filename,
wxVector<wxVector<wxVariant>>& vecSet, bool show_extension, wxVector<wxVector<wxVariant>>& vecSet, bool show_extension,
const std::string& icon_star_filled, const std::string& icon_star_empty) override; const std::string& icon_star_filled, const std::string& icon_star_empty);
wxVector<wxVector<wxVariant>> wxVector<wxVector<wxVariant>>
FilterDatabaseBySampleName(wxVector<wxVector<wxVariant>>& sampleVec, FilterDatabaseBySampleName(const std::string& sampleName, bool show_extension,
const std::string& sampleName, bool show_extension, const std::string& icon_star_filled, const std::string& icon_star_empty);
const std::string& icon_star_filled, const std::string& icon_star_empty) override;
wxVector<wxVector<wxVariant>> wxVector<wxVector<wxVariant>>
FilterDatabaseByHiveName(wxVector<wxVector<wxVariant>>& sampleVec, FilterDatabaseByHiveName(const std::string& hiveName, bool show_extension,
const std::string& hiveName, bool show_extension, const std::string& icon_star_filled, const std::string& icon_star_empty);
const std::string& icon_star_filled, const std::string& icon_star_empty) override;
}; };

View File

@ -1,96 +0,0 @@
/* SampleHive
* Copyright (C) 2021 Apoorv Singh
* A simple, modern audio sample browser/manager for GNU/Linux.
*
* This file is a part of SampleHive
*
* SampleHive is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SampleHive is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <memory>
#include <vector>
#include "Sample.hpp"
#include <wx/vector.h>
class IDatabase
{ public:
virtual ~IDatabase() = default;
// -------------------------------------------------------------------
// Create the table
virtual void CreateTableSamples() = 0;
virtual void CreateTableHives() = 0;
// -------------------------------------------------------------------
// Insert into database
virtual void InsertIntoSamples(std::vector<Sample>) = 0;
virtual void InsertIntoHives(const std::string& hiveName) = 0;
// -------------------------------------------------------------------
// Update database
virtual void UpdateFavoriteColumn(const std::string& filename, int value) = 0;
virtual void UpdateHive(const std::string& hiveOldName, const std::string& hiveNewName) = 0;
virtual void UpdateHiveName(const std::string& filename, const std::string& hiveName) = 0;
virtual void UpdateTrashColumn(const std::string& filename, int value) = 0;
virtual void UpdateSamplePack(const std::string& filename, const std::string& samplePack) = 0;
virtual void UpdateSampleType(const std::string& filename, const std::string& type) = 0;
// -------------------------------------------------------------------
// Get from database
virtual int GetFavoriteColumnValueByFilename(const std::string& filename) = 0;
virtual std::string GetHiveByFilename(const std::string& filename) = 0;
virtual std::string GetSamplePathByFilename(const std::string& filename) = 0;
virtual std::string GetSampleFileExtension(const std::string& filename) = 0;
virtual std::string GetSampleType(const std::string& filename) = 0;
// -------------------------------------------------------------------
// Check database
virtual bool IsTrashed(const std::string& filename) = 0;
virtual wxArrayString CheckDuplicates(const wxArrayString& files) = 0;
// -------------------------------------------------------------------
// Remove from database
virtual void RemoveSampleFromDatabase(const std::string& filename) = 0;
virtual void RemoveHiveFromDatabase(const std::string& hiveName) = 0;
// -------------------------------------------------------------------
virtual wxVector<wxVector<wxVariant>>
// LoadDatabase(wxVector<wxVector<wxVariant>> &vecSet,
// wxTreeCtrl& favorite_tree, wxTreeItemId& favorite_item,
// wxTreeCtrl& trash_tree, wxTreeItemId& trash_item, bool show_extension) = 0;
LoadSamplesDatabase(wxVector<wxVector<wxVariant>>& vecSet,
wxDataViewTreeCtrl& favorite_tree, wxDataViewItem& favorite_item,
wxTreeCtrl& trash_tree, wxTreeItemId& trash_item, bool show_extension,
const std::string& icon_star_filled, const std::string& icon_star_emtpy) = 0;
virtual void LoadHivesDatabase(wxDataViewTreeCtrl& favorite_tree) = 0;
virtual wxVector<wxVector<wxVariant>>
RestoreFromTrashByFilename(const std::string& filename,
wxVector<wxVector<wxVariant>>& vecSet, bool show_extension,
const std::string& icon_star_filled, const std::string& icon_star_empty) = 0;
virtual wxVector<wxVector<wxVariant>>
FilterDatabaseBySampleName(wxVector<wxVector<wxVariant>>& sampleVec,
const std::string& sampleName, bool show_extension,
const std::string& icon_star_filled, const std::string& icon_star_empty) = 0;
virtual wxVector<wxVector<wxVariant>>
FilterDatabaseByHiveName(wxVector<wxVector<wxVariant>>& sampleVec,
const std::string& hiveName, bool show_extension,
const std::string& icon_star_filled, const std::string& icon_star_empty) = 0;
};
enum class DatabaseType {
SqlLite
};
std::unique_ptr<IDatabase> createSqlDatabase(DatabaseType type, wxInfoBar& infoBar, const std::string& dbPath);

View File

@ -544,9 +544,9 @@ MainFrame::MainFrame()
m_BottomRightPanelMainSizer->Layout(); m_BottomRightPanelMainSizer->Layout();
// Initialize the database // Initialize the database
m_pDatabase = createSqlDatabase(DatabaseType::SqlLite, *m_InfoBar, m_DatabaseFilepath); m_database = std::make_unique<Database>(*m_InfoBar, m_DatabaseFilepath);
m_pDatabase->CreateTableSamples(); m_database->CreateTableSamples();
m_pDatabase->CreateTableHives(); m_database->CreateTableHives();
// Restore the data previously added to Library // Restore the data previously added to Library
LoadDatabase(); LoadDatabase();
@ -604,7 +604,7 @@ void MainFrame::AddSamples(wxArrayString& files)
//Check All Files At Once //Check All Files At Once
wxArrayString sorted_files; wxArrayString sorted_files;
sorted_files = m_pDatabase->CheckDuplicates(files); sorted_files = m_database->CheckDuplicates(files);
files = sorted_files; files = sorted_files;
if(files.size() < 1) if(files.size() < 1)
@ -685,7 +685,7 @@ void MainFrame::AddSamples(wxArrayString& files)
progressDialog->Pulse(_("Updating Database.."), NULL); progressDialog->Pulse(_("Updating Database.."), NULL);
m_pDatabase->InsertIntoSamples(sample_array); m_database->InsertIntoSamples(sample_array);
progressDialog->Destroy(); progressDialog->Destroy();
} }
@ -785,23 +785,23 @@ void MainFrame::OnDragAndDropToHives(wxDropFilesEvent& event)
rows - i, files[i], m_Hives->GetItemText(drop_target)); rows - i, files[i], m_Hives->GetItemText(drop_target));
if (drop_target.IsOk() && m_Hives->IsContainer(drop_target) && if (drop_target.IsOk() && m_Hives->IsContainer(drop_target) &&
m_pDatabase->GetFavoriteColumnValueByFilename( file_name.ToStdString()) == 0) m_database->GetFavoriteColumnValueByFilename(file_name.ToStdString()) == 0)
{ {
m_Hives->AppendItem(drop_target, files[i]); m_Hives->AppendItem(drop_target, files[i]);
m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_FILLED_16px)), row, 0); m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_FILLED_16px)), row, 0);
m_pDatabase->UpdateFavoriteColumn(file_name.ToStdString(), 1); m_database->UpdateFavoriteColumn(file_name.ToStdString(), 1);
m_pDatabase->UpdateHiveName( file_name.ToStdString(), hive_name.ToStdString()); m_database->UpdateHiveName( file_name.ToStdString(), hive_name.ToStdString());
msg = wxString::Format(_("%s added to %s."), files[i], hive_name); msg = wxString::Format(_("%s added to %s."), files[i], hive_name);
} }
else else
{ {
if (m_pDatabase->GetFavoriteColumnValueByFilename( file_name.ToStdString()) == 1) if (m_database->GetFavoriteColumnValueByFilename(file_name.ToStdString()) == 1)
{ {
wxMessageBox(wxString::Format(_("%s is already added to %s hive"), files[i], wxMessageBox(wxString::Format(_("%s is already added to %s hive"), files[i],
m_pDatabase->GetHiveByFilename( file_name.ToStdString())), m_database->GetHiveByFilename(file_name.ToStdString())),
_("Error!"), wxOK | wxICON_ERROR | wxCENTRE, this); _("Error!"), wxOK | wxICON_ERROR | wxCENTRE, this);
} }
else else
@ -902,12 +902,12 @@ void MainFrame::OnDragFromLibrary(wxDataViewEvent& event)
wxString selection = m_Library->GetTextValue(selected_row, 1); wxString selection = m_Library->GetTextValue(selected_row, 1);
// wxString sample_with_extension = m_pDatabase->GetSamplePathByFilename(selection.BeforeLast('.').ToStdString()); // wxString sample_with_extension = m_database->GetSamplePathByFilename(selection.BeforeLast('.').ToStdString());
// wxString sample_without_extension = m_pDatabase->GetSamplePathByFilename(selection.ToStdString()); // wxString sample_without_extension = m_database->GetSamplePathByFilename(selection.ToStdString());
// std::string extension = settings.ShouldShowFileExtension() ? // std::string extension = settings.ShouldShowFileExtension() ?
// m_pDatabase->GetSampleFileExtension(selection.ToStdString()) : // m_database->GetSampleFileExtension(selection.ToStdString()) :
// m_pDatabase->GetSampleFileExtension(selection.BeforeLast('.').ToStdString()); // m_database->GetSampleFileExtension(selection.BeforeLast('.').ToStdString());
// wxString sample = selection.Contains(wxString::Format(".%s", extension)) ? // wxString sample = selection.Contains(wxString::Format(".%s", extension)) ?
// sample_with_extension : sample_without_extension; // sample_with_extension : sample_without_extension;
@ -935,12 +935,12 @@ void MainFrame::OnClickPlay(wxCommandEvent& event)
wxString selection = m_Library->GetTextValue(selected_row, 1); wxString selection = m_Library->GetTextValue(selected_row, 1);
// wxString sample_with_extension = m_pDatabase->GetSamplePathByFilename(selection.BeforeLast('.').ToStdString()); // wxString sample_with_extension = m_database->GetSamplePathByFilename(selection.BeforeLast('.').ToStdString());
// wxString sample_without_extension = m_pDatabase->GetSamplePathByFilename(selection.ToStdString()); // wxString sample_without_extension = m_database->GetSamplePathByFilename(selection.ToStdString());
// std::string extension = settings.ShouldShowFileExtension() ? // std::string extension = settings.ShouldShowFileExtension() ?
// m_pDatabase->GetSampleFileExtension(selection.ToStdString()) : // m_database->GetSampleFileExtension(selection.ToStdString()) :
// m_pDatabase->GetSampleFileExtension(selection.BeforeLast('.').ToStdString()); // m_database->GetSampleFileExtension(selection.BeforeLast('.').ToStdString());
// wxString sample = selection.Contains(wxString::Format(".%s", extension)) ? // wxString sample = selection.Contains(wxString::Format(".%s", extension)) ?
// sample_with_extension : sample_without_extension; // sample_with_extension : sample_without_extension;
@ -1129,12 +1129,12 @@ void MainFrame::OnClickLibrary(wxDataViewEvent& event)
// else // else
// selection = m_Library->GetTextValue(selected_row, 1); // selection = m_Library->GetTextValue(selected_row, 1);
// wxString sample_with_extension = m_pDatabase->GetSamplePathByFilename(selection.BeforeLast('.').ToStdString()); // wxString sample_with_extension = m_database->GetSamplePathByFilename(selection.BeforeLast('.').ToStdString());
// wxString sample_without_extension = m_pDatabase->GetSamplePathByFilename(selection.ToStdString()); // wxString sample_without_extension = m_database->GetSamplePathByFilename(selection.ToStdString());
// std::string extension = settings.ShouldShowFileExtension() ? // std::string extension = settings.ShouldShowFileExtension() ?
// m_pDatabase->GetSampleFileExtension(selection.ToStdString()) : // m_database->GetSampleFileExtension(selection.ToStdString()) :
// m_pDatabase->GetSampleFileExtension(selection.BeforeLast('.').ToStdString()); // m_database->GetSampleFileExtension(selection.BeforeLast('.').ToStdString());
// wxString sample = selection.Contains(wxString::Format(".%s", extension)) ? // wxString sample = selection.Contains(wxString::Format(".%s", extension)) ?
// sample_with_extension : sample_without_extension; // sample_with_extension : sample_without_extension;
@ -1177,12 +1177,12 @@ void MainFrame::OnClickLibrary(wxDataViewEvent& event)
wxDataViewItem container; wxDataViewItem container;
wxDataViewItem child; wxDataViewItem child;
if (m_pDatabase->GetFavoriteColumnValueByFilename(filename) == 0) if (m_database->GetFavoriteColumnValueByFilename(filename) == 0)
{ {
m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_FILLED_16px)), selected_row, 0); m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_FILLED_16px)), selected_row, 0);
m_pDatabase->UpdateFavoriteColumn(filename, 1); m_database->UpdateFavoriteColumn(filename, 1);
m_pDatabase->UpdateHiveName(filename, hive_name); m_database->UpdateHiveName(filename, hive_name);
for (int i = 0; i < m_Hives->GetChildCount(root); i++) for (int i = 0; i < m_Hives->GetChildCount(root); i++)
{ {
@ -1201,8 +1201,8 @@ void MainFrame::OnClickLibrary(wxDataViewEvent& event)
{ {
m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), selected_row, 0); m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), selected_row, 0);
m_pDatabase->UpdateFavoriteColumn(filename, 0); m_database->UpdateFavoriteColumn(filename, 0);
m_pDatabase->UpdateHiveName( filename, m_Hives->GetItemText(favorites_hive).ToStdString()); m_database->UpdateHiveName(filename, m_Hives->GetItemText(favorites_hive).ToStdString());
for (int i = 0; i < m_Hives->GetChildCount(root); i++) for (int i = 0; i < m_Hives->GetChildCount(root); i++)
{ {
@ -1329,7 +1329,7 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event)
wxLogDebug("Sample count: %d", sample_count); wxLogDebug("Sample count: %d", sample_count);
m_Hives->SetItemText(selected_hive, hive_name); m_Hives->SetItemText(selected_hive, hive_name);
m_pDatabase->UpdateHive( selected_hive_name.ToStdString(), hive_name.ToStdString()); m_database->UpdateHive(selected_hive_name.ToStdString(), hive_name.ToStdString());
} }
else else
{ {
@ -1343,8 +1343,8 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event)
wxLogDebug("Sample count: %d :: Sample name: %s", sample_count, sample_name); wxLogDebug("Sample count: %d :: Sample name: %s", sample_count, sample_name);
m_pDatabase->UpdateHiveName( sample_name.ToStdString(), hive_name.ToStdString()); m_database->UpdateHiveName(sample_name.ToStdString(), hive_name.ToStdString());
m_pDatabase->UpdateHive( selected_hive_name.ToStdString(), hive_name.ToStdString()); m_database->UpdateHive(selected_hive_name.ToStdString(), hive_name.ToStdString());
m_Hives->SetItemText(selected_hive, hive_name); m_Hives->SetItemText(selected_hive, hive_name);
} }
@ -1413,7 +1413,7 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event)
{ {
m_Hives->DeleteItem(selected_hive); m_Hives->DeleteItem(selected_hive);
m_pDatabase->RemoveHiveFromDatabase( hive_name.ToStdString()); m_database->RemoveHiveFromDatabase(hive_name.ToStdString());
msg = wxString::Format(_("%s deleted from hives successfully."), hive_name); msg = wxString::Format(_("%s deleted from hives successfully."), hive_name);
} }
@ -1454,9 +1454,8 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event)
m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), i, 0); m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), i, 0);
m_pDatabase->UpdateFavoriteColumn( matched_sample.ToStdString(), 0); m_database->UpdateFavoriteColumn(matched_sample.ToStdString(), 0);
m_pDatabase->UpdateHiveName( matched_sample.ToStdString(), m_database->UpdateHiveName(matched_sample.ToStdString(), m_Hives->GetItemText(favorites_hive).ToStdString());
m_Hives->GetItemText(favorites_hive).ToStdString());
break; break;
} }
@ -1468,7 +1467,7 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event)
m_Hives->DeleteChildren(selected_hive); m_Hives->DeleteChildren(selected_hive);
m_Hives->DeleteItem(selected_hive); m_Hives->DeleteItem(selected_hive);
m_pDatabase->RemoveHiveFromDatabase( hive_name.ToStdString()); m_database->RemoveHiveFromDatabase(hive_name.ToStdString());
msg = wxString::Format( msg = wxString::Format(
_("%s and all samples inside %s have been deleted from hives successfully."), _("%s and all samples inside %s have been deleted from hives successfully."),
@ -1492,11 +1491,11 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event)
{ {
try try
{ {
wxVector<wxVector<wxVariant>> dataset; const auto dataset = m_database->FilterDatabaseByHiveName(hive_name.ToStdString(),
if (m_pDatabase->FilterDatabaseByHiveName( dataset, hive_name.ToStdString(),
settings.ShouldShowFileExtension(), settings.ShouldShowFileExtension(),
ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty()) ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px);
if (dataset.empty())
{ {
wxMessageBox(_("Error! Database is empty."), _("Error!"), wxMessageBox(_("Error! Database is empty."), _("Error!"),
wxOK | wxICON_ERROR | wxCENTRE, this); wxOK | wxICON_ERROR | wxCENTRE, this);
@ -1526,10 +1525,10 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event)
{ {
try try
{ {
wxVector<wxVector<wxVariant>> dataset; const auto dataset = m_database->FilterDatabaseBySampleName("", settings.ShouldShowFileExtension(),
ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px);
if (m_pDatabase->FilterDatabaseBySampleName( dataset, "", settings.ShouldShowFileExtension(), if (dataset.empty())
ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty())
{ {
wxMessageBox(_("Error! Database is empty."), _("Error!"), wxMessageBox(_("Error! Database is empty."), _("Error!"),
wxOK | wxICON_ERROR | wxCENTRE, this); wxOK | wxICON_ERROR | wxCENTRE, this);
@ -1579,8 +1578,8 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event)
m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), i, 0); m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), i, 0);
m_pDatabase->UpdateFavoriteColumn(matched_sample.ToStdString(), 0); m_database->UpdateFavoriteColumn(matched_sample.ToStdString(), 0);
m_pDatabase->UpdateHiveName(matched_sample.ToStdString(), m_database->UpdateHiveName(matched_sample.ToStdString(),
m_Hives->GetItemText(favorites_hive).ToStdString()); m_Hives->GetItemText(favorites_hive).ToStdString());
m_Hives->DeleteItem(selected_hive); m_Hives->DeleteItem(selected_hive);
@ -1590,7 +1589,7 @@ void MainFrame::OnShowHivesContextMenu(wxDataViewEvent& event)
m_InfoBar->ShowMessage(wxString::Format(_("Removed %s from %s"), m_InfoBar->ShowMessage(wxString::Format(_("Removed %s from %s"),
m_Hives->GetItemText(event.GetItem()), m_Hives->GetItemText(event.GetItem()),
m_pDatabase->GetHiveByFilename(matched_sample.ToStdString())), m_database->GetHiveByFilename(matched_sample.ToStdString())),
wxICON_INFORMATION); wxICON_INFORMATION);
} }
break; break;
@ -1650,7 +1649,7 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event)
//true = add false = remove //true = add false = remove
bool favorite_add = false; bool favorite_add = false;
if (m_pDatabase->GetFavoriteColumnValueByFilename(filename) == 1) if (m_database->GetFavoriteColumnValueByFilename(filename) == 1)
menu.Append(MN_FavoriteSample, _("Remove from hive"), _("Remove the selected sample(s) from hive")); menu.Append(MN_FavoriteSample, _("Remove from hive"), _("Remove the selected sample(s) from hive"));
else else
{ {
@ -1704,7 +1703,7 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event)
filename = settings.ShouldShowFileExtension() ? filename = settings.ShouldShowFileExtension() ?
name.BeforeLast('.').ToStdString() : name.ToStdString(); name.BeforeLast('.').ToStdString() : name.ToStdString();
db_status = m_pDatabase->GetFavoriteColumnValueByFilename(filename); db_status = m_database->GetFavoriteColumnValueByFilename(filename);
// Aleady Added, Do Nothing // Aleady Added, Do Nothing
if (favorite_add && db_status == 1) if (favorite_add && db_status == 1)
@ -1719,8 +1718,8 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event)
{ {
m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_FILLED_16px)), selected_row, 0); m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_FILLED_16px)), selected_row, 0);
m_pDatabase->UpdateFavoriteColumn(filename, 1); m_database->UpdateFavoriteColumn(filename, 1);
m_pDatabase->UpdateHiveName(filename, hive_name); m_database->UpdateHiveName(filename, hive_name);
for (int i = 0; i < m_Hives->GetChildCount(root); i++) for (int i = 0; i < m_Hives->GetChildCount(root); i++)
{ {
@ -1740,8 +1739,8 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event)
//Remove From Favorites //Remove From Favorites
m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), selected_row, 0); m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), selected_row, 0);
m_pDatabase->UpdateFavoriteColumn(filename, 0); m_database->UpdateFavoriteColumn(filename, 0);
m_pDatabase->UpdateHiveName(filename, m_database->UpdateHiveName(filename,
m_Hives->GetItemText(favorites_hive).ToStdString()); m_Hives->GetItemText(favorites_hive).ToStdString());
for (int i = 0; i < m_Hives->GetChildCount(root); i++) for (int i = 0; i < m_Hives->GetChildCount(root); i++)
@ -1804,7 +1803,7 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event)
{ {
wxLogDebug("Selected row: %d :: Sample: %s", selected_row, filename); wxLogDebug("Selected row: %d :: Sample: %s", selected_row, filename);
m_pDatabase->RemoveSampleFromDatabase(filename); m_database->RemoveSampleFromDatabase(filename);
m_Library->DeleteItem(selected_row); m_Library->DeleteItem(selected_row);
for (int j = 0; j < m_Hives->GetChildCount(root); j++) for (int j = 0; j < m_Hives->GetChildCount(root); j++)
@ -1854,7 +1853,7 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event)
std::string multi_selection = settings.ShouldShowFileExtension() ? std::string multi_selection = settings.ShouldShowFileExtension() ?
text_value.BeforeLast('.').ToStdString() : text_value.ToStdString() ; text_value.BeforeLast('.').ToStdString() : text_value.ToStdString() ;
m_pDatabase->RemoveSampleFromDatabase(multi_selection); m_database->RemoveSampleFromDatabase(multi_selection);
m_Library->DeleteItem(row); m_Library->DeleteItem(row);
for (int j = 0; j < m_Hives->GetChildCount(root); j++) for (int j = 0; j < m_Hives->GetChildCount(root); j++)
@ -1896,7 +1895,7 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event)
wxDataViewItem root = wxDataViewItem(wxNullPtr); wxDataViewItem root = wxDataViewItem(wxNullPtr);
wxDataViewItem container, child; wxDataViewItem container, child;
if (m_pDatabase->IsTrashed(filename)) if (m_database->IsTrashed(filename))
wxLogDebug(_("Already trashed..")); wxLogDebug(_("Already trashed.."));
else else
{ {
@ -1921,11 +1920,11 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event)
files = file_data.GetFilenames(); files = file_data.GetFilenames();
if (m_pDatabase->GetFavoriteColumnValueByFilename(files[i].ToStdString())) if (m_database->GetFavoriteColumnValueByFilename(files[i].ToStdString()))
{ {
m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), item_row, 0); m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), item_row, 0);
m_pDatabase->UpdateFavoriteColumn(files[i].ToStdString(), 0); m_database->UpdateFavoriteColumn(files[i].ToStdString(), 0);
for (int j = 0; j < m_Hives->GetChildCount(root); j++) for (int j = 0; j < m_Hives->GetChildCount(root); j++)
{ {
@ -1948,8 +1947,8 @@ void MainFrame::OnShowLibraryContextMenu(wxDataViewEvent& event)
} }
} }
m_pDatabase->UpdateTrashColumn(files[i].ToStdString(), 1); m_database->UpdateTrashColumn(files[i].ToStdString(), 1);
m_pDatabase->UpdateHiveName(files[i].ToStdString(), m_database->UpdateHiveName(files[i].ToStdString(),
m_Hives->GetItemText(favorites_hive).ToStdString()); m_Hives->GetItemText(favorites_hive).ToStdString());
m_Trash->AppendItem(trash_root, text_value); m_Trash->AppendItem(trash_root, text_value);
@ -2058,13 +2057,13 @@ void MainFrame::LoadDatabase()
Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath); Settings settings(this, m_ConfigFilepath, m_DatabaseFilepath);
try try
{ {
m_pDatabase->LoadHivesDatabase(*m_Hives); m_database->LoadHivesDatabase(*m_Hives);
wxVector<wxVector<wxVariant>> dataset; const auto dataset = m_database->LoadSamplesDatabase(*m_Hives, favorites_hive,
if (m_pDatabase->LoadSamplesDatabase(dataset, *m_Hives, favorites_hive,
*m_Trash, trash_root, settings.ShouldShowFileExtension(), *m_Trash, trash_root, settings.ShouldShowFileExtension(),
ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty()) ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px);
if (dataset.empty())
{ {
wxLogInfo(_("Error! Database is empty.")); wxLogInfo(_("Error! Database is empty."));
} }
@ -2104,7 +2103,7 @@ void MainFrame::OnShowTrashContextMenu(wxTreeEvent& event)
m_Trash->GetItemText(selected_trashed_item).BeforeLast('.') : m_Trash->GetItemText(selected_trashed_item).BeforeLast('.') :
m_Trash->GetItemText(selected_trashed_item); m_Trash->GetItemText(selected_trashed_item);
m_pDatabase->RemoveSampleFromDatabase(trashed_item_name.ToStdString()); m_database->RemoveSampleFromDatabase(trashed_item_name.ToStdString());
m_Trash->Delete(selected_trashed_item); m_Trash->Delete(selected_trashed_item);
} }
@ -2135,13 +2134,13 @@ void MainFrame::OnShowTrashContextMenu(wxTreeEvent& event)
files = file_data.GetFilenames(); files = file_data.GetFilenames();
m_pDatabase->UpdateTrashColumn(files[i].ToStdString(), 0); m_database->UpdateTrashColumn(files[i].ToStdString(), 0);
try try
{ {
wxVector<wxVector<wxVariant>> dataset; wxVector<wxVector<wxVariant>> dataset;
if (m_pDatabase->RestoreFromTrashByFilename(files[i].ToStdString(), if (m_database->RestoreFromTrashByFilename(files[i].ToStdString(),
dataset, settings.ShouldShowFileExtension(), dataset, settings.ShouldShowFileExtension(),
ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty()) ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty())
{ {
@ -2201,11 +2200,11 @@ void MainFrame::OnDragAndDropToTrash(wxDropFilesEvent& event)
files = file_data.GetFilenames(); files = file_data.GetFilenames();
if (m_pDatabase->GetFavoriteColumnValueByFilename(files[i].ToStdString())) if (m_database->GetFavoriteColumnValueByFilename(files[i].ToStdString()))
{ {
m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), item_row, 0); m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), item_row, 0);
m_pDatabase->UpdateFavoriteColumn(files[i].ToStdString(), 0); m_database->UpdateFavoriteColumn(files[i].ToStdString(), 0);
for (int j = 0; j < m_Hives->GetChildCount(root); j++) for (int j = 0; j < m_Hives->GetChildCount(root); j++)
{ {
@ -2228,8 +2227,8 @@ void MainFrame::OnDragAndDropToTrash(wxDropFilesEvent& event)
} }
} }
m_pDatabase->UpdateTrashColumn(files[i].ToStdString(), 1); m_database->UpdateTrashColumn(files[i].ToStdString(), 1);
m_pDatabase->UpdateHiveName(files[i].ToStdString(), m_database->UpdateHiveName(files[i].ToStdString(),
m_Hives->GetItemText(favorites_hive).ToStdString()); m_Hives->GetItemText(favorites_hive).ToStdString());
m_Trash->AppendItem(trash_root, text_value); m_Trash->AppendItem(trash_root, text_value);
@ -2307,7 +2306,7 @@ void MainFrame::OnClickAddHive(wxCommandEvent& event)
else else
{ {
m_Hives->AppendContainer(wxDataViewItem(wxNullPtr), hive_name); m_Hives->AppendContainer(wxDataViewItem(wxNullPtr), hive_name);
m_pDatabase->InsertIntoHives(hive_name.ToStdString()); m_database->InsertIntoHives(hive_name.ToStdString());
msg = wxString::Format(_("%s added to Hives."), hive_name); msg = wxString::Format(_("%s added to Hives."), hive_name);
} }
@ -2375,7 +2374,7 @@ void MainFrame::OnClickRemoveHive(wxCommandEvent& event)
{ {
m_Hives->DeleteItem(selected_item); m_Hives->DeleteItem(selected_item);
m_pDatabase->RemoveHiveFromDatabase(hive_name.ToStdString()); m_database->RemoveHiveFromDatabase(hive_name.ToStdString());
msg = wxString::Format(_("%s deleted from hives successfully."), hive_name); msg = wxString::Format(_("%s deleted from hives successfully."), hive_name);
} }
break; break;
@ -2415,8 +2414,8 @@ void MainFrame::OnClickRemoveHive(wxCommandEvent& event)
m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), i, 0); m_Library->SetValue(wxVariant(wxBitmap(ICON_STAR_EMPTY_16px)), i, 0);
m_pDatabase->UpdateFavoriteColumn(matched_sample.ToStdString(), 0); m_database->UpdateFavoriteColumn(matched_sample.ToStdString(), 0);
m_pDatabase->UpdateHiveName(matched_sample.ToStdString(), m_database->UpdateHiveName(matched_sample.ToStdString(),
m_Hives->GetItemText(favorites_hive).ToStdString()); m_Hives->GetItemText(favorites_hive).ToStdString());
break; break;
@ -2429,7 +2428,7 @@ void MainFrame::OnClickRemoveHive(wxCommandEvent& event)
m_Hives->DeleteChildren(selected_item); m_Hives->DeleteChildren(selected_item);
m_Hives->DeleteItem(selected_item); m_Hives->DeleteItem(selected_item);
m_pDatabase->RemoveHiveFromDatabase(hive_name.ToStdString()); m_database->RemoveHiveFromDatabase(hive_name.ToStdString());
msg = wxString::Format(_("%s and all samples inside %s have been deleted from hives successfully."), msg = wxString::Format(_("%s and all samples inside %s have been deleted from hives successfully."),
hive_name, hive_name); hive_name, hive_name);
@ -2484,13 +2483,13 @@ void MainFrame::OnClickRestoreTrashItem(wxCommandEvent& event)
files = file_data.GetFilenames(); files = file_data.GetFilenames();
m_pDatabase->UpdateTrashColumn(files[i].ToStdString(), 0); m_database->UpdateTrashColumn(files[i].ToStdString(), 0);
try try
{ {
wxVector<wxVector<wxVariant>> dataset; wxVector<wxVector<wxVariant>> dataset;
if (m_pDatabase->RestoreFromTrashByFilename(files[i].ToStdString(), dataset, if (m_database->RestoreFromTrashByFilename(files[i].ToStdString(), dataset,
settings.ShouldShowFileExtension(), settings.ShouldShowFileExtension(),
ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty()) ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty())
{ {
@ -2521,10 +2520,10 @@ void MainFrame::OnDoSearch(wxCommandEvent& event)
try try
{ {
wxVector<wxVector<wxVariant>> dataset; const auto dataset = m_database->FilterDatabaseBySampleName(search, settings.ShouldShowFileExtension(),
ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px);
if (m_pDatabase->FilterDatabaseBySampleName(dataset, search, settings.ShouldShowFileExtension(), if (dataset.empty())
ICON_STAR_FILLED_16px, ICON_STAR_EMPTY_16px).empty())
{ {
wxLogDebug(_("Error! Database is empty.")); wxLogDebug(_("Error! Database is empty."));
} }
@ -2652,14 +2651,14 @@ MainFrame::GetFilenamePathAndExtension(const wxString& selected, bool checkExten
wxString path; wxString path;
std::string extension, filename; std::string extension, filename;
wxString filename_with_extension = m_pDatabase->GetSamplePathByFilename(selected.BeforeLast('.').ToStdString()); wxString filename_with_extension = m_database->GetSamplePathByFilename(selected.BeforeLast('.').ToStdString());
wxString filename_without_extension = m_pDatabase->GetSamplePathByFilename(selected.ToStdString()); wxString filename_without_extension = m_database->GetSamplePathByFilename(selected.ToStdString());
if (checkExtension) if (checkExtension)
{ {
extension = settings.ShouldShowFileExtension() ? extension = settings.ShouldShowFileExtension() ?
m_pDatabase->GetSampleFileExtension(selected.ToStdString()) : m_database->GetSampleFileExtension(selected.ToStdString()) :
m_pDatabase->GetSampleFileExtension(selected.BeforeLast('.').ToStdString()); m_database->GetSampleFileExtension(selected.BeforeLast('.').ToStdString());
} }
path = selected.Contains(wxString::Format(".%s", extension)) ? path = selected.Contains(wxString::Format(".%s", extension)) ?

View File

@ -24,6 +24,7 @@
#include "SampleHiveConfig.hpp" #include "SampleHiveConfig.hpp"
#include "SH_Event.hpp" #include "SH_Event.hpp"
#include <memory>
#include <string> #include <string>
#include <wx/button.h> #include <wx/button.h>
@ -65,7 +66,7 @@
#include <taglib/tstring.h> #include <taglib/tstring.h>
#endif #endif
#include "IDatabase.hpp" #include "Database.hpp"
struct FileInfo struct FileInfo
{ {
@ -182,7 +183,7 @@ class MainFrame : public wxFrame
// ------------------------------------------------------------------- // -------------------------------------------------------------------
wxSystemAppearance m_Theme = wxSystemSettings::GetAppearance(); wxSystemAppearance m_Theme = wxSystemSettings::GetAppearance();
std::unique_ptr<IDatabase> m_pDatabase; std::unique_ptr<Database> m_database;
private: private:
// ------------------------------------------------------------------- // -------------------------------------------------------------------
bool bAutoplay = false; bool bAutoplay = false;