From aeaa50ff8299fe9d7c6ec88cb7797e04996eb986 Mon Sep 17 00:00:00 2001 From: apoorv569 Date: Wed, 21 Jul 2021 19:42:58 +0530 Subject: [PATCH] Add ability to reset/clear app data. --- src/MainFrame.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/MainFrame.hpp | 1 + 2 files changed, 45 insertions(+) diff --git a/src/MainFrame.cpp b/src/MainFrame.cpp index 762fb0d..442dca8 100644 --- a/src/MainFrame.cpp +++ b/src/MainFrame.cpp @@ -131,6 +131,7 @@ MainFrame::MainFrame() m_ViewMenu->Append(m_ToggleStatusBar)->Check(m_StatusBar->IsShown()); // Help menu items + m_HelpMenu->Append(wxID_REFRESH, _("Reset app data"), _("Clear the application data revert to default configuration")); m_HelpMenu->Append(wxID_ABOUT, wxEmptyString, _("Show about the application")); // Append all menus to menubar @@ -371,6 +372,7 @@ MainFrame::MainFrame() Bind(wxEVT_MENU, &MainFrame::OnSelectToggleStatusBar, this, MN_ToggleStatusBar); Bind(wxEVT_MENU, &MainFrame::OnSelectExit, this, wxID_EXIT); Bind(wxEVT_MENU, &MainFrame::OnSelectPreferences, this, wxID_PREFERENCES); + Bind(wxEVT_MENU, &MainFrame::OnSelectResetAppData, this, wxID_REFRESH); Bind(wxEVT_MENU, &MainFrame::OnSelectAbout, this, wxID_ABOUT); m_StatusBar->Connect(wxEVT_SIZE, wxSizeEventHandler(MainFrame::OnResizeStatusBar), NULL, this); @@ -2762,6 +2764,48 @@ void MainFrame::OnSelectPreferences(wxCommandEvent& event) } } +void MainFrame::OnSelectResetAppData(wxCommandEvent& event) +{ + wxMessageDialog clearDataDialog(this, wxString::Format(_("Warning! This will delete configuration file \"%s\" and database file \"%s\" permanently, " + "are you sure you want to delete these files?"), m_ConfigFilepath, m_DatabaseFilepath), + _("Clear app data"), wxYES_NO | wxNO_DEFAULT | wxCENTRE, wxDefaultPosition); + + bool remove = false; + + switch (clearDataDialog.ShowModal()) + { + case wxID_YES: + remove = true; + + if (remove) + { + bool configIsDeleted = wxRemoveFile(m_ConfigFilepath); + + if (configIsDeleted) + wxLogDebug("Deleted %s", m_ConfigFilepath); + else + wxLogDebug("Could not delete %s", m_ConfigFilepath); + + bool dbIsDeleted = wxRemoveFile(m_DatabaseFilepath); + + if (dbIsDeleted) + wxLogDebug("Deleted %s", m_DatabaseFilepath); + else + wxLogDebug("Could not delete %s", m_DatabaseFilepath); + + if (configIsDeleted && dbIsDeleted) + m_InfoBar->ShowMessage(_("Successfully cleared app data"), wxICON_INFORMATION); + else + wxMessageBox(_("Error! Could not clear app data"), _("Error!"), wxOK | wxCENTRE | wxICON_ERROR, this); + } + break; + case wxID_NO: + break; + default: + break; + } +} + void MainFrame::OnSelectAbout(wxCommandEvent& event) { wxAboutDialogInfo aboutInfo; diff --git a/src/MainFrame.hpp b/src/MainFrame.hpp index 319d00d..dfa4bfa 100644 --- a/src/MainFrame.hpp +++ b/src/MainFrame.hpp @@ -228,6 +228,7 @@ class MainFrame : public wxFrame void OnSelectToggleStatusBar(wxCommandEvent& event); void OnSelectExit(wxCommandEvent& event); void OnSelectPreferences(wxCommandEvent& event); + void OnSelectResetAppData(wxCommandEvent& event); void OnSelectAbout(wxCommandEvent& event); // -------------------------------------------------------------------