Skip to content

Commit

Permalink
Prevent Overriding An Opened Account
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogozzo committed Nov 15, 2022
1 parent c6a9263 commit 1338b4b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/controllers/mainwindowcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ std::string MainWindowController::getFirstOpenAccountPath() const
return m_openAccounts[0];
}

bool MainWindowController::isAccountOpened(const std::string& path) const
{
return std::find(m_openAccounts.begin(), m_openAccounts.end(), path) != m_openAccounts.end();
}

AccountViewController MainWindowController::createAccountViewControllerForLatestAccount() const
{
return { m_openAccounts[m_openAccounts.size() - 1], m_configuration.getLocale(), m_sendToastCallback };
Expand Down
7 changes: 7 additions & 0 deletions src/controllers/mainwindowcontroller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ namespace NickvisionMoney::Controllers
* @returns The path of the first opened account
*/
std::string getFirstOpenAccountPath() const;
/**
* Gets whether or not an account is opened
*
* @param path The path to the account
* @returns True for opened, else false
*/
bool isAccountOpened(const std::string& path) const;
/**
* Creates an AccountViewController for the latest account
*
Expand Down
13 changes: 10 additions & 3 deletions src/ui/views/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,18 @@ void MainWindow::onNewAccount()
MainWindow* mainWindow{ reinterpret_cast<MainWindow*>(data) };
GFile* file{ gtk_file_chooser_get_file(GTK_FILE_CHOOSER(dialog)) };
std::string path{ g_file_get_path(file) };
if(std::filesystem::exists(path))
if(mainWindow->m_controller.isAccountOpened(path))
{
std::filesystem::remove(path);
adw_toast_overlay_add_toast(ADW_TOAST_OVERLAY(mainWindow->m_toastOverlay), adw_toast_new("Unable to override an opened account."));
}
else
{
if(std::filesystem::exists(path))
{
std::filesystem::remove(path);
}
mainWindow->m_controller.addAccount(path);
}
mainWindow->m_controller.addAccount(path);
g_object_unref(file);
}
g_object_unref(dialog);
Expand Down

0 comments on commit 1338b4b

Please sign in to comment.