From 12570e268958a0b43a769d7d234c430142707e75 Mon Sep 17 00:00:00 2001 From: Humdinger Date: Mon, 1 Jul 2024 16:27:55 +0200 Subject: [PATCH] Don't let the user set internal categories The user mustn't set "internal categories" (Income, Spending, Split, Transfer, Uncategorized). Therefore they mustn't show up in the "Categories" window, or the menu of the category button, or when doing autocomplete. The new function IsInternalCategory() checks if a category is "internal". --- src/CategoryBox.cpp | 8 ++------ src/CategoryButton.cpp | 5 ++++- src/CategoryWindow.cpp | 12 ++++++------ src/Database.cpp | 20 ++++++++++++++++++++ src/Database.h | 2 ++ src/SplitView.cpp | 2 +- 6 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/CategoryBox.cpp b/src/CategoryBox.cpp index bbbbdf1..8ffad0b 100644 --- a/src/CategoryBox.cpp +++ b/src/CategoryBox.cpp @@ -56,7 +56,7 @@ CategoryBoxFilter::KeyFilter(const int32& key, const int32& mod) BString autocomplete = acc->AutocompleteCategory(string.String()); - if (autocomplete.CountChars() > 0) { + if (autocomplete.CountChars() > 0 && !IsInternalCategory(autocomplete.String())) { BMessage automsg(M_CATEGORY_AUTOCOMPLETE); automsg.AddInt32("start", strlen(TextControl()->Text()) + 1); automsg.AddString("string", autocomplete.String()); @@ -101,11 +101,7 @@ CategoryBox::Validate(void) return false; } - if (category.ICompare(B_TRANSLATE_CONTEXT("Income", "CommonTerms")) == 0 - || category.ICompare(B_TRANSLATE_CONTEXT("Spending", "CommonTerms")) == 0 - || category.ICompare(B_TRANSLATE_CONTEXT("Split", "CommonTerms")) == 0 - || category.ICompare(B_TRANSLATE_CONTEXT("Transfer", "CommonTerms")) == 0 - || category.ICompare(B_TRANSLATE_CONTEXT("Uncategorized", "CommonTerms")) == 0) { + if (IsInternalCategory(category.String())) { ShowAlert(B_TRANSLATE("Can't use this category name"), B_TRANSLATE( "CapitalBe uses 'Income', 'Spending', 'Split', 'Transfer', and 'Uncategorized' " diff --git a/src/CategoryButton.cpp b/src/CategoryButton.cpp index 63dd662..af8f1f5 100644 --- a/src/CategoryButton.cpp +++ b/src/CategoryButton.cpp @@ -137,7 +137,10 @@ CategoryButton::ShowPopUpMenu() while (!query.eof()) { int expense = query.getIntField(1); BString name = DeescapeIllegalCharacters(query.getStringField(0)); - + if (IsInternalCategory(name.String())) { + query.nextRow(); + continue; + } BMessage* msg = new BMessage(M_CATEGORY_CHOSEN); msg->AddString("category", name); diff --git a/src/CategoryWindow.cpp b/src/CategoryWindow.cpp index cf80767..dc8278d 100644 --- a/src/CategoryWindow.cpp +++ b/src/CategoryWindow.cpp @@ -214,11 +214,7 @@ CategoryView::MessageReceived(BMessage* msg) || msg->FindBool("expense", &expense) != B_OK) break; - if (name.ICompare(B_TRANSLATE_CONTEXT("Income", "CommonTerms")) == 0 - || name.ICompare(B_TRANSLATE_CONTEXT("Spending", "CommonTerms")) == 0 - || name.ICompare(B_TRANSLATE_CONTEXT("Split", "CommonTerms")) == 0 - || name.ICompare(B_TRANSLATE_CONTEXT("Transfer", "CommonTerms")) == 0 - || name.ICompare(B_TRANSLATE_CONTEXT("Uncategorized", "CommonTerms")) == 0) { + if (IsInternalCategory(name.String())) { ShowAlert(B_TRANSLATE("Can't use this category name"), B_TRANSLATE( "CapitalBe uses 'Income', 'Spending', 'Split', 'Transfer', and " @@ -303,9 +299,13 @@ CategoryView::RefreshCategoryList(void) CppSQLite3Query query = gDatabase.DBQuery( "SELECT * FROM categorylist ORDER BY name DESC", "CategoryView::CategoryView"); while (!query.eof()) { - int expense = query.getIntField(1); BString name = query.getStringField(0); + if (IsInternalCategory(name.String())) { + query.nextRow(); + continue; + } + int expense = query.getIntField(1); if (expense == SPENDING) fListView->AddUnder(new CategoryItem(name.String()), fSpendingItem); else if (expense == INCOME) diff --git a/src/Database.cpp b/src/Database.cpp index a256dc9..ac26bc7 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -1,4 +1,5 @@ #include "Database.h" +#include #include #include #include @@ -1604,3 +1605,22 @@ Database::DeescapeDatabase(void) return B_OK; } + +bool +IsInternalCategory(const char* category) +{ + const char* internal_categories[] = { + B_TRANSLATE_CONTEXT("Income", "CommonTerms"), + B_TRANSLATE_CONTEXT("Spending", "CommonTerms"), + B_TRANSLATE_CONTEXT("Split", "CommonTerms"), + B_TRANSLATE_CONTEXT("Transfer", "CommonTerms"), + B_TRANSLATE_CONTEXT("Uncategorized", "CommonTerms"), + NULL + }; + + for (int32 i = 0; internal_categories[i] != NULL; i++) { + if (strcasecmp(internal_categories[i], category) == 0) + return true; + } + return false; +} \ No newline at end of file diff --git a/src/Database.h b/src/Database.h index 589abf0..b7d9ff7 100644 --- a/src/Database.h +++ b/src/Database.h @@ -4,6 +4,7 @@ #include #include #include + #include "Account.h" #include "CBLocale.h" #include "CppSQLite3.h" @@ -22,6 +23,7 @@ enum category_type { }; BString AccountTypeToString(const AccountType& type); +bool IsInternalCategory(const char* category); class Database : public Notifier { public: diff --git a/src/SplitView.cpp b/src/SplitView.cpp index 7c6a31b..6c9f071 100644 --- a/src/SplitView.cpp +++ b/src/SplitView.cpp @@ -167,7 +167,7 @@ SplitView::SplitView(const char* name, const TransactionData& trans, const int32 if (fTransaction.CountCategories() > 1 || strcmp(fTransaction.NameAt(0), B_TRANSLATE_CONTEXT("Split", "CommonTerms")) == 0) { - fCategory->SetText(B_TRANSLATE("Split transaction")); + fCategory->SetText(B_TRANSLATE_CONTEXT("Split transaction", "CommonTerms")); fStartExpanded = true; }