Skip to content

Commit

Permalink
Shared - Implement NewAccountDialogController
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogozzo committed Mar 14, 2024
1 parent 45f4ffc commit eae9127
Show file tree
Hide file tree
Showing 33 changed files with 1,359 additions and 748 deletions.
2 changes: 1 addition & 1 deletion docs/po/denaro.pot
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2024-03-12 23:50-0400\n"
"POT-Creation-Date: 2024-03-14 00:10-0400\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down
6 changes: 6 additions & 0 deletions libdenaro/include/controllers/mainwindowcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ namespace Nickvision::Money::Shared::Controllers
* @param path The path of the account file
*/
bool isAccountPasswordProtected(const std::filesystem::path& path) const;
/**
* @brief Creates a new account.
* @brief This method will invoke the AccountAdded event if the account is successfully created.
* @param newAccountDialogController The NewAccountDialogController for the new account
*/
void newAccount(const std::shared_ptr<NewAccountDialogController>& newAccountDialogController);
/**
* @brief Opens an account.
* @brief This method will invoke the AccountAdded event if the account is successfully opened.
Expand Down
102 changes: 100 additions & 2 deletions libdenaro/include/controllers/newaccountdialogcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
#include <filesystem>
#include <string>
#include <vector>
#include "models/accountmetadata.h"
#include "models/accounttype.h"
#include "models/amountstyle.h"
#include "models/currency.h"
#include "models/currencycheckstatus.h"
#include "models/remindersthreshold.h"
#include "models/transactiontype.h"

namespace Nickvision::Money::Shared::Controllers
{
Expand All @@ -16,10 +23,101 @@ namespace Nickvision::Money::Shared::Controllers
/**
* @brief Constructs a NewAccountDialogController.
*/
NewAccountDialogController(const std::vector<std::filesystem::path>& openAccounts);
NewAccountDialogController();
/**
* @brief Gets the file path of the new account.
* @return The file path of the new account
*/
const std::filesystem::path& getFilePath() const;
/**
* @brief Gets the metadata of the new account.
* @return The metadata of the new account
*/
const Models::AccountMetadata& getMetadata() const;
/**
* @brief Gets the folder of the new account path.
* @return The folder of the new account path
*/
std::filesystem::path getFolder() const;
/**
* @brief Sets the folder of the new account path.
* @param folder The folder of the new account path
*/
void setFolder(const std::filesystem::path& folder);
/**
* @brief Sets the name of the new account.
* @param name The new name of the new account
* @return True if the name was set, else false (the new name results in an existing account path and overwrite is off)
*/
bool setName(const std::string& name);
/**
* @brief Gets the password of the new account.
* @return The password of the new account
*/
const std::string& getPassword() const;
/**
* @brief Sets the password of the new account.
* @param password The new password of the new account
*/
void setPassword(const std::string& password);
/**
* @brief Gets whether or not to overwrite an existing account.
* @return True to overwrite, else false
*/
bool getOverwriteExisting() const;
/**
* @brief Sets whether or not to overwrite an existing account.
* @param overwriteExisting True to overwrite, else false
*/
void setOverwriteExisting(bool overwriteExisting);
/**
* @brief Sets the account type of the new account.
* @param accountType The new account type of the new account
*/
void setAccountType(Models::AccountType accountType);
/**
* @brief Sets the default transaction type of the new account.
* @param defaultTransactionType The new default transaction type of the new account
*/
void setDefaultTransactionType(Models::TransactionType defaultTransactionType);
/**
* @brief Sets the transaction reminders threshold of the new account.
* @param transactionReminderThreshold The new transaction reminders threshold of the new account
*/
void setTransactionRemindersThreshold(Models::RemindersThreshold transactionReminderThreshold);
/**
* @brief Sets off the custom currency on the new account.
*/
void setCustomCurrencyOff();
/**
* @brief Sets the custom currency of the new account.
* @param symbol The symbol of the new custom currency
* @param code The code of the new custom currency
* @param decimalSeparator The decimal separator of the new custom currency
* @param groupSeparator The group separator of the new custom currency
* @param decimalDigits The decimal digits of the new custom currency
* @param amountStyle The amount style of the new custom currency
* @return CurrencyCheckStatus
*/
Models::CurrencyCheckStatus setCustomCurrency(const std::string& symbol, const std::string& code, char decimalSeparator, char groupSeparator, int decimalDigits, Models::AmountStyle amountStyle);
/**
* @brief Gets the path of a file to import into the new account when created.
* @return The path of the file to import
*/
const std::filesystem::path& getImportFile() const;
/**
* @brief Sets the path of a file to import into the new account when created.
* @param importFile The path of the file to import
* @return True if the import file was set, else false (the file does not exist)
*/
bool setImportFile(const std::filesystem::path& importFile);

private:
std::vector<std::filesystem::path> m_openAccounts;
std::filesystem::path m_path;
Models::AccountMetadata m_metadata;
std::string m_password;
bool m_overwriteExisting;
std::filesystem::path m_importFile;
};
}

Expand Down
52 changes: 46 additions & 6 deletions libdenaro/src/controllers/mainwindowcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,7 @@ namespace Nickvision::Money::Shared::Controllers

std::shared_ptr<NewAccountDialogController> MainWindowController::createNewAccountDialogController() const
{
std::vector<std::filesystem::path> openAccounts;
for(const std::pair<const std::filesystem::path, std::shared_ptr<AccountViewController>>& pair : m_accountViewControllers)
{
openAccounts.push_back(pair.first);
}
return std::make_shared<NewAccountDialogController>(openAccounts);
return std::make_shared<NewAccountDialogController>();
}

const std::shared_ptr<AccountViewController>& MainWindowController::getAccountViewController(const std::filesystem::path& path) const
Expand Down Expand Up @@ -251,6 +246,51 @@ namespace Nickvision::Money::Shared::Controllers
return a.isEncrypted();
}

void MainWindowController::newAccount(const std::shared_ptr<NewAccountDialogController>& newAccountDialogController)
{
if(std::filesystem::exists(newAccountDialogController->getFilePath()))
{
//Check if overwrite is allowed
if(!newAccountDialogController->getOverwriteExisting())
{
m_notificationSent.invoke({ _("This account already exists."), NotificationSeverity::Error });
return;
}
//Check if the account is open in the app (cannot delete if open)
if(m_accountViewControllers.contains(newAccountDialogController->getFilePath()))
{
m_notificationSent.invoke({ _("This account cannot be overwritten."), NotificationSeverity::Error });
return;
}
std::filesystem::remove(newAccountDialogController->getFilePath());
}
Configuration& config{ Aura::getActive().getConfig<Configuration>("config") };
//Create the new account
std::unique_ptr<Account> account{ std::make_unique<Account>(newAccountDialogController->getFilePath()) };
account->login("");
account->setMetadata(newAccountDialogController->getMetadata());
account->importFromFile(newAccountDialogController->getImportFile(), config.getTransactionDefaultColor(), config.getGroupDefaultColor());
account->changePassword(newAccountDialogController->getPassword());
account.reset();
//Create the controller and open the account
std::shared_ptr<AccountViewController> controller{ nullptr };
try
{
controller = std::make_shared<AccountViewController>(newAccountDialogController->getFilePath(), newAccountDialogController->getPassword());
}
catch(const std::exception& e)
{
m_notificationSent.invoke({ e.what(), NotificationSeverity::Error });
}
if(controller)
{
m_accountViewControllers.emplace(std::make_pair(newAccountDialogController->getFilePath(), controller));
config.addRecentAccount(controller->toRecentAccount());
config.save();
m_accountAdded.invoke(controller);
}
}

void MainWindowController::openAccount(std::filesystem::path path, const std::string& password)
{
//Check if the file is a Denaro account file
Expand Down
113 changes: 111 additions & 2 deletions libdenaro/src/controllers/newaccountdialogcontroller.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,119 @@
#include "controllers/newaccountdialogcontroller.h"
#include <libnick/filesystem/userdirectories.h>

using namespace Nickvision::Filesystem;
using namespace Nickvision::Money::Shared::Models;

namespace Nickvision::Money::Shared::Controllers
{
NewAccountDialogController::NewAccountDialogController(const std::vector<std::filesystem::path>& openAccounts)
: m_openAccounts(openAccounts)
NewAccountDialogController::NewAccountDialogController()
: m_path{ UserDirectories::getDocuments() / "New.nmoney"},
m_metadata{ "", AccountType::Checking },
m_overwriteExisting{ false }
{

}

const std::filesystem::path& NewAccountDialogController::getFilePath() const
{
return m_path;
}

const AccountMetadata& NewAccountDialogController::getMetadata() const
{
return m_metadata;
}

std::filesystem::path NewAccountDialogController::getFolder() const
{
return m_path.stem();
}

void NewAccountDialogController::setFolder(const std::filesystem::path& folder)
{
m_path = folder / m_path.filename();
}

bool NewAccountDialogController::setName(const std::string& name)
{
if(std::filesystem::exists(m_path.stem() / (name + ".nmoney")) && !m_overwriteExisting)
{
return false;
}
m_metadata.setName(name);
m_path.replace_filename(name + ".nmoney");
return true;
}

const std::string& NewAccountDialogController::getPassword() const
{
return m_password;
}

void NewAccountDialogController::setPassword(const std::string& password)
{
m_password = password;
}

bool NewAccountDialogController::getOverwriteExisting() const
{
return m_overwriteExisting;
}

void NewAccountDialogController::setOverwriteExisting(bool overwriteExisting)
{
m_overwriteExisting = overwriteExisting;
}

void NewAccountDialogController::setAccountType(AccountType accountType)
{
m_metadata.setType(accountType);
}

void NewAccountDialogController::setDefaultTransactionType(TransactionType defaultTransactionType)
{
m_metadata.setDefaultTransactionType(defaultTransactionType);
}

void NewAccountDialogController::setTransactionRemindersThreshold(RemindersThreshold transactionReminderThreshold)
{
m_metadata.setTransactionRemindersThreshold(transactionReminderThreshold);
}

void NewAccountDialogController::setCustomCurrencyOff()
{
m_metadata.setUseCustomCurrency(false);
m_metadata.setCustomCurrency({});
}

CurrencyCheckStatus NewAccountDialogController::setCustomCurrency(const std::string& symbol, const std::string& code, char decimalSeparator, char groupSeparator, int decimalDigits, AmountStyle amountStyle)
{
Currency currency{ symbol, code };
currency.setDecimalSeparator(decimalSeparator);
currency.setGroupSeparator(groupSeparator);
currency.setDecimalDigits(decimalDigits);
currency.setAmountStyle(amountStyle);
CurrencyCheckStatus status{ currency.validate() };
if(status == CurrencyCheckStatus::Valid)
{
m_metadata.setUseCustomCurrency(true);
m_metadata.setCustomCurrency(currency);
}
return status;
}

const std::filesystem::path& NewAccountDialogController::getImportFile() const
{
return m_importFile;
}

bool NewAccountDialogController::setImportFile(const std::filesystem::path& importFile)
{
if(std::filesystem::exists(importFile))
{
m_importFile = importFile;
return true;
}
return false;
}
}
2 changes: 1 addition & 1 deletion org.nickvision.money.gnome/src/views/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ namespace Nickvision::Money::GNOME::Views
g_object_set_property(G_OBJECT(passwordEntry), "activates-default", &valTrue);
g_object_set_property(G_OBJECT(passwordEntry), "placeholder-text", &valPlaceholderText);
g_signal_connect(passwordEntry, "changed", G_CALLBACK(+[](GtkEditable* self, gpointer data){ *(reinterpret_cast<std::string*>(data)) = gtk_editable_get_text(self); }), &password);
AdwMessageDialog* messageDialog{ ADW_MESSAGE_DIALOG(adw_message_dialog_new(GTK_WINDOW(m_window), _("Unlock Account"), path.filename().string().c_str())) };
AdwMessageDialog* messageDialog{ ADW_MESSAGE_DIALOG(adw_message_dialog_new(GTK_WINDOW(m_window), path.filename().string().c_str(), nullptr)) };
adw_message_dialog_set_extra_child(messageDialog, GTK_WIDGET(passwordEntry));
adw_message_dialog_add_response(messageDialog, "cancel", _("Cancel"));
adw_message_dialog_add_response(messageDialog, "unlock", _("Unlock"));
Expand Down
Loading

0 comments on commit eae9127

Please sign in to comment.