Add KFileWidget::setSelectedUrl()
ClosedPublic

Authored by dfaure on Mar 4 2017, 7:54 PM.

Details

Summary

It turns out that setSelection() cannot take both relative paths (filenames)
and absolute URLs as input. "a:b" can be both, and URLs for files called "a#b"
were handled wrongly.

CCBUG: 376365

Diff Detail

Repository
R241 KIO
Branch
master
Lint
No Linters Available
Unit
No Unit Test Coverage
dfaure created this revision.Mar 4 2017, 7:54 PM
Restricted Application added a project: Frameworks. · View Herald TranscriptMar 4 2017, 7:54 PM
fvogt requested changes to this revision.Mar 4 2017, 8:16 PM
fvogt added a subscriber: fvogt.
fvogt added inline comments.
src/filewidgets/kfilewidget.h
169

This function only works correctly for URLs, as filenames can contain ':'s.
However, for those setSelectedUrl is the right function, so I'd mark this
function as deprecated

This revision now requires changes to proceed.Mar 4 2017, 8:16 PM
dfaure added inline comments.Mar 5 2017, 1:29 PM
src/filewidgets/kfilewidget.h
169

Very good point.

But there might be other users of this class who need the ability to select by filename.
I think this means we have two options:

  • changing setSelection to only handle filenames, including those with a ':' (bad, not backwards compatible)
  • adding a setSelectedFileName(const QString &fileName), and then we can deprecated setSelection(QString).
fvogt added inline comments.Mar 5 2017, 1:39 PM
src/filewidgets/kfilewidget.h
169

Currently that works with setSelectedUrl as well as it handles relative urls (basically escaped filenames) as well, but it would definitely be a good shortcut, so I'm for option #2

dfaure added inline comments.Mar 5 2017, 1:43 PM
src/filewidgets/kfilewidget.h
169

I guess you mean "with setSelection".
I made setSelectedUrl handle only full URLs.

fvogt added inline comments.Mar 5 2017, 1:45 PM
src/filewidgets/kfilewidget.h
169

No, I meant setSelectedUrl indeed. It works for relative URLs as well.
Both the function itself and setLocationText check url.isRelative()

dfaure added inline comments.Mar 5 2017, 1:50 PM
src/filewidgets/kfilewidget.h
169

Ah right, OK.

But people are not going to URL-escape filenames and then we'll have the same bug again ;-)

They'll write QUrl(fileName) which won't work for this purpose, for instance for files called "a:b" and "a#b" it will lead to QUrl("a:b") or QUrl("a#b") which will be parsed as "scheme==a" and "fragment==b" respectively,

dfaure added a comment.Mar 7 2017, 9:09 AM

I just found that this bug was very likely introduced by the fix for https://bugs.kde.org/show_bug.cgi?id=369216, proving that a method that takes a badly defined QString leads to some code (kdialog) calling it with a path and other code (filedialog integration plugin) calling it with URLs, which only leads to ping-pong fixes ;)

As much as I like ping-pong, I'll write unittests so that it stops there ;)

dfaure updated this revision to Diff 12255.Mar 7 2017, 9:38 AM
dfaure edited edge metadata.

deprecate setSelection

fvogt accepted this revision.Mar 7 2017, 9:50 AM

Looks good to me, although a new setSelectedFilename method would be nice to have

This revision is now accepted and ready to land.Mar 7 2017, 9:50 AM

I thought about it, but the call sites I found didn't actually need that. So unless you know of one, I changed my mind to "let's wait for someone to request it". Although I guess it won't happen because people will just pass a QUrl instead ;-)

fvogt requested changes to this revision.Mar 7 2017, 11:51 AM

Yes, but constructing a relative QUrl isn't really that easy to do (definitely not obvious at that) and so future users will either pass a filename with QUrl(filename) (broken) or store the absolute path themselves, which is unnecessary memory use and more complicated.

I just noticed that with KIOFILEWIDGETS_NO_DEPRECATED defined the header file will not include the declaration but the cpp file still has the definition of the method, so it won't compile...

This revision now requires changes to proceed.Mar 7 2017, 11:51 AM
dfaure updated this revision to Diff 12280.Mar 8 2017, 8:24 AM
dfaure edited edge metadata.

Add test for relative URL and document the proper way to do it

fvogt accepted this revision.Mar 8 2017, 12:09 PM
This revision is now accepted and ready to land.Mar 8 2017, 12:09 PM
dfaure closed this revision.Mar 8 2017, 12:11 PM
dfaure added a comment.Mar 8 2017, 1:01 PM

BTW don't port the callers to this new method yet, we'll have to wait until the KF5 minimum requirement is raised in kdialog and plasma-integration. I made a TODO for June ;)