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 <QNetworkReply>
34 #include <QDomDocument>
36 #include "SearchPlugin.h"
37 #include "DownloadManager.h"
41 const QString ENGINES_DIR = "engines";
42 const QString DESCRIPTION_FILENAME = "opensearch.xml";
43 const QString SEARCH_TERMS_STRING = "{searchTerms}";
46 SearchPlugin::SearchPlugin() :
47 comboBox_(NULL), searchLine_(NULL),
48 searchButton_(NULL), result_(NULL),
49 dlManager_(NULL), host_(NULL)
51 // TODO: Add back/forward/refresh -buttons to widget to allow some browsing functionality
55 void SearchPlugin::initialize(PluginHostInterface* host, Info info)
61 // Build up the plugin widget:
62 QWidget *pluginWidget = new QWidget;
63 QVBoxLayout *vbox = new QVBoxLayout;
64 QHBoxLayout *hbox = new QHBoxLayout;
65 comboBox_ = new QComboBox;
66 searchLine_ = new QLineEdit;
67 searchButton_ = new QPushButton("Search");
69 hbox->addWidget(searchLine_);
70 hbox->addWidget(searchButton_);
71 vbox->addWidget(comboBox_);
72 vbox->addLayout(hbox);
73 pluginWidget->setLayout(vbox);
75 connect(searchButton_, SIGNAL(clicked()), this, SLOT(on_searchButton_clicked()));
76 //connect(this, SIGNAL(searchResult(QWidget*)), this, SLOT(on_searchResult(QWidget*)));
77 qDebug() << info.directory;
78 QDir dir(info.directory);
79 if (dir.cd(ENGINES_DIR)) {
80 ParseSearchEngineDescriptions(dir);
83 host_->setGui(pluginWidget, qtrapids::PluginHostInterface::BASE_WIDGET);
87 QWidget* SearchPlugin::getGui()
93 void SearchPlugin::on_searchButton_clicked()
95 int i = comboBox_->currentIndex();
96 QString tmp = engineTemplates_.at(i);
98 i = tmp.indexOf(SEARCH_TERMS_STRING);
99 tmp.replace(i, SEARCH_TERMS_STRING.length(), searchLine_->text());
102 qDebug() << searchUrl;
103 result_ = new QWebView;
105 // Get underlying QWebPage and change link delegation, so we can meddle with links in on_linkClicked().
106 QWebPage *resultPage = result_->page();
107 resultPage->setLinkDelegationPolicy(QWebPage::DelegateExternalLinks);
109 // Get the network access manager for examining HTTP replies.
110 QNetworkAccessManager *netwAccessManager_ = resultPage->networkAccessManager();
112 connect(resultPage, SIGNAL(linkClicked(const QUrl&)), this, SLOT(on_linkClicked(const QUrl&)));
113 connect(result_, SIGNAL(loadFinished(bool)), this, SLOT(on_loadFinished(bool)));
114 connect(netwAccessManager_, SIGNAL(finished(QNetworkReply*)), this, SLOT(on_networkReplyFinished(QNetworkReply*)));
116 result_->load(searchUrl);
118 on_searchResult((QWidget*)result_);
122 void SearchPlugin::on_loadFinished(bool ok)
124 #ifdef QTRAPIDS_DEBUG
126 qDebug() << "on_loadFinished(): success";
132 void SearchPlugin::on_networkReplyFinished(QNetworkReply* reply)
135 qDebug() << "on_networkReplyFinished()";
136 QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
138 QByteArray replyData;
139 QUrl url = reply->url();
141 qDebug() << "on_networkReplyFinished():" << url;
143 // If content type is torrent data, read reply data:
144 if (contentType == "application/x-bittorrent") {
146 // If HTTP-response has Content-Disposition: -header, then we must use that as filename.
147 if (reply->hasRawHeader("Content-Disposition")) { // NOTE this code block taken from kwebpage.cpp
148 const QString value = QLatin1String(reply->rawHeader("Content-Disposition"));
149 const int pos = value.indexOf(QLatin1String("filename="));
151 QString name = value.mid(pos + 9);
152 if (name.startsWith(QLatin1Char('"')) && name.endsWith(QLatin1Char('"')))
153 name = name.mid(1, name.size() - 2);
156 // No content-disposition header, use last part (the filename) of URL as filename:
158 QString path = url.path();
159 QFileInfo fInfo(path);
160 filename = fInfo.fileName();
163 // Destroy ongoing download, if any.
169 /// @todo Is this a bit of a hack now; we get the reply and check Content-Type,
170 /// The download is then started afterwards, so we get unecessarily one extra
171 /// HTTP-response here.
172 /// @todo Could this whole content-type checking be logically moved to DownloadManager?
173 // Start downloading Torrent file.
174 dlManager_ = new DownloadManager(url, "/tmp/" + filename);
175 connect(dlManager_, SIGNAL(finished(QString)), this, SLOT(on_downloadFinished(QString)));
182 void SearchPlugin::on_searchResult(QWidget* resultWidget)
184 #ifdef QTRAPIDS_DEBUG
185 qDebug() << "on_searchResult()";
188 host_->addPluginWidget(resultWidget, qtrapids::PluginHostInterface::TAB_PAGE);
193 /// @todo It may be that we don't actually need link delegation, because we check response Content-Type header
194 /// on_linkClicked() in that case unnecessary function
195 void SearchPlugin::on_linkClicked(const QUrl& url)
197 qDebug() << "on_linkClicked():" << url;
205 void SearchPlugin::on_downloadFinished(QString filepath)
207 #ifdef QTRAPIDS_DEBUG
208 qDebug() << "TORRENT DOWNLOADED: " << filepath;
213 host_->eventRequest(QVariant(filepath), qtrapids::PluginHostInterface::OPEN_FILE);
218 void SearchPlugin::ParseSearchEngineDescriptions(const QDir& dir)
220 foreach (QString dirName, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
222 QFile file(dir.path() + "/" + dirName + "/" + DESCRIPTION_FILENAME);
224 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
225 qWarning() << "Unable to open " << DESCRIPTION_FILENAME << " for reading in " << dir.path();
229 // Parse the XML file to DOM document.
230 QDomDocument document;
232 // Second parameter: nameSpaceProcessing = false
233 if (!document.setContent(&file, false)) {
234 qWarning() << "Unable to parse " << DESCRIPTION_FILENAME << " in " << dirName;
236 QDomNodeList urlElements = document.elementsByTagName("Url");
237 QDomNodeList shortNameElements = document.elementsByTagName("ShortName");
239 QDomNode n = urlElements.item(0);
241 QDomNamedNodeMap attribMap;
242 if (n.hasAttributes()) {
243 attribMap = n.attributes();
244 m = attribMap.namedItem("template");
245 engineTemplates_.push_back(m.nodeValue());
248 n = shortNameElements.item(0);
249 if (n.hasChildNodes()) {
251 comboBox_->addItem(m.nodeValue());
255 /// @todo save parsed xml elements <Shortname> and <Url template="<foo>"> to a model which is displayed in combobox.
258 } // namespace qtrapids
260 Q_EXPORT_PLUGIN2(searchplugin, qtrapids::SearchPlugin)