1 /*
2  * Copyright 2004       Martin Preuss aquamaniac@users.sourceforge.net
3  * Copyright 2009       Cristian Onet onet.cristian@gmail.com
4  * Copyright 2010-2019  Thomas Baumgart tbaumgart@kde.org
5  * Copyright 2016       Christian David christian-david@web.de
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 #ifndef KBANKING_H
21 #define KBANKING_H
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config-kmymoney.h>
25 #endif
26 
27 // ----------------------------------------------------------------------------
28 // QT Includes
29 
30 #include <QSet>
31 
32 // ----------------------------------------------------------------------------
33 // KDE & Library Includes
34 
35 class KAction;
36 class QBanking;
37 class KBankingExt;
38 class KBAccountSettings;
39 
40 #include <aqbanking/banking.h>
41 
42 // ----------------------------------------------------------------------------
43 // Project Includes
44 
45 #include "kmymoneyplugin.h"
46 #include "onlinepluginextended.h"
47 #include "mymoneyaccount.h"
48 #include "mymoneykeyvaluecontainer.h"
49 
50 #include "mymoney/onlinejobtyped.h"
51 #include "onlinetasks/sepa/sepaonlinetransfer.h"
52 
53 #include "banking.hpp"
54 
55 /**
56   * This class represents the KBanking plugin towards KMymoney.
57   * All GUI related issues are handled in this object.
58   */
59 class MyMoneyStatement;
60 class KBanking : public KMyMoneyPlugin::OnlinePluginExtended
61 {
62   friend class KBankingExt;
63 
64   Q_OBJECT
65   Q_INTERFACES(KMyMoneyPlugin::OnlinePluginExtended
66                KMyMoneyPlugin::OnlinePlugin)
67 
68 public:
69   explicit KBanking(QObject *parent, const QVariantList &args);
70   ~KBanking() override;
71 
72   bool importStatement(const MyMoneyStatement& s);
73 
74   MyMoneyAccount account(const QString& key, const QString& value) const;
75 
76   void setAccountOnlineParameters(const MyMoneyAccount& acc, const MyMoneyKeyValueContainer& kvps) const;
77 
78   void protocols(QStringList& protocolList) const override;
79 
80   QStringList availableJobs(QString accountId) override;
81   IonlineTaskSettings::ptr settings(QString accountId, QString taskName) override;
82 
83   void sendOnlineJob(QList<onlineJob>& jobs) override;
84 
85   void plug() override;
86   void unplug() override;
87 
88 private:
89   /**
90     * creates the action objects available through the application menus
91     */
92   void createActions();
93 
94   /**
95     * creates the context menu
96     */
97   void createContextMenu();
98 
99   /**
100     * checks whether a given KMyMoney account with id @p id is
101     * already mapped or not.
102     *
103     * @param acc reference to KMyMoney account object
104     * @retval false account is not mapped to an AqBanking account
105     * @retval true account is mapped to an AqBanking account
106     */
107   bool accountIsMapped(const MyMoneyAccount& acc);
108 
109   /**
110    * sets up the reference string consisting out of BLZ and account number
111    * in the KMyMoney object so that we can find it later on when importing data.
112    */
113   void setupAccountReference(const MyMoneyAccount& acc, AB_ACCOUNT_SPEC* ab_acc);
114 
115   /**
116    * Returns the value of the parameter @a s with all leading 0's stripped.
117    */
118   QString stripLeadingZeroes(const QString& s) const;
119 
120   /**
121    * Prefills the protocol conversion list to allow mapping
122    * of AqBanking internal names to external names
123    */
124   void loadProtocolConversion();
125 
126   /**
127    * Creates an additional tab widget for the account edit dialog
128    * to represent the necessary parameters for online banking
129    * through AqBanking.
130    */
131   QWidget* accountConfigTab(const MyMoneyAccount& acc, QString& name) override;
132 
133   /**
134    * Stores the configuration data kept in the widgets created
135    * in accountConfigTab() and returns them in a key value container
136    * The current settings are accessible through the reference to
137    * @a current.
138    */
139   MyMoneyKeyValueContainer onlineBankingSettings(const MyMoneyKeyValueContainer& current) override;
140 
141   /**
142     * Called by the application to map the KMyMoney account @a acc
143     * to an AqBanking account. Calls KBanking to set up AqBanking mappings.
144     * Returns the necessary settings for the plugin in @a settings and
145     * @a true if the mapping was successful.
146     */
147   bool mapAccount(const MyMoneyAccount& acc, MyMoneyKeyValueContainer& settings) override;
148 
149   /**
150    * This method translates a MyMoneyAccount to the corresponding AB_ACCOUNT object pointer.
151    * If no mapped account can be detected, it returns 0.
152    */
153   AB_ACCOUNT_SPEC* aqbAccount(const MyMoneyAccount& acc) const;
154 
155   /**
156    * This is a convenient method for aqbAccount if you have KMyMoney's account id only.
157    */
158   AB_ACCOUNT_SPEC* aqbAccount(const QString& accountId) const;
159 
160   /**
161     * Called by the application framework to update the
162     * KMyMoney account @a acc with data from the online source.
163     * Store the jobs in the outbox in case @a moreAccounts is true
164     */
165   bool updateAccount(const MyMoneyAccount& acc, bool moreAccounts) override;
166 
167   /**
168     * Kept for backward compatibility. Use
169     * updateAccount(const MyMoneyAccount& acc, bool moreAccounts) instead.
170     *
171     * @deprecated
172     */
173   bool updateAccount(const MyMoneyAccount& acc) DEPRECATED;
174 
175   /**
176     * Trigger the password cache timer
177     */
178   void startPasswordTimer();
179 
180   bool enqueTransaction(onlineJobTyped<sepaOnlineTransfer>& job);
181 
182 
183 protected Q_SLOTS:
184   void slotSettings();
185   void slotImport();
186   void slotClearPasswordCache();
187   void executeQueue();
188 
189 Q_SIGNALS:
190   void queueChanged();
191 
192 private:
193   class Private;
194   Private* const d;
195   KAction*                m_configAction;
196   KAction*                m_importAction;
197   KBankingExt*            m_kbanking;
198   QMap<QString, QString>  m_protocolConversionMap;
199   KBAccountSettings*      m_accountSettings;
200   int                     m_statementCount;
201   /**
202    * @brief @ref onlineJob "onlineJobs" which are executed at the moment
203    * Key is onlineJob->id(). This container is used during execution of jobs.
204    */
205   QMap<QString, onlineJob> m_onlineJobQueue;
206 };
207 
208 /**
209   * This class is the special implementation to glue the AB_Banking class
210   * with the KMyMoneyPlugin structure.
211   */
212 class KBankingExt : public AB_Banking
213 {
214   friend class KBanking;
215 
216 public:
217   explicit KBankingExt(KBanking* parent, const char* appname, const char* fname = 0);
~KBankingExt()218   virtual ~KBankingExt() {}
219 
220   int executeQueue(AB_IMEXPORTER_CONTEXT *ctx);
221 
222   int enqueueJob(AB_TRANSACTION *j);
223   int dequeueJob(AB_TRANSACTION *j);
224   std::list<AB_TRANSACTION*> getEnqueuedJobs();
225   void transfer();
226 
227   virtual bool interactiveImport();
228 
229 protected:
230   int init() final override;
231   int fini() final override;
232 
233   bool askMapAccount(const MyMoneyAccount& acc);
234   QString mappingId(const MyMoneyObject& object) const;
235 
236   bool importAccountInfo(AB_IMEXPORTER_CONTEXT *ctx, AB_IMEXPORTER_ACCOUNTINFO *ai, uint32_t flags) final override;
237   void _slToStatement(MyMoneyStatement &ks,
238                       const MyMoneyAccount&,
239                       const AB_SECURITY *sy);
240   void _xaToStatement(MyMoneyStatement &ks,
241                       const MyMoneyAccount&,
242                       const AB_TRANSACTION *t);
243   void clearPasswordCache();
244 
245 private:
246   KBanking* m_parent;
247   QMap<QString, bool> m_hashMap;
248   AB_TRANSACTION_LIST2 *_jobQueue;
249   QSet<QString>   m_sepaKeywords;
250 };
251 
252 #endif // KBANKING
253