Changeset View
Standalone View
src/filewidgets/kfilewidget.cpp
Show First 20 Lines • Show All 1321 Lines • ▼ Show 20 Line(s) | 1311 | { | |||
---|---|---|---|---|---|
1322 | } | 1322 | } | ||
1323 | } | 1323 | } | ||
1324 | 1324 | | |||
1325 | void KFileWidgetPrivate::setLocationText(const QList<QUrl> &urlList) | 1325 | void KFileWidgetPrivate::setLocationText(const QList<QUrl> &urlList) | ||
1326 | { | 1326 | { | ||
1327 | const QUrl currUrl = ops->url(); | 1327 | const QUrl currUrl = ops->url(); | ||
1328 | 1328 | | |||
1329 | if (urlList.count() > 1) { | 1329 | if (urlList.count() > 1) { | ||
1330 | // implementation must be compatible with tokenize() | ||||
1330 | QString urls; | 1331 | QString urls; | ||
1331 | for (const QUrl &url : urlList) { | 1332 | for (const QUrl &url : urlList) { | ||
1332 | urls += QStringLiteral("\"%1\"").arg(relativePathOrUrl(currUrl, url)) + QLatin1Char(' '); | 1333 | urls += QStringLiteral("\"%1\"").arg(relativePathOrUrl(currUrl, url)) + QLatin1Char(' '); | ||
1333 | } | 1334 | } | ||
1334 | urls.chop(1); | 1335 | urls.chop(1); | ||
1335 | 1336 | | |||
1336 | setDummyHistoryEntry(urls, QPixmap(), false); | 1337 | setDummyHistoryEntry(urls, QPixmap(), false); | ||
1337 | } else if (urlList.count() == 1) { | 1338 | } else if (urlList.count() == 1) { | ||
▲ Show 20 Lines • Show All 392 Lines • ▼ Show 20 Line(s) | 1730 | else { | |||
1730 | urlList = tokenize(filenames); | 1731 | urlList = tokenize(filenames); | ||
1731 | } | 1732 | } | ||
1732 | 1733 | | |||
1733 | filenames.clear(); // indicate that we parsed that one | 1734 | filenames.clear(); // indicate that we parsed that one | ||
1734 | 1735 | | |||
1735 | return urlList; | 1736 | return urlList; | ||
1736 | } | 1737 | } | ||
1737 | 1738 | | |||
1738 | // FIXME: current implementation drawback: a filename can't contain quotes | 1739 | // FIXME: current implementation drawback: a filename can't contain '" "' | ||
1739 | QList<QUrl> KFileWidgetPrivate::tokenize(const QString &line) const | 1740 | QList<QUrl> KFileWidgetPrivate::tokenize(const QString &line) const | ||
1740 | { | 1741 | { | ||
1741 | // qDebug(); | 1742 | // qDebug(); | ||
1742 | 1743 | | |||
1743 | QList<QUrl> urls; | 1744 | QList<QUrl> urls; | ||
1744 | QUrl u(ops->url()); | 1745 | QUrl u(ops->url()); | ||
1745 | if (!u.path().endsWith(QLatin1Char('/'))) { | 1746 | if (!u.path().endsWith(QLatin1Char('/'))) { | ||
1746 | u.setPath(u.path() + QLatin1Char('/')); | 1747 | u.setPath(u.path() + QLatin1Char('/')); | ||
1747 | } | 1748 | } | ||
1748 | QString name; | 1749 | QString name; | ||
1749 | 1750 | | |||
1750 | const int count = line.count(QLatin1Char('"')); | 1751 | if (!line.contains(QStringLiteral("\" \""))) { | ||
dfaure: What if I had `"file1.txt" "file2.txt"` and I manually edit that to remove the second file… | |||||
1751 | if (count == 0) { // no " " -> assume one single file | | |||
1752 | if (!QDir::isAbsolutePath(line)) { | 1752 | if (!QDir::isAbsolutePath(line)) { | ||
1753 | u = u.adjusted(QUrl::RemoveFilename); | 1753 | u = u.adjusted(QUrl::RemoveFilename); | ||
1754 | u.setPath(u.path() + line); | 1754 | u.setPath(u.path() + line); | ||
1755 | if (u.isValid()) { | 1755 | if (u.isValid()) { | ||
1756 | urls.append(u); | 1756 | urls.append(u); | ||
1757 | } | 1757 | } | ||
1758 | } else { | 1758 | } else { | ||
1759 | urls << QUrl::fromLocalFile(line); | 1759 | urls << QUrl::fromLocalFile(line); | ||
1760 | } | 1760 | } | ||
1761 | 1761 | | |||
1762 | return urls; | 1762 | return urls; | ||
1763 | } | 1763 | } | ||
1764 | 1764 | | |||
1765 | int start = 0; | 1765 | int index1 = 0, index2 = -1; | ||
1766 | int index1 = -1, index2 = -1; | | |||
1767 | while (true) { | 1766 | while (true) { | ||
1768 | index1 = line.indexOf(QLatin1Char('"'), start); | 1767 | index1 = line.indexOf(QStringLiteral("\""), index1); | ||
Isn't there a " at this position, always? Well, I guess we can keep an initial indexOf (outside the loop) in case there's a leading space (because a user manually removed the first file). So this needs additional unittests for leading space, trailing space, single-quoted-file .... and I wonder if we are OK with no longer supporting two spaces (e.g. because the user manually removed some intermediate file in the list). The separator is in fact " +" in regexp syntax, then? dfaure: Isn't there a `"` at this position, always?
Initially, at position 0, and then later, we know… | |||||
1769 | index2 = line.indexOf(QLatin1Char('"'), index1 + 1); | 1768 | // find next word separator | ||
1769 | index2 = line.indexOf(QStringLiteral("\" \""), index1 + 1); | ||||
this could move to after the if() block on the next line. dfaure: this could move to after the if() block on the next line.
If index1 < 0, there's no point in… | |||||
1770 | 1770 | | |||
1771 | if (index1 < 0 || index2 < 0) { | 1771 | if (index1 < 0) { | ||
1772 | break; | 1772 | break; | ||
1773 | } | 1773 | } | ||
1774 | if (index2 == -1) { | ||||
1775 | // when no word separator is found | ||||
1776 | // grab everything left until last character | ||||
1777 | index2 = line.length() -1; | ||||
Isn't the last character a double-quote (in the normal case)? Don't we want to stop just before it, to avoid grabbing it? Of course this opens the question of what to do if the last character is NOT a double-quote, but dfaure: Isn't the last character a double-quote (in the normal case)? Don't we want to stop just before… | |||||
1778 | } | ||||
1774 | 1779 | | |||
1775 | // get everything between the " " | 1780 | // get everything between the " " | ||
1776 | name = line.mid(index1 + 1, index2 - index1 - 1); | 1781 | name = line.mid(index1 + 1, index2 - index1 - 1); | ||
1777 | 1782 | | |||
1778 | // since we use setPath we need to do this under a temporary url | 1783 | // since we use setPath we need to do this under a temporary url | ||
1779 | QUrl _u(u); | 1784 | QUrl _u(u); | ||
1780 | QUrl currUrl(name); | 1785 | QUrl currUrl(name); | ||
1781 | 1786 | | |||
1782 | if (!QDir::isAbsolutePath(currUrl.url())) { | 1787 | if (!QDir::isAbsolutePath(currUrl.url())) { | ||
1783 | _u = _u.adjusted(QUrl::RemoveFilename); | 1788 | _u = _u.adjusted(QUrl::RemoveFilename); | ||
1784 | _u.setPath(_u.path() + name); | 1789 | _u.setPath(_u.path() + name); | ||
1785 | } else { | 1790 | } else { | ||
1786 | // we allow to insert various absolute paths like: | 1791 | // we allow to insert various absolute paths like: | ||
1787 | // "/home/foo/bar.txt" "/boot/grub/menu.lst" | 1792 | // "/home/foo/bar.txt" "/boot/grub/menu.lst" | ||
1788 | _u = currUrl; | 1793 | _u = currUrl; | ||
1789 | } | 1794 | } | ||
1790 | 1795 | | |||
1791 | if (_u.isValid()) { | 1796 | if (_u.isValid()) { | ||
1792 | urls.append(_u); | 1797 | urls.append(_u); | ||
1793 | } | 1798 | } | ||
1794 | 1799 | | |||
1795 | start = index2 + 1; | 1800 | index1 = index2 + 1; | ||
1796 | } | 1801 | } | ||
1797 | 1802 | | |||
1798 | return urls; | 1803 | return urls; | ||
1799 | } | 1804 | } | ||
1800 | 1805 | | |||
1801 | QString KFileWidget::selectedFile() const | 1806 | QString KFileWidget::selectedFile() const | ||
1802 | { | 1807 | { | ||
1803 | // qDebug(); | 1808 | // qDebug(); | ||
▲ Show 20 Lines • Show All 1162 Lines • Show Last 20 Lines |
What if I had "file1.txt" "file2.txt" and I manually edit that to remove the second file, ending up with just "file1.txt"? The double-quotes will no longer be removed, because of this modified condition.