Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
ConnecterWakeOnLan.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include <QLoggingCategory>
4#include <QHBoxLayout>
5#include <QVBoxLayout>
6#include <QRegularExpression>
7#include <QPushButton>
8#include <QtGlobal>
9#include <QCoreApplication>
10#include "RabbitCommonTools.h"
11#include "PluginClient.h"
12
13#include "ParameterWakeOnLanUI.h"
14#include "ConnecterWakeOnLan.h"
15
16static Q_LOGGING_CATEGORY(log, "WakeOnLan.Connecter")
18 : CConnecterConnect(plugin)
19 , m_pView(nullptr)
20 , m_pModel(nullptr)
21 , m_pParameterClient(nullptr)
22{
23 qDebug(log) << Q_FUNC_INFO;
24}
25
26CConnecterWakeOnLan::~CConnecterWakeOnLan()
27{
28 qDebug(log) << Q_FUNC_INFO;
29}
30
32{
33 return 0;
34}
35
37{
38 qDebug(log) << Q_FUNC_INFO;
39 bool check = false;
40 CPluginClient* plugin = GetPlugClient();
41 m_pModel = new CWakeOnLanModel(this);
42 if(!m_pModel)
43 return -1;
44 m_pView = new CFrmWakeOnLan(m_pModel);
45 if(!m_pView) return -2;
46 m_pView->setWindowTitle(plugin->Name());
47 check = connect(m_pView, &CFrmWakeOnLan::customContextMenuRequested,
48 this, [&](const QPoint &pos){
49 m_Menu.exec(m_pView->mapToGlobal(pos));
50 });
51 Q_ASSERT(check);
52
53 m_Menu.addAction(QIcon::fromTheme("list-add"), tr("Add"),
54 this, SLOT(slotAdd()));
55 m_Menu.addAction(QIcon::fromTheme("document-edit"), tr("Edit"),
56 this, [&](){
57 QSharedPointer<CParameterWakeOnLan> para
58 = m_pModel->GetData(m_pView->GetCurrentIndex());
59 if(!para) {
60 QMessageBox::information(
61 nullptr,
62 tr("Information"),
63 tr("Please select a item"));
64 return;
65 }
67 dlg.SetParameter(para.data());
68 RC_SHOW_WINDOW(&dlg);
69 });
70 m_Menu.addAction(QIcon::fromTheme("list-remove"), tr("Remove"),
71 m_pView, SLOT(slotRemoveRow()));
72#if defined(Q_OS_UNIX)
73 if(RabbitCommon::CTools::HasAdministratorPrivilege())
74 {
75 m_Menu.addAction(QIcon::fromTheme("view-refresh"), tr("Refresh"),
76 this, [&](){
77 foreach(auto p, m_pModel->m_Data)
78 m_Arp.GetMac(p);
79 });
80 m_Menu.addAction(
81 QIcon::fromTheme("mac"), tr("Get mac address"),
82 this, [&](){
83 if(!m_pModel || !m_pView)
84 return;
85 foreach(auto index, m_pView->GetSelect()) {
86 QSharedPointer<CParameterWakeOnLan> p
87 = m_pModel->GetData(index);
88 if(!p) continue;
89 if(m_Arp.GetMac(p))
90 p->SetHostState(CParameterWakeOnLan::HostState::GetMac);
91 }
92 });
93 }
94#else
95 m_Menu.addAction(QIcon::fromTheme("view-refresh"), tr("Refresh"),
96 this, [&](){
97 foreach(auto p, m_pModel->m_Data)
98 m_Arp.GetMac(p);
99 });
100 m_Menu.addAction(
101 QIcon::fromTheme("mac"), tr("Get mac address"),
102 this, [&](){
103 if(!m_pModel || !m_pView)
104 return;
105 foreach(auto index, m_pView->GetSelect()) {
106 QSharedPointer<CParameterWakeOnLan> p = m_pModel->GetData(index);
107 if(!p) continue;
108 if(m_Arp.GetMac(p))
109 p->SetHostState(CParameterWakeOnLan::HostState::GetMac);
110 }
111 });
112#endif
113 m_Menu.addAction(
114 QIcon::fromTheme("lan"), tr("Wake on lan"),
115 this, [&](){
116 if(!m_pModel || !m_pView)
117 return;
118 foreach(auto index, m_pView->GetSelect()) {
119 QSharedPointer<CParameterWakeOnLan> p = m_pModel->GetData(index);
120 if(!p) continue;
121 if(!m_Arp.WakeOnLan(p))
122 p->SetHostState(CParameterWakeOnLan::HostState::WakeOnLan);
123 }
124 });
125
126 return 0;
127}
128
130{
131 qDebug(log) << Q_FUNC_INFO;
132 if(m_pView)
133 delete m_pView;
134 if(m_pModel)
135 delete m_pModel;
136 return 0;
137}
138
140{
141 return m_pView;
142}
143
145{
146 return nullptr;
147}
148
150{
151 QString szId = Protocol() + "_" + GetPlugClient()->Name();
152 return szId;
153}
154
156{
157 QString szName;
158 if(GetParameter() && GetParameter()->GetParameterClient()
159 && GetParameter()->GetParameterClient()->GetShowProtocolPrefix())
160 szName = Protocol() + ":";
161 szName += GetPlugClient()->Name();
162 return szName;
163}
164
165int CConnecterWakeOnLan::Connect()
166{
167#if defined(Q_OS_UNIX)
168 if(!RabbitCommon::CTools::HasAdministratorPrivilege())
169 {
170 static bool bShow = false;
171 if(!bShow) {
172 bShow = true;
173 int nRet = QMessageBox::warning(
174 nullptr, tr("Warning"),
175 tr("There are no administrator privileges, "
176 "and some functions(Get mac address) are restricted. "
177 "Please restart the program with administrative privileges."),
178 QMessageBox::Yes | QMessageBox::No);
179 if(QMessageBox::Yes == nRet) {
180 bool bRet = RabbitCommon::CTools::executeByRoot(
181 QCoreApplication::applicationFilePath());
182 if(bRet)
183 QCoreApplication::quit();
184 }
185 }
186 }
187#endif
188 emit sigConnected();
189 return 0;
190}
191
192int CConnecterWakeOnLan::DisConnect()
193{
194 emit sigDisconnected();
195 return 0;
196}
197
199{
200 return nullptr;
201}
202
204{
205 m_pParameterClient = pPara;
206 return 0;
207}
208
209int CConnecterWakeOnLan::Load(QSettings &set)
210{
211 if(!m_pModel) return -1;
212 return m_pModel->Load(set, m_pParameterClient);
213}
214
215int CConnecterWakeOnLan::Save(QSettings &set)
216{
217 if(!m_pModel) return -1;
218 return m_pModel->Save(set);
219}
220
221void CConnecterWakeOnLan::slotAdd()
222{
223 QSharedPointer<CParameterWakeOnLan> para(new CParameterWakeOnLan());
224 para->SetParameterClient(m_pParameterClient);
226 dlg.SetParameter(para.data());
227 int nRet = RC_SHOW_WINDOW(&dlg);
228 if(QDialog::Accepted == nRet)
229 m_pModel->AddItem(para);
230}
Connect interface.
Definition Connect.h:45
The connector interface of the plug-in, which is only used by the plug-in.
virtual CParameterBase * GetParameter()
Get parameter.
virtual CConnect * InstanceConnect() override
New connect.
virtual QDialog * OnOpenDialogSettings(QWidget *parent) override
Open settgins dialog.
virtual int Clean() override
Clean parameters and resource.
virtual const QString Name() override
Display order:
virtual int Save(QSettings &set) override
Save parameters.
virtual int Load(QSettings &set) override
Load parameters.
virtual QWidget * GetViewer() override
Get Viewer.
virtual int Initial() override
Initial parameters and resource.
virtual const QString Id() override
Identity.
virtual int SetParameterClient(CParameterClient *pPara) override
Set CParameterClient.
virtual qint16 Version() override
Version.
void sigConnected()
Successful connection signal.
void sigDisconnected()
Successful disconnection signal.
virtual const QString Protocol() const
Protocol.
Definition Connecter.cpp:69
The parameters of client.
The wake on lan parameters UI.
int SetParameter(CParameter *pParameter)
Set the parameters and initialize the user interface.
The wake on lan parameters.
The plugin interface.
virtual const QString Name() const =0
This name must be the same as the project name (${PROJECT_NAME}). The translation file (${PROJECT_NAM...