1#include <QLoggingCategory>
3#include <QNetworkInterface>
5#include "WakeOnLanModel.h"
6#include "ParameterWakeOnLanUI.h"
8static Q_LOGGING_CATEGORY(log,
"WakeOnLan.Model")
10 : QAbstractTableModel{parent}
14CWakeOnLanModel::~CWakeOnLanModel()
16 qDebug(log) << Q_FUNC_INFO;
19int CWakeOnLanModel::rowCount(
const QModelIndex &parent)
const
24int CWakeOnLanModel::columnCount(
const QModelIndex &parent)
const
29QVariant CWakeOnLanModel::headerData(
int section, Qt::Orientation orientation,
int role)
const
32 if(Qt::Horizontal != orientation)
34 if(Qt::DisplayRole == role)
43 val = tr(
"Broadcast address");
45 val = tr(
"Network interface");
50 return QAbstractTableModel::headerData(section, orientation, role);
53Qt::ItemFlags CWakeOnLanModel::flags(
const QModelIndex &index)
const
56 return Qt::NoItemFlags;
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;
68QVariant CWakeOnLanModel::data(
const QModelIndex &index,
int role)
const
72 int c = index.column();
73 if(r >= m_Data.size() || c >= m_Colume)
76 auto para = m_Data.at(r);
78 if(Qt::BackgroundRole == role)
79 switch(para->GetHostState()) {
80 case CParameterWakeOnLan::HostState::Online:
81 val = QBrush(Qt::green);
83 case CParameterWakeOnLan::HostState::GetMac:
84 val = QBrush(Qt::yellow);
86 case CParameterWakeOnLan::HostState::WakeOnLan:
87 val = QBrush(Qt::blue);
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)
101 case CParameterWakeOnLan::HostState::Offline:
102 if(Qt::DecorationRole == role)
103 val = QIcon::fromTheme(
"network-offline");
104 if(Qt::DisplayRole == role)
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 ...");
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 ...");
122 if(Qt::DisplayRole == role)
125 val = para->m_Net.GetHost();
127 val = para->GetMac();
129 val = para->GetBroadcastAddress();
131 val = para->GetNetworkInterface();
133 val = para->GetPort();
139bool CWakeOnLanModel::setData(
const QModelIndex &index,
const QVariant &value,
int role)
141 qDebug(log) << Q_FUNC_INFO << index << value << role;
142 if(!index.isValid()) {
146 int c = index.column();
147 if(r >= m_Data.size() || c >= m_Colume)
149 if(value.isNull() || value.toString().isEmpty())
151 auto p = m_Data.at(r);
154 p->m_Net.SetHost(value.toString());
155 foreach(
auto iface, QNetworkInterface::allInterfaces()) {
157 auto entry = iface.addressEntries();
158 if(iface.flags() & QNetworkInterface::IsLoopBack)
160 if(!(iface.flags() & QNetworkInterface::CanBroadcast))
163 foreach(
auto e, entry) {
164 if(!e.broadcast().isNull()) {
165 if(CParameterWakeOnLanUI::GetSubNet(
167 e.netmask().toString())
168 == CParameterWakeOnLanUI::GetSubNet(
170 e.netmask().toString()))
172 p->SetNetworkInterface(e.ip().toString());
173 p->SetBroadcastAddress(e.broadcast().toString());
181 p->SetMac(value.toString());
183 p->SetBroadcastAddress(value.toString());
185 p->SetNetworkInterface(value.toString());
187 p->SetPort(value.toInt());
191int CWakeOnLanModel::AddItem(QSharedPointer<CParameterWakeOnLan> para)
195 QString szIp = para->m_Net.GetHost();
196 foreach(
auto p, m_Data)
198 if(p->m_Net.GetHost() == szIp){
199 qDebug(log) << szIp <<
"is existed";
203 bool check = connect(para.data(), SIGNAL(sigHostStateChanged()),
204 this, SLOT(slotHostStateChanged()));
207 m_Data.push_back(para);
212bool CWakeOnLanModel::removeRows(
int row,
int count,
const QModelIndex &parent)
214 qDebug(log) << Q_FUNC_INFO << row << count << parent;
216 m_Data.erase(m_Data.begin() + row);
224 CSortIp(Qt::SortOrder order,
int column)
230 bool operator()(
const QSharedPointer<CParameterWakeOnLan>& k1,
231 const QSharedPointer<CParameterWakeOnLan>& k2)
234 if(m_Order == Qt::AscendingOrder)
235 return k1->GetHostState() < k2->GetHostState();
237 return k1->GetHostState() > k2->GetHostState();
240 if(m_Order == Qt::AscendingOrder)
241 return k1->m_Net.GetHost() < k2->m_Net.GetHost();
243 return k1->m_Net.GetHost() > k2->m_Net.GetHost();
248 Qt::SortOrder m_Order;
252void CWakeOnLanModel::sort(
int column, Qt::SortOrder order)
254 qDebug(log) << Q_FUNC_INFO << column << order;
255 if(1 != column && 0 != column)
257 if(m_Sort.find(column) != m_Sort.end())
258 if(m_Sort[column] == order)
260 m_Sort[column] = order;
263 std::sort(m_Data.begin(), m_Data.end(), cmp);
271 count = set.value(
"Count").toInt();
272 for(
int i = 0; i < count; i++)
279 p->SetParameterClient(pClient);
280 set.beginGroup(
"Host" + QString::number(i));
290int CWakeOnLanModel::Save(QSettings &set)
294 set.setValue(
"Count", (
int)m_Data.size());
295 foreach(
auto d, m_Data)
297 set.beginGroup(
"Host" + QString::number(i++));
306void CWakeOnLanModel::slotHostStateChanged()
311 QString szIp = p->m_Net.GetHost();
312 for(
int i = 0; i < m_Data.size(); i++)
314 if(m_Data.at(i)->m_Net.GetHost() == szIp) {
315 emit dataChanged(index(i, 0), index(i, m_Colume));
321QSharedPointer<CParameterWakeOnLan> CWakeOnLanModel::GetData(
const QModelIndex &index)
323 if(index.row() < 0 || index.row() > m_Data.size())
325 return m_Data.at(index.row());
The parameters of client.
The wake on lan parameters.