Results 1 to 4 of 4

Thread: Qt Jambi Webkit Custom Protocol

  1. #1
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    1
    Qt products
    Qt Jambi
    Platforms
    Unix/X11

    Default Qt Jambi Webkit Custom Protocol

    I'm trying to add a custom protocol to QtWebView called "local://". I followed an example for python I found here:

    http://www.diotavelli.net/PyQtWiki/U...ith%20QtWebKit

    and converted it to Java. It kind of works, but there is some sort of character encoding issues I can't figure out. Here is the example Java code I created....

    LocalNetworkReply:

    Qt Code:
    1. public class LocalNetworkReply extends QNetworkReply {
    2.  
    3. private String content;
    4. private int offset = 0;
    5.  
    6. public LocalNetworkReply(Operation op, QNetworkRequest request, QIODevice outgoingData){
    7. super();
    8.  
    9. this.content = "<html><head><title>Test</title></head><body>This is a test.</body></html>";
    10. this.offset = 0;
    11.  
    12. this.setHeader(QNetworkRequest.KnownHeaders.ContentTypeHeader, "text/html; charset=UTF-8");
    13. this.setHeader(QNetworkRequest.KnownHeaders.ContentLengthHeader, (this.content.length()));
    14.  
    15.  
    16. QIODevice.OpenModeFlag[] flags = new QIODevice.OpenModeFlag[2];
    17. flags[0] = QIODevice.OpenModeFlag.ReadOnly;
    18. flags[1] = QIODevice.OpenModeFlag.Unbuffered;
    19.  
    20. QTimer.singleShot(0, this, "readyRead()");
    21. QTimer.singleShot(0, this, "finished()");
    22.  
    23. this.open(flags);
    24. this.setUrl(request.url());
    25. }
    26.  
    27. @Override
    28. public boolean isSequential() {
    29. return true;
    30. }
    31.  
    32. @Override
    33. public long bytesAvailable() {
    34. return this.content.getBytes().length - this.offset;
    35. }
    36.  
    37. @Override
    38. public void abort() {
    39. System.out.println("Abort called");
    40. }
    41.  
    42. @Override
    43. protected int readData(byte[] data) {
    44. try {
    45. data = this.content.getBytes("UTF-8");
    46. System.out.println(new String(data,"UTF-8"));
    47. System.out.println(data.length);
    48. System.out.println(this.content.getBytes().length);
    49. } catch (UnsupportedEncodingException e) {
    50. // TODO Auto-generated catch block
    51. e.printStackTrace();
    52. }
    53. return data.length;
    54. }
    55. }
    To copy to clipboard, switch view to plain text mode 

    LocalNetworkAccessManager:

    Qt Code:
    1. public class LocalNetworkAccessManager extends QNetworkAccessManager {
    2.  
    3. public LocalNetworkAccessManager(){
    4. super();
    5. }
    6.  
    7. public LocalNetworkAccessManager(QObject qObject){
    8. super(qObject);
    9.  
    10.  
    11. }
    12.  
    13. @Override
    14. protected QNetworkReply createRequest(Operation op,
    15. QNetworkRequest request, QIODevice outgoingData) {
    16.  
    17. if(request.url().scheme().equalsIgnoreCase("local") && op.compareTo(QNetworkAccessManager.Operation.GetOperation) == 0){
    18. LocalNetworkReply lnr = new LocalNetworkReply(op, request, outgoingData);
    19. return lnr;
    20. }else{
    21. return super.createRequest(op, request, outgoingData);
    22. }
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    Application Code:
    Qt Code:
    1. .....
    2. browser = new QWebView();
    3. browser.page().setNetworkAccessManager(new LocalNetworkAccessManager());
    4. .....
    5. browser.load(new QUrl("local://index.html"));
    6. .....
    To copy to clipboard, switch view to plain text mode 

    index.html
    Qt Code:
    1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    2. <html>
    3. <head>
    4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    5. <title>Hello World</title>
    6. </head>
    7. <body>
    8. <h1>Hello World!</h1>
    9. </body>
    10. </html>
    To copy to clipboard, switch view to plain text mode 

    When I run the application, I get a bunch of garbage characters like this:

    6Pj� ��� 0�����+ l� ��벼%��ڨ�p��%���k� 

    Could this be a bug in Qt?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt Jambi Webkit Custom Protocol

    Not likely. I would start by making this.content a byte array and not a java string. I'm also not sure that your readData() implementation is correct. It should overwrite the content of the argument passed to it. I don't know how to do that in Java but your code doesn't seem to do it (to me it looks like you overwrite the variable and not its content). Also readData() should take an integer with a maximum size of data to read.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    tgl (2nd July 2009)

  4. #3
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    1
    Qt products
    Qt Jambi
    Platforms
    Unix/X11

    Default Re: Qt Jambi Webkit Custom Protocol

    The method signature is correct based on the javadoc. I took your suggestion about writing the data into the method parameter as opposed to what I was previously doing. I changed the code in LocalNetworkReply to this:

    Qt Code:
    1. @Override
    2. protected int readData(byte[] data) {
    3.  
    4. try {
    5. byte[] cBytes = this.content.getBytes("UTF-8");
    6. for(int i=0; i<cBytes.length; i++){
    7. data[i] = cBytes[i];
    8. }
    9.  
    10. System.out.println(new String(data,"UTF-8"));
    11. System.out.println(data.length);
    12. System.out.println(this.content.getBytes().length);
    13. } catch (UnsupportedEncodingException e) {
    14. // TODO Auto-generated catch block
    15. e.printStackTrace();
    16. }
    17. return data.length;
    18. }
    To copy to clipboard, switch view to plain text mode 

    Using this code gives me the correct output. I will test a little more, but I think this fixed the issue I was seeing. Thanks for your help.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt Jambi Webkit Custom Protocol

    Isn't there a java equivalent of memcpy() or qstrncpy()?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QUdpSocket error
    By mdecandia in forum Qt Programming
    Replies: 8
    Last Post: 25th October 2007, 10:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.