File indexing completed on 2024-04-28 16:29:35

0001 /*
0002     SPDX-FileCopyrightText: 2000-2001 Michael Edwardes <mte@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2017, 2018 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #ifndef KMYMONEY_H
0007 #define KMYMONEY_H
0008 
0009 // ----------------------------------------------------------------------------
0010 // QT Includes
0011 
0012 #include <QList>
0013 #include <QByteArray>
0014 #include <QFileDialog>
0015 #include <QUrl>
0016 
0017 // ----------------------------------------------------------------------------
0018 // KDE Includes
0019 
0020 #include <KXmlGuiWindow>
0021 
0022 // ----------------------------------------------------------------------------
0023 // Project Includes
0024 
0025 #include <imymoneyprocessingcalendar.h>
0026 
0027 #include "kmymoneyutils.h"
0028 
0029 #include "mymoneyaccount.h"
0030 #include "mymoney/onlinejob.h"
0031 #include "onlinejobtyped.h"
0032 #include "mymoneykeyvaluecontainer.h"
0033 #include "mymoneymoney.h"
0034 #include "selectedtransactions.h"
0035 #include "mymoneysplit.h"
0036 #include "mymoneytransaction.h"
0037 #include "mymoneyenums.h"
0038 #include "viewenums.h"
0039 
0040 class QResizeEvent;
0041 class MyMoneyObject;
0042 class MyMoneyInstitution;
0043 class MyMoneyAccount;
0044 class MyMoneySecurity;
0045 class MyMoneyPayee;
0046 class MyMoneyPrice;
0047 class MyMoneyTag;
0048 class MyMoneySplit;
0049 class MyMoneyTransaction;
0050 class WebConnect;
0051 class creditTransfer;
0052 class IMyMoneyOperationsFormat;
0053 
0054 template <class T> class onlineJobTyped;
0055 typedef  void (*KMyMoneyAppCallback)(int, int, const QString &);
0056 
0057 namespace eKMyMoney {
0058 enum class FileAction;
0059 }
0060 namespace eDialogs {
0061 enum class ScheduleResultCode;
0062 }
0063 namespace eMenu {
0064 enum class Action;
0065 enum class Menu;
0066 }
0067 
0068 /*! \mainpage KMyMoney Main Page for API documentation.
0069  *
0070  * \section intro Introduction
0071  *
0072  * This is the API documentation for KMyMoney.  It should be used as a reference
0073  * for KMyMoney developers and users who wish to see how KMyMoney works.  This
0074  * documentation will be kept up-to-date as development progresses and should be
0075  * read for new features that have been developed in KMyMoney.
0076  */
0077 
0078 /**
0079   * The base class for KMyMoney application windows. It sets up the main
0080   * window and reads the config file as well as providing a menubar, toolbar
0081   * and statusbar.
0082   *
0083   * @see KMyMoneyView
0084   *
0085   * @author Michael Edwardes 2000-2001
0086   * @author Thomas Baumgart 2006-2008
0087   *
0088   * @short Main application class.
0089   */
0090 class KMyMoneyApp : public KXmlGuiWindow, public IMyMoneyProcessingCalendar
0091 {
0092     Q_OBJECT
0093 
0094 private Q_SLOTS:
0095     /**
0096       * Add a context menu to the list used by KMessageBox::informationList to display the consistency check results.
0097       */
0098     void slotInstallConsistencyCheckContextMenu();
0099 
0100     /**
0101       * Handle the context menu of the list used by KMessageBox::informationList to display the consistency check results.
0102       */
0103     void slotShowContextMenuForConsistencyCheck(const QPoint &);
0104 
0105 protected Q_SLOTS:
0106     /**
0107       * This slot is intended to be used as part of auto saving. This is used when the
0108       * QTimer emits the timeout signal and simply checks that the file is dirty (has
0109       * received modifications to its contents), and call the appropriate method to
0110       * save the file. Furthermore, re-starts the timer (possibly not needed).
0111       * @author mvillarino 2005
0112       * @see KMyMoneyApp::slotDataChanged()
0113       */
0114     void slotAutoSave();
0115 
0116     /**
0117       * This slot re-enables all message for which the "Don't show again"
0118       * option had been selected.
0119       */
0120     void slotEnableMessages();
0121 
0122     /**
0123       * Called to run performance test.
0124       */
0125     void slotPerformanceTest();
0126 
0127     /**
0128       * Called to generate the sql to create kmymoney database tables etc.
0129       */
0130     void slotGenerateSql();
0131 
0132 #ifdef KMM_DEBUG
0133     /**
0134       * Called when the user asks for file information.
0135       */
0136     void slotFileFileInfo();
0137 
0138     /**
0139       * Debugging only: turn on/off traces
0140       */
0141     void slotToggleTraces();
0142 #endif
0143 
0144     /**
0145       * Debugging only: turn on/off timers
0146       */
0147     void slotToggleTimers();
0148 
0149     /**
0150       * Called when the user asks for the personal information.
0151       */
0152     void slotFileViewPersonal();
0153 
0154     void slotLoadAccountTemplates();
0155     void slotSaveAccountTemplates();
0156 
0157     /**
0158       * Open up the application wide settings dialog.
0159       *
0160       * @see KSettingsDlg
0161       */
0162     void slotSettings();
0163 
0164     /**
0165       * Called to show credits window.
0166       */
0167     void slotShowCredits();
0168 
0169     /**
0170       * Called when the user wishes to backup the current file
0171       */
0172     void slotBackupFile();
0173 
0174     /**
0175       * Perform mount operation before making a backup of the current file
0176       */
0177     void slotBackupMount();
0178 
0179     /**
0180       * Perform the backup write operation
0181       */
0182     bool slotBackupWriteFile();
0183 
0184     /**
0185       * Perform unmount operation after making a backup of the current file
0186       */
0187     void slotBackupUnmount();
0188 
0189     /**
0190       * Finish backup of the current file
0191       */
0192     void slotBackupFinish();
0193 
0194     /**
0195       * Handle events on making a backup of the current file
0196       */
0197     void slotBackupHandleEvents();
0198 
0199     void slotShowTipOfTheDay();
0200 
0201     void slotShowPreviousView();
0202 
0203     void slotShowNextView();
0204 
0205     void slotViewSelected(View view);
0206 
0207     /**
0208       * Calls the print logic for the current view
0209       */
0210     void slotPrintView();
0211 
0212     /**
0213       * Call this slot, if any configuration parameter has changed
0214       */
0215     void slotUpdateConfiguration(const QString &dialogName);
0216 
0217     /**
0218       * This slot is used to start new features during the development cycle
0219       */
0220     void slotNewFeature();
0221 
0222     /**
0223       * This slot triggers an update of all views and restarts
0224       * a single shot timer to call itself again at beginning of
0225       * the next day.
0226       */
0227     void slotDateChanged();
0228 
0229     /**
0230       * This slot will be called when the engine data changed
0231       * and the application object needs to update its state.
0232       */
0233     void slotDataChanged();
0234 
0235     /**
0236       * This slot collects information for a new scheduled transaction
0237       * based on transaction @a t and @a occurrence and saves it in the engine.
0238       */
0239     void slotScheduleNew(const MyMoneyTransaction& t, eMyMoney::Schedule::Occurrence occurrence = eMyMoney::Schedule::Occurrence::Monthly);
0240 
0241     void slotStatusProgressDone();
0242 
0243     void slotCheckSchedules();
0244 
0245 public:
0246     /**
0247       * This method checks if there is at least one asset or liability account
0248       * in the current storage object. If not, it starts the new account wizard.
0249       */
0250     void createInitialAccount();
0251 
0252     /**
0253       * This method returns the last URL used or an empty URL
0254       * depending on the option setting if the last file should
0255       * be opened during startup or the open file dialog should
0256       * be displayed.
0257       *
0258       * @return URL of last opened file or empty if the program
0259       *         should start with the open file dialog
0260       */
0261     QUrl lastOpenedURL();
0262 
0263     /**
0264       * constructor of KMyMoneyApp, calls all init functions to create the application.
0265       */
0266     explicit KMyMoneyApp(QWidget* parent = 0);
0267 
0268     /**
0269       * Destructor
0270       */
0271     ~KMyMoneyApp();
0272 
0273     static void progressCallback(int current, int total, const QString&);
0274 
0275     void writeLastUsedDir(const QString& directory);
0276     QString readLastUsedDir() const;
0277     void writeLastUsedFile(const QString& fileName);
0278     QString readLastUsedFile() const;
0279 
0280     /**
0281       * Returns whether there is an importer available that can handle this file
0282       */
0283     bool isImportableFile(const QUrl &url);
0284 
0285     /**
0286       * This method returns a list of all 'other' dcop registered kmymoney processes.
0287       * It's a subset of the return of DCOPclient()->registeredApplications().
0288       *
0289       * @retval QStringList of process ids
0290       */
0291     QList<QString> instanceList() const;
0292 
0293 #ifdef KMM_DEBUG
0294     /**
0295       * Dump a list of the names of all defined KActions to stdout.
0296       */
0297     void dumpActions() const;
0298 #endif
0299 
0300     /**
0301       * Popup the context menu with the respective @p containerName.
0302       * Valid container names are defined in kmymoneyui.rc
0303       */
0304     void showContextMenu(const QString& containerName);
0305 
0306     void createAccount(MyMoneyAccount& newAccount, MyMoneyAccount& parentAccount, MyMoneyAccount& brokerageAccount, MyMoneyMoney openingBal);
0307 
0308     QString filename() const;
0309     QUrl filenameURL() const;
0310     void writeFilenameURL(const QUrl &url);
0311 
0312     void addToRecentFiles(const QUrl &url);
0313     QTimer* autosaveTimer();
0314 
0315     /**
0316       * Checks if the file with the @a url already exists. If so,
0317       * the user is asked if he/she wants to override the file.
0318       * If the user's answer is negative, @p false will be returned.
0319       * @p true will be returned in all other cases.
0320       */
0321     bool okToWriteFile(const QUrl &url);
0322 
0323     /**
0324      * Return pointer to the WebConnect object
0325      */
0326     WebConnect* webConnect() const;
0327 
0328     /**
0329      * Call this to find out if the currently open file is a sql database
0330      *
0331      * @retval true file is database
0332      * @retval false file is serial
0333      */
0334     bool isDatabase();
0335 
0336     /**
0337       * Call this to find out if the currently open file is native KMM
0338       *
0339       * @retval true file is native
0340       * @retval false file is foreign
0341       */
0342     bool isNativeFile();
0343 
0344     bool fileOpen() const;
0345 
0346     KMyMoneyAppCallback progressCallback();
0347 
0348     void consistencyCheck(bool alwaysDisplayResult);
0349 
0350 protected:
0351     /** save general Options like all bar positions and status as well as the geometry and the recent file list to the configuration
0352      * file
0353      */
0354     void saveOptions();
0355 
0356     /**
0357       * Creates the interfaces necessary for the plugins to work. Therefore,
0358       * this method must be called prior to loadPlugins().
0359       */
0360     void createInterfaces();
0361 
0362     /**
0363      * read general options again and initialize all variables like the recent file list
0364      */
0365     void readOptions();
0366 
0367     /**
0368      * Gets pointers for menus preset by KXMLGUIFactory
0369      * @return pointers for menus
0370      */
0371     QHash<eMenu::Menu, QMenu *> initMenus();
0372 
0373     /**
0374      * Initializes QActions (names, object names, icons, some connections, shortcuts)
0375      * @return pointers for actions
0376      */
0377     QHash<eMenu::Action, QAction *> initActions();
0378 
0379     /** initializes the dynamic menus (account selectors) */
0380     void initDynamicMenus();
0381 
0382     /**
0383      * sets up the statusbar for the main window by initializing a statuslabel.
0384      */
0385     void initStatusBar();
0386 
0387     /** sets up the application icons */
0388     void initIcons();
0389 
0390     /** queryClose is called by KMainWindow on each closeEvent of a window. Against the
0391      * default implementation (only returns true), this calls saveModified() on the document object to ask if the document shall
0392      * be saved if Modified; on cancel the closeEvent is rejected.
0393      * The settings are saved using saveOptions() if we are about to close.
0394      * @see KMainWindow#queryClose
0395      * @see QWidget#closeEvent
0396      */
0397     bool queryClose() final override;
0398 
0399 #ifdef KMM_DEBUG
0400     void resizeEvent(QResizeEvent*) final override;
0401 #endif
0402 
0403     void createSchedule(MyMoneySchedule newSchedule, MyMoneyAccount& newAccount);
0404 
0405     /**
0406       * This method preloads the holidays for the duration of the default forecast period
0407       */
0408     void preloadHolidays();
0409 
0410 public Q_SLOTS:
0411 
0412     void slotFileInfoDialog();
0413 
0414     bool isFileOpenedInAnotherInstance(const QUrl &url);
0415 
0416     void slotFileConsistencyCheck();
0417 
0418     /**
0419       * fires up the price table editor
0420       */
0421     void slotPriceDialog();
0422 
0423     /**
0424       * fires up the currency table editor
0425       */
0426     void slotCurrencyDialog();
0427 
0428     /**
0429       * dummy method needed just for initialization
0430       */
0431     void slotShowTransactionDetail();
0432 
0433     /**
0434       * Toggles the hide reconciled transactions setting
0435       */
0436     void slotHideReconciledTransactions();
0437 
0438     /**
0439       * Toggles the show reconciled balances setting
0440       */
0441     void slotShowReconciledBalances();
0442 
0443     /**
0444       * Toggles the hide unused categories setting
0445       */
0446     void slotHideUnusedCategories();
0447 
0448     /**
0449       * Toggles the show all accounts setting
0450       */
0451     void slotShowAllAccounts();
0452 
0453 
0454     /**
0455       * changes the statusbar contents for the standard label permanently,
0456       * used to indicate current actions. Returns the previous value for
0457       * 'stacked' usage.
0458       *
0459       * @param text the text that is displayed in the statusbar
0460       */
0461     QString slotStatusMsg(const QString &text);
0462 
0463     /**
0464       * This method changes the progress bar in the status line according
0465       * to the parameters @p current and @p total. The following special
0466       * cases exist:
0467       *
0468       * - current = -1 and total = -1  will reset the progress bar
0469       * - current = ?? and total != 0  will setup the 100% mark to @p total
0470       * - current = xx and total == 0  will set the percentage
0471       *
0472       * @param current the current value with respect to the initialised
0473       *                 100% mark
0474       * @param total the total value (100%)
0475       */
0476     void slotStatusProgressBar(int current, int total = 0);
0477 
0478     /**
0479       * Called to update stock and currency prices from the user menu
0480       */
0481     void slotEquityPriceUpdate();
0482 
0483     /**
0484       * This slot reparents account @p src to be a child of account @p dest
0485       *
0486       * @param src account to be reparented
0487       * @param dest new parent
0488       */
0489     void slotReparentAccount(const MyMoneyAccount& src, const MyMoneyAccount& dest);
0490 
0491     /**
0492       * This slot reparents account @p src to be a held at institution @p dest
0493       *
0494       * @param src account to be reparented
0495       * @param dest new parent institution
0496       */
0497     void slotReparentAccount(const MyMoneyAccount& src, const MyMoneyInstitution& dest);
0498 
0499     /**
0500       * Create a new investment in a given @p parent investment account
0501       */
0502     void slotInvestmentNew(MyMoneyAccount& account, const MyMoneyAccount& parent);
0503 
0504     /**
0505       * Brings up the new category editor and saves the information.
0506       * The dialog will be preset with the name and parent account.
0507       *
0508       * @param account reference of category to be created. The @p name member
0509       *                should be filled by the caller. The object will be filled
0510       *                with additional information during the creation process
0511       *                esp. the @p id member.
0512       * @param parent reference to parent account (defaults to none)
0513       */
0514     void slotCategoryNew(MyMoneyAccount& account, const MyMoneyAccount& parent);
0515     void slotCategoryNew(MyMoneyAccount& account);
0516 
0517     /**
0518       */
0519     void slotPayeeNew(const QString& newnameBase, QString& id);
0520 
0521     /**
0522       * This slot fires up the KCalc application
0523       */
0524     void slotToolsStartKCalc();
0525 
0526     /**
0527       * Brings up the new account wizard and saves the information.
0528       */
0529     void slotAccountNew(MyMoneyAccount&);
0530 
0531     void webConnect(const QString& sourceUrl, const QByteArray &asn_id);
0532     void webConnectUrl(const QUrl url);
0533 
0534 private:
0535     /**
0536       * This method sets the holidayRegion for use by the processing calendar.
0537       */
0538     void setHolidayRegion(const QString& holidayRegion);
0539 
0540     /**
0541       * Load the status bar with the 'ready' message. This is hold in a single
0542       * place, so that is consistent with isReady().
0543       */
0544     void ready();
0545 
0546     /**
0547       * Check if the status bar contains the 'ready' message. The return
0548       * value is used e.g. to detect if a quit operation is allowed or not.
0549       *
0550       * @retval true application is idle
0551       * @retval false application is active working on a longer operation
0552       */
0553     bool isReady();
0554 
0555     /**
0556       * Re-implemented from IMyMoneyProcessingCalendar
0557       */
0558     bool isProcessingDate(const QDate& date) const final override;
0559 
0560 Q_SIGNALS:
0561     /**
0562       * This signal is emitted when a new file is loaded. In the case file
0563       * is closed, this signal is also emitted with an empty url.
0564       */
0565     void fileLoaded(const QUrl &url);
0566 
0567     /**
0568       * This signal is emitted when a transaction/list of transactions has been selected by
0569       * the GUI. If no transaction is selected or the selection is removed,
0570       * @p transactions is identical to an empty QList. This signal is used
0571       * by plugins to get information about changes.
0572       */
0573     void transactionsSelected(const KMyMoneyRegister::SelectedTransactions& transactions);
0574 
0575     /**
0576      * This signal is emitted if a specific transaction with @a transactionId in account
0577      * @a accountId is selected
0578      */
0579     void transactionSelected(const QString accountId, const QString& transactionId);
0580 
0581     /**
0582       * This signal is sent out, when the user presses Ctrl+A or activates
0583       * the Select all transactions action.
0584       */
0585     void selectAllTransactions();
0586 
0587     /**
0588       * This signal is emitted when a new institution has been selected by
0589       * the GUI. If no institution is selected or the selection is removed,
0590       * @a institution is identical to MyMoneyInstitution(). This signal is used
0591       * by plugins to get information about changes.
0592       */
0593     void institutionSelected(const MyMoneyInstitution& institution);
0594 
0595     /**
0596       * This signal is emitted when a new schedule has been selected by
0597       * the GUI. If no schedule is selected or the selection is removed,
0598       * @a schedule is identical to MyMoneySchedule(). This signal is used
0599       * by plugins to get information about changes.
0600       */
0601     void scheduleSelected(const MyMoneySchedule& schedule);
0602 
0603     void startMatchTransaction(const MyMoneyTransaction& t);
0604     void cancelMatchTransaction();
0605 
0606     void kmmFilePlugin(unsigned int);
0607 
0608 public:
0609 
0610     bool isActionToggled(const eMenu::Action _a);
0611     static const QHash<eMenu::Action, QString> s_Actions;
0612 
0613 private:
0614     /// \internal d-pointer class.
0615     class Private;
0616     /*
0617      * Actually, one should write "Private * const d" but that confuses the KIDL
0618      * compiler in this context. It complains about the const keyword. So we leave
0619      * it out here
0620      */
0621     /// \internal d-pointer instance.
0622     Private* d;
0623 
0624 public Q_SLOTS:
0625     bool slotFileNew();
0626     void slotFileOpen();
0627     bool slotFileOpenRecent(const QUrl &url);
0628     bool slotFileSave();
0629     bool slotFileSaveAs();
0630     bool slotFileClose();
0631     /**
0632       * closes all open windows by calling close() on each memberList item
0633       * until the list is empty, then quits the application.
0634       * If queryClose() returns false because the user canceled the
0635       * saveModified() dialog, the closing breaks.
0636       */
0637     void slotFileQuit();
0638 };
0639 
0640 extern KMyMoneyApp *kmymoney;
0641 
0642 class KMStatus
0643 {
0644 public:
0645     explicit KMStatus(const QString &text);
0646     ~KMStatus();
0647 private:
0648     QString m_prevText;
0649 };
0650 
0651 #define KMSTATUS(msg) KMStatus _thisStatus(msg)
0652 
0653 #endif // KMYMONEY_H