SilentEye 0.4.1

updatedialog.cpp

Go to the documentation of this file.
00001 //  This file is part of SilentEye.
00002 //
00003 //  SilentEye is free software: you can redistribute it and/or modify
00004 //  it under the terms of the GNU General Public License as published by
00005 //  the Free Software Foundation, either version 3 of the License, or
00006 //  (at your option) any later version.
00007 //
00008 //  SilentEye is distributed in the hope that it will be useful,
00009 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //  GNU General Public License for more details.
00012 //
00013 //  You should have received a copy of the GNU General Public License
00014 //  along with SilentEye. If not, see <http://www.gnu.org/licenses/>.
00015 
00016 #include "updatedialog.h"
00017 
00018 #include "controller.h"
00019 #include <config.h>
00020 
00021 namespace SilentEye {
00022 
00023     UpdateDialog::UpdateDialog(QWidget* parent) : QDialog(parent)
00024     {
00025         setupUi(this);
00026         setObjectName("UpdateDialog");
00027         m_logger = new Logger(this);
00028 
00029         Config version(Controller::instance()->appPath, "version.xml", true);
00030         if (version.isLoaded()) {
00031             currentVersionLabel->setText(version.get("name"));
00032             currentDateLabel->setText(version.get("date"));
00033         } else {
00034             currentVersionLabel->setText("Unknown");
00035             currentDateLabel->setText("Unknown");
00036         }
00037         linkLabel->setText("<a href='http://www.silenteye.org/download.html?i2&referer=update-default'>http://www.silenteye.org/download.html</a>");
00038     }
00039 
00040     UpdateDialog::~UpdateDialog()
00041     {
00042         if (!m_logger.isNull()) {
00043             delete m_logger;
00044         }
00045     }
00046 
00047     void UpdateDialog::showEvent(QShowEvent* event)
00048     {
00049         if(!event->spontaneous())
00050         {
00051             progressBar->setMaximum(0);
00052             progressBar->setValue(-1);
00053             download(QUrl(Controller::instance()->config.get("updatelink")));
00054         }
00055     }
00056 
00057     void UpdateDialog::download(QUrl url)
00058     {
00059 
00060         m_url = url;
00061         m_logger->debug("Download: " + m_url.toString());
00062         m_buffer = "";
00063         m_reply = Controller::instance()->networkManager.get(QNetworkRequest(url));
00064         connect(m_reply, SIGNAL(finished()),
00065                 this, SLOT(httpFinished()));
00066         connect(m_reply, SIGNAL(readyRead()),
00067                 this, SLOT(httpReadyRead()));
00068     }
00069 
00070     void UpdateDialog::httpReadyRead()
00071     {
00072         m_buffer += QString(m_reply->readAll()).trimmed();
00073     }
00074 
00075     void UpdateDialog::httpFinished()
00076     {
00077         m_logger->debug("Finished");
00078         progressBar->setMaximum(100);
00079         QVariant redirectionTarget = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
00080         if (m_reply->error()) {
00081             m_logger->error("Download failed: " + m_reply->errorString());
00082             progressBar->setValue(0);
00083 
00084             newVersionLabel->setText("error...");
00085             dateLabel->setText("error...");
00086             releaseBrowser->setText("check failed: " + m_reply->errorString());
00087             linkLabel->setText("<a href='http://www.silenteye.org/download.html?i2&referer=update-error'>http://www.silenteye.org/download.html</a>");
00088         } else if (!redirectionTarget.isNull()) {
00089             QUrl newUrl = m_url.resolved(redirectionTarget.toUrl());
00090             m_logger->debug("redirect: " + newUrl.toString());
00091             m_reply->deleteLater();
00092             download(newUrl);
00093             return;
00094         } else {
00095             progressBar->setValue(100);
00096             Config conf(m_buffer);
00097 
00098             newVersionLabel->setText(conf.get("name"));
00099             dateLabel->setText(conf.get("date"));
00100             releaseBrowser->setText(conf.get("releasenote").trimmed());
00101 #if defined(Q_WS_WIN)
00102             linkLabel->setText("<a href='" + conf.get("link-win") + "'>"
00103                                + conf.get("link-win") + "</a>");
00104 #elif defined(Q_WS_X11)
00105             linkLabel->setText("<a href='" + conf.get("link-lin") + "'>"
00106                                + conf.get("link-lin") + "</a>");
00107 #elif defined(Q_WS_MAC)
00108             linkLabel->setText("<a href='" + conf.get("link-mac") + "'>"
00109                                + conf.get("link-mac") + "</a>");
00110 #else
00111             linkLabel->setText("<a href='http://www.silenteye.org/download.html?i2&referer=update-platform-unknown'>http://www.silenteye.org/download.html</a>");
00112 #endif
00113         }
00114 
00115         m_reply->deleteLater();
00116     }
00117 
00118 
00119 }