Rabbit Remote Control 0.1.0-bate10
Loading...
Searching...
No Matches
Backend.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include <QTimer>
4#include <QLoggingCategory>
5
6#include "Backend.h"
7
8static Q_LOGGING_CATEGORY(log, "Operate")
9
10CBackend::CBackend(COperate *pOperate, bool bStopSignal)
11 : QObject() // Because it's in a different thread with pOperate
12 , m_bStopSignal(bStopSignal)
13{
14 qDebug(log) << Q_FUNC_INFO;
15 SetConnect(pOperate);
16}
17
18CBackend::~CBackend()
19{
20 qDebug(log) << Q_FUNC_INFO;
21}
22
23int CBackend::SetConnect(COperate *pOperate)
24{
25 qDebug(log) << Q_FUNC_INFO;
26 if(!pOperate) return -1;
27
28 bool check = false;
29 check = connect(this, SIGNAL(sigRunning()),
30 pOperate, SIGNAL(sigRunning()));
31 Q_ASSERT(check);
32 check = connect(this, SIGNAL(sigFinished()),
33 pOperate, SIGNAL(sigFinished()));
34 Q_ASSERT(check);
35 check = connect(this, SIGNAL(sigStop()),
36 pOperate, SIGNAL(sigStop()));
37 Q_ASSERT(check);
38 check = connect(this, SIGNAL(sigError(const int, const QString&)),
39 pOperate, SIGNAL(sigError(const int, const QString&)));
40 Q_ASSERT(check);
41 check = connect(this, SIGNAL(sigInformation(const QString&)),
42 pOperate, SIGNAL(sigInformation(const QString&)));
43 Q_ASSERT(check);
44 check = connect(
45 this, SIGNAL(sigShowMessageBox(const QString&, const QString&,
46 const QMessageBox::Icon&)),
47 pOperate, SIGNAL(sigShowMessageBox(const QString&, const QString&,
48 const QMessageBox::Icon&)));
49 Q_ASSERT(check);
50 check = connect(this, SIGNAL(sigBlockShowMessageBox(
51 const QString&, const QString&,
52 QMessageBox::StandardButtons,
53 QMessageBox::StandardButton&,
54 bool&, QString)),
55 pOperate, SLOT(slotBlockShowMessageBox(
56 const QString&, const QString&,
57 QMessageBox::StandardButtons,
58 QMessageBox::StandardButton&,
59 bool&, QString)),
60 Qt::BlockingQueuedConnection);
61 Q_ASSERT(check);
62 check = connect(this, SIGNAL(sigBlockInputDialog(const QString&,
63 const QString&,
64 const QString&,
65 QString&)),
66 pOperate, SLOT(slotBlockInputDialog(const QString&,
67 const QString&,
68 const QString&,
69 QString&)),
70 Qt::BlockingQueuedConnection);
71 Q_ASSERT(check);
72 check = connect(
73 this,
74 SIGNAL(sigBlockShowWidget(const QString&, int&, void*)),
75 pOperate, SLOT(slotBlockShowWidget(const QString&, int&, void*)),
76 Qt::BlockingQueuedConnection);
77 Q_ASSERT(check);
78 check = connect(this, SIGNAL(sigSecurityLevel(CSecurityLevel::Levels)),
79 pOperate, SLOT(slotSetSecurityLevel(CSecurityLevel::Levels)));
80 Q_ASSERT(check);
81 return 0;
82}
83
85{
86 qDebug(log) << Q_FUNC_INFO;
87 OnInitReturnValue nRet = OnInitReturnValue::Fail;
88 nRet = OnInit();
89 switch(nRet) {
90 case OnInitReturnValue::Fail:
91 return -1;
92 case OnInitReturnValue::Success:
93 QTimer::singleShot(0, this, SLOT(slotTimeOut()));
94 break;
95 case OnInitReturnValue::NotUseOnProcess:
96 break;
97 default:
98 qWarning(log) << "Unknown return value:" << (int)nRet;
99 }
100 return 0;
101}
102
104{
105 qDebug(log) << Q_FUNC_INFO;
106 int nRet = 0;
107 nRet = OnClean();
108 return nRet;
109}
110
112{
113 return 0;
114}
115
117{
118 //qDebug(log) << "CConnect::slotTimeOut()";
119 try {
120 // >= 0: continue. Call interval
121 // = -1: stop
122 // < -1: error
123 int nTime = OnProcess();
124 if(nTime >= 0)
125 {
126 QTimer::singleShot(nTime, this, SLOT(slotTimeOut()));
127 return;
128 }
129 qDebug(log) << "Process fail(< -1) or stop(= -1):" << nTime;
130 if(nTime < -1) {
131 qCritical(log) << "Process fail:" << nTime;
132 emit sigError(nTime, "Process fail or stop");
133 }
134 } catch(std::exception e) {
135 qCritical(log) << "Process fail:" << e.what();
136 emit sigError(-2, e.what());
137 } catch (...) {
138 qCritical(log) << "Process fail";
139 emit sigError(-3, "Process fail");
140 }
141
142 if(m_bStopSignal) {
143 // Error or stop, must notify user disconnect it
144 emit sigStop();
145 }
146}
147
149{
150 qWarning(log) << "Need to implement CConnect::OnProcess()";
151 return 0;
152}
Backend interface.
Definition Backend.h:42
void sigInformation(const QString &szInfo)
Triggering from a background thread displays information in the main thread without blocking the back...
virtual int Stop()
Stop.
Definition Backend.cpp:103
virtual int WakeUp()
Wake up backend thread(background thread).
Definition Backend.cpp:111
virtual int OnProcess()
Specific operation processing of plug-in.
Definition Backend.cpp:148
void sigStop()
Notify the user to stop.
virtual OnInitReturnValue OnInit()=0
Initialization.
virtual int OnClean()=0
Clean.
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...
virtual int Start()
Start.
Definition Backend.cpp:84
void sigError(const int nError, const QString &szError=QString())
Triggered when an error is generated.
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 sigRunning()
Emitted when the plugin is successfully started.
void sigFinished()
Successful stopped signal.
virtual void slotTimeOut()
a non-Qt event loop (that is, normal loop processing), It call OnProcess(), and start timer.
Definition Backend.cpp:116
void sigSecurityLevel(CSecurityLevel::Levels level)
Triggered when the security level changes.
void sigBlockShowWidget(const QString &className, int &nRet, void *pContext)
Blocks the background thread and displays the window in the foreground thread.
bool m_bStopSignal
When an error occurs, emit a COperate::sigStop() signal.
Definition Backend.h:288
void sigBlockInputDialog(const QString &szTitle, const QString &szLable, const QString &szMessage, QString &szText)
Block background threads and display input dialogs in foreground threads (QInputDialog)
Operate interface.
Definition Operate.h:51