Make Konqueror's searchbar plugin work with any KPart based browser engine...

View: New views
6 Messages — Rating Filter:   Alert me  

Make Konqueror's searchbar plugin work with any KPart based browser engine...

by Bugzilla from adawit@kde.org :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

Since there is no "Konqueror" or "Konq-plugins" group in reviewboard, I am
going to post this patch here. As the subject states the attached patch makes
it possible for the searchbar plugin to be used with rendering engines other
other than khtml, e.g. webkitpart.

Please CC me when you reply since I am not subscribed to kfm-devel.

Thanks.

[searchbar.patch]

Index: searchbar.cpp
===================================================================
--- searchbar.cpp (revision 1027752)
+++ searchbar.cpp (working copy)
@@ -194,7 +194,7 @@
     if (m_urlEnterLock || search.isEmpty() || !m_part)
         return;
     if (m_searchMode == FindInThisPage) {
-        KHTMLPart *part = dynamic_cast<KHTMLPart*>(m_part);
+        KHTMLPart *part = qobject_cast<KHTMLPart*>(m_part);
         if (part) {
             part->findText(search, 0);
             part->findTextNext();
@@ -292,7 +292,8 @@
 
         m_popupMenu = new QMenu(m_searchCombo);
         m_popupMenu->setObjectName("search selection menu");
-        m_popupMenu->addAction(KIcon("edit-find"), i18n("Find in This Page"), this, SLOT(useFindInThisPage()));
+        QAction *action = m_popupMenu->addAction(KIcon("edit-find"), i18n("Find in This Page"), this, SLOT(useFindInThisPage()));
+        action->setDisabled((qobject_cast<KHTMLPart*>(m_part) == 0));
         m_popupMenu->addSeparator();
 
         int i =- 1;
@@ -458,7 +459,7 @@
 
 void SearchBarPlugin::updateComboVisibility()
 {
-    bool isBrowser = m_part && dynamic_cast<KHTMLPart *>(m_part);
+    bool isBrowser = (m_part && m_part->browserExtension());
 
     m_searchComboAction->setVisible(isBrowser && !m_searchComboAction->associatedWidgets().isEmpty());
 
@@ -502,33 +503,35 @@
 
 void SearchBarPlugin::HTMLDocLoaded()
 {
-    KHTMLPart *khtmlPart = static_cast<KHTMLPart *>(m_part);
+    KHTMLPart *khtmlPart = qobject_cast<KHTMLPart *>(m_part);
 
-    DOM::HTMLDocument htmlDoc = khtmlPart->htmlDocument();
-    DOM::NodeList headNL = htmlDoc.getElementsByTagName("head");
-    if (headNL.length() < 1) {
-        return;
-    }
+    if (khtmlPart) {
+        DOM::HTMLDocument htmlDoc = khtmlPart->htmlDocument();
+        DOM::NodeList headNL = htmlDoc.getElementsByTagName("head");
+        if (headNL.length() < 1) {
+            return;
+        }
 
-    DOM::Node headNode = headNL.item(0);
-    if (headNode.nodeType() != 1) {
-        return;
-    }
+        DOM::Node headNode = headNL.item(0);
+        if (headNode.nodeType() != 1) {
+            return;
+        }
 
-    DOM::HTMLElement headEl = (DOM::HTMLElement) headNode;
-    DOM::NodeList linkNL = headEl.getElementsByTagName("link");
+        DOM::HTMLElement headEl = (DOM::HTMLElement) headNode;
+        DOM::NodeList linkNL = headEl.getElementsByTagName("link");
 
-    for (unsigned int i = 0; i < linkNL.length(); i++) {
-        if (linkNL.item(i).nodeType() == 1) {
-            DOM::HTMLElement linkEl = (DOM::HTMLElement)linkNL.item(i);
-            if (linkEl.getAttribute("rel") == "search" && linkEl.getAttribute("type") == "application/opensearchdescription+xml") {
-                if (headEl.getAttribute("profile") != "http://a9.com/-/spec/opensearch/1.1/") {
-                    kWarning() << "Warning: there is no profile attribute or wrong profile attribute in <head>, as specified by open search specification 1.1";
+        for (unsigned int i = 0; i < linkNL.length(); i++) {
+            if (linkNL.item(i).nodeType() == 1) {
+                DOM::HTMLElement linkEl = (DOM::HTMLElement)linkNL.item(i);
+                if (linkEl.getAttribute("rel") == "search" && linkEl.getAttribute("type") == "application/opensearchdescription+xml") {
+                    if (headEl.getAttribute("profile") != "http://a9.com/-/spec/opensearch/1.1/") {
+                        kWarning() << "Warning: there is no profile attribute or wrong profile attribute in <head>, as specified by open search specification 1.1";
+                    }
+
+                    QString title = linkEl.getAttribute("title").string();
+                    QString href = linkEl.getAttribute("href").string();
+                    m_openSearchDescs.insert(title, href);
                 }
-
-                QString title = linkEl.getAttribute("title").string();
-                QString href = linkEl.getAttribute("href").string();
-                m_openSearchDescs.insert(title, href);
             }
         }
     }


Re: Make Konqueror's searchbar plugin work with any KPart based browser engine...

by Friedrich W. H. Kossebau-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Dawit,

Vendredi, le 25 septembre 2009, à 18:02, Dawit A. a écrit:
> Hello,
>
> Since there is no "Konqueror" or "Konq-plugins" group in reviewboard, I am
> going to post this patch here. As the subject states the attached patch
> makes it possible for the searchbar plugin to be used with rendering
> engines other other than khtml, e.g. webkitpart.
>
> Please CC me when you reply since I am not subscribed to kfm-devel.

@@ -458,7 +459,7 @@
 
 void SearchBarPlugin::updateComboVisibility()
 {
-    bool isBrowser = m_part && dynamic_cast<KHTMLPart *>(m_part);
+    bool isBrowser = (m_part && m_part->browserExtension());

The name "browserExtension()" might have lead you to the wrong assumption. The
BrowserExtension [BE] is an extension of KParts for better support _of_
browser-a-like containers like Konqueror, for stuff like going back and
forward in history. It is not the extension of a (HTML) browsing-able KPart.

Besides the KHtmlPart at least the KParts of Kate and Okteta are implementing
this extension. So this check for "isBrowser" does not work I fear.

[BE]
http://api.kde.org/4.x-api/kdelibs-apidocs/kparts/html/classKParts_1_1BrowserExtension.html

Cheers
Friedrich
--
Okteta - KDE 4 Hex Editor - http://utils.kde.org/projects/okteta

Re: Make Konqueror's searchbar plugin work with any KPart based browser engine...

by Bugzilla from adawit@kde.org :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Friday 25 September 2009 14:24:52 Friedrich W. H. Kossebau wrote:

> Hi Dawit,
>
> Vendredi, le 25 septembre 2009, à 18:02, Dawit A. a écrit:
> > Hello,
> >
> > Since there is no "Konqueror" or "Konq-plugins" group in reviewboard, I
> > am going to post this patch here. As the subject states the attached
> > patch makes it possible for the searchbar plugin to be used with
> > rendering engines other other than khtml, e.g. webkitpart.
> >
> > Please CC me when you reply since I am not subscribed to kfm-devel.
>
> @@ -458,7 +459,7 @@
>
>  void SearchBarPlugin::updateComboVisibility()
>  {
> -    bool isBrowser = m_part && dynamic_cast<KHTMLPart *>(m_part);
> +    bool isBrowser = (m_part && m_part->browserExtension());
>
> The name "browserExtension()" might have lead you to the wrong assumption.
>  The BrowserExtension [BE] is an extension of KParts for better support
>  _of_ browser-a-like containers like Konqueror, for stuff like going back
>  and forward in history. It is not the extension of a (HTML) browsing-able
>  KPart.
>
> Besides the KHtmlPart at least the KParts of Kate and Okteta are
>  implementing this extension. So this check for "isBrowser" does not work I
>  fear.
Indeed... That check was accidentally left there by me when I was orginally
playing with it. You only need to check whether or not there is a part. That
is sufficient since the install directory for the .desktop file determines to
which application the plugin belongs ; so there is no need to check the type
of KPart you dealing with. You only need to make sure that features specific
only to a given KPart are invoked correctly which is what the patch was
intended to address.

Attached is the corrected version of the patch... Thanks.

[searchbar.patch]

Index: searchbar.cpp
===================================================================
--- searchbar.cpp (revision 1027752)
+++ searchbar.cpp (working copy)
@@ -194,7 +194,7 @@
     if (m_urlEnterLock || search.isEmpty() || !m_part)
         return;
     if (m_searchMode == FindInThisPage) {
-        KHTMLPart *part = dynamic_cast<KHTMLPart*>(m_part);
+        KHTMLPart *part = qobject_cast<KHTMLPart*>(m_part);
         if (part) {
             part->findText(search, 0);
             part->findTextNext();
@@ -292,7 +292,8 @@
 
         m_popupMenu = new QMenu(m_searchCombo);
         m_popupMenu->setObjectName("search selection menu");
-        m_popupMenu->addAction(KIcon("edit-find"), i18n("Find in This Page"), this, SLOT(useFindInThisPage()));
+        QAction *action = m_popupMenu->addAction(KIcon("edit-find"), i18n("Find in This Page"), this, SLOT(useFindInThisPage()));
+        action->setDisabled((qobject_cast<KHTMLPart*>(m_part) == 0));
         m_popupMenu->addSeparator();
 
         int i =- 1;
@@ -458,7 +459,7 @@
 
 void SearchBarPlugin::updateComboVisibility()
 {
-    bool isBrowser = m_part && dynamic_cast<KHTMLPart *>(m_part);
+    const bool isBrowser = (m_part != 0);
 
     m_searchComboAction->setVisible(isBrowser && !m_searchComboAction->associatedWidgets().isEmpty());
 
@@ -502,33 +503,35 @@
 
 void SearchBarPlugin::HTMLDocLoaded()
 {
-    KHTMLPart *khtmlPart = static_cast<KHTMLPart *>(m_part);
+    KHTMLPart *khtmlPart = qobject_cast<KHTMLPart *>(m_part);
 
-    DOM::HTMLDocument htmlDoc = khtmlPart->htmlDocument();
-    DOM::NodeList headNL = htmlDoc.getElementsByTagName("head");
-    if (headNL.length() < 1) {
-        return;
-    }
+    if (khtmlPart) {
+        DOM::HTMLDocument htmlDoc = khtmlPart->htmlDocument();
+        DOM::NodeList headNL = htmlDoc.getElementsByTagName("head");
+        if (headNL.length() < 1) {
+            return;
+        }
 
-    DOM::Node headNode = headNL.item(0);
-    if (headNode.nodeType() != 1) {
-        return;
-    }
+        DOM::Node headNode = headNL.item(0);
+        if (headNode.nodeType() != 1) {
+            return;
+        }
 
-    DOM::HTMLElement headEl = (DOM::HTMLElement) headNode;
-    DOM::NodeList linkNL = headEl.getElementsByTagName("link");
+        DOM::HTMLElement headEl = (DOM::HTMLElement) headNode;
+        DOM::NodeList linkNL = headEl.getElementsByTagName("link");
 
-    for (unsigned int i = 0; i < linkNL.length(); i++) {
-        if (linkNL.item(i).nodeType() == 1) {
-            DOM::HTMLElement linkEl = (DOM::HTMLElement)linkNL.item(i);
-            if (linkEl.getAttribute("rel") == "search" && linkEl.getAttribute("type") == "application/opensearchdescription+xml") {
-                if (headEl.getAttribute("profile") != "http://a9.com/-/spec/opensearch/1.1/") {
-                    kWarning() << "Warning: there is no profile attribute or wrong profile attribute in <head>, as specified by open search specification 1.1";
+        for (unsigned int i = 0; i < linkNL.length(); i++) {
+            if (linkNL.item(i).nodeType() == 1) {
+                DOM::HTMLElement linkEl = (DOM::HTMLElement)linkNL.item(i);
+                if (linkEl.getAttribute("rel") == "search" && linkEl.getAttribute("type") == "application/opensearchdescription+xml") {
+                    if (headEl.getAttribute("profile") != "http://a9.com/-/spec/opensearch/1.1/") {
+                        kWarning() << "Warning: there is no profile attribute or wrong profile attribute in <head>, as specified by open search specification 1.1";
+                    }
+
+                    QString title = linkEl.getAttribute("title").string();
+                    QString href = linkEl.getAttribute("href").string();
+                    m_openSearchDescs.insert(title, href);
                 }
-
-                QString title = linkEl.getAttribute("title").string();
-                QString href = linkEl.getAttribute("href").string();
-                m_openSearchDescs.insert(title, href);
             }
         }
     }


Re: Make Konqueror's searchbar plugin work with any KPart based browser engine...

by Friedrich W. H. Kossebau-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Vendredi, le 25 septembre 2009, à 21:01, Dawit A. a écrit:
> Indeed... That check was accidentally left there by me when I was orginally
> playing with it. You only need to check whether or not there is a part.

Glad this didn't break your approach, so I as messenger now was shot ;)

> That is sufficient since the install directory for the .desktop file
> determines to which application the plugin belongs ; so there is no need to
> check the type of KPart you dealing with. You only need to make sure that
> features specific only to a given KPart are invoked correctly which is what
> the patch was intended to address.
>
> Attached is the corrected version of the patch... Thanks.

Perhaps the name of the variable "isBrowser" should be adapted, then, so other
persons reading the code understand what this bool is about? :)

(BTW: I am no Konqui developer, just lurking on the list since some time due
to me planning some bigger discussion needed for the network:/ kioslave, just
missing the time yet to prepare it as needed :) )

Cheers
Friedrich
--
Okteta - KDE 4 Hex Editor - http://utils.kde.org/projects/okteta

Re: Make Konqueror's searchbar plugin work with any KPart based browser engine...

by Fredy Yanardi :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Dawit,

I've looked through the patch, and I think generally the patch is ok
to make the searchbar plugin works with any kpart browser engine (like
webkit).

But there is one problem: the searchbar is enabled (and shown)
regardless of the active part, and the worse part is if the user
choose to search using the searchbar, the result is just passed to the
active part. So for example if currently active part is kate part, and
user uses searchbar for google search, what he / she gets is the raw
html text shown in the kate part. (IIRC, there is a bug report for
this problem)

One solution for this is to change the active part to be a browser
part, but I haven't really looked into it.

Best regards,

Fredy Yanardi

On Sat, Sep 26, 2009 at 3:01 AM, Dawit A. <adawit@...> wrote:

> Indeed... That check was accidentally left there by me when I was orginally
> playing with it. You only need to check whether or not there is a part. That
> is sufficient since the install directory for the .desktop file determines to
> which application the plugin belongs ; so there is no need to check the type
> of KPart you dealing with. You only need to make sure that features specific
> only to a given KPart are invoked correctly which is what the patch was
> intended to address.
>
> Attached is the corrected version of the patch... Thanks.
>

Re: Make Konqueror's searchbar plugin work with any KPart based browser engine...

by Bugzilla from adawit@kde.org :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Fredy,

On Wednesday 30 September 2009 13:06:02 you wrote:

> Hi Dawit,
>
> I've looked through the patch, and I think generally the patch is ok
> to make the searchbar plugin works with any kpart browser engine (like
> webkit).
>
> But there is one problem: the searchbar is enabled (and shown)
> regardless of the active part, and the worse part is if the user
> choose to search using the searchbar, the result is just passed to the
> active part. So for example if currently active part is kate part, and
> user uses searchbar for google search, what he / she gets is the raw
> html text shown in the kate part. (IIRC, there is a bug report for
> this problem)

Right... the patch attached to this email is not the final patch which I ended
up committing a couple of days ago. The committed change does not have the
problem you raised here because it hides the search bar if the Kpart is
ReadWrite part, which of course the katepart is... Doing that is not a perfect
solution for this problem, but I believe it is better than checking for all
the specific engine types you want to support.

The ideal solution would probably be to either add a new service type to
identify browsing engines which we could query for or add a new property, e.g.
X-KDE-Browser-Engine=true, to the part's ".desktop" file to identify it as a
browsing engine and checking for that property. Until then me thinks the check
for the ReadWrite part should be sufficient...