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.h b/src/readonlypart.h --- a/src/readonlypart.h +++ b/src/readonlypart.h @@ -111,7 +111,9 @@ public: /** - * Returns the URL currently opened in this part. + * Returns the URL currently opened in (or being opened by) this part. + * @note url() is not cleared if openUrl() fails to load the URL. + * Call closeUrl() if you need to explicitly reset it. * * @return The current URL. */ @@ -123,6 +125,7 @@ * automatically in this case). * If the current URL is not fully loaded yet, aborts loading. * Deletes the temporary file used when the url is remote. + * Resets the current url() to QUrl(). * @return always true, but the return value exists for reimplementations */ virtual bool closeUrl(); 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; @@ -245,6 +248,9 @@ 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.