- SearchPlugin checks for Content-type -header, so now it is able to download
[qtrapids] / src / plugins / searchplugin / SearchPlugin.cpp
1 /***************************************************************************
2  *   Copyright (C) 2009 by Lassi Väätämöinen   *
3  *   lassi.vaatamoinen@ixonos.com   *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include <QDebug>
22 #include <QtCore/qplugin.h>
23 #include <QVBoxLayout>
24 #include <QHBoxLayout>
25 #include <QComboBox>
26 #include <QLineEdit>
27 #include <QPushButton>
28 #include <QUrl>
29 #include <QDir>
30 #include <QFileInfo>
31 #include <QWebView>
32 #include <QWebPage>
33 #include <QNetworkReply>
34 #include <QDomDocument>
35
36 #include "SearchPlugin.h"
37 #include "DownloadManager.h"
38
39 namespace qtrapids
40 {
41         const QString ENGINES_DIR = "engines";
42         const QString DESCRIPTION_FILENAME = "opensearch.xml";
43         const QString SEARCH_TERMS_STRING = "{searchTerms}";
44         
45         
46         SearchPlugin::SearchPlugin() : 
47                 comboBox_(NULL), searchLine_(NULL),
48                 searchButton_(NULL), result_(NULL), 
49                 dlManager_(NULL), host_(NULL)
50         {
51                 // TODO: Add back/forward/refresh -buttons to widget to allow some browsing functionality
52         }
53         
54         
55         void SearchPlugin::initialize(PluginHostInterface* host, Info info)
56         {
57                 host_ = host;
58                 
59                 if (host_ != NULL) {
60                 
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");
68                         
69                         hbox->addWidget(searchLine_);
70                         hbox->addWidget(searchButton_);
71                         vbox->addWidget(comboBox_);
72                         vbox->addLayout(hbox);
73                         pluginWidget->setLayout(vbox);
74         
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);
81                         }
82                         
83                         host_->setGui(pluginWidget, qtrapids::PluginHostInterface::BASE_WIDGET);
84                 }
85         }
86         
87         QWidget* SearchPlugin::getGui()
88         {
89                 return NULL;
90         }
91
92
93         void SearchPlugin::on_searchButton_clicked()
94         {
95                 int i = comboBox_->currentIndex();
96                 QString tmp = engineTemplates_.at(i);
97                 
98                 i = tmp.indexOf(SEARCH_TERMS_STRING);
99                 tmp.replace(i, SEARCH_TERMS_STRING.length(), searchLine_->text());
100                 
101                 QUrl searchUrl(tmp);
102                 qDebug() << searchUrl;
103                 result_ = new QWebView;
104                 
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);
108                 
109                 // Get the network access manager for examining HTTP replies.
110                 QNetworkAccessManager *netwAccessManager_ = resultPage->networkAccessManager();
111                 
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*)));
115                 
116                 result_->load(searchUrl);
117                 
118                 on_searchResult((QWidget*)result_);
119         }
120         
121         
122         void SearchPlugin::on_loadFinished(bool ok)
123         {
124 #ifdef QTRAPIDS_DEBUG
125                 if (ok) {
126                         qDebug() << "on_loadFinished(): success";
127                 }
128 #endif
129         }
130         
131         
132         void SearchPlugin::on_networkReplyFinished(QNetworkReply* reply)
133         {
134                 
135                 qDebug() << "on_networkReplyFinished()";
136                 QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
137                 QString filename;
138                 QByteArray replyData;
139                 QUrl url = reply->url();
140                 
141                 qDebug() << "on_networkReplyFinished():" << url;
142                 
143                 // If content type is torrent data, read reply data:
144                 if (contentType == "application/x-bittorrent") {
145                         
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="));
150                                 if (pos != -1) {
151                                         QString name = value.mid(pos + 9);
152                                         if (name.startsWith(QLatin1Char('"')) && name.endsWith(QLatin1Char('"')))
153                                                 name = name.mid(1, name.size() - 2);
154                                         filename = name;
155                                 }
156                         // No content-disposition header, use last part (the filename) of URL as filename:
157                         } else {
158                                 QString path = url.path();
159                                 QFileInfo fInfo(path);
160                                 filename = fInfo.fileName();
161                         }
162                         
163                         // Destroy ongoing download, if any.
164                         if (dlManager_) {
165                                 delete dlManager_;
166                                 dlManager_ = NULL;
167                         }
168                 
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)));
176                         dlManager_->start();
177                         
178                 }
179         }
180         
181         
182         void SearchPlugin::on_searchResult(QWidget* resultWidget)
183         {
184 #ifdef QTRAPIDS_DEBUG
185                 qDebug() << "on_searchResult()";
186 #endif
187                 if (host_) {
188                         host_->addPluginWidget(resultWidget, qtrapids::PluginHostInterface::TAB_PAGE);
189                 }
190         }
191
192
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)
196         {
197                 qDebug() << "on_linkClicked():" << url;
198                 
199                 if (url.isValid()) {
200                         result_->load(url);
201                 }
202         }
203         
204         
205         void SearchPlugin::on_downloadFinished(QString filepath)
206         {
207 #ifdef QTRAPIDS_DEBUG
208                 qDebug() << "TORRENT DOWNLOADED: " << filepath;
209 #endif
210                 delete dlManager_;
211                 dlManager_ = NULL;
212                 if (host_) {
213                         host_->eventRequest(QVariant(filepath), qtrapids::PluginHostInterface::OPEN_FILE);
214                 }
215         }
216
217
218         void SearchPlugin::ParseSearchEngineDescriptions(const QDir& dir)
219         {
220                 foreach (QString dirName, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
221                 
222                         QFile file(dir.path() + "/" + dirName + "/" + DESCRIPTION_FILENAME);
223                         
224                         if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
225                                 qWarning() << "Unable to open " << DESCRIPTION_FILENAME << " for reading in " << dir.path();
226                                 continue;
227                         }
228                         
229                         // Parse the XML file to DOM document.
230                         QDomDocument document;
231                         
232                         // Second parameter: nameSpaceProcessing = false
233                         if (!document.setContent(&file, false)) {
234                                 qWarning() << "Unable to parse " << DESCRIPTION_FILENAME << " in " << dirName;
235                         } else {
236                                 QDomNodeList urlElements = document.elementsByTagName("Url");
237                                 QDomNodeList shortNameElements = document.elementsByTagName("ShortName");
238                                 
239                                 QDomNode n = urlElements.item(0);
240                                 QDomNode m;
241                                 QDomNamedNodeMap attribMap;
242                                 if (n.hasAttributes()) {
243                                         attribMap = n.attributes();
244                                         m = attribMap.namedItem("template");
245                                         engineTemplates_.push_back(m.nodeValue());
246                                 }
247                                 
248                                 n = shortNameElements.item(0);
249                                 if (n.hasChildNodes()) {
250                                         m = n.firstChild();
251                                         comboBox_->addItem(m.nodeValue());
252                                 }
253                         }
254                 }
255                 /// @todo save parsed xml elements <Shortname> and <Url template="<foo>"> to a model which is displayed in combobox.
256         }
257
258 } // namespace qtrapids
259
260 Q_EXPORT_PLUGIN2(searchplugin, qtrapids::SearchPlugin)