downloader.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 6733 $                                                         *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2009-08-05 09:32:32 +0200 (Mi, 05. Aug 2009) $                   *
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    // If username or passowrd are supplied, use them
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   // Download the file to the Home Directory
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:                   // Ok
00154   case 301:                   // Moved Permanently
00155   case 302:                   // Found
00156   case 303:                   // See Other
00157   case 307:                   // Temporary Redirect
00158     // these are not error conditions
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 

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .