diff --git a/autotests/parttest.h b/autotests/parttest.h --- a/autotests/parttest.h +++ b/autotests/parttest.h @@ -33,6 +33,7 @@ void testOpenUrlArguments(); void testAutomaticMimeType(); + void testEmptyUrlAfterCloseUrl(); void testToolbarVisibility(); }; diff --git a/autotests/parttest.cpp b/autotests/parttest.cpp --- a/autotests/parttest.cpp +++ b/autotests/parttest.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -170,6 +171,23 @@ delete part; } +void PartTest::testEmptyUrlAfterCloseUrl() +{ + TestPart *part = new TestPart(nullptr, nullptr); + + QVERIFY(part->openUrl(QUrl::fromLocalFile(QFINDTESTDATA("notepad.desktop")))); + QSignalSpy spy(part, &KParts::ReadOnlyPart::urlChanged); + QVERIFY(part->openUrl(QUrl::fromLocalFile(QFINDTESTDATA("parttest.cpp")))); + QVERIFY(!part->url().isEmpty()); + QCOMPARE(spy.count(), 1); + spy.clear(); + QVERIFY(part->closeUrl()); + QVERIFY(part->url().isEmpty()); + QCOMPARE(spy.count(), 1); + + delete part; +} + #include #include #include diff --git a/src/readonlypart.cpp b/src/readonlypart.cpp --- a/src/readonlypart.cpp +++ b/src/readonlypart.cpp @@ -137,7 +137,10 @@ d->m_bAutoDetectedMime = false; } OpenUrlArguments args = d->m_arguments; - if (!closeUrl()) { + d->m_closeUrlFromOpenUrl = true; + const bool closed = closeUrl(); + d->m_closeUrlFromOpenUrl = false; + if (!closed) { return false; } d->m_arguments = args; @@ -238,13 +241,18 @@ } } +// TODO KF6: make it non-virtual so that we can just call +// closeUrlImpl() + setUrl(QUrl()) and get rid of m_closeUrlFromOpenUrl. bool ReadOnlyPart::closeUrl() { Q_D(ReadOnlyPart); abortLoad(); //just in case d->m_arguments = KParts::OpenUrlArguments(); + if (!d->m_closeUrlFromOpenUrl) { + setUrl(QUrl()); + } if (d->m_bTemp) { QFile::remove(d->m_file); diff --git a/src/readonlypart_p.h b/src/readonlypart_p.h --- a/src/readonlypart_p.h +++ b/src/readonlypart_p.h @@ -50,6 +50,7 @@ m_duringSaveAs = false; m_bTemp = false; m_bAutoDetectedMime = false; + m_closeUrlFromOpenUrl = false; } ~ReadOnlyPartPrivate() @@ -79,6 +80,8 @@ // whether the mimetype in the arguments was detected by the part itself bool m_bAutoDetectedMime : 1; + // Whether we are calling closeUrl() from openUrl(). + bool m_closeUrlFromOpenUrl; /** * Remote (or local) url - the one displayed to the user.