diff --git a/kget/extensions/konqueror/CMakeLists.txt b/kget/extensions/konqueror/CMakeLists.txt index 57d6ad7..ba47ba4 100644 --- a/kget/extensions/konqueror/CMakeLists.txt +++ b/kget/extensions/konqueror/CMakeLists.txt @@ -26,7 +26,7 @@ QT4_ADD_DBUS_INTERFACE(kget_browser_integration_SRCS ../../dbus/org.kde.kget.mai kde4_add_plugin(kget_browser_integration ${kget_browser_integration_SRCS}) -target_link_libraries(kget_browser_integration ${KDE4_KHTML_LIBS}) +target_link_libraries(kget_browser_integration ${KDE4_KHTML_LIBS} ${KDE4_KIO_LIBRARY}) if(KWEBKITPART_FOUND) target_link_libraries(kget_browser_integration ${KWEBKITPART_LIBRARIES} ${KDE4_KDEUI_LIBRARY} ${QT_QTWEBKIT_LIBRARY}) @@ -37,6 +37,7 @@ install(TARGETS kget_browser_integration DESTINATION ${PLUGIN_INSTALL_DIR}) ########### install files ############### install(FILES kget_plug_in.rc DESTINATION ${DATA_INSTALL_DIR}/khtml/kpartplugins) +install(FILES kget_plug_in.rc DESTINATION ${DATA_INSTALL_DIR}/dolphinpart/kpartplugins) if (KWEBKITPART_FOUND) install(FILES kget_plug_in.rc DESTINATION ${DATA_INSTALL_DIR}/kwebkitpart/kpartplugins) diff --git a/kget/extensions/konqueror/kget_plug_in.cpp b/kget/extensions/konqueror/kget_plug_in.cpp index a0ff509..9970baa 100644 --- a/kget/extensions/konqueror/kget_plug_in.cpp +++ b/kget/extensions/konqueror/kget_plug_in.cpp @@ -4,6 +4,7 @@ Copyright (C) 2002 Carsten Pfeiffer Copyright (C) 2007 Urs Wolfer Copyright (C) 2010 Dawit Alemayehu + Copyright (C) 2010 Matthias Fuchs This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public @@ -41,126 +42,12 @@ #include #endif - -QStringList getLinks(QObject *obj, bool selectedOnly = false) +KGet_plug_in::KGet_plug_in(QObject *parent) + : Plugin(parent), + m_dolphinPart(0), + m_type(None) { - Q_ASSERT(obj); - - QStringList linkList; - - if (obj->inherits("KHTMLPart")) - { - KHTMLPart *htmlPart = qobject_cast(obj); - KParts::Part *activePart = 0; - - if ( htmlPart && htmlPart->partManager() ) - { - activePart = htmlPart->partManager()->activePart(); - if ( activePart && activePart->inherits( "KHTMLPart" ) ) - htmlPart = qobject_cast( activePart ); - } - - DOM::HTMLDocument doc; - - if ( selectedOnly ) - { - doc.open(); - if (htmlPart) - doc.write(htmlPart->selectedTextAsHTML()); - doc.close(); - } - else - { - if (htmlPart) - doc = htmlPart->htmlDocument(); - } - - if ( !doc.isNull() ) - { - DOM::HTMLCollection links = doc.links(); - DOM::HTMLCollection images = doc.images(); - - for ( uint i = 0; i < links.length(); i++ ) - { - DOM::Node link = links.item( i ); - if ( link.isNull() || link.nodeType() != DOM::Node::ELEMENT_NODE ) - continue; - - LinkItem *item = new LinkItem( (DOM::Element) link ); - if (item->isValid()) - linkList.append(item->url.url()); - else - delete item; - } - - /* do the same for images */ - for ( uint i = 0; i < images.length(); i++ ) - { - DOM::Node image = images.item( i ); - if ( image.isNull() || image.nodeType() != DOM::Node::ELEMENT_NODE ) - continue; - - LinkItem *item = new LinkItem( (DOM::Element) image ); - if (item->isValid()) - linkList.append(item->url.url()); - else - delete item; - } - } - } -#ifdef HAVE_KWEBKITPART - else if (obj->inherits("KWebKitPart")) - { - KWebKitPart *part = qobject_cast(obj); - Q_ASSERT(part); - - QWebView *view = part->view(); - if (view) - { - QWebPage *page = view->page(); - Q_ASSERT(page); - - QWebFrame *frame = page->currentFrame(); - - // QtWebKit selector language is the same as the CSS3 language selectors. Consult - // http://www.w3.org/TR/css3-selectors/#selectors for details on CSS3 language - // selectors. The query below finds all the anchor() and image () links - // in the current frame. - const QWebElementCollection collection = frame->findAllElements(QLatin1String("a[href], img[src]")); - QString attrValue; - - if (selectedOnly) - { - // TODO: Find a way to find links and images in selected content. - // No straight forward way to accomplish this in QtWebKit without - // resorting to javascript based hacks right now! - } - else - { - Q_FOREACH(const QWebElement &element, collection) - { - if (element.hasAttribute(QLatin1String("href"))) - attrValue = QLatin1String("href"); - else - attrValue = QLatin1String("src"); - - const QUrl url = frame->baseUrl().resolved(QUrl(element.attribute("href"))); - if (url.isValid()) - linkList << url.toString(); - } - } - } - } -#endif - - return linkList; -} - -KGet_plug_in::KGet_plug_in( QObject* parent ) - :Plugin(parent) -{ - KActionMenu *menu = new KActionMenu(KIcon("kget"), i18n("Download Manager"), - actionCollection()); + KActionMenu *menu = new KActionMenu(KIcon("kget"), i18n("Download Manager"), actionCollection()); actionCollection()->addAction("kget_menu", menu); menu->setDelayed( false ); @@ -182,8 +69,24 @@ KGet_plug_in::KGet_plug_in( QObject* parent ) connect(showSelectedLinksAction, SIGNAL(triggered()), SLOT(slotShowSelectedLinks())); menu->addAction(showSelectedLinksAction); - if (parent && !parent->inherits("KHTMLPart") && !parent->inherits("KWebKitPart")) + if (parent) { + if (parent->inherits("KHTMLPart")) { + m_type = KHTMLType; + } else if (parent->inherits("KWebKitPart")) { + m_type = KWebkitType; + } else if (parent->inherits("DolphinPart")) { + m_type = DolphinType; + } + } + + //FIXME once DolphinPart gets exported + if ((m_type != KHTMLType) && (m_type != KWebkitType))//TODO support also for DolphinPart? --> DolphinView::selectedUrls() actionCollection()->action("show_selected_links")->setVisible(false); + + if (m_type == DolphinType) { + m_dolphinPart = qobject_cast(parent); + connect(m_dolphinPart, SIGNAL(started(KIO::Job*)), this, SLOT(slotCheckUrlDolphin()));//TODO use aboutToOpenURL from DolphinPart instead? + } } @@ -191,59 +94,64 @@ KGet_plug_in::~KGet_plug_in() { } +void KGet_plug_in::slotCheckUrlDolphin() +{ + m_dolphinPartUrl = m_dolphinPart->url(); + const QString protocol = m_dolphinPartUrl.protocol(); + const bool visible = protocol.contains("ftp"); + actionCollection()->action("kget_menu")->setVisible(visible); +} + void KGet_plug_in::showPopup() { bool hasDropTarget = false; - if(QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.kget")) - { + if (QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.kget")) { OrgKdeKgetMainInterface kgetInterface("org.kde.kget", "/KGet", QDBusConnection::sessionBus()); QDBusReply reply = kgetInterface.dropTargetVisible(); - if (reply.isValid()) + if (reply.isValid()) { hasDropTarget = reply.value(); + } } m_dropTargetAction->setChecked(hasDropTarget); - if ( parent() ) - { - bool enabled = false; - if (parent()->inherits("KHTMLPart")) - { - const QString selectedHtml = qobject_cast(parent())->selectedTextAsHTML(); - DOM::HTMLDocument document; - document.open(); - document.write( selectedHtml ); - document.close(); - enabled = (document.getElementsByTagName("a").length() > 0); - } + bool enabled = false; + if (m_type == KHTMLType) { + const QString selectedHtml = qobject_cast(parent())->selectedTextAsHTML(); + DOM::HTMLDocument document; + document.open(); + document.write( selectedHtml ); + document.close(); + enabled = (document.getElementsByTagName("a").length() > 0); + } #ifdef HAVE_KWEBKITPART - else if (parent()->inherits("KWebKitPart")) - { - // TODO: Find a way to find links and images in selected content. - // No straight forward way to accomplish this in QtWebKit without - // resorting to javascript based hacks right now! - } -#endif - actionCollection()->action("show_selected_links")->setEnabled(enabled); + else if (m_type == KWebkitType) { + // TODO: Find a way to find links and images in selected content. + // No straight forward way to accomplish this in QtWebKit without + // resorting to javascript based hacks right now! } +#endif + actionCollection()->action("show_selected_links")->setEnabled(enabled); } void KGet_plug_in::slotShowDrop() { - if(!QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.kget")) { + if (!QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.kget")) { QWidget *parentWidget = 0; - if (parent()) - { - if (parent()->inherits("KHTMLPart")) - parentWidget = qobject_cast(parent())->widget(); + if (m_type == KHTMLType) { + parentWidget = qobject_cast(parent())->widget(); + } #ifdef HAVE_KWEBKITPART - else if (parent()->inherits("KWebKitPart")) - parentWidget = qobject_cast(parent())->view(); + else if (m_type == KWebkitType) { + parentWidget = qobject_cast(parent())->view(); + } #endif - KRun::runCommand("kget --showDropTarget --hideMainWindow", "kget", "kget", parentWidget); + else if (m_type == DolphinType) { + parentWidget = qobject_cast(parent())->widget(); } + KRun::runCommand("kget --showDropTarget --hideMainWindow", "kget", "kget", parentWidget); } else { OrgKdeKgetMainInterface kgetInterface("org.kde.kget", "/KGet", QDBusConnection::sessionBus()); kgetInterface.setDropTargetVisible(m_dropTargetAction->isChecked()); @@ -252,20 +160,17 @@ void KGet_plug_in::slotShowDrop() void KGet_plug_in::slotShowLinks() { - showLinks(false); + getLinks(false); } void KGet_plug_in::slotShowSelectedLinks() { - showLinks(true); + getLinks(true); } -void KGet_plug_in::showLinks( bool selectedOnly ) +void KGet_plug_in::slotImportLinks() { - QStringList linkList = getLinks(parent(), selectedOnly); - - if ( linkList.isEmpty() ) - { + if (m_linkList.isEmpty()) { KParts::ReadOnlyPart *part = qobject_cast(parent()); KMessageBox::sorry(part ? part->widget() : 0, i18n("There are no links in the active frame of the current HTML page."), @@ -274,11 +179,10 @@ void KGet_plug_in::showLinks( bool selectedOnly ) } // Remove any duplicates links from the list... - linkList.removeDuplicates(); + m_linkList.removeDuplicates(); if (!QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.kget") && - KToolInvocation::kdeinitExecWait("kget") != 0) - { + KToolInvocation::kdeinitExecWait("kget") != 0) { KParts::ReadOnlyPart *part = qobject_cast(parent()); KMessageBox::sorry(part ? part->widget() : 0, i18n("Unable to communicate with the KGet download manager."), @@ -287,9 +191,140 @@ void KGet_plug_in::showLinks( bool selectedOnly ) } OrgKdeKgetMainInterface kgetInterface("org.kde.kget", "/KGet", QDBusConnection::sessionBus()); - kgetInterface.importLinks(linkList); + kgetInterface.importLinks(m_linkList); } +void KGet_plug_in::getLinks(bool selectedOnly) +{ + QObject *obj = parent(); + Q_ASSERT(obj); + + m_linkList.clear(); + + if (m_type == KHTMLType) { + KHTMLPart *htmlPart = qobject_cast(obj); + KParts::Part *activePart = 0; + + if (htmlPart && htmlPart->partManager()) { + activePart = htmlPart->partManager()->activePart(); + if (activePart && activePart->inherits("KHTMLPart")) { + htmlPart = qobject_cast(activePart); + } + } + + DOM::HTMLDocument doc; + + if (selectedOnly) { + doc.open(); + if (htmlPart) { + doc.write(htmlPart->selectedTextAsHTML()); + } + doc.close(); + } else { + if (htmlPart) { + doc = htmlPart->htmlDocument(); + } + } + + if (!doc.isNull()) { + DOM::HTMLCollection links = doc.links(); + DOM::HTMLCollection images = doc.images(); + + for (uint i = 0; i < links.length(); ++i) { + DOM::Node link = links.item( i ); + if (link.isNull() || (link.nodeType() != DOM::Node::ELEMENT_NODE)) { + continue; + } + + LinkItem *item = new LinkItem(DOM::Element(link)); + if (item->isValid()) { + m_linkList.append(item->url.url()); + } else { + delete item; + } + } + + /* do the same for images */ + for (uint i = 0; i < images.length(); ++i) { + DOM::Node image = images.item(i); + if ( image.isNull() || (image.nodeType() != DOM::Node::ELEMENT_NODE)) + continue; + + LinkItem *item = new LinkItem(DOM::Element(image)); + if (item->isValid()) { + m_linkList.append(item->url.url()); + } else { + delete item; + } + } + } + slotImportLinks(); + } +#ifdef HAVE_KWEBKITPART + else if (m_type == KWebkitType) { + KWebKitPart *part = qobject_cast(obj); + Q_ASSERT(part); + + QWebView *view = part->view(); + if (view) { + QWebPage *page = view->page(); + Q_ASSERT(page); + + QWebFrame *frame = page->currentFrame(); + + // QtWebKit selector language is the same as the CSS3 language selectors. Consult + // http://www.w3.org/TR/css3-selectors/#selectors for details on CSS3 language + // selectors. The query below finds all the anchor() and image () links + // in the current frame. + const QWebElementCollection collection = frame->findAllElements(QLatin1String("a[href], img[src]")); + QString attrValue; + + if (selectedOnly) { + // TODO: Find a way to find links and images in selected content. + // No straight forward way to accomplish this in QtWebKit without + // resorting to javascript based hacks right now! + } else { + foreach (const QWebElement &element, collection) { + if (element.hasAttribute(QLatin1String("href"))) { + attrValue = QLatin1String("href"); + } else { + attrValue = QLatin1String("src"); + } + + const QUrl url = frame->baseUrl().resolved(QUrl(element.attribute("href"))); + if (url.isValid()) { + m_linkList << url.toString(); + } + } + } + } + slotImportLinks(); + } +#endif + else if (m_type == DolphinType) { + const KUrl url = qobject_cast(obj)->url(); + KIO::ListJob *list = KIO::listDir(m_dolphinPartUrl, KIO::HideProgressInfo); + connect(list, SIGNAL(entries(KIO::Job*,KIO::UDSEntryList)), this, SLOT(slotEntries(KIO::Job*,KIO::UDSEntryList))); + connect(list, SIGNAL(finished(KJob*)), this, SLOT(slotImportLinks())); + } +} + +void KGet_plug_in::slotEntries(KIO::Job *job, const KIO::UDSEntryList &entries) +{ + Q_UNUSED(job) + + const QString baseUrl = m_dolphinPartUrl.url(KUrl::AddTrailingSlash); + foreach (const KIO::UDSEntry &entry, entries) { + //skip all found dirs + if (!entry.isDir()) { + const QString name = entry.stringValue(KIO::UDSEntry::UDS_NAME); + m_linkList << baseUrl + name; + } + } +} + + + KGetPluginFactory::KGetPluginFactory( QObject* parent ) :KPluginFactory("kget", "kget", parent) { diff --git a/kget/extensions/konqueror/kget_plug_in.h b/kget/extensions/konqueror/kget_plug_in.h index 78a7a9e..50552da 100644 --- a/kget/extensions/konqueror/kget_plug_in.h +++ b/kget/extensions/konqueror/kget_plug_in.h @@ -1,6 +1,7 @@ /* This file is part of the KDE project Copyright (C) 2002 Patrick Charbonnier + Copyright (C) 2010 Matthias Fuchs This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public @@ -11,11 +12,16 @@ #ifndef PLUGIN_KGET_PLUG_IN_H #define PLUGIN_KGET_PLUG_IN_H +#include #include #include class KToggleAction; +namespace KParts { + class ReadOnlyPart; +} + class KGet_plug_in : public KParts::Plugin { @@ -25,14 +31,37 @@ public: KToggleAction *m_dropTargetAction; virtual ~KGet_plug_in(); + enum PartType { + None = 0, + KHTMLType, + KWebkitType, + DolphinType + }; + private: - void showLinks( bool ); + void getLinks(bool selectedOnly = false); private slots: void slotShowDrop(); void slotShowLinks(); void slotShowSelectedLinks(); + void slotImportLinks(); + /** + * Only show the KGet Plugin in the DolphinPart, if the selected Url is ftp or sftp + */ + void slotCheckUrlDolphin(); + + /** + * Links of the DolphinPart, gotten by a listJob + */ + void slotEntries(KIO::Job *job, const KIO::UDSEntryList &entries); void showPopup(); + +private: + KParts::ReadOnlyPart *m_dolphinPart; + KUrl m_dolphinPartUrl; + PartType m_type; + QStringList m_linkList; };