Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
Connect.cpp
1// Author: Kang Lin <kl222@126.com>
2#include <QTimer>
3#include <QLoggingCategory>
4
5#include "Connect.h"
6
7static Q_LOGGING_CATEGORY(log, "Client.Connect")
8
9CConnect::CConnect(CConnecter *pConnecter) : QObject()
10{
11 SetConnecter(pConnecter);
12}
13
14CConnect::~CConnect()
15{
16 qDebug(log) << "CConnect::~CConnect()";
17}
18
19int CConnect::SetConnecter(CConnecter* pConnecter)
20{
21 qDebug(log) << "CConnect::SetConnecter" << pConnecter;
22 if(!pConnecter) return -1;
23
24 bool check = false;
25 check = connect(this, SIGNAL(sigConnected()),
26 pConnecter, SIGNAL(sigConnected()));
27 Q_ASSERT(check);
28 check = connect(this, SIGNAL(sigDisconnect()),
29 pConnecter, SIGNAL(sigDisconnect()));
30 Q_ASSERT(check);
31 check = connect(this, SIGNAL(sigDisconnected()),
32 pConnecter, SIGNAL(sigDisconnected()));
33 Q_ASSERT(check);
34 check = connect(this, SIGNAL(sigError(const int, const QString&)),
35 pConnecter, SIGNAL(sigError(const int, const QString&)));
36 Q_ASSERT(check);
37 check = connect(this, SIGNAL(sigInformation(const QString&)),
38 pConnecter, SIGNAL(sigInformation(const QString&)));
39 Q_ASSERT(check);
40 check = connect(
41 this, SIGNAL(sigShowMessageBox(const QString&, const QString&,
42 const QMessageBox::Icon&)),
43 pConnecter, SIGNAL(sigShowMessageBox(const QString&, const QString&,
44 const QMessageBox::Icon&)));
45 Q_ASSERT(check);
46 check = connect(this, SIGNAL(sigBlockShowMessageBox(
47 const QString&, const QString&,
48 QMessageBox::StandardButtons,
49 QMessageBox::StandardButton&,
50 bool&, QString)),
51 pConnecter, SLOT(slotBlockShowMessageBox(
52 const QString&, const QString&,
53 QMessageBox::StandardButtons,
54 QMessageBox::StandardButton&,
55 bool&, QString)),
56 Qt::BlockingQueuedConnection);
57 Q_ASSERT(check);
58 check = connect(this, SIGNAL(sigBlockInputDialog(const QString&,
59 const QString&,
60 const QString&,
61 QString&)),
62 pConnecter, SLOT(slotBlockInputDialog(const QString&,
63 const QString&,
64 const QString&,
65 QString&)),
66 Qt::BlockingQueuedConnection);
67 Q_ASSERT(check);
68 check = connect(
69 this,
70 SIGNAL(sigBlockShowWidget(const QString&, int&, void*)),
71 pConnecter, SLOT(slotBlockShowWidget(const QString&, int&, void*)),
72 Qt::BlockingQueuedConnection);
73 Q_ASSERT(check);
74 return 0;
75}
76
78{
79 qDebug(log) << "CConnect::Connect()";
80 int nRet = 0;
81 nRet = static_cast<int>(OnInit());
82 if(nRet < 0) return nRet;
83
84 if(0 == nRet)
85 QTimer::singleShot(0, this, SLOT(slotTimeOut()));
86 return 0;
87}
88
90{
91 qDebug(log) << "CConnect::Disconnect()";
92 int nRet = 0;
93 nRet = OnClean();
94 return nRet;
95}
96
98{
99 //qDebug(log) << "CConnect::slotTimeOut()";
100 try {
101 // >= 0: continue. Call interval
102 // = -1: stop
103 // < -1: error
104 int nTime = OnProcess();
105 if(nTime >= 0)
106 {
107 QTimer::singleShot(nTime, this, SLOT(slotTimeOut()));
108 return;
109 }
110 qDebug(log) << "Process fail(< -1) or stop(= -1):" << nTime;
111 if(nTime < -1) {
112 qCritical(log) << "Process fail:" << nTime;
113 emit sigError(nTime, "Process fail or stop");
114 }
115 } catch(std::exception e) {
116 qCritical(log) << "Process fail:" << e.what();
117 emit sigError(-2, e.what());
118 } catch (...) {
119 qCritical(log) << "Process fail";
120 emit sigError(-3, "Process fail");
121 }
122
123 // Error or stop, must notify user disconnect it
124 emit sigDisconnect();
125}
126
128{
129 qWarning(log) << "Need to implement CConnect::OnProcess()";
130 return 0;
131}
132
Connect interface.
Definition Connect.h:45
void sigError(const int nError, const QString &szError=QString())
Triggered when an error is generated.
void sigInformation(const QString &szInfo)
Triggering from a background thread displays information in the main thread without blocking the back...
void sigBlockShowMessageBox(const QString &szTitle, const QString &szMessage, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton &nRet, bool &checkBox, QString checkBoxContext=QString())
Block background threads and display message dialogs in foreground threads (QMessageBox)
void sigShowMessageBox(const QString &szTitle, const QString &szMessage, const QMessageBox::Icon &icon=QMessageBox::Information)
Trigger the display of a message dialog (QMessageBox) in the main thread from a background thread wit...
void sigBlockInputDialog(const QString &szTitle, const QString &szLable, const QString &szMessage, QString &szText)
Block background threads and display input dialogs in foreground threads (QInputDialog)
void sigConnected()
Emitted when the plugin is successfully connected.
void sigDisconnect()
Notify the user to call disconnect.
virtual int Disconnect()
Disconnect.
Definition Connect.cpp:89
virtual int OnProcess()
Specific operation processing of plug-in connection.
Definition Connect.cpp:127
virtual OnInitReturnValue OnInit()=0
Specific plug-in realizes connection initialization.
virtual void slotTimeOut()
a non-Qt event loop (that is, normal loop processing), It call OnProcess(), and start timer.
Definition Connect.cpp:97
virtual int Connect()
Start connect.
Definition Connect.cpp:77
virtual int OnClean()=0
Clean.
void sigDisconnected()
Successful disconnection signal.
void sigBlockShowWidget(const QString &className, int &nRet, void *pContext)
Blocks the background thread and displays the window in the foreground thread.
Connecter interface.
Definition Connecter.h:62