Results 1 to 3 of 3

Thread: Problem with QHttp's data encoding.

  1. #1
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem with QHttp's data encoding.

    Hello QtCentre members. I'm experiencing problems with data encoding. Let me describe my problem. I have done launcher for some game with Qt that has few simple QWebView elements to lookup news, servers status etc. and .dll injector for custom stuff for client. And now I wanted to implement auto updater feature. It works like parse ./data/version and http://url/version files then cast both buffers into integer values and compare them. If version on server is higher than version of client then it automatically hides launcher and downloads new package with QHttp object and after download is complete it calls function which saves buffer into file and calls 7z.exe to extract archive. Everything works here as I wanted to except for retrevied object's encoding. First both archives had same size (exact to byte) but after opening both in Notepad++ some of characters look different, I tried to change data stream's encoding to UTF8 with setCodec, but after that retrieved file is about 4 Mb larger than the file on server. Have you guys any suggestion how do I solve this problem?

    Here's my code:
    Qt Code:
    1. #include "updater.h"
    2.  
    3. Updater::Updater(QWidget *parent) : QDialog(parent), ui(new Ui::Updater)
    4. {
    5. _parent = parent;
    6. params << "-y" << "x" << "*******.Launcher.tmp";
    7.  
    8. ui->setupUi(this);
    9. ui->pBar->setRange(0, 3);
    10.  
    11. QTimer::singleShot(0, this, SLOT( versionCheck() ));
    12. }
    13.  
    14. Updater::~Updater()
    15. {
    16. if( http )
    17. delete http;
    18.  
    19. delete this;
    20. }
    21.  
    22. int Updater::getCurrentVersion()
    23. {
    24. int ret = 0;
    25. QFile *file = new QFile("./data/version");
    26. if( file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text) ) {
    27. QTextStream stream(file);
    28. ret = atoi(stream.readLine().toLatin1().data());
    29. file->close();
    30. }
    31.  
    32. return ret;
    33. }
    34.  
    35. void Updater::updateCurrentVersion()
    36. {
    37. //gotta fix this one later
    38. QFile *file = new QFile("./data/version");
    39. if( file->exists() && file->open(QIODevice::WriteOnly | QIODevice::Text) ) {
    40. QTextStream s(file);
    41. s << (int) getCurrentVersion() + 1;
    42. file->close();
    43. }
    44. }
    45.  
    46. void Updater::versionCheck()
    47. {
    48. ui->pBar->setValue(1);
    49. ui->pLabel->setText("Checking for updates...");
    50.  
    51. http = new QHttp();
    52. http->setHost("launcher.*******.pl", 80);
    53. http->get("/version");
    54. connect(http, SIGNAL( done(bool) ), this, SLOT( finishVersionCheck() ));
    55.  
    56. ui->pBar->setValue(2);
    57. ui->pLabel->setText("Retrieving data from server...");
    58. }
    59.  
    60. void Updater::finishVersionCheck()
    61. {
    62. ui->pBar->setValue(3);
    63. ui->pLabel->setText("Checking version...");
    64.  
    65. if( atoi(http->readAll().data()) > getCurrentVersion() ) {
    66. if( QMessageBox::question(this, "Updater", "There is an update available.\nWould you like to download it now?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes ) {
    67. ui->pBar->setValue(0);
    68. ui->pLabel->setText("Downloading...");
    69.  
    70. http = new QHttp();
    71. http->setHost("launcher.*******.pl", 80);
    72. http->get("/test.7z");
    73. connect(http, SIGNAL( done(bool) ), this, SLOT( finishDownload() ));
    74. connect(http, SIGNAL( dataReadProgress(int, int) ), this, SLOT( updateProgress(int, int) ));
    75. }
    76. else {
    77. _parent->close();
    78. }
    79. }
    80. else {
    81. _parent->show();
    82. close();
    83. }
    84. }
    85.  
    86. void Updater::finishDownload()
    87. {
    88. QFile *file = new QFile("*******.Launcher.tmp");
    89. if( file->exists() )
    90. file->remove();
    91.  
    92. if( !file->open(QIODevice::WriteOnly | QIODevice::Text) ) {
    93. QMessageBox::critical(this, "Updater", "Failed to open file.");
    94. return;
    95. }
    96.  
    97. QTextStream data(file);
    98. data.setCodec("UTf-8");
    99. data << http->readAll();
    100. file->close();
    101.  
    102. QProcess *proc = new QProcess();
    103. proc->start("7z.exe", params);
    104. delete proc;
    105.  
    106. //file->remove();
    107. updateCurrentVersion();
    108. QMessageBox::information(this, "Updater", "New version has been downloaded.");
    109.  
    110. _parent->show();
    111. close();
    112. delete file;
    113. }
    114.  
    115. void Updater::updateProgress(int done, int total)
    116. {
    117. ui->pBar->setRange(0, total);
    118. ui->pBar->setValue(done);
    119.  
    120. QString text;
    121. text.sprintf("Downloading: %d/%d kb (%3.2f%%)", done / 1024, total / 1024, (done / (double) total) * 100.0);
    122. ui->pLabel->setText(text);
    123. }
    To copy to clipboard, switch view to plain text mode 

    Regards, Diath.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with QHttp's data encoding.

    QHttp is obsolete and should not be used in new code

    You are requesting a binary file and then telling Qt that it is a text file (line 92) in UTF-8 encoding (line 97-98). At the very least the CR and LF characters may be mangled. The QTextStream may also remove or substitute bytes sequences that are not valid UTF-8. Just open the output file and use QIODevice::write() to output the QByteArray you get from readAll(). No need for a QTextStream.

    BTW: Lines 25, 38 and 88 are memory leaks. There's no need to allocate QFile objects on the heap.

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

    Diath (3rd July 2011)

  4. #3
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QHttp's data encoding.

    So I have changed QTextStream to file.write(http->readAll()) but still after download is compile I'm not able to open the archive (Archive is corrupt~.).
    Here's my current code:
    Qt Code:
    1. void Updater::finishDownload()
    2. {
    3. QFile file("Launcher.tmp");
    4. if( file.exists() )
    5. file.remove();
    6.  
    7. if( !file.open(QIODevice::WriteOnly | QIODevice::Text) ) {
    8. QMessageBox::critical(this, "Updater", "Failed to open file.");
    9. return;
    10. }
    11.  
    12. file.write(http->readAll());
    13. file.close();
    14.  
    15. QProcess proc;
    16. proc.start("7z.exe", params);
    17.  
    18. //file->remove();
    19. updateCurrentVersion();
    20. QMessageBox::information(this, "Updater", "New version has been downloaded.");
    21.  
    22. _parent->show();
    23. close();
    24. }
    To copy to clipboard, switch view to plain text mode 

    Any other tips?

    @Edit:
    Oh, dumb me. QIODevice::Text as you said, of course. Problem solved, thanks Chris.
    Last edited by Diath; 3rd July 2011 at 22:09.

Similar Threads

  1. encoding problem
    By lexqqq in forum Qt Programming
    Replies: 1
    Last Post: 26th November 2010, 23:41
  2. Replies: 0
    Last Post: 21st July 2010, 21:55
  3. Encoding problem with QTextEdit
    By alexandernst in forum Newbie
    Replies: 3
    Last Post: 2nd September 2009, 11:49
  4. QTableView encoding problem
    By SudaNix in forum Newbie
    Replies: 7
    Last Post: 26th February 2008, 14:19
  5. qt and mysql encoding problem
    By ferasodh in forum Qt Programming
    Replies: 1
    Last Post: 8th September 2007, 09:48

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.