PageRenderTime 2ms CodeModel.GetById 60ms app.highlight 16ms RepoModel.GetById 65ms app.codeStats 0ms

/kdeplasma-addons-4.8.97/runners/duckduckgo/duckduckgo.cpp

#
C++ | 169 lines | 94 code | 30 blank | 45 comment | 17 complexity | 2a761fbc88172205ac6c7e5d32b5b921 MD5 | raw file
  1/******************************************************************************
  2 *  Copyright (C) 2012 by Shaun Reich <sreich@kde.org                         *
  3 *                                                                            *
  4 *  This library is free software; you can redistribute it and/or modify      *
  5 *  it under the terms of the GNU Lesser General Public License as published  *
  6 *  by the Free Software Foundation; either version 2 of the License or (at   *
  7 *  your option) any later version.                                           *
  8 *                                                                            *
  9 *  This library is distributed in the hope that it will be useful,           *
 10 *  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 11 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
 12 *  Library General Public License for more details.                          *
 13 *                                                                            *
 14 *  You should have received a copy of the GNU Lesser General Public License  *
 15 *  along with this library; see the file COPYING.LIB.                        *
 16 *  If not, see <http://www.gnu.org/licenses/>.                               *
 17 *****************************************************************************/
 18
 19#include "duckduckgo.h"
 20
 21#include <KDebug>
 22#include <KToolInvocation>
 23
 24#include <QtCore/QTimer>
 25#include <QtCore/QWaitCondition>
 26#include <QtCore/QEventLoop>
 27#include <qjson/parser.h>
 28
 29DuckDuckGo::DuckDuckGo(QObject *parent, const QVariantList& args)
 30    : Plasma::AbstractRunner(parent, args)
 31{
 32    Q_UNUSED(args);
 33    setObjectName(QLatin1String("DuckDuckGo"));
 34    setIgnoredTypes(Plasma::RunnerContext::FileSystem | Plasma::RunnerContext::Directory | Plasma::RunnerContext::NetworkLocation);
 35
 36    addSyntax(Plasma::RunnerSyntax(QLatin1String( "duckduckgo :q:" ), i18n("Lists the search entries matching the query, using DuckDuckGo search")));
 37    addSyntax(Plasma::RunnerSyntax(QLatin1String( "wolfram :q:" ), i18n("Searches using Wolfram Alpha, powered by DuckDuckGo")));
 38    addSyntax(Plasma::RunnerSyntax(QLatin1String( "define :q:" ), i18n("Defines words using dictionaries, powered by DuckDuckGo")));
 39    setSpeed(SlowSpeed);
 40    setPriority(LowPriority);
 41
 42    qRegisterMetaType<Plasma::RunnerContext*>();
 43
 44    KUrl url = KUrl("http://api.duckduckgo.com/?q=futurama+characters&format=json&pretty=1");
 45       // "http://api.duckduckgo.com/?q=define+ostensibly&format=json&pretty=1");
 46
 47    m_job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo);
 48    connect(m_job, SIGNAL(result(KJob*)), this, SLOT(jobFinished(KJob*)));
 49    m_job->start();
 50}
 51
 52DuckDuckGo::~DuckDuckGo()
 53{
 54}
 55
 56void DuckDuckGo::match(Plasma::RunnerContext &context)
 57{
 58    kDebug() << "MATCH MADE, emitting matchmade";
 59//    connect(this, SIGNAL(matchMade(Plasma::RunnerContext*)), this, SLOT(startDuckDuckGoJob(Plasma::RunnerContext*)));
 60 //   emit matchMade(&context);
 61
 62    QString term = context.query();
 63
 64    if (!term.startsWith("duckduckgo ")) {
 65        return;
 66    } else {
 67        term = term.remove("duckduckgo ");
 68    }
 69
 70    if (!term.startsWith("wolfram ")) {
 71        return;
 72    } else {
 73        term = term.remove("wolfram ");
 74    }
 75
 76    if (!term.startsWith("define ")) {
 77        return;
 78    } else {
 79        term = term.remove("define ");
 80    }
 81
 82
 83    if (term.length() < 3) {
 84        return;
 85    }
 86
 87    if (!context.isValid()) {
 88        return;
 89    }
 90}
 91
 92void DuckDuckGo::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
 93{
 94//    Q_UNUSED(context)
 95//    const QString session = match.data().toString();
 96//    kDebug() << "Open Konsole Session " << session;
 97//
 98//    if (!session.isEmpty()) {
 99//        QStringList args;
100//        args << QLatin1String( "--profile" );
101//        args << session;
102//        kDebug() << "=== START: konsole" << args;
103//        KToolInvocation::kdeinitExec(QLatin1String( "konsole" ), args);
104//    }
105}
106
107void DuckDuckGo::startDuckDuckGoJob(Plasma::RunnerContext *context)
108{
109
110    kDebug() << "%%%%%% DUCKDUCKGO RUNNING JOB!";
111}
112
113void DuckDuckGo::dataArrived(KIO::Job* job, const QByteArray& data)
114{
115//    kDebug()  << "DATA:" << data;
116    if (!data.isEmpty()) {
117        buffer << data;
118//        parseJson(data);
119    }
120//    const QString term = context->query();
121//    Plasma::QueryMatch match(this);
122//    match.setType(Plasma::QueryMatch::PossibleMatch);
123//
124//    //  match.setRelevance(1.0);
125//    //  match.setIcon(m_icon);
126////    match.setData("TEST");
127//    match.setText(QLatin1String( "DuckDuckGo: " ));
128//
129//    context->addMatch(term, match);
130
131}
132
133void DuckDuckGo::jobFinished(KJob *job)
134{
135    parseJson(m_job->data());
136}
137
138void DuckDuckGo::parseJson(const QByteArray& data)
139{
140    kDebug() << "JSON PARSER ONLINE";
141    QJson::Parser parser;
142    const QVariantMap resultsMap = parser.parse(data).toMap();
143
144    const QString& match = "duckduckgo";
145
146    if (match == "define") {
147        //dictionary mode
148        kDebug() << "Heading:" << resultsMap.value("Heading");
149        kDebug() << "AbstractSource:" << resultsMap.value("AbstractSource");
150        kDebug() << "Abstract:" << resultsMap.value("Abstract");
151        kDebug() << "AbstractURL:" << resultsMap.value("AbstractURL");
152    } else if (match == "wolfram") {
153        //wolfram mode (simple redirection, because web search providers are assholes)
154        kDebug() << "Redirect:" << resultsMap.value("Redirect");
155    } else if (match == "duckduckgo") {
156        QList<QVariant> related = resultsMap.value("RelatedTopics").toList();
157
158        foreach (const QVariant& variant, related) {
159            QVariantMap submap = variant.toMap();
160
161            kDebug() << "FirstURL:" << submap.value("FirstURL");
162            kDebug() << "Text:" << submap.value("Text");
163            kDebug() << "Icon:" << submap.value("Icon").toMap().value("URL");
164        }
165    }
166}
167
168
169#include "duckduckgo.moc"