Commit d198d4ef authored by Eric Jiang's avatar Eric Jiang
Browse files

DocOpenResult should hold unique_ptr to doc

This lets the created KdenliveDoc be deallocated automatically if it is
fetched with getDocument() or if it is never retrieved and DocOpenResult
is destroyed.
parent ea7395ae
Pipeline #197422 passed with stage
in 9 minutes and 53 seconds
......@@ -258,7 +258,7 @@ DocOpenResult KdenliveDoc::Open(const QUrl &url, const QString &projectFolder, Q
}
// create KdenliveDoc object
KdenliveDoc *doc = new KdenliveDoc(url, domDoc, projectFolder, undoGroup, parent);
auto doc = std::unique_ptr<KdenliveDoc>(new KdenliveDoc(url, domDoc, projectFolder, undoGroup, parent));
if (!validationResult.second.isEmpty()) {
doc->m_modifiedDecimalPoint = validationResult.second;
//doc->setModifiedDecimalPoint(validationResult.second);
......@@ -276,7 +276,7 @@ DocOpenResult KdenliveDoc::Open(const QUrl &url, const QString &projectFolder, Q
if (result.wasModified() || result.wasUpgraded()) {
doc->requestBackup();
}
result.setDocument(doc);
result.setDocument(std::move(doc));
return result;
}
......
......@@ -51,26 +51,30 @@ class Profile;
*/
class DocOpenResult {
public:
bool isSuccessful() const { return m_doc != nullptr; }
KdenliveDoc * getDocument() const { return m_doc; }
bool isSuccessful() const { return m_succeeded; }
/** @returns a unique_ptr to the KdenliveDoc, or an empty unique_ptr */
std::unique_ptr<KdenliveDoc> getDocument() { return std::move(m_doc); }
/** @returns an error message if the doc could not be opened. */
QString getError() const { return m_errorMessage; }
/** @return true if the doc was upgraded from an older version */
bool wasUpgraded() const { return m_upgraded; }
/** @return true if the doc was modified by the validator */
bool wasModified() const { return m_modified; }
void setDocument(KdenliveDoc *doc) { m_doc = doc; }
void setDocument(std::unique_ptr<KdenliveDoc> doc) {
m_doc = std::move(doc);
m_succeeded = true;
}
void setError(const QString &error) { m_errorMessage = error; }
void setUpgraded(bool upgraded) { m_upgraded = upgraded; }
void setModified(bool modified) { m_modified = modified; }
private:
KdenliveDoc *m_doc = nullptr;
std::unique_ptr<KdenliveDoc> m_doc;
QString m_errorMessage = QString();
QString m_notification = QString();
bool m_upgraded = false;
bool m_modified = false;
bool m_succeeded = false;
};
class KdenliveDoc : public QObject
......
......@@ -636,7 +636,7 @@ void ProjectManager::doOpenFile(const QUrl &url, KAutoSaveFile *stale, bool isBa
openResult = KdenliveDoc::Open(stale ? QUrl::fromLocalFile(stale->fileName()) : url,
QString(), pCore->window()->m_commandStack, true, pCore->window());
if (openResult.isSuccessful()) {
doc = openResult.getDocument();
doc = openResult.getDocument().release();
doc->requestBackup();
} else {
KMessageBox::error(pCore->window(), "Could not recover corrupted file.");
......@@ -646,7 +646,7 @@ void ProjectManager::doOpenFile(const QUrl &url, KAutoSaveFile *stale, bool isBa
KMessageBox::detailedSorry(pCore->window(), "Could not open the backup project file.", openResult.getError());
}
} else {
doc = openResult.getDocument();
doc = openResult.getDocument().release();
}
// if we could not open the file, and could not recover (or user declined), stop now
......
......@@ -339,11 +339,11 @@ TEST_CASE("Non-BMP Unicode", "[NONBMP]")
QUrl openURL = QUrl::fromLocalFile(saveFile.fileName());
QUndoGroup *undoGroup = new QUndoGroup();
undoGroup->addStack(undoStack.get());
auto openResults = KdenliveDoc::Open(openURL, QDir::temp().path(),
DocOpenResult openResults = KdenliveDoc::Open(openURL, QDir::temp().path(),
undoGroup, false, nullptr);
REQUIRE(openResults.isSuccessful() == true);
KdenliveDoc *openedDoc = openResults.getDocument();
std::unique_ptr<KdenliveDoc> openedDoc = openResults.getDocument();
QDomDocument *newDoc = &openedDoc->m_document;
auto producers = newDoc->elementsByTagName(QStringLiteral("producer"));
QDomElement textTitle;
......@@ -360,7 +360,6 @@ TEST_CASE("Non-BMP Unicode", "[NONBMP]")
auto xmldata = getProperty(textTitle, QStringLiteral("xmldata"));
REQUIRE(xmldata != nullptr);
CHECK(clipname->text().contains(emojiTestString));
delete openedDoc;
}
binModel->clean();
pCore->m_projectManager = nullptr;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment