Rabbit Remote Control 0.1.0-de
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{}
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 foreach(auto p, m_Data)
196 {
197 if(p->m_Net.GetHost() == szIp){
198 qDebug(log) << szIp << "is existed";
199 return 0;
200 }
201 }
202 bool check = connect(para.data(), SIGNAL(sigHostStateChanged()),
203 this, SLOT(slotHostStateChanged()));
204 Q_ASSERT(check);
205 beginResetModel();
206 m_Data.push_back(para);
207 endResetModel();
208 return 0;
209}
210
211bool CWakeOnLanModel::removeRows(int row, int count, const QModelIndex &parent)
212{
213 qDebug(log) << Q_FUNC_INFO << row << count << parent;
214 beginResetModel();
215 m_Data.erase(m_Data.begin() + row);
216 endResetModel();
217 return true;
218}
219
221{
222public:
223 CSortIp(Qt::SortOrder order, int column)
224 {
225 m_Order = order;
226 m_column = column;
227 }
228
229 bool operator()(const QSharedPointer<CParameterWakeOnLan>& k1,
230 const QSharedPointer<CParameterWakeOnLan>& k2)
231 {
232 if(0 == m_column) {
233 if(m_Order == Qt::AscendingOrder)
234 return k1->GetHostState() < k2->GetHostState();
235 else
236 return k1->GetHostState() > k2->GetHostState();
237 }
238 if(1 == m_column) {
239 if(m_Order == Qt::AscendingOrder)
240 return k1->m_Net.GetHost() < k2->m_Net.GetHost();
241 else
242 return k1->m_Net.GetHost() > k2->m_Net.GetHost();
243 }
244 return false;
245 }
246private:
247 Qt::SortOrder m_Order;
248 int m_column;
249};
250
251void CWakeOnLanModel::sort(int column, Qt::SortOrder order)
252{
253 qDebug(log) << Q_FUNC_INFO << column << order;
254 if(1 != column && 0 != column)
255 return;
256 if(m_Sort.find(column) != m_Sort.end())
257 if(m_Sort[column] == order)
258 return;
259 m_Sort[column] = order;
260 beginResetModel();
261 CSortIp cmp(order, column);
262 std::sort(m_Data.begin(), m_Data.end(), cmp);
263 endResetModel();
264}
265
266int CWakeOnLanModel::Load(QSettings &set, CParameterPlugin* pPlugin)
267{
268 int nRet = 0;
269 int count = 0;
270 count = set.value("Count").toInt();
271 for(int i = 0; i < count; i++)
272 {
273 QSharedPointer<CParameterWakeOnLan> p(new CParameterWakeOnLan());
274 if(!p) {
275 nRet = -1;
276 break;
277 }
278 p->SetGlobalParameters(pPlugin);
279 set.beginGroup("Host" + QString::number(i));
280 nRet = p->Load(set);
281 set.endGroup();
282 if(nRet)
283 break;
284 AddItem(p);
285 }
286 return nRet;
287}
288
289int CWakeOnLanModel::Save(QSettings &set)
290{
291 int nRet = 0;
292 int i = 0;
293 set.setValue("Count", (int)m_Data.size());
294 foreach(auto d, m_Data)
295 {
296 set.beginGroup("Host" + QString::number(i++));
297 nRet = d->Save(set);
298 set.endGroup();
299 if(nRet)
300 break;
301 }
302 return nRet;
303}
304
305void CWakeOnLanModel::slotHostStateChanged()
306{
307 CParameterWakeOnLan* p = qobject_cast<CParameterWakeOnLan*>(sender());
308 if(!p)
309 return;
310 QString szIp = p->m_Net.GetHost();
311 for(int i = 0; i < m_Data.size(); i++)
312 {
313 if(m_Data.at(i)->m_Net.GetHost() == szIp) {
314 emit dataChanged(index(i, 0), index(i, (int)ColumeValue::End - 1));
315 break;
316 }
317 }
318}
319
320QSharedPointer<CParameterWakeOnLan> CWakeOnLanModel::GetData(const QModelIndex &index)
321{
322 if(index.row() < 0 || index.row() > m_Data.size())
323 return nullptr;
324 return m_Data.at(index.row());
325}
Global parameters of plugins.
The wake on lan parameters.