• 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
  • extensions
  • konqueror
kget_plug_in.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2 
3  Copyright (C) 2002 Patrick Charbonnier <pch@valleeurpe.net>
4  Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
5  Copyright (C) 2007 Urs Wolfer <uwolfer @ kde.org>
6  Copyright (C) 2010 Dawit Alemayehu <adawit@kde.org>
7  Copyright (C) 2010 Matthias Fuchs <mat69@gmx.net>
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public
11  License as published by the Free Software Foundation; either
12  version 2 of the License, or (at your option) any later version.
13 */
14 
15 #include "kget_plug_in.h"
16 
17 #include "kget_interface.h"
18 
19 #include <KDE/KActionCollection>
20 #include <KDE/KToggleAction>
21 #include <KDE/KActionMenu>
22 #include <KDE/KIconLoader>
23 #include <KDE/KComponentData>
24 #include <KDE/KLocale>
25 #include <KDE/KMessageBox>
26 #include <KDE/KMenu>
27 #include <KDE/KRun>
28 #include <KDE/KIcon>
29 #include <KDE/KToolInvocation>
30 #include <KDE/KGenericFactory>
31 #include <KDE/KProtocolInfo>
32 #include <KDE/KFileItem>
33 #include <KDE/KParts/Part>
34 #include <KDE/KParts/PartManager>
35 #include <KDE/KParts/HtmlExtension>
36 #include <KDE/KParts/FileInfoExtension>
37 
38 #include <QtDBus/QDBusConnection>
39 
40 
41 #define QL1S(x) QLatin1String(x)
42 
43 K_PLUGIN_FACTORY(KGetPluginFactory, registerPlugin<KGetPlugin>();)
44 K_EXPORT_PLUGIN(KGetPluginFactory("kgetplugin"))
45 
46 static QWidget* partWidget(QObject* obj)
47 {
48  KParts::ReadOnlyPart* part = qobject_cast<KParts::ReadOnlyPart*>(obj);
49  return part ? part->widget() : 0;
50 }
51 
52 KGetPlugin::KGetPlugin(QObject *parent, const QVariantList&)
53  :KParts::Plugin(parent)
54 {
55  KActionMenu *menu = new KActionMenu(KIcon("kget"), i18n("Download Manager"), actionCollection());
56  actionCollection()->addAction("kget_menu", menu);
57 
58  menu->setDelayed( false );
59  connect( menu->menu(), SIGNAL(aboutToShow()), SLOT(showPopup()));
60 
61  m_dropTargetAction = new KToggleAction(i18n("Show Drop Target"), actionCollection());
62 
63  connect(m_dropTargetAction, SIGNAL(triggered()), this, SLOT(slotShowDrop()));
64  actionCollection()->addAction(QL1S("show_drop"), m_dropTargetAction);
65  menu->addAction(m_dropTargetAction);
66 
67  QAction *showLinksAction = actionCollection()->addAction(QL1S("show_links"));
68  showLinksAction->setText(i18n("List All Links"));
69  connect(showLinksAction, SIGNAL(triggered()), SLOT(slotShowLinks()));
70  menu->addAction(showLinksAction);
71 
72  QAction *showSelectedLinksAction = actionCollection()->addAction(QL1S("show_selected_links"));
73  showSelectedLinksAction->setText(i18n("List Selected Links"));
74  connect(showSelectedLinksAction, SIGNAL(triggered()), SLOT(slotShowSelectedLinks()));
75  menu->addAction(showSelectedLinksAction);
76 
77  // Hide this plugin if the parent part does not support either
78  // The FileInfo or Html extensions...
79  if (!KParts::HtmlExtension::childObject(parent) && !KParts::FileInfoExtension::childObject(parent))
80  menu->setVisible(false);
81 }
82 
83 
84 KGetPlugin::~KGetPlugin()
85 {
86 }
87 
88 static bool hasDropTarget()
89 {
90  bool found = false;
91 
92  if (QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.kget")) {
93  OrgKdeKgetMainInterface kgetInterface("org.kde.kget", "/KGet", QDBusConnection::sessionBus());
94  QDBusReply<bool> reply = kgetInterface.dropTargetVisible();
95  if (reply.isValid()) {
96  found = reply.value();
97  }
98  }
99 
100  return found;
101 }
102 
103 void KGetPlugin::showPopup()
104 {
105  // Check for HtmlExtension support...
106  KParts::HtmlExtension* htmlExtn = KParts::HtmlExtension::childObject(parent());
107  if (htmlExtn) {
108  KParts::SelectorInterface* selector = qobject_cast<KParts::SelectorInterface*>(htmlExtn);
109  if (selector) {
110  m_dropTargetAction->setChecked(hasDropTarget());
111  const KParts::SelectorInterface::QueryMethods methods = selector->supportedQueryMethods();
112  bool enable = (methods & KParts::SelectorInterface::EntireContent);
113  actionCollection()->action(QL1S("show_links"))->setEnabled(enable);
114  enable = (htmlExtn->hasSelection() && (methods & KParts::SelectorInterface::SelectedContent));
115  actionCollection()->action(QL1S("show_selected_links"))->setEnabled(enable);
116  enable = (actionCollection()->action(QL1S("show_links"))->isEnabled() ||
117  actionCollection()->action(QL1S("show_selected_links"))->isEnabled());
118  actionCollection()->action(QL1S("show_drop"))->setEnabled(enable);
119  return;
120  }
121  }
122 
123  // Check for FileInfoExtension support...
124  KParts::FileInfoExtension* fileinfoExtn = KParts::FileInfoExtension::childObject(parent());
125  if (fileinfoExtn) {
126  m_dropTargetAction->setChecked(hasDropTarget());
127  const KParts::FileInfoExtension::QueryModes modes = fileinfoExtn->supportedQueryModes();
128  bool enable = (modes & KParts::FileInfoExtension::AllItems);
129  actionCollection()->action(QL1S("show_links"))->setEnabled(enable);
130  enable = (fileinfoExtn->hasSelection() && (modes & KParts::FileInfoExtension::SelectedItems));
131  actionCollection()->action(QL1S("show_selected_links"))->setEnabled(enable);
132  enable = (actionCollection()->action(QL1S("show_links"))->isEnabled() ||
133  actionCollection()->action(QL1S("show_selected_links"))->isEnabled());
134  actionCollection()->action(QL1S("show_drop"))->setEnabled(enable);
135  return;
136  }
137 
138  actionCollection()->action(QL1S("show_selected_links"))->setEnabled(false);
139  actionCollection()->action(QL1S("show_links"))->setEnabled(false);
140  actionCollection()->action(QL1S("show_drop"))->setEnabled(false);
141  if (m_dropTargetAction->isChecked())
142  m_dropTargetAction->setChecked(false);
143 }
144 
145 void KGetPlugin::slotShowDrop()
146 {
147  if (!QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.kget")) {
148  KRun::runCommand("kget --showDropTarget --hideMainWindow", "kget", "kget", partWidget(parent()));
149  } else {
150  OrgKdeKgetMainInterface kgetInterface("org.kde.kget", "/KGet", QDBusConnection::sessionBus());
151  kgetInterface.setDropTargetVisible(m_dropTargetAction->isChecked());
152  }
153 }
154 
155 void KGetPlugin::slotShowLinks()
156 {
157  getLinks(false);
158 }
159 
160 void KGetPlugin::slotShowSelectedLinks()
161 {
162  getLinks(true);
163 }
164 
165 void KGetPlugin::slotImportLinks()
166 {
167  if (m_linkList.isEmpty()) {
168  KMessageBox::sorry(partWidget(parent()),
169  i18n("No downloadable links were found."),
170  i18n("No Links"));
171  return;
172  }
173 
174  // Remove any duplicates links from the list...
175  m_linkList.removeDuplicates();
176 
177  if (!QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.kget") &&
178  KToolInvocation::kdeinitExecWait("kget") != 0) {
179  KMessageBox::sorry(partWidget(parent()),
180  i18n("Unable to communicate with the KGet download manager."),
181  i18n("Communication Error"));
182  return;
183  }
184 
185  OrgKdeKgetMainInterface kgetInterface("org.kde.kget", "/KGet", QDBusConnection::sessionBus());
186  kgetInterface.importLinks(m_linkList);
187 }
188 
189 void KGetPlugin::getLinks(bool selectedOnly)
190 {
191  KParts::HtmlExtension* htmlExtn = KParts::HtmlExtension::childObject(parent());
192  if (htmlExtn) {
193  KParts::SelectorInterface* selector = qobject_cast<KParts::SelectorInterface*>(htmlExtn);
194  if (selector) {
195  m_linkList.clear();
196  const QUrl baseUrl = htmlExtn->baseUrl();
197  const QString query = QL1S("a[href], img[src], audio[src], video[src], embed[src], object[data]");
198  const KParts::SelectorInterface::QueryMethod method = (selectedOnly ? KParts::SelectorInterface::SelectedContent:
199  KParts::SelectorInterface::EntireContent);
200  const QList<KParts::SelectorInterface::Element> elements = selector->querySelectorAll(query, method);
201  QString attr;
202  Q_FOREACH(const KParts::SelectorInterface::Element& element, elements) {
203  if (element.hasAttribute(QL1S("href")))
204  attr = QL1S("href");
205  else if (element.hasAttribute(QL1S("src")))
206  attr = QL1S("src");
207  else if (element.hasAttribute(QL1S("data")))
208  attr = QL1S("data");
209  const KUrl resolvedUrl (baseUrl.resolved(element.attribute(attr)));
210  // Only select valid and non-local links for download...
211  if (resolvedUrl.isValid() && !resolvedUrl.isLocalFile() && !resolvedUrl.host().isEmpty()) {
212  if (element.hasAttribute(QL1S("type")))
213  m_linkList << QString(QL1S("url ") + resolvedUrl.url() + QL1S(" type ") + element.attribute(QL1S("type")));
214  else
215  m_linkList << resolvedUrl.url();
216 
217  }
218  }
219  }
220  slotImportLinks();
221  }
222 
223  KParts::FileInfoExtension* fileinfoExtn = KParts::FileInfoExtension::childObject(parent());
224  if (fileinfoExtn) {
225  m_linkList.clear();
226  const KParts::FileInfoExtension::QueryMode mode = (selectedOnly ? KParts::FileInfoExtension::SelectedItems:
227  KParts::FileInfoExtension::AllItems);
228  const KFileItemList items = fileinfoExtn->queryFor(mode);
229  Q_FOREACH(const KFileItem& item, items) {
230  const KUrl url = item.url();
231  // Only select valid and non local links for download...
232  if (item.isReadable() && item.isFile() && !item.isLocalFile() && !url.host().isEmpty()) {
233  if (item.mimetype().isEmpty())
234  m_linkList << url.url();
235  else
236  m_linkList << QString(QL1S("url ") + url.url() + QL1S(" type ") + item.mimetype());
237  }
238  }
239  slotImportLinks();
240  }
241 }
242 
243 #include "kget_plug_in.moc"
kget_plug_in.h
KGetPlugin::KGetPlugin
KGetPlugin(QObject *parent, const QVariantList &args)
Definition: plugin.cpp:16
hasDropTarget
static bool hasDropTarget()
Definition: kget_plug_in.cpp:88
QWidget
QObject
partWidget
static QWidget * partWidget(QObject *obj)
Definition: kget_plug_in.cpp:46
KGetPlugin::~KGetPlugin
virtual ~KGetPlugin()
Definition: plugin.cpp:22
QL1S
#define QL1S(x)
Definition: kget_plug_in.cpp:41
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