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

Language C++ Lines 170
MD5 Hash 2a761fbc88172205ac6c7e5d32b5b921 Estimated Cost $2,256 (why?)
Repository git://pkgs.fedoraproject.org/kdeplasma-addons View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/******************************************************************************
 *  Copyright (C) 2012 by Shaun Reich <sreich@kde.org                         *
 *                                                                            *
 *  This library is free software; you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as published  *
 *  by the Free Software Foundation; either version 2 of the License or (at   *
 *  your option) any later version.                                           *
 *                                                                            *
 *  This library is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
 *  Library General Public License for more details.                          *
 *                                                                            *
 *  You should have received a copy of the GNU Lesser General Public License  *
 *  along with this library; see the file COPYING.LIB.                        *
 *  If not, see <http://www.gnu.org/licenses/>.                               *
 *****************************************************************************/

#include "duckduckgo.h"

#include <KDebug>
#include <KToolInvocation>

#include <QtCore/QTimer>
#include <QtCore/QWaitCondition>
#include <QtCore/QEventLoop>
#include <qjson/parser.h>

DuckDuckGo::DuckDuckGo(QObject *parent, const QVariantList& args)
    : Plasma::AbstractRunner(parent, args)
{
    Q_UNUSED(args);
    setObjectName(QLatin1String("DuckDuckGo"));
    setIgnoredTypes(Plasma::RunnerContext::FileSystem | Plasma::RunnerContext::Directory | Plasma::RunnerContext::NetworkLocation);

    addSyntax(Plasma::RunnerSyntax(QLatin1String( "duckduckgo :q:" ), i18n("Lists the search entries matching the query, using DuckDuckGo search")));
    addSyntax(Plasma::RunnerSyntax(QLatin1String( "wolfram :q:" ), i18n("Searches using Wolfram Alpha, powered by DuckDuckGo")));
    addSyntax(Plasma::RunnerSyntax(QLatin1String( "define :q:" ), i18n("Defines words using dictionaries, powered by DuckDuckGo")));
    setSpeed(SlowSpeed);
    setPriority(LowPriority);

    qRegisterMetaType<Plasma::RunnerContext*>();

    KUrl url = KUrl("http://api.duckduckgo.com/?q=futurama+characters&format=json&pretty=1");
       // "http://api.duckduckgo.com/?q=define+ostensibly&format=json&pretty=1");

    m_job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo);
    connect(m_job, SIGNAL(result(KJob*)), this, SLOT(jobFinished(KJob*)));
    m_job->start();
}

DuckDuckGo::~DuckDuckGo()
{
}

void DuckDuckGo::match(Plasma::RunnerContext &context)
{
    kDebug() << "MATCH MADE, emitting matchmade";
//    connect(this, SIGNAL(matchMade(Plasma::RunnerContext*)), this, SLOT(startDuckDuckGoJob(Plasma::RunnerContext*)));
 //   emit matchMade(&context);

    QString term = context.query();

    if (!term.startsWith("duckduckgo ")) {
        return;
    } else {
        term = term.remove("duckduckgo ");
    }

    if (!term.startsWith("wolfram ")) {
        return;
    } else {
        term = term.remove("wolfram ");
    }

    if (!term.startsWith("define ")) {
        return;
    } else {
        term = term.remove("define ");
    }


    if (term.length() < 3) {
        return;
    }

    if (!context.isValid()) {
        return;
    }
}

void DuckDuckGo::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
{
//    Q_UNUSED(context)
//    const QString session = match.data().toString();
//    kDebug() << "Open Konsole Session " << session;
//
//    if (!session.isEmpty()) {
//        QStringList args;
//        args << QLatin1String( "--profile" );
//        args << session;
//        kDebug() << "=== START: konsole" << args;
//        KToolInvocation::kdeinitExec(QLatin1String( "konsole" ), args);
//    }
}

void DuckDuckGo::startDuckDuckGoJob(Plasma::RunnerContext *context)
{

    kDebug() << "%%%%%% DUCKDUCKGO RUNNING JOB!";
}

void DuckDuckGo::dataArrived(KIO::Job* job, const QByteArray& data)
{
//    kDebug()  << "DATA:" << data;
    if (!data.isEmpty()) {
        buffer << data;
//        parseJson(data);
    }
//    const QString term = context->query();
//    Plasma::QueryMatch match(this);
//    match.setType(Plasma::QueryMatch::PossibleMatch);
//
//    //  match.setRelevance(1.0);
//    //  match.setIcon(m_icon);
////    match.setData("TEST");
//    match.setText(QLatin1String( "DuckDuckGo: " ));
//
//    context->addMatch(term, match);

}

void DuckDuckGo::jobFinished(KJob *job)
{
    parseJson(m_job->data());
}

void DuckDuckGo::parseJson(const QByteArray& data)
{
    kDebug() << "JSON PARSER ONLINE";
    QJson::Parser parser;
    const QVariantMap resultsMap = parser.parse(data).toMap();

    const QString& match = "duckduckgo";

    if (match == "define") {
        //dictionary mode
        kDebug() << "Heading:" << resultsMap.value("Heading");
        kDebug() << "AbstractSource:" << resultsMap.value("AbstractSource");
        kDebug() << "Abstract:" << resultsMap.value("Abstract");
        kDebug() << "AbstractURL:" << resultsMap.value("AbstractURL");
    } else if (match == "wolfram") {
        //wolfram mode (simple redirection, because web search providers are assholes)
        kDebug() << "Redirect:" << resultsMap.value("Redirect");
    } else if (match == "duckduckgo") {
        QList<QVariant> related = resultsMap.value("RelatedTopics").toList();

        foreach (const QVariant& variant, related) {
            QVariantMap submap = variant.toMap();

            kDebug() << "FirstURL:" << submap.value("FirstURL");
            kDebug() << "Text:" << submap.value("Text");
            kDebug() << "Icon:" << submap.value("Icon").toMap().value("URL");
        }
    }
}


#include "duckduckgo.moc"
Back to Top