1#include <QLoggingCategory>
3#include <QNetworkInterface>
5#include "WakeOnLanModel.h"
6#include "ParameterWakeOnLanUI.h"
8static Q_LOGGING_CATEGORY(log,
"WakeOnLan.Model")
10 : QAbstractTableModel{parent}
13CWakeOnLanModel::~CWakeOnLanModel()
15 qDebug(log) << Q_FUNC_INFO;
18int CWakeOnLanModel::rowCount(
const QModelIndex &parent)
const
23int CWakeOnLanModel::columnCount(
const QModelIndex &parent)
const
25 return (
int)ColumeValue::End;
28QVariant CWakeOnLanModel::headerData(
int section, Qt::Orientation orientation,
int role)
const
31 if(Qt::Horizontal != orientation)
33 if(Qt::DisplayRole == role)
42 val = tr(
"Broadcast address");
44 val = tr(
"Network interface");
49 return QAbstractTableModel::headerData(section, orientation, role);
52Qt::ItemFlags CWakeOnLanModel::flags(
const QModelIndex &index)
const
55 return Qt::NoItemFlags;
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;
67QVariant CWakeOnLanModel::data(
const QModelIndex &index,
int role)
const
71 ColumeValue c = (ColumeValue)index.column();
72 if(r >= m_Data.size() || c >= ColumeValue::End)
75 auto para = m_Data.at(r);
77 if(Qt::BackgroundRole == role)
78 switch(para->GetHostState()) {
79 case CParameterWakeOnLan::HostState::Online:
80 val = QBrush(Qt::green);
82 case CParameterWakeOnLan::HostState::GetMac:
83 val = QBrush(Qt::yellow);
85 case CParameterWakeOnLan::HostState::WakeOnLan:
86 val = QBrush(Qt::blue);
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)
100 case CParameterWakeOnLan::HostState::Offline:
101 if(Qt::DecorationRole == role)
102 val = QIcon::fromTheme(
"network-offline");
103 if(Qt::DisplayRole == role)
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 ...");
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 ...");
121 if(Qt::DisplayRole == role)
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();
138bool CWakeOnLanModel::setData(
const QModelIndex &index,
const QVariant &value,
int role)
140 qDebug(log) << Q_FUNC_INFO << index << value << role;
141 if(!index.isValid()) {
145 ColumeValue c = (ColumeValue)index.column();
146 if(r >= m_Data.size() || c >= ColumeValue::End)
148 if(value.isNull() || value.toString().isEmpty())
150 auto p = m_Data.at(r);
151 if(ColumeValue::Ip == c)
153 p->m_Net.SetHost(value.toString());
154 foreach(
auto iface, QNetworkInterface::allInterfaces()) {
156 auto entry = iface.addressEntries();
157 if(iface.flags() & QNetworkInterface::IsLoopBack)
159 if(!(iface.flags() & QNetworkInterface::CanBroadcast))
162 foreach(
auto e, entry) {
163 if(!e.broadcast().isNull()) {
164 if(CParameterWakeOnLanUI::GetSubNet(
166 e.netmask().toString())
167 == CParameterWakeOnLanUI::GetSubNet(
169 e.netmask().toString()))
171 p->SetNetworkInterface(e.ip().toString());
172 p->SetBroadcastAddress(e.broadcast().toString());
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());
190int CWakeOnLanModel::AddItem(QSharedPointer<CParameterWakeOnLan> para)
194 QString szIp = para->m_Net.GetHost();
195 foreach(
auto p, m_Data)
197 if(p->m_Net.GetHost() == szIp){
198 qDebug(log) << szIp <<
"is existed";
202 bool check = connect(para.data(), SIGNAL(sigHostStateChanged()),
203 this, SLOT(slotHostStateChanged()));
206 m_Data.push_back(para);
211bool CWakeOnLanModel::removeRows(
int row,
int count,
const QModelIndex &parent)
213 qDebug(log) << Q_FUNC_INFO << row << count << parent;
215 m_Data.erase(m_Data.begin() + row);
223 CSortIp(Qt::SortOrder order,
int column)
229 bool operator()(
const QSharedPointer<CParameterWakeOnLan>& k1,
230 const QSharedPointer<CParameterWakeOnLan>& k2)
233 if(m_Order == Qt::AscendingOrder)
234 return k1->GetHostState() < k2->GetHostState();
236 return k1->GetHostState() > k2->GetHostState();
239 if(m_Order == Qt::AscendingOrder)
240 return k1->m_Net.GetHost() < k2->m_Net.GetHost();
242 return k1->m_Net.GetHost() > k2->m_Net.GetHost();
247 Qt::SortOrder m_Order;
251void CWakeOnLanModel::sort(
int column, Qt::SortOrder order)
253 qDebug(log) << Q_FUNC_INFO << column << order;
254 if(1 != column && 0 != column)
256 if(m_Sort.find(column) != m_Sort.end())
257 if(m_Sort[column] == order)
259 m_Sort[column] = order;
262 std::sort(m_Data.begin(), m_Data.end(), cmp);
270 count = set.value(
"Count").toInt();
271 for(
int i = 0; i < count; i++)
278 p->SetGlobalParameters(pPlugin);
279 set.beginGroup(
"Host" + QString::number(i));
289int CWakeOnLanModel::Save(QSettings &set)
293 set.setValue(
"Count", (
int)m_Data.size());
294 foreach(
auto d, m_Data)
296 set.beginGroup(
"Host" + QString::number(i++));
305void CWakeOnLanModel::slotHostStateChanged()
310 QString szIp = p->m_Net.GetHost();
311 for(
int i = 0; i < m_Data.size(); i++)
313 if(m_Data.at(i)->m_Net.GetHost() == szIp) {
314 emit dataChanged(index(i, 0), index(i, (
int)ColumeValue::End - 1));
320QSharedPointer<CParameterWakeOnLan> CWakeOnLanModel::GetData(
const QModelIndex &index)
322 if(index.row() < 0 || index.row() > m_Data.size())
324 return m_Data.at(index.row());
Global parameters of plugins.
The wake on lan parameters.