Rabbit Remote Control 0.0.33
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 <QCheckBox>
9#include <QtGlobal>
10#include <QCoreApplication>
11#include "RabbitCommonTools.h"
12#include "PluginClient.h"
13
14#include "ParameterWakeOnLanUI.h"
15#include "ConnecterWakeOnLan.h"
16
17static Q_LOGGING_CATEGORY(log, "WakeOnLan.Connecter")
19 : CConnecterConnect(plugin)
20 , m_pView(nullptr)
21 , m_pModel(nullptr)
22 , m_pParameterClient(nullptr)
23{
24 qDebug(log) << Q_FUNC_INFO;
25}
26
27CConnecterWakeOnLan::~CConnecterWakeOnLan()
28{
29 qDebug(log) << Q_FUNC_INFO;
30}
31
33{
34 return 0;
35}
36
38{
39 int nRet = CConnecter::Initial();
40 if(nRet) return nRet;
41 qDebug(log) << Q_FUNC_INFO;
42 bool check = false;
43 CPluginClient* plugin = GetPlugClient();
44 m_pModel = new CWakeOnLanModel(this);
45 if(!m_pModel)
46 return -1;
47 m_pView = new CFrmWakeOnLan(m_pModel);
48 if(!m_pView) return -2;
49 m_pView->setWindowTitle(plugin->Name());
50 check = connect(m_pView, &CFrmWakeOnLan::customContextMenuRequested,
51 this, [&](const QPoint &pos){
52 m_Menu.exec(m_pView->mapToGlobal(pos));
53 });
54 Q_ASSERT(check);
55
56 m_Menu.addAction(QIcon::fromTheme("list-add"), tr("Add"),
57 this, SLOT(slotAdd()));
58 m_Menu.addAction(QIcon::fromTheme("document-edit"), tr("Edit"),
59 this, [&](){
60 QSharedPointer<CParameterWakeOnLan> para
61 = m_pModel->GetData(m_pView->GetCurrentIndex());
62 if(!para) {
63 QMessageBox::information(
64 nullptr,
65 tr("Information"),
66 tr("Please select a item"));
67 return;
68 }
70 dlg.SetParameter(para.data());
71 RC_SHOW_WINDOW(&dlg);
72 });
73 m_Menu.addAction(QIcon::fromTheme("list-remove"), tr("Remove"),
74 m_pView, SLOT(slotRemoveRow()));
75#if defined(Q_OS_UNIX)
76 QString szFlatpak_ID = qgetenv("FLATPAK_ID");
77 if(RabbitCommon::CTools::HasAdministratorPrivilege()
78 || "io.github.KangLin.RabbitRemoteControl" == szFlatpak_ID)
79 {
80 m_Menu.addAction(QIcon::fromTheme("view-refresh"), tr("Refresh"),
81 this, [&](){
82 foreach(auto p, m_pModel->m_Data)
83 m_Arp.GetMac(p);
84 });
85 m_Menu.addAction(
86 QIcon::fromTheme("mac"), tr("Get mac address"),
87 this, [&](){
88 if(!m_pModel || !m_pView)
89 return;
90 foreach(auto index, m_pView->GetSelect()) {
91 QSharedPointer<CParameterWakeOnLan> p
92 = m_pModel->GetData(index);
93 if(!p) continue;
94 if(m_Arp.GetMac(p))
95 p->SetHostState(CParameterWakeOnLan::HostState::GetMac);
96 }
97 });
98 }
99#else
100 m_Menu.addAction(QIcon::fromTheme("view-refresh"), tr("Refresh"),
101 this, [&](){
102 foreach(auto p, m_pModel->m_Data)
103 m_Arp.GetMac(p);
104 });
105 m_Menu.addAction(
106 QIcon::fromTheme("mac"), tr("Get mac address"),
107 this, [&](){
108 if(!m_pModel || !m_pView)
109 return;
110 foreach(auto index, m_pView->GetSelect()) {
111 QSharedPointer<CParameterWakeOnLan> p = m_pModel->GetData(index);
112 if(!p) continue;
113 if(m_Arp.GetMac(p))
114 p->SetHostState(CParameterWakeOnLan::HostState::GetMac);
115 }
116 });
117#endif
118 m_Menu.addAction(
119 QIcon::fromTheme("lan"), tr("Wake on lan"),
120 this, [&](){
121 if(!m_pModel || !m_pView)
122 return;
123 foreach(auto index, m_pView->GetSelect()) {
124 QSharedPointer<CParameterWakeOnLan> p = m_pModel->GetData(index);
125 if(!p) continue;
126 if(!m_Arp.WakeOnLan(p))
127 p->SetHostState(CParameterWakeOnLan::HostState::WakeOnLan);
128 }
129 });
130
131 return 0;
132}
133
135{
136 qDebug(log) << Q_FUNC_INFO;
137 if(m_pView)
138 delete m_pView;
139 if(m_pModel)
140 delete m_pModel;
141 return 0;
142}
143
145{
146 return m_pView;
147}
148
150{
151 return nullptr;
152}
153
155{
156 QString szId = Protocol() + "_" + GetPlugClient()->Name();
157 return szId;
158}
159
161{
162 QString szName;
163 if(GetParameter() && GetParameter()->GetParameterClient()
164 && GetParameter()->GetParameterClient()->GetShowProtocolPrefix())
165 szName = Protocol() + ":";
166 szName += GetPlugClient()->Name();
167 return szName;
168}
169
170int CConnecterWakeOnLan::Connect()
171{
172#if defined(Q_OS_UNIX)
173 if(!RabbitCommon::CTools::HasAdministratorPrivilege())
174 {
175 static bool bShow = false;
176 if(!bShow) {
177 bShow = true;
178 int nRet = 0;
179 QMessageBox msg(
180 QMessageBox::Warning, tr("Warning"),
181 tr("There are no administrator privileges, "
182 "and some functions(Get mac address) are restricted. "
183 "Please restart the program with administrative privileges."),
184 QMessageBox::Yes | QMessageBox::No);
185 msg.setCheckBox(new QCheckBox(tr("Exit the program"), &msg));
186 msg.checkBox()->setCheckable(true);
187 nRet = msg.exec();
188 if(QMessageBox::Yes == nRet) {
189 QString szExec = QCoreApplication::applicationFilePath();
190 QString szAppImage = QString::fromLocal8Bit(qgetenv("APPIMAGE"));
191 if(!szAppImage.isEmpty())
192 szExec = szAppImage;
193 if(!szExec.isEmpty()) {
194 bool bRet = RabbitCommon::CTools::ExecuteWithAdministratorPrivilege(szExec);
195 qDebug(log) << "Execute:" << bRet << QCoreApplication::applicationFilePath();
196 if(bRet && msg.checkBox()->isChecked()) {
197 QCoreApplication::quit();
198 }
199 }
200 }
201 }
202 }
203#endif
204 emit sigConnected();
205 return 0;
206}
207
208int CConnecterWakeOnLan::DisConnect()
209{
210 emit sigDisconnected();
211 return 0;
212}
213
215{
216 return nullptr;
217}
218
220{
221 m_pParameterClient = pPara;
222 return 0;
223}
224
225int CConnecterWakeOnLan::Load(QSettings &set)
226{
227 if(!m_pModel) return -1;
228 return m_pModel->Load(set, m_pParameterClient);
229}
230
231int CConnecterWakeOnLan::Save(QSettings &set)
232{
233 if(!m_pModel) return -1;
234 return m_pModel->Save(set);
235}
236
237void CConnecterWakeOnLan::slotAdd()
238{
239 QSharedPointer<CParameterWakeOnLan> para(new CParameterWakeOnLan());
240 para->SetParameterClient(m_pParameterClient);
242 dlg.SetParameter(para.data());
243 int nRet = RC_SHOW_WINDOW(&dlg);
244 if(QDialog::Accepted == nRet)
245 m_pModel->AddItem(para);
246}
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:61
virtual Q_INVOKABLE int Initial()
Initial parameters and resource.
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...