• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kget

  • sources
  • kde-4.12
  • kdenetwork
  • kget
  • plasma
  • runner
kgetrunner.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the KDE project.
3  *
4  * Copyright (C) 2009 Tomas Van Verrewegen <tomasvanverrewegen@telenet.be>
5  * Copyright (C) 2009 Lukas Appelhans <l.appelhans@gmx.de>
6  * Copyright (C) 2010 Matthias Fuchs <mat69@gmx.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published
10  * by the Free Software Foundation; either version 2 of the License,
11  * or (at your option) any later version.
12  */
13 
14 #include "kgetrunner.h"
15 #include <QDBusInterface>
16 #include <QDBusPendingCall>
17 #include <QDBusPendingCallWatcher>
18 #include <QDBusPendingReply>
19 #include <QDBusConnectionInterface>
20 #include <QTimer>
21 #include <KNotification>
22 #include <KIconLoader>
23 #include <KDebug>
24 
25 const QString KGET_DBUS_SERVICE = "org.kde.kget";
26 const QString KGET_DBUS_PATH = "/KGet";
27 
28 KGetRunner::KGetRunner(QObject* parent, const QVariantList& args)
29  : Plasma::AbstractRunner(parent, args), m_icon("kget")
30 {
31  setObjectName("KGet");
32  addSyntax(Plasma::RunnerSyntax(":q:", i18n("Find all links in :q: and download them with KGet.")));
33 }
34 
35 
36 KGetRunner::~KGetRunner()
37 {
38 }
39 
40 void KGetRunner::init()
41 {
42  m_kget = new OrgKdeKgetMainInterface(KGET_DBUS_SERVICE, KGET_DBUS_PATH, QDBusConnection::sessionBus(), this);
43  m_interface = QDBusConnection::sessionBus().interface();
44 }
45 
46 void KGetRunner::match(Plasma::RunnerContext& context)
47 {
48  QString query = context.query();
49  m_urls = parseUrls(context.query());
50  if (!m_urls.isEmpty()) {
51  Plasma::QueryMatch match(this);
52  match.setType(Plasma::QueryMatch::PossibleMatch);
53  match.setRelevance(0.9);
54  match.setIcon(m_icon);
55  if(m_urls.size() == 1) {
56  match.setText(i18n("Download %1 with KGet.", KUrl(m_urls.first()).prettyUrl()));
57  }
58  else {
59  match.setText(i18np("Download %1 link with KGet.", "Download %1 links with KGet.", m_urls.size()));
60  }
61  context.addMatch(query, match);
62  }
63 }
64 
65 
66 void KGetRunner::run(const Plasma::RunnerContext& /*context*/, const Plasma::QueryMatch& /*match*/)
67 {
68  QDBusConnectionInterface* connection = QDBusConnection::sessionBus().interface();
69  if(connection->isServiceRegistered(KGET_DBUS_SERVICE)) {
70  // KGet is running. Make the call immediately.
71  showNewTransferDialog();
72  return;
73  }
74 
75  // KGet is not running. Ask DBus to start it.
76  connection->startService(KGET_DBUS_SERVICE);
77  if(connection->lastError().type() != QDBusError::NoError) {
78  KNotification::event(KNotification::Error,
79  i18n("<p>KGet Runner could not communicate with KGet.</p><p style=\"font-size: small;\">Response from DBus:<br/>%1</p>", connection->lastError().message()),
80  KIcon("dialog-warning").pixmap(KIconLoader::SizeSmall)/*, 0, KNotification::Persistant*/);
81  return;
82  }
83 
84  // Set a timer to make the call when KGet has been started.
85  // This might still fail if it takes too long to start KGet.
86  // For me, the 1000ms delay is mooooore than sufficient.
87  QTimer::singleShot(1000, this, SLOT(showNewTransferDialog()));
88 }
89 
90 void KGetRunner::showNewTransferDialog()
91 {
92  QDBusPendingCall call = m_kget->asyncCall("showNewTransferDialog", m_urls);
93  QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(call, this);
94  QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), this, SLOT(callFinished(QDBusPendingCallWatcher*)));
95 
96  m_urls.clear();
97 }
98 
99 void KGetRunner::callFinished(QDBusPendingCallWatcher* call)
100 {
101  QDBusPendingReply<> reply = *call;
102 
103  // TODO Remove the check for QDBusError::NoReply when NewTransferDialog is fixed to show asynchronously.
104  if(!reply.isValid() && (reply.error().type() != QDBusError::NoReply)) {
105  // Send a notification about the error to the user.
106  KNotification::event(KNotification::Error,
107  i18n("<p>KGet Runner could not communicate with KGet.</p><p style=\"font-size: small;\">Response from DBus:<br/>%1</p>", reply.error().message()),
108  KIcon("dialog-warning").pixmap(KIconLoader::SizeSmall)/*, 0, KNotification::Persistant*/);
109  }
110 }
111 
112 QStringList KGetRunner::parseUrls(const QString& text) const
113 {
114  QStringList urls;
115  // We could use QString::split() or similar, but a simple split on whitespace will
116  // keep us from finding many possible matches (eg: "http://... or =http://...).
117  // So, for now, let's traverse the query with a simple regexp.
118  QRegExp re("\\b\\S+");
119  int i = re.indexIn(text);
120  while(i != -1) {
121  // We check if the match is a valid URL, if the protocol is handled by KGet,
122  // and if the host is not empty, otherwise "http://" would also be matched.
123  KUrl url(re.cap());
124  if (m_interface->isServiceRegistered(KGET_DBUS_SERVICE)
125  ? m_kget->isSupported(url.url()).value()
126  : (url.isValid() && url.hasHost())) {
127  urls << url.url();
128 
129  // continue searching after last match...
130  i = re.indexIn(text, i + re.matchedLength());
131  } else {
132  // if the match is not a URL, continue searching from next character...
133  i = re.indexIn(text, i + 1);
134  }
135  }
136  return urls;
137 }
138 
139 
140 #include "kgetrunner.moc"
kgetrunner.h
KGetRunner::~KGetRunner
~KGetRunner()
Definition: kgetrunner.cpp:36
KGetRunner::init
void init()
Definition: kgetrunner.cpp:40
QObject
KGetRunner::run
void run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
Definition: kgetrunner.cpp:66
KGetRunner::KGetRunner
KGetRunner(QObject *parent, const QVariantList &args)
Definition: kgetrunner.cpp:28
KGET_DBUS_PATH
const QString KGET_DBUS_PATH
Definition: kgetrunner.cpp:26
KGET_DBUS_SERVICE
const QString KGET_DBUS_SERVICE
Definition: kgetrunner.cpp:25
KGetRunner::match
void match(Plasma::RunnerContext &context)
Definition: kgetrunner.cpp:46
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:17 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kget

Skip menu "kget"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal