#include <QLineEdit>
#include <QPushButton>
#include <QUrl>
+#include <QFileInfo>
#include <QWebView>
-
+#include <QWebPage>
#include "SearchPlugin.h"
+#include "DownloadManager.h"
namespace qtrapids
{
SearchPlugin::SearchPlugin() :
- comboBox_(NULL), searchLine_(NULL), searchButton_(NULL), host_(NULL)
+ comboBox_(NULL), searchLine_(NULL),
+ searchButton_(NULL), result_(NULL),
+ dlManager_(NULL), host_(NULL)
{
- // TODO: Parse engine descriptions.
- // -Add engines to model
+ // TODO: Parse search engine XML(?) descriptions.
+ // -Add search engines to model
// -Show model in comboBox
+ // Add back/forward/refresh -buttons to widget to allow some browsing
}
host_ = host;
if (host_ != NULL) {
-
QWidget *pluginWidget = new QWidget;
QVBoxLayout *vbox = new QVBoxLayout;
QHBoxLayout *hbox = new QHBoxLayout;
return NULL;
}
+
void SearchPlugin::on_searchButton_clicked()
{
QUrl searchUrl(QString("http://www.google.fi/search?q="
+ searchLine_->text()));
qDebug() << searchUrl;
- QWebView *result = new QWebView;
- result->load(searchUrl);
+ result_ = new QWebView;
+
+ // Get underlying QWebPage and change link delegation, so we can meddle with links in on_linkClicked().
+ QWebPage *resultPage = result_->page();
+ resultPage->setLinkDelegationPolicy(QWebPage::DelegateExternalLinks);
+
+ connect(resultPage, SIGNAL(linkClicked(const QUrl&)), this, SLOT(on_linkClicked(const QUrl&)));
+ connect(result_, SIGNAL(loadFinished(bool)), this, SLOT(on_loadFinished(bool)));
+
+ result_->load(searchUrl);
- on_searchResult((QWidget*)result);
+ on_searchResult((QWidget*)result_);
+ }
+
+
+ void SearchPlugin::on_loadFinished(bool ok)
+ {
+ if (ok) {
+ qDebug() << "on_loadFinished()";
+ }
}
+
void SearchPlugin::on_searchResult(QWidget* resultWidget)
{
qDebug() << "on_searchResult()";
}
}
+ void SearchPlugin::on_linkClicked(const QUrl& url)
+ {
+ // Get the path part of URL (the part after the host part)
+ QString path = url.path();
+ QFileInfo fInfo(path);
+
+ // Check path suffix. If it is .torrent, we should download:
+ /// @todo We should also check MIME-type, instead of relying on file suffix.
+ /// @todo Also, after downloading, the torrent bencoding validity should be checked at plugin host..
+ if (fInfo.suffix() == "torrent") {
+ qDebug() << "IS TORRENT";
+ QString filename = fInfo.fileName();
+
+ // Destroy ongoing download, if any.
+ if (dlManager_) {
+ delete dlManager_;
+ dlManager_ = NULL;
+ }
+
+ // Start downloading Torrent file.
+ dlManager_ = new DownloadManager(url, "/tmp/" + filename);
+ connect(dlManager_, SIGNAL(finished(QString)), this, SLOT(on_downloadFinished(QString)));
+ dlManager_->start();
+ } else {
+ // If was not .torrent -file, check URL validity and load the page as usual.
+ if (url.isValid()) {
+ result_->load(url);
+ }
+ }
+ }
+
+ void SearchPlugin::on_downloadFinished(QString filepath)
+ {
+ qDebug() << "TORRENT DOWNLOADED: " << filepath;
+ delete dlManager_;
+ dlManager_ = NULL;
+ host_->eventRequest(QVariant(filepath), qtrapids::PluginHostInterface::OPEN_FILE);
+ }
+
} // namespace qtrapids
Q_EXPORT_PLUGIN2(searchplugin, qtrapids::SearchPlugin)