CuteHMI - Shared Database (CuteHMI.SharedDatabase.0)
DatabaseConnectionHandler.hpp
1 #ifndef H_EXTENSIONS_CUTEHMI_SHAREDDATABASE_0_INCLUDE_CUTEHMI_SHAREDDATABASE_INTERNAL_DATABASECONNECTIONHANDLER_HPP
2 #define H_EXTENSIONS_CUTEHMI_SHAREDDATABASE_0_INCLUDE_CUTEHMI_SHAREDDATABASE_INTERNAL_DATABASECONNECTIONHANDLER_HPP
3 
4 #include "common.hpp"
5 #include "DatabaseConfig.hpp"
6 
7 #include <cutehmi/InplaceError.hpp>
8 
9 #include <QObject>
10 #include <QBasicTimer>
11 #include <QSqlDatabase>
12 
13 
14 namespace cutehmi {
15 namespace shareddatabase {
16 namespace internal {
17 
18 class CUTEHMI_SHAREDDATABASE_PRIVATE DatabaseConnectionHandler:
19  public QObject
20 {
21  Q_OBJECT
22 
23  typedef QObject Parent;
24 
25  public:
26  static constexpr int INITIAL_MONITOR_INTERVAL = 1000;
27 
28  static constexpr int INITIAL_MAINTENANCE_INTERVAL = 1000 * 60 * 60 * 12;
29 
30  DatabaseConnectionHandler(DatabaseConfig config, QObject * parent = nullptr);
31 
32  public slots:
33  void connect();
34 
35  void disconnect();
36 
37  signals:
38  void connected(QString connectionName);
39 
40  void disconnected(QString connectionName);
41 
42  void errored(cutehmi::InplaceError error);
43 
44  protected:
45  void timerEvent(QTimerEvent * event) override;
46 
47  private slots:
48  void printError(cutehmi::InplaceError error);
49 
50  private:
51  struct Members
52  {
53  int monitorInterval;
54  int maintenanceCount;
55  DatabaseConfig config;
56  QBasicTimer monitorTimer;
57  QSqlDatabase db;
58  QString connectionName;
59 
60  Members(DatabaseConfig p_config):
61  monitorInterval(INITIAL_MONITOR_INTERVAL),
62  maintenanceCount(0),
63  config(p_config)
64  {
65  }
66  };
67 
68  MPtr<Members> m;
69 };
70 
71 }
72 }
73 }
74 
75 #endif
76 
77 //(c)C: Copyright © 2020, Michał Policht <michal@policht.pl>. All rights reserved.
78 //(c)C: This file is a part of CuteHMI.
79 //(c)C: CuteHMI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
80 //(c)C: CuteHMI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
81 //(c)C: You should have received a copy of the GNU Lesser General Public License along with CuteHMI. If not, see <https://www.gnu.org/licenses/>.
QTimerEvent
QBasicTimer
cutehmi::shareddatabase::internal::DatabaseConfig
Definition: DatabaseConfig.hpp:12
QSqlDatabase
cutehmi::InplaceError
cutehmi::MPtr< Members >
QObject
cutehmi
QString
cutehmi::shareddatabase::internal::DatabaseConnectionHandler
Definition: DatabaseConnectionHandler.hpp:18
std::internal
T internal(T... args)