17 #include "kget_interface.h"
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>
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>
38 #include <QtDBus/QDBusConnection>
41 #define QL1S(x) QLatin1String(x)
43 K_PLUGIN_FACTORY(KGetPluginFactory, registerPlugin<KGetPlugin>();)
44 K_EXPORT_PLUGIN(KGetPluginFactory(
"kgetplugin"))
48 KParts::ReadOnlyPart* part = qobject_cast<KParts::ReadOnlyPart*>(obj);
49 return part ? part->widget() : 0;
53 :KParts::Plugin(parent)
55 KActionMenu *menu =
new KActionMenu(KIcon(
"kget"), i18n(
"Download Manager"), actionCollection());
56 actionCollection()->addAction(
"kget_menu", menu);
58 menu->setDelayed(
false );
59 connect( menu->menu(), SIGNAL(aboutToShow()), SLOT(showPopup()));
61 m_dropTargetAction =
new KToggleAction(i18n(
"Show Drop Target"), actionCollection());
63 connect(m_dropTargetAction, SIGNAL(triggered()),
this, SLOT(slotShowDrop()));
64 actionCollection()->addAction(
QL1S(
"show_drop"), m_dropTargetAction);
65 menu->addAction(m_dropTargetAction);
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);
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);
79 if (!KParts::HtmlExtension::childObject(parent) && !KParts::FileInfoExtension::childObject(parent))
80 menu->setVisible(
false);
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();
103 void KGetPlugin::showPopup()
106 KParts::HtmlExtension* htmlExtn = KParts::HtmlExtension::childObject(parent());
108 KParts::SelectorInterface* selector = qobject_cast<KParts::SelectorInterface*>(htmlExtn);
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);
124 KParts::FileInfoExtension* fileinfoExtn = KParts::FileInfoExtension::childObject(parent());
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);
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);
145 void KGetPlugin::slotShowDrop()
147 if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(
"org.kde.kget")) {
148 KRun::runCommand(
"kget --showDropTarget --hideMainWindow",
"kget",
"kget",
partWidget(parent()));
150 OrgKdeKgetMainInterface kgetInterface(
"org.kde.kget",
"/KGet", QDBusConnection::sessionBus());
151 kgetInterface.setDropTargetVisible(m_dropTargetAction->isChecked());
155 void KGetPlugin::slotShowLinks()
160 void KGetPlugin::slotShowSelectedLinks()
165 void KGetPlugin::slotImportLinks()
167 if (m_linkList.isEmpty()) {
169 i18n(
"No downloadable links were found."),
175 m_linkList.removeDuplicates();
177 if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(
"org.kde.kget") &&
178 KToolInvocation::kdeinitExecWait(
"kget") != 0) {
180 i18n(
"Unable to communicate with the KGet download manager."),
181 i18n(
"Communication Error"));
185 OrgKdeKgetMainInterface kgetInterface(
"org.kde.kget",
"/KGet", QDBusConnection::sessionBus());
186 kgetInterface.importLinks(m_linkList);
189 void KGetPlugin::getLinks(
bool selectedOnly)
191 KParts::HtmlExtension* htmlExtn = KParts::HtmlExtension::childObject(parent());
193 KParts::SelectorInterface* selector = qobject_cast<KParts::SelectorInterface*>(htmlExtn);
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);
202 Q_FOREACH(
const KParts::SelectorInterface::Element& element, elements) {
203 if (element.hasAttribute(
QL1S(
"href")))
205 else if (element.hasAttribute(
QL1S(
"src")))
207 else if (element.hasAttribute(
QL1S(
"data")))
209 const KUrl resolvedUrl (baseUrl.resolved(element.attribute(attr)));
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")));
215 m_linkList << resolvedUrl.url();
223 KParts::FileInfoExtension* fileinfoExtn = KParts::FileInfoExtension::childObject(parent());
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();
232 if (item.isReadable() && item.isFile() && !item.isLocalFile() && !url.host().isEmpty()) {
233 if (item.mimetype().isEmpty())
234 m_linkList << url.url();
236 m_linkList << QString(
QL1S(
"url ") + url.url() +
QL1S(
" type ") + item.mimetype());
243 #include "kget_plug_in.moc"
KGetPlugin(QObject *parent, const QVariantList &args)
static bool hasDropTarget()
static QWidget * partWidget(QObject *obj)