PageRenderTime 66ms CodeModel.GetById 23ms RepoModel.GetById 0ms 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
Possible License(s): GPL-2.0, LGPL-2.1, CC-BY-SA-3.0, GPL-3.0
  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. #include "duckduckgo.h"
  19. #include <KDebug>
  20. #include <KToolInvocation>
  21. #include <QtCore/QTimer>
  22. #include <QtCore/QWaitCondition>
  23. #include <QtCore/QEventLoop>
  24. #include <qjson/parser.h>
  25. DuckDuckGo::DuckDuckGo(QObject *parent, const QVariantList& args)
  26. : Plasma::AbstractRunner(parent, args)
  27. {
  28. Q_UNUSED(args);
  29. setObjectName(QLatin1String("DuckDuckGo"));
  30. setIgnoredTypes(Plasma::RunnerContext::FileSystem | Plasma::RunnerContext::Directory | Plasma::RunnerContext::NetworkLocation);
  31. addSyntax(Plasma::RunnerSyntax(QLatin1String( "duckduckgo :q:" ), i18n("Lists the search entries matching the query, using DuckDuckGo search")));
  32. addSyntax(Plasma::RunnerSyntax(QLatin1String( "wolfram :q:" ), i18n("Searches using Wolfram Alpha, powered by DuckDuckGo")));
  33. addSyntax(Plasma::RunnerSyntax(QLatin1String( "define :q:" ), i18n("Defines words using dictionaries, powered by DuckDuckGo")));
  34. setSpeed(SlowSpeed);
  35. setPriority(LowPriority);
  36. qRegisterMetaType<Plasma::RunnerContext*>();
  37. KUrl url = KUrl("http://api.duckduckgo.com/?q=futurama+characters&format=json&pretty=1");
  38. // "http://api.duckduckgo.com/?q=define+ostensibly&format=json&pretty=1");
  39. m_job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo);
  40. connect(m_job, SIGNAL(result(KJob*)), this, SLOT(jobFinished(KJob*)));
  41. m_job->start();
  42. }
  43. DuckDuckGo::~DuckDuckGo()
  44. {
  45. }
  46. void DuckDuckGo::match(Plasma::RunnerContext &context)
  47. {
  48. kDebug() << "MATCH MADE, emitting matchmade";
  49. // connect(this, SIGNAL(matchMade(Plasma::RunnerContext*)), this, SLOT(startDuckDuckGoJob(Plasma::RunnerContext*)));
  50. // emit matchMade(&context);
  51. QString term = context.query();
  52. if (!term.startsWith("duckduckgo ")) {
  53. return;
  54. } else {
  55. term = term.remove("duckduckgo ");
  56. }
  57. if (!term.startsWith("wolfram ")) {
  58. return;
  59. } else {
  60. term = term.remove("wolfram ");
  61. }
  62. if (!term.startsWith("define ")) {
  63. return;
  64. } else {
  65. term = term.remove("define ");
  66. }
  67. if (term.length() < 3) {
  68. return;
  69. }
  70. if (!context.isValid()) {
  71. return;
  72. }
  73. }
  74. void DuckDuckGo::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
  75. {
  76. // Q_UNUSED(context)
  77. // const QString session = match.data().toString();
  78. // kDebug() << "Open Konsole Session " << session;
  79. //
  80. // if (!session.isEmpty()) {
  81. // QStringList args;
  82. // args << QLatin1String( "--profile" );
  83. // args << session;
  84. // kDebug() << "=== START: konsole" << args;
  85. // KToolInvocation::kdeinitExec(QLatin1String( "konsole" ), args);
  86. // }
  87. }
  88. void DuckDuckGo::startDuckDuckGoJob(Plasma::RunnerContext *context)
  89. {
  90. kDebug() << "%%%%%% DUCKDUCKGO RUNNING JOB!";
  91. }
  92. void DuckDuckGo::dataArrived(KIO::Job* job, const QByteArray& data)
  93. {
  94. // kDebug() << "DATA:" << data;
  95. if (!data.isEmpty()) {
  96. buffer << data;
  97. // parseJson(data);
  98. }
  99. // const QString term = context->query();
  100. // Plasma::QueryMatch match(this);
  101. // match.setType(Plasma::QueryMatch::PossibleMatch);
  102. //
  103. // // match.setRelevance(1.0);
  104. // // match.setIcon(m_icon);
  105. //// match.setData("TEST");
  106. // match.setText(QLatin1String( "DuckDuckGo: " ));
  107. //
  108. // context->addMatch(term, match);
  109. }
  110. void DuckDuckGo::jobFinished(KJob *job)
  111. {
  112. parseJson(m_job->data());
  113. }
  114. void DuckDuckGo::parseJson(const QByteArray& data)
  115. {
  116. kDebug() << "JSON PARSER ONLINE";
  117. QJson::Parser parser;
  118. const QVariantMap resultsMap = parser.parse(data).toMap();
  119. const QString& match = "duckduckgo";
  120. if (match == "define") {
  121. //dictionary mode
  122. kDebug() << "Heading:" << resultsMap.value("Heading");
  123. kDebug() << "AbstractSource:" << resultsMap.value("AbstractSource");
  124. kDebug() << "Abstract:" << resultsMap.value("Abstract");
  125. kDebug() << "AbstractURL:" << resultsMap.value("AbstractURL");
  126. } else if (match == "wolfram") {
  127. //wolfram mode (simple redirection, because web search providers are assholes)
  128. kDebug() << "Redirect:" << resultsMap.value("Redirect");
  129. } else if (match == "duckduckgo") {
  130. QList<QVariant> related = resultsMap.value("RelatedTopics").toList();
  131. foreach (const QVariant& variant, related) {
  132. QVariantMap submap = variant.toMap();
  133. kDebug() << "FirstURL:" << submap.value("FirstURL");
  134. kDebug() << "Text:" << submap.value("Text");
  135. kDebug() << "Icon:" << submap.value("Icon").toMap().value("URL");
  136. }
  137. }
  138. }
  139. #include "duckduckgo.moc"