Skip to content

Commit

Permalink
[ADD] : add a threaded import dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
aiekick committed Jun 10, 2024
1 parent c3a0e5f commit 4355158
Show file tree
Hide file tree
Showing 23 changed files with 416 additions and 120 deletions.
2 changes: 1 addition & 1 deletion 3rdparty/ImGuiPack
55 changes: 55 additions & 0 deletions plugins/LCLBroker/src/Abstract/Base.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <Abstract/Base.h>
#include <ctools/cTools.h>

bool parseDescription(const std::string& vDesc, //
std::string& vOutEntity,
std::string& vOutOperation,
std::string& vOutDescription) {
bool ret = false;
if (!vDesc.empty()) {
vOutDescription = vDesc;
const auto first_not_space_pos = vOutDescription.find_first_not_of(' ');
if (first_not_space_pos != std::string::npos) {
auto space_pos = vOutDescription.find(' ', first_not_space_pos);
if (space_pos != std::string::npos) {
vOutOperation = vOutDescription.substr(first_not_space_pos, space_pos - first_not_space_pos);
++space_pos; // inc from ' '
const auto first_slash_pos = vOutDescription.find('/', space_pos);
if (first_slash_pos != std::string::npos) {
const auto end_space_pos = vOutDescription.rfind(' ', first_slash_pos);
if (end_space_pos != std::string::npos) {
vOutEntity = vOutDescription.substr(space_pos, end_space_pos - space_pos);
}
}
if (vOutEntity.empty()) {
vOutEntity = vOutDescription.substr(space_pos);
}
}
}
while (ct::replaceString(vOutEntity, " ", " ")) {
}
while (ct::replaceString(vOutOperation, " ", " ")) {
}
while (ct::replaceString(vOutDescription, " ", " ")) {
}
if (vOutDescription.front() == ' ') {
vOutDescription = vOutDescription.substr(1);
}
if (vOutDescription.back() == ' ') {
vOutDescription = vOutDescription.substr(0, vOutDescription.size() - 1U);
}
ret = true;
}
return ret;
}

bool Base::init(Cash::PluginBridge* vBridgePtr) {
return true;
};

void Base::unit() {
};

std::string Base::getFileExt() const {
return "";
}
15 changes: 8 additions & 7 deletions plugins/LCLBroker/src/Abstract/Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

#include <apis/CashMePluginApi.h>

bool parseDescription(const std::string& vDesc, //
std::string& vOutEntity,
std::string& vOutOperation,
std::string& vOutDescription);

class Base : public Cash::BankStatementImportModule {
public:
virtual ~Base() = default;
bool init(Cash::PluginBridge* vBridgePtr) final {
return true;
};
void unit() final{};
std::string getFileExt() const override {
return "";
}
bool init(Cash::PluginBridge* vBridgePtr) final;
void unit() final;
std::string getFileExt() const override;
};
4 changes: 2 additions & 2 deletions plugins/LCLBroker/src/Headers/LCLBrokerBuild.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#define LCLBroker_Prefix "LCLBroker"
#define LCLBroker_BuildNumber 249
#define LCLBroker_BuildNumber 260
#define LCLBroker_MinorNumber 0
#define LCLBroker_MajorNumber 0
#define LCLBroker_BuildId "0.0.249"
#define LCLBroker_BuildId "0.0.260"
19 changes: 1 addition & 18 deletions plugins/LCLBroker/src/Modules/OfcAccountStatementModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,7 @@ Cash::AccountStatements OfcAccountStatementModule::importBankStatement(const std
trans.trans.amount = ct::dvariant(line).GetD();
} else if (line.find("<NAME>") != std::string::npos) {
ct::replaceString(line, "<NAME>", "");
trans.trans.description = line;
const auto& first_not_space = trans.trans.description.find_first_not_of(' ');
if (first_not_space != std::string::npos) {
const auto& space_pos = trans.trans.description.find(' ', first_not_space);
if (space_pos != std::string::npos) {
trans.trans.operation = trans.trans.description.substr(first_not_space, space_pos - first_not_space);
}
}
trans.trans.entity = ""; // todo
while (ct::replaceString(trans.trans.description, " ", " ")) {
// empty
}
if (trans.trans.description.front() == ' ') {
trans.trans.description = trans.trans.description.substr(1);
}
if (trans.trans.description.back() == ' ') {
trans.trans.description = trans.trans.description.substr(0, trans.trans.description.size() - 1U);
}
parseDescription(line, trans.trans.entity, trans.trans.operation, trans.trans.description);
} else if (line.find("<MEMO>") != std::string::npos) {
ct::replaceString(line, "<MEMO>", "");
trans.trans.comment = line;
Expand Down
18 changes: 1 addition & 17 deletions plugins/LCLBroker/src/Modules/PdfAccountStatementModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,23 +492,7 @@ class TableSolver {
}
} else if (idx == 1) {
if (is_new_line) {
trans.trans.description = tk.token;
const auto &first_not_space = trans.trans.description.find_first_not_of(' ');
if (first_not_space != std::string::npos) {
const auto &space_pos = trans.trans.description.find(' ', first_not_space);
if (space_pos != std::string::npos) {
trans.trans.operation = trans.trans.description.substr(first_not_space, space_pos - first_not_space);
}
}
trans.trans.entity = "";//todo
while (ct::replaceString(trans.trans.description, " ", " ")) {
}
if (trans.trans.description.front() == ' ') {
trans.trans.description = trans.trans.description.substr(1);
}
if (trans.trans.description.back() == ' ') {
trans.trans.description = trans.trans.description.substr(0, trans.trans.description.size() - 1U);
}
parseDescription(tk.token, trans.trans.entity, trans.trans.operation, trans.trans.description);
} else {
trans.trans.comment += tk.token;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Backend/MainBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <Frontend/MainFrontend.h>

#include <Panes/ConsolePane.h>
#include <Panes/AccountPane.h>

#include <Models/DataBase.h>

Expand Down Expand Up @@ -154,6 +155,9 @@ void MainBackend::PostRenderingActions() {
ProjectFile::Instance()->Clear();
m_NeedToCloseProject = false;
}

// Backend operation, cant be blocked if a imgui item is not displayed
AccountPane::Instance()->DoBackend();
}

bool MainBackend::IsNeedToCloseApp() {
Expand Down
4 changes: 2 additions & 2 deletions src/Frontend/Dialogs/AccountDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void AccountDialog::m_drawContentCreation(const ImVec2& vPos) {
m_AccountNameInputText.DisplayInputText(width, "Account Name", "", false, align, true);
m_AccountTypeInputText.DisplayInputText(width, "Account Type", "", false, align, true);
m_AccountNumberInputText.DisplayInputText(width, "Account Number", "", false, align, true);
m_DisplayAlignedWidget(width, "Base Solde", align, [this]() { ImGui::InputDouble("##BaseSolde", &m_AccountBaseSoldeInputDouble); });
ImGui::DisplayAlignedWidget(width, "Base Solde", align, [this]() { ImGui::InputDouble("##BaseSolde", &m_AccountBaseSoldeInputDouble); });
}

void AccountDialog::m_confirmDialogUpdate() {
Expand All @@ -152,7 +152,7 @@ void AccountDialog::m_drawContentUpdate(const ImVec2& vPos) {
m_AccountNameInputText.DisplayInputText(width, "Account Name", "", false, align, true);
m_AccountTypeInputText.DisplayInputText(width, "Account Type", "", false, align, true);
m_AccountNumberInputText.DisplayInputText(width, "Account Number", "", false, align, true);
m_DisplayAlignedWidget(width, "Base Solde", align, [this]() { ImGui::InputDouble("##BaseSolde", &m_AccountBaseSoldeInputDouble); });
ImGui::DisplayAlignedWidget(width, "Base Solde", align, [this]() { ImGui::InputDouble("##BaseSolde", &m_AccountBaseSoldeInputDouble); });
}

void AccountDialog::m_confirmDialogDeletion() {
Expand Down
2 changes: 1 addition & 1 deletion src/Frontend/Dialogs/AccountDialog.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <Headers/DatasDef.h>
#include <Frontend/Dialogs/abstract/ADataDialog.hpp>
#include <Frontend/Dialogs/abstract/ADataDialog.h>

class AccountDialog : public ADataDialog {
private:
Expand Down
2 changes: 1 addition & 1 deletion src/Frontend/Dialogs/BankDialog.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <Headers/DatasDef.h>
#include <Frontend/Dialogs/abstract/ADataDialog.hpp>
#include <Frontend/Dialogs/abstract/ADataDialog.h>

class BankDialog : public ADataDialog {
private:
Expand Down
2 changes: 1 addition & 1 deletion src/Frontend/Dialogs/CategoryDialog.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <Headers/DatasDef.h>
#include <Frontend/Dialogs/abstract/ADataDialog.hpp>
#include <Frontend/Dialogs/abstract/ADataDialog.h>

class CategoryDialog : public ADataDialog {
private:
Expand Down
2 changes: 1 addition & 1 deletion src/Frontend/Dialogs/EntityDialog.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <Headers/DatasDef.h>
#include <Frontend/Dialogs/abstract/ADataDialog.hpp>
#include <Frontend/Dialogs/abstract/ADataDialog.h>

class EntityDialog : public ADataDialog {
private:
Expand Down
2 changes: 1 addition & 1 deletion src/Frontend/Dialogs/OperationDialog.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <Headers/DatasDef.h>
#include <Frontend/Dialogs/abstract/ADataDialog.hpp>
#include <Frontend/Dialogs/abstract/ADataDialog.h>

class OperationDialog : public ADataDialog {
private:
Expand Down
8 changes: 4 additions & 4 deletions src/Frontend/Dialogs/TransactionDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ void TransactionDialog::m_drawContentCreation(const ImVec2& vPos) {
m_TransactionDateInputText.DisplayInputText(width, "Date", "", false, align);
m_TransactionDescriptionInputText.DisplayInputText(width, "Description", "", false, align);
m_TransactionCommentInputText.DisplayInputText(width, "Comment", "", false, align);
m_DisplayAlignedWidget(width, "Amount", align, [this]() { ImGui::InputDouble("##Amount", &m_TransactionAmountInputDouble); });
m_DisplayAlignedWidget(width, "Confirmed", align, [this]() { ImGui::CheckBoxBoolDefault("##Confirmed", &m_TransactionConfirmed, false); });
ImGui::DisplayAlignedWidget(width, "Amount", align, [this]() { ImGui::InputDouble("##Amount", &m_TransactionAmountInputDouble); });
ImGui::DisplayAlignedWidget(width, "Confirmed", align, [this]() { ImGui::CheckBoxBoolDefault("##Confirmed", &m_TransactionConfirmed, false); });
}

void TransactionDialog::m_drawContentUpdate(const ImVec2& vPos) {
Expand All @@ -72,9 +72,9 @@ void TransactionDialog::m_drawContentUpdate(const ImVec2& vPos) {
m_TransactionCommentInputText.DisplayInputText(width, "Comment", "", false, align);
// the update all if for descriptive items buit not for amounrt
if (getCurrentMode() != DataDialogMode::MODE_UPDATE_ALL) {
m_DisplayAlignedWidget(width, "Amount", align, [this]() { ImGui::InputDouble("##Amount", &m_TransactionAmountInputDouble); });
ImGui::DisplayAlignedWidget(width, "Amount", align, [this]() { ImGui::InputDouble("##Amount", &m_TransactionAmountInputDouble); });
}
m_DisplayAlignedWidget(width, "Confirmed", align, [this]() {
ImGui::DisplayAlignedWidget(width, "Confirmed", align, [this]() {
if (!m_TransactionConfirmedManyValues) {
ImGui::CheckBoxBoolDefault("##Confirmed", &m_TransactionConfirmed, false);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Frontend/Dialogs/TransactionDialog.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <Headers/DatasDef.h>
#include <Frontend/Dialogs/abstract/ADataDialog.hpp>
#include <Frontend/Dialogs/abstract/ADataDialog.h>

class TransactionDialog : public ADataDialog {
private:
Expand Down
16 changes: 1 addition & 15 deletions src/Frontend/Dialogs/abstract/ADataDialog.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <Frontend/Dialogs/abstract/ADataDialog.hpp>
#include <Frontend/Dialogs/abstract/ADataDialog.h>

ADataDialog::ADataDialog(const char* vPopupLabel) : m_PopupLabel(vPopupLabel) {
}
Expand Down Expand Up @@ -54,17 +54,3 @@ bool ADataDialog::draw(const ImVec2& vPos) {
}
return ret;
}

void ADataDialog::m_DisplayAlignedWidget(const float& vWidth, const std::string& vLabel, const float& vOffsetFromStart, std::function<void()> vWidget) {
float px = ImGui::GetCursorPosX();
ImGui::Text("%s", vLabel.c_str());
ImGui::SameLine(vOffsetFromStart);
const float w = vWidth - (ImGui::GetCursorPosX() - px);
ImGui::PushID(++ImGui::CustomStyle::pushId);
ImGui::PushItemWidth(w);
if (vWidget != nullptr) {
vWidget();
}
ImGui::PopItemWidth();
ImGui::PopID();
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ class ADataDialog {
virtual bool m_canConfirm() = 0;
virtual void m_confirmDialog() = 0;
virtual void m_cancelDialog() = 0;
void m_DisplayAlignedWidget(const float& vWidth, const std::string& vLabel, const float& vOffsetFromStart, std::function<void()> vWidget);
};
4 changes: 2 additions & 2 deletions src/Headers/CashMeBuild.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#define CashMe_Prefix "CashMe"
#define CashMe_BuildNumber 533
#define CashMe_BuildNumber 544
#define CashMe_MinorNumber 0
#define CashMe_MajorNumber 0
#define CashMe_BuildId "0.0.533"
#define CashMe_BuildId "0.0.544"
14 changes: 8 additions & 6 deletions src/Models/DataBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1357,12 +1357,14 @@ CREATE TABLE settings (
}

void DataBase::m_CloseDB() {
if (m_SqliteDB) {
if (sqlite3_close(m_SqliteDB) == SQLITE_BUSY) {
// try to force closing
sqlite3_close_v2(m_SqliteDB);
if (!m_TransactionStarted) {
if (m_SqliteDB) {
if (sqlite3_close(m_SqliteDB) == SQLITE_BUSY) {
// try to force closing
sqlite3_close_v2(m_SqliteDB);
}
}
m_SqliteDB = nullptr;
// there is also sqlite3LeaveMutexAndCloseZombie when sqlite is stucked
}
m_SqliteDB = nullptr;
// there is also sqlite3LeaveMutexAndCloseZombie when sqlite is stucked
}
53 changes: 17 additions & 36 deletions src/Panes/AccountPane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ bool AccountPane::DrawDialogsAndPopups(const uint32_t& /*vCurrentFrame*/, const
const ImVec2 center = vRect.GetCenter();

bool ret = false;

ret |= m_BankDialog.draw(center);
if (m_AccountDialog.draw(center)) {
DebitCreditPane::Instance()->Load();
Expand All @@ -83,6 +84,8 @@ bool AccountPane::DrawDialogsAndPopups(const uint32_t& /*vCurrentFrame*/, const
ret |= m_CategoryDialog.draw(center);
ret |= m_OperationDialog.draw(center);
ret |= m_TransactionDialog.draw(center);

m_ImportThread.drawDialog(center);

if (ret) {
m_refreshDatas();
Expand Down Expand Up @@ -114,6 +117,10 @@ bool AccountPane::DrawWidgets(const uint32_t& /*vCurrentFrame*/, ImGuiContext* v
return false;
}

void AccountPane::DoBackend() {
m_ImportThread.finishIfNeeded();
}

void AccountPane::Load() {
m_refreshDatas();
}
Expand Down Expand Up @@ -549,42 +556,16 @@ void AccountPane::m_GetAvailableDataBrokers() {
}
}

void AccountPane::m_ImportFromFiles(const std::vector<std::string> vFiles) {
auto ptr = m_SelectedBroker.lock();
if (ptr != nullptr) {
for (const auto& file : vFiles) {
const auto& stmt = ptr->importBankStatement(file);
if (!stmt.statements.empty()) {
RowID account_id = 0U;
if (DataBase::Instance()->GetAccount(stmt.account.number, account_id)) {
if (DataBase::Instance()->BeginTransaction()) {
for (const auto& s : stmt.statements) {
DataBase::Instance()->AddTransaction( //
account_id,
s.entity,
s.operation,
s.category,
s.source,
s.source_type,
s.source_sha1,
s.date,
s.description,
s.comment,
s.amount,
s.confirmed,
s.hash);
}
DataBase::Instance()->CommitTransaction();
m_refreshDatas();
m_refreshFiltering();
}
} else {
LogVarError("Import interrupted, no account found for %s", stmt.account.number.c_str());
break;
}
}
}
}
void AccountPane::m_ImportFromFiles(const std::vector<std::string>& vFiles) {
m_ImportThread.start( //
"Import Datas",
m_SelectedBroker,
vFiles,
[this]() {
m_refreshDatas();
m_refreshFiltering();
},
nullptr);
}

void AccountPane::m_ResetFiltering() {
Expand Down
Loading

0 comments on commit 4355158

Please sign in to comment.