diff --git a/libgnucash/app-utils/gnc-option-impl.cpp b/libgnucash/app-utils/gnc-option-impl.cpp index 0f0c566645..ea4e52e23b 100644 --- a/libgnucash/app-utils/gnc-option-impl.cpp +++ b/libgnucash/app-utils/gnc-option-impl.cpp @@ -326,6 +326,7 @@ template GncOptionValue::GncOptionValue(const GncOptionValue::GncOptionValue(const GncOptionValue&); template GncOptionValue::GncOptionValue(const GncOptionValue&); template GncOptionValue::GncOptionValue(const GncOptionValue&); +template GncOptionValue::GncOptionValue(const GncOptionValue&); template GncOptionValue::GncOptionValue(const GncOptionValue&); template GncOptionValue::GncOptionValue(const GncOptionValue&); template GncOptionValue::GncOptionValue(const GncOptionValue&); @@ -340,6 +341,7 @@ template void GncOptionValue::set_value(const char*); template void GncOptionValue::set_value(std::string); template void GncOptionValue::set_value(const QofInstance*); template void GncOptionValue::set_value(const QofQuery*); +template void GncOptionValue::set_value(const GncOwner*); template void GncOptionValue::set_value(RelativeDatePeriod); template void GncOptionValue::set_value(size_t); template void GncOptionValue::set_value(GncOptionAccountList); @@ -353,6 +355,7 @@ template void GncOptionValue::set_default_value(const char*); template void GncOptionValue::set_default_value(std::string); template void GncOptionValue::set_default_value(const QofInstance*); template void GncOptionValue::set_default_value(const QofQuery*); +template void GncOptionValue::set_default_value(const GncOwner*); template void GncOptionValue::set_default_value(RelativeDatePeriod); template void GncOptionValue::set_default_value(size_t); template void GncOptionValue::set_default_value(GncOptionAccountList); @@ -366,6 +369,7 @@ template void GncOptionValue::reset_default_value(); template void GncOptionValue::reset_default_value(); template void GncOptionValue::reset_default_value(); template void GncOptionValue::reset_default_value(); +template void GncOptionValue::reset_default_value(); template void GncOptionValue::reset_default_value(); template void GncOptionValue::reset_default_value(); template void GncOptionValue::reset_default_value(); diff --git a/libgnucash/app-utils/gnc-option-impl.hpp b/libgnucash/app-utils/gnc-option-impl.hpp index 20d33f4c62..485b1fcce2 100644 --- a/libgnucash/app-utils/gnc-option-impl.hpp +++ b/libgnucash/app-utils/gnc-option-impl.hpp @@ -309,6 +309,7 @@ std::istream& operator>>(std::istream& iss, OptType& opt) { std::decay_t value; if constexpr (std::is_same_v, SCM> || + std::is_same_v, const _gncOwner*> || std::is_same_v, const _QofQuery*>) return iss; else @@ -1198,6 +1199,20 @@ operator>> (std::istream& iss, /** QofQuery Options */ +inline std::istream& +gnc_option_from_scheme(std::istream& iss, GncOptionValue& opt) +{ +//FIXME: Implement or maybe rethink. + return iss; +} + +inline std::ostream& +gnc_option_to_scheme(std::ostream& oss, GncOptionValue& opt) +{ +//FIXME: Implement or maybe rethink. + return oss; +} + inline std::istream& gnc_option_from_scheme(std::istream& iss, GncOptionValue& opt) { diff --git a/libgnucash/app-utils/gnc-option.cpp b/libgnucash/app-utils/gnc-option.cpp index 1153e0c943..812a2c1b7c 100644 --- a/libgnucash/app-utils/gnc-option.cpp +++ b/libgnucash/app-utils/gnc-option.cpp @@ -468,6 +468,8 @@ GncOption::from_scheme(std::istream& iss) (std::is_same_v, GncOptionMultichoiceValue>) || std::is_same_v, + GncOptionValue> || + std::is_same_v, GncOptionValue> || std::is_same_v, GncOptionValue> || @@ -564,6 +566,8 @@ template GncOption::GncOption(const char*, const char*, const char*, const char*, SCM, GncOptionUIType); template GncOption::GncOption(const char*, const char*, const char*, const char*, const QofQuery*, GncOptionUIType); +template GncOption::GncOption(const char*, const char*, const char*, + const char*, const GncOwner*, GncOptionUIType); template bool GncOption::get_value() const; template int GncOption::get_value() const; diff --git a/libgnucash/app-utils/gnc-option.hpp b/libgnucash/app-utils/gnc-option.hpp index dfc875befb..66e2ee25fd 100644 --- a/libgnucash/app-utils/gnc-option.hpp +++ b/libgnucash/app-utils/gnc-option.hpp @@ -40,6 +40,10 @@ extern "C" struct OptionClassifier; class GncOptionUIItem; using GncOptionUIItemPtr = std::unique_ptr; +#ifndef SWIG //SWIG pulls in GncOwner from swig-engine. +struct _gncOwner; +using GncOwner = _gncOwner; +#endif struct _QofQuery; using QofQuery = _QofQuery; struct QofInstance_s; @@ -56,6 +60,7 @@ using GncOptionVariant = std::variant, GncOptionValue, GncOptionValue, GncOptionValue, + GncOptionValue, GncOptionValue, GncOptionAccountValue, GncOptionMultichoiceValue, diff --git a/libgnucash/app-utils/gnc-optiondb.cpp b/libgnucash/app-utils/gnc-optiondb.cpp index abb80efed1..3c0c1c79da 100644 --- a/libgnucash/app-utils/gnc-optiondb.cpp +++ b/libgnucash/app-utils/gnc-optiondb.cpp @@ -1061,9 +1061,19 @@ gnc_register_number_plot_size_option(GncOptionDB* db, void gnc_register_query_option(GncOptionDB* db, const char* section, const char* name, const char* key, - const char* doc_string, QofQuery* value) + const char* doc_string, const QofQuery* value) { - GncOption option{section, name, key, doc_string, (const QofInstance*)value, + GncOption option{section, name, key, doc_string, value, + GncOptionUIType::INTERNAL}; + db->register_option(section, std::move(option)); +} + +void +gnc_register_owner_option(GncOptionDB* db, const char* section, + const char* name, const char* key, + const char* doc_string, const GncOwner* value) +{ + GncOption option{section, name, key, doc_string, value, GncOptionUIType::INTERNAL}; db->register_option(section, std::move(option)); } diff --git a/libgnucash/app-utils/gnc-optiondb.hpp b/libgnucash/app-utils/gnc-optiondb.hpp index 92e04cf5e5..e445f08370 100644 --- a/libgnucash/app-utils/gnc-optiondb.hpp +++ b/libgnucash/app-utils/gnc-optiondb.hpp @@ -501,18 +501,43 @@ inline void gnc_register_number_plot_size_option(const GncOptionDBPtr& db, */ void gnc_register_query_option(GncOptionDB* db, const char* section, const char* name, const char* key, - const char* doc_string, QofQuery* value); + const char* doc_string, const QofQuery* value); /** * As above but takes a const GncOptionDBPtr& (const std::unique_ptr&) for calling from C++. */ inline void gnc_register_query_option(GncOptionDBPtr& db, const char* section, const char* name, const char* key, - const char* doc_string, QofQuery* value) + const char* doc_string, + const QofQuery* value) { gnc_register_query_option(db.get(), section, name, key, doc_string, value); } +/** + * Create a new GncOwner option and register it in the options database. + * + * @param db A GncOptionDB* for calling from C. Caller retains ownership. + * @param section The database section for the option. + * @param name The option name. + * @param doc_string A description of the option. This will be used in tooltips and should be marked for translation. + * @param value The initial and default value for the option. + */ +void gnc_register_owner_option(GncOptionDB* db, const char* section, + const char* name, const char* key, + const char* doc_string, const GncOwner* value); + +/** + * As above but takes a const GncOptionDBPtr& (const std::unique_ptr&) for calling from C++. + */ +inline void gnc_register_owner_option(GncOptionDBPtr& db, const char* section, + const char* name, const char* key, + const char* doc_string, + const GncOwner* value) +{ + gnc_register_owner_option(db.get(), section, name, key, doc_string, value); +} + /** * Create a new color option and register it in the options database. * diff --git a/libgnucash/app-utils/gnc-optiondb.i b/libgnucash/app-utils/gnc-optiondb.i index 9fbfa90834..bf991c481d 100644 --- a/libgnucash/app-utils/gnc-optiondb.i +++ b/libgnucash/app-utils/gnc-optiondb.i @@ -53,6 +53,7 @@ SCM scm_init_sw_gnc_optiondb_module(void); %include %import %import (module="sw_engine") +%import (module="sw_engine") %import (module="sw_engine") %import (module="sw_engine") %import (module="sw_engine") @@ -195,13 +196,16 @@ scm_from_value(const QofInstance* value) type = SWIGTYPE_p__gncJob; else if (GNC_IS_VENDOR(value)) type = SWIGTYPE_p__gncVendor; -/* There is no type macro for QofQuery, it's not a GObject. - else if (GNC_IS_QOFQUERY(value)) - type = SWIGTYPE_p_Query; -*/ + return SWIG_NewPointerObj(ptr, type, FALSE); } +template <> inline SCM +scm_from_value(QofInstance* value) +{ + return scm_from_value(value); +} + template <> inline SCM scm_from_value(const QofQuery* value) { @@ -216,9 +220,16 @@ scm_from_value(QofQuery* value) } template <> inline SCM -scm_from_value(QofInstance* value) +scm_from_value(const GncOwner* value) { - return scm_from_value(value); + auto ptr{static_cast(const_cast(value))}; + return SWIG_NewPointerObj(ptr, SWIGTYPE_p__gncOwner, FALSE); +} + +template <> inline SCM +scm_from_value(GncOwner* value) +{ + return scm_from_value(value); } template inline ValueType @@ -267,16 +278,16 @@ scm_to_value(SCM new_value) { if (new_value == SCM_BOOL_F) return nullptr; - + auto info = SWIG_PointerType(new_value); - static const std::array types{ - SWIGTYPE_p_QofInstance_s, SWIGTYPE_p_gnc_commodity, - SWIGTYPE_p_budget_s, SWIGTYPE_p__gncInvoice, - SWIGTYPE_p__gncTaxTable, SWIGTYPE_p_Account, - SWIGTYPE_p__gncCustomer, SWIGTYPE_p__gncEmployee, - SWIGTYPE_p__gncJob, SWIGTYPE_p__gncVendor - }; + static const std::array types{ + SWIGTYPE_p_QofInstance_s, SWIGTYPE_p_gnc_commodity, + SWIGTYPE_p_budget_s, SWIGTYPE_p__gncInvoice, + SWIGTYPE_p__gncTaxTable, SWIGTYPE_p_Account, + SWIGTYPE_p__gncCustomer, SWIGTYPE_p__gncEmployee, + SWIGTYPE_p__gncJob, SWIGTYPE_p__gncVendor + }; void* ptr{}; auto pos = std::find_if(types.begin(), types.end(), [&new_value, &ptr](auto type){ @@ -298,6 +309,16 @@ scm_to_value(SCM new_value) return static_cast(ptr); } +template <> inline const GncOwner* +scm_to_value(SCM new_value) +{ + if (new_value == SCM_BOOL_F) + return nullptr; + void* ptr{}; + SWIG_ConvertPtr(new_value, &ptr, SWIGTYPE_p__gncOwner, 0); + return static_cast(ptr); +} + template <>inline GncOptionAccountList scm_to_value(SCM new_value) { @@ -776,6 +797,7 @@ wrap_unique_ptr(GncOptionDBPtr, GncOptionDB); %template(gnc_make_int64_option) gnc_make_option; %template(gnc_make_qofinstance_option) gnc_make_option; %template(gnc_make_query_option) gnc_make_option; +%template(gnc_make_owner_option) gnc_make_option; %extend GncOption { SCM get_scm_value() diff --git a/libgnucash/app-utils/options.scm b/libgnucash/app-utils/options.scm index 97905c5db7..1ade4370f8 100644 --- a/libgnucash/app-utils/options.scm +++ b/libgnucash/app-utils/options.scm @@ -232,15 +232,22 @@ (gnc-make-SCM-option section name key desc default type))) (define-public (gnc:make-owner-option section name key docstring getter validator owner-type) (issue-deprecation-warning "gnc:make-owner-option is deprecated. Make and register the option in one command with gnc-register-owner-option.") - (let ((ui-type (cond + (let* ((ui-type (cond ((eqv? owner-type GNC-OWNER-CUSTOMER) (GncOptionUIType-CUSTOMER)) ((eqv? owner-type GNC-OWNER-VENDOR) (GncOptionUIType-VENDOR)) ((eqv? owner-type GNC-OWNER-EMPLOYEE) (GncOptionUIType-EMPLOYEE)) ((eqv? owner-type GNC-OWNER-JOB) (GncOptionUIType-JOB)) (else (GncOptionUIType-INTERNAL)))) - (defval (if getter (getter) #f))) - (format #t "Making owner option ~a:~a ~a ~a~%" section name defval ui-type)(force-output) - (gnc-make-qofinstance-option section name key docstring defval ui-type))) + + (guid (gncOwnerReturnGUID (getter))) + (book (gnc-get-current-book)) + (defval (cond + ((eqv? owner-type GNC-OWNER-CUSTOMER) (gncCustomerLookupFlip guid book)) + ((eqv? owner-type GNC-OWNER-VENDOR) (gncVendorLookupFlip guid book)) + ((eqv? owner-type GNC-OWNER-EMPLOYEE) (gncEmployeeLookupFlip guid book)) + ((eqv? owner-type GNC-OWNER-JOB) (gncJobLookupFlip guid book))))) + + (gnc-make-owner-option section name key docstring defval ui-type))) (define-public (gnc:make-invoice-option section name key docstring getter validator) (issue-deprecation-warning "gnc:make-invoice-option is deprecated. Make and register the option in one command with gnc-register-ionvoice-option.") (gnc-make-qofinstance-option section name key docstring #f (GncOptionUIType-INVOICE)))