这里是以文本方式加载文件。但是可能遇到的是二进制文件,里面会做判断 二进制时hexAsk是否询问,当用户指定打开格式时,不需要询问 MsgBoxParent::尽量把这个给一下,让MsgBox有图标,不那么难看。
| 287 | //二进制时hexAsk是否询问,当用户指定打开格式时,不需要询问 |
| 288 | //MsgBoxParent::尽量把这个给一下,让MsgBox有图标,不那么难看。 |
| 289 | int FileManager::loadFileDataInText(ScintillaEditView* editView, QString filePath, CODE_ID& fileTextCode, RC_LINE_FORM& lineEnd,CCNotePad * callbackObj, bool hexAsk, QWidget* msgBoxParent) |
| 290 | { |
| 291 | QFile file(filePath); |
| 292 | |
| 293 | //如果文件不存在,直接返回 |
| 294 | if (!file.exists()) |
| 295 | { |
| 296 | return -1; |
| 297 | } |
| 298 | |
| 299 | QFlags<QFileDevice::Permission> power = QFile::permissions(filePath); |
| 300 | |
| 301 | //直接以只读的方式打开,至于能不能保存,是保存时需要考虑的问题。 |
| 302 | //只需要在保存的时候获取admin权限即可 |
| 303 | QIODevice::OpenMode mode; |
| 304 | |
| 305 | mode = QIODevice::ExistingOnly | QIODevice::ReadOnly; |
| 306 | |
| 307 | if (!file.open(mode)) |
| 308 | { |
| 309 | qDebug() << file.error(); |
| 310 | #ifdef Q_OS_WIN |
| 311 | //打开失败,这里一般是权限问题导致。如果是windows,在外面申请权限后继续处理 |
| 312 | if (QFileDevice::OpenError == file.error()) |
| 313 | { |
| 314 | if (callbackObj != nullptr) |
| 315 | { |
| 316 | return callbackObj->runAsAdmin(filePath); |
| 317 | } |
| 318 | return 1; |
| 319 | } |
| 320 | #endif |
| 321 | #ifdef Q_OS_UNIX |
| 322 | QMessageBox::warning(msgBoxParent, tr("Error"), tr("Open File %1 failed").arg(filePath)); |
| 323 | #endif |
| 324 | return 2; |
| 325 | } |
| 326 | |
| 327 | qint64 fileSize = file.size(); |
| 328 | |
| 329 | //如果文件是空的。检查一下,有可能在临时文件损坏情况下出现,外面需要使用 |
| 330 | if (fileSize == 0) |
| 331 | { |
| 332 | m_lastErrorCode = ERROR_TYPE::OPEN_EMPTY_FILE; |
| 333 | file.close(); |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | qint64 bufferSizeRequested = fileSize + qMin((qint64)(1 << 20), (qint64)(fileSize / 6)); |
| 338 | |
| 339 | if (bufferSizeRequested > INT_MAX) |
| 340 | { |
| 341 | QMessageBox::warning(msgBoxParent, tr("Error"), tr("File is too big to be opened by Notepad--")); |
| 342 | file.close(); |
| 343 | return 3; |
| 344 | } |
| 345 | |
| 346 | QList<LineFileInfo> outputLineInfoVec; |
no test coverage detected