Rabbit Remote Control 0.0.37
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)
11 : QObject() // Because it's in a different thread with pOperate
12{
13 qDebug(log) << Q_FUNC_INFO;
14 SetConnect(pOperate);
15}
16
17CBackend::~CBackend()
18{
19 qDebug(log) << Q_FUNC_INFO;
20}
21
22int CBackend::SetConnect(COperate *pOperate)
23{
24 qDebug(log) << Q_FUNC_INFO;
25 if(!pOperate) return -1;
26
27 bool check = false;
28 check = connect(this, SIGNAL(sigRunning()),
29 pOperate, SIGNAL(sigRunning()));
30 Q_ASSERT(check);
31 check = connect(this, SIGNAL(sigFinished()),
32 pOperate, SIGNAL(sigFinished()));
33 Q_ASSERT(check);
34 check = connect(this, SIGNAL(sigStop()),
35 pOperate, SIGNAL(sigStop()));
36 Q_ASSERT(check);
37 check = connect(this, SIGNAL(sigError(const int, const QString&)),
38 pOperate, SIGNAL(sigError(const int, const QString&)));
39 Q_ASSERT(check);
40 check = connect(this, SIGNAL(sigInformation(const QString&)),
41 pOperate, SIGNAL(sigInformation(const QString&)));
42 Q_ASSERT(check);
43 check = connect(
44 this, SIGNAL(sigShowMessageBox(const QString&, const QString&,
45 const QMessageBox::Icon&)),
46 pOperate, SIGNAL(sigShowMessageBox(const QString&, const QString&,
47 const QMessageBox::Icon&)));
48 Q_ASSERT(check);
49 check = connect(this, SIGNAL(sigBlockShowMessageBox(
50 const QString&, const QString&,
51 QMessageBox::StandardButtons,
52 QMessageBox::StandardButton&,
53 bool&, QString)),
54 pOperate, SLOT(slotBlockShowMessageBox(
55 const QString&, const QString&,
56 QMessageBox::StandardButtons,
57 QMessageBox::StandardButton&,
58 bool&, QString)),
59 Qt::BlockingQueuedConnection);
60 Q_ASSERT(check);
61 check = connect(this, SIGNAL(sigBlockInputDialog(const QString&,
62 const QString&,
63 const QString&,
64 QString&)),
65 pOperate, SLOT(slotBlockInputDialog(const QString&,
66 const QString&,
67 const QString&,
68 QString&)),
69 Qt::BlockingQueuedConnection);
70 Q_ASSERT(check);
71 check = connect(
72 this,
73 SIGNAL(sigBlockShowWidget(const QString&, int&, void*)),
74 pOperate, SLOT(slotBlockShowWidget(const QString&, int&, void*)),
75 Qt::BlockingQueuedConnection);
76 Q_ASSERT(check);
77 return 0;
78}
79
81{
82 qDebug(log) << Q_FUNC_INFO;
83 int nRet = 0;
84 nRet = static_cast<int>(OnInit());
85 if(nRet < 0) return nRet;
86
87 if(0 == nRet)
88 QTimer::singleShot(0, this, SLOT(slotTimeOut()));
89 return 0;
90}
91
93{
94 qDebug(log) << Q_FUNC_INFO;
95 int nRet = 0;
96 nRet = OnClean();
97 return nRet;
98}
99
101{
102 return 0;
103}
104
106{
107 //qDebug(log) << "CConnect::slotTimeOut()";
108 try {
109 // >= 0: continue. Call interval
110 // = -1: stop
111 // < -1: error
112 int nTime = OnProcess();
113 if(nTime >= 0)
114 {
115 QTimer::singleShot(nTime, this, SLOT(slotTimeOut()));
116 return;
117 }
118 qDebug(log) << "Process fail(< -1) or stop(= -1):" << nTime;
119 if(nTime < -1) {
120 qCritical(log) << "Process fail:" << nTime;
121 emit sigError(nTime, "Process fail or stop");
122 }
123 } catch(std::exception e) {
124 qCritical(log) << "Process fail:" << e.what();
125 emit sigError(-2, e.what());
126 } catch (...) {
127 qCritical(log) << "Process fail";
128 emit sigError(-3, "Process fail");
129 }
130
131 // Error or stop, must notify user disconnect it
132 emit sigStop();
133}
134
136{
137 qWarning(log) << "Need to implement CConnect::OnProcess()";
138 return 0;
139}
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:92
virtual int WakeUp()
Wake up.
Definition Backend.cpp:100
virtual int OnProcess()
Specific operation processing of plug-in.
Definition Backend.cpp:135
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:80
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:105
void sigBlockShowWidget(const QString &className, int &nRet, void *pContext)
Blocks the background thread and displays the window in the foreground thread.
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:50