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