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.