File indexing completed on 2024-04-28 05:06:07

0001 /*
0002     SPDX-FileCopyrightText: 2000-2003 Michael Edwardes <mte@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2000-2003 Javier Campos Morales <javi_c@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2000-2003 Felix Rodriguez <frodriguez@users.sourceforge.net>
0005     SPDX-FileCopyrightText: 2000-2003 John C <thetacoturtle@users.sourceforge.net>
0006     SPDX-FileCopyrightText: 2000-2003 Thomas Baumgart <ipwizard@users.sourceforge.net>
0007     SPDX-FileCopyrightText: 2000-2003 Kevin Tambascio <ktambascio@users.sourceforge.net>
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #ifndef KMYMONEYUTILS_H
0012 #define KMYMONEYUTILS_H
0013 
0014 // ----------------------------------------------------------------------------
0015 // QT Includes
0016 
0017 #include <QStandardPaths>
0018 #include <QMap>
0019 #include <QUrl>
0020 #include <QDebug>
0021 
0022 // ----------------------------------------------------------------------------
0023 // KDE Headers
0024 
0025 // ----------------------------------------------------------------------------
0026 // Project Includes
0027 
0028 #ifndef KMYMONEY_DEPRECATED
0029 #  define KMYMONEY_DEPRECATED Q_DECL_DEPRECATED
0030 #endif
0031 
0032 class QIcon;
0033 
0034 /**
0035   * @author Thomas Baumgart
0036   */
0037 
0038 class QPixmap;
0039 class QWizard;
0040 class QWidget;
0041 
0042 class KGuiItem;
0043 class KXmlGuiWindow;
0044 
0045 class MyMoneyMoney;
0046 class MyMoneyAccount;
0047 class MyMoneySecurity;
0048 class MyMoneySchedule;
0049 class MyMoneySplit;
0050 class MyMoneyTransaction;
0051 class MyMoneyStatement;
0052 class MyMoneyInstitution;
0053 class MyMoneyForecast;
0054 class SplitModel;
0055 
0056 namespace eMyMoney {
0057 namespace Schedule {
0058 enum class Occurrence;
0059 enum class PaymentType;
0060 enum class WeekendOption;
0061 enum class Type;
0062 }
0063 namespace Split {
0064 enum class State;
0065 enum class InvestmentTransactionType;
0066 }
0067 }
0068 
0069 class KMyMoneyUtils
0070 {
0071 public:
0072     enum transactionTypeE {
0073         /**
0074           * Unknown transaction type (e.g. used for a transaction with only
0075           * a single split)
0076           */
0077         Unknown,
0078 
0079         /**
0080           * A 'normal' transaction is one that consists out two splits: one
0081           * referencing an income/expense account, the other referencing
0082           * an asset/liability account.
0083           */
0084         Normal,
0085 
0086         /**
0087           * A transfer denotes a transaction consisting of two splits.
0088           * Both of the splits reference an asset/liability
0089           * account.
0090           */
0091         Transfer,
0092 
0093         /**
0094           * Whenever a transaction consists of more than 2 splits,
0095           * it is treated as 'split transaction'.
0096           */
0097         SplitTransaction,
0098 
0099         /**
0100           * This transaction denotes a specific transaction where
0101           * a loan account is involved. Usually, a special dialog
0102           * is used to modify this transaction.
0103           */
0104         LoanPayment,
0105 
0106         /**
0107           * This transaction denotes a specific transaction where
0108           * an investment is involved. Usually, a special dialog
0109           * is used to modify this transaction.
0110           */
0111         InvestmentTransaction,
0112     };
0113 
0114     static const int maxHomePageItems = 5;
0115 
0116     KMyMoneyUtils() = default;
0117     ~KMyMoneyUtils() = default;
0118 
0119     /**
0120       * This method is used to convert the payment type from its
0121       * internal representation into a human readable format.
0122       *
0123       * @param paymentType numerical representation of the MyMoneySchedule
0124       *                  payment type
0125       *
0126       * @return QString representing the human readable format translated according to the language catalog
0127       *
0128       * @sa MyMoneySchedule::paymentMethodToString()
0129       */
0130     static const QString paymentMethodToString(eMyMoney::Schedule::PaymentType paymentType);
0131 
0132     /**
0133       * This method is used to convert the schedule weekend option from its
0134       * internal representation into a human readable format.
0135       *
0136       * @param weekendOption numerical representation of the MyMoneySchedule
0137       *                  weekend option
0138       *
0139       * @return QString representing the human readable format translated according to the language catalog
0140       *
0141       * @sa MyMoneySchedule::weekendOptionToString()
0142       */
0143     static const QString weekendOptionToString(eMyMoney::Schedule::WeekendOption weekendOption);
0144 
0145     /**
0146       * This method is used to convert the schedule type from its
0147       * internal representation into a human readable format.
0148       *
0149       * @param type numerical representation of the MyMoneySchedule
0150       *                  schedule type
0151       *
0152       * @return QString representing the human readable format translated according to the language catalog
0153       *
0154       * @sa MyMoneySchedule::scheduleTypeToString()
0155       */
0156     static const QString scheduleTypeToString(eMyMoney::Schedule::Type type);
0157 
0158     /**
0159       * This method is used to convert a numeric index of an item
0160       * represented on the home page into its string form.
0161       *
0162       * @param idx numeric index of item
0163       *
0164       * @return QString with text of this item
0165       */
0166     static const QString homePageItemToString(const int idx);
0167 
0168     /**
0169       * This method is used to convert the name of a home page item
0170       * to its internal numerical representation
0171       *
0172       * @param txt QString reference of the items name
0173       *
0174       * @retval 0 @p txt is unknown
0175       * @retval >0 numeric value for @p txt
0176       */
0177     static int stringToHomePageItem(const QString& txt);
0178 
0179     /**
0180       * Retrieve a KDE KGuiItem for the new schedule button.
0181       *
0182       * @return The KGuiItem that can be used to display the icon and text
0183       */
0184     static KGuiItem scheduleNewGuiItem();
0185 
0186     /**
0187       * Retrieve a KDE KGuiItem for the account filter button
0188       *
0189       * @return The KGuiItem that can be used to display the icon and text
0190       */
0191     static KGuiItem accountsFilterGuiItem();
0192 
0193     /**
0194       * This method adds the file extension passed as argument @p extension
0195       * to the end of the file name passed as argument @p name if it is not present.
0196       * If @p name contains an extension it will be removed.
0197       *
0198       * @param name filename to be checked
0199       * @param extension extension to be added (w/o the dot)
0200       *
0201       * @retval true if @p name was changed
0202       * @retval false if @p name remained unchanged
0203       */
0204     static bool appendCorrectFileExt(QString& name, const QString& extension);
0205 
0206     /**
0207       * Check that internal MyMoney engine constants use the same
0208       * values as the KDE constants.
0209       */
0210     static void checkConstants();
0211 
0212     /**
0213      * Returns common CSS stylesheet used by HTML renderers.
0214      *
0215      * Note that some elements are generated on the fly based on the user preferences.
0216      *
0217      * @param baseStylesheet filename of the custom CSS stylesheet file that will be used as a base.
0218      *                           If empty or null, a stock embedded stylesheet will be used.
0219      *
0220      *
0221      * @retval The processed CSS stylesheet with base and dynamic elements combined.
0222      */
0223     static QString getStylesheet(QString baseStylesheet = QString());
0224 
0225     /**
0226       * This method returns the split referencing a stock account if
0227       * one exists in the transaction passed as @p t. If none is present
0228       * in @p t, an empty MyMoneySplit() object will be returned.
0229       *
0230       * @param t transaction to be checked for a stock account
0231       * @return MyMoneySplit object referencing a stock account or an
0232       *         empty MyMoneySplit object.
0233       */
0234     static const MyMoneySplit stockSplit(const MyMoneyTransaction& t);
0235 
0236     /**
0237       * This method analyses the splits of a transaction and returns
0238       * the type of transaction. Possible values are defined by the
0239       * KMyMoneyUtils::transactionTypeE enum.
0240       *
0241       * @param t const reference to the transaction
0242       *
0243       * @return KMyMoneyUtils::transactionTypeE value of the action
0244       */
0245     static transactionTypeE transactionType(const MyMoneyTransaction& t);
0246 
0247     /**
0248       * This method modifies a scheduled loan transaction such that all
0249       * references to automatic calculated values are resolved to actual values.
0250       *
0251       * @param schedule const reference to the schedule the transaction is based on
0252       * @param transaction reference to the transaction to be checked and modified
0253       * @param balances QMap of (account-id,balance) pairs to be used as current balance
0254       *                 for the calculation of interest. If map is empty, the engine
0255       *                 will be interrogated for current balances.
0256       */
0257     static void calculateAutoLoan(const MyMoneySchedule& schedule, MyMoneyTransaction& transaction, const QMap<QString, MyMoneyMoney>& balances);
0258 
0259     /**
0260       * Returns the next check number for account @a acc. No check is performed, if the
0261       * number is already in use.
0262       */
0263     static QString nextCheckNumber(const MyMoneyAccount& acc);
0264 
0265     /**
0266       * Returns the next check free number for account @a acc.
0267       */
0268     static QString nextFreeCheckNumber(const MyMoneyAccount& acc);
0269 
0270     /**
0271      * Returns previous number if offset is -1 or
0272      * the following number if offset is 1.
0273      */
0274     static QString getAdjacentNumber(const QString& number, int offset = 1);
0275 
0276     /**
0277      * Returns the text representing the reconcile flag. If @a text is @p true
0278      * then the full text will be returned otherwise a short form (usually one character).
0279      */
0280     static QString reconcileStateToString(eMyMoney::Split::State flag, bool text = false);
0281 
0282     /**
0283      * Returns the transaction for @a schedule. In case of a loan payment the
0284      * transaction will be modified by calculateAutoLoan().
0285      * The ID of the transaction as well as the entryDate will be reset.
0286      *
0287      * @returns adjusted transaction
0288      */
0289     static MyMoneyTransaction scheduledTransaction(const MyMoneySchedule& schedule);
0290 
0291     /**
0292      * This method replaces the deprecated QApplication::mainWidget() from Qt 3.x.
0293      * It assumes that there is only one KXmlGuiWindow in the application, and
0294      * returns it.
0295      *
0296      * @return the first KXmlGuiWindow found in QApplication::topLevelWidgets()
0297      */
0298     static KXmlGuiWindow* mainWindow();
0299 
0300     /**
0301       * This method sets the button text and icons to the KDE standard ones
0302       * for the QWizard passed as argument.
0303       */
0304     static void updateWizardButtons(QWizard *);
0305 
0306     static void dissectInvestmentTransaction(const QModelIndex &investSplitIdx, QModelIndex &assetAccountSplitIdx, SplitModel* feeSplitModel, SplitModel* interestSplitModel, MyMoneySecurity &security, MyMoneySecurity &currency, eMyMoney::Split::InvestmentTransactionType &transactionType);
0307     static void processPriceList(const MyMoneyStatement& st);
0308 
0309     /**
0310       * This method deletes security and associated price list but asks beforehand.
0311       */
0312     static void deleteSecurity(const MyMoneySecurity &security, QWidget *parent = nullptr);
0313 
0314     /**
0315      * Check whether the url links to an existing file or not
0316      * @returns whether the file exists or not
0317      */
0318     static bool fileExists(const QUrl &url);
0319 
0320     static QString downloadFile(const QUrl &url);
0321 
0322     static std::tuple<bool, QString> newPayee(const QString& newnameBase);
0323 
0324     static std::tuple<bool, QString> newTag(const QString& newnameBase);
0325 
0326     /**
0327       * Creates a new institution entry in the MyMoneyFile engine
0328       *
0329       * @param institution MyMoneyInstitution object containing the data of
0330       *                    the institution to be created.
0331       */
0332     static void newInstitution(MyMoneyInstitution& institution);
0333 
0334     static QDebug debug();
0335 
0336     static MyMoneyForecast forecast();
0337 
0338     static bool canUpdateAllAccounts();
0339 
0340     static void showStatementImportResult(const QStringList& resultMessages, uint statementCount);
0341 
0342     /**
0343       * This method returns a double converted into a QString
0344       * after removing any group separators, trailing zeros,
0345       * and decimal separators.
0346       *
0347       * @param val reference to a qreal value to be converted
0348       * @param loc reference to a Qlocale converter
0349       * @param f format specifier:
0350       *          e - format as [-]9.9e[+|-]999
0351       *          E - format as [-]9.9E[+|-]999
0352       *          f - format as [-]9.9
0353       *          g - use e or f format, whichever is the most concise
0354       *          G - use E or f format, whichever is the most concise
0355       * @param prec precision representing the number of digits
0356       *             after the decimal point ('e', 'E' and 'f' formats)
0357       *             or the maximum number of significant digits
0358       *             (trailing zeroes are omitted) ('g' and 'G' formats)
0359       * @return QString object containing the converted value
0360       */
0361     static QString normalizeNumericString(const qreal& val, const QLocale& loc, const char f = 'g', const int prec = 6);
0362 
0363     /**
0364      * This method returns the tab order based on the configuration found in
0365      * the parameter identified in @a name in the section [TabOrder] of
0366      * the global configuration file kmymoneyrc. If no setting is found in the
0367      * configuration file, the @a defaultTabOrder is used.
0368      *
0369      * @sa setupTabOrder(), storeTabOrder()
0370      */
0371     static QStringList tabOrder(const QString& name, const QStringList& defaultTabOrder);
0372 
0373     /**
0374      * This method sets the tab order based on the order provided
0375      * in @a tabOrder. The named widgets are searched under
0376      * @a parent.
0377      *
0378      * @note the widgets must carry the respective object name.
0379      *
0380      * @sa tabOrder(), storeTabOrder()
0381      */
0382     static void setupTabOrder(QWidget* parent, const QStringList& tabOrder);
0383 
0384     /**
0385      * This method stores the tab order to the parameter identified
0386      * in @a name in the section [TabOrder] of the global configuration
0387      * file kmymoneyrc.
0388      *
0389      * @param name name of the widget type
0390      * @param tabOrder QStringList of widget names
0391      *
0392      * @sa setupTabOrder()
0393      */
0394     static void storeTabOrder(const QString& name, const QStringList& tabOrder);
0395 
0396     static bool tabFocusHelper(QWidget* topLevelWidget, bool next);
0397 };
0398 
0399 #endif