if (m_nepomukActivated && isSearchPathIndexed()) {
url = nepomukUrlForSearching();
} else {
- url = m_searchPath;
url.setProtocol("filenamesearch");
url.addQueryItem("search", m_searchInput->text());
if (m_contentButton->isChecked()) {
url.addQueryItem("checkContent", "yes");
}
+
+ QString encodedUrl;
if (m_everywhereButton->isChecked()) {
// It is very unlikely, that the majority of Dolphins target users
// mean "the whole harddisk" instead of "my home folder" when
// selecting the "Everywhere" button.
- url.setPath(QDir::homePath());
+ encodedUrl = QDir::homePath();
+ } else {
+ encodedUrl = m_searchPath.url();
}
+ url.addQueryItem("url", encodedUrl);
}
return url;
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
***************************************************************************/
-
+
#include "filenamesearchprotocol.h"
#include <kcomponentdata.h>
#include <kdirlister.h>
#include <kfileitem.h>
+#include <kio/netaccess.h>
+#include <kio/job.h>
#include <kurl.h>
-
+#include <ktemporaryfile.h>
+
#include <QCoreApplication>
#include <QEventLoop>
#include <QRegExp>
delete m_regExp;
m_regExp = 0;
- const QStringList searchValues = url.allQueryItemValues("search");
- if (!searchValues.isEmpty()) {
- m_regExp = new QRegExp(searchValues.first(), Qt::CaseInsensitive, QRegExp::Wildcard);
+ const QString search = url.queryItem("search");
+ if (!search.isEmpty()) {
+ m_regExp = new QRegExp(search, Qt::CaseInsensitive, QRegExp::Wildcard);
}
m_checkContent = false;
- const QStringList checkContentValues = url.allQueryItemValues("checkContent");
- if (!checkContentValues.isEmpty() && (checkContentValues.first() == QLatin1String("yes"))) {
+ const QString checkContent = url.queryItem("checkContent");
+ if (checkContent == QLatin1String("yes")) {
m_checkContent = true;
}
- KUrl directory = url;
- directory.setProtocol("file");
- directory.setEncodedQuery(QByteArray());
-
- searchDirectory(directory);
+ const QString urlString = url.queryItem("url");
+ searchDirectory(KUrl(urlString));
finished();
}
dirLister->setDelayedMimeTypes(false);
dirLister->setAutoErrorHandlingEnabled(false, 0);
dirLister->openUrl(directory);
-
+
QEventLoop eventLoop;
QObject::connect(dirLister, SIGNAL(canceled()), &eventLoop, SLOT(quit()));
QObject::connect(dirLister, SIGNAL(completed()), &eventLoop, SLOT(quit()));
eventLoop.exec();
-
+
// Visualize all items that match the search pattern
QList<KUrl> pendingDirs;
const KFileItemList items = dirLister->items();
if ((m_regExp == 0) || item.name().contains(*m_regExp)) {
addItem = true;
} else if (m_checkContent && item.mimetype().startsWith(QLatin1String("text/"))) {
- addItem = containsPattern(item.url());
+ addItem = contentContainsPattern(item.url());
}
if (addItem) {
}
}
-bool FileNameSearchProtocol::containsPattern(const KUrl& fileName) const
+bool FileNameSearchProtocol::contentContainsPattern(const KUrl& fileName) const
{
Q_ASSERT(m_regExp != 0);
- QFile file(fileName.path());
+ QString path;
+ KTemporaryFile tempFile;
+
+ if (fileName.isLocalFile()) {
+ path = fileName.path();
+ } else if (tempFile.open()) {
+ KIO::Job* getJob = KIO::file_copy(fileName,
+ tempFile.fileName(),
+ -1,
+ KIO::Overwrite | KIO::HideProgressInfo);
+ if (!KIO::NetAccess::synchronousRun(getJob, 0)) {
+ // The non-local file could not be downloaded
+ return false;
+ }
+ path = tempFile.fileName();
+ } else {
+ // No temporary file could be created for downloading non-local files
+ return false;
+ }
+
+ QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return false;
- }
+ }
QTextStream in(&file);
while (!in.atEnd()) {
}
extern "C" int KDE_EXPORT kdemain( int argc, char **argv )
-{
+{
KComponentData instance("kio_search");
QCoreApplication app(argc, argv);
fprintf(stderr, "Usage: kio_filenamesearch protocol domain-socket1 domain-socket2\n");
exit(-1);
}
-
+
FileNameSearchProtocol slave(argv[2], argv[3]);
slave.dispatchLoop();
-
+
return 0;
}
#ifndef FILENAMESEARCHPROTOCOL_H
#define FILENAMESEARCHPROTOCOL_H
-
+
#include <kio/slavebase.h>
+class KFileItem;
class KUrl;
class QRegExp;
-
+
/**
* @brief Lists files where the filename matches do a given query.
*
* The query is defined as part of the "search" query item of the URL.
- * Example: The URL filenamesearch:///home/peter?search=hello lists
- * recursively all files inside the directory home/peter, that contain
- * the "hello" as part of their filename.
+ * The directory where the searching is started is defined in the "url" query
+ * item. If the query item "checkContent" is set to "yes", all files with
+ * a text MIME type will be checked for the content.
*/
class FileNameSearchProtocol : public KIO::SlaveBase {
public:
FileNameSearchProtocol(const QByteArray& pool, const QByteArray& app);
virtual ~FileNameSearchProtocol();
-
+
virtual void listDir(const KUrl& url);
private:
* @return True, if the pattern m_searchPattern is part of
* the file \a fileName.
*/
- bool containsPattern(const KUrl& fileName) const;
+ bool contentContainsPattern(const KUrl& fileName) const;
+
bool m_checkContent;
QRegExp* m_regExp;
};
-
+
#endif