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