X-Git-Url: http://git.maemo.org/git/?p=qtrapids;a=blobdiff_plain;f=src%2Fplugins%2Fsearchplugin%2FSearchPlugin.cpp;fp=src%2Fplugins%2Fsearchplugin%2FSearchPlugin.cpp;h=add01e0e9a7e0d903447dd524228b65a8617e14c;hp=4440063a70f80eb8557f072c0e40d910e411d4b5;hb=a988ddfbc3be7773bccd4017a70fcd8ce990d8c7;hpb=a25be1792ef730256ea47908df24b1bbbded553d diff --git a/src/plugins/searchplugin/SearchPlugin.cpp b/src/plugins/searchplugin/SearchPlugin.cpp index 4440063..add01e0 100644 --- a/src/plugins/searchplugin/SearchPlugin.cpp +++ b/src/plugins/searchplugin/SearchPlugin.cpp @@ -26,15 +26,22 @@ #include #include #include +#include #include #include #include +#include #include "SearchPlugin.h" #include "DownloadManager.h" namespace qtrapids { + const QString ENGINES_DIR = "engines"; + const QString DESCRIPTION_FILENAME = "opensearch.xml"; + const QString SEARCH_TERMS_STRING = "{searchTerms}"; + + SearchPlugin::SearchPlugin() : comboBox_(NULL), searchLine_(NULL), searchButton_(NULL), result_(NULL), @@ -47,11 +54,13 @@ namespace qtrapids } - void SearchPlugin::initialize(PluginHostInterface* host) + void SearchPlugin::initialize(PluginHostInterface* host, Info info) { host_ = host; if (host_ != NULL) { + + // Build up the plugin widget: QWidget *pluginWidget = new QWidget; QVBoxLayout *vbox = new QVBoxLayout; QHBoxLayout *hbox = new QHBoxLayout; @@ -67,6 +76,11 @@ namespace qtrapids connect(searchButton_, SIGNAL(clicked()), this, SLOT(on_searchButton_clicked())); //connect(this, SIGNAL(searchResult(QWidget*)), this, SLOT(on_searchResult(QWidget*))); + qDebug() << info.directory; + QDir dir(info.directory); + if (dir.cd(ENGINES_DIR)) { + ParseSearchEngineDescriptions(dir); + } host_->setGui(pluginWidget, qtrapids::PluginHostInterface::BASE_WIDGET); } @@ -76,12 +90,16 @@ namespace qtrapids { return NULL; } - - + + void SearchPlugin::on_searchButton_clicked() { - QUrl searchUrl(QString("http://www.google.fi/search?q=" - + searchLine_->text())); + + int i = comboBox_->currentIndex(); + QString tmp = engineTemplates_.at(i); + i = tmp.indexOf(SEARCH_TERMS_STRING); + tmp.replace(i, SEARCH_TERMS_STRING.length(), searchLine_->text()); + QUrl searchUrl(tmp); qDebug() << searchUrl; result_ = new QWebView; @@ -125,7 +143,11 @@ namespace qtrapids 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 We should actually check for MIME-type application/x-bittorrent in HTTP response, + /// instead of relying on file suffix. + /// e.g. link + ///http://www.bitenova.org/download.php?id=c84375141231eef49fc6c55e6347ba4f4a623a05&name=Nero_Linux_3.5.1.0__(Debian)_deutsch + /// will not work. /// @todo Also, after downloading, the torrent bencoding validity should be checked at plugin host.. if (fInfo.suffix() == "torrent") { #ifdef QTRAPIDS_DEBUG @@ -143,6 +165,7 @@ namespace qtrapids 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()) { @@ -161,6 +184,61 @@ namespace qtrapids host_->eventRequest(QVariant(filepath), qtrapids::PluginHostInterface::OPEN_FILE); } + + void SearchPlugin::ParseSearchEngineDescriptions(const QDir& dir) + { + foreach (QString dirName, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) { + + QFile file(dir.path() + "/" + dirName + "/" + DESCRIPTION_FILENAME); + + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + qWarning() << "Unable to open " << DESCRIPTION_FILENAME << " for reading in " << dir.path(); + continue; + } + + // Parse the XML file to DOM document. + QDomDocument document; + + // Second parameter: nameSpaceProcessing = false + if (!document.setContent(&file, false)) { + qWarning() << "Unable to parse " << DESCRIPTION_FILENAME << " in " << dirName; + } else { + QDomNodeList urlElements = document.elementsByTagName("Url"); + QDomNodeList shortNameElements = document.elementsByTagName("ShortName"); + + QDomNode n = urlElements.item(0); + QDomNode m; + QDomNamedNodeMap attribMap; + if (n.hasAttributes()) { + attribMap = n.attributes(); + m = attribMap.namedItem("template"); + engineTemplates_.push_back(m.nodeValue()); + } + + n = shortNameElements.item(0); + if (n.hasChildNodes()) { + m = n.firstChild(); + comboBox_->addItem(m.nodeValue()); + } + } + } + + + /// @todo save parsed xml elements and to a model which is displayed in combobox. + } + + /* + QDomElement docElem = document.documentElement(); + QDomNode n = docElem.firstChild(); + while(!n.isNull()) { + QDomElement e = n.toElement(); // try to convert the node to an element. + if(!e.isNull()) { + qDebug() << qPrintable(e.tagName()); // the node really is an element. + } + n = n.nextSibling(); + } + */ + } // namespace qtrapids Q_EXPORT_PLUGIN2(searchplugin, qtrapids::SearchPlugin)