downloader.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include "optionsWidget.hh"
00047 #include <iostream>
00048 #include <OpenFlipper/common/GlobalOptions.hh>
00049
00050 void OptionsWidget::startDownload( QString _url ) {
00051 QUrl url(_url);
00052
00053
00054 if ( ! updateUser->text().isEmpty() )
00055 url.setUserName(updateUser->text());
00056
00057 if ( ! updatePass->text().isEmpty() )
00058 url.setPassword(updatePass->text());
00059
00060
00061 QFileInfo urlInfo(_url);
00062
00063
00064 QFileInfo fileInfo( QDir::home().absolutePath() + OpenFlipper::Options::dirSeparator() +
00065 ".OpenFlipper" + OpenFlipper::Options::dirSeparator() + urlInfo.fileName() );
00066
00067 QString fileName = fileInfo.filePath();
00068
00069 if (QFile::exists(fileName)) {
00070 QFile::remove(fileName);
00071 }
00072
00073 file = new QFile(fileName);
00074 if (!file->open(QIODevice::WriteOnly)) {
00075 std::cerr << "Unable to Open local file " + fileName.toStdString() + " for writing" << std::endl;
00076 delete file;
00077 file = 0;
00078 checkUpdateButton->setEnabled(true);
00079 } else {
00080 QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
00081 http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());
00082
00083 if (!url.userName().isEmpty())
00084 http->setUser(url.userName(), url.password());
00085
00086 httpRequestAborted = false;
00087 QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/");
00088 if (path.isEmpty())
00089 path = "/";
00090
00091 statusLabel->setText(tr("Getting Versions file from Server"));
00092
00093 progressDialog->setWindowTitle(tr("HTTP"));
00094 progressDialog->setLabelText(tr("Downloading %1.").arg(fileName));
00095 progressDialog->show();
00096
00097 httpGetId = http->get(path, file);
00098
00099 checkUpdateButton->setEnabled(false);
00100 }
00101
00102 }
00103
00104 void OptionsWidget::httpRequestFinished(int requestId, bool error)
00105 {
00106 if (requestId != httpGetId)
00107 return;
00108 if (httpRequestAborted) {
00109 if (file) {
00110 file->close();
00111 file->remove();
00112 delete file;
00113 file = 0;
00114 }
00115
00116 progressDialog->hide();
00117 checkUpdateButton->setEnabled(true);
00118 return;
00119 }
00120
00121 if (requestId != httpGetId)
00122 return;
00123
00124 progressDialog->hide();
00125 file->close();
00126
00127 if (error) {
00128 file->remove();
00129 statusLabel->setText(tr("Download failed: %1.").arg(http->errorString()));
00130 QMessageBox::information(this, tr("HTTP"),
00131 tr("Download failed: %1.")
00132 .arg(http->errorString()) + file->fileName() );
00133 } else {
00134 QString fileName = QFileInfo(QUrl(updateURL->text()).path()).fileName();
00135 statusLabel->setText(tr("Downloaded %1").arg(file->fileName() ));
00136 }
00137
00138 checkUpdateButton->setEnabled(true);
00139 delete file;
00140 file = 0;
00141
00142 if ( !error ) {
00143 if ( downloadType == VERSIONS_FILE )
00144 compareVersions();
00145 if ( downloadType == PLUGIN )
00146 updateComponent();
00147 }
00148 }
00149
00150 void OptionsWidget::readResponseHeader(const QHttpResponseHeader &responseHeader)
00151 {
00152 switch (responseHeader.statusCode()) {
00153 case 200:
00154 case 301:
00155 case 302:
00156 case 303:
00157 case 307:
00158
00159 break;
00160
00161 default:
00162 QMessageBox::information(this, tr("HTTP"),
00163 tr("Download failed: %1.")
00164 .arg(responseHeader.reasonPhrase()));
00165 statusLabel->setText(tr("Download failed: ") + responseHeader.reasonPhrase());
00166 httpRequestAborted = true;
00167 progressDialog->hide();
00168 http->abort();
00169 }
00170 }
00171
00172 void OptionsWidget::cancelDownload()
00173 {
00174 statusLabel->setText(tr("download canceled."));
00175 httpRequestAborted = true;
00176 http->abort();
00177 checkUpdateButton->setEnabled(true);
00178 }
00179
00180 void OptionsWidget::updateDataReadProgress(int bytesRead, int totalBytes)
00181 {
00182 if (httpRequestAborted)
00183 return;
00184
00185 progressDialog->setMaximum(totalBytes);
00186 progressDialog->setValue(bytesRead);
00187 }
00188
00189
00190
00191
00192