Rabbit Remote Control 0.1.0-bate8
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 int nRet = 0;
88 nRet = static_cast<int>(OnInit());
89 if(nRet < 0) return nRet;
90
91 if(0 == nRet)
92 QTimer::singleShot(0, this, SLOT(slotTimeOut()));
93 return 0;
94}
95
97{
98 qDebug(log) << Q_FUNC_INFO;
99 int nRet = 0;
100 nRet = OnClean();
101 return nRet;
102}
103
105{
106 return 0;
107}
108
110{
111 //qDebug(log) << "CConnect::slotTimeOut()";
112 try {
113 // >= 0: continue. Call interval
114 // = -1: stop
115 // < -1: error
116 int nTime = OnProcess();
117 if(nTime >= 0)
118 {
119 QTimer::singleShot(nTime, this, SLOT(slotTimeOut()));
120 return;
121 }
122 qDebug(log) << "Process fail(< -1) or stop(= -1):" << nTime;
123 if(nTime < -1) {
124 qCritical(log) << "Process fail:" << nTime;
125 emit sigError(nTime, "Process fail or stop");
126 }
127 } catch(std::exception e) {
128 qCritical(log) << "Process fail:" << e.what();
129 emit sigError(-2, e.what());
130 } catch (...) {
131 qCritical(log) << "Process fail";
132 emit sigError(-3, "Process fail");
133 }
134
135 if(m_bStopSignal) {
136 // Error or stop, must notify user disconnect it
137 emit sigStop();
138 }
139}
140
142{
143 qWarning(log) << "Need to implement CConnect::OnProcess()";
144 return 0;
145}
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:96
virtual int WakeUp()
Wake up.
Definition Backend.cpp:104
virtual int OnProcess()
Specific operation processing of plug-in.
Definition Backend.cpp:141
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:109
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:283
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