[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kfm-devel
Subject:    [PATCH] Create web shortcuts in khtml::LineEditWidget
From:       José_Millán_Soto <fid () gpul ! org>
Date:       2008-09-22 0:05:25
Message-ID: 200809220205.25828.fid () gpul ! org
[Download RAW message or body]

Hi,
I've created a patch which makes it possible to create new web shortcuts in 
the text inputs of the forms.
This code obtains the query URL by iterating through the elements of the form 
and using the values that the element has in the moment where the web 
shortcut is being made.

["render_form.diff" (text/x-patch)]

Index: khtml/rendering/render_form.h
===================================================================
--- khtml/rendering/render_form.h	(revision 863373)
+++ khtml/rendering/render_form.h	(working copy)
@@ -301,6 +301,7 @@
     void clearHistoryActivated();
     void slotCheckSpelling();
     void slotSpellCheckDone( const QString &s );
+    void slotCreateWebShortcut();
     void spellCheckerMisspelling( const QString &text, int pos);
     void spellCheckerCorrected( const QString &, int, const QString &);
     void spellCheckerFinished();
Index: khtml/rendering/render_form.cpp
===================================================================
--- khtml/rendering/render_form.cpp	(revision 863373)
+++ khtml/rendering/render_form.cpp	(working copy)
@@ -46,15 +46,23 @@
 #include <kstandardaction.h>
 #include <kactioncollection.h>
 #include <kdeuiwidgetsproxystyle_p.h>
+#include <kurl.h>
+#include <KDesktopFile>
+#include <KConfigGroup>
+#include <kstandarddirs.h>
+#include <kdialog.h>
+#include <kbuildsycocaprogressdialog.h>
 
 #include <QAbstractItemView>
 #include <QAbstractTextDocumentLayout>
 #include <QtGui/QStyle>
 #include <QStyleOptionButton>
+#include <QtGui/QLabel>
 
 #include <misc/helper.h>
 #include <xml/dom2_eventsimpl.h>
 #include <html/html_formimpl.h>
+#include <html/html_miscimpl.h>
 #include <misc/htmlhashes.h>
 
 #include <assert.h>
@@ -65,6 +73,8 @@
 
 #include <QtGui/QMenu>
 #include <QtGui/QBitmap>
+#include <QtGui/QHBoxLayout>
+#include <QtGui/QVBoxLayout>
 
 using namespace khtml;
 
@@ -563,6 +573,87 @@
         setText( s );
 }
 
+void LineEditWidget::slotCreateWebShortcut() {
+    QString queryName=m_input->name().string();
+    HTMLFormElementImpl *form=m_input->form();
+    KUrl url(form->action().string());
+    KUrl baseUrl(m_view->part()->baseURL().url()+'?');
+    if ( !url.hasPath() ) {
+        url.setPath(baseUrl.path());
+    }
+    if ( !url.hasHost() ) {
+        url.setProtocol(baseUrl.protocol());
+        url.setHost(baseUrl.host());
+    }
+    NodeImpl *node;
+    HTMLInputElementImpl *inputNode;
+    for(unsigned long i=0; (node=form->elements()->item(i)); i++) {
+        inputNode=dynamic_cast<HTMLInputElementImpl *>(node);
+        if (inputNode) {
+            if ( ( !inputNode->name().string().size() ) ||
+              (inputNode->name().string() == queryName)) continue;
+            else {
+                switch ( inputNode->inputType() ) {
+                case HTMLInputElementImpl::CHECKBOX:
+                case HTMLInputElementImpl::RADIO:
+                    if ( !inputNode->checked() ) break;
+                case HTMLInputElementImpl::TEXT:
+                case HTMLInputElementImpl::PASSWORD:
+                case HTMLInputElementImpl::HIDDEN:
+                    url.addQueryItem(inputNode->name().string(), inputNode->value().string());
+                default:
+                    //Any element of another type is ignored
+                    ;
+                }
+            }
+        }
+    }
+    QString query=url.url();
+    if ( !query.contains("?") )
+        query+='?'; //This input is the only one of the form
+    query+='&'+queryName+"=\\{@}";
+    //New shortcut dialog
+    KDialog *dialog = new KDialog();
+    QWidget *widget = new QWidget();
+    dialog->setButtons( KDialog::Ok | KDialog::Cancel );
+    dialog->setCaption( i18n("New Web Shortcut") );
+    QVBoxLayout *mainLayout = new QVBoxLayout();
+    widget->setLayout(mainLayout);
+    dialog->setMainWidget(widget);
+    QHBoxLayout *layout = new QHBoxLayout();
+    mainLayout->addLayout(layout);
+    QLabel *label = new QLabel(i18n("Search &provider name:"));
+    layout->addWidget( label );
+    QLineEdit *nameEdit = new QLineEdit(i18n("New search provider"));
+    label->setBuddy( nameEdit );
+    layout->addWidget( nameEdit );
+    layout = new QHBoxLayout();
+    mainLayout->addLayout(layout);
+    label = new QLabel(i18n("UR&I shortcuts:"));
+    layout->addWidget( label );
+    QLineEdit *keysEdit = new QLineEdit();
+    label->setBuddy(keysEdit);
+    layout->addWidget(keysEdit);
+    if (dialog->exec()) {
+        //SearchProvider class is part of kdebase, so the file is written as
+        //an standard desktop file.
+        QString fileName=keysEdit->text();
+        KStandardDirs dirs;
+        QString dir=dirs.saveLocation("services", "searchproviders");
+        while ( KStandardDirs::exists(dir+fileName+".desktop") )
+          fileName+='_';
+        KDesktopFile f(dir+fileName+".desktop");
+        f.desktopGroup().writeEntry("Keys", keysEdit->text());
+        f.desktopGroup().writeEntry("Type", "Service");
+        f.desktopGroup().writeEntry("ServiceTypes", "SearchProvider");
+        f.desktopGroup().writeEntry("Name", nameEdit->text());
+        f.desktopGroup().writeEntry("Query", query);
+        f.sync();
+        KBuildSycocaProgressDialog::rebuildKSycoca(this);
+    }
+    delete dialog;
+}
+
 void LineEditWidget::contextMenuEvent(QContextMenuEvent *e)
 {
     QMenu* popup = createStandardContextMenu();
@@ -585,6 +676,14 @@
         popup->addAction( m_spellAction );
         m_spellAction->setEnabled( !text().isEmpty() );
     }
+    if ( !m_view->part()->onlyLocalReferences() ) {
+        QAction *act = popup->addAction( i18n("Create Web Shortcut") );
+        connect(act, SIGNAL(triggered()),
+                this, SLOT(slotCreateWebShortcut()));
+    }
+
+    emit aboutToShowContextMenu(popup);
+
     popup->exec(e->globalPos());
     delete popup;
 }


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic