1 /***************************************************************************
2 * Copyright (C) 2009 by Lassi Väätämöinen *
3 * lassi.vaatamoinen@ixonos.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
22 #include <QtCore/qplugin.h>
23 #include <QVBoxLayout>
24 #include <QHBoxLayout>
27 #include <QPushButton>
33 #include "SearchPlugin.h"
34 #include "DownloadManager.h"
38 SearchPlugin::SearchPlugin() :
39 comboBox_(NULL), searchLine_(NULL),
40 searchButton_(NULL), result_(NULL),
41 dlManager_(NULL), host_(NULL)
43 // TODO: Parse search engine XML(?) descriptions.
44 // -Add search engines to model
45 // -Show model in comboBox
46 // Add back/forward/refresh -buttons to widget to allow some browsing
50 void SearchPlugin::initialize(PluginHostInterface* host)
55 QWidget *pluginWidget = new QWidget;
56 QVBoxLayout *vbox = new QVBoxLayout;
57 QHBoxLayout *hbox = new QHBoxLayout;
58 comboBox_ = new QComboBox;
59 searchLine_ = new QLineEdit;
60 searchButton_ = new QPushButton("Search");
62 hbox->addWidget(searchLine_);
63 hbox->addWidget(searchButton_);
64 vbox->addWidget(comboBox_);
65 vbox->addLayout(hbox);
66 pluginWidget->setLayout(vbox);
68 connect(searchButton_, SIGNAL(clicked()), this, SLOT(on_searchButton_clicked()));
69 //connect(this, SIGNAL(searchResult(QWidget*)), this, SLOT(on_searchResult(QWidget*)));
71 host_->setGui(pluginWidget, qtrapids::PluginHostInterface::BASE_WIDGET);
75 QWidget* SearchPlugin::getGui()
81 void SearchPlugin::on_searchButton_clicked()
83 QUrl searchUrl(QString("http://www.google.fi/search?q="
84 + searchLine_->text()));
85 qDebug() << searchUrl;
86 result_ = new QWebView;
88 // Get underlying QWebPage and change link delegation, so we can meddle with links in on_linkClicked().
89 QWebPage *resultPage = result_->page();
90 resultPage->setLinkDelegationPolicy(QWebPage::DelegateExternalLinks);
92 connect(resultPage, SIGNAL(linkClicked(const QUrl&)), this, SLOT(on_linkClicked(const QUrl&)));
93 connect(result_, SIGNAL(loadFinished(bool)), this, SLOT(on_loadFinished(bool)));
95 result_->load(searchUrl);
97 on_searchResult((QWidget*)result_);
101 void SearchPlugin::on_loadFinished(bool ok)
103 #ifdef QTRAPIDS_DEBUG
105 qDebug() << "on_loadFinished(): success";
111 void SearchPlugin::on_searchResult(QWidget* resultWidget)
113 #ifdef QTRAPIDS_DEBUG
114 qDebug() << "on_searchResult()";
117 host_->addPluginWidget(resultWidget, qtrapids::PluginHostInterface::TAB_PAGE);
121 void SearchPlugin::on_linkClicked(const QUrl& url)
123 // Get the path part of URL (the part after the host part)
124 QString path = url.path();
125 QFileInfo fInfo(path);
127 // Check path suffix. If it is .torrent, we should download:
128 /// @todo We should also check MIME-type, instead of relying on file suffix.
129 /// @todo Also, after downloading, the torrent bencoding validity should be checked at plugin host..
130 if (fInfo.suffix() == "torrent") {
131 #ifdef QTRAPIDS_DEBUG
132 qDebug() << "IS TORRENT";
134 QString filename = fInfo.fileName();
136 // Destroy ongoing download, if any.
142 // Start downloading Torrent file.
143 dlManager_ = new DownloadManager(url, "/tmp/" + filename);
144 connect(dlManager_, SIGNAL(finished(QString)), this, SLOT(on_downloadFinished(QString)));
147 // If was not .torrent -file, check URL validity and load the page as usual.
154 void SearchPlugin::on_downloadFinished(QString filepath)
156 #ifdef QTRAPIDS_DEBUG
157 qDebug() << "TORRENT DOWNLOADED: " << filepath;
161 host_->eventRequest(QVariant(filepath), qtrapids::PluginHostInterface::OPEN_FILE);
164 } // namespace qtrapids
166 Q_EXPORT_PLUGIN2(searchplugin, qtrapids::SearchPlugin)