Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions dcc-network/operation/netitemmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#include "netitemmodel.h"

#include "netitem.h"

namespace dde {
namespace network {
enum NetModelRole {
Expand Down Expand Up @@ -42,9 +40,31 @@ protected Q_SLOTS:
onAddObject(child);
}

void AboutToRemoveObject(const NetItem *, int pos) { beginRemoveRows(QModelIndex(), pos, pos); }
bool needRemoveNodeItem(const NetItem *childItem) const
{
if (childItem && (childItem->itemType() == NetType::NetItemType::SystemProxyControlItem
|| childItem->itemType() == NetType::NetItemType::AppProxyControlItem)) {
// 对于系统代理和应用代理(应用代理在V25上被裁减了,但是这里还是加上去这个判断,防止以后加上),
// 这里始终显示当前的节点
return false;
}
return true;
}

void removeObject(const NetItem *child) { endRemoveRows(); }
void AboutToRemoveObject(const NetItem *parentItem, int pos)
{
if (!parentItem)
return;

if (needRemoveNodeItem(parentItem->getChild(pos)))
beginRemoveRows(QModelIndex(), pos, pos);
}

void removeObject(const NetItem *child)
{
if (needRemoveNodeItem(child))
endRemoveRows();
}

protected:
NetItem *m_root;
Expand Down Expand Up @@ -195,10 +215,32 @@ QHash<int, QByteArray> NetItemModel::roleNames() const
return names;
}

bool NetItemModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
QModelIndex childIndex = sourceModel()->index(source_row, 0, source_parent);
NetItem *curItem = static_cast<NetItem *>(childIndex.internalPointer());
if (!curItem) {
return false;
}
// 有的节点出现了两次,所以这里让显示第一次出现的那个节点
if (m_rowMap.contains(curItem->itemType())) {
return (m_rowMap[curItem->itemType()] == source_row);
}

m_rowMap[curItem->itemType()] = source_row;
return true;
}

bool NetItemModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
{
NetItem *leftItem = static_cast<NetItem *>(source_left.internalPointer());
if (!leftItem)
return false;

NetItem *rightItem = static_cast<NetItem *>(source_right.internalPointer());
if (!rightItem)
Comment on lines 236 to +241
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Handling null pointers in lessThan by returning false can violate strict weak ordering.

Returning false when either pointer is null means that for a null vs non-null pair, both lessThan(left, right) and lessThan(right, left) can be false, which violates strict weak ordering required by QSortFilterProxyModel and can cause unstable sorting. Consider defining a consistent ordering for nulls (e.g., always less or always greater than non-null) so comparisons remain deterministic.

return false;

if (leftItem->itemType() != rightItem->itemType())
return leftItem->itemType() < rightItem->itemType();
switch (leftItem->itemType()) {
Expand Down
6 changes: 6 additions & 0 deletions dcc-network/operation/netitemmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
#ifndef NETITEMMODEL_H
#define NETITEMMODEL_H

#include <QSortFilterProxyModel>

Check warning on line 7 in dcc-network/operation/netitemmodel.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QSortFilterProxyModel> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include "netitem.h"

Check warning on line 9 in dcc-network/operation/netitemmodel.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "netitem.h" not found.

namespace dde {
namespace network {
class NetItem;
Expand All @@ -27,7 +29,11 @@
void rootChanged(NetItem *root);

protected:
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override;

private:
mutable QMap<NetType::NetItemType, int> m_rowMap;
};
} // namespace network
} // namespace dde
Expand Down
4 changes: 2 additions & 2 deletions dcc-network/qml/PageSystemProxy.qml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ DccObject {
id: devCheck
D.Switch {
checked: netItem.isEnabled
enabled: netItem.enabledable
enabled: true
onClicked: {
dccData.exec(netItem.isEnabled ? NetManager.DisabledDevice : NetManager.EnabledDevice, netItem.id, {})
}
Expand All @@ -70,7 +70,7 @@ DccObject {
pageType: DccObject.Editor
page: D.Switch {
checked: root.method !== NetType.None
enabled: netItem.enabledable
enabled: true
onClicked: {
if (checked) {
root.method = netItem.lastMethod
Expand Down
Loading