Skip to content

Fix: Remote servers cannot be effectively grouped.#517

Merged
lzwind merged 2 commits intolinuxdeepin:masterfrom
JWWTSL:416
Apr 16, 2026
Merged

Fix: Remote servers cannot be effectively grouped.#517
lzwind merged 2 commits intolinuxdeepin:masterfrom
JWWTSL:416

Conversation

@JWWTSL
Copy link
Copy Markdown
Contributor

@JWWTSL JWWTSL commented Apr 16, 2026

Log: When adding a group, only when servers are selected will a group record be created. If no servers are selected, the code block is not executed, causing the group to not be added to m_serverConfigs.

PMS: bug-357213

Log: When adding a group, only when servers are selected will a group record be created. If no servers are selected, the code block is not executed, causing the group to not be added to m_serverConfigs.

PMS: bug-357213
@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: JWWTSL, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@JWWTSL
Copy link
Copy Markdown
Contributor Author

JWWTSL commented Apr 16, 2026

/forcemerge

@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

这段代码修复了一个关于分组配置的BUG(编号355415),目的是确保即使没有选中任何服务器,分组也会被添加到配置中。下面是对这段代码的详细审查意见,包括语法逻辑、代码质量、代码性能和代码安全方面的建议。

1. 语法逻辑审查

当前逻辑分析:

  • 代码获取了 m_groupNameEdit 中的文本,并去除首尾空格。
  • 检查 serverConfigs 中是否已存在该分组名。
  • 如果不存在,则创建一个空的 QList<ServerConfig *> 并赋值给该分组名。

潜在问题:

  • 变量名冲突:在构造函数 GroupConfigOptDlg::GroupConfigOptDlg(const QString &groupName, QWidget *parent) 中,参数名为 groupName,而在修复代码中又声明了一个局部变量 QString groupName。这会遮蔽构造函数的参数 groupName,可能导致逻辑错误(如果后续代码依赖于构造函数参数的话)。
  • 逻辑顺序:这段代码放在 accept() 之前,意味着它是在点击确定按钮后立即执行的。但在此之前,代码中已经有一个 if 块处理了服务器配置的更新。需要确认这段新代码是否应该放在那个 if 块之前或之后,或者是否应该合并到现有的逻辑中。

改进建议:

  • 修改局部变量名以避免遮蔽构造函数参数,例如改为 QString newGroupName
  • 如果构造函数参数 groupName 是用于初始化分组的,那么应该检查是否需要更新它,或者是否应该直接使用构造函数参数而不是从 m_groupNameEdit 重新获取。

2. 代码质量审查

优点:

  • 使用了 trimmed() 方法去除首尾空格,这是一个好习惯,可以避免因用户输入空格导致的配置问题。
  • 使用了 contains() 方法检查键是否存在,这是 QMap 的标准用法,代码清晰易读。

改进建议:

  • 代码复用:如果 m_groupNameEdit->text().trimmed() 在其他地方也被使用,可以考虑将其提取为一个局部变量或成员函数,以避免重复代码。
  • 注释:虽然已有注释说明修复的BUG编号,但可以进一步说明为什么需要这个修复(例如:"确保空分组也能被保存")。

3. 代码性能审查

当前性能分析:

  • QMap::contains()QMap::operator[] 的时间复杂度都是 O(log n),对于大多数应用场景来说性能足够。
  • 创建一个空的 QList<ServerConfig *> 是轻量级操作,不会显著影响性能。

改进建议:

  • 如果 serverConfigs 非常大(例如包含数千个分组),可以考虑使用 QHash 替代 QMap,因为 QHash 的查找和插入操作平均时间复杂度为 O(1),比 QMap 的 O(log n) 更快。但需要权衡内存占用和键的排序需求。

4. 代码安全审查

潜在问题:

  • 空字符串分组名:如果 m_groupNameEdit 中的文本为空(或仅包含空格),trimmed() 会返回空字符串。这可能导致创建一个名为空字符串的分组,这可能不是预期的行为。应该检查 groupName 是否为空,如果是,则提示用户或取消操作。
  • 内存管理serverConfigs 存储的是 QList<ServerConfig *>,即指针列表。需要确保 ServerConfig 对象的生命周期管理正确,避免内存泄漏或悬空指针。虽然这段代码只是创建一个空列表,但整体设计需要考虑这一点。

改进建议:

  • 添加对空分组名的检查:
    QString newGroupName = m_groupNameEdit->text().trimmed();
    if (newGroupName.isEmpty()) {
        // 提示用户或取消操作
        return;
    }
  • 如果 ServerConfig 对象的生命周期由 ServerConfigManager 管理,确保在删除或修改配置时正确处理指针。

5. 改进后的代码示例

// 修复BUG 355415: 确保即使没有选中任何服务器,分组也会被添加到配置中
QString newGroupName = m_groupNameEdit->text().trimmed();
if (newGroupName.isEmpty()) {
    // 提示用户分组名不能为空
    DDialog dialog(this);
    dialog.setMessage("分组名不能为空");
    dialog.exec();
    return;
}

QMap<QString, QList<ServerConfig *>> &serverConfigs = ServerConfigManager::instance()->getServerConfigs();
if (!serverConfigs.contains(newGroupName)) {
    serverConfigs[newGroupName] = QList<ServerConfig *>();
}

accept();

总结

这段代码修复了一个重要的BUG,但可以通过以下改进使其更加健壮和安全:

  1. 避免变量名遮蔽。
  2. 添加对空分组名的检查。
  3. 考虑代码复用和性能优化(如使用 QHash)。
  4. 确保内存管理正确。

这些改进将提高代码的可读性、健壮性和性能。

@lzwind lzwind merged commit b6b0983 into linuxdeepin:master Apr 16, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants