Skip to content

feat(main): Improve command-line save path handling with full file pa…#791

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/snipefrom
dengzhongyuan365-dev:commit20260122
Jan 22, 2026
Merged

feat(main): Improve command-line save path handling with full file pa…#791
deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/snipefrom
dengzhongyuan365-dev:commit20260122

Conversation

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member

…th support

  • Reorder command-line argument processing to handle save-path before screenshot mode options
  • Add parsePathArgument() method to distinguish between directory paths and complete file paths
  • Support full file path specification with format detection (PNG, JPEG, BMP)
  • Add m_shotWithFullPath flag to track whether user specified a complete file path
  • Extract and store file name and format from full path arguments
  • Update saveAction() to use specified file path directly when provided via command-line
  • Improve logging with qCInfo for better debugging of path handling logic
  • Prevent default screenshot from launching when save-path option is set
  • Ensure directory creation before saving when using command-line specified paths
  • Refactor savePath() method to handle both directory and full file path modes

…th support

- Reorder command-line argument processing to handle save-path before screenshot mode options
- Add parsePathArgument() method to distinguish between directory paths and complete file paths
- Support full file path specification with format detection (PNG, JPEG, BMP)
- Add m_shotWithFullPath flag to track whether user specified a complete file path
- Extract and store file name and format from full path arguments
- Update saveAction() to use specified file path directly when provided via command-line
- Improve logging with qCInfo for better debugging of path handling logic
- Prevent default screenshot from launching when save-path option is set
- Ensure directory creation before saving when using command-line specified paths
- Refactor savePath() method to handle both directory and full file path modes
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

Git Diff 代码审查报告

总体评价

这次代码修改主要实现了对截图保存路径的增强功能,允许用户指定完整文件路径(包括文件名和格式)而不仅仅是目录路径。代码整体结构清晰,新增了几个辅助函数来处理路径解析和应用设置,提高了代码的可维护性。

语法逻辑分析

1. 参数处理逻辑改进 (src/main.cpp)

// 检查是否有其他截图模式参数
bool hasOtherMode = cmdParser.isSet(delayOption) || 
                    cmdParser.isSet(fullscreenOption) || 
                    cmdParser.isSet(topWindowOption) || 
                    cmdParser.isSet(prohibitNotifyOption) || 
                    cmdParser.isSet(screenRecordFullScreenOption);

// 如果设置了 save-path 且有其他模式,只设置路径不启动截图
if (cmdParser.isSet(savePathOption) && hasOtherMode) {
    window.setSavePath(cmdParser.value(savePathOption));
    qCDebug(dsrApp) << "Save path set to:" << cmdParser.value(savePathOption);
}

优点:

  • 明确区分了单独设置保存路径和与其他截图模式结合使用的情况
  • 使用布尔变量hasOtherMode使逻辑更清晰

建议:

  • 考虑将这段逻辑提取为一个单独的函数,提高可读性和复用性

2. 路径解析函数 (src/main_window.cpp)

bool MainWindow::parsePathArgument(const QString &path, QString &outDir, QString &outFileName, QString &outFormat)
{
    if (path.isEmpty()) {
        qCWarning(dsrApp) << "parsePathArgument: 输入路径为空";
        return false;
    }
    
    QFileInfo fileInfo(path);
    QString suffix = fileInfo.suffix().toLower();
    
    // 检查是否为支持的图片格式
    bool isSupportedFormat = (suffix == "png" || suffix == "jpg" || suffix == "jpeg" || suffix == "bmp");
    
    // 如果是支持的图片格式,且不是已存在的目录,则认为是文件路径
    if (isSupportedFormat && !fileInfo.isDir()) {
        // 这是一个完整的文件路径
        outDir = fileInfo.absolutePath();
        outFileName = fileInfo.fileName();
        
        // 转换格式名称
        if (suffix == "png") {
            outFormat = "PNG";
        } else if (suffix == "jpg" || suffix == "jpeg") {
            outFormat = "JPEG";
        } else if (suffix == "bmp") {
            outFormat = "BMP";
        }
        
        qCInfo(dsrApp) << "parsePathArgument: 完整文件路径 - 目录:" << outDir 
                       << ", 文件名:" << outFileName << ", 格式:" << outFormat;
        return true;
    } else {
        // 这是一个目录路径
        outDir = path;
        outFileName.clear();
        outFormat.clear();
        
        qCInfo(dsrApp) << "parsePathArgument: 目录路径:" << outDir;
        return false;
    }
}

优点:

  • 清晰区分了目录路径和完整文件路径
  • 正确处理了多种图片格式
  • 使用了QFileInfo来处理路径,提高了跨平台兼容性

建议:

  • 格式判断部分可以使用QMap或QHash来简化代码
  • 考虑添加对更多格式的支持,如WebP

代码质量

优点

  1. 新增函数都有适当的注释说明其用途
  2. 日志记录详细,便于调试
  3. 代码结构清晰,职责分离良好

改进建议

  1. 魔法数字: 在saveAction函数中,格式判断使用的是数字(0,1,2),建议使用枚举类型提高可读性

    enum ImageFormat {
        PNG = 0,
        JPEG = 1,
        BMP = 2
    };
  2. 代码重复: 在parsePathArgument和saveAction中都有格式转换逻辑,可以提取为公共函数

    QString MainWindow::getImageFormatString(int formatCode) {
        switch (formatCode) {
            case PNG: return "PNG";
            case JPEG: return "JPEG";
            case BMP: return "BMP";
            default: return "PNG";
        }
    }
  3. 错误处理: 在saveAction函数中,当目录创建失败时直接返回false,但没有给用户明确的反馈,建议添加错误提示

代码性能

  1. 路径处理: 使用QFileInfo处理路径是合理的,但频繁调用可能会影响性能,特别是在处理大量文件时

    • 建议: 考虑缓存QFileInfo结果,避免重复计算
  2. 字符串操作: 代码中有多个字符串拼接操作,如:

    m_saveFileName = m_shotSavePath + "/" + m_shotFileName;
    • 建议: 使用QDir的filePath()方法,更高效且跨平台:
      m_saveFileName = QDir(m_shotSavePath).filePath(m_shotFileName);

代码安全

  1. 路径验证: 代码中检查了路径是否存在,但没有验证路径是否合法或是否在允许的范围内

    • 建议: 添加路径合法性检查,防止路径遍历攻击
  2. 文件覆盖: 当用户指定完整文件路径时,代码会直接覆盖同名文件

    // 构建完整路径(直接覆盖同名文件)
    m_saveFileName = m_shotSavePath + "/" + m_shotFileName;
    • 建议: 添加文件存在检查,并在覆盖前提示用户确认
  3. 输入验证: parsePathArgument函数检查了空路径,但没有验证路径中的特殊字符或非法字符

    • 建议: 添加更严格的输入验证

其他建议

  1. 单元测试: 建议为新增的parsePathArgument和applyPathSettings函数编写单元测试,确保各种边界情况得到正确处理

  2. 文档更新: 建议更新用户文档,说明新增的完整文件路径功能

  3. 向后兼容性: 代码保持了与旧版本的兼容性,这是一个好的实践

  4. 国际化: 日志消息使用了中文,考虑是否需要国际化支持

总结

这次代码修改实现了对截图保存路径的增强功能,整体代码质量较高,逻辑清晰。主要改进点在于:

  1. 明确区分了目录路径和完整文件路径
  2. 改进了参数处理逻辑
  3. 增加了详细的日志记录

建议的改进主要集中在:

  1. 使用枚举替代魔法数字
  2. 提取公共函数减少代码重复
  3. 增强输入验证和错误处理
  4. 优化字符串操作以提高性能

总体而言,这是一次有价值的代码改进,增强了功能的同时保持了良好的代码质量。

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengzhongyuan365-dev, 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

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Jan 22, 2026

Copy link
Copy Markdown
Contributor

This pr force merged! (status: unstable)

@deepin-bot deepin-bot Bot merged commit 7b7ea33 into linuxdeepin:develop/snipe Jan 22, 2026
7 of 9 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