diff --git a/src/plugins/bineditor/bineditorplugin.cpp b/src/plugins/bineditor/bineditorplugin.cpp index 1ec24de3986..39a653c79ca 100644 --- a/src/plugins/bineditor/bineditorplugin.cpp +++ b/src/plugins/bineditor/bineditorplugin.cpp @@ -97,13 +97,13 @@ public: return type == TypeRemoved ? BehaviorSilent : IDocument::reloadBehavior(state, type); } - OpenResult open(const FilePath &filePath, const FilePath &realFilePath) final + Result<> open(const FilePath &filePath, const FilePath &realFilePath) final { QTC_CHECK(filePath == realFilePath); // The bineditor can do no autosaving return openImpl(filePath); } - OpenResult openImpl(const FilePath &filePath, quint64 offset = 0); + Result<> openImpl(const FilePath &filePath, quint64 offset = 0); void provideData(quint64 address); @@ -2118,35 +2118,27 @@ bool BinEditorDocument::setContents(const QByteArray &contents) return true; } -IDocument::OpenResult BinEditorDocument::openImpl(const FilePath &filePath, quint64 offset) +Result<> BinEditorDocument::openImpl(const FilePath &filePath, quint64 offset) { const qint64 size = filePath.fileSize(); if (size < 0) { - QString msg = Tr::tr("Cannot open %1: %2").arg(filePath.toUserOutput(), Tr::tr("File Error")); // FIXME: Was: file.errorString(), but we don't have a file anymore. - QMessageBox::critical(ICore::dialogParent(), Tr::tr("File Error"), msg); - return {OpenResult::CannotHandle, msg}; + return ResultError(Tr::tr("Cannot open %1: %2").arg(filePath.toUserOutput())); } - if (size == 0) { - QString msg = Tr::tr("The Binary Editor cannot open empty files."); - QMessageBox::critical(ICore::dialogParent(), Tr::tr("File Error"), msg); - return {OpenResult::CannotHandle, msg}; - } + if (size == 0) + return ResultError(Tr::tr("The Binary Editor cannot open empty files.")); - if (size / 16 >= qint64(1) << 31) { - // The limit is 2^31 lines (due to QText* interfaces) * 16 bytes per line. - QString msg = Tr::tr("The file is too big for the Binary Editor (max. 32GB)."); - QMessageBox::critical(ICore::dialogParent(), Tr::tr("File Error"), msg); - return {OpenResult::CannotHandle, msg}; - } + // The limit is 2^31 lines (due to QText* interfaces) * 16 bytes per line. + if (size / 16 >= qint64(1) << 31) + return ResultError(Tr::tr("The file is too big for the Binary Editor (max. 32GB).")); if (offset >= quint64(size)) - return OpenResult::CannotHandle; + return ResultError(Tr::tr("File offset too large")); setFilePath(filePath); setSizes(offset, size); - return OpenResult::Success; + return ResultOk; } void BinEditorDocument::provideData(quint64 address) @@ -2179,8 +2171,8 @@ Result<> BinEditorDocument::reload(ReloadFlag flag, ChangeType type) return ResultOk; emit aboutToReload(); clear(); - const OpenResult result = openImpl(filePath()); - emit reloadFinished(result.code == OpenResult::Success); + const Result<> result = openImpl(filePath()); + emit reloadFinished(result.has_value()); return result; } diff --git a/src/plugins/clangformat/clangformatconfigwidget.cpp b/src/plugins/clangformat/clangformatconfigwidget.cpp index 3d2ac617bc7..2f452a364a1 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatconfigwidget.cpp @@ -272,8 +272,7 @@ void ClangFormatConfigWidget::reopenClangFormatDocument() { GuardLocker locker(m_ignoreChanges); - if (m_editor->document()->open(m_config->filePath(), m_config->filePath()).code - == Core::IDocument::OpenResult::Success) { + if (m_editor->document()->open(m_config->filePath(), m_config->filePath())) { invokeMethodForLanguageClientManager("documentOpened", Q_ARG(Core::IDocument *, m_editor->document())); } diff --git a/src/plugins/compilerexplorer/compilerexplorereditor.cpp b/src/plugins/compilerexplorer/compilerexplorereditor.cpp index 544ef6a2e64..a5e9a483678 100644 --- a/src/plugins/compilerexplorer/compilerexplorereditor.cpp +++ b/src/plugins/compilerexplorer/compilerexplorereditor.cpp @@ -151,8 +151,8 @@ class JsonSettingsDocument : public Core::IDocument public: JsonSettingsDocument(QUndoStack *undoStack); - OpenResult open(const Utils::FilePath &filePath, - const Utils::FilePath &realFilePath) override; + Result<> open(const Utils::FilePath &filePath, + const Utils::FilePath &realFilePath) override; Result<> saveImpl(const Utils::FilePath &filePath, bool autoSave) override; @@ -380,25 +380,25 @@ JsonSettingsDocument::JsonSettingsDocument(QUndoStack *undoStack) m_ceSettings.setUndoStack(undoStack); } -IDocument::OpenResult JsonSettingsDocument::open(const FilePath &filePath, - const FilePath &realFilePath) +Result<> JsonSettingsDocument::open(const FilePath &filePath, + const FilePath &realFilePath) { if (!filePath.isReadableFile()) - return OpenResult::CannotHandle; + return ResultError(Tr::tr("File not readable")); Result contents = realFilePath.fileContents(); if (!contents) - return {OpenResult::CannotHandle, contents.error()}; + return ResultError(contents.error()); Result result = storeFromJson(*contents); if (!result) - return {OpenResult::CannotHandle, result.error()}; + return ResultError(result.error()); setFilePath(filePath); - m_ceSettings.fromMap(*result); + m_ceSettings.fromMap(result.value()); emit settingsChanged(); - return OpenResult::Success; + return ResultOk; } Result<> JsonSettingsDocument::saveImpl(const FilePath &newFilePath, bool autoSave) diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp index c857ad8264c..5b2b23bb4e9 100644 --- a/src/plugins/coreplugin/editormanager/editormanager.cpp +++ b/src/plugins/coreplugin/editormanager/editormanager.cpp @@ -903,15 +903,13 @@ IEditor *EditorManagerPrivate::openEditor(EditorView *view, const FilePath &file factory = factories.isEmpty() ? nullptr : factories.takeFirst(); continue; } - IDocument::OpenResult openResult = editor->document()->open(filePath, realFp); - if (openResult.code == IDocument::OpenResult::Success) + Result<> openResult = editor->document()->open(filePath, realFp); + if (openResult) break; - errorString = openResult.error; + errorString = openResult.error(); overrideCursor.reset(); delete editor; editor = nullptr; - // can happen e.g. when trying to open an completely empty .qrc file - QTC_CHECK(openResult.code == IDocument::OpenResult::CannotHandle); } else { QTC_ASSERT(factory->isExternalEditor(), factory = factories.isEmpty() ? nullptr : factories.takeFirst(); @@ -2993,7 +2991,7 @@ void EditorManager::populateOpenWithMenu(QMenu *menu, const FilePath &filePath) menu->setEnabled(anyMatches); } -void EditorManager::runWithTemporaryEditor(const Utils::FilePath &filePath, +void EditorManager::runWithTemporaryEditor(const FilePath &filePath, const std::function &callback) { const MimeType mt = mimeTypeForFile(filePath, MimeMatchMode::MatchDefaultAndRemote); @@ -3006,7 +3004,7 @@ void EditorManager::runWithTemporaryEditor(const Utils::FilePath &filePath, if (!editor) continue; editor->document()->setTemporary(true); - if (editor->document()->open(filePath, filePath).code != IDocument::OpenResult::Success) + if (!editor->document()->open(filePath, filePath)) continue; callback(editor.get()); break; diff --git a/src/plugins/coreplugin/idocument.cpp b/src/plugins/coreplugin/idocument.cpp index 3774e2ad929..7e01baceda9 100644 --- a/src/plugins/coreplugin/idocument.cpp +++ b/src/plugins/coreplugin/idocument.cpp @@ -71,7 +71,7 @@ */ /*! - \enum Core::IDocument::OpenResult + \enum Core::Utils::Result<> The OpenResult enum describes whether a file was successfully opened. @@ -315,11 +315,11 @@ Id IDocument::id() const \sa shouldAutoSave() \sa setFilePath() */ -IDocument::OpenResult IDocument::open(const FilePath &filePath, const FilePath &realFilePath) +Result<> IDocument::open(const FilePath &filePath, const FilePath &realFilePath) { Q_UNUSED(filePath) Q_UNUSED(realFilePath) - return OpenResult::CannotHandle; + return ResultError(ResultUnimplemented); } /*! @@ -807,9 +807,4 @@ QString IDocument::uniqueDisplayName() const return d->uniqueDisplayName; } -IDocument::OpenResult::operator Result<>() const -{ - return makeResult(code == Success, error); -} - } // namespace Core diff --git a/src/plugins/coreplugin/idocument.h b/src/plugins/coreplugin/idocument.h index 4d957360d97..a8a780c8192 100644 --- a/src/plugins/coreplugin/idocument.h +++ b/src/plugins/coreplugin/idocument.h @@ -26,24 +26,6 @@ class CORE_EXPORT IDocument : public QObject Q_OBJECT public: - class CORE_EXPORT OpenResult - { - public: - enum Code { - Success, - CannotHandle - }; - - OpenResult() = default; - OpenResult(Code code) : code(code) {} - OpenResult(Code code, const QString &error) : code(code), error(error) {} - - operator Utils::Result<>() const; - - Code code; - QString error; - }; - // This enum must match the indexes of the reloadBehavior widget // in generalsettings.ui enum ReloadSetting { @@ -78,7 +60,7 @@ public: void setId(Utils::Id id); Utils::Id id() const; - virtual OpenResult open(const Utils::FilePath &filePath, const Utils::FilePath &realFilePath); + virtual Utils::Result<> open(const Utils::FilePath &filePath, const Utils::FilePath &realFilePath); Utils::Result<> save(const Utils::FilePath &filePath = {}, bool autoSave = false); diff --git a/src/plugins/cppeditor/cpppointerdeclarationformatter_test.cpp b/src/plugins/cppeditor/cpppointerdeclarationformatter_test.cpp index 39c11c65f95..ab5ad23b098 100644 --- a/src/plugins/cppeditor/cpppointerdeclarationformatter_test.cpp +++ b/src/plugins/cppeditor/cpppointerdeclarationformatter_test.cpp @@ -82,8 +82,8 @@ public: // Open file QScopedPointer editor(TextEditor::createPlainTextEditor()); - Core::IDocument::OpenResult res = editor->document()->open(document->filePath(), document->filePath()); - QVERIFY(res.error.isEmpty()); + Result<> res = editor->document()->open(document->filePath(), document->filePath()); + QVERIFY(res.has_value()); // Set cursor position QTextCursor cursor = editor->textCursor(); diff --git a/src/plugins/designer/formwindowfile.cpp b/src/plugins/designer/formwindowfile.cpp index ac5fc3067b4..2b455542cc3 100644 --- a/src/plugins/designer/formwindowfile.cpp +++ b/src/plugins/designer/formwindowfile.cpp @@ -43,27 +43,26 @@ FormWindowFile::FormWindowFile(QDesignerFormWindowInterface *form, QObject *pare m_resourceHandler, &ResourceHandler::updateResources); } -IDocument::OpenResult FormWindowFile::open(const FilePath &filePath, - const FilePath &realFilePath) +Result<> FormWindowFile::open(const FilePath &filePath, const FilePath &realFilePath) { if (Designer::Constants::Internal::debug) qDebug() << "FormWindowFile::open" << filePath.toUserOutput(); QDesignerFormWindowInterface *form = formWindow(); - QTC_ASSERT(form, return OpenResult::CannotHandle); + QTC_ASSERT(form, return ResultError(ResultAssert)); if (filePath.isEmpty()) - return OpenResult::CannotHandle; + return ResultError("File name is empty"); // FIXME: Use something better QString contents; QString errorString; TextFileFormat::ReadResult readResult = read(filePath.absoluteFilePath(), &contents, &errorString); - if (readResult == Utils::TextFileFormat::ReadEncodingError) - return {OpenResult::CannotHandle, errorString}; - if (readResult != Utils::TextFileFormat::ReadSuccess) - return {OpenResult::CannotHandle, errorString}; + if (readResult == TextFileFormat::ReadEncodingError) + return ResultError(errorString); + if (readResult != TextFileFormat::ReadSuccess) + return ResultError(errorString); form->setFileName(filePath.absoluteFilePath().toUrlishString()); const QByteArray contentsBA = contents.toUtf8(); @@ -71,7 +70,7 @@ IDocument::OpenResult FormWindowFile::open(const FilePath &filePath, str.setData(contentsBA); str.open(QIODevice::ReadOnly); if (!form->setContents(&str, &errorString)) - return {OpenResult::CannotHandle, errorString}; + return ResultError(errorString); form->setDirty(filePath != realFilePath); syncXmlFromFormWindow(); @@ -79,7 +78,7 @@ IDocument::OpenResult FormWindowFile::open(const FilePath &filePath, setShouldAutoSave(false); resourceHandler()->updateProjectResources(); - return OpenResult::Success; + return ResultOk; } Result<> FormWindowFile::saveImpl(const FilePath &filePath, bool autoSave) @@ -201,8 +200,8 @@ Result<> FormWindowFile::reload(ReloadFlag flag, ChangeType type) return ResultOk; } else { emit aboutToReload(); - const OpenResult result = open(filePath(), filePath()); - emit reloadFinished(result.code == OpenResult::Success); + const Result<> result = open(filePath(), filePath()); + emit reloadFinished(result.has_value()); return result; } } diff --git a/src/plugins/designer/formwindowfile.h b/src/plugins/designer/formwindowfile.h index 2a74c766518..444676cb954 100644 --- a/src/plugins/designer/formwindowfile.h +++ b/src/plugins/designer/formwindowfile.h @@ -26,8 +26,8 @@ public: ~FormWindowFile() override { } // IDocument - OpenResult open(const Utils::FilePath &filePath, - const Utils::FilePath &realFilePath) override; + Utils::Result<> open(const Utils::FilePath &filePath, + const Utils::FilePath &realFilePath) override; QByteArray contents() const override; bool setContents(const QByteArray &contents) override; bool shouldAutoSave() const override; diff --git a/src/plugins/diffeditor/diffeditordocument.cpp b/src/plugins/diffeditor/diffeditordocument.cpp index db5614e8421..bbb5ec1eee4 100644 --- a/src/plugins/diffeditor/diffeditordocument.cpp +++ b/src/plugins/diffeditor/diffeditordocument.cpp @@ -265,7 +265,7 @@ Result<> DiffEditorDocument::reload(ReloadFlag flag, ChangeType type) return open(filePath(), filePath()); } -IDocument::OpenResult DiffEditorDocument::open(const FilePath &filePath, const FilePath &realFilePath) +Result<> DiffEditorDocument::open(const FilePath &filePath, const FilePath &realFilePath) { QTC_CHECK(filePath == realFilePath); // does not support autosave beginReload(); @@ -274,7 +274,7 @@ IDocument::OpenResult DiffEditorDocument::open(const FilePath &filePath, const F ReadResult readResult = read(filePath, &patch, &errorString); if (readResult == TextFileFormat::ReadIOError || readResult == TextFileFormat::ReadMemoryAllocationError) { - return {OpenResult::CannotHandle, errorString}; + return ResultError(errorString); } const std::optional> fileDataList = DiffUtils::readPatch(patch); @@ -294,8 +294,8 @@ IDocument::OpenResult DiffEditorDocument::open(const FilePath &filePath, const F if (!ok && readResult == TextFileFormat::ReadEncodingError) ok = selectEncoding(); if (!ok) - return {OpenResult::CannotHandle, errorString}; - return OpenResult::Success; + return ResultError(errorString); + return ResultOk; } bool DiffEditorDocument::selectEncoding() diff --git a/src/plugins/diffeditor/diffeditordocument.h b/src/plugins/diffeditor/diffeditordocument.h index 5c03c5ec01a..1157993d01f 100644 --- a/src/plugins/diffeditor/diffeditordocument.h +++ b/src/plugins/diffeditor/diffeditordocument.h @@ -62,8 +62,8 @@ public: bool isSaveAsAllowed() const override; void reload(); Utils::Result<> reload(ReloadFlag flag, ChangeType type) override; - OpenResult open(const Utils::FilePath &filePath, - const Utils::FilePath &realFilePath) override; + Utils::Result<> open(const Utils::FilePath &filePath, + const Utils::FilePath &realFilePath) override; bool selectEncoding(); State state() const { return m_state; } diff --git a/src/plugins/imageviewer/imageviewerfile.cpp b/src/plugins/imageviewer/imageviewerfile.cpp index 5085f3552e5..3d7453cf24e 100644 --- a/src/plugins/imageviewer/imageviewerfile.cpp +++ b/src/plugins/imageviewer/imageviewerfile.cpp @@ -64,26 +64,26 @@ ImageViewerFile::~ImageViewerFile() cleanUp(); } -IDocument::OpenResult ImageViewerFile::open(const FilePath &filePath, const FilePath &realfilePath) +Result<> ImageViewerFile::open(const FilePath &filePath, const FilePath &realfilePath) { QTC_CHECK(filePath == realfilePath); // does not support auto save - OpenResult res = openImpl(filePath); - emit openFinished(res.code == OpenResult::Success); + Result<> res = openImpl(filePath); + emit openFinished(res.has_value()); return res; } -IDocument::OpenResult ImageViewerFile::openImpl(const FilePath &filePath) +Result<> ImageViewerFile::openImpl(const FilePath &filePath) { cleanUp(); if (!filePath.isReadableFile()) - return OpenResult::CannotHandle; + return ResultError(Tr::tr("File not readable")); const QString &fileName = filePath.toUrlishString(); QByteArray format = QImageReader::imageFormat(fileName); // if it is impossible to recognize a file format - file will not be open correctly if (format.isEmpty()) - return {OpenResult::CannotHandle, Tr::tr("Image format not supported.")}; + return ResultError(Tr::tr("Image format not supported.")); #ifndef QT_NO_SVG if (format.startsWith("svg")) { @@ -92,7 +92,7 @@ IDocument::OpenResult ImageViewerFile::openImpl(const FilePath &filePath) if (!bound.isValid() || (qFuzzyIsNull(bound.width()) && qFuzzyIsNull(bound.height()))) { delete m_tempSvgItem; m_tempSvgItem = nullptr; - return {OpenResult::CannotHandle, Tr::tr("Failed to read SVG image.")}; + return ResultError(Tr::tr("Failed to read SVG image.")); } m_type = TypeSvg; emit imageSizeChanged(m_tempSvgItem->boundingRect().size().toSize()); @@ -105,7 +105,7 @@ IDocument::OpenResult ImageViewerFile::openImpl(const FilePath &filePath) if (!m_movie->isValid()) { delete m_movie; m_movie = nullptr; - return {OpenResult::CannotHandle, Tr::tr("Failed to read image.")}; + return ResultError(Tr::tr("Failed to read image.")); } m_type = TypeMovie; connect(m_movie, &QMovie::resized, this, &ImageViewerFile::imageSizeChanged); @@ -115,7 +115,7 @@ IDocument::OpenResult ImageViewerFile::openImpl(const FilePath &filePath) if (m_pixmap->isNull()) { delete m_pixmap; m_pixmap = nullptr; - return {OpenResult::CannotHandle, Tr::tr("Failed to read image.")}; + return ResultError(Tr::tr("Failed to read image.")); } m_type = TypePixmap; emit imageSizeChanged(m_pixmap->size()); @@ -123,7 +123,7 @@ IDocument::OpenResult ImageViewerFile::openImpl(const FilePath &filePath) setFilePath(filePath); setMimeType(Utils::mimeTypeForFile(filePath).name()); - return OpenResult::Success; + return ResultOk; } Core::IDocument::ReloadBehavior ImageViewerFile::reloadBehavior(ChangeTrigger state, ChangeType type) const @@ -141,8 +141,8 @@ Result<> ImageViewerFile::reload(IDocument::ReloadFlag flag, IDocument::ChangeTy if (flag == FlagIgnore) return ResultOk; emit aboutToReload(); - const OpenResult result = openImpl(filePath()); - emit reloadFinished( result.code == OpenResult::Success); + const Result<> result = openImpl(filePath()); + emit reloadFinished(result.has_value()); return result; } diff --git a/src/plugins/imageviewer/imageviewerfile.h b/src/plugins/imageviewer/imageviewerfile.h index ea562341107..1f030f24fcc 100644 --- a/src/plugins/imageviewer/imageviewerfile.h +++ b/src/plugins/imageviewer/imageviewerfile.h @@ -35,7 +35,8 @@ public: ImageViewerFile(); ~ImageViewerFile() override; - OpenResult open(const Utils::FilePath &filePath, const Utils::FilePath &realFilePath) override; + Utils::Result<> open(const Utils::FilePath &filePath, + const Utils::FilePath &realFilePath) override; ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const override; Utils::Result<> reload(ReloadFlag flag, ChangeType type) override; @@ -54,7 +55,7 @@ signals: private: void cleanUp(); - OpenResult openImpl(const Utils::FilePath &filePath); + Utils::Result<> openImpl(const Utils::FilePath &filePath); ImageType m_type = TypeInvalid; #ifndef QT_NO_SVG diff --git a/src/plugins/modeleditor/modeldocument.cpp b/src/plugins/modeleditor/modeldocument.cpp index 501bf6e1a88..49763ade7b7 100644 --- a/src/plugins/modeleditor/modeldocument.cpp +++ b/src/plugins/modeleditor/modeldocument.cpp @@ -42,13 +42,10 @@ ModelDocument::~ModelDocument() delete d; } -Core::IDocument::OpenResult ModelDocument::open(const FilePath &filePath, - const FilePath &realFilePath) +Result<> ModelDocument::open(const FilePath &filePath, const FilePath &realFilePath) { Q_UNUSED(filePath) - - OpenResult result = load(realFilePath); - return result; + return load(realFilePath); } Result<> ModelDocument::saveImpl(const FilePath &filePath, bool autoSave) @@ -111,7 +108,7 @@ ExtDocumentController *ModelDocument::documentController() const return d->documentController; } -Core::IDocument::OpenResult ModelDocument::load(const FilePath &fileName) +Result<> ModelDocument::load(const FilePath &fileName) { d->documentController = ModelEditorPlugin::modelsManager()->createModel(this); connect(d->documentController, &qmt::DocumentController::changed, this, &IDocument::changed); @@ -120,11 +117,10 @@ Core::IDocument::OpenResult ModelDocument::load(const FilePath &fileName) d->documentController->loadProject(fileName); setFilePath(d->documentController->projectController()->project()->fileName()); } catch (const qmt::FileNotFoundException &ex) { - return {OpenResult::CannotHandle, ex.errorMessage()}; + return ResultError(ex.errorMessage()); } catch (const qmt::Exception &ex) { - return {OpenResult::CannotHandle, - Tr::tr("Could not open \"%1\" for reading: %2.") - .arg(fileName.toUserOutput(), ex.errorMessage())}; + return ResultError(Tr::tr("Could not open \"%1\" for reading: %2.") + .arg(fileName.toUserOutput(), ex.errorMessage())); } FilePath configPath = d->documentController->projectController()->project()->configPath(); @@ -139,7 +135,7 @@ Core::IDocument::OpenResult ModelDocument::load(const FilePath &fileName) } emit contentSet(); - return OpenResult::Success; + return ResultOk; } } // namespace ModelEditor::Internal diff --git a/src/plugins/modeleditor/modeldocument.h b/src/plugins/modeleditor/modeldocument.h index d6a06594db0..98b23800ba2 100644 --- a/src/plugins/modeleditor/modeldocument.h +++ b/src/plugins/modeleditor/modeldocument.h @@ -12,8 +12,7 @@ namespace ModelEditor::Internal { class ExtDocumentController; -class ModelDocument : - public Core::IDocument +class ModelDocument : public Core::IDocument { Q_OBJECT class ModelDocumentPrivate; @@ -26,8 +25,8 @@ signals: void contentSet(); public: - OpenResult open(const Utils::FilePath &filePath, - const Utils::FilePath &realFilePath) override; + Utils::Result<> open(const Utils::FilePath &filePath, + const Utils::FilePath &realFilePath) override; bool shouldAutoSave() const override; bool isModified() const override; bool isSaveAsAllowed() const override; @@ -35,7 +34,7 @@ public: ExtDocumentController *documentController() const; - OpenResult load(const Utils::FilePath &fileName); + Utils::Result<> load(const Utils::FilePath &fileName); protected: Utils::Result<> saveImpl(const Utils::FilePath &filePath, bool autoSave) override; diff --git a/src/plugins/resourceeditor/qrceditor/resourcefile.cpp b/src/plugins/resourceeditor/qrceditor/resourcefile.cpp index 1463c41750f..eb04747f6ee 100644 --- a/src/plugins/resourceeditor/qrceditor/resourcefile.cpp +++ b/src/plugins/resourceeditor/qrceditor/resourcefile.cpp @@ -89,13 +89,13 @@ ResourceFile::~ResourceFile() clearPrefixList(); } -Core::IDocument::OpenResult ResourceFile::load() +Result<> ResourceFile::load() { m_error_message.clear(); if (m_filePath.isEmpty()) { m_error_message = Tr::tr("The file name is empty."); - return Core::IDocument::OpenResult::CannotHandle; + return ResultError(m_error_message); } clearPrefixList(); @@ -108,7 +108,7 @@ Core::IDocument::OpenResult ResourceFile::load() QFile file(m_filePath.toUrlishString()); if (!file.open(QIODevice::ReadOnly)) { m_error_message = file.errorString(); - return Core::IDocument::OpenResult::CannotHandle; + return ResultError(m_error_message); } QByteArray data = file.readAll(); // Detect line ending style @@ -122,7 +122,7 @@ Core::IDocument::OpenResult ResourceFile::load() if (!doc.setContent(data, &error_msg, &error_line, &error_col)) { m_error_message = Tr::tr("XML error on line %1, col %2: %3") .arg(error_line).arg(error_col).arg(error_msg); - return Core::IDocument::OpenResult::CannotHandle; + return ResultError(m_error_message); } } else { @@ -133,7 +133,7 @@ Core::IDocument::OpenResult ResourceFile::load() if (!doc.setContent(m_contents, &error_msg, &error_line, &error_col)) { m_error_message = Tr::tr("XML error on line %1, col %2: %3") .arg(error_line).arg(error_col).arg(error_msg); - return Core::IDocument::OpenResult::CannotHandle; + return ResultError(m_error_message); } } @@ -141,7 +141,7 @@ Core::IDocument::OpenResult ResourceFile::load() QDomElement root = doc.firstChildElement(QLatin1String("RCC")); if (root.isNull()) { m_error_message = Tr::tr("The root element is missing."); - return Core::IDocument::OpenResult::CannotHandle; + return ResultError(m_error_message); } QDomElement relt = root.firstChildElement(QLatin1String("qresource")); @@ -174,7 +174,7 @@ Core::IDocument::OpenResult ResourceFile::load() } } - return Core::IDocument::OpenResult::Success; + return ResultOk; } QString ResourceFile::contents() const @@ -1078,11 +1078,11 @@ QModelIndex ResourceModel::deleteItem(const QModelIndex &idx) return index(file_idx, 0, prefix_model_idx); } -Core::IDocument::OpenResult ResourceModel::reload() +Result<> ResourceModel::reload() { beginResetModel(); - Core::IDocument::OpenResult result = m_resource_file.load(); - if (result.code == Core::IDocument::OpenResult::Success) + Result<> result = m_resource_file.load(); + if (result.has_value()) setDirty(false); endResetModel(); return result; diff --git a/src/plugins/resourceeditor/qrceditor/resourcefile_p.h b/src/plugins/resourceeditor/qrceditor/resourcefile_p.h index 600e6833b47..110c5062131 100644 --- a/src/plugins/resourceeditor/qrceditor/resourcefile_p.h +++ b/src/plugins/resourceeditor/qrceditor/resourcefile_p.h @@ -107,7 +107,7 @@ public: void setFilePath(const Utils::FilePath &filePath) { m_filePath = filePath; } Utils::FilePath filePath() const { return m_filePath; } - Core::IDocument::OpenResult load(); + Utils::Result<> load(); bool save(); QString contents() const; QString errorMessage() const { return m_error_message; } @@ -227,7 +227,7 @@ private: bool renameFile(const QString &fileName, const QString &newFileName); public: - virtual Core::IDocument::OpenResult reload(); + virtual Utils::Result<> reload(); virtual bool save(); QString contents() const { return m_resource_file.contents(); } diff --git a/src/plugins/resourceeditor/resourceeditor.cpp b/src/plugins/resourceeditor/resourceeditor.cpp index 463927b64c8..2126c030f23 100644 --- a/src/plugins/resourceeditor/resourceeditor.cpp +++ b/src/plugins/resourceeditor/resourceeditor.cpp @@ -48,7 +48,7 @@ class ResourceEditorDocument final : public IDocument public: ResourceEditorDocument(QObject *parent = nullptr); - OpenResult open(const FilePath &filePath, const FilePath &realFilePath) final; + Result<> open(const FilePath &filePath, const FilePath &realFilePath) final; QString plainText() const { return m_model.contents(); } QByteArray contents() const final { return m_model.contents().toUtf8(); } bool setContents(const QByteArray &contents) final; @@ -179,8 +179,7 @@ ResourceEditorImpl::~ResourceEditorImpl() delete m_toolBar; } -IDocument::OpenResult ResourceEditorDocument::open(const FilePath &filePath, - const FilePath &realFilePath) +Result<> ResourceEditorDocument::open(const FilePath &filePath, const FilePath &realFilePath) { if (debugResourceEditorW) qDebug() << "ResourceEditorW::open: " << filePath; @@ -189,9 +188,9 @@ IDocument::OpenResult ResourceEditorDocument::open(const FilePath &filePath, m_model.setFilePath(realFilePath); - OpenResult openResult = m_model.reload(); - if (openResult.code != OpenResult::Success) { - openResult.error = m_model.errorMessage(); + Result<> openResult = m_model.reload(); + if (!openResult) { + openResult = ResultError(m_model.errorMessage()); // FIXME: Move to m_model setBlockDirtyChanged(false); emit loaded(false); return openResult; @@ -203,7 +202,7 @@ IDocument::OpenResult ResourceEditorDocument::open(const FilePath &filePath, m_shouldAutoSave = false; emit loaded(true); - return OpenResult::Success; + return ResultOk; } Result<> ResourceEditorDocument::saveImpl(const FilePath &filePath, bool autoSave) @@ -246,7 +245,7 @@ bool ResourceEditorDocument::setContents(const QByteArray &contents) const FilePath originalFileName = m_model.filePath(); m_model.setFilePath(saver.filePath()); - const bool success = (m_model.reload().code == OpenResult::Success); + const bool success = (m_model.reload().has_value()); m_model.setFilePath(originalFileName); m_shouldAutoSave = false; if (debugResourceEditorW) @@ -283,8 +282,8 @@ Result<> ResourceEditorDocument::reload(ReloadFlag flag, ChangeType type) if (flag == FlagIgnore) return ResultOk; emit aboutToReload(); - const OpenResult result = open(filePath(), filePath()); - emit reloadFinished(result.code == OpenResult::Success); + const Result<> result = open(filePath(), filePath()); + emit reloadFinished(result.has_value()); return result; } diff --git a/src/plugins/resourceeditor/resourcenode.cpp b/src/plugins/resourceeditor/resourcenode.cpp index 5411ac9a0f7..7b67fefdcb6 100644 --- a/src/plugins/resourceeditor/resourcenode.cpp +++ b/src/plugins/resourceeditor/resourcenode.cpp @@ -121,7 +121,7 @@ static bool addFilesToResource(const FilePath &resourceFile, *notAdded = filePaths; ResourceFile file(resourceFile); - if (file.load().code != IDocument::OpenResult::Success) + if (!file.load()) return false; int index = file.indexOfPrefix(prefix, lang); @@ -272,7 +272,7 @@ static void compressTree(FolderNode *n) void ResourceTopLevelNode::addInternalNodes() { ResourceFile file(filePath(), m_contents); - if (file.load().code != IDocument::OpenResult::Success) + if (!file.load()) return; QMap folderNodes; @@ -382,7 +382,7 @@ RemovedFilesFromProject ResourceTopLevelNode::removeFiles(const FilePaths &fileP bool ResourceTopLevelNode::addPrefix(const QString &prefix, const QString &lang) { ResourceFile file(filePath()); - if (file.load().code != IDocument::OpenResult::Success) + if (!file.load()) return false; int index = file.addPrefix(prefix, lang); if (index == -1) @@ -395,7 +395,7 @@ bool ResourceTopLevelNode::addPrefix(const QString &prefix, const QString &lang) bool ResourceTopLevelNode::removePrefix(const QString &prefix, const QString &lang) { ResourceFile file(filePath()); - if (file.load().code != IDocument::OpenResult::Success) + if (!file.load()) return false; for (int i = 0; i < file.prefixCount(); ++i) { if (file.prefix(i) == prefix @@ -411,7 +411,7 @@ bool ResourceTopLevelNode::removePrefix(const QString &prefix, const QString &la bool ResourceTopLevelNode::removeNonExistingFiles() { ResourceFile file(filePath()); - if (file.load().code != IDocument::OpenResult::Success) + if (!file.load()) return false; QFileInfo fi; @@ -490,7 +490,7 @@ RemovedFilesFromProject ResourceFolderNode::removeFiles(const FilePaths &filePat if (notRemoved) *notRemoved = filePaths; ResourceFile file(m_topLevelNode->filePath()); - if (file.load().code != IDocument::OpenResult::Success) + if (!file.load()) return RemovedFilesFromProject::Error; int index = file.indexOfPrefix(m_prefix, m_lang); if (index == -1) @@ -518,8 +518,7 @@ bool ResourceFolderNode::canRenameFile(const FilePath &oldFilePath, const FilePa bool fileEntryExists = false; ResourceFile file(m_topLevelNode->filePath()); - int index = (file.load().code != IDocument::OpenResult::Success) - ? -1 : file.indexOfPrefix(m_prefix, m_lang); + int index = file.load() ? file.indexOfPrefix(m_prefix, m_lang) : -1; if (index != -1) { for (int j = 0; j < file.fileCount(index); ++j) { if (file.file(index, j) == oldFilePath.toUrlishString()) { @@ -535,7 +534,7 @@ bool ResourceFolderNode::canRenameFile(const FilePath &oldFilePath, const FilePa bool ResourceFolderNode::renameFiles(const FilePairs &filesToRename, FilePaths *notRenamed) { ResourceFile file(m_topLevelNode->filePath()); - if (file.load().code != IDocument::OpenResult::Success) + if (!file.load()) return false; int index = file.indexOfPrefix(m_prefix, m_lang); if (index == -1) @@ -567,7 +566,7 @@ bool ResourceFolderNode::renameFiles(const FilePairs &filesToRename, FilePaths * bool ResourceFolderNode::renamePrefix(const QString &prefix, const QString &lang) { ResourceFile file(m_topLevelNode->filePath()); - if (file.load().code != IDocument::OpenResult::Success) + if (!file.load()) return false; int index = file.indexOfPrefix(m_prefix, m_lang); if (index == -1) diff --git a/src/plugins/scxmleditor/scxmleditordocument.cpp b/src/plugins/scxmleditor/scxmleditordocument.cpp index 339d440897f..720d96bd2fd 100644 --- a/src/plugins/scxmleditor/scxmleditordocument.cpp +++ b/src/plugins/scxmleditor/scxmleditordocument.cpp @@ -35,24 +35,23 @@ ScxmlEditorDocument::ScxmlEditorDocument(MainWidget *designWidget, QObject *pare }); } -Core::IDocument::OpenResult ScxmlEditorDocument::open(const FilePath &filePath, - const FilePath &realFilePath) +Result<> ScxmlEditorDocument::open(const FilePath &filePath, const FilePath &realFilePath) { Q_UNUSED(realFilePath) if (filePath.isEmpty()) - return OpenResult::CannotHandle; + return ResultError("File path is empty"); // FIXME: Use something better if (!m_designWidget) - return OpenResult::CannotHandle; + return ResultError(ResultAssert); const FilePath &absoluteFilePath = filePath.absoluteFilePath(); if (!m_designWidget->load(absoluteFilePath.toUrlishString())) - return {OpenResult::CannotHandle, m_designWidget->errorMessage()}; + return ResultError(m_designWidget->errorMessage()); setFilePath(absoluteFilePath); - return OpenResult::Success; + return ResultOk; } Result<> ScxmlEditorDocument::saveImpl(const FilePath &filePath, bool autoSave) diff --git a/src/plugins/scxmleditor/scxmleditordocument.h b/src/plugins/scxmleditor/scxmleditordocument.h index 70d93d2ab72..fb7521f436d 100644 --- a/src/plugins/scxmleditor/scxmleditordocument.h +++ b/src/plugins/scxmleditor/scxmleditordocument.h @@ -25,7 +25,8 @@ public: explicit ScxmlEditorDocument(Common::MainWidget *designWidget, QObject *parent = nullptr); // IDocument - OpenResult open(const Utils::FilePath &filePath, const Utils::FilePath &realFilePath) override; + Utils::Result<> open(const Utils::FilePath &filePath, + const Utils::FilePath &realFilePath) override; bool shouldAutoSave() const override; bool isSaveAsAllowed() const override; bool isModified() const override; diff --git a/src/plugins/squish/objectsmapdocument.cpp b/src/plugins/squish/objectsmapdocument.cpp index 51845e9cc80..3fb7d9052a7 100644 --- a/src/plugins/squish/objectsmapdocument.cpp +++ b/src/plugins/squish/objectsmapdocument.cpp @@ -28,11 +28,10 @@ ObjectsMapDocument::ObjectsMapDocument() connect(m_contentModel, &ObjectsMapModel::modelChanged, this, [this] { setModified(true); }); } -IDocument::OpenResult ObjectsMapDocument::open(const FilePath &fileName, - const FilePath &realFileName) +Result<> ObjectsMapDocument::open(const FilePath &fileName, const FilePath &realFileName) { - OpenResult result = openImpl(fileName, realFileName); - if (result.code == OpenResult::Success) { + Result<> result = openImpl(fileName, realFileName); + if (result.has_value()) { setFilePath(fileName); setModified(fileName != realFileName); } @@ -77,11 +76,10 @@ Result<> ObjectsMapDocument::reload(IDocument::ReloadFlag flag, IDocument::Chang if (flag == FlagIgnore) return ResultOk; emit aboutToReload(); - const OpenResult result = openImpl(filePath(), filePath()); - const bool success = result.code == OpenResult::Success; - if (success) + const Result<> result = openImpl(filePath(), filePath()); + if (result.has_value()) setModified(false); - emit reloadFinished(success); + emit reloadFinished(result.has_value()); return result; } @@ -173,28 +171,27 @@ QByteArray ObjectsMapDocument::contents() const return result; } -IDocument::OpenResult ObjectsMapDocument::openImpl(const FilePath &fileName, - const FilePath &realFileName) +Result<> ObjectsMapDocument::openImpl(const FilePath &fileName, const FilePath &realFileName) { if (fileName.isEmpty()) - return OpenResult::CannotHandle; + return ResultError("File name is empty"); // FIXME: Find somethong better QByteArray text; if (realFileName.fileName() == "objects.map") { FileReader reader; if (const Result<> res = reader.fetch(realFileName); !res) - return {OpenResult::CannotHandle, res.error()}; + return res; text = reader.text(); } else { const FilePath base = settings().squishPath(); if (base.isEmpty()) { - return {OpenResult::CannotHandle, Tr::tr("Incomplete Squish settings. " - "Missing Squish installation path.")}; + return ResultError(Tr::tr("Incomplete Squish settings. " + "Missing Squish installation path.")); } const FilePath exe = base.pathAppended("lib/exec/objectmaptool").withExecutableSuffix(); if (!exe.isExecutableFile()) - return {OpenResult::CannotHandle, Tr::tr("objectmaptool not found.")}; + return ResultError(Tr::tr("objectmaptool not found.")); Process objectMapReader; @@ -206,8 +203,8 @@ IDocument::OpenResult ObjectsMapDocument::openImpl(const FilePath &fileName, text = objectMapReader.cleanedStdOut().toUtf8(); } if (!setContents(text)) - return {OpenResult::CannotHandle, Tr::tr("Failure while parsing objects.map content.")}; - return OpenResult::Success; + return ResultError(Tr::tr("Failure while parsing objects.map content.")); + return ResultOk; } bool ObjectsMapDocument::writeFile(const Utils::FilePath &fileName) const diff --git a/src/plugins/squish/objectsmapdocument.h b/src/plugins/squish/objectsmapdocument.h index b8355464ecc..6d057b53b94 100644 --- a/src/plugins/squish/objectsmapdocument.h +++ b/src/plugins/squish/objectsmapdocument.h @@ -14,11 +14,12 @@ class ObjectsMapModel; class ObjectsMapDocument : public Core::IDocument { Q_OBJECT + public: ObjectsMapDocument(); - OpenResult open(const Utils::FilePath &fileName, - const Utils::FilePath &realFileName) override; + Utils::Result<> open(const Utils::FilePath &fileName, + const Utils::FilePath &realFileName) override; Utils::FilePath fallbackSaveAsPath() const override; QString fallbackSaveAsFileName() const override; bool isModified() const override { return m_isModified; } @@ -35,8 +36,8 @@ protected: Utils::Result<> saveImpl(const Utils::FilePath &fileName, bool autoSave) override; private: - OpenResult openImpl(const Utils::FilePath &fileName, - const Utils::FilePath &realFileName); + Utils::Result<> openImpl(const Utils::FilePath &fileName, + const Utils::FilePath &realFileName); bool buildObjectsMapTree(const QByteArray &contents); bool writeFile(const Utils::FilePath &fileName) const; void syncXMLFromEditor(); diff --git a/src/plugins/texteditor/textdocument.cpp b/src/plugins/texteditor/textdocument.cpp index bdd6781e97d..48ff564ab96 100644 --- a/src/plugins/texteditor/textdocument.cpp +++ b/src/plugins/texteditor/textdocument.cpp @@ -754,11 +754,11 @@ bool TextDocument::isModified() const return d->m_document.isModified(); } -IDocument::OpenResult TextDocument::open(const FilePath &filePath, const FilePath &realFilePath) +Result<> TextDocument::open(const FilePath &filePath, const FilePath &realFilePath) { emit aboutToOpen(filePath, realFilePath); - OpenResult result = openImpl(filePath, realFilePath, /*reload =*/ false); - if (result.code == OpenResult::Success) { + const Result<> result = openImpl(filePath, realFilePath, /*reload =*/ false); + if (result) { setMimeType(Utils::mimeTypeForFile(filePath, MimeMatchMode::MatchDefaultAndRemote).name()); setTabSettings(d->m_tabSettings); emit openFinishedSuccessfully(); @@ -766,9 +766,9 @@ IDocument::OpenResult TextDocument::open(const FilePath &filePath, const FilePat return result; } -IDocument::OpenResult TextDocument::openImpl(const FilePath &filePath, - const FilePath &realFilePath, - bool reload) +Result<> TextDocument::openImpl(const FilePath &filePath, + const FilePath &realFilePath, + bool reload) { QStringList content; QString errorString; @@ -820,15 +820,15 @@ IDocument::OpenResult TextDocument::openImpl(const FilePath &filePath, auto documentLayout = qobject_cast(d->m_document.documentLayout()); - QTC_ASSERT(documentLayout, return OpenResult::CannotHandle); + QTC_ASSERT(documentLayout, return ResultError(ResultAssert)); documentLayout->lastSaveRevision = d->m_autoSaveRevision = d->m_document.revision(); d->updateRevisions(); d->m_document.setModified(filePath != realFilePath); setFilePath(filePath); } - if (readResult == Utils::TextFileFormat::ReadIOError) - return {OpenResult::CannotHandle, errorString}; - return OpenResult::Success; + if (readResult == TextFileFormat::ReadIOError) + return ResultError(errorString); + return ResultOk; } Result<> TextDocument::reload(const QByteArray &codec) @@ -851,11 +851,11 @@ Result<> TextDocument::reload(const FilePath &realFilePath) if (documentLayout) documentLayout->documentAboutToReload(this); // removes text marks non-permanently - const OpenResult result = openImpl(filePath(), realFilePath, /*reload =*/true); + const Result<> result = openImpl(filePath(), realFilePath, /*reload =*/true); if (documentLayout) documentLayout->documentReloaded(this); // re-adds text marks - emit reloadFinished(result.code == OpenResult::Success); + emit reloadFinished(result.has_value()); return result; } diff --git a/src/plugins/texteditor/textdocument.h b/src/plugins/texteditor/textdocument.h index 65285dc1c83..0008935b355 100644 --- a/src/plugins/texteditor/textdocument.h +++ b/src/plugins/texteditor/textdocument.h @@ -117,7 +117,8 @@ public: void setFallbackSaveAsPath(const Utils::FilePath &fallbackSaveAsPath); void setFallbackSaveAsFileName(const QString &fallbackSaveAsFileName); - OpenResult open(const Utils::FilePath &filePath, const Utils::FilePath &realFilePath) override; + Utils::Result<> open(const Utils::FilePath &filePath, + const Utils::FilePath &realFilePath) override; virtual Utils::Result<> reload(); Utils::Result<> reload(const Utils::FilePath &realFilePath); @@ -169,9 +170,9 @@ protected: virtual void slotCodeStyleSettingsChanged(); // Used in CppEditorDocumet private: - OpenResult openImpl(const Utils::FilePath &filePath, - const Utils::FilePath &realFileName, - bool reload); + Utils::Result<> openImpl(const Utils::FilePath &filePath, + const Utils::FilePath &realFileName, + bool reload); void cleanWhitespace(QTextCursor &cursor, bool inEntireDocument, bool cleanIndentation); void ensureFinalNewLine(QTextCursor &cursor); void modificationChanged(bool modified); diff --git a/src/plugins/vcsbase/submiteditorfile.cpp b/src/plugins/vcsbase/submiteditorfile.cpp index 996781b9ef5..72b1f028150 100644 --- a/src/plugins/vcsbase/submiteditorfile.cpp +++ b/src/plugins/vcsbase/submiteditorfile.cpp @@ -27,22 +27,22 @@ SubmitEditorFile::SubmitEditorFile(VcsBaseSubmitEditor *editor) : this, &IDocument::contentsChanged); } -IDocument::OpenResult SubmitEditorFile::open(const FilePath &filePath, const FilePath &realFilePath) +Result<> SubmitEditorFile::open(const FilePath &filePath, const FilePath &realFilePath) { if (filePath.isEmpty()) - return OpenResult::CannotHandle; + return ResultError("File name is empty"); // FIXME: Use something better FileReader reader; if (const Result<> res = reader.fetch(realFilePath); !res) - return {OpenResult::CannotHandle, res.error()}; + return res; const QString text = QString::fromLocal8Bit(reader.text()); if (!m_editor->setFileContents(text.toUtf8())) - return OpenResult::CannotHandle; + return ResultError("Cannot set file contents"); // FIXME: Use something better setFilePath(filePath.absoluteFilePath()); setModified(filePath != realFilePath); - return OpenResult::Success; + return ResultOk; } QByteArray SubmitEditorFile::contents() const diff --git a/src/plugins/vcsbase/submiteditorfile.h b/src/plugins/vcsbase/submiteditorfile.h index 398138d6dee..9ccfe3c16d6 100644 --- a/src/plugins/vcsbase/submiteditorfile.h +++ b/src/plugins/vcsbase/submiteditorfile.h @@ -17,7 +17,8 @@ class SubmitEditorFile : public Core::IDocument public: explicit SubmitEditorFile(VcsBaseSubmitEditor *editor); - OpenResult open(const Utils::FilePath &filePath, const Utils::FilePath &realFilePath) override; + Utils::Result<> open(const Utils::FilePath &filePath, + const Utils::FilePath &realFilePath) override; QByteArray contents() const override; bool setContents(const QByteArray &contents) override;