Basic Live Connect support for java

Koos Vriezen koos.vriezen at xs4all.nl
Sun Apr 7 18:54:22 BST 2002


Hi,

Attached a patch and java classes for Live Connect. It works with the
example http://www.innovation.ch/java/HTTPClient/disp_html.html. You have
to put quotes around the HTML code here.
Only some members of netscape.javascript.JSObject are implemented.

This patch also makes KJASAppletContext compatable with jdk1.4.

Return values are not yet supported. Because libkjava.so isn't linked
against libkhtml.so and communicates via the stdin/stdout streams, I used
KHTML DCOP interface to evaluate the JS code. This makes it hard to
return JS objects. Maybe a map of referenced JS objects, where only id's
are exchanged, could help.

Is something like this needed in KHTML?

Regards,

Koos Vriezen
-------------- next part --------------
--- khtml_part.cpp	2002/04/07 11:15:37	1.688
+++ khtml_part.cpp	2002/04/07 17:19:11
@@ -693,7 +693,7 @@ KJavaAppletContext *KHTMLPart::createJav
 {
 #ifndef Q_WS_QWS
   if ( !d->m_javaContext ) {
-      d->m_javaContext = new KJavaAppletContext();
+      d->m_javaContext = new KJavaAppletContext(d->m_dcopobject);
       connect( d->m_javaContext, SIGNAL(showStatus(const QString&)),
                this, SIGNAL(setStatusBarText(const QString&)) );
       connect( d->m_javaContext, SIGNAL(showDocument(const QString&, const QString&)),
Binary files /tmp/cvsgVDAjp and kjava.jar differ
--- java/kjavaappletcontext.cpp	2002/03/20 19:43:24	1.22
+++ java/kjavaappletcontext.cpp	2002/04/07 17:19:14
@@ -14,6 +14,7 @@ class KJavaAppletContextPrivate
 friend class KJavaAppletContext;
 private:
     QMap< int, QGuardedPtr<KJavaApplet> > applets;
+    DCOPObject *browser;
 };
 
 
@@ -22,10 +23,11 @@ int KJavaAppletContext::contextCount = 0
 
 /*  Class Implementation
  */
-KJavaAppletContext::KJavaAppletContext()
+KJavaAppletContext::KJavaAppletContext(DCOPObject *object)
     : QObject()
 {
     d = new KJavaAppletContextPrivate;
+    d->browser = object;
     server = KJavaAppletServer::allocateJavaServer();
 
     id = contextCount;
@@ -144,6 +146,10 @@ void KJavaAppletContext::received( const
             tmp->resizeAppletWidget( width, height );
         }
     }
+}
+
+DCOPObject * KJavaAppletContext::getBrowserObject() {
+    return d->browser;
 }
 
 #include <kjavaappletcontext.moc>
--- java/kjavaappletcontext.h	2001/01/23 17:30:01	1.13
+++ java/kjavaappletcontext.h	2002/04/07 17:19:14
@@ -42,13 +42,14 @@
 class KJavaAppletServer;
 class KJavaApplet;
 class KJavaAppletContextPrivate;
+class DCOPObject;
 
 class KJavaAppletContext : public QObject
 {
 Q_OBJECT
 
 public:
-    KJavaAppletContext();
+    KJavaAppletContext(DCOPObject *obj=NULL);
     ~KJavaAppletContext();
 
     /**
@@ -92,6 +93,7 @@ public:
      */
     void processCmd( QString cmd, QStringList args );
 
+    DCOPObject * getBrowserObject();
 signals:
     /**
      * Signals the KHMTL Part to show this as the status message.
--- java/kjavaappletserver.cpp	2001/12/25 20:54:57	1.41
+++ java/kjavaappletserver.cpp	2002/04/07 17:19:14
@@ -9,6 +9,7 @@
 #include <klocale.h>
 #include <kio/kprotocolmanager.h>
 #include <kio/job.h>
+#include <dcopobject.h>
 
 #include <qtimer.h>
 #include <qguardedptr.h>
@@ -30,6 +31,7 @@
 #define KJAS_GET_URLDATA       (char)12
 #define KJAS_URLDATA           (char)13
 #define KJAS_SHUTDOWN_SERVER   (char)14
+#define KJAS_EVALUATE_JAVASCRIPT   (char)15
 
 // For future expansion
 class KJavaAppletServerPrivate
@@ -383,6 +385,11 @@ void KJavaAppletServer::slotJavaRequest(
             kdDebug(6100) << "GetURLData from classloader: "<< contextID
                           << " for url: " << args[0] << endl;
             break;
+        case KJAS_EVALUATE_JAVASCRIPT:
+            //here we need to get some data for a class loader and send it back...
+            kdDebug(6100) << "Javascript request: "<< contextID
+                          << " code: " << args[0] << endl;
+            break;
 
         default:
             return;
@@ -392,6 +399,22 @@ void KJavaAppletServer::slotJavaRequest(
     if( cmd_code == KJAS_GET_URLDATA )
     {
         new KJavaDownloader( contextID, args[0] );
+    }
+    else if (cmd_code == KJAS_EVALUATE_JAVASCRIPT)
+    {
+        bool ok;
+        int contextID_num = contextID.toInt( &ok );
+        KJavaAppletContext * tmp = d->contexts[contextID_num];
+	kdDebug(6100) << "KJavaAppletContext: "<< (void*) tmp << endl;
+        DCOPObject * browser = NULL;
+        if (tmp && (browser = tmp->getBrowserObject())) {
+	    kdDebug(6100) << "BrowserObject: "<< (void*) browser << endl;
+            QByteArray data, retval;
+            QDataStream arg(data, IO_WriteOnly);
+            arg << args[0];
+            QCString rettype;
+            browser->process(QCString("evalJS(QString)"), data, rettype, retval);
+        }
     }
     else
     {
--- java/org/kde/kjas/server/KJASAppletContext.java	2001/02/14 18:24:01	1.27
+++ java/org/kde/kjas/server/KJASAppletContext.java	2002/04/07 17:19:14
@@ -14,6 +14,7 @@ public class KJASAppletContext implement
 {
     private Hashtable stubs;
     private Hashtable images;
+    private Hashtable streams;
 
     private String myID;
     private KJASAppletClassLoader loader;
@@ -25,6 +26,7 @@ public class KJASAppletContext implement
     {
         stubs  = new Hashtable();
         images = new Hashtable();
+        streams = new Hashtable();
         myID   = _contextID;
         active = true;
     }
@@ -274,6 +276,23 @@ public class KJASAppletContext implement
         {
             Main.protocol.sendShowStatusCmd( myID, message );
         }
+    }
+    public String evaluateJavaScript(String script) {
+        if( active && (script != null) )
+            Main.protocol.sendEvaluateJavaScriptCmd(myID, script);
+        return null; // for now
+    }
+    public void setStream(String key, InputStream stream) throws IOException {
+        Main.debug("setStream, key = " + key);
+        streams.put(key, stream);
+    }
+    public InputStream getStream(String key){
+        Main.debug("getStream, key = " + key);
+        return (InputStream) streams.get(key);
+    }
+    public Iterator getStreamKeys() {
+        Main.debug("getStreamKeys");
+        return streams.keySet().iterator();
     }
 
 }
--- java/org/kde/kjas/server/KJASProtocolHandler.java	2001/02/14 18:24:01	1.25
+++ java/org/kde/kjas/server/KJASProtocolHandler.java	2002/04/07 17:19:14
@@ -27,6 +27,7 @@ public class KJASProtocolHandler
     private static final int GetURLDataCode      = 12;
     private static final int URLDataCode         = 13;
     private static final int ShutdownServerCode  = 14;
+    private static final int EvalulateJavaScript = 15;
 
     //Holds contexts in contextID-context pairs
     private Hashtable contexts;
@@ -421,7 +422,32 @@ public class KJASProtocolHandler
 
         signals.print( chars );
     }
+    public void sendEvaluateJavaScriptCmd( String contextID, String script )
+    {
+        Main.debug( "sendEvaluateJavaScriptCmd, contextID = " + contextID + " script = " + script );
+
+        int length = contextID.length() + script.length() + 4;
+        char[] chars = new char[ length + 8 ]; //for length of message
+        char[] tmpchar = getPaddedLength( length );
+        int index = 0;
+
+        System.arraycopy( tmpchar, 0, chars, index, tmpchar.length );
+        index += tmpchar.length;
+        chars[index++] = (char) EvalulateJavaScript;
+        chars[index++] = sep;
+
+        tmpchar = contextID.toCharArray();
+        System.arraycopy( tmpchar, 0, chars, index, tmpchar.length );
+        index += tmpchar.length;
+        chars[index++] = sep;
+
+        tmpchar = script.toCharArray();
+        System.arraycopy( tmpchar, 0, chars, index, tmpchar.length );
+        index += tmpchar.length;
+        chars[index++] = sep;
 
+        signals.print( chars );
+    }
     /**************************************************************
      *****  Utility functions for parsing commands ****************
      **************************************************************/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: jsobjects.tar.gz
Type: application/x-gunzip
Size: 1043 bytes
Desc: 
URL: <https://mail.kde.org/mailman/private/kfm-devel/attachments/20020407/e3e0b564/attachment.bin>


More information about the kfm-devel mailing list