-Added some error checking to torrent file writing
[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 <QFileInfo>
30 #include <QWebView>
31 #include <QWebPage>
32
33 #include "SearchPlugin.h"
34 #include "DownloadManager.h"
35
36 namespace qtrapids
37 {
38         SearchPlugin::SearchPlugin() : 
39                 comboBox_(NULL), searchLine_(NULL),
40                 searchButton_(NULL), result_(NULL), 
41                 dlManager_(NULL), host_(NULL)
42         {
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
47         
48         }
49         
50         void SearchPlugin::initialize(PluginHostInterface* host)
51         {
52                 host_ = host;
53                 
54                 if (host_ != NULL) {
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");
61                         
62                         hbox->addWidget(searchLine_);
63                         hbox->addWidget(searchButton_);
64                         vbox->addWidget(comboBox_);
65                         vbox->addLayout(hbox);
66                         pluginWidget->setLayout(vbox);
67         
68                         connect(searchButton_, SIGNAL(clicked()), this, SLOT(on_searchButton_clicked()));
69                         //connect(this, SIGNAL(searchResult(QWidget*)), this, SLOT(on_searchResult(QWidget*)));
70                         
71                         host_->setGui(pluginWidget, qtrapids::PluginHostInterface::BASE_WIDGET);
72                 }
73         }
74         
75         QWidget* SearchPlugin::getGui()
76         {
77                 return NULL;
78         }
79         
80         
81         void SearchPlugin::on_searchButton_clicked()
82         {
83                 QUrl searchUrl(QString("http://www.google.fi/search?q="
84                         + searchLine_->text()));
85                 qDebug() << searchUrl;
86                 result_ = new QWebView;
87                 
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);
91                 
92                 connect(resultPage, SIGNAL(linkClicked(const QUrl&)), this, SLOT(on_linkClicked(const QUrl&)));
93                 connect(result_, SIGNAL(loadFinished(bool)), this, SLOT(on_loadFinished(bool)));
94         
95                 result_->load(searchUrl);
96                 
97                 on_searchResult((QWidget*)result_);
98         }
99         
100         
101         void SearchPlugin::on_loadFinished(bool ok)
102         {
103 #ifdef QTRAPIDS_DEBUG
104                 if (ok) {
105                         qDebug() << "on_loadFinished(): success";
106                 }
107 #endif
108         }
109         
110         
111         void SearchPlugin::on_searchResult(QWidget* resultWidget)
112         {
113 #ifdef QTRAPIDS_DEBUG
114                 qDebug() << "on_searchResult()";
115 #endif
116                 if (host_) {
117                         host_->addPluginWidget(resultWidget, qtrapids::PluginHostInterface::TAB_PAGE);
118                 }
119         }
120
121         void SearchPlugin::on_linkClicked(const QUrl& url)
122         {
123                 // Get the path part of URL (the part after the host part) 
124                 QString path = url.path();
125                 QFileInfo fInfo(path);
126                 
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";
133 #endif
134                         QString filename = fInfo.fileName();
135                         
136                         // Destroy ongoing download, if any.
137                         if (dlManager_) {
138                                 delete dlManager_;
139                                 dlManager_ = NULL;
140                         }
141                         
142                         // Start downloading Torrent file.
143                         dlManager_ = new DownloadManager(url, "/tmp/" + filename);
144                         connect(dlManager_, SIGNAL(finished(QString)), this, SLOT(on_downloadFinished(QString)));
145                         dlManager_->start();
146                 } else {
147                         // If was not .torrent -file, check URL validity and load the page as usual.
148                         if (url.isValid()) {
149                                 result_->load(url);
150                         }
151                 }
152         }
153         
154         void SearchPlugin::on_downloadFinished(QString filepath)
155         {
156 #ifdef QTRAPIDS_DEBUG
157                 qDebug() << "TORRENT DOWNLOADED: " << filepath;
158 #endif
159                 delete dlManager_;
160                 dlManager_ = NULL;
161                 host_->eventRequest(QVariant(filepath), qtrapids::PluginHostInterface::OPEN_FILE);
162         }
163
164 } // namespace qtrapids
165
166 Q_EXPORT_PLUGIN2(searchplugin, qtrapids::SearchPlugin)