Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
WakeOnLanModel.cpp
1#include <QLoggingCategory>
2#include <QIcon>
3#include <QNetworkInterface>
4
5#include "WakeOnLanModel.h"
6#include "ParameterWakeOnLanUI.h"
7
8static Q_LOGGING_CATEGORY(log, "WakeOnLan.Model")
9CWakeOnLanModel::CWakeOnLanModel(QObject *parent)
10 : QAbstractTableModel{parent}
11 , m_Colume(6)
12{}
13
14CWakeOnLanModel::~CWakeOnLanModel()
15{
16 qDebug(log) << Q_FUNC_INFO;
17}
18
19int CWakeOnLanModel::rowCount(const QModelIndex &parent) const
20{
21 return m_Data.size();
22}
23
24int CWakeOnLanModel::columnCount(const QModelIndex &parent) const
25{
26 return m_Colume;
27}
28
29QVariant CWakeOnLanModel::headerData(int section, Qt::Orientation orientation, int role) const
30{
31 QVariant val;
32 if(Qt::Horizontal != orientation)
33 return val;
34 if(Qt::DisplayRole == role)
35 {
36 if(0 == section)
37 val = tr("State");
38 else if(1 == section)
39 val = tr("IP");
40 else if(2 == section)
41 val = tr("MAC");
42 else if(3 == section)
43 val = tr("Broadcast address");
44 else if(4 == section)
45 val = tr("Network interface");
46 else if(5 == section)
47 val = tr("Port");
48 return val;
49 }
50 return QAbstractTableModel::headerData(section, orientation, role);
51}
52
53Qt::ItemFlags CWakeOnLanModel::flags(const QModelIndex &index) const
54{
55 if(!index.isValid())
56 return Qt::NoItemFlags;
57 int r = index.row();
58 int c = index.column();
59 if(r >= m_Data.size() || c >= m_Colume)
60 return Qt::NoItemFlags;
61 Qt::ItemFlags f = QAbstractTableModel::flags(index);
62 if(1 == c || 2 == c) {
63 return Qt::ItemIsEditable | f;
64 }
65 return f;
66}
67
68QVariant CWakeOnLanModel::data(const QModelIndex &index, int role) const
69{
70 QVariant val;
71 int r = index.row();
72 int c = index.column();
73 if(r >= m_Data.size() || c >= m_Colume)
74 return val;
75
76 auto para = m_Data.at(r);
77
78 if(Qt::BackgroundRole == role)
79 switch(para->GetHostState()) {
80 case CParameterWakeOnLan::HostState::Online:
81 val = QBrush(Qt::green);
82 break;
83 case CParameterWakeOnLan::HostState::GetMac:
84 val = QBrush(Qt::yellow);
85 break;
86 case CParameterWakeOnLan::HostState::WakeOnLan:
87 val = QBrush(Qt::blue);
88 break;
89 default:
90 break;
91 }
92
93 if(0 == c) {
94 switch(para->GetHostState()) {
95 case CParameterWakeOnLan::HostState::Online:
96 if(Qt::DecorationRole == role)
97 val = QIcon::fromTheme("network-online");
98 if(Qt::DisplayRole == role)
99 val = tr("Online");
100 break;
101 case CParameterWakeOnLan::HostState::Offline:
102 if(Qt::DecorationRole == role)
103 val = QIcon::fromTheme("network-offline");
104 if(Qt::DisplayRole == role)
105 val = tr("Offline");
106 break;
107 case CParameterWakeOnLan::HostState::GetMac:
108 if(Qt::DecorationRole == role)
109 val = QIcon::fromTheme("mac");
110 if(Qt::DisplayRole == role)
111 val = tr("Get mac ...");
112 break;
113 case CParameterWakeOnLan::HostState::WakeOnLan:
114 if(Qt::DecorationRole == role)
115 val = QIcon::fromTheme("lan");
116 if(Qt::DisplayRole == role)
117 val = tr("Wake on lan ...");
118 default:
119 break;
120 }
121 } else {
122 if(Qt::DisplayRole == role)
123 {
124 if(1 == c)
125 val = para->m_Net.GetHost();
126 else if(2 == c)
127 val = para->GetMac();
128 else if(3 == c)
129 val = para->GetBroadcastAddress();
130 else if(4 == c)
131 val = para->GetNetworkInterface();
132 else if(5 == c)
133 val = para->GetPort();
134 }
135 }
136 return val;
137}
138
139bool CWakeOnLanModel::setData(const QModelIndex &index, const QVariant &value, int role)
140{
141 qDebug(log) << Q_FUNC_INFO << index << value << role;
142 if(!index.isValid()) {
143 return false;
144 }
145 int r = index.row();
146 int c = index.column();
147 if(r >= m_Data.size() || c >= m_Colume)
148 return false;
149 if(value.isNull() || value.toString().isEmpty())
150 return false;
151 auto p = m_Data.at(r);
152 if(1 == c)
153 {
154 p->m_Net.SetHost(value.toString());
155 foreach(auto iface, QNetworkInterface::allInterfaces()) {
156 //qDebug(log) << iface;
157 auto entry = iface.addressEntries();
158 if(iface.flags() & QNetworkInterface::IsLoopBack)
159 continue;
160 if(!(iface.flags() & QNetworkInterface::CanBroadcast))
161 continue;
162 QString szBroadcast;
163 foreach(auto e, entry) {
164 if(!e.broadcast().isNull()) {
165 if(CParameterWakeOnLanUI::GetSubNet(
166 p->m_Net.GetHost(),
167 e.netmask().toString())
168 == CParameterWakeOnLanUI::GetSubNet(
169 e.ip().toString(),
170 e.netmask().toString()))
171 {
172 p->SetNetworkInterface(e.ip().toString());
173 p->SetBroadcastAddress(e.broadcast().toString());
174 break;
175 }
176 }
177 }
178 }
179 }
180 else if(2 == c)
181 p->SetMac(value.toString());
182 else if(3 == c)
183 p->SetBroadcastAddress(value.toString());
184 else if(4 == c)
185 p->SetNetworkInterface(value.toString());
186 else if(5 == c)
187 p->SetPort(value.toInt());
188 return true;
189}
190
191int CWakeOnLanModel::AddItem(QSharedPointer<CParameterWakeOnLan> para)
192{
193 if(!para)
194 return -1;
195 QString szIp = para->m_Net.GetHost();
196 foreach(auto p, m_Data)
197 {
198 if(p->m_Net.GetHost() == szIp){
199 qDebug(log) << szIp << "is existed";
200 return 0;
201 }
202 }
203 bool check = connect(para.data(), SIGNAL(sigHostStateChanged()),
204 this, SLOT(slotHostStateChanged()));
205 Q_ASSERT(check);
206 beginResetModel();
207 m_Data.push_back(para);
208 endResetModel();
209 return 0;
210}
211
212bool CWakeOnLanModel::removeRows(int row, int count, const QModelIndex &parent)
213{
214 qDebug(log) << Q_FUNC_INFO << row << count << parent;
215 beginResetModel();
216 m_Data.erase(m_Data.begin() + row);
217 endResetModel();
218 return true;
219}
220
222{
223public:
224 CSortIp(Qt::SortOrder order, int column)
225 {
226 m_Order = order;
227 m_column = column;
228 }
229
230 bool operator()(const QSharedPointer<CParameterWakeOnLan>& k1,
231 const QSharedPointer<CParameterWakeOnLan>& k2)
232 {
233 if(0 == m_column) {
234 if(m_Order == Qt::AscendingOrder)
235 return k1->GetHostState() < k2->GetHostState();
236 else
237 return k1->GetHostState() > k2->GetHostState();
238 }
239 if(1 == m_column) {
240 if(m_Order == Qt::AscendingOrder)
241 return k1->m_Net.GetHost() < k2->m_Net.GetHost();
242 else
243 return k1->m_Net.GetHost() > k2->m_Net.GetHost();
244 }
245 return false;
246 }
247private:
248 Qt::SortOrder m_Order;
249 int m_column;
250};
251
252void CWakeOnLanModel::sort(int column, Qt::SortOrder order)
253{
254 qDebug(log) << Q_FUNC_INFO << column << order;
255 if(1 != column && 0 != column)
256 return;
257 if(m_Sort.find(column) != m_Sort.end())
258 if(m_Sort[column] == order)
259 return;
260 m_Sort[column] = order;
261 beginResetModel();
262 CSortIp cmp(order, column);
263 std::sort(m_Data.begin(), m_Data.end(), cmp);
264 endResetModel();
265}
266
267int CWakeOnLanModel::Load(QSettings &set, CParameterClient* pClient)
268{
269 int nRet = 0;
270 int count = 0;
271 count = set.value("Count").toInt();
272 for(int i = 0; i < count; i++)
273 {
274 QSharedPointer<CParameterWakeOnLan> p(new CParameterWakeOnLan());
275 if(!p) {
276 nRet = -1;
277 break;
278 }
279 p->SetParameterClient(pClient);
280 set.beginGroup("Host" + QString::number(i));
281 nRet = p->Load(set);
282 set.endGroup();
283 if(nRet)
284 break;
285 AddItem(p);
286 }
287 return nRet;
288}
289
290int CWakeOnLanModel::Save(QSettings &set)
291{
292 int nRet = 0;
293 int i = 0;
294 set.setValue("Count", (int)m_Data.size());
295 foreach(auto d, m_Data)
296 {
297 set.beginGroup("Host" + QString::number(i++));
298 nRet = d->Save(set);
299 set.endGroup();
300 if(nRet)
301 break;
302 }
303 return nRet;
304}
305
306void CWakeOnLanModel::slotHostStateChanged()
307{
308 CParameterWakeOnLan* p = qobject_cast<CParameterWakeOnLan*>(sender());
309 if(!p)
310 return;
311 QString szIp = p->m_Net.GetHost();
312 for(int i = 0; i < m_Data.size(); i++)
313 {
314 if(m_Data.at(i)->m_Net.GetHost() == szIp) {
315 emit dataChanged(index(i, 0), index(i, m_Colume));
316 break;
317 }
318 }
319}
320
321QSharedPointer<CParameterWakeOnLan> CWakeOnLanModel::GetData(const QModelIndex &index)
322{
323 if(index.row() < 0 || index.row() > m_Data.size())
324 return nullptr;
325 return m_Data.at(index.row());
326}
The parameters of client.
The wake on lan parameters.